diff --git a/CHANGELOG.md b/CHANGELOG.md index f76339c..65080ac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Support hiding the window initially - Support creating maximized windows - Support waiting for events to reduce CPU load +- Adding clipping rectangle support in GLTriangles ## [v0.10.0-beta] 2020-05-10 - Add `WindowConfig.TransparentFramebuffer` option to support window transparency onto the background diff --git a/pixelgl/canvas.go b/pixelgl/canvas.go index 6cb06e3..2cb310b 100644 --- a/pixelgl/canvas.go +++ b/pixelgl/canvas.go @@ -4,6 +4,8 @@ import ( "fmt" "image/color" + "github.com/go-gl/gl/v3.3-core/gl" + "github.com/faiface/glhf" "github.com/faiface/mainthread" "github.com/faiface/pixel" @@ -320,6 +322,10 @@ func (ct *canvasTriangles) draw(tex *glhf.Texture, bounds pixel.Rect) { ct.shader.s.SetUniformAttr(loc, u.Value()) } + if clip, has := ct.ClipRect(); has { + gl.Scissor(int32(clip.Min.X), int32(clip.Min.Y), int32(clip.W()), int32(clip.H())) + } + if tex == nil { ct.vs.Begin() ct.vs.Draw() diff --git a/pixelgl/gltriangles.go b/pixelgl/gltriangles.go index 698fe53..1cbcbe0 100644 --- a/pixelgl/gltriangles.go +++ b/pixelgl/gltriangles.go @@ -16,6 +16,7 @@ type GLTriangles struct { vs *glhf.VertexSlice data []float32 shader *GLShader + clip pixel.Rect } var ( @@ -213,3 +214,14 @@ func (gt *GLTriangles) Picture(i int) (pic pixel.Vec, intensity float64) { intensity = float64(gt.data[i*gt.vs.Stride()+8]) return pixel.V(float64(tx), float64(ty)), intensity } + +// SetClipRect sets the rectangle to scissor the triangles by +func (gt *GLTriangles) SetClipRect(r pixel.Rect) { + gt.clip = r.Norm() +} + +// ClipRect gets the clipping rectangle and returns true if that +// rectangle is not the Zero Rectangle +func (gt *GLTriangles) ClipRect() (pixel.Rect, bool) { + return gt.clip, gt.clip.Area() != 0 +}