Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package frustum;
- import org.lwjgl.util.vector.Matrix4f;
- import org.lwjgl.util.vector.Vector3f;
- import org.lwjgl.util.vector.Vector4f;
- public class Frustum {
- private final Matrix4f projection;
- private final Vector4f[] coefficients = new Vector4f[6];
- public Frustum(final Matrix4f projection) {
- this.projection = projection;
- }
- public void update(final Matrix4f view) {
- Matrix4f a = Matrix4f.mul(projection, view, null);
- // left
- coefficients[0] = new Vector4f(a.m00 + a.m03, a.m10 + a.m13, a.m20 + a.m23, a.m30 + a.m33);
- // right
- coefficients[1] = new Vector4f(-a.m00 + a.m03, -a.m10 + a.m13, -a.m20 + a.m23, -a.m30 + a.m33);
- // bottom
- coefficients[2] = new Vector4f(a.m01 + a.m03, a.m11 + a.m13, a.m21 + a.m23, a.m31 + a.m33);
- // top
- coefficients[3] = new Vector4f(-a.m01 + a.m03, -a.m11 + a.m13, -a.m21 + a.m23, -a.m31 + a.m33);
- // near
- coefficients[4] = new Vector4f(a.m02 + a.m03, a.m12 + a.m13, a.m22 + a.m23, a.m32 + a.m33);
- // far
- coefficients[5] = new Vector4f(-a.m02 + a.m03, -a.m12 + a.m13, -a.m22 + a.m23, -a.m32 + a.m33);
- for (int i = 0; i < 6; i++) {
- coefficients[i].normalise();
- }
- }
- public boolean contains(final Vector3f v) {
- return contains(new Vector4f(v.x, v.y, v.z, 1));
- }
- public boolean contains(final Vector4f v) {
- for (int i = 0; i < 6; i++) {
- float value = Vector4f.dot(coefficients[i], v);
- if (value < 0.0f) {
- return false;
- }
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement