diff --git a/backend/gogl/clip.go b/backend/gogl/clip.go index 7da6254..f90558a 100644 --- a/backend/gogl/clip.go +++ b/backend/gogl/clip.go @@ -7,6 +7,7 @@ import ( ) func (b *GoGLBackend) ClearClip() { + b.curClip = nil b.activate() gl.StencilMask(0xFF) @@ -14,7 +15,9 @@ func (b *GoGLBackend) ClearClip() { } func (b *GoGLBackend) Clip(pts [][2]float64) { + b.curClip = nil b.activate() + b.curClip = pts b.ptsBuf = b.ptsBuf[:0] b.ptsBuf = append(b.ptsBuf, diff --git a/backend/gogl/fill.go b/backend/gogl/fill.go index 5346150..77ab226 100644 --- a/backend/gogl/fill.go +++ b/backend/gogl/fill.go @@ -100,10 +100,12 @@ func (b *GoGLBackend) Fill(style *backendbase.FillStyle, pts [][2]float64) { if style.Color.A >= 255 { vertex := b.useShader(style) + gl.StencilFunc(gl.EQUAL, 0, 0xFF) gl.EnableVertexAttribArray(vertex) gl.VertexAttribPointer(vertex, 2, gl.FLOAT, false, 0, nil) gl.DrawArrays(mode, 4, int32(len(pts))) gl.DisableVertexAttribArray(vertex) + gl.StencilFunc(gl.ALWAYS, 0, 0xFF) } else { gl.ColorMask(false, false, false, false) gl.StencilFunc(gl.ALWAYS, 1, 0xFF) diff --git a/backend/gogl/gogl.go b/backend/gogl/gogl.go index 7f982dd..894b1b6 100644 --- a/backend/gogl/gogl.go +++ b/backend/gogl/gogl.go @@ -206,6 +206,8 @@ type GoGLBackend struct { *GLContext + curClip [][2]float64 + activateFn func() disableTextureRenderTarget func() } @@ -244,6 +246,11 @@ func New(x, y, w, h int, ctx *GLContext) (*GoGLBackend, error) { b.activateFn = func() { gl.BindFramebuffer(gl.FRAMEBUFFER, 0) gl.Viewport(int32(b.x), int32(b.y), int32(b.w), int32(b.h)) + gl.Clear(gl.STENCIL_BUFFER_BIT) + if c := b.curClip; c != nil { + b.curClip = nil + b.Clip(c) + } } b.disableTextureRenderTarget = func() { gl.BindFramebuffer(gl.FRAMEBUFFER, 0) diff --git a/backend/xmobile/clip.go b/backend/xmobile/clip.go index b088e87..9e48578 100755 --- a/backend/xmobile/clip.go +++ b/backend/xmobile/clip.go @@ -7,6 +7,7 @@ import ( ) func (b *XMobileBackend) ClearClip() { + b.curClip = nil b.activate() b.glctx.StencilMask(0xFF) @@ -14,7 +15,9 @@ func (b *XMobileBackend) ClearClip() { } func (b *XMobileBackend) Clip(pts [][2]float64) { + b.curClip = nil b.activate() + b.curClip = pts b.ptsBuf = b.ptsBuf[:0] b.ptsBuf = append(b.ptsBuf, diff --git a/backend/xmobile/fill.go b/backend/xmobile/fill.go index 15fb54c..bbd645f 100755 --- a/backend/xmobile/fill.go +++ b/backend/xmobile/fill.go @@ -100,10 +100,12 @@ func (b *XMobileBackend) Fill(style *backendbase.FillStyle, pts [][2]float64) { if style.Color.A >= 255 { vertex := b.useShader(style) + b.glctx.StencilFunc(gl.EQUAL, 0, 0xFF) b.glctx.EnableVertexAttribArray(vertex) b.glctx.VertexAttribPointer(vertex, 2, gl.FLOAT, false, 0, 0) b.glctx.DrawArrays(mode, 4, len(pts)) b.glctx.DisableVertexAttribArray(vertex) + b.glctx.StencilFunc(gl.ALWAYS, 0, 0xFF) } else { b.glctx.ColorMask(false, false, false, false) b.glctx.StencilFunc(gl.ALWAYS, 1, 0xFF) diff --git a/backend/xmobile/xmobile.go b/backend/xmobile/xmobile.go index 7a27c3c..b51288b 100755 --- a/backend/xmobile/xmobile.go +++ b/backend/xmobile/xmobile.go @@ -211,6 +211,8 @@ type XMobileBackend struct { *GLContext + curClip [][2]float64 + activateFn func() disableTextureRenderTarget func() } @@ -239,6 +241,11 @@ func New(x, y, w, h int, ctx *GLContext) (*XMobileBackend, error) { b.activateFn = func() { b.glctx.BindFramebuffer(gl.FRAMEBUFFER, gl.Framebuffer{Value: 0}) b.glctx.Viewport(b.x, b.y, b.w, b.h) + b.glctx.Clear(gl.STENCIL_BUFFER_BIT) + if c := b.curClip; c != nil { + b.curClip = nil + b.Clip(c) + } } b.disableTextureRenderTarget = func() { b.glctx.BindFramebuffer(gl.FRAMEBUFFER, gl.Framebuffer{Value: 0})