From 48201a6ea6471eb5951edb735b36156ab3410a15 Mon Sep 17 00:00:00 2001
From: Sven Gothel <sgothel@jausoft.com>
Date: Sat, 23 Apr 2011 06:12:10 +0200
Subject: Refactored graph: Reduce/remove data copy/recreation; Shader cleanup

- Pass the current GL context object where it's required

- Introduce RenderState (which has ShaderState) to acquire/change shader related data (Region)

- Shader Cleanup: User import for common stuff; use req. version

- Reduce/remove data copy/recreation in *Region implementation

- UI/RIButton: Use defaults I like :)
---
 .../com/jogamp/graph/curve/opengl/Renderer.java    | 251 +++++++++++++++------
 1 file changed, 185 insertions(+), 66 deletions(-)

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

diff --git a/src/jogl/classes/com/jogamp/graph/curve/opengl/Renderer.java b/src/jogl/classes/com/jogamp/graph/curve/opengl/Renderer.java
index ce3e83692..4db917fdd 100644
--- a/src/jogl/classes/com/jogamp/graph/curve/opengl/Renderer.java
+++ b/src/jogl/classes/com/jogamp/graph/curve/opengl/Renderer.java
@@ -1,40 +1,75 @@
+/**
+ * Copyright 2010 JogAmp Community. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without modification, are
+ * permitted provided that the following conditions are met:
+ *
+ *    1. Redistributions of source code must retain the above copyright notice, this list of
+ *       conditions and the following disclaimer.
+ *
+ *    2. Redistributions in binary form must reproduce the above copyright notice, this list
+ *       of conditions and the following disclaimer in the documentation and/or other materials
+ *       provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * The views and conclusions contained in the software and documentation are those of the
+ * authors and should not be interpreted as representing official policies, either expressed
+ * or implied, of JogAmp Community.
+ */
 package com.jogamp.graph.curve.opengl;
 
+import java.nio.FloatBuffer;
+
 import javax.media.opengl.GL2ES2;
 import javax.media.opengl.GLUniformData;
 import javax.media.opengl.fixedfunc.GLMatrixFunc;
 
-import jogamp.opengl.Debug;
+import jogamp.graph.curve.opengl.RenderStateImpl;
 
 import com.jogamp.graph.curve.Region;
 import com.jogamp.graph.geom.Vertex;
-import com.jogamp.graph.geom.opengl.SVertex;
 import com.jogamp.opengl.util.PMVMatrix;
 import com.jogamp.opengl.util.glsl.ShaderState;
 
 public abstract class Renderer {
     protected static final boolean DEBUG = Region.DEBUG;
+    protected static final boolean DEBUG_INSTANCE = Region.DEBUG_INSTANCE;    
+
+    public static RenderState createRenderState(ShaderState st, Vertex.Factory<? extends Vertex> pointFactory, PMVMatrix pmvMatrix) {
+        return new RenderStateImpl(st, pointFactory, pmvMatrix);
+    }
 
-    protected abstract boolean initImpl(GL2ES2 gl);
+    public static RenderState createRenderState(ShaderState st, Vertex.Factory<? extends Vertex> pointFactory) {
+        return new RenderStateImpl(st, pointFactory);
+    }
+    
+    /**
+     * Implementation shall load, compile and link the shader program and leave it active.
+     * @param gl
+     * @return
+     */
+    protected abstract boolean initShaderProgram(GL2ES2 gl);
     
     protected abstract void disposeImpl(GL2ES2 gl);
    
     /** 
      * Flushes all cached data
+     * @see #dispose(GL2ES2)
      */
-    public abstract void flushCache();
+    public abstract void flushCache(GL2ES2 gl);
+    
+    protected final RenderState rs;
+    public final int renderType;    
     
-    public abstract float getAlpha();
-
-    public abstract void setAlpha(GL2ES2 gl, float alpha_t);
-
-    public abstract void setColor(GL2ES2 gl, float r, float g, float b);
-
-    protected final Vertex.Factory<? extends Vertex> pointFactory;    
-    protected ShaderState st = new ShaderState();
-    protected PMVMatrix pmvMatrix = new PMVMatrix();
-    protected GLUniformData mgl_PMVMatrix;
-    protected int renderType;
     protected int vp_width = 0;
     protected int vp_height = 0;
     
@@ -42,16 +77,15 @@ public abstract class Renderer {
     private boolean initialized = false;
     
     /**
-     * 
-     * @param factory
+     * @param rs the used {@link RenderState} 
      * @param renderType either {@link com.jogamp.graph.curve.Region#SINGLE_PASS} or {@link com.jogamp.graph.curve.Region#TWO_PASS}
      */
-    protected Renderer(Vertex.Factory<? extends Vertex> factory, int renderType) {
+    protected Renderer(RenderState rs, int renderType) {
+        this.rs = rs;
         this.renderType = renderType;
-        this.pointFactory = (null != factory) ? factory : SVertex.factory();        
     }
     
-    public Vertex.Factory<? extends Vertex> getFactory() { return pointFactory; }
+    public Vertex.Factory<? extends Vertex> getFactory() { return rs.getPointFactory(); }
     
     public final boolean isInitialized() { return initialized; }
     
@@ -63,10 +97,12 @@ public abstract class Renderer {
     public final int getHeight() { return vp_height; }
     
     /** 
-     * Initialize shaders and bindings for GPU based rendering. 
-     * Leaves the renderer enabled, ie ShaderState on.
+     * Initialize shader and bindings for GPU based rendering bound to the given GL object's GLContext.
      *  
-     * @param gl the current GL state
+     * Leaves the renderer enabled, ie ShaderState.
+     *  
+     * @param gl referencing the current GLContext to which the ShaderState is bound to
+     * 
      * @return true if succeeded, false otherwise
      */
     public boolean init(GL2ES2 gl) {
@@ -87,92 +123,175 @@ public abstract class Renderer {
             System.err.println("TextRendererImpl01: VBO Supported = " + isVBOSupported());
         }
         
-        initialized = initImpl(gl);
+        if(!vboSupported){
+            return false;
+        }
+        
+        rs.attachTo(gl);
+        
+        gl.glEnable(GL2ES2.GL_BLEND);
+        gl.glBlendFunc(GL2ES2.GL_SRC_ALPHA, GL2ES2.GL_ONE_MINUS_SRC_ALPHA);
+        
+        initialized = initShaderProgram(gl);
+        if(!initialized) {
+            return false;
+        }
+        
+        if(!rs.getShaderState().glUniform(gl, rs.getPMVMatrixUniform())) {
+            if(DEBUG){
+                System.err.println("Error setting PMVMatrix in shader: "+rs.getShaderState());
+            }
+            return false;
+        }
+        
+        if(!rs.getShaderState().glUniform(gl, rs.getSharpness())) {
+            if(DEBUG){
+                System.err.println("Error setting sharpness in shader: "+rs.getShaderState());
+            }
+            return false;
+        }
+                
+        if(!rs.getShaderState().glUniform(gl, rs.getAlpha())) {
+            if(DEBUG){
+                System.err.println("Error setting global alpha in shader: "+rs.getShaderState());
+            }
+            return false;
+        }        
+        
+        if(!rs.getShaderState().glUniform(gl, rs.getColorStatic())) {
+            if(DEBUG){
+                System.err.println("Error setting global color in shader: "+rs.getShaderState());
+            }
+            return false;
+        }        
+        
+        if(!rs.getShaderState().glUniform(gl, rs.getStrength())) {
+            System.err.println("Error setting antialias strength in shader: "+rs.getShaderState());
+        }
+                
         return initialized;
     }
 
     public void dispose(GL2ES2 gl) {
         if(!initialized){
-            if(DEBUG) {
+            if(DEBUG_INSTANCE) {
                 System.err.println("TextRenderer: Not initialized!");
             }
             return;
         }
+        flushCache(gl);
         disposeImpl(gl);
-        st.destroy(gl);
-        flushCache();        
+        rs.getShaderState().destroy(gl);
         initialized = false;        
     }
     
-    public final ShaderState getShaderState() { return st; }
+    public final RenderState getRenderState() { return rs; }
+    public final ShaderState getShaderState() { return rs.getShaderState(); }
     
     public final void enable(GL2ES2 gl, boolean enable) { 
-        st.glUseProgram(gl, enable);
+        rs.getShaderState().glUseProgram(gl, enable);
     }
 
-    public final PMVMatrix getMatrix() { return pmvMatrix; }
+    public float getSharpness() {
+        return rs.getSharpness().floatValue();
+    }
+    
+    public void setSharpness(GL2ES2 gl, float v) {
+        rs.getSharpness().setData(v);
+        if(null != gl && rs.getShaderState().inUse()) {
+            rs.getShaderState().glUniform(gl, rs.getSharpness());
+        }
+    }
 
-    public void rotate(GL2ES2 gl, float angle, float x, float y, float z) {
-        pmvMatrix.glRotatef(angle, x, y, z);
-        if(initialized && null != gl && st.inUse()) {
-            st.glUniform(gl, mgl_PMVMatrix);
+    public float getStrength() {
+        return rs.getStrength().floatValue();
+    }
+    
+    public void setStrength(GL2ES2 gl, float v) {
+        rs.getStrength().setData(v);
+        if(null != gl && rs.getShaderState().inUse()) {
+            rs.getShaderState().glUniform(gl, rs.getStrength());
         }
     }
+    
+    public float getAlpha() {
+        return rs.getAlpha().floatValue();
+    }
 
-    public void translate(GL2ES2 gl, float x, float y, float z) {
-        pmvMatrix.glTranslatef(x, y, z);
-        if(initialized && null != gl && st.inUse()) {
-            st.glUniform(gl, mgl_PMVMatrix);
+    public void setAlpha(GL2ES2 gl, float alpha_t) {
+        rs.getAlpha().setData(alpha_t);
+        if(null != gl && rs.getShaderState().inUse()) {
+            rs.getShaderState().glUniform(gl, rs.getAlpha());
         }
+
+    }
+
+    public void getColorStatic(GL2ES2 gl, float[] rgb) {
+        FloatBuffer fb = (FloatBuffer) rs.getColorStatic().getBuffer();
+        rgb[0] = fb.get(0); 
+        rgb[1] = fb.get(1); 
+        rgb[2] = fb.get(2); 
     }
     
-    public void scale(GL2ES2 gl, float x, float y, float z) {
-        pmvMatrix.glScalef(x, y, z);
-        if(initialized && null != gl && st.inUse()) {
-            st.glUniform(gl, mgl_PMVMatrix);
+    public void setColorStatic(GL2ES2 gl, float r, float g, float b){
+        FloatBuffer fb = (FloatBuffer) rs.getColorStatic().getBuffer();
+        fb.put(0, r);
+        fb.put(1, g);
+        fb.put(2, b);
+        if(null != gl && rs.getShaderState().inUse()) {
+            rs.getShaderState().glUniform(gl, rs.getColorStatic());
         }
     }
+    
+    public final PMVMatrix getMatrix() { return rs.getPMVMatrix(); }
+
+    public void rotate(GL2ES2 gl, float angle, float x, float y, float z) {
+        rs.getPMVMatrix().glRotatef(angle, x, y, z);
+        updateMatrix(gl);
+    }
+
+    public void translate(GL2ES2 gl, float x, float y, float z) {
+        rs.getPMVMatrix().glTranslatef(x, y, z);
+        updateMatrix(gl);
+    }
+    
+    public void scale(GL2ES2 gl, float x, float y, float z) {
+        rs.getPMVMatrix().glScalef(x, y, z);
+        updateMatrix(gl);
+    }
 
     public void resetModelview(GL2ES2 gl) {
-        pmvMatrix.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
-        pmvMatrix.glLoadIdentity();
-        if(initialized && null != gl && st.inUse()) {
-            st.glUniform(gl, mgl_PMVMatrix);
-        }
+        rs.getPMVMatrix().glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
+        rs.getPMVMatrix().glLoadIdentity();
+        updateMatrix(gl);
     }
 
     public void updateMatrix(GL2ES2 gl) {
-        if(initialized && null != gl && st.inUse()) {
-            st.glUniform(gl, mgl_PMVMatrix);
+        if(initialized && null != gl && rs.getShaderState().inUse()) {
+            rs.getShaderState().glUniform(gl, rs.getPMVMatrixUniform());
         }
     }
 
     public boolean reshapePerspective(GL2ES2 gl, float angle, int width, int height, float near, float far) {
         this.vp_width = width;
         this.vp_height = height;
-        float ratio = (float)width/(float)height;
-        pmvMatrix.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
-        pmvMatrix.glLoadIdentity();
-        pmvMatrix.gluPerspective(angle, ratio, near, far);
-        
-        if(initialized && null != gl) {
-            st.glUniform(gl, mgl_PMVMatrix);
-        }
-        
+        final float ratio = (float)width/(float)height;
+        final PMVMatrix p = rs.getPMVMatrix();
+        p.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
+        p.glLoadIdentity();
+        p.gluPerspective(angle, ratio, near, far);
+        updateMatrix(gl);
         return true;
     }
 
     public boolean reshapeOrtho(GL2ES2 gl, int width, int height, float near, float far) {
         this.vp_width = width;
         this.vp_height = height;
-        pmvMatrix.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
-        pmvMatrix.glLoadIdentity();
-        pmvMatrix.glOrthof(0, width, 0, height, near, far);
-        
-        if(initialized && null != gl) {
-            st.glUniform(gl, mgl_PMVMatrix);
-        }
-        
+        final PMVMatrix p = rs.getPMVMatrix();
+        p.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
+        p.glLoadIdentity();
+        p.glOrthof(0, width, 0, height, near, far);
+        updateMatrix(gl);
         return true;        
     }
 
-- 
cgit v1.2.3