diff options
author | Sven Gothel <[email protected]> | 2014-04-02 04:41:48 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2014-04-02 04:41:48 +0200 |
commit | e8a5a1cbb988670ca206ab1ac633e19a91bfa478 (patch) | |
tree | 5b9f17a06ad350150375a2a0e38daa3d6dd11251 /src/jogl/classes/com/jogamp/opengl | |
parent | 6f5686696b1e9085a759774056c7be9887a9e34f (diff) |
Bug 801: WIP 2/2 - Add color attribute; Switch Shader instead of branching in shader; Update attributes and uniforms manually, drop ShaderState;
- Due to shader-switching,
'renderModes' are now local to Region, e.g. UIShape etc
- Remove RegionRenderer.renderModes
- VBORegion2P*:
- Use simple 2x float matrix for orthogonal P+Mv
- Cleanup shader
Diffstat (limited to 'src/jogl/classes/com/jogamp/opengl')
4 files changed, 181 insertions, 82 deletions
diff --git a/src/jogl/classes/com/jogamp/opengl/math/FloatUtil.java b/src/jogl/classes/com/jogamp/opengl/math/FloatUtil.java index 9d3ee412b..cf56ff0a4 100644 --- a/src/jogl/classes/com/jogamp/opengl/math/FloatUtil.java +++ b/src/jogl/classes/com/jogamp/opengl/math/FloatUtil.java @@ -29,6 +29,8 @@ package com.jogamp.opengl.math; import java.nio.FloatBuffer; +import javax.media.opengl.GLException; + import jogamp.opengl.Debug; import com.jogamp.common.os.Platform; @@ -199,6 +201,123 @@ public class FloatUtil { } /** + * Make given matrix the orthogonal matrix based on given parameters. + * + * @param a 4x4 matrix in column-major order (also result) + * @param a_off + * @param initA if true, given matrix will be initialized w/ identity matrix. + * @param left + * @param right + * @param bottom + * @param top + * @param zNear + * @param zFar + * @return given matrix for chaining + */ + public static final float[] makeOrthof(final float[] a, final int a_off, final boolean initA, + final float left, final float right, + final float bottom, final float top, + final float zNear, final float zFar) { + if( initA ) { + FloatUtil.makeIdentityf(a, a_off); + } + // Ortho matrix (Column Order): + // 2/dx 0 0 0 + // 0 2/dy 0 0 + // 0 0 2/dz 0 + // tx ty tz 1 + final float dx=right-left; + final float dy=top-bottom; + final float dz=zFar-zNear; + final float tx=-1.0f*(right+left)/dx; + final float ty=-1.0f*(top+bottom)/dy; + final float tz=-1.0f*(zFar+zNear)/dz; + + a[a_off+0+4*0] = 2.0f/dx; + a[a_off+1+4*1] = 2.0f/dy; + a[a_off+2+4*2] = -2.0f/dz; + a[a_off+0+4*3] = tx; + a[a_off+1+4*3] = ty; + a[a_off+2+4*3] = tz; + + return a; + } + + /** + * Make given matrix the frustum matrix based on given parameters. + * + * @param a 4x4 matrix in column-major order (also result) + * @param a_off + * @param initA if true, given matrix will be initialized w/ identity matrix. + * @param left + * @param right + * @param bottom + * @param top + * @param zNear + * @param zFar + * @return given matrix for chaining + */ + public static final float[] makeFrustumf(final float[] a, final int a_off, final boolean initA, + final float left, final float right, + final float bottom, final float top, + final float zNear, final float zFar) { + if(zNear<=0.0f||zFar<0.0f) { + throw new GLException("GL_INVALID_VALUE: zNear and zFar must be positive, and zNear>0"); + } + if(left==right || top==bottom) { + throw new GLException("GL_INVALID_VALUE: top,bottom and left,right must not be equal"); + } + if( initA ) { + FloatUtil.makeIdentityf(a, a_off); + } + // Frustum matrix (Column Order): + // 2*zNear/dx 0 0 0 + // 0 2*zNear/dy 0 0 + // A B C -1 + // 0 0 D 0 + final float zNear2 = 2.0f*zNear; + final float dx=right-left; + final float dy=top-bottom; + final float dz=zFar-zNear; + final float A=(right+left)/dx; + final float B=(top+bottom)/dy; + final float C=-1.0f*(zFar+zNear)/dz; + final float D=-2.0f*(zFar*zNear)/dz; + + a[a_off+0+4*0] = zNear2/dx; + a[a_off+1+4*1] = zNear2/dy; + a[a_off+2+4*2] = C; + + a[a_off+0+4*2] = A; + a[a_off+1+4*2] = B; + + a[a_off+2+4*3] = D; + a[a_off+3+4*2] = -1.0f; + return a; + } + + /** + * Make given matrix the perspective matrix based on given parameters. + * + * @param a 4x4 matrix in column-major order (also result) + * @param a_off + * @param initA if true, given matrix will be initialized w/ identity matrix. + * @param fovy angle in radians + * @param aspect + * @param zNear + * @param zFar + * @return given matrix for chaining + */ + public static final float[] makePerspective(final float[] a, final int a_off, final boolean initA, + final float fovy, final float aspect, final float zNear, final float zFar) { + float top=(float)Math.tan(fovy)*zNear; + float bottom=-1.0f*top; + float left=aspect*bottom; + float right=aspect*top; + return makeFrustumf(a, a_off, initA, left, right, bottom, top, zNear, zFar); + } + + /** * Multiply matrix: [d] = [a] x [b] * @param a 4x4 matrix in column-major order * @param b 4x4 matrix in column-major order @@ -840,4 +959,47 @@ public class FloatUtil { public static float sqrt(final float a) { return (float) java.lang.Math.sqrt(a); } + /** + * Returns resolution of Z buffer of given parameter, + * see <a href="http://www.sjbaker.org/steve/omniv/love_your_z_buffer.html">Love Your Z-Buffer</a>. + * <pre> + * return z * z / ( zNear * (1<<zBits) - z ) + * </pre> + * @param zBits number of bits of Z precision, i.e. z-buffer depth + * @param z distance from the eye to the object + * @param zNear distance from eye to near clip plane + * @return smallest resolvable Z separation at this range. + */ + public static float getZBufferEpsilon(final int zBits, final float z, final float zNear) { + return z * z / ( zNear * ( 1 << zBits ) - z ); + } + + /** + * Returns Z buffer value of given parameter, + * see <a href="http://www.sjbaker.org/steve/omniv/love_your_z_buffer.html">Love Your Z-Buffer</a>. + * <pre> + * float a = zFar / ( zFar - zNear ) + * float b = zFar * zNear / ( zNear - zFar ) + * return (int) ( (1<<zBits) * ( a + b / z ) ) + * </pre> + * @param zBits number of bits of Z precision, i.e. z-buffer depth + * @param z distance from the eye to the object + * @param zNear distance from eye to near clip plane + * @param zFar distance from eye to far clip plane + * @return z buffer value + */ + public static int getZBufferValue(final int zBits, final float z, final float zNear, final float zFar) { + final float a = zFar / ( zFar - zNear ); + final float b = zFar * zNear / ( zNear - zFar ); + return (int) ( (1<<zBits) * ( a + b / z ) ); + } + + /** + * Returns orthogonal distance + * (1f/zNear-1f/orthoDist)/(1f/zNear-1f/zFar); + */ + public static float getOrthoWinZ(final float orthoZ, final float zNear, final float zFar) { + return (1f/zNear-1f/orthoZ)/(1f/zNear-1f/zFar); + } + }
\ No newline at end of file diff --git a/src/jogl/classes/com/jogamp/opengl/util/PMVMatrix.java b/src/jogl/classes/com/jogamp/opengl/util/PMVMatrix.java index 44288256a..8f5beeebe 100644 --- a/src/jogl/classes/com/jogamp/opengl/util/PMVMatrix.java +++ b/src/jogl/classes/com/jogamp/opengl/util/PMVMatrix.java @@ -686,61 +686,12 @@ public class PMVMatrix implements GLMatrixFunc { @Override public final void glOrthof(final float left, final float right, final float bottom, final float top, final float zNear, final float zFar) { - // Ortho matrix (Column Order): - // 2/dx 0 0 0 - // 0 2/dy 0 0 - // 0 0 2/dz 0 - // tx ty tz 1 - final float dx=right-left; - final float dy=top-bottom; - final float dz=zFar-zNear; - final float tx=-1.0f*(right+left)/dx; - final float ty=-1.0f*(top+bottom)/dy; - final float tz=-1.0f*(zFar+zNear)/dz; - - matrixOrtho[0+4*0] = 2.0f/dx; - matrixOrtho[1+4*1] = 2.0f/dy; - matrixOrtho[2+4*2] = -2.0f/dz; - matrixOrtho[0+4*3] = tx; - matrixOrtho[1+4*3] = ty; - matrixOrtho[2+4*3] = tz; - - glMultMatrixf(matrixOrtho, 0); + glMultMatrixf( FloatUtil.makeOrthof(matrixOrtho, 0, false, left, right, bottom, top, zNear, zFar), 0 ); } @Override public final void glFrustumf(final float left, final float right, final float bottom, final float top, final float zNear, final float zFar) { - if(zNear<=0.0f||zFar<0.0f) { - throw new GLException("GL_INVALID_VALUE: zNear and zFar must be positive, and zNear>0"); - } - if(left==right || top==bottom) { - throw new GLException("GL_INVALID_VALUE: top,bottom and left,right must not be equal"); - } - // Frustum matrix (Column Order): - // 2*zNear/dx 0 0 0 - // 0 2*zNear/dy 0 0 - // A B C -1 - // 0 0 D 0 - final float zNear2 = 2.0f*zNear; - final float dx=right-left; - final float dy=top-bottom; - final float dz=zFar-zNear; - final float A=(right+left)/dx; - final float B=(top+bottom)/dy; - final float C=-1.0f*(zFar+zNear)/dz; - final float D=-2.0f*(zFar*zNear)/dz; - - matrixFrustum[0+4*0] = zNear2/dx; - matrixFrustum[1+4*1] = zNear2/dy; - matrixFrustum[2+4*2] = C; - - matrixFrustum[0+4*2] = A; - matrixFrustum[1+4*2] = B; - - matrixFrustum[2+4*3] = D; - matrixFrustum[3+4*2] = -1.0f; - - glMultMatrixf(matrixFrustum, 0); + glMultMatrixf( FloatUtil.makeFrustumf(matrixFrustum, 0, false, left, right, bottom, top, zNear, zFar), 0 ); } // @@ -751,11 +702,7 @@ public class PMVMatrix implements GLMatrixFunc { * {@link #glMultMatrixf(FloatBuffer) Multiply} the {@link #glGetMatrixMode() current matrix} with the perspective/frustum matrix. */ public final void gluPerspective(final float fovy, final float aspect, final float zNear, final float zFar) { - float top=(float)Math.tan(fovy*((float)Math.PI)/360.0f)*zNear; - float bottom=-1.0f*top; - float left=aspect*bottom; - float right=aspect*top; - glFrustumf(left, right, bottom, top, zNear, zFar); + glMultMatrixf( FloatUtil.makePerspective(matrixFrustum, 0, false, fovy*((float)Math.PI)/360.0f, aspect, zNear, zFar), 0 ); } /** @@ -840,24 +787,12 @@ public class PMVMatrix implements GLMatrixFunc { * using a {@link AABBox#intersectsRay(Ray, float[]) bounding box}. * <p> * Notes for picking <i>winz0</i> and <i>winz1</i>: + * <ul> + * <li>see {@link FloatUtil#getZBufferEpsilon(int, float, float)}</li> + * <li>see {@link FloatUtil#getZBufferValue(int, float, float, float)}</li> + * <li>see {@link FloatUtil#getOrthoWinZ(float, float, float)}</li> + * </ul> * </p> - * <p> - * <a href="http://www.sjbaker.org/steve/omniv/love_your_z_buffer.html">Love Your Z-Buffer</a> - * <pre> - * delta = z * z / ( zNear * (1<<N) - z ) - * - * Where: - * N = number of bits of Z precision - * zNear = distance from eye to near clip plane - * z = distance from the eye to the object - * delta = the smallest resolvable Z separation at this range. - * </pre> - * Another equation to determine winZ for 'orthoDist' > 0 - * <pre> - * winZ = (1f/zNear-1f/orthoDist)/(1f/zNear-1f/zFar); - * </pre> - * </p> - * * @param winx * @param winy * @param winz0 diff --git a/src/jogl/classes/com/jogamp/opengl/util/glsl/ShaderProgram.java b/src/jogl/classes/com/jogamp/opengl/util/glsl/ShaderProgram.java index b289b41e2..6a4b13dfb 100644 --- a/src/jogl/classes/com/jogamp/opengl/util/glsl/ShaderProgram.java +++ b/src/jogl/classes/com/jogamp/opengl/util/glsl/ShaderProgram.java @@ -312,17 +312,20 @@ public class ShaderProgram { gl.glUseProgram( on ? shaderProgram : 0 ); programInUse = on; } + public synchronized void notifyNotInUse() { + programInUse = false; + } - protected boolean programLinked = false; - protected boolean programInUse = false; - protected int shaderProgram = 0; // non zero is valid! - protected HashSet<ShaderCode> allShaderCode = new HashSet<ShaderCode>(); - protected HashSet<ShaderCode> attachedShaderCode = new HashSet<ShaderCode>(); - protected int id = -1; + private boolean programLinked = false; + private boolean programInUse = false; + private int shaderProgram = 0; // non zero is valid! + private final HashSet<ShaderCode> allShaderCode = new HashSet<ShaderCode>(); + private final HashSet<ShaderCode> attachedShaderCode = new HashSet<ShaderCode>(); + private int id = -1; private static synchronized int getNextID() { return nextID++; } - protected static int nextID = 1; + private static int nextID = 1; } diff --git a/src/jogl/classes/com/jogamp/opengl/util/glsl/ShaderState.java b/src/jogl/classes/com/jogamp/opengl/util/glsl/ShaderState.java index ce4c2615d..64dd589b8 100644 --- a/src/jogl/classes/com/jogamp/opengl/util/glsl/ShaderState.java +++ b/src/jogl/classes/com/jogamp/opengl/util/glsl/ShaderState.java @@ -176,8 +176,7 @@ public class ShaderState { } if(shaderProgram.inUse()) { if(null != prog && enable) { - // new program will issue glUseProgram(..) - shaderProgram.programInUse = false; + shaderProgram.notifyNotInUse(); } else { // no new 'enabled' program - disable useProgram(gl, false); |