func (glRenderer *OpenglRenderer) loadTexture(img image.Image, textureUnit uint32, lod bool) uint32 { rgba := image.NewRGBA(img.Bounds()) if rgba.Stride != rgba.Rect.Size().X*4 { log.Fatal("unsupported stride") } draw.Draw(rgba, rgba.Bounds(), img, image.Point{0, 0}, draw.Src) var texId uint32 gl.GenTextures(1, &texId) gl.ActiveTexture(textureUnit) gl.BindTexture(gl.TEXTURE_2D, texId) if rgba.Rect.Size().X == 0 || rgba.Rect.Size().Y == 0 { return texId } gl.TexImage2D( gl.TEXTURE_2D, 0, gl.RGBA, int32(rgba.Rect.Size().X), int32(rgba.Rect.Size().Y), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(rgba.Pix), ) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) 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) if lod { gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR) gl.GenerateMipmap(gl.TEXTURE_2D) } else { gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR) } return texId }
func (glRenderer *OpenglRenderer) loadCubeMap(right, left, top, bottom, back, front image.Image, textureUnit uint32, lod bool) uint32 { var texId uint32 gl.GenTextures(1, &texId) gl.ActiveTexture(textureUnit) gl.BindTexture(gl.TEXTURE_CUBE_MAP, texId) for i := 0; i < 6; i++ { img := right var texIndex uint32 = gl.TEXTURE_CUBE_MAP_POSITIVE_X switch i { case 1: img = left texIndex = gl.TEXTURE_CUBE_MAP_NEGATIVE_X case 2: img = top texIndex = gl.TEXTURE_CUBE_MAP_NEGATIVE_Y case 3: img = bottom texIndex = gl.TEXTURE_CUBE_MAP_POSITIVE_Y case 4: img = back texIndex = gl.TEXTURE_CUBE_MAP_NEGATIVE_Z case 5: img = front texIndex = gl.TEXTURE_CUBE_MAP_POSITIVE_Z } img = imaging.FlipV(img) rgba := image.NewRGBA(img.Bounds()) if rgba.Stride != rgba.Rect.Size().X*4 { log.Fatal("unsupported stride") } draw.Draw(rgba, rgba.Bounds(), img, image.Point{0, 0}, draw.Src) gl.TexImage2D( texIndex, 0, gl.RGBA, int32(rgba.Rect.Size().X), int32(rgba.Rect.Size().Y), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(rgba.Pix), ) gl.TexParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MAG_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE) gl.TexParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE) gl.TexParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_R, gl.CLAMP_TO_EDGE) if lod { gl.TexParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR) gl.GenerateMipmap(gl.TEXTURE_CUBE_MAP) } else { gl.TexParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MIN_FILTER, gl.LINEAR) } } return texId }
func NewTexture(img image.Image, wrapR, wrapS int32) (*Texture, error) { rgba := image.NewRGBA(img.Bounds()) draw.Draw(rgba, rgba.Bounds(), img, image.Pt(0, 0), draw.Src) if rgba.Stride != rgba.Rect.Size().X*4 { // TODO-cs: why? return nil, errUnsupportedStride } var handle uint32 gl.GenTextures(1, &handle) target := uint32(gl.TEXTURE_2D) internalFmt := int32(gl.SRGB_ALPHA) format := uint32(gl.RGBA) width := int32(rgba.Rect.Size().X) height := int32(rgba.Rect.Size().Y) pixType := uint32(gl.UNSIGNED_BYTE) dataPtr := gl.Ptr(rgba.Pix) texture := Texture{ handle: handle, target: target, } texture.Bind(gl.TEXTURE0) defer texture.UnBind() // set the texture wrapping/filtering options (applies to current bound texture obj) // TODO-cs gl.TexParameteri(texture.target, gl.TEXTURE_WRAP_R, wrapR) gl.TexParameteri(texture.target, gl.TEXTURE_WRAP_S, wrapS) gl.TexParameteri(texture.target, gl.TEXTURE_MIN_FILTER, gl.LINEAR) // minification filter gl.TexParameteri(texture.target, gl.TEXTURE_MAG_FILTER, gl.LINEAR) // magnification filter gl.TexImage2D(target, 0, internalFmt, width, height, 0, format, pixType, dataPtr) gl.GenerateMipmap(texture.handle) return &texture, nil }