I give up! Hello everyone!
I am struggling to get the frustum culling working for the last day, but i can’t find what’s wrong with what i do.
Here is my code for updating the frustum (called eveytime camera is moved) and checking if a point (a box) is in frustum.
public void update() {
FloatBuffer modelMatrixBuffer = BufferUtils.createFloatBuffer(16);
glGetFloat(GL_MODELVIEW_MATRIX, modelMatrixBuffer);
modelMatrixBuffer.rewind();
Matrix4f modelMatrix = new Matrix4f();
modelMatrix.load(modelMatrixBuffer);
planes[0] = new Plane(modelMatrix.m30 + modelMatrix.m00, modelMatrix.m31 + modelMatrix.m01, modelMatrix.m32 + modelMatrix.m02, modelMatrix.m33 + modelMatrix.m03);
planes[1] = new Plane(modelMatrix.m30 - modelMatrix.m00, modelMatrix.m31 - modelMatrix.m01, modelMatrix.m32 - modelMatrix.m02, modelMatrix.m33 - modelMatrix.m03);
planes[2] = new Plane(modelMatrix.m30 + modelMatrix.m10, modelMatrix.m31 + modelMatrix.m11, modelMatrix.m32 + modelMatrix.m12, modelMatrix.m33 + modelMatrix.m13);
planes[3] = new Plane(modelMatrix.m30 - modelMatrix.m10, modelMatrix.m31 - modelMatrix.m11, modelMatrix.m32 - modelMatrix.m12, modelMatrix.m33 - modelMatrix.m13);
planes[4] = new Plane(modelMatrix.m30 + modelMatrix.m20, modelMatrix.m31 + modelMatrix.m21, modelMatrix.m32 + modelMatrix.m22, modelMatrix.m33 + modelMatrix.m23);
planes[5] = new Plane(modelMatrix.m30 - modelMatrix.m20, modelMatrix.m31 - modelMatrix.m21, modelMatrix.m32 - modelMatrix.m22, modelMatrix.m33 - modelMatrix.m23);
}
public static boolean inFrustum(int x, int y, int z) {
Frustum camFrustum = Camera.getInstance().fow;
Vector4f vCube[] = new Vector4f[] {
new Vector4f(x, y, z, 1f),
new Vector4f(x + 1, y + 1, z + 1, 1f)
};
for(int v = 0; v < 2; v++) {
for(int i = 0; i < 6; i++)
if(distanceToPlane(vCube[v], camFrustum.planes[i]) < 0)
return false;
}
return true;
}
public static float distanceToPlane(Vector4f vertex, Plane plane) {
return plane.a * vertex.x + plane.b * vertex.y + plane.c * vertex.z + plane.d;
}
The Plane class is containing only the a, b, c and d from the plane’s equation.
For the moment i just want to see it working and then i will try to optimize it.
Can anyone please help me?