unified Clear and ClearRect

This commit is contained in:
Thomas Friedel 2019-02-22 11:50:23 +01:00
parent e17505b056
commit cf788d55f3
4 changed files with 25 additions and 16 deletions

View file

@ -14,7 +14,7 @@ type Backend interface {
LoadLinearGradient(data *LinearGradientData) LinearGradient
LoadRadialGradient(data *RadialGradientData) RadialGradient
ClearRect(x, y, w, h int)
// ClearRect(x, y, w, h int)
Clear(pts [4][2]float64)
Fill(style *FillStyle, pts [][2]float64)
DrawImage(dimg Image, sx, sy, sw, sh, dx, dy, dw, dh float64, alpha float64)

View file

@ -9,15 +9,22 @@ import (
"github.com/tfriedel6/canvas/backend/backendbase"
)
// ClearRect sets the color of the rectangle to transparent black
func (b *GoGLBackend) ClearRect(x, y, w, h int) {
gl.Scissor(int32(x), int32(b.h-y-h), int32(w), int32(h))
gl.ClearColor(0, 0, 0, 0)
gl.Clear(gl.COLOR_BUFFER_BIT)
// cv.applyScissor()
}
func (b *GoGLBackend) Clear(pts [4][2]float64) {
// first check if the four points are aligned to form a nice rectangle, which can be more easily
// cleared using glScissor and glClear
aligned := pts[0][0] == pts[1][0] && pts[2][0] == pts[3][0] && pts[0][1] == pts[3][1] && pts[1][1] == pts[2][1]
if !aligned {
aligned = pts[0][0] == pts[3][0] && pts[1][0] == pts[2][0] && pts[0][1] == pts[1][1] && pts[2][1] == pts[3][1]
}
if aligned {
minX := math.Floor(math.Min(pts[0][0], pts[2][0]))
maxX := math.Ceil(math.Max(pts[0][0], pts[2][0]))
minY := math.Floor(math.Min(pts[0][1], pts[2][1]))
maxY := math.Ceil(math.Max(pts[0][1], pts[2][1]))
b.clearRect(int(minX), int(minY), int(maxX)-int(minX), int(maxY)-int(minY))
return
}
data := [8]float32{
float32(pts[0][0]), float32(pts[0][1]),
float32(pts[1][0]), float32(pts[1][1]),
@ -46,6 +53,13 @@ func (b *GoGLBackend) Clear(pts [4][2]float64) {
gl.Enable(gl.BLEND)
}
func (b *GoGLBackend) clearRect(x, y, w, h int) {
gl.Scissor(int32(x), int32(b.h-y-h), int32(w), int32(h))
gl.ClearColor(0, 0, 0, 0)
gl.Clear(gl.COLOR_BUFFER_BIT)
// cv.applyScissor()
}
func (b *GoGLBackend) Fill(style *backendbase.FillStyle, pts [][2]float64) {
if style.Blur > 0 {
b.offscr1.alpha = true

View file

@ -414,6 +414,8 @@ func (b *GoGLBackend) enableTextureRenderTarget(offscr *offscreenBuffer) {
}
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
gl.GenFramebuffers(1, &offscr.frameBuf)
gl.BindFramebuffer(gl.FRAMEBUFFER, offscr.frameBuf)

View file

@ -536,17 +536,10 @@ func (cv *Canvas) FillRect(x, y, w, h float64) {
func (cv *Canvas) ClearRect(x, y, w, h float64) {
cv.activate()
if cv.state.transform == matIdentity() {
cv.b.ClearRect(int(x+0.5), int(y+0.5), int(w+0.5), int(h+0.5))
cv.applyScissor()
return
}
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})
data := [4][2]float64{{p0[0], p0[1]}, {p1[0], p1[1]}, {p2[0], p2[1]}, {p3[0], p3[1]}}
cv.b.Clear(data)