diff options
author | Sven Gothel <[email protected]> | 2012-11-11 05:18:18 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2012-11-11 05:18:18 +0100 |
commit | 241f3bfdbbec655130234601652d88c30269fde4 (patch) | |
tree | e4450d085d588712bc0e6f49b4a64d956f6267d9 /src/jogl | |
parent | 5fafc1ac360333645b807dcd8dff0c0a655ea439 (diff) |
Cleanup Frustum Math Util: Independent / Compile Clean / Relocation ; PMVMatrix: Add getPreMultipliedPMV(..)
- Independent / Compile Clean
- Remove OpenMALI dependencies
- Use basic float[] type and FloatUtil
- Use AABBox - FIXME: May need BBox (no axis alignment ?!)
- Relocation
- Move to com.jogamp.opengl.math.geom (see commit 5fafc1ac360333645b807dcd8dff0c0a655ea439)
Diffstat (limited to 'src/jogl')
3 files changed, 194 insertions, 191 deletions
diff --git a/src/jogl/classes/com/jogamp/opengl/math/geom/Frustum.java b/src/jogl/classes/com/jogamp/opengl/math/geom/Frustum.java new file mode 100644 index 000000000..7981903a9 --- /dev/null +++ b/src/jogl/classes/com/jogamp/opengl/math/geom/Frustum.java @@ -0,0 +1,174 @@ +/** + * 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.opengl.math.geom; + +import com.jogamp.common.os.Platform; +import com.jogamp.opengl.math.FloatUtil; + +public class Frustum { + protected Plane[] planes = new Plane[6]; + protected float[] pmv; + + public Frustum(float[] pmv) { + this.pmv = pmv; + for (int i = 0; i < 6; ++i) { + planes[i] = new Plane(); + } + } + + public void setMatrix(float[] pmv) { + this.pmv = pmv; + } + + public final Plane[] getPlanes() { return planes; } + + public final float[] getPMV() { return pmv; } + + public class Plane { + public final float[] n = new float[3]; + public float d; + + public final float distanceTo(float x, float y, float z) { + return (n[0] * x) + (n[1] * y) + (n[2] * z) + d; + } + + @Override + public String toString() { + return "Plane[ [ " + n[0] + ", " + n[1] + ", " + n[2] + " ], " + d + "]"; + } + } + + private static final boolean quickClassify(Plane p, AABBox box) { + final float[] low = box.getLow(); + final float[] high = box.getHigh(); + + if (p.distanceTo(low[0], low[1], low[2]) > 0.0f) + return (true); + if (p.distanceTo(high[0], low[1], low[2]) > 0.0f) + return (true); + if (p.distanceTo(low[0], high[1], low[2]) > 0.0f) + return (true); + if (p.distanceTo(high[0], high[1], low[2]) > 0.0f) + return (true); + if (p.distanceTo(low[0], low[1], high[2]) > 0.0f) + return (true); + if (p.distanceTo(high[0], low[1], high[2]) > 0.0f) + return (true); + if (p.distanceTo(low[0], high[1], high[2]) > 0.0f) + return (true); + if (p.distanceTo(high[0], high[1], high[2]) > 0.0f) + return (true); + + return (false); + } + + /** + * Quick check to see if an orthogonal bounding box is inside the frustum + */ + public final boolean isInside(AABBox box) { + for (int i = 0; i < 6; ++i) { + if (!quickClassify(planes[i], box)) + return false; + } + // We make no attempt to determine whether it's fully inside or not. + return true; + } + + public void compute() { + // Left: [30+00, 31+01, 32+02, 33+03] + // comboMatrix.m[12] + comboMatrix.m[0]; + + planes[0].n[0] = pmv[12] + pmv[0]; + planes[0].n[1] = pmv[13] + pmv[1]; + planes[0].n[2] = pmv[14] + pmv[2]; + planes[0].d = pmv[15] + pmv[3]; + + // Right: [30-00, 31-01, 32-02, 33-03] + + planes[1].n[0] = pmv[12] - pmv[0]; + planes[1].n[1] = pmv[13] - pmv[1]; + planes[1].n[2] = pmv[14] - pmv[2]; + planes[1].d = pmv[15] - pmv[3]; + + // Bottom: [30+10, 31+11, 32+12, 33+13] + + planes[2].n[0] = pmv[12] + pmv[4]; + planes[2].n[1] = pmv[13] + pmv[5]; + planes[2].n[2] = pmv[14] + pmv[6]; + planes[2].d = pmv[15] + pmv[7]; + + // Top: [30-10, 31-11, 32-12, 33-13] + + planes[3].n[0] = pmv[12] - pmv[4]; + planes[3].n[1] = pmv[13] - pmv[5]; + planes[3].n[2] = pmv[14] - pmv[6]; + planes[3].d = pmv[15] - pmv[7]; + + // Far: [30-20, 31-21, 32-22, 33-23] + + planes[5].n[0] = pmv[12] - pmv[8]; + planes[5].n[1] = pmv[13] - pmv[9]; + planes[5].n[2] = pmv[14] - pmv[10]; + planes[5].d = pmv[15] - pmv[11]; + + // Near: [30+20, 31+21, 32+22, 33+23] + + planes[4].n[0] = pmv[12] + pmv[8]; + planes[4].n[1] = pmv[13] + pmv[9]; + planes[4].n[2] = pmv[14] + pmv[10]; + planes[4].d = pmv[15] + pmv[11]; + + + for (int i = 0; i < 6; ++i) { + final float[] p_n = planes[i].n; + final double invl = Math.sqrt(p_n[0] * p_n[0] + p_n[1] * p_n[1] + p_n[2] * p_n[2]); + + p_n[0] *= invl; + p_n[1] *= invl; + p_n[2] *= invl; + planes[i].d *= invl; + } + } + + public StringBuilder toString(StringBuilder sb) { + if( null == sb ) { + sb = new StringBuilder(); + } + sb.append("Frustum[ P*MV[").append(Platform.NEWLINE); + FloatUtil.matrixToString(sb, "p*mv", null, pmv, 0, 4, 4, false); + sb.append("], Planes[").append(planes[0]).append(", "); + sb.append(planes[1]).append(", ").append(planes[2]).append(", ").append(planes[3]).append(", ").append(planes[4]).append(", "); + sb.append(planes[5]).append("] ]"); + return sb; + } + + @Override + public String toString() { + return toString(null).toString(); + } +} diff --git a/src/jogl/classes/com/jogamp/opengl/util/Frustum2.java b/src/jogl/classes/com/jogamp/opengl/util/Frustum2.java deleted file mode 100644 index 0e115d1a2..000000000 --- a/src/jogl/classes/com/jogamp/opengl/util/Frustum2.java +++ /dev/null @@ -1,191 +0,0 @@ -package com.jogamp.opengl.util; - -import java.nio.FloatBuffer; - -// import org.openmali.spatial.bodies.Box; -// import org.openmali.spatial.bodies.Classifier; -// import org.openmali.vecmath2.Matrix4f; - -public class Frustum2 { - protected Plane[] planes = new Plane[6]; - protected PMVMatrix pmvMatrix; - protected FloatBuffer pmv = FloatBuffer.allocate(16); - protected int pmvOffset; - - public Frustum2(PMVMatrix matrix) { - setMatrix(matrix); - for (int i = 0; i < 6; ++i) { - planes[i] = new Plane(); - } - } - - public void setMatrix(PMVMatrix matrix) { - this.pmvMatrix = matrix; - // pmv = pmvMatrix.glGetPMvMatrixf(); - // pmvOffset = pmv.position(); - makePmvMatrix(); - } - - protected Matrix4f proj; - protected Matrix4f modl; - - private FloatBuffer b; - private int bOffset; - - private float f(int offset) { - return b.get(bOffset + offset); - } - - private Matrix4f getMatrix4f(FloatBuffer b) { - this.b = b; - bOffset = b.position(); - return new Matrix4f(f(0), f(1), f(2), f(3), f(4), f(5), f(6)/* 12 */, f(7), f(8), f(9), f(10), f(11), f(12), - f(13), f(14), f(15)); - } - - public void makePmvMatrix() { - getMatrix4f(pmvMatrix.glGetMvMatrixf()).mul(getMatrix4f(pmvMatrix.glGetPMatrixf())).writeToBuffer(pmv, true, - false); - } - - protected class Vector3f { - public float x; - public float y; - public float z; - - @Override - public String toString() { - return "{" + x + "," + y + "," + z + "}"; - } - } - - protected class Plane { - public Vector3f n = new Vector3f(); - public float d; - - public final float distanceTo(float x, float y, float z) { - return (n.x * x) + (n.y * y) + (n.z * z) + d; - } - - @Override - public String toString() { - return "Plane[" + n + ", " + d + "]"; - } - } - - protected float[] getMatrixFloat(FloatBuffer b) { - if (pmvMatrix.usesBackingArray()) { - return b.array(); - } else { - int p = b.position(); - float[] pm = new float[16]; - b.get(pm, p, 16); - b.position(p); - return pm; - } - } - - protected float m(int a) { - return pmv.get(a); - } - - private static final boolean quickClassify(Plane p, Box box) { - if (p.distanceTo(box.getLowerX(), box.getLowerY(), box.getLowerZ()) > 0.0f) - return (true); - if (p.distanceTo(box.getUpperX(), box.getLowerY(), box.getLowerZ()) > 0.0f) - return (true); - if (p.distanceTo(box.getLowerX(), box.getUpperY(), box.getLowerZ()) > 0.0f) - return (true); - if (p.distanceTo(box.getUpperX(), box.getUpperY(), box.getLowerZ()) > 0.0f) - return (true); - if (p.distanceTo(box.getLowerX(), box.getLowerY(), box.getUpperZ()) > 0.0f) - return (true); - if (p.distanceTo(box.getUpperX(), box.getLowerY(), box.getUpperZ()) > 0.0f) - return (true); - if (p.distanceTo(box.getLowerX(), box.getUpperY(), box.getUpperZ()) > 0.0f) - return (true); - if (p.distanceTo(box.getUpperX(), box.getUpperY(), box.getUpperZ()) > 0.0f) - return (true); - - return (false); - } - - /** - * Quick check to see if an orthogonal bounding box is inside the frustum - */ - public final Classifier.Classification quickClassify(Box box) { - for (int i = 0; i < 6; ++i) { - if (!quickClassify(planes[i], box)) - return (Classifier.Classification.OUTSIDE); - } - - // We make no attempt to determine whether it's fully inside or not. - return (Classifier.Classification.SPANNING); - } - - protected float[] mat = new float[16]; - - public void compute() { - // Left: [30+00, 31+01, 32+02, 33+03] - // comboMatrix.m[12] + comboMatrix.m[0]; - - planes[0].n.x = m(12) + m(0); - planes[0].n.y = m(13) + m(1); - planes[0].n.z = m(14) + m(2); - planes[0].d = m(15) + m(3); - - // Right: [30-00, 31-01, 32-02, 33-03] - - planes[1].n.x = m(12) - m(0); - planes[1].n.y = m(13) - m(1); - planes[1].n.z = m(14) - m(2); - planes[1].d = m(15) - m(3); - - // Bottom: [30+10, 31+11, 32+12, 33+13] - - planes[2].n.x = m(12) + m(4); - planes[2].n.y = m(13) + m(5); - planes[2].n.z = m(14) + m(6); - planes[2].d = m(15) + m(7); - - // Top: [30-10, 31-11, 32-12, 33-13] - - planes[3].n.x = m(12) - m(4); - planes[3].n.y = m(13) - m(5); - planes[3].n.z = m(14) - m(6); - planes[3].d = m(15) - m(7); - - // Far: [30-20, 31-21, 32-22, 33-23] - - planes[5].n.x = m(12) - m(8); - planes[5].n.y = m(13) - m(9); - planes[5].n.z = m(14) - m(10); - planes[5].d = m(15) - m(11); - - // Near: [30+20, 31+21, 32+22, 33+23] - - planes[4].n.x = m(12) + m(8); - planes[4].n.y = m(13) + m(9); - planes[4].n.z = m(14) + m(10); - planes[4].d = m(15) + m(11); - - - for (int i = 0; i < 6; ++i) { - double invl = Math.sqrt(planes[i].n.x * planes[i].n.x + planes[i].n.y * planes[i].n.y + planes[i].n.z - * planes[i].n.z); - - planes[i].n.x *= invl; - planes[i].n.y *= invl; - planes[i].n.z *= invl; - planes[i].d *= invl; - } - } - - @Override - public String toString() { - return "m[" + m(0) + "|" + m(1) + "|" + m(2) + "|" + m(3) + "|" + m(4) + "|" + m(5) + "|" + m(6) + "|" + m(7) - + "|" + m(8) + "|" + m(9) + "|" + m(10) + "|" + m(11) + "|" + m(12) + "|" + m(13) + "|" + m(14) + "|" - + m(15) + "]" + "Frustum2[" + planes[0] + ", " + planes[1] + ", " + planes[2] + ", " + planes[3] + ", " - + planes[4] + ", " + planes[5] + "]"; - } -} diff --git a/src/jogl/classes/com/jogamp/opengl/util/PMVMatrix.java b/src/jogl/classes/com/jogamp/opengl/util/PMVMatrix.java index 34971a9af..1628e0a8e 100644 --- a/src/jogl/classes/com/jogamp/opengl/util/PMVMatrix.java +++ b/src/jogl/classes/com/jogamp/opengl/util/PMVMatrix.java @@ -385,6 +385,26 @@ public class PMVMatrix implements GLMatrixFunc { public final FloatBuffer glGetPMvMatrixf() { return matrixPMv; } + + /** + * @param d a 4*4 float array to hold the pre-multiplied P*MV + * @param d_off offset of d + * @return the pre-multiplied P*MV, param d + */ + public float[] getPreMultipliedPMV(float[] d, int d_off) { + FloatUtil.multMatrixf(matrixP, matrixMv, d, d_off); + return d; + } + + /** + * @param d a 4*4 FloatBuffer to hold the premultiplied P*MV + * @param d_off offset of d + * @return the pre-multiplied P*MV, param d + */ + public FloatBuffer getPreMultipliedPMV(FloatBuffer d) { + FloatUtil.multMatrixf(matrixP, matrixMv, d); + return d; + } /** * Returns 3 matrices within one FloatBuffer: {@link #glGetPMatrixf() P}, {@link #glGetMvMatrixf() Mv} and {@link #glGetMviMatrixf() Mvi}. |