Use Vertex Array to optimize OpenGL Drawing
This commit is contained in:
parent
b476b6531f
commit
a37314d721
3 changed files with 34 additions and 7 deletions
|
@ -1,7 +1,7 @@
|
||||||
|
|
||||||
include $(GOROOT)/src/Make.inc
|
include $(GOROOT)/src/Make.inc
|
||||||
|
|
||||||
TARG=gettingStarted testdraw2d testX11draw testandroid testgopher testimage testpostscript testWalkDraw draw2dgl
|
TARG=gettingStarted testdraw2d testX11draw testandroid testgopher testimage testpostscript draw2dgl
|
||||||
|
|
||||||
OFILES=$(TARG:%=%.$O)
|
OFILES=$(TARG:%=%.$O)
|
||||||
|
|
||||||
|
|
|
@ -91,7 +91,7 @@ func display() {
|
||||||
|
|
||||||
gc.Translate(380, 400)
|
gc.Translate(380, 400)
|
||||||
gc.Scale(1, -1)
|
gc.Scale(1, -1)
|
||||||
rotate = (rotate + 10) % 360
|
rotate = (rotate + 1) % 360
|
||||||
gc.Rotate(float64(rotate) * math.Pi / 180)
|
gc.Rotate(float64(rotate) * math.Pi / 180)
|
||||||
gc.Translate(-380, -400)
|
gc.Translate(-380, -400)
|
||||||
interpreter := postscript.NewInterpreter(gc)
|
interpreter := postscript.NewInterpreter(gc)
|
||||||
|
|
|
@ -4,8 +4,10 @@ import (
|
||||||
"image"
|
"image"
|
||||||
"exp/draw"
|
"exp/draw"
|
||||||
"gl"
|
"gl"
|
||||||
|
"unsafe"
|
||||||
"freetype-go.googlecode.com/hg/freetype/raster"
|
"freetype-go.googlecode.com/hg/freetype/raster"
|
||||||
"draw2d.googlecode.com/hg/draw2d"
|
"draw2d.googlecode.com/hg/draw2d"
|
||||||
|
//"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
type GLPainter struct {
|
type GLPainter struct {
|
||||||
|
@ -14,23 +16,44 @@ type GLPainter struct {
|
||||||
// The 16-bit color to paint the spans.
|
// The 16-bit color to paint the spans.
|
||||||
cr, cg, cb uint8
|
cr, cg, cb uint8
|
||||||
ca uint32
|
ca uint32
|
||||||
|
colors []uint8
|
||||||
|
vertices []int32
|
||||||
}
|
}
|
||||||
|
|
||||||
const M16 uint32 = 1<<16 - 1
|
const M16 uint32 = 1<<16 - 1
|
||||||
|
|
||||||
// Paint satisfies the Painter interface by painting ss onto an image.RGBA.
|
// Paint satisfies the Painter interface by painting ss onto an image.RGBA.
|
||||||
func (p *GLPainter) Paint(ss []raster.Span, done bool) {
|
func (p *GLPainter) Paint(ss []raster.Span, done bool) {
|
||||||
gl.Begin(gl.LINES)
|
//gl.Begin(gl.LINES)
|
||||||
for _, s := range ss {
|
for _, s := range ss {
|
||||||
ma := s.A >> 16
|
ma := s.A >> 16
|
||||||
a := ma * p.ca / M16
|
a := ma * p.ca / M16
|
||||||
gl.Color4ub(p.cr, p.cg, p.cb, uint8(a>>8))
|
/*gl.Color4ub(p.cr, p.cg, p.cb, uint8(a>>8))
|
||||||
gl.Vertex2i(s.X0, s.Y)
|
gl.Vertex2i(s.X0, s.Y)
|
||||||
gl.Vertex2i(s.X1, s.Y)
|
gl.Vertex2i(s.X1, s.Y)*/
|
||||||
|
p.colors = append(p.colors, p.cr, p.cg, p.cb, uint8(a>>8), p.cr, p.cg, p.cb, uint8(a>>8))
|
||||||
|
p.vertices = append(p.vertices, int32(s.X0), int32(s.Y), int32(s.X1), int32(s.Y))
|
||||||
}
|
}
|
||||||
gl.End()
|
//gl.End()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *GLPainter) Flush() {
|
||||||
|
if len(p.vertices) != 0 {
|
||||||
|
gl.EnableClientState(gl.COLOR_ARRAY)
|
||||||
|
gl.EnableClientState(gl.VERTEX_ARRAY)
|
||||||
|
gl.ColorPointer(4, gl.UNSIGNED_BYTE, 0, unsafe.Pointer(&(p.colors[0])))
|
||||||
|
gl.VertexPointer(2, gl.INT, 0, unsafe.Pointer(&(p.vertices[0])))
|
||||||
|
|
||||||
|
// draw lines
|
||||||
|
gl.DrawArrays(gl.LINES, 0, len(p.vertices)/2)
|
||||||
|
gl.DisableClientState(gl.VERTEX_ARRAY)
|
||||||
|
gl.DisableClientState(gl.COLOR_ARRAY)
|
||||||
|
p.vertices = make([]int32, 0, 1024)
|
||||||
|
p.colors = make([]uint8, 0, 1024)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// SetColor sets the color to paint the spans.
|
// SetColor sets the color to paint the spans.
|
||||||
func (p *GLPainter) SetColor(c image.Color) {
|
func (p *GLPainter) SetColor(c image.Color) {
|
||||||
r, g, b, a := c.RGBA()
|
r, g, b, a := c.RGBA()
|
||||||
|
@ -49,7 +72,10 @@ func (p *GLPainter) SetColor(c image.Color) {
|
||||||
|
|
||||||
// NewRGBAPainter creates a new RGBAPainter for the given image.
|
// NewRGBAPainter creates a new RGBAPainter for the given image.
|
||||||
func NewGLPainter() *GLPainter {
|
func NewGLPainter() *GLPainter {
|
||||||
return &GLPainter{}
|
p := new(GLPainter)
|
||||||
|
p.vertices = make([]int32, 0, 1024)
|
||||||
|
p.colors = make([]uint8, 0, 1024)
|
||||||
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
type GraphicContext struct {
|
type GraphicContext struct {
|
||||||
|
@ -102,6 +128,7 @@ func (gc *GraphicContext) paint(rasterizer *raster.Rasterizer, color image.Color
|
||||||
gc.painter.SetColor(color)
|
gc.painter.SetColor(color)
|
||||||
rasterizer.Rasterize(gc.painter)
|
rasterizer.Rasterize(gc.painter)
|
||||||
rasterizer.Clear()
|
rasterizer.Clear()
|
||||||
|
gc.painter.Flush()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gc *GraphicContext) Stroke(paths ...*draw2d.PathStorage) {
|
func (gc *GraphicContext) Stroke(paths ...*draw2d.PathStorage) {
|
||||||
|
|
Loading…
Reference in a new issue