Advertisement
Guest User

Frustum

a guest
Nov 12th, 2013
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.46 KB | None | 0 0
  1. package frustum;
  2.  
  3. import org.lwjgl.util.vector.Matrix4f;
  4. import org.lwjgl.util.vector.Vector3f;
  5. import org.lwjgl.util.vector.Vector4f;
  6.  
  7. public class Frustum {
  8.  
  9.     private final Matrix4f projection;
  10.     private final Vector4f[] coefficients = new Vector4f[6];
  11.  
  12.     public Frustum(final Matrix4f projection) {
  13.         this.projection = projection;
  14.     }
  15.  
  16.     public void update(final Matrix4f view) {
  17.         Matrix4f a = Matrix4f.mul(projection, view, null);
  18.  
  19.         // left
  20.         coefficients[0] = new Vector4f(a.m00 + a.m03, a.m10 + a.m13, a.m20 + a.m23, a.m30 + a.m33);
  21.         // right
  22.         coefficients[1] = new Vector4f(-a.m00 + a.m03, -a.m10 + a.m13, -a.m20 + a.m23, -a.m30 + a.m33);
  23.  
  24.         // bottom
  25.         coefficients[2] = new Vector4f(a.m01 + a.m03, a.m11 + a.m13, a.m21 + a.m23, a.m31 + a.m33);
  26.         // top
  27.         coefficients[3] = new Vector4f(-a.m01 + a.m03, -a.m11 + a.m13, -a.m21 + a.m23, -a.m31 + a.m33);
  28.  
  29.         // near
  30.         coefficients[4] = new Vector4f(a.m02 + a.m03, a.m12 + a.m13, a.m22 + a.m23, a.m32 + a.m33);
  31.         // far
  32.         coefficients[5] = new Vector4f(-a.m02 + a.m03, -a.m12 + a.m13, -a.m22 + a.m23, -a.m32 + a.m33);
  33.  
  34.         for (int i = 0; i < 6; i++) {
  35.             coefficients[i].normalise();
  36.         }
  37.     }
  38.  
  39.     public boolean contains(final Vector3f v) {
  40.         return contains(new Vector4f(v.x, v.y, v.z, 1));
  41.     }
  42.  
  43.     public boolean contains(final Vector4f v) {
  44.         for (int i = 0; i < 6; i++) {
  45.             float value = Vector4f.dot(coefficients[i], v);
  46.             if (value < 0.0f) {
  47.                 return false;
  48.             }
  49.         }
  50.  
  51.         return true;
  52.     }
  53.  
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement