From fbbdf8b82e159078274475c5013f2f1147b0a4d8 Mon Sep 17 00:00:00 2001
From: Sven Gothel <sgothel@jausoft.com>
Date: Wed, 22 Mar 2023 11:05:19 +0100
Subject: GraphUI Scene: Add optional frustum culling (default: disabled)

---
 .../classes/com/jogamp/graph/ui/gl/Scene.java      | 36 ++++++++++++++--------
 1 file changed, 23 insertions(+), 13 deletions(-)

(limited to 'src')

diff --git a/src/graphui/classes/com/jogamp/graph/ui/gl/Scene.java b/src/graphui/classes/com/jogamp/graph/ui/gl/Scene.java
index 39039d83d..5e7151076 100644
--- a/src/graphui/classes/com/jogamp/graph/ui/gl/Scene.java
+++ b/src/graphui/classes/com/jogamp/graph/ui/gl/Scene.java
@@ -96,6 +96,7 @@ public final class Scene implements GLEventListener {
     private static final boolean DEBUG = false;
 
     private final ArrayList<Shape> shapes = new ArrayList<Shape>();
+    private boolean doFrustumCulling = false;
 
     private float[] clearColor = null;
     private int clearMask;
@@ -164,6 +165,12 @@ public final class Scene implements GLEventListener {
     /** Returns the {@link GL#glClear(int) glClear(..)} mask, see {@link #setClearParams(float[], int)}. */
     public final int getClearMask() { return clearMask; }
 
+    /** Enable or disable {@link PMVMatrix#glGetFrustum()} culling per {@link Shape}. Default is disabled. */
+    public final void setFrustumCullingEnabled(final boolean v) { doFrustumCulling = v; }
+
+    /** Return whether {@link #setFrustumCullingEnabled(boolean) frustum culling} is enabled. */
+    public final boolean isFrustumCullingEnabled() { return doFrustumCulling; }
+
     public void attachInputListenerTo(final GLWindow window) {
         if(null == sbcMouseListener) {
             sbcMouseListener = new SBCMouseListener();
@@ -289,7 +296,7 @@ public final class Scene implements GLEventListener {
 
     private static final int[] sampleCountGLSelect = { -1 };
 
-    private void display(final GLAutoDrawable drawable, final Object[] shapesS, final boolean glSelect) {
+    private void display(final GLAutoDrawable drawable, final Object[] shapes, final boolean glSelect) {
         final GL2ES2 gl = drawable.getGL().getGL2ES2();
 
         final int[] sampleCount0;
@@ -315,22 +322,25 @@ public final class Scene implements GLEventListener {
         }
 
         //final int shapeCount = shapes.size();
-        final int shapeCount = shapesS.length;
+        final int shapeCount = shapes.length;
         for(int i=0; i<shapeCount; i++) {
             // final UIShape uiShape = shapes.get(i);
-            final Shape uiShape = (Shape)shapesS[i];
+            final Shape shape = (Shape)shapes[i];
             // System.err.println("Id "+i+": "+uiShape);
-            if( uiShape.isEnabled() ) {
+            if( shape.isEnabled() ) {
                 pmv.glPushMatrix();
-                uiShape.setTransform(pmv);
-                if( glSelect ) {
-                    final float color = ( i + 1f ) / ( shapeCount + 2f );
-                    // FIXME
-                    // System.err.printf("drawGL: color %f, index %d of [0..%d[%n", color, i, shapeCount);
-                    renderer.getRenderState().setColorStatic(color, color, color, 1f);
-                    uiShape.drawGLSelect(gl, renderer, sampleCount0);
-                } else {
-                    uiShape.draw(gl, renderer, sampleCount0);
+                shape.setTransform(pmv);
+
+                if( !doFrustumCulling || !pmv.glGetFrustum().isAABBoxOutside( shape.getBounds() ) ) {
+                    if( glSelect ) {
+                        final float color = ( i + 1f ) / ( shapeCount + 2f );
+                        // FIXME
+                        // System.err.printf("drawGL: color %f, index %d of [0..%d[%n", color, i, shapeCount);
+                        renderer.getRenderState().setColorStatic(color, color, color, 1f);
+                        shape.drawGLSelect(gl, renderer, sampleCount0);
+                    } else {
+                        shape.draw(gl, renderer, sampleCount0);
+                    }
                 }
                 pmv.glPopMatrix();
             }
-- 
cgit v1.2.3