// 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 }
// 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.") } }