Advertisement
Guest User

FrustumTest

a guest
Nov 12th, 2013
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.72 KB | None | 0 0
  1. package frustum;
  2.  
  3. import org.lwjgl.LWJGLException;
  4. import org.lwjgl.input.Keyboard;
  5. import org.lwjgl.opengl.Display;
  6. import org.lwjgl.opengl.DisplayMode;
  7. import org.lwjgl.opengl.GL11;
  8. import org.lwjgl.util.vector.Matrix4f;
  9. import org.lwjgl.util.vector.Vector3f;
  10. import org.lwjgl.util.vector.Vector4f;
  11.  
  12. public class FrustumTest {
  13.  
  14.     private final int WIDTH = 800;
  15.     private final int HEIGHT = 600;
  16.     private final float SPEED = 2.0f;
  17.  
  18.     private Matrix4f projection;
  19.     private Matrix4f camera;
  20.     private Matrix4f viewProjection;
  21.     private Frustum frustum;
  22.  
  23.     private Vector3f[] quad;
  24.     private Vector3f position = new Vector3f();
  25.  
  26.     public void create() {
  27.         projection = createPerspectiveProjection(45.0f, 1.0f, 0.1f, 100.0f);
  28.         camera = new Matrix4f();
  29.         frustum = new Frustum(projection);
  30.  
  31.         quad = new Vector3f[4];
  32.         quad[0] = new Vector3f(-0.5f, 0.5f, -2.0f);
  33.         quad[1] = new Vector3f(-0.5f, -0.5f, -2.0f);
  34.         quad[2] = new Vector3f(0.5f, -0.5f, -2.0f);
  35.         quad[3] = new Vector3f(0.5f, 0.5f, -2.0f);
  36.     }
  37.  
  38.     public void update(final float deltaTime) {
  39.         // Position
  40.         Vector3f deltaPosition = new Vector3f();
  41.         boolean changed = false;
  42.         if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
  43.             deltaPosition.translate(0, 0, -SPEED * deltaTime);
  44.             changed = true;
  45.         }
  46.         if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
  47.             deltaPosition.translate(0, 0, SPEED * deltaTime);
  48.             changed = true;
  49.         }
  50.         if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
  51.             deltaPosition.translate(-SPEED * deltaTime, 0, 0);
  52.             changed = true;
  53.         }
  54.         if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
  55.             deltaPosition.translate(SPEED * deltaTime, 0, 0);
  56.             changed = true;
  57.         }
  58.  
  59.         if (changed || viewProjection == null) {
  60.             position = Vector3f.add(position, deltaPosition, null);
  61.  
  62.             camera.setIdentity();
  63.             camera.translate(new Vector3f(position));
  64.  
  65.             Matrix4f view = Matrix4f.invert(camera, null);
  66.             viewProjection = Matrix4f.mul(projection, view, null);
  67.  
  68.             frustum.update(view);
  69.             System.out.println(position);
  70.         }
  71.     }
  72.  
  73.     public void render() {
  74.         GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
  75.         GL11.glBegin(GL11.GL_QUADS);
  76.         for (int i = 0; i < 4; i++) {
  77.             Vector4f v = Matrix4f.transform(viewProjection, new Vector4f(quad[i].x, quad[i].y, quad[i].z, 1.0f), null);
  78.  
  79.             if (frustum.contains(v)) {
  80.                 GL11.glColor3f(0, 1.0f, 0);
  81.             } else {
  82.                 GL11.glColor3f(1.0f, 0, 0);
  83.             }
  84.             GL11.glVertex3f(v.x / v.w, v.y / v.w, v.z / v.w);
  85.         }
  86.         GL11.glEnd();
  87.  
  88.     }
  89.  
  90.     private Matrix4f createPerspectiveProjection(final float fov, final float aspect, final float zNear, final float zFar) {
  91.         Matrix4f mat = new Matrix4f();
  92.  
  93.         float yScale = 1f / (float) Math.tan(Math.toRadians(fov / 2f));
  94.         float xScale = yScale / aspect;
  95.         float frustumLength = zFar - zNear;
  96.  
  97.         mat.m00 = xScale;
  98.         mat.m11 = yScale;
  99.         mat.m22 = -((zFar + zNear) / frustumLength);
  100.         mat.m23 = -1;
  101.         mat.m32 = -((2 * zFar * zNear) / frustumLength);
  102.         mat.m33 = 0;
  103.  
  104.         return mat;
  105.     }
  106.  
  107.     private void run() throws LWJGLException {
  108.         Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
  109.         Display.create();
  110.  
  111.         GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  112.         GL11.glViewport(0, 0, WIDTH, HEIGHT);
  113.  
  114.         create();
  115.  
  116.         long lastTime = System.currentTimeMillis();
  117.         while (!Display.isCloseRequested()) {
  118.             long currentTime = System.currentTimeMillis();
  119.             float deltaTime = (currentTime - lastTime) / 1000.0f;
  120.             lastTime = currentTime;
  121.             update(deltaTime);
  122.             render();
  123.  
  124.             if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
  125.                 System.exit(0);
  126.             }
  127.  
  128.             Display.update();
  129.             Display.sync(60);
  130.         }
  131.         Display.destroy();
  132.     }
  133.  
  134.     public static void main(final String[] args) {
  135.         try {
  136.             new FrustumTest().run();
  137.         } catch (Exception e) {
  138.             e.printStackTrace();
  139.         }
  140.     }
  141.  
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement