func loadTexture(fileName string) (uint32, error) { imgFile, err := os.Open("./res/textures/" + fileName) if err != nil { return 0, err } imgCfg, _, err := image.DecodeConfig(imgFile) if err != nil { return 0, err } _, err = imgFile.Seek(0, 0) if err != nil { return 0, err } w, h := int32(imgCfg.Width), int32(imgCfg.Height) img, _, err := image.Decode(imgFile) if err != nil { return 0, err } buffer := make([]byte, w*h*4) index := 0 for y := 0; y < int(h); y++ { for x := 0; x < int(w); x++ { pixel := img.At(x, y).(color.NRGBA) buffer[index] = pixel.R buffer[index+1] = pixel.G buffer[index+2] = pixel.B buffer[index+3] = pixel.A index += 4 } } var texture uint32 gl.GenTextures(1, &texture) gl.BindTexture(gl.TEXTURE_2D, texture) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT) gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST) gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST) gl.TexImage2D( gl.TEXTURE_2D, 0, gl.RGBA8, w, h, 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(buffer)) return texture, nil }
// Shouldn't have tabs nor newlines func (o *OpenGlStream) PrintSegment(s string) { if s == "" { return } if o.BackgroundColor != nil && o.BorderColor == nil { gl.PushAttrib(gl.CURRENT_BIT) gl.Color3dv((*float64)(&o.BackgroundColor[0])) gl.PushMatrix() gl.Translated(float64(o.pos[0]), float64(o.pos[1]), 0) for range s { gl.CallList(oFontBackground) } gl.PopMatrix() gl.PopAttrib() } gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_LOD_BIAS, float32(lodBias*0.01)) gl.Enable(gl.BLEND) defer gl.Disable(gl.BLEND) gl.Enable(gl.TEXTURE_2D) defer gl.Disable(gl.TEXTURE_2D) gl.PushMatrix() gl.Translated(float64(o.pos[0]), float64(o.pos[1]), 0) gl.ListBase(oFontBase + uint32(o.FontOptions)*96) gl.CallLists(int32(len(s)), gl.UNSIGNED_BYTE, gl.Ptr(&[]byte(s)[0])) gl.PopMatrix() //CheckGLError() }
func (texture *Texture) SetMipmapSharpness(sharpness float32) { var maxMipmapSharpness float32 gl.GetFloatv(gl.MAX_TEXTURE_LOD_BIAS, &maxMipmapSharpness) mipmapSharpness := math.Min(math.Max(float64(sharpness), -float64(maxMipmapSharpness+0.01)), float64(maxMipmapSharpness-0.01)) bindTexture(texture.getHandle()) //negative bias is sharper gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_LOD_BIAS, -float32(mipmapSharpness)) }
func LoadTexture(path string) { //fmt.Printf("Trying to load texture %q: ", path) // Open the file file, err := os.Open(path) if err != nil { fmt.Println(os.Getwd()) log.Fatal(err) } defer file.Close() // Decode the image img, _, err := image.Decode(file) if err != nil { log.Fatal(err) } bounds := img.Bounds() //fmt.Printf("Loaded %vx%v texture.\n", bounds.Dx(), bounds.Dy()) var format int var pixPointer *uint8 switch img := img.(type) { case *image.RGBA: format = gl.RGBA pixPointer = &img.Pix[0] case *image.NRGBA: format = gl.RGBA pixPointer = &img.Pix[0] case *image.Gray: format = gl.ALPHA pixPointer = &img.Pix[0] default: log.Fatalf("LoadTexture: Unsupported type %T.\n", img) } var texture uint32 gl.GenTextures(1, &texture) gl.BindTexture(gl.TEXTURE_2D, texture) gl.TexParameteri(gl.TEXTURE_2D, gl.GENERATE_MIPMAP, gl.TRUE) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_LOD_BIAS, -0.5) gl.TexImage2D(gl.TEXTURE_2D, 0, int32(format), int32(bounds.Dx()), int32(bounds.Dy()), 0, uint32(format), gl.UNSIGNED_BYTE, gl.Ptr(pixPointer)) CheckGLError() }
// TexParameterf sets a float texture parameter. // // http://www.khronos.org/opengles/sdk/docs/man3/html/glTexParameter.xhtml func TexParameterf(target, pname Enum, param float32) { gl.TexParameterf(uint32(target), uint32(pname), param) }