From 964380f6732e1dcc091670222048aa2c08a1b10d Mon Sep 17 00:00:00 2001 From: Thomas Friedel Date: Wed, 23 May 2018 12:32:43 +0200 Subject: [PATCH] implemented clearRect --- README.md | 2 +- glimpl/android/glandroidimpl.go | 6 +++++ glimpl/gogl/goglimpl.go | 6 +++++ glimpl/ios/gliosimpl.go | 6 +++++ openglinterface.go | 2 ++ paths.go | 40 +++++++++++++++++++++++++++++++++ 6 files changed, 61 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2b33e9d..31fba1c 100644 --- a/README.md +++ b/README.md @@ -112,6 +112,7 @@ These features *should* work just like their HTML5 counterparts, but there are l - drawImage - getImageData - putImageData +- clearRect # Missing features @@ -120,7 +121,6 @@ These features *should* work just like their HTML5 counterparts, but there are l - miterLimit - shadows - textBaseline -- clearRect - getLineDash - isPointInPath - isPointInStroke diff --git a/glimpl/android/glandroidimpl.go b/glimpl/android/glandroidimpl.go index 035e0a0..13e81be 100644 --- a/glimpl/android/glandroidimpl.go +++ b/glimpl/android/glandroidimpl.go @@ -64,6 +64,9 @@ func (_ GLImpl) BufferData(target uint32, size int, data unsafe.Pointer, usage u func (_ GLImpl) Clear(mask uint32) { C.glClear(C.uint(mask)) } +func (_ GLImpl) ClearColor(red float32, green float32, blue float32, alpha float32) { + C.glClearColor(C.float(red), C.float(green), C.float(blue), C.float(alpha)) +} func (_ GLImpl) ColorMask(red bool, green bool, blue bool, alpha bool) { var r, g, b, a C.uchar if red { @@ -95,6 +98,9 @@ func (_ GLImpl) DeleteShader(shader uint32) { func (_ GLImpl) DeleteTextures(n int32, textures *uint32) { C.glDeleteTextures(C.int(n), (*C.uint)(textures)) } +func (_ GLImpl) Disable(cap uint32) { + C.glDisable(C.uint(cap)) +} func (_ GLImpl) DisableVertexAttribArray(index uint32) { C.glDisableVertexAttribArray(C.uint(index)) } diff --git a/glimpl/gogl/goglimpl.go b/glimpl/gogl/goglimpl.go index a8ff0a4..f1a2a62 100644 --- a/glimpl/gogl/goglimpl.go +++ b/glimpl/gogl/goglimpl.go @@ -36,6 +36,9 @@ func (_ GLImpl) BufferData(target uint32, size int, data unsafe.Pointer, usage u func (_ GLImpl) Clear(mask uint32) { gl.Clear(mask) } +func (_ GLImpl) ClearColor(red float32, green float32, blue float32, alpha float32) { + gl.ClearColor(red, green, blue, alpha) +} func (_ GLImpl) ColorMask(red bool, green bool, blue bool, alpha bool) { gl.ColorMask(red, green, blue, alpha) } @@ -54,6 +57,9 @@ func (_ GLImpl) DeleteShader(shader uint32) { func (_ GLImpl) DeleteTextures(n int32, textures *uint32) { gl.DeleteTextures(n, textures) } +func (_ GLImpl) Disable(cap uint32) { + gl.Disable(cap) +} func (_ GLImpl) DisableVertexAttribArray(index uint32) { gl.DisableVertexAttribArray(index) } diff --git a/glimpl/ios/gliosimpl.go b/glimpl/ios/gliosimpl.go index 6c459de..66884d9 100644 --- a/glimpl/ios/gliosimpl.go +++ b/glimpl/ios/gliosimpl.go @@ -64,6 +64,9 @@ func (_ GLImpl) BufferData(target uint32, size int, data unsafe.Pointer, usage u func (_ GLImpl) Clear(mask uint32) { C.glClear(C.GLbitfield(mask)) } +func (_ GLImpl) ClearColor(red float32, green float32, blue float32, alpha float32) { + C.glClearColor(C.GLfloat(red), C.GLfloat(green), C.GLfloat(blue), C.GLfloat(alpha)) +} func (_ GLImpl) ColorMask(red bool, green bool, blue bool, alpha bool) { var r, g, b, a C.GLboolean if red { @@ -95,6 +98,9 @@ func (_ GLImpl) DeleteShader(shader uint32) { func (_ GLImpl) DeleteTextures(n int32, textures *uint32) { C.glDeleteTextures(C.GLsizei(n), (*C.GLuint)(textures)) } +func (_ GLImpl) Disable(cap uint32) { + C.glDisable(C.GLenum(cap)) +} func (_ GLImpl) DisableVertexAttribArray(index uint32) { C.glDisableVertexAttribArray(C.GLuint(index)) } diff --git a/openglinterface.go b/openglinterface.go index 32f5fe3..9bd4cbd 100644 --- a/openglinterface.go +++ b/openglinterface.go @@ -801,12 +801,14 @@ type GL interface { BlendFunc(sfactor uint32, dfactor uint32) BufferData(target uint32, size int, data unsafe.Pointer, usage uint32) Clear(mask uint32) + ClearColor(red float32, green float32, blue float32, alpha float32) ColorMask(red bool, green bool, blue bool, alpha bool) CompileShader(shader uint32) CreateProgram() uint32 CreateShader(xtype uint32) uint32 DeleteShader(shader uint32) DeleteTextures(n int32, textures *uint32) + Disable(cap uint32) DisableVertexAttribArray(index uint32) DrawArrays(mode uint32, first int32, count int32) Enable(cap uint32) diff --git a/paths.go b/paths.go index 9fddf57..d79fc75 100644 --- a/paths.go +++ b/paths.go @@ -714,3 +714,43 @@ func (cv *Canvas) FillRect(x, y, w, h float64) { gli.StencilFunc(gl_ALWAYS, 0, 0xFF) } + +// ClearRect sets the color of the rectangle to transparent black +func (cv *Canvas) ClearRect(x, y, w, h float64) { + cv.activate() + + if cv.state.transform == matIdentity() { + gli.Scissor(int32(x+0.5), int32(cv.fh-y-h+0.5), int32(w+0.5), int32(h+0.5)) + gli.ClearColor(0, 0, 0, 0) + gli.Clear(gl_COLOR_BUFFER_BIT) + cv.applyScissor() + return + } + + gli.UseProgram(sr.id) + gli.Uniform2f(sr.canvasSize, float32(cv.fw), float32(cv.fh)) + gli.Uniform4f(sr.color, 0, 0, 0, 0) + gli.Uniform1f(sr.globalAlpha, 1) + + gli.Disable(gl_BLEND) + + p0 := cv.tf(vec{x, y}) + p1 := cv.tf(vec{x, y + h}) + p2 := cv.tf(vec{x + w, y + h}) + p3 := cv.tf(vec{x + w, y}) + + gli.BindBuffer(gl_ARRAY_BUFFER, buf) + data := [8]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])} + gli.BufferData(gl_ARRAY_BUFFER, len(data)*4, unsafe.Pointer(&data[0]), gl_STREAM_DRAW) + + gli.StencilFunc(gl_EQUAL, 0, 0xFF) + + gli.VertexAttribPointer(sr.vertex, 2, gl_FLOAT, false, 0, 0) + gli.EnableVertexAttribArray(sr.vertex) + gli.DrawArrays(gl_TRIANGLE_FAN, 0, 4) + gli.DisableVertexAttribArray(sr.vertex) + + gli.StencilFunc(gl_ALWAYS, 0, 0xFF) + + gli.Enable(gl_BLEND) +}