draw2dgl: use github.com/go-gl/{gl,glfw}

This commit is contained in:
Sebastien Binet 2015-04-16 11:56:55 +02:00
parent 3d2a09c9e2
commit a8154b175c
2 changed files with 77 additions and 16 deletions

View file

@ -18,15 +18,16 @@
package main
import (
"gl"
"glut"
"io/ioutil"
"log"
"math"
"os"
"runtime"
"strings"
"time"
"github.com/go-gl/gl/v2.1/gl"
"github.com/go-gl/glfw/v3.1/glfw"
"github.com/llgcode/draw2d/draw2dgl"
"github.com/llgcode/draw2d/postscript"
)
@ -36,9 +37,10 @@ var postscriptContent string
var (
width, height int
rotate int
window *glfw.Window
)
func reshape(w, h int) {
func reshape(window *glfw.Window, w, h int) {
/* Because Gil specified "screen coordinates" (presumably with an
upper-left origin), this short bit of code sets up the coordinate
system to correspond to actual window coodrinates. This code
@ -46,7 +48,7 @@ func reshape(w, h int) {
coordinate system. */
gl.ClearColor(1, 1, 1, 1)
//fmt.Println(gl.GetString(gl.EXTENSIONS))
gl.Viewport(0, 0, w, h) /* Establish viewing area to cover entire window. */
gl.Viewport(0, 0, int32(w), int32(h)) /* Establish viewing area to cover entire window. */
gl.MatrixMode(gl.PROJECTION) /* Start modifying the projection matrix. */
gl.LoadIdentity() /* Reset project matrix. */
gl.Ortho(0, float64(w), 0, float64(h), -1, 1) /* Map abstract coords directly to window coords. */
@ -75,7 +77,7 @@ func display() {
dt := time.Now().Sub(lastTime)
log.Printf("Redraw in : %f ms\n", float64(dt)*1e-6)
gl.Flush() /* Single buffered, so needs a flush. */
glut.PostRedisplay()
window.SwapBuffers()
}
func main() {
@ -87,11 +89,44 @@ func main() {
defer src.Close()
bytes, err := ioutil.ReadAll(src)
postscriptContent = string(bytes)
glut.Init()
glut.InitWindowSize(800, 800)
glut.CreateWindow("Show Tiger in Opengl")
err = glfw.Init()
if err != nil {
panic(err)
}
defer glfw.Terminate()
glut.DisplayFunc(display)
glut.ReshapeFunc(reshape)
glut.MainLoop()
window, err = glfw.CreateWindow(800, 800, "Show Tiger in OpenGL", nil, nil)
if err != nil {
panic(err)
}
window.MakeContextCurrent()
window.SetSizeCallback(reshape)
window.SetKeyCallback(onKey)
glfw.SwapInterval(1)
err = gl.Init()
if err != nil {
panic(err)
}
for !window.ShouldClose() {
display()
glfw.PollEvents()
window.SwapBuffers()
// time.Sleep(2 * time.Second)
}
}
func onKey(w *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
switch {
case key == glfw.KeyEscape && action == glfw.Press,
key == glfw.KeyQ && action == glfw.Press:
w.SetShouldClose(true)
}
}
func init() {
runtime.LockOSThread()
}

View file

@ -1,16 +1,20 @@
package draw2dgl
import (
"gl"
"image"
"image/color"
"image/draw"
"runtime"
"code.google.com/p/freetype-go/freetype/raster"
"github.com/go-gl/gl/v2.1/gl"
"github.com/llgcode/draw2d/draw2d"
//"log"
)
func init() {
runtime.LockOSThread()
}
type GLPainter struct {
// The Porter-Duff composition operator.
Op draw.Op
@ -71,11 +75,11 @@ func (p *GLPainter) Flush() {
if len(p.vertices) != 0 {
gl.EnableClientState(gl.COLOR_ARRAY)
gl.EnableClientState(gl.VERTEX_ARRAY)
gl.ColorPointer(4, 0, p.colors)
gl.VertexPointer(2, 0, p.vertices)
gl.ColorPointer(4, gl.UNSIGNED_BYTE, 0, gl.Ptr(p.colors))
gl.VertexPointer(2, gl.INT, 0, gl.Ptr(p.vertices))
// draw lines
gl.DrawArrays(gl.LINES, 0, len(p.vertices)/2)
gl.DrawArrays(gl.LINES, 0, int32(len(p.vertices)/2))
gl.DisableClientState(gl.VERTEX_ARRAY)
gl.DisableClientState(gl.COLOR_ARRAY)
p.vertices = p.vertices[0:0]
@ -143,6 +147,28 @@ func NewGraphicContext(width, height int) *GraphicContext {
return gc
}
func (gc *GraphicContext) CreateStringPath(s string, x, y float64) float64 {
panic("not implemented")
}
func (gc *GraphicContext) FillStringAt(text string, x, y float64) (cursor float64) {
panic("not implemented")
}
func (gc *GraphicContext) GetStringBounds(s string) (left, top, right, bottom float64) {
panic("not implemented")
}
func (gc *GraphicContext) StrokeString(text string) (cursor float64) {
return gc.StrokeStringAt(text, 0, 0)
}
func (gc *GraphicContext) StrokeStringAt(text string, x, y float64) (cursor float64) {
width := gc.CreateStringPath(text, x, y)
gc.Stroke()
return width
}
func (gc *GraphicContext) SetDPI(dpi int) {
}