func (v *Video) initGL() { if err := gl.Init(); err != nil { panic(err) } gl.Enable(gl.CULL_FACE) gl.Enable(gl.DEPTH_TEST) gl.ClearColor(0.0, 0.0, 0.0, 1.0) v.prog = createProgram(vertShaderSrcDef, fragShaderSrcDef) posAttrib := uint32(gl.GetAttribLocation(v.prog, gl.Str("vPosition"+"\x00"))) texCoordAttr := uint32(gl.GetAttribLocation(v.prog, gl.Str("vTexCoord"+"\x00"))) v.textureUni = gl.GetAttribLocation(v.prog, gl.Str("texture"+"\x00")) var texture uint32 gl.GenTextures(1, &texture) gl.ActiveTexture(gl.TEXTURE0) gl.BindTexture(gl.TEXTURE_2D, texture) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST) gl.UseProgram(v.prog) gl.EnableVertexAttribArray(posAttrib) gl.EnableVertexAttribArray(texCoordAttr) //posAttrib.EnableArray() //texCoordAttr.EnableArray() var vbo uint32 gl.GenBuffers(1, &vbo) gl.BindBuffer(gl.ARRAY_BUFFER, vbo) verts := []float32{-1.0, 1.0, -1.0, -1.0, 1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0} gl.BufferData(gl.ARRAY_BUFFER, len(verts)*int(unsafe.Sizeof(verts[0])), gl.Ptr(verts), gl.STATIC_DRAW) var textCoorBuf uint32 gl.GenBuffers(1, &textCoorBuf) gl.BindBuffer(gl.ARRAY_BUFFER, textCoorBuf) texVerts := []float32{0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0} gl.BufferData(gl.ARRAY_BUFFER, len(texVerts)*int(unsafe.Sizeof(texVerts[0])), gl.Ptr(texVerts), gl.STATIC_DRAW) gl.VertexAttribPointer(posAttrib, 2, gl.FLOAT, false, 0, gl.PtrOffset(0)) gl.VertexAttribPointer(texCoordAttr, 2, gl.FLOAT, false, 0, gl.PtrOffset(0)) //posAttrib.AttribPointer(2, gl.FLOAT, false, 0, uintptr(0)) //texCoordAttr.AttribPointer(2, gl.FLOAT, false, 0, uintptr(0)) }
func NewTexture(img *image.RGBA, opts ...TextureOption) (*Texture, error) { if img.Stride != img.Rect.Size().X*4 { return nil, fmt.Errorf("unsupported stride in texture image") } opt := textureOption{ filterMin: LINEAR, filterMag: LINEAR, wrap_s: REPEAT, wrap_t: REPEAT, } for _, o := range opts { o(&opt) } var id uint32 gl.GenTextures(1, &id) gl.ActiveTexture(gl.TEXTURE0) gl.BindTexture(gl.TEXTURE_2D, id) defer gl.BindTexture(gl.TEXTURE_2D, 0) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, int32(opt.filterMin)) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, int32(opt.filterMag)) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, int32(opt.wrap_s)) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, int32(opt.wrap_t)) gl.TexImage2D( gl.TEXTURE_2D, 0, gl.RGBA, int32(img.Rect.Size().X), int32(img.Rect.Size().Y), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(img.Pix)) return &Texture{ id: id, size: img.Rect.Size(), }, nil }
// ActiveTexture sets the active texture unit. // // http://www.khronos.org/opengles/sdk/docs/man3/html/glActiveTexture.xhtml func ActiveTexture(texture Enum) { gl.ActiveTexture(uint32(texture)) }
func (v *Video) Render() { for running { select { case buf := <-v.videoTick: gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.UseProgram(v.prog) gl.ActiveTexture(gl.TEXTURE0) gl.BindTexture(v.texture, gl.TEXTURE_2D) gl.TexImage2D(gl.TEXTURE_2D, 0, 3, 240, 224, 0, gl.RGBA, gl.UNSIGNED_INT_8_8_8_8, gl.Ptr(buf)) gl.DrawArrays(gl.TRIANGLES, 0, 6) if v.screen != nil { sdl.GL_SwapBuffers() v.fpsmanager.FramerateDelay() } case ev := <-sdl.Events: switch e := ev.(type) { case sdl.ResizeEvent: v.ResizeEvent(e.W, e.H) case sdl.QuitEvent: os.Exit(0) case sdl.KeyboardEvent: switch e.Keysym.Sym { case sdl.K_ESCAPE: running = false case sdl.K_r: // Trigger reset interrupt if e.Type == sdl.KEYDOWN { // cpu.RequestInterrupt(InterruptReset) } case sdl.K_l: if e.Type == sdl.KEYDOWN { nes.LoadGameState() } case sdl.K_s: if e.Type == sdl.KEYDOWN { nes.SaveGameState() } case sdl.K_i: if e.Type == sdl.KEYDOWN { nes.AudioEnabled = !nes.AudioEnabled } case sdl.K_p: if e.Type == sdl.KEYDOWN { nes.TogglePause() } case sdl.K_d: if e.Type == sdl.KEYDOWN { jsHandler.ReloadFile(debugfile) } case sdl.K_m: if e.Type == sdl.KEYDOWN { nes.Handler.Handle("debug-mode") } case sdl.K_BACKSLASH: if e.Type == sdl.KEYDOWN { nes.Pause() nes.StepFrame() } case sdl.K_1: if e.Type == sdl.KEYDOWN { v.ResizeEvent(256, 240) } case sdl.K_2: if e.Type == sdl.KEYDOWN { v.ResizeEvent(512, 480) } case sdl.K_3: if e.Type == sdl.KEYDOWN { v.ResizeEvent(768, 720) } case sdl.K_4: if e.Type == sdl.KEYDOWN { v.ResizeEvent(1024, 960) } } switch e.Type { case sdl.KEYDOWN: nes.Pads[0].KeyDown(e, 0) case sdl.KEYUP: nes.Pads[0].KeyUp(e, 0) } } } } }
func (*backend) ActiveTexture(tex gg.Enum) { gl.ActiveTexture(uint32(tex)) }
func (texture *Texture) Use(unit int) { gl.ActiveTexture(gl.TEXTURE0 + uint32(unit)) gl.BindTexture(gl.TEXTURE_2D, texture.id) }