From bb11c498b7d4fe269e7273a1b164e46f3c7a3b50 Mon Sep 17 00:00:00 2001
From: Sven Göthel <sgothel@jausoft.com>
Date: Tue, 9 Jan 2024 03:13:20 +0100
Subject: Graph Clipping: Initial Region impl of AABBox clipping using GLSL
 shader (Convenient using Graph/GraphUI produced AABBox)

Simple demo, setting clip-bbox manually:
- src/demos/com/jogamp/opengl/demos/graph/ui/UIShapeClippingDemo00.java

TODO:
- GLSL: Add missing Mv-multiplication of vertex-position -> gcv_ClipBBoxCoord
-- AABBox min/max should be set pre-multiplied w/ Mv covering an independent area, not per Shape/Region.
-- This to properly work with moving/scaling of each Shape/Region etc
---
 .../com/jogamp/graph/curve/opengl/GLRegion.java    |  9 +++----
 .../jogamp/graph/curve/opengl/RegionRenderer.java  | 28 ++++++++++++++++++----
 .../com/jogamp/graph/curve/opengl/RenderState.java | 11 ++++++---
 3 files changed, 37 insertions(+), 11 deletions(-)

(limited to 'src/jogl/classes/com/jogamp/graph/curve/opengl')

diff --git a/src/jogl/classes/com/jogamp/graph/curve/opengl/GLRegion.java b/src/jogl/classes/com/jogamp/graph/curve/opengl/GLRegion.java
index 2cd40c6e4..af8a54177 100644
--- a/src/jogl/classes/com/jogamp/graph/curve/opengl/GLRegion.java
+++ b/src/jogl/classes/com/jogamp/graph/curve/opengl/GLRegion.java
@@ -366,6 +366,7 @@ public abstract class GLRegion extends Region {
         // gca_VerticesAttr.putf(coords, 0, 3);
         // gca_CurveParamsAttr.putf(texParams, 0, 3);
         // gca_VerticesAttr.put3f(coords.x(), coords.y(), coords.z());
+        // System.err.println("GLRegion V: "+coords);
         put3f((FloatBuffer)vpc_ileave.getBuffer(), coords);
         put3f((FloatBuffer)vpc_ileave.getBuffer(), texParams);
         if( hasColorChannel() ) {
@@ -384,6 +385,7 @@ public abstract class GLRegion extends Region {
         if( cc && null == rgba ) {
             throw new IllegalArgumentException("Null color given for COLOR_CHANNEL rendering mode");
         }
+        // System.err.println("GLRegion V: "+coords1+", "+coords2+", "+coords3);
         put3f((FloatBuffer)vpc_ileave.getBuffer(), coords1);
         put3f((FloatBuffer)vpc_ileave.getBuffer(), texParams1);
         if( cc ) {
@@ -531,7 +533,7 @@ public abstract class GLRegion extends Region {
             markStateDirty();
         }
         if( isShapeDirty() ) {
-            updateImpl(gl, curRenderModes);
+            updateImpl(gl, renderer, curRenderModes);
         }
         drawImpl(gl, renderer, curRenderModes, sampleCount);
         clearDirtyBits(DIRTY_SHAPE|DIRTY_STATE);
@@ -544,9 +546,8 @@ public abstract class GLRegion extends Region {
      * objects for use in rendering if {@link #isShapeDirty()}.
      * <p>Allocates the ogl related data and initializes it the 1st time.<p>
      * <p>Called by {@link #draw(GL2ES2, RenderState, int, int, int)}.</p>
-     * @param curRenderModes TODO
      */
-    protected abstract void updateImpl(final GL2ES2 gl, int curRenderModes);
+    protected abstract void updateImpl(final GL2ES2 gl, final RegionRenderer renderer, final int curRenderModes);
 
-    protected abstract void drawImpl(final GL2ES2 gl, final RegionRenderer renderer, int curRenderModes, final int[/*1*/] sampleCount);
+    protected abstract void drawImpl(final GL2ES2 gl, final RegionRenderer renderer, final int curRenderModes, final int[/*1*/] sampleCount);
 }
diff --git a/src/jogl/classes/com/jogamp/graph/curve/opengl/RegionRenderer.java b/src/jogl/classes/com/jogamp/graph/curve/opengl/RegionRenderer.java
index 0927c41cb..06c10586c 100644
--- a/src/jogl/classes/com/jogamp/graph/curve/opengl/RegionRenderer.java
+++ b/src/jogl/classes/com/jogamp/graph/curve/opengl/RegionRenderer.java
@@ -48,6 +48,7 @@ import com.jogamp.common.util.IntObjectHashMap;
 import com.jogamp.graph.curve.Region;
 import com.jogamp.math.Recti;
 import com.jogamp.math.Vec4f;
+import com.jogamp.math.geom.AABBox;
 import com.jogamp.math.util.PMVMatrix4f;
 
 /**
@@ -302,6 +303,9 @@ public final class RegionRenderer {
 
     public final void setColorStatic(final float r, final float g, final float b, final float a){ rs.setColorStatic(r, g, b, a); }
 
+    public final void setClipBBox(final AABBox clipBBox) { rs.setClipBBox(clipBBox); }
+    public final AABBox getClipBBox() { return rs.getClipBBox(); }
+
     public final boolean isHintMaskSet(final int mask) { return rs.isHintMaskSet(mask); }
 
     public final void setHintMask(final int mask) { rs.setHintMask(mask); }
@@ -398,9 +402,9 @@ public final class RegionRenderer {
 
     private static final String SHADER_SRC_SUB = "";
     private static final String SHADER_BIN_SUB = "bin";
-
     private static final String GLSL_USE_COLOR_CHANNEL = "#define USE_COLOR_CHANNEL 1\n";
     private static final String GLSL_USE_COLOR_TEXTURE = "#define USE_COLOR_TEXTURE 1\n";
+    private static final String GLSL_USE_AABBOX_CLIPPING = "#define USE_AABBOX_CLIPPING 1\n";
     private static final String GLSL_DEF_SAMPLE_COUNT = "#define SAMPLE_COUNT ";
     private static final String GLSL_CONST_SAMPLE_COUNT = "const float sample_count = ";
     private static final String GLSL_MAIN_BEGIN = "void main (void)\n{\n";
@@ -530,6 +534,7 @@ public final class RegionRenderer {
                                           final boolean pass1, final int quality, final int sampleCount, final TextureSequence colorTexSeq) {
         final ShaderModeSelector1 sel1 = pass1 ? ShaderModeSelector1.selectPass1(renderModes) :
                                                  ShaderModeSelector1.selectPass2(renderModes, quality, sampleCount);
+        final boolean hasAABBoxClipping = null != getClipBBox();
         final boolean isTwoPass = Region.isTwoPass( renderModes );
         final boolean hasColorChannel = Region.hasColorChannel( renderModes );
         final boolean hasColorTexture = Region.hasColorTexture( renderModes ) && null != colorTexSeq;
@@ -543,8 +548,16 @@ public final class RegionRenderer {
             texLookupFuncName = null;
             colorTexSeqHash = 0;
         }
-        final int shaderKey = ( (colorTexSeqHash << 5) - colorTexSeqHash ) +
-                              ( sel1.ordinal() | ( HIGH_MASK & renderModes ) | ( isTwoPass ? TWO_PASS_BIT : 0 ) );
+        final int shaderKey;
+        {
+            // 31 * x == (x << 5) - x
+            int hash = 31 + colorTexSeqHash;
+            hash = ((hash << 5) - hash) + sel1.ordinal();
+            hash = ((hash << 5) - hash) + ( HIGH_MASK & renderModes );
+            hash = ((hash << 5) - hash) + ( hasAABBoxClipping ? 1 : 0 );
+            hash = ((hash << 5) - hash) + ( isTwoPass ? TWO_PASS_BIT : 0 );
+            shaderKey = hash;
+        }
 
         /**
         if(DEBUG) {
@@ -617,6 +630,11 @@ public final class RegionRenderer {
             posFp = rsFp.insertShaderSource(0, posFp, GLSL_USE_DISCARD);
         }
 
+        if( hasAABBoxClipping ) {
+            posVp = rsVp.insertShaderSource(0, posVp, GLSL_USE_AABBOX_CLIPPING);
+            posFp = rsFp.insertShaderSource(0, posFp, GLSL_USE_AABBOX_CLIPPING);
+        }
+
         if( hasColorChannel ) {
             posVp = rsVp.insertShaderSource(0, posVp, GLSL_USE_COLOR_CHANNEL);
             posFp = rsFp.insertShaderSource(0, posFp, GLSL_USE_COLOR_CHANNEL);
@@ -631,7 +649,9 @@ public final class RegionRenderer {
         }
 
         try {
-            posFp = rsFp.insertShaderSource(0, posFp, AttributeNames.class, "functions.glsl");
+            if( isPass1ColorTexSeq || hasAABBoxClipping ) {
+                posFp = rsFp.insertShaderSource(0, posFp, AttributeNames.class, "functions.glsl");
+            }
             posFp = rsFp.insertShaderSource(0, posFp, AttributeNames.class, "uniforms.glsl");
             posFp = rsFp.insertShaderSource(0, posFp, AttributeNames.class, "varyings.glsl");
         } catch (final IOException ioe) {
diff --git a/src/jogl/classes/com/jogamp/graph/curve/opengl/RenderState.java b/src/jogl/classes/com/jogamp/graph/curve/opengl/RenderState.java
index b1bbdde91..32483c1fc 100644
--- a/src/jogl/classes/com/jogamp/graph/curve/opengl/RenderState.java
+++ b/src/jogl/classes/com/jogamp/graph/curve/opengl/RenderState.java
@@ -39,6 +39,7 @@ import jogamp.graph.curve.opengl.shader.UniformNames;
 
 import com.jogamp.graph.curve.Region;
 import com.jogamp.math.Vec4f;
+import com.jogamp.math.geom.AABBox;
 import com.jogamp.math.util.PMVMatrix4f;
 import com.jogamp.opengl.util.GLArrayDataWrapper;
 import com.jogamp.opengl.util.glsl.ShaderProgram;
@@ -91,15 +92,16 @@ public class RenderState {
         return (RenderState) gl.getContext().getAttachedObject(thisKey);
     }
 
+    private final int id;
     private final PMVMatrix4f pmvMatrix;
     private final float[] weight;
     private final FloatBuffer weightBuffer;
     private final float[] colorStatic;
     private final FloatBuffer colorStaticBuffer;
-    private ShaderProgram sp;
+    private AABBox clipBBox;
     private int hintBitfield;
+    private ShaderProgram sp;
 
-    private final int id;
     private static synchronized int getNextID() {
         return nextID++;
     }
@@ -189,13 +191,14 @@ public class RenderState {
      */
     /* pp */ RenderState(final PMVMatrix4f sharedPMVMatrix) {
         this.id = getNextID();
-        this.sp = null;
         this.pmvMatrix = null != sharedPMVMatrix ? sharedPMVMatrix : new PMVMatrix4f();
         this.weight = new float[1];
         this.weightBuffer = FloatBuffer.wrap(weight);
         this.colorStatic = new float[] { 1, 1, 1, 1 };
         this.colorStaticBuffer = FloatBuffer.wrap(colorStatic);
+        this.clipBBox = null;
         this.hintBitfield = 0;
+        this.sp = null;
     }
 
     public final int id() { return id; }
@@ -259,6 +262,8 @@ public class RenderState {
         colorStatic[3] = a;
     }
 
+    public final void setClipBBox(final AABBox clipBBox) { this.clipBBox = clipBBox; }
+    public final AABBox getClipBBox() { return this.clipBBox; }
 
     /**
      *
-- 
cgit v1.2.3