Merge branch 'sbinet-draw2dgl-with-go-gl'

This commit is contained in:
Laurent Le Goff 2015-04-17 15:03:49 +02:00
commit 0abcba8669
3 changed files with 89 additions and 46 deletions

View File

@ -5,16 +5,16 @@ all: install test
install:
cd draw2d && go install
# cd draw2dgl && make install
cd draw2dgl && go install
# cd wingui && make install
build:
cd draw2d && go build
# cd draw2dgl && make build
cd draw2dgl && 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

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/ps"
)
@ -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,17 +1,21 @@
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"
)
type GLPainter struct {
func init() {
runtime.LockOSThread()
}
type Painter struct {
// The Porter-Duff composition operator.
Op draw.Op
// The 16-bit color to paint the spans.
@ -24,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
@ -67,15 +71,15 @@ 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)
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]
@ -84,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
@ -100,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
@ -109,40 +113,44 @@ func NewGLPainter() *GLPainter {
type GraphicContext struct {
*draw2d.StackGraphicContext
painter *GLPainter
painter *Painter
fillRasterizer *raster.Rasterizer
strokeRasterizer *raster.Rasterizer
}
type GLVertex struct {
x, y float64
}
func NewGLVertex() *GLVertex {
return &GLVertex{}
}
func (glVertex *GLVertex) NextCommand(cmd draw2d.VertexCommand) {
}
func (glVertex *GLVertex) 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(),
NewGLPainter(),
NewPainter(),
raster.NewRasterizer(width, height),
raster.NewRasterizer(width, height),
}
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) {
}