func (c *Context) SetViewport(f Framebuffer, width, height int) error { gl.Flush() gl.BindFramebuffer(gl.FRAMEBUFFER, uint32(f)) if err := gl.CheckFramebufferStatus(gl.FRAMEBUFFER); err != gl.FRAMEBUFFER_COMPLETE { if e := gl.GetError(); e != 0 { return errors.New(fmt.Sprintf("glBindFramebuffer failed: %d", e)) } return errors.New("glBindFramebuffer failed: the context is different?") } gl.Viewport(0, 0, int32(width), int32(height)) return nil }
func (c *Context) FramebufferPixels(f Framebuffer, width, height int) ([]uint8, error) { gl.Flush() gl.BindFramebuffer(gl.FRAMEBUFFER, uint32(f)) pixels := make([]uint8, 4*width*height) gl.ReadPixels(0, 0, int32(width), int32(height), gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(pixels)) if e := gl.GetError(); e != gl.NO_ERROR { return nil, errors.New(fmt.Sprintf("glReadPixels: %d", e)) } return pixels, nil }
func display() { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.LineWidth(1) gc := draw2dgl.NewGraphicContext(width, height) gc.SetFontData(draw2d.FontData{ Name: "luxi", Family: draw2d.FontFamilyMono, Style: draw2d.FontStyleBold | draw2d.FontStyleItalic}) gc.BeginPath() draw2dkit.RoundedRectangle(gc, 200, 200, 600, 600, 100, 100) gc.SetFillColor(color.RGBA{0, 0, 0, 0xff}) gc.Fill() gl.Flush() /* Single buffered, so needs a flush. */ }
func display() { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) lastTime := time.Now() gl.LineWidth(1) gc := draw2dgl.NewGraphicContext(width, height) gc.Translate(380, 400) gc.Scale(1, -1) rotate = (rotate + 1) % 360 gc.Rotate(float64(rotate) * math.Pi / 180) gc.Translate(-380, -400) interpreter := ps.NewInterpreter(gc) reader := strings.NewReader(postscriptContent) interpreter.Execute(reader) dt := time.Now().Sub(lastTime) log.Printf("Redraw in : %f ms\n", float64(dt)*1e-6) gl.Flush() /* Single buffered, so needs a flush. */ }
func (c *Context) FramebufferPixels(f Framebuffer, width, height int) ([]uint8, error) { var pixels []uint8 if err := c.runOnContextThread(func() error { gl.Flush() return nil }); err != nil { return nil, err } if err := c.bindFramebuffer(f); err != nil { return nil, err } if err := c.runOnContextThread(func() error { pixels = make([]uint8, 4*width*height) gl.ReadPixels(0, 0, int32(width), int32(height), gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(pixels)) if e := gl.GetError(); e != gl.NO_ERROR { pixels = nil return fmt.Errorf("opengl: glReadPixels: %d", e) } return nil }); err != nil { return nil, err } return pixels, nil }
// Flush empties all buffers. It does not block. // // An OpenGL implementation may buffer network communication, // the command stream, or data inside the graphics accelerator. // // http://www.khronos.org/opengles/sdk/docs/man3/html/glFlush.xhtml func Flush() { gl.Flush() }
func (w *Window) Show() { if err := glfw.Init(); err != nil { log.Fatalln("failed to initialize glfw:", err) } defer glfw.Terminate() glfw.WindowHint(glfw.Resizable, glfw.False) glfw.WindowHint(glfw.Resizable, glfw.True) window, err := glfw.CreateWindow(w.W, w.H, w.Title, nil, nil) if err != nil { panic(err) } if err := gl.Init(); err != nil { panic(err) } window.MakeContextCurrent() if len(w.Icon) == 1 { window.SetIcon(w.Icon) } if w.RestrictionsEnabled { window.SetSizeLimits(w.MinW, w.MinH, w.MaxW, w.MaxH) } if w.Blend { gl.Enable(gl.BLEND) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) } else if w.Smooth { gl.Enable(gl.SMOOTH) gl.Enable(gl.POINT_SMOOTH) gl.Enable(gl.LINE_SMOOTH) gl.Enable(gl.POLYGON_SMOOTH) gl.Enable(gl.RELATIVE_SMOOTH_CUBIC_CURVE_TO_NV) gl.Enable(gl.RELATIVE_SMOOTH_QUADRATIC_CURVE_TO_NV) } else if w.Multisample { gl.Enable(gl.MULTISAMPLE) } reshape(w.W, w.H) window.SetRefreshCallback(func(g *glfw.Window) { reshape(w.W, w.H) }) window.SetSizeCallback(func(g *glfw.Window, width int, height int) { reshape(width, height) w.W = width w.H = height }) version := gl.GoStr(gl.GetString(gl.VERSION)) if !silentMode { fmt.Println("OpenGL version", version) } for !window.ShouldClose() { //Reset? gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) if w.DrawFunc != nil { w.DrawFunc() } // Maintenance gl.Flush() window.SwapBuffers() glfw.PollEvents() } }
func (c *Context) Flush() { _ = c.runOnContextThread(func() error { gl.Flush() return nil }) }