From 38eddd28371e8ca796f74c4d758ddec1986d7015 Mon Sep 17 00:00:00 2001 From: Thomas Friedel Date: Wed, 20 Feb 2019 16:09:44 +0100 Subject: [PATCH] moved a lot of fill code to the backend; also started moving shadow drawing code to backend --- backend/backendbase/base.go | 8 ++ backend/gogl/fill.go | 11 +- backend/gogl/shadows.go | 222 +++++++++++++++++++++++++++++ canvas_test.go | 2 + paths.go | 270 ++++++++++++++++++------------------ sdlcanvas/sdlcanvas.go | 2 +- triangulation.go | 4 +- 7 files changed, 375 insertions(+), 144 deletions(-) create mode 100644 backend/gogl/shadows.go diff --git a/backend/backendbase/base.go b/backend/backendbase/base.go index afce75d..e6f36f6 100644 --- a/backend/backendbase/base.go +++ b/backend/backendbase/base.go @@ -5,7 +5,15 @@ import "image/color" type Style struct { Color color.RGBA GlobalAlpha float64 + Shadow Shadow // radialGradient *RadialGradient // linearGradient *LinearGradient // image *Image } + +type Shadow struct { + Color color.RGBA + OffsetX float64 + OffsetY float64 + Blur float64 +} diff --git a/backend/gogl/fill.go b/backend/gogl/fill.go index bf52a29..6378812 100644 --- a/backend/gogl/fill.go +++ b/backend/gogl/fill.go @@ -112,6 +112,11 @@ func (b *GoGLBackend) Fill(style *backendbase.Style, pts [4][2]float64) { func (b *GoGLBackend) Fill(style *backendbase.Style, pts [][2]float64) { b.ptsBuf = b.ptsBuf[:0] + b.ptsBuf = append(b.ptsBuf, + 0, 0, + 0, float32(b.fh), + float32(b.fw), float32(b.fh), + float32(b.fw), 0) for _, pt := range pts { b.ptsBuf = append(b.ptsBuf, float32(pt[0]), float32(pt[1])) } @@ -124,12 +129,12 @@ func (b *GoGLBackend) Fill(style *backendbase.Style, pts [][2]float64) { gl.BindBuffer(gl.ARRAY_BUFFER, b.buf) gl.BufferData(gl.ARRAY_BUFFER, len(b.ptsBuf)*4, unsafe.Pointer(&b.ptsBuf[0]), gl.STREAM_DRAW) - if style.GlobalAlpha >= 1 { // && cv.state.fill.isOpaque() { + if style.GlobalAlpha >= 1 && style.Color.A >= 255 { vertex := b.useShader(style) gl.EnableVertexAttribArray(vertex) gl.VertexAttribPointer(vertex, 2, gl.FLOAT, false, 0, nil) - gl.DrawArrays(mode, 0, int32(len(b.ptsBuf)/2)) + gl.DrawArrays(mode, 4, int32(len(pts))) gl.DisableVertexAttribArray(vertex) } else { gl.ColorMask(false, false, false, false) @@ -143,7 +148,7 @@ func (b *GoGLBackend) Fill(style *backendbase.Style, pts [][2]float64) { gl.EnableVertexAttribArray(b.sr.Vertex) gl.VertexAttribPointer(b.sr.Vertex, 2, gl.FLOAT, false, 0, nil) - gl.DrawArrays(mode, 0, int32(len(b.ptsBuf)/2)) + gl.DrawArrays(mode, 4, int32(len(pts))) gl.DisableVertexAttribArray(b.sr.Vertex) gl.ColorMask(true, true, true, true) diff --git a/backend/gogl/shadows.go b/backend/gogl/shadows.go new file mode 100644 index 0000000..f1b73c3 --- /dev/null +++ b/backend/gogl/shadows.go @@ -0,0 +1,222 @@ +package goglbackend + +/* +import ( + "image" + "math" + "unsafe" + + "github.com/go-gl/gl/v3.2-core/gl" + "github.com/tfriedel6/canvas/backend/backendbase" +) + +func (b *GoGLBackend) drawShadow(sh *backendbase.Shadow, tris []float32) { + if len(tris) == 0 || sh.Color.A == 0 { + return + } + + if sh.Blur > 0 { + b.offscr1.alpha = true + cv.enableTextureRenderTarget(&b.offscr1) + gl.ClearColor(0, 0, 0, 0) + gl.Clear(gl.COLOR_BUFFER_BIT | gl.STENCIL_BUFFER_BIT) + } + + ox, oy := float32(sh.OffsetX), float32(sh.OffsetY) + + count := len(tris) + for i := 12; i < count; i += 2 { + tris[i] += ox + tris[i+1] += oy + } + + gl.BindBuffer(gl.ARRAY_BUFFER, b.shadowBuf) + gl.BufferData(gl.ARRAY_BUFFER, len(tris)*4, unsafe.Pointer(&tris[0]), gl.STREAM_DRAW) + + gl.ColorMask(false, false, false, false) + gl.StencilFunc(gl.ALWAYS, 1, 0xFF) + gl.StencilOp(gl.REPLACE, gl.REPLACE, gl.REPLACE) + gl.StencilMask(0x01) + + gl.UseProgram(b.sr.ID) + gl.Uniform4f(b.sr.Color, 0, 0, 0, 0) + gl.Uniform2f(b.sr.CanvasSize, float32(b.fw), float32(b.fh)) + + gl.EnableVertexAttribArray(b.sr.Vertex) + gl.VertexAttribPointer(b.sr.Vertex, 2, gl.FLOAT, false, 0, nil) + gl.DrawArrays(gl.TRIANGLES, 6, int32(len(tris)/2-6)) + gl.DisableVertexAttribArray(b.sr.Vertex) + + gl.ColorMask(true, true, true, true) + + gl.StencilFunc(gl.EQUAL, 1, 0xFF) + + var style drawStyle + style.color = colorGLToGo(sh.Color) + + vertex := b.useShader(&style) + gl.EnableVertexAttribArray(vertex) + gl.VertexAttribPointer(vertex, 2, gl.FLOAT, false, 0, nil) + gl.DrawArrays(gl.TRIANGLES, 0, 6) + gl.DisableVertexAttribArray(vertex) + + gl.StencilOp(gl.KEEP, gl.KEEP, gl.KEEP) + gl.StencilFunc(gl.ALWAYS, 0, 0xFF) + + gl.Clear(gl.STENCIL_BUFFER_BIT) + gl.StencilMask(0xFF) + + if sh.Blur > 0 { + b.drawBlurredShadow() + } +} + +func (b *GoGLBackend) drawTextShadow(sh *backendbase.Shadow, offset image.Point, strWidth, strHeight int, x, y float64) { + x += sh.OffsetX + y += sh.OffsetY + + if sh.Blur > 0 { + b.offscr1.alpha = true + cv.enableTextureRenderTarget(&b.offscr1) + gl.ClearColor(0, 0, 0, 0) + gl.Clear(gl.COLOR_BUFFER_BIT | gl.STENCIL_BUFFER_BIT) + } + + gl.StencilFunc(gl.EQUAL, 0, 0xFF) + + gl.BindBuffer(gl.ARRAY_BUFFER, b.buf) + + var style drawStyle + style.color = colorGLToGo(sh.Color) + + vertex, alphaTexCoord := b.useAlphaShader(&style, 1) + + gl.EnableVertexAttribArray(vertex) + gl.EnableVertexAttribArray(alphaTexCoord) + + p0 := cv.tf(vec{float64(offset.X) + x, float64(offset.Y) + y}) + p1 := cv.tf(vec{float64(offset.X) + x, float64(offset.Y+strHeight) + y}) + p2 := cv.tf(vec{float64(offset.X+strWidth) + x, float64(offset.Y+strHeight) + y}) + p3 := cv.tf(vec{float64(offset.X+strWidth) + x, float64(offset.Y) + y}) + + tw := float64(strWidth) / alphaTexSize + th := float64(strHeight) / alphaTexSize + data := [16]float32{float32(p0[0]), float32(p0[1]), float32(p1[0]), float32(p1[1]), float32(p2[0]), float32(p2[1]), float32(p3[0]), float32(p3[1]), + 0, 1, 0, float32(1 - th), float32(tw), float32(1 - th), float32(tw), 1} + gl.BufferData(gl.ARRAY_BUFFER, len(data)*4, unsafe.Pointer(&data[0]), gl.STREAM_DRAW) + + gl.VertexAttribPointer(vertex, 2, gl.FLOAT, false, 0, 0) + gl.VertexAttribPointer(alphaTexCoord, 2, gl.FLOAT, false, 0, 8*4) + gl.DrawArrays(gl.TRIANGLE_FAN, 0, 4) + + gl.DisableVertexAttribArray(vertex) + gl.DisableVertexAttribArray(alphaTexCoord) + + gl.ActiveTexture(gl.TEXTURE0) + + gl.StencilFunc(gl.ALWAYS, 0, 0xFF) + + if cv.state.shadowBlur > 0 { + cv.drawBlurredShadow() + } +} + +func (b *GoGLBackend) drawBlurredShadow() { + gl.BlendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA) + + var kernel []float32 + var kernelBuf [255]float32 + var gs *gaussianShader + if cv.state.shadowBlur < 3 { + gs = gauss15r + kernel = kernelBuf[:15] + } else if cv.state.shadowBlur < 12 { + gs = gauss63r + kernel = kernelBuf[:63] + } else { + gs = gauss127r + kernel = kernelBuf[:127] + } + + gaussianKernel(cv.state.shadowBlur, kernel) + + offscr2.alpha = true + cv.enableTextureRenderTarget(&offscr2) + gl.ClearColor(0, 0, 0, 0) + gl.Clear(gl.COLOR_BUFFER_BIT | gl.STENCIL_BUFFER_BIT) + + gl.StencilFunc(gl.EQUAL, 0, 0xFF) + + gl.BindBuffer(gl.ARRAY_BUFFER, shadowBuf) + data := [16]float32{0, 0, 0, float32(cv.h), float32(cv.w), float32(cv.h), float32(cv.w), 0, 0, 0, 0, 1, 1, 1, 1, 0} + gl.BufferData(gl.ARRAY_BUFFER, len(data)*4, unsafe.Pointer(&data[0]), gl.STREAM_DRAW) + + gl.ActiveTexture(gl.TEXTURE0) + gl.BindTexture(gl.TEXTURE_2D, offscr1.tex) + + gl.UseProgram(gs.id) + gl.Uniform1i(gs.image, 0) + gl.Uniform2f(gs.canvasSize, float32(cv.fw), float32(cv.fh)) + gl.Uniform2f(gs.kernelScale, 1.0/float32(cv.fw), 0.0) + gl.Uniform1fv(gs.kernel, int32(len(kernel)), &kernel[0]) + gl.VertexAttribPointer(gs.vertex, 2, gl.FLOAT, false, 0, 0) + gl.VertexAttribPointer(gs.texCoord, 2, gl.FLOAT, false, 0, 8*4) + gl.EnableVertexAttribArray(gs.vertex) + gl.EnableVertexAttribArray(gs.texCoord) + gl.DrawArrays(gl.TRIANGLE_FAN, 0, 4) + gl.DisableVertexAttribArray(gs.vertex) + gl.DisableVertexAttribArray(gs.texCoord) + + gl.StencilFunc(gl.ALWAYS, 0, 0xFF) + + cv.disableTextureRenderTarget() + + gl.StencilFunc(gl.EQUAL, 0, 0xFF) + + gl.BindBuffer(gl.ARRAY_BUFFER, shadowBuf) + data = [16]float32{0, 0, 0, float32(cv.h), float32(cv.w), float32(cv.h), float32(cv.w), 0, 0, 0, 0, 1, 1, 1, 1, 0} + gl.BufferData(gl.ARRAY_BUFFER, len(data)*4, unsafe.Pointer(&data[0]), gl.STREAM_DRAW) + + gl.ActiveTexture(gl.TEXTURE0) + gl.BindTexture(gl.TEXTURE_2D, offscr2.tex) + + gl.UseProgram(gs.id) + gl.Uniform1i(gs.image, 0) + gl.Uniform2f(gs.canvasSize, float32(cv.fw), float32(cv.fh)) + gl.Uniform2f(gs.kernelScale, 0.0, 1.0/float32(cv.fh)) + gl.Uniform1fv(gs.kernel, int32(len(kernel)), &kernel[0]) + gl.VertexAttribPointer(gs.vertex, 2, gl.FLOAT, false, 0, 0) + gl.VertexAttribPointer(gs.texCoord, 2, gl.FLOAT, false, 0, 8*4) + gl.EnableVertexAttribArray(gs.vertex) + gl.EnableVertexAttribArray(gs.texCoord) + gl.DrawArrays(gl.TRIANGLE_FAN, 0, 4) + gl.DisableVertexAttribArray(gs.vertex) + gl.DisableVertexAttribArray(gs.texCoord) + + gl.StencilFunc(gl.ALWAYS, 0, 0xFF) + + gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) +} + +func gaussianKernel(stddev float64, target []float32) { + stddevSqr := stddev * stddev + center := float64(len(target) / 2) + factor := 1.0 / math.Sqrt(2*math.Pi*stddevSqr) + for i := range target { + x := float64(i) - center + target[i] = float32(factor * math.Pow(math.E, -x*x/(2*stddevSqr))) + } + // normalizeKernel(target) +} + +func normalizeKernel(kernel []float32) { + var sum float32 + for _, v := range kernel { + sum += v + } + factor := 1.0 / sum + for i := range kernel { + kernel[i] *= factor + } +} +*/ diff --git a/canvas_test.go b/canvas_test.go index 0832901..58d8b92 100644 --- a/canvas_test.go +++ b/canvas_test.go @@ -91,10 +91,12 @@ func run(t *testing.T, fn func(cv *canvas.Canvas)) { r3, g3, b3, a3 := refImg.At(x, y).RGBA() if r1 != r3 || g1 != g3 || b1 != b3 || a1 != a3 { writeImage(img, fmt.Sprintf("testdata/%s_fail.png", callerFuncName)) + t.Error("onscreen canvas failed") t.FailNow() } if r2 != r3 || g2 != g3 || b2 != b3 || a2 != a3 { writeImage(img2, fmt.Sprintf("testdata/%s_fail.png", callerFuncName)) + t.Error("offscreen canvas failed") t.FailNow() } } diff --git a/paths.go b/paths.go index a8813fd..b763f3e 100644 --- a/paths.go +++ b/paths.go @@ -2,7 +2,6 @@ package canvas import ( "math" - "unsafe" ) // BeginPath clears the current path and starts a new one @@ -101,9 +100,8 @@ func (cv *Canvas) strokePath(path *Path2D) { dashedPath := cv.applyLineDash(path.p) - var triBuf [1000]float32 + var triBuf [500][2]float64 tris := triBuf[:0] - tris = append(tris, 0, 0, float32(cv.fw), 0, float32(cv.fw), float32(cv.fh), 0, 0, float32(cv.fw), float32(cv.fh), 0, float32(cv.fh)) start := true var p0 vec @@ -148,9 +146,7 @@ func (cv *Canvas) strokePath(path *Path2D) { } } - tris = append(tris, - float32(lp0[0]), float32(lp0[1]), float32(lp1[0]), float32(lp1[1]), float32(lp3[0]), float32(lp3[1]), - float32(lp0[0]), float32(lp0[1]), float32(lp3[0]), float32(lp3[1]), float32(lp2[0]), float32(lp2[1])) + tris = append(tris, lp0, lp1, lp3, lp0, lp3, lp2) if p.flags&pathAttach != 0 && cv.state.lineWidth > 1 { tris = cv.lineJoint(p, p0, p1, p.next, lp0, lp1, lp2, lp3, tris) @@ -160,57 +156,62 @@ func (cv *Canvas) strokePath(path *Path2D) { start = false } - gli.BindBuffer(gl_ARRAY_BUFFER, buf) - gli.BufferData(gl_ARRAY_BUFFER, len(tris)*4, unsafe.Pointer(&tris[0]), gl_STREAM_DRAW) + // todo draw shadow - cv.drawShadow(tris) + stl := cv.backendStyle(&cv.state.stroke, 1) + cv.b.Fill(&stl, tris) - gli.BindBuffer(gl_ARRAY_BUFFER, buf) + // gli.BindBuffer(gl_ARRAY_BUFFER, buf) + // gli.BufferData(gl_ARRAY_BUFFER, len(tris)*4, unsafe.Pointer(&tris[0]), gl_STREAM_DRAW) - if cv.state.globalAlpha >= 1 && cv.state.lineAlpha >= 1 && cv.state.stroke.isOpaque() { - vertex := cv.useShader(&cv.state.stroke) + // cv.drawShadow(tris) - gli.EnableVertexAttribArray(vertex) - gli.VertexAttribPointer(vertex, 2, gl_FLOAT, false, 0, 0) - gli.DrawArrays(gl_TRIANGLES, 6, int32(len(tris)/2-6)) - gli.DisableVertexAttribArray(vertex) - } else { - gli.ColorMask(false, false, false, false) - gli.StencilFunc(gl_ALWAYS, 1, 0xFF) - gli.StencilOp(gl_REPLACE, gl_REPLACE, gl_REPLACE) - gli.StencilMask(0x01) + // gli.BindBuffer(gl_ARRAY_BUFFER, buf) - gli.UseProgram(sr.id) - gli.Uniform4f(sr.color, 0, 0, 0, 0) - gli.Uniform2f(sr.canvasSize, float32(cv.fw), float32(cv.fh)) + // if cv.state.globalAlpha >= 1 && cv.state.lineAlpha >= 1 && cv.state.stroke.isOpaque() { + // vertex := cv.useShader(&cv.state.stroke) - gli.EnableVertexAttribArray(sr.vertex) - gli.VertexAttribPointer(sr.vertex, 2, gl_FLOAT, false, 0, 0) - gli.DrawArrays(gl_TRIANGLES, 6, int32(len(tris)/2-6)) - gli.DisableVertexAttribArray(sr.vertex) + // gli.EnableVertexAttribArray(vertex) + // gli.VertexAttribPointer(vertex, 2, gl_FLOAT, false, 0, 0) + // gli.DrawArrays(gl_TRIANGLES, 6, int32(len(tris)/2-6)) + // gli.DisableVertexAttribArray(vertex) + // } else { + // gli.ColorMask(false, false, false, false) + // gli.StencilFunc(gl_ALWAYS, 1, 0xFF) + // gli.StencilOp(gl_REPLACE, gl_REPLACE, gl_REPLACE) + // gli.StencilMask(0x01) - gli.ColorMask(true, true, true, true) + // gli.UseProgram(sr.id) + // gli.Uniform4f(sr.color, 0, 0, 0, 0) + // gli.Uniform2f(sr.canvasSize, float32(cv.fw), float32(cv.fh)) - gli.StencilFunc(gl_EQUAL, 1, 0xFF) + // gli.EnableVertexAttribArray(sr.vertex) + // gli.VertexAttribPointer(sr.vertex, 2, gl_FLOAT, false, 0, 0) + // gli.DrawArrays(gl_TRIANGLES, 6, int32(len(tris)/2-6)) + // gli.DisableVertexAttribArray(sr.vertex) - origAlpha := cv.state.globalAlpha - if cv.state.lineAlpha < 1 { - cv.state.globalAlpha *= cv.state.lineAlpha - } - vertex := cv.useShader(&cv.state.stroke) - cv.state.globalAlpha = origAlpha + // gli.ColorMask(true, true, true, true) - gli.EnableVertexAttribArray(vertex) - gli.VertexAttribPointer(vertex, 2, gl_FLOAT, false, 0, 0) - gli.DrawArrays(gl_TRIANGLES, 0, 6) - gli.DisableVertexAttribArray(vertex) + // gli.StencilFunc(gl_EQUAL, 1, 0xFF) - gli.StencilOp(gl_KEEP, gl_KEEP, gl_KEEP) - gli.StencilFunc(gl_ALWAYS, 0, 0xFF) + // origAlpha := cv.state.globalAlpha + // if cv.state.lineAlpha < 1 { + // cv.state.globalAlpha *= cv.state.lineAlpha + // } + // vertex := cv.useShader(&cv.state.stroke) + // cv.state.globalAlpha = origAlpha - gli.Clear(gl_STENCIL_BUFFER_BIT) - gli.StencilMask(0xFF) - } + // gli.EnableVertexAttribArray(vertex) + // gli.VertexAttribPointer(vertex, 2, gl_FLOAT, false, 0, 0) + // gli.DrawArrays(gl_TRIANGLES, 0, 6) + // gli.DisableVertexAttribArray(vertex) + + // gli.StencilOp(gl_KEEP, gl_KEEP, gl_KEEP) + // gli.StencilFunc(gl_ALWAYS, 0, 0xFF) + + // gli.Clear(gl_STENCIL_BUFFER_BIT) + // gli.StencilMask(0xFF) + // } } func (cv *Canvas) applyLineDash(path []pathPoint) []pathPoint { @@ -269,7 +270,7 @@ func (cv *Canvas) applyLineDash(path []pathPoint) []pathPoint { return path2 } -func (cv *Canvas) lineJoint(p pathPoint, p0, p1, p2, l0p0, l0p1, l0p2, l0p3 vec, tris []float32) []float32 { +func (cv *Canvas) lineJoint(p pathPoint, p0, p1, p2, l0p0, l0p1, l0p2, l0p3 vec, tris [][2]float64) [][2]float64 { v2 := p1.sub(p2).norm() v3 := vec{v2[1], -v2[0]}.mulf(cv.state.lineWidth * 0.5) @@ -295,9 +296,7 @@ func (cv *Canvas) lineJoint(p pathPoint, p0, p1, p2, l0p0, l0p1, l0p2, l0p3 vec, l1p1 := p1.sub(v3) l1p3 := p1.add(v3) - tris = append(tris, - float32(p1[0]), float32(p1[1]), float32(l0p1[0]), float32(l0p1[1]), float32(l1p1[0]), float32(l1p1[1]), - float32(p1[0]), float32(p1[1]), float32(l1p3[0]), float32(l1p3[1]), float32(l0p3[0]), float32(l0p3[1])) + tris = append(tris, p1, l0p1, l1p1, p1, l1p3, l0p3) return tris } @@ -315,24 +314,16 @@ func (cv *Canvas) lineJoint(p pathPoint, p0, p1, p2, l0p0, l0p1, l0p2, l0p3 vec, l1p1 := p1.sub(v3) l1p3 := p1.add(v3) - tris = append(tris, - float32(p1[0]), float32(p1[1]), float32(l0p1[0]), float32(l0p1[1]), float32(l1p1[0]), float32(l1p1[1]), - float32(p1[0]), float32(p1[1]), float32(l1p3[0]), float32(l1p3[1]), float32(l0p3[0]), float32(l0p3[1])) + tris = append(tris, p1, l0p1, l1p1, p1, l1p3, l0p3) return tris } - tris = append(tris, - float32(p1[0]), float32(p1[1]), float32(l0p1[0]), float32(l0p1[1]), float32(ip0[0]), float32(ip0[1]), - float32(p1[0]), float32(p1[1]), float32(ip0[0]), float32(ip0[1]), float32(l1p1[0]), float32(l1p1[1]), - float32(p1[0]), float32(p1[1]), float32(l1p3[0]), float32(l1p3[1]), float32(ip1[0]), float32(ip1[1]), - float32(p1[0]), float32(p1[1]), float32(ip1[0]), float32(ip1[1]), float32(l0p3[0]), float32(l0p3[1])) + tris = append(tris, p1, l0p1, ip0, p1, ip0, l1p1, p1, l1p3, ip1, p1, ip1, l0p3) case Bevel: l1p1 := p1.sub(v3) l1p3 := p1.add(v3) - tris = append(tris, - float32(p1[0]), float32(p1[1]), float32(l0p1[0]), float32(l0p1[1]), float32(l1p1[0]), float32(l1p1[1]), - float32(p1[0]), float32(p1[1]), float32(l1p3[0]), float32(l1p3[1]), float32(l0p3[0]), float32(l0p3[1])) + tris = append(tris, p1, l0p1, l1p1, p1, l1p3, l0p3) case Round: tris = cv.addCircleTris(p1, cv.state.lineWidth*0.5, tris) } @@ -340,7 +331,7 @@ func (cv *Canvas) lineJoint(p pathPoint, p0, p1, p2, l0p0, l0p1, l0p2, l0p3 vec, return tris } -func (cv *Canvas) addCircleTris(center vec, radius float64, tris []float32) []float32 { +func (cv *Canvas) addCircleTris(center vec, radius float64, tris [][2]float64) [][2]float64 { step := 6 / radius if step > 0.8 { step = 0.8 @@ -351,8 +342,7 @@ func (cv *Canvas) addCircleTris(center vec, radius float64, tris []float32) []fl for angle := step; angle <= math.Pi*2+step; angle += step { s, c := math.Sincos(angle) p1 := vec{center[0] + s*radius, center[1] + c*radius} - tris = append(tris, - float32(center[0]), float32(center[1]), float32(p0[0]), float32(p0[1]), float32(p1[0]), float32(p1[1])) + tris = append(tris, center, p0, p1) p0 = p1 } return tris @@ -392,9 +382,8 @@ func (cv *Canvas) FillPath(path *Path2D) { } cv.activate() - var triBuf [1000]float32 + var triBuf [500][2]float64 tris := triBuf[:0] - tris = append(tris, 0, 0, float32(cv.fw), 0, float32(cv.fw), float32(cv.fh), 0, 0, float32(cv.fw), float32(cv.fh), 0, float32(cv.fh)) start := 0 for i, p := range path.p { @@ -413,61 +402,66 @@ func (cv *Canvas) FillPath(path *Path2D) { return } - gli.BindBuffer(gl_ARRAY_BUFFER, buf) - gli.BufferData(gl_ARRAY_BUFFER, len(tris)*4, unsafe.Pointer(&tris[0]), gl_STREAM_DRAW) + // todo draw shadow - cv.drawShadow(tris) + stl := cv.backendStyle(&cv.state.fill, 1) + cv.b.Fill(&stl, tris) - gli.BindBuffer(gl_ARRAY_BUFFER, buf) + // gli.BindBuffer(gl_ARRAY_BUFFER, buf) + // gli.BufferData(gl_ARRAY_BUFFER, len(tris)*4, unsafe.Pointer(&tris[0]), gl_STREAM_DRAW) - if cv.state.globalAlpha >= 1 && cv.state.lineAlpha >= 1 && cv.state.fill.isOpaque() { - vertex := cv.useShader(&cv.state.fill) + // cv.drawShadow(tris) - gli.EnableVertexAttribArray(vertex) - gli.VertexAttribPointer(vertex, 2, gl_FLOAT, false, 0, 0) - gli.DrawArrays(gl_TRIANGLES, 6, int32(len(tris)/2-6)) - gli.DisableVertexAttribArray(vertex) - } else { - gli.ColorMask(false, false, false, false) - gli.StencilFunc(gl_ALWAYS, 1, 0xFF) - gli.StencilOp(gl_REPLACE, gl_REPLACE, gl_REPLACE) - gli.StencilMask(0x01) + // gli.BindBuffer(gl_ARRAY_BUFFER, buf) - gli.UseProgram(sr.id) - gli.Uniform4f(sr.color, 0, 0, 0, 0) - gli.Uniform2f(sr.canvasSize, float32(cv.fw), float32(cv.fh)) + // if cv.state.globalAlpha >= 1 && cv.state.lineAlpha >= 1 && cv.state.fill.isOpaque() { + // vertex := cv.useShader(&cv.state.fill) - gli.EnableVertexAttribArray(sr.vertex) - gli.VertexAttribPointer(sr.vertex, 2, gl_FLOAT, false, 0, 0) - gli.DrawArrays(gl_TRIANGLES, 6, int32(len(tris)/2-6)) - gli.DisableVertexAttribArray(sr.vertex) + // gli.EnableVertexAttribArray(vertex) + // gli.VertexAttribPointer(vertex, 2, gl_FLOAT, false, 0, 0) + // gli.DrawArrays(gl_TRIANGLES, 6, int32(len(tris)/2-6)) + // gli.DisableVertexAttribArray(vertex) + // } else { + // gli.ColorMask(false, false, false, false) + // gli.StencilFunc(gl_ALWAYS, 1, 0xFF) + // gli.StencilOp(gl_REPLACE, gl_REPLACE, gl_REPLACE) + // gli.StencilMask(0x01) - gli.ColorMask(true, true, true, true) + // gli.UseProgram(sr.id) + // gli.Uniform4f(sr.color, 0, 0, 0, 0) + // gli.Uniform2f(sr.canvasSize, float32(cv.fw), float32(cv.fh)) - gli.StencilFunc(gl_EQUAL, 1, 0xFF) + // gli.EnableVertexAttribArray(sr.vertex) + // gli.VertexAttribPointer(sr.vertex, 2, gl_FLOAT, false, 0, 0) + // gli.DrawArrays(gl_TRIANGLES, 6, int32(len(tris)/2-6)) + // gli.DisableVertexAttribArray(sr.vertex) - vertex := cv.useShader(&cv.state.fill) - gli.EnableVertexAttribArray(vertex) - gli.VertexAttribPointer(vertex, 2, gl_FLOAT, false, 0, 0) - gli.DrawArrays(gl_TRIANGLES, 0, 6) - gli.DisableVertexAttribArray(vertex) + // gli.ColorMask(true, true, true, true) - gli.StencilOp(gl_KEEP, gl_KEEP, gl_KEEP) - gli.StencilFunc(gl_ALWAYS, 0, 0xFF) + // gli.StencilFunc(gl_EQUAL, 1, 0xFF) - gli.Clear(gl_STENCIL_BUFFER_BIT) - gli.StencilMask(0xFF) - } + // vertex := cv.useShader(&cv.state.fill) + // gli.EnableVertexAttribArray(vertex) + // gli.VertexAttribPointer(vertex, 2, gl_FLOAT, false, 0, 0) + // gli.DrawArrays(gl_TRIANGLES, 0, 6) + // gli.DisableVertexAttribArray(vertex) + + // gli.StencilOp(gl_KEEP, gl_KEEP, gl_KEEP) + // gli.StencilFunc(gl_ALWAYS, 0, 0xFF) + + // gli.Clear(gl_STENCIL_BUFFER_BIT) + // gli.StencilMask(0xFF) + // } } -func (cv *Canvas) appendSubPathTriangles(tris []float32, path []pathPoint) []float32 { +func (cv *Canvas) appendSubPathTriangles(tris [][2]float64, path []pathPoint) [][2]float64 { last := path[len(path)-1] if last.flags&pathIsConvex != 0 { p0, p1 := path[0].pos, path[1].pos last := len(path) for i := 2; i < last; i++ { p2 := path[i].pos - tris = append(tris, float32(p0[0]), float32(p0[1]), float32(p1[0]), float32(p1[1]), float32(p2[0]), float32(p2[1])) + tris = append(tris, p0, p1, p2) p1 = p2 } } else if last.flags&pathSelfIntersects != 0 { @@ -506,53 +500,53 @@ func (cv *Canvas) clip(path []pathPoint) { return } - cv.activate() + // cv.activate() - var triBuf [1000]float32 - tris := triBuf[:0] - tris = append(tris, 0, 0, float32(cv.fw), 0, float32(cv.fw), float32(cv.fh), 0, 0, float32(cv.fw), float32(cv.fh), 0, float32(cv.fh)) - baseLen := len(tris) - tris = triangulatePath(path, tris) - if len(tris) <= baseLen { - return - } + // var triBuf [1000]float32 + // tris := triBuf[:0] + // tris = append(tris, 0, 0, float32(cv.fw), 0, float32(cv.fw), float32(cv.fh), 0, 0, float32(cv.fw), float32(cv.fh), 0, float32(cv.fh)) + // baseLen := len(tris) + // tris = triangulatePath(path, tris) + // if len(tris) <= baseLen { + // return + // } - gli.BindBuffer(gl_ARRAY_BUFFER, buf) - gli.BufferData(gl_ARRAY_BUFFER, len(tris)*4, unsafe.Pointer(&tris[0]), gl_STREAM_DRAW) - gli.VertexAttribPointer(sr.vertex, 2, gl_FLOAT, false, 0, 0) + // gli.BindBuffer(gl_ARRAY_BUFFER, buf) + // gli.BufferData(gl_ARRAY_BUFFER, len(tris)*4, unsafe.Pointer(&tris[0]), gl_STREAM_DRAW) + // gli.VertexAttribPointer(sr.vertex, 2, gl_FLOAT, false, 0, 0) - gli.UseProgram(sr.id) - gli.Uniform4f(sr.color, 1, 1, 1, 1) - gli.Uniform2f(sr.canvasSize, float32(cv.fw), float32(cv.fh)) - gli.EnableVertexAttribArray(sr.vertex) + // gli.UseProgram(sr.id) + // gli.Uniform4f(sr.color, 1, 1, 1, 1) + // gli.Uniform2f(sr.canvasSize, float32(cv.fw), float32(cv.fh)) + // gli.EnableVertexAttribArray(sr.vertex) - gli.ColorMask(false, false, false, false) + // gli.ColorMask(false, false, false, false) - gli.StencilMask(0x04) - gli.StencilFunc(gl_ALWAYS, 4, 0x04) - gli.StencilOp(gl_REPLACE, gl_REPLACE, gl_REPLACE) - gli.DrawArrays(gl_TRIANGLES, 6, int32(len(tris)/2-6)) + // gli.StencilMask(0x04) + // gli.StencilFunc(gl_ALWAYS, 4, 0x04) + // gli.StencilOp(gl_REPLACE, gl_REPLACE, gl_REPLACE) + // gli.DrawArrays(gl_TRIANGLES, 6, int32(len(tris)/2-6)) - gli.StencilMask(0x02) - gli.StencilFunc(gl_EQUAL, 0, 0x06) - gli.StencilOp(gl_KEEP, gl_INVERT, gl_INVERT) - gli.DrawArrays(gl_TRIANGLES, 0, 6) + // gli.StencilMask(0x02) + // gli.StencilFunc(gl_EQUAL, 0, 0x06) + // gli.StencilOp(gl_KEEP, gl_INVERT, gl_INVERT) + // gli.DrawArrays(gl_TRIANGLES, 0, 6) - gli.StencilMask(0x04) - gli.StencilFunc(gl_ALWAYS, 0, 0x04) - gli.StencilOp(gl_ZERO, gl_ZERO, gl_ZERO) - gli.DrawArrays(gl_TRIANGLES, 0, 6) + // gli.StencilMask(0x04) + // gli.StencilFunc(gl_ALWAYS, 0, 0x04) + // gli.StencilOp(gl_ZERO, gl_ZERO, gl_ZERO) + // gli.DrawArrays(gl_TRIANGLES, 0, 6) - gli.DisableVertexAttribArray(sr.vertex) + // gli.DisableVertexAttribArray(sr.vertex) - gli.ColorMask(true, true, true, true) - gli.StencilOp(gl_KEEP, gl_KEEP, gl_KEEP) - gli.StencilMask(0xFF) - gli.StencilFunc(gl_EQUAL, 0, 0xFF) + // gli.ColorMask(true, true, true, true) + // gli.StencilOp(gl_KEEP, gl_KEEP, gl_KEEP) + // gli.StencilMask(0xFF) + // gli.StencilFunc(gl_EQUAL, 0, 0xFF) - cv.state.clip = cv.path - cv.state.clip.p = make([]pathPoint, len(cv.path.p)) - copy(cv.state.clip.p, cv.path.p) + // cv.state.clip = cv.path + // cv.state.clip.p = make([]pathPoint, len(cv.path.p)) + // copy(cv.state.clip.p, cv.path.p) } func (cv *Canvas) scissor(path []pathPoint) { diff --git a/sdlcanvas/sdlcanvas.go b/sdlcanvas/sdlcanvas.go index 18b3229..096b87c 100644 --- a/sdlcanvas/sdlcanvas.go +++ b/sdlcanvas/sdlcanvas.go @@ -89,7 +89,7 @@ func CreateWindow(w, h int, title string) (*Window, *canvas.Canvas, error) { return nil, nil, fmt.Errorf("Error initializing GL: %v", err) } - backend, err := goglbackend.New(0, 0, 1280, 720) + backend, err := goglbackend.New(0, 0, w, h) if err != nil { log.Fatalf("Error loading GoGL backend: %v", err) } diff --git a/triangulation.go b/triangulation.go index df92afd..2bb5da1 100644 --- a/triangulation.go +++ b/triangulation.go @@ -62,7 +62,7 @@ func polygonContainsPoint(polygon []vec, p vec) bool { return count%2 == 1 } -func triangulatePath(path []pathPoint, target []float32) []float32 { +func triangulatePath(path []pathPoint, target [][2]float64) [][2]float64 { var buf [500]vec polygon := buf[:0] for _, p := range path { @@ -93,7 +93,7 @@ func triangulatePath(path []pathPoint, target []float32) []float32 { continue triangles } } - target = append(target, float32(a[0]), float32(a[1]), float32(b[0]), float32(b[1]), float32(c[0]), float32(c[1])) + target = append(target, a, b, c) break } remove := (i + 1) % len(polygon)