func (t *TextureData) LoadIntoGL() { t.id.Bind(gl.TEXTURE_2D) //gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) //gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR) gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, t.Width(), t.Height(), 0, gl.RGBA, gl.UNSIGNED_BYTE, t.Pix.Pix) for i, v := range t.Mips { gl.TexImage2D(gl.TEXTURE_2D, i+1, gl.RGBA, v.w, v.h, 0, gl.RGBA, gl.UNSIGNED_BYTE, v.Pix) } }
func loadTexture(path string) (*gl.Texture, error) { file, err := os.Open(path) if err != nil { } defer file.Close() srcimg, _, err := image.Decode(file) if err != nil { return nil, err } // convert to RGBA if not already b := srcimg.Bounds() var dstimg *image.RGBA if srcimg.ColorModel() != color.RGBAModel { dstimg = image.NewRGBA(b) draw.Draw(dstimg, b, srcimg, b.Min, draw.Src) } else { dstimg = srcimg.(*image.RGBA) } tex := gl.GenTexture() tex.Bind(gl.TEXTURE_2D) gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, b.Dx(), b.Dy(), 0, gl.RGBA, gl.UNSIGNED_BYTE, dstimg.Pix) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST) return &tex, nil }
func uploadTexture_NRGBA32(img *image.NRGBA) gl.Texture { b := img.Bounds() data := make([]uint8, b.Max.X*b.Max.Y*4) for y := 0; y < b.Max.Y; y++ { for x := 0; x < b.Max.X; x++ { p := img.At(x, y) offset := y*b.Max.X*4 + x*4 r, g, b, a := p.RGBA() data[offset+0] = uint8(r) data[offset+1] = uint8(g) data[offset+2] = uint8(b) data[offset+3] = uint8(a) } } id := gl.GenTexture() id.Bind(gl.TEXTURE_2D) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR) 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) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_R, gl.CLAMP_TO_EDGE) gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, b.Max.X, b.Max.Y, 0, gl.RGBA, gl.UNSIGNED_BYTE, data) if gl.GetError() != gl.NO_ERROR { id.Delete() panic(errors.New("Failed to load a texture")) return 0 } return id }
func (v *Video) Render() { for running { select { case dimensions := <-v.resize: v.ResizeEvent(dimensions[0], dimensions[1]) case val := <-v.tick: slice := make([]uint8, len(val)*3) for i := 0; i < len(val); i = i + 1 { slice[i*3+0] = (uint8)((val[i] >> 16) & 0xff) slice[i*3+1] = (uint8)((val[i] >> 8) & 0xff) slice[i*3+2] = (uint8)((val[i]) & 0xff) } gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) v.tex.Bind(gl.TEXTURE_2D) if ppu.OverscanEnabled { gl.TexImage2D(gl.TEXTURE_2D, 0, 3, 240, 224, 0, gl.RGB, gl.UNSIGNED_BYTE, slice) } else { gl.TexImage2D(gl.TEXTURE_2D, 0, 3, 256, 240, 0, gl.RGB, gl.UNSIGNED_BYTE, slice) } gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST) gl.Begin(gl.QUADS) gl.TexCoord2f(0.0, 1.0) gl.Vertex3f(-1.0, -1.0, 0.0) gl.TexCoord2f(1.0, 1.0) gl.Vertex3f(1.0, -1.0, 0.0) gl.TexCoord2f(1.0, 0.0) gl.Vertex3f(1.0, 1.0, 0.0) gl.TexCoord2f(0.0, 0.0) gl.Vertex3f(-1.0, 1.0, 0.0) gl.End() if v.screen != nil { sdl.GL_SwapBuffers() v.fpsmanager.FramerateDelay() } } } }
// Load bitmap from path as GL texture func LoadGLTexture(path string) { image := sdl.Load(path) if image == nil { panic(sdl.GetError()) } // Check that the image's width is a power of 2 if image.W&(image.W-1) != 0 { fmt.Println("warning:", path, "has a width that is not a power of 2") } // Also check if the height is a power of 2 if image.H&(image.H-1) != 0 { fmt.Println("warning:", path, "has an height that is not a power of 2") } // get the number of channels in the SDL surface nOfColors := image.Format.BytesPerPixel var textureFormat gl.GLenum if nOfColors == 4 { // contains alpha channel if image.Format.Rmask == 0x000000ff { textureFormat = gl.RGBA } else { textureFormat = gl.BGRA } } else if nOfColors == 3 { // no alpha channel if image.Format.Rmask == 0x000000ff { textureFormat = gl.RGB } else { textureFormat = gl.BGR } } else { fmt.Println("warning:", path, "is not truecolor, this will probably break") } texture = gl.GenTexture() // Typical texture generation using data from the bitmap gl.BindTexture(gl.TEXTURE_2D, uint(texture)) // Generate the texture gl.TexImage2D(gl.TEXTURE_2D, 0, int(image.Format.BytesPerPixel), int(image.W), int(image.H), 0, textureFormat, gl.UNSIGNED_BYTE, image.Pixels, ) // linear filtering gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) // free up memory we have used. image.Free() }
func uploadTexture(img *image.RGBA) gl.Texture { gl.Enable(gl.TEXTURE_2D) tex := gl.GenTexture() tex.Bind(gl.TEXTURE_2D) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST) gl.TexImage2D(gl.TEXTURE_2D, 0, 4, img.Bounds().Max.X, img.Bounds().Max.Y, 0, gl.RGBA, gl.UNSIGNED_BYTE, img.Pix) gl.Disable(gl.TEXTURE_2D) return tex }
// MakeImage makes an image from an image.NRGBA. func MakeImage(i *image.NRGBA) (img Image) { img.Width, img.Height = i.Bounds().Dx(), i.Bounds().Dy() img.tex = gl.GenTexture() img.tex.Bind(gl.TEXTURE_2D) gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR) gl.TexImage2D(gl.TEXTURE_2D, 0, 4, img.Width, img.Height, 0, gl.RGBA, gl.UNSIGNED_BYTE, i.Pix) return }
func (t *TextureData) LoadIntoGL() { t.id.Bind(gl.TEXTURE_2D) for i, v := range t.Mips { gl.TexImage2D(gl.TEXTURE_2D, i, gl.RGBA, v.w, v.h, 0, gl.RGBA, gl.UNSIGNED_BYTE, v.Pix) if !globals.CreateMipmaps { gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR) break //just one level if no mipmaps are wanted } } t.Unbind() }
func NewFont(filename string, size int, c color.Color) *Font { var font Font font.textures = make(map[rune]gl.Texture) font.widths = make(map[rune]int) extfont := ttf.OpenFont(filename, size) if extfont == nil { panic("Could not load font") } defer extfont.Close() font.height = extfont.LineSkip() for _, ch := range "abcdefghijklmnopqrdstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 :;@'<>,.?/~#{}[]!£$%^&*()_-+=\"\\|" { _, _, _, _, advance, err := extfont.GlyphMetrics(uint16(ch)) if err != 0 { panic("Could not get glyph metrics") } // Create a bitmap with width=advance, height=font.height surface := sdl.CreateRGBSurface(sdl.SWSURFACE|sdl.SRCALPHA, advance, font.height, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000) //surface.FillRect(&sdl.Rect{0,0, uint16(advance), uint16(font.height)}, 0x0) // rect := sdl.Rect{0,0, uint16(advance), uint16(ascent)} // rect := sdl.Rect{int16(minx), int16(ascent)-int16(maxy), 0, 0} fontSurface := ttf.RenderText_Blended(extfont, string(ch), sdl.ColorFromGoColor(c)) fontSurface.Blit(nil, surface, nil) rgba := image.NewRGBA(image.Rect(0, 0, advance, font.height)) for x := 0; x < advance; x++ { for y := 0; y < font.height; y++ { rgba.Set(x, y, fontSurface.At(x, font.height-y)) } } font.widths[ch] = advance font.textures[ch] = gl.GenTexture() font.textures[ch].Bind(gl.TEXTURE_2D) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST) gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, advance, font.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, &rgba.Pix[0]) font.textures[ch].Unbind(gl.TEXTURE_2D) } return &font }
func uploadTexture_RGBA32(w, h int, data []byte) gl.Texture { id := gl.GenTexture() id.Bind(gl.TEXTURE_2D) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR) 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) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_R, gl.CLAMP_TO_EDGE) gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, int(w), int(h), 0, gl.RGBA, gl.UNSIGNED_BYTE, data) if gl.GetError() != gl.NO_ERROR { id.Delete() panic("Failed to load a texture") return 0 } return id }
func draw() { for y := 0; y < Size; y++ { for x := 0; x < Size; x++ { var color uint8 if grid.Front()[y][x] { color = 0x00 } else { color = 0xFF } pixels[(y*Size+x)*3+0] = color pixels[(y*Size+x)*3+1] = color pixels[(y*Size+x)*3+2] = color } } gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.TexImage2D(gl.TEXTURE_2D, 0, 3, Size, Size, 0, gl.RGB, gl.UNSIGNED_BYTE, pixels) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST) gl.Begin(gl.QUADS) gl.TexCoord2f(0.0, 1.0) gl.Vertex3f(-1.0, -1.0, 0.0) gl.TexCoord2f(1.0, 1.0) gl.Vertex3f(1.0, -1.0, 0.0) gl.TexCoord2f(1.0, 0.0) gl.Vertex3f(1.0, 1.0, 0.0) gl.TexCoord2f(0.0, 0.0) gl.Vertex3f(-1.0, 1.0, 0.0) gl.End() glfw.SwapBuffers() }
func (v *Video) Render() { runtime.LockOSThread() for running { select { case val := <-v.tick: slice := make([]uint8, len(val)*3) for i := 0; i < len(val); i = i + 1 { slice[i*3+0] = (uint8)((val[i] >> 16) & 0xff) slice[i*3+1] = (uint8)((val[i] >> 8) & 0xff) slice[i*3+2] = (uint8)((val[i]) & 0xff) } gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) v.tex.Bind(gl.TEXTURE_2D) gl.TexImage2D(gl.TEXTURE_2D, 0, 3, 256, 240, 0, gl.RGB, gl.UNSIGNED_BYTE, slice) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST) gl.Begin(gl.QUADS) gl.TexCoord2f(0.0, 1.0) gl.Vertex3f(-1.0, -1.0, 0.0) gl.TexCoord2f(1.0, 1.0) gl.Vertex3f(1.0, -1.0, 0.0) gl.TexCoord2f(1.0, 0.0) gl.Vertex3f(1.0, 1.0, 0.0) gl.TexCoord2f(0.0, 0.0) gl.Vertex3f(-1.0, 1.0, 0.0) gl.End() glfw.SwapBuffers() v.fpsmanager.FramerateDelay() } } }
// load in bitmap as a GL texture func LoadGLTextures(path string) { image := sdl.Load(path) if image == nil { panic(sdl.GetError()) } // Check that the image's width is a power of 2 if image.W&(image.W-1) != 0 { fmt.Println("warning:", path, "has a width that is not a power of 2") } // Also check if the height is a power of 2 if image.H&(image.H-1) != 0 { fmt.Println("warning:", path, "has an height that is not a power of 2") } // get the number of channels in the SDL surface nOfColors := image.Format.BytesPerPixel var textureFormat gl.GLenum if nOfColors == 4 { // contains alpha channel if image.Format.Rmask == 0x000000ff { textureFormat = gl.RGBA } else { textureFormat = gl.BGRA } } else if nOfColors == 3 { // no alpha channel if image.Format.Rmask == 0x000000ff { textureFormat = gl.RGB } else { textureFormat = gl.BGR } } else { fmt.Println("warning:", path, "is not truecolor, this will probably break") } // Create the textures gl.GenTextures(textures[:]) // First texture gl.BindTexture(gl.TEXTURE_2D, uint(textures[0])) gl.TexImage2D( gl.TEXTURE_2D, 0, 3, int(image.W), int(image.H), 0, textureFormat, gl.UNSIGNED_BYTE, image.Pixels, ) // linear filtering gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST) // Second texture gl.BindTexture(gl.TEXTURE_2D, uint(textures[1])) gl.TexImage2D(gl.TEXTURE_2D, 0, 3, int(image.W), int(image.H), 0, textureFormat, gl.UNSIGNED_BYTE, image.Pixels, ) // Mipmapped filtering gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) // Third texture gl.BindTexture(gl.TEXTURE_2D, uint(textures[2])) gl.TexImage2D(gl.TEXTURE_2D, 0, 3, int(image.W), int(image.H), 0, textureFormat, gl.UNSIGNED_BYTE, image.Pixels, ) // Mipmapped filtering gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_NEAREST) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) glu.Build2DMipmaps( gl.TEXTURE_2D, 3, int(image.W), int(image.H), textureFormat, image.Pixels, ) }
func genTexture(into gl.Texture, from *sdl.Surface, format gl.GLenum) { gl.BindTexture(gl.TEXTURE_2D, uint(into)) gl.TexImage2D(gl.TEXTURE_2D, 0, 3, int(from.W), int(from.H), 0, format, gl.UNSIGNED_BYTE, from.Pixels, ) }