Skip to content

Instantly share code, notes, and snippets.

@olamedia
Last active October 12, 2015 18:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save olamedia/4067336 to your computer and use it in GitHub Desktop.
Save olamedia/4067336 to your computer and use it in GitHub Desktop.
/**
* Copyright 2012 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.test.junit.jogl.acore;
import java.nio.FloatBuffer;
import javax.media.opengl.GL2ES1;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLContext;
import javax.media.opengl.GLDrawable;
import javax.media.opengl.GLDrawableFactory;
import javax.media.opengl.GLProfile;
import javax.media.opengl.fixedfunc.GLMatrixFunc;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import com.jogamp.common.os.Platform;
import com.jogamp.opengl.math.FloatUtil;
import com.jogamp.opengl.math.geom.Frustum;
import com.jogamp.opengl.test.junit.util.MiscUtils;
import com.jogamp.opengl.test.junit.util.UITestCase;
import com.jogamp.opengl.util.PMVMatrix;
public class TestFrustum extends UITestCase {
public float[] translate(float[] p, float d, float[] result){
for (int i = 0; i<3; i++){
result[i] += p[i]*d;
}
}
public float[] set(float[] p, float[] result){
for (int i = 0; i<3; i++){
result[i] = p[i];
}
}
@SuppressWarnings("unused")
@Test
public void test05VectorPlane() {
final float fov = 90;
final float aspect = 0.4;
final float zNear = 0.1f;
final float zFar = 100f;
final float[] eye = {7,-4, 23};
final float pitch = 30;
final float yaw = 24;
final float roll = -23;
final PMVMatrix pmvMatrix = new PMVMatrix(true);
final FloatBuffer mv = pmvMatrix.glGetMvMatrixf();
final int mvOffset = mv.position();
pmvMatrix.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
pmvMatrix.glLoadIdentity();
pmvMatrix.gluPerspective(fov, aspect, zNear, zFar);
pmvMatrix.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
pmvMatrix.glLoadIdentity();
pmvMatrix.glRotatef(-pitch, 1, 0, 0);
pmvMatrix.glRotatef(-yaw, 0, 1, 0);
pmvMatrix.glRotatef(-roll, 0, 0, 1);
pmvMatrix.glTranslatef(eye[0], eye[1], eye[2]);
pmvMatrix.update();
final float[] right = new float[3];
right[0] = mv.get(mvOffset + 0); // mv - modelview matrix
right[1] = mv.get(mvOffset + 4);
right[2] = mv.get(mvOffset + 8);
final float[] look = new float[3];
look[0] = mv.get(mvOffset + 0); // mv - modelview matrix
look[1] = mv.get(mvOffset + 4);
look[2] = mv.get(mvOffset + 8);
final float[] up = VectorUtil.cross(look, right);
final Frustum frustum = pmvMatrix.glGetFrustum();
final FloatBuffer Mvi = FloatBuffer.allocate(16);
final FloatBuffer Pi = FloatBuffer.allocate(16);
final ProjectFloat projectFloat = new ProjectFloat(true);
projectFloat.gluInvertMatrixf(pmvMatrix.glGetMvMatrixf(), Mvi);
projectFloat.gluInvertMatrixf(pmvMatrix.glGetPMatrixf(), Pi);
final float tang = (float) Math.tan((Math.PI * fov / 180) / 2.0f);
final float nh = zNear * tang;
final float nw = nh * aspect;
final float fh = zFar * tang;
final float fw = fh * aspect;
final float[] nearc = new float[3];
set(eye, nearc);
translate(look, -zNear, nearc);
final float[] farc = new float[3];
set(eye, farc);
translate(look, -zFar, farc);
final float[] flb = new float[3];
set(farc, flb);
translate(right, -fw / 2, flb);
translate(up, -fh / 2, flb);
Assert.assertEquals(frustum.getPlanes()[Frustum.FAR].distance(flb), 0);
Assert.assertEquals(frustum.getPlanes()[Frustum.BOTTOM].distance(flb), 0);
Assert.assertEquals(frustum.getPlanes()[Frustum.LEFT].distance(flb), 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment