From a8154b175c7590b07140fb3801b14f498172bee8 Mon Sep 17 00:00:00 2001 From: Sebastien Binet Date: Thu, 16 Apr 2015 11:56:55 +0200 Subject: [PATCH 1/4] draw2dgl: use github.com/go-gl/{gl,glfw} --- cmd/draw2dgl.go | 57 +++++++++++++++++++++++++++++++++++++++---------- draw2dgl/gc.go | 36 ++++++++++++++++++++++++++----- 2 files changed, 77 insertions(+), 16 deletions(-) diff --git a/cmd/draw2dgl.go b/cmd/draw2dgl.go index f6bd896..4d31621 100644 --- a/cmd/draw2dgl.go +++ b/cmd/draw2dgl.go @@ -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() } diff --git a/draw2dgl/gc.go b/draw2dgl/gc.go index e6c092a..6fc7889 100644 --- a/draw2dgl/gc.go +++ b/draw2dgl/gc.go @@ -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) { } From c1b7f443b4fea001357c46edb5f33cdc77bf8ac4 Mon Sep 17 00:00:00 2001 From: Sebastien Binet Date: Thu, 16 Apr 2015 11:59:00 +0200 Subject: [PATCH 2/4] draw2dgl: de-stutter types --- draw2dgl/gc.go | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/draw2dgl/gc.go b/draw2dgl/gc.go index 6fc7889..032b26a 100644 --- a/draw2dgl/gc.go +++ b/draw2dgl/gc.go @@ -15,7 +15,7 @@ func init() { runtime.LockOSThread() } -type GLPainter struct { +type Painter struct { // The Porter-Duff composition operator. Op draw.Op // The 16-bit color to paint the spans. @@ -28,7 +28,7 @@ type GLPainter struct { const M16 uint32 = 1<<16 - 1 // Paint satisfies the Painter interface by painting ss onto an image.RGBA. -func (p *GLPainter) Paint(ss []raster.Span, done bool) { +func (p *Painter) Paint(ss []raster.Span, done bool) { //gl.Begin(gl.LINES) sslen := len(ss) clenrequired := sslen * 8 @@ -71,7 +71,7 @@ func (p *GLPainter) Paint(ss []raster.Span, done bool) { } } -func (p *GLPainter) Flush() { +func (p *Painter) Flush() { if len(p.vertices) != 0 { gl.EnableClientState(gl.COLOR_ARRAY) gl.EnableClientState(gl.VERTEX_ARRAY) @@ -88,7 +88,7 @@ func (p *GLPainter) Flush() { } // SetColor sets the color to paint the spans. -func (p *GLPainter) SetColor(c color.Color) { +func (p *Painter) SetColor(c color.Color) { r, g, b, a := c.RGBA() if a == 0 { p.cr = 0 @@ -104,8 +104,8 @@ func (p *GLPainter) SetColor(c color.Color) { } // NewRGBAPainter creates a new RGBAPainter for the given image. -func NewGLPainter() *GLPainter { - p := new(GLPainter) +func NewPainter() *Painter { + p := new(Painter) p.vertices = make([]int32, 0, 1024) p.colors = make([]uint8, 0, 1024) return p @@ -113,24 +113,24 @@ func NewGLPainter() *GLPainter { type GraphicContext struct { *draw2d.StackGraphicContext - painter *GLPainter + painter *Painter fillRasterizer *raster.Rasterizer strokeRasterizer *raster.Rasterizer } -type GLVertex struct { +type Vertex struct { x, y float64 } -func NewGLVertex() *GLVertex { - return &GLVertex{} +func NewVertex() *Vertex { + return &Vertex{} } -func (glVertex *GLVertex) NextCommand(cmd draw2d.VertexCommand) { +func (*Vertex) NextCommand(cmd draw2d.VertexCommand) { } -func (glVertex *GLVertex) Vertex(x, y float64) { +func (*Vertex) Vertex(x, y float64) { gl.Vertex2d(x, y) } @@ -140,7 +140,7 @@ func (glVertex *GLVertex) Vertex(x, y float64) { func NewGraphicContext(width, height int) *GraphicContext { gc := &GraphicContext{ draw2d.NewStackGraphicContext(), - NewGLPainter(), + NewPainter(), raster.NewRasterizer(width, height), raster.NewRasterizer(width, height), } From ef603db5350894be8622484f7626f9e83b134f78 Mon Sep 17 00:00:00 2001 From: Sebastien Binet Date: Thu, 16 Apr 2015 12:00:04 +0200 Subject: [PATCH 3/4] draw2dgl: remove unneeded GLVertex --- draw2dgl/gc.go | 20 +------------------- 1 file changed, 1 insertion(+), 19 deletions(-) diff --git a/draw2dgl/gc.go b/draw2dgl/gc.go index 032b26a..7de60f2 100644 --- a/draw2dgl/gc.go +++ b/draw2dgl/gc.go @@ -118,25 +118,7 @@ type GraphicContext struct { strokeRasterizer *raster.Rasterizer } -type Vertex struct { - x, y float64 -} - -func NewVertex() *Vertex { - return &Vertex{} -} - -func (*Vertex) NextCommand(cmd draw2d.VertexCommand) { - -} - -func (*Vertex) Vertex(x, y float64) { - gl.Vertex2d(x, y) -} - -/** - * Create a new Graphic context from an image - */ +// NewGraphicContext creates a new Graphic context from an image. func NewGraphicContext(width, height int) *GraphicContext { gc := &GraphicContext{ draw2d.NewStackGraphicContext(), From 0eeb62825f397305533e7b14f8346d1f88fb2089 Mon Sep 17 00:00:00 2001 From: Sebastien Binet Date: Thu, 16 Apr 2015 17:05:30 +0200 Subject: [PATCH 4/4] make: enable draw2dgl build+test --- Makefile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index aff76cd..0218823 100644 --- a/Makefile +++ b/Makefile @@ -5,18 +5,18 @@ all: install test install: cd draw2d && go install -# cd draw2dgl && make install + cd draw2dgl && go install cd postscript && go install # cd wingui && make install build: cd draw2d && go build -# cd draw2dgl && make build + cd draw2dgl && go build cd postscript && go build # cd wingui && make build test: - #cd cmd && go build draw2dgl.go + cd cmd && go build draw2dgl.go cd cmd && go build gettingStarted.go cd cmd && go build testandroid.go cd cmd && go build testdraw2d.go