// Implements Renderer interface. func (gc *opengl) BindTexture(texture *data.Texture) (err error) { if texture == nil { return } if glerr := gl.GetError(); glerr != gl.NO_ERROR { log.Printf("opengl:bindTexture need to find and fix prior error %X", glerr) } gl.GenTextures(1, &(texture.Tid)) gl.BindTexture(gl.TEXTURE_2D, texture.Tid) // ensure image is in RGBA format b := texture.Img.Bounds() rgba := image.NewRGBA(image.Rect(0, 0, b.Dx(), b.Dy())) draw.Draw(rgba, rgba.Bounds(), texture.Img, b.Min, draw.Src) width, height := int32(b.Dx()), int32(b.Dy()) gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Pointer(&(rgba.Pix[0]))) gl.GenerateMipmap(gl.TEXTURE_2D) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR) if glerr := gl.GetError(); glerr != gl.NO_ERROR { err = fmt.Errorf("Failed binding texture %s\n", texture.Name) } return }
// drawScene renders the scene consisting of one VAO. func (tb *tbtag) drawScene() { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.UseProgram(tb.shaders) gl.Uniform1i(tb.sampler, 0) gl.ActiveTexture(gl.TEXTURE0 + 0) gl.BindTexture(gl.TEXTURE_2D, tb.texture.Tid) gl.BindVertexArray(tb.vao) tb.mvp32 = renderMatrix(tb.ortho, tb.mvp32) gl.UniformMatrix4fv(tb.mvpref, 1, false, tb.mvp32.Pointer()) gl.DrawElements(gl.TRIANGLES, int32(len(tb.faces)), gl.UNSIGNED_BYTE, gl.Pointer(nil)) // cleanup gl.ActiveTexture(0) gl.UseProgram(0) gl.BindVertexArray(0) }
// Implements Renderer interface. func (gc *opengl) MapTexture(tid int, t *data.Texture) { tmap := map[int]uint32{ 0: gl.TEXTURE0, 1: gl.TEXTURE1, 2: gl.TEXTURE2, 3: gl.TEXTURE3, 4: gl.TEXTURE4, 5: gl.TEXTURE5, 6: gl.TEXTURE6, 7: gl.TEXTURE7, 8: gl.TEXTURE8, 9: gl.TEXTURE9, } gl.ActiveTexture(tmap[tid]) gl.BindTexture(gl.TEXTURE_2D, t.Tid) }
// initTexture loads the texture and binds it to the graphics device. func (tb *tbtag) initTexture() { texture := &data.Texture{} loader := data.NewLoader() if loader.Load("image", &texture); texture != nil { tb.texture = texture gl.GenTextures(1, &texture.Tid) gl.BindTexture(gl.TEXTURE_2D, texture.Tid) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST) // ensure image is in RGBA format b := texture.Img.Bounds() rgba := image.NewRGBA(image.Rect(0, 0, b.Dx(), b.Dy())) draw.Draw(rgba, rgba.Bounds(), texture.Img, b.Min, draw.Src) width, height := int32(b.Dx()), int32(b.Dy()) gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Pointer(&(rgba.Pix[0]))) if glerr := gl.GetError(); glerr != gl.NO_ERROR { fmt.Printf("Failed binding texture image.png\n") } } else { fmt.Println("Could not load image.png file.") } }