func main() { err := initGL() if err != nil { log.Printf("InitGL: %v", err) return } defer glfw.Terminate() // Create our texture atlas. atlas := glh.NewTextureAtlas(AtlasSize, AtlasSize, 4) defer atlas.Release() // Fill the altas with image data. fillAtlas(atlas) // Display the atlas texture on a quad, so we can see // what it looks like. for glfw.WindowParam(glfw.Opened) > 0 { gl.Clear(gl.COLOR_BUFFER_BIT) // Bind the atlas texture and render a quad with it. atlas.Bind(gl.TEXTURE_2D) gl.Begin(gl.QUADS) gl.TexCoord2f(0, 0) gl.Vertex2f(0, 0) gl.TexCoord2f(1, 0) gl.Vertex2f(AtlasSize, 0) gl.TexCoord2f(1, 1) gl.Vertex2f(AtlasSize, AtlasSize) gl.TexCoord2f(0, 1) gl.Vertex2f(0, AtlasSize) gl.End() atlas.Unbind(gl.TEXTURE_2D) glfw.SwapBuffers() } }
// createAtlas loads the current LEM1802 font into a texture atlas. func (d *LEM1802Display) createAtlas(gw, gh int) { if d.atlas == nil { d.atlas = glh.NewTextureAtlas(gw*32, gh*6, 1) } if d.glyphs == nil { d.glyphs = make([][4]float32, 128) } atlasWidth := float32(d.atlas.Width()) atlasHeight := float32(d.atlas.Height()) img := image.NewAlpha(image.Rect(0, 0, gw, gh)) font := d.dev.Font() pw := uint(gw / 4) ph := uint(gh / 8) d.atlas.Clear() for i := 0; i < 128; i++ { a, b := font[i*2], font[i*2+1] region, ok := d.atlas.Allocate(gw, gh) if !ok { continue } d.glyphs[i][0] = float32(region.X) / atlasWidth d.glyphs[i][1] = float32(region.Y) / atlasHeight d.glyphs[i][2] = float32(region.X+region.W) / atlasWidth d.glyphs[i][3] = float32(region.Y+region.H) / atlasHeight drawGlyph(img, pw, ph, byte(a>>8), byte(a), byte(b>>8), byte(b)) d.atlas.Set(region, img.Pix, gw) } d.atlas.Commit(gl.TEXTURE_2D) }