func main() { if !glfw.Init() { log.Fatal("glfw failed to initialize") } defer glfw.Terminate() window, err := glfw.CreateWindow(640, 480, "Deformable", nil, nil) if err != nil { log.Fatal(err.Error()) } window.MakeContextCurrent() glfw.SwapInterval(1) window.SetMouseButtonCallback(handleMouseButton) window.SetKeyCallback(handleKeyDown) window.SetInputMode(glfw.Cursor, glfw.CursorHidden) gl.Init() initGL() i := 16 m = GenerateMap(1600/i, 1200/i, i) for running && !window.ShouldClose() { x, y := window.GetCursorPosition() if drawing != 0 { m.Add(int(x)+int(camera[0]), int(y)+int(camera[1]), drawing, brushSizes[currentBrushSize]) } gl.Clear(gl.COLOR_BUFFER_BIT) gl.LoadIdentity() gl.PushMatrix() gl.PushAttrib(gl.CURRENT_BIT | gl.ENABLE_BIT | gl.LIGHTING_BIT | gl.POLYGON_BIT | gl.LINE_BIT) gl.Translatef(-camera[0], -camera[1], 0) m.Draw() gl.PopAttrib() gl.PopMatrix() gl.PushAttrib(gl.COLOR_BUFFER_BIT) gl.LineWidth(2) gl.Enable(gl.BLEND) gl.BlendFunc(gl.ONE_MINUS_DST_COLOR, gl.ZERO) // gl.Enable(gl.LINE_SMOOTH) // gl.Hint(gl.LINE_SMOOTH_HINT, gl.NICEST) gl.Translatef(float32(x), float32(y), 0) gl.EnableClientState(gl.VERTEX_ARRAY) gl.VertexPointer(2, gl.DOUBLE, 0, cursorVerts) gl.DrawArrays(gl.LINE_LOOP, 0, 24) gl.PopAttrib() window.SwapBuffers() glfw.PollEvents() } }
func (m *Map) Draw() { gl.PushMatrix() gl.PushAttrib(gl.CURRENT_BIT | gl.ENABLE_BIT | gl.LIGHTING_BIT | gl.POLYGON_BIT | gl.LINE_BIT) gl.EnableClientState(gl.VERTEX_ARRAY) gl.VertexPointer(3, gl.FLOAT, 0, m.vertices) gl.EnableClientState(gl.NORMAL_ARRAY) gl.NormalPointer(gl.FLOAT, 0, m.normals) // gl.EnableClientState(gl.TEXTURE_COORD_ARRAY) // gl.TexCoordPointer(2, gl.FLOAT, 0, m.texcoords) gl.EnableClientState(gl.COLOR_ARRAY) gl.ColorPointer(3, gl.FLOAT, 0, m.colors) // draw solids gl.Enable(gl.COLOR_MATERIAL) // gl.DrawArrays(gl.TRIANGLE_STRIP, 0, len(m.vertices)/3) gl.PolygonMode(gl.FRONT_AND_BACK, gl.LINE) gl.LineWidth(1.0) gl.Color4f(1, 1, 1, 1) gl.DrawArrays(gl.TRIANGLE_STRIP, 0, len(m.vertices)/3) gl.PopAttrib() gl.PopMatrix() }
// Commit creates the actual texture from the atlas image data. // This should be called after all regions have been defined and set, // and before you start using the texture for display. func (a *TextureAtlas) Commit(target gl.GLenum) { gl.PushAttrib(gl.CURRENT_BIT | gl.ENABLE_BIT) gl.Enable(target) a.texture.Bind(target) gl.TexParameteri(target, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE) gl.TexParameteri(target, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE) gl.TexParameteri(target, gl.TEXTURE_MAG_FILTER, gl.LINEAR) gl.TexParameteri(target, gl.TEXTURE_MIN_FILTER, gl.LINEAR) switch a.depth { case 4: gl.TexImage2D(target, 0, gl.RGBA, a.width, a.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, a.data) case 3: gl.TexImage2D(target, 0, gl.RGB, a.width, a.height, 0, gl.RGB, gl.UNSIGNED_BYTE, a.data) case 1: gl.TexImage2D(target, 0, gl.ALPHA, a.width, a.height, 0, gl.ALPHA, gl.UNSIGNED_BYTE, a.data) } gl.PopAttrib() }
func (r *RenderTarget) popGlStates() { gl.MatrixMode(gl.PROJECTION) gl.PopMatrix() gl.MatrixMode(gl.MODELVIEW) gl.PopMatrix() gl.MatrixMode(gl.TEXTURE) gl.PopMatrix() gl.PopClientAttrib() gl.PopAttrib() }
func (b Texture) Exit() { b.Unbind(gl.TEXTURE_2D) gl.PopAttrib() }
// Printf draws the given string at the specified coordinates. // It expects the string to be a single line. Line breaks are not // handled as line breaks and are rendered as glyphs. // // In order to render multi-line text, it is up to the caller to split // the text up into individual lines of adequate length and then call // this method for each line seperately. func (f *Font) Printf(x, y float32, fs string, argv ...interface{}) error { indices := []rune(fmt.Sprintf(fs, argv...)) if len(indices) == 0 { return nil } // Runes form display list indices. // For this purpose, they need to be offset by -FontConfig.Low low := f.Config.Low for i := range indices { indices[i] -= low } var vp [4]int32 gl.GetIntegerv(gl.VIEWPORT, vp[:]) gl.PushAttrib(gl.TRANSFORM_BIT) gl.MatrixMode(gl.PROJECTION) gl.PushMatrix() gl.LoadIdentity() gl.Ortho(float64(vp[0]), float64(vp[2]), float64(vp[1]), float64(vp[3]), 0, 1) gl.PopAttrib() gl.PushAttrib(gl.LIST_BIT | gl.CURRENT_BIT | gl.ENABLE_BIT | gl.TRANSFORM_BIT) { gl.MatrixMode(gl.MODELVIEW) gl.Disable(gl.LIGHTING) gl.Disable(gl.DEPTH_TEST) gl.Enable(gl.BLEND) gl.Enable(gl.TEXTURE_2D) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) gl.TexEnvf(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.MODULATE) f.Texture.Bind(gl.TEXTURE_2D) var mv [16]float32 gl.GetFloatv(gl.MODELVIEW_MATRIX, mv[:]) gl.PushMatrix() { gl.LoadIdentity() mgw := float32(f.maxGlyphWidth) mgh := float32(f.maxGlyphHeight) switch f.Config.Dir { case LeftToRight, TopToBottom: gl.Translatef(x, float32(vp[3])-y-mgh, 0) case RightToLeft: gl.Translatef(x-mgw, float32(vp[3])-y-mgh, 0) } gl.MultMatrixf(&mv) gl.CallLists(len(indices), gl.UNSIGNED_INT, indices) } gl.PopMatrix() f.Texture.Unbind(gl.TEXTURE_2D) } gl.PopAttrib() gl.PushAttrib(gl.TRANSFORM_BIT) gl.MatrixMode(gl.PROJECTION) gl.PopMatrix() gl.PopAttrib() return glh.CheckGLError() }
// Render renders the LEM1802 contents. func (d *LEM1802Display) Render() { var tex [4]float32 var addr, x, y int var fg, bg, blink, char byte mem := d.dev.Buffer() if len(mem) == 0 { return } palette := d.dev.Palette() border := d.dev.Border() clr_ut := d.quads_ut.Colors().Data().([]byte) clr_t := d.quads_t.Colors().Data().([]byte) tex_t := d.quads_t.TexCoords().Data().([]float32) // Set background/border color. r, g, b := lem1802.RGB(palette[border]) setColor(clr_ut, r, g, b) clr_ut = clr_ut[12:] for y = 0; y < lem1802.CellsPerCol; y++ { for x = 0; x < lem1802.CellsPerRow; x++ { fg, bg, blink, char = lem1802.Decode(mem[addr]) addr++ // Swap foreground and background colours if this // character has the blink bit set, and the current // blink state is 1. if blink == 1 && atomic.LoadUint32(&d.blinkState) == 1 { fg = bg } // Update glyph background color. r, g, b := lem1802.RGB(palette[bg]) setColor(clr_ut, r, g, b) clr_ut = clr_ut[12:] // Update glyph foreground color and tex coords. r, g, b = lem1802.RGB(palette[fg]) setColor(clr_t, r, g, b) clr_t = clr_t[12:] tex = d.glyphs[char] tex_t[0] = tex[0] tex_t[1] = tex[1] tex_t[2] = tex[2] tex_t[3] = tex[1] tex_t[4] = tex[2] tex_t[5] = tex[3] tex_t[6] = tex[0] tex_t[7] = tex[3] tex_t = tex_t[8:] } } // Ensure these buffers are re-comitted to the GPU, // because we have just changed their contents. d.quads_ut.Colors().Invalidate() d.quads_t.Colors().Invalidate() d.quads_t.TexCoords().Invalidate() gl.PushAttrib(gl.ENABLE_BIT) { gl.Disable(gl.DEPTH_TEST) gl.Disable(gl.TEXTURE_2D) gl.Enable(gl.COLOR_MATERIAL) gl.Enable(gl.BLEND) gl.LoadIdentity() // Render border and glyph backgrounds. d.quads_ut.Render(gl.QUADS) // Render glyphs. gl.Enable(gl.TEXTURE_2D) d.atlas.Bind(gl.TEXTURE_2D) d.quads_t.Render(gl.QUADS) d.atlas.Unbind(gl.TEXTURE_2D) } gl.PopAttrib() }
func (a Attrib) Exit() { gl.PopAttrib() }
func (m Matrix) Exit() { gl.PopMatrix() gl.PopAttrib() }
func (e _enable) Exit() { gl.PopAttrib() }
func (e _disable) Exit() { gl.PopAttrib() }