From e79a8c37e60ff00111fe91cd97bf5ad15bc9f9a8 Mon Sep 17 00:00:00 2001
From: Allen Ray <allentray125@gmail.com>
Date: Sun, 21 Jun 2020 22:51:08 -0400
Subject: [PATCH] Adding support for clipping rectangles in GLTriangles

---
 pixelgl/canvas.go      |  3 +++
 pixelgl/gltriangles.go | 12 ++++++++++++
 2 files changed, 15 insertions(+)

diff --git a/pixelgl/canvas.go b/pixelgl/canvas.go
index 25da939..5bc8437 100644
--- a/pixelgl/canvas.go
+++ b/pixelgl/canvas.go
@@ -327,6 +327,9 @@ func (ct *canvasTriangles) draw(tex *glhf.Texture, bounds pixel.Rect) {
 			}
 
 			ct.vs.Begin()
+			if clip, has := ct.ClipRect(); has {
+				gl.Scissor(int32(clip.Min.X), int32(clip.Min.Y), int32(clip.W()), int32(clip.H()))
+			}
 			ct.vs.Draw()
 			ct.vs.End()
 
diff --git a/pixelgl/gltriangles.go b/pixelgl/gltriangles.go
index f258102..557f420 100644
--- a/pixelgl/gltriangles.go
+++ b/pixelgl/gltriangles.go
@@ -16,6 +16,7 @@ type GLTriangles struct {
 	vs     *glhf.VertexSlice
 	data   []float32
 	shader *glhf.Shader
+	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
+}
-- 
GitLab