Hi, I’m trying to write an algorithm for frustum culling by following the tutorial at http://www.lighthouse3d.com/tutorials/view-frustum-culling/ and I can’t figure out what I’m doing wrong.
I’m 99% sure the problem is somewhere in my method to build the frustum.
top,bottom,left,right,front and back are Planes, defined by 4 points and a vector for the normal.
ftl, ftl, nbl, nbr…etc are vectors for the 8 points of the frustum. f = far, t = top, n = near, b = bottom, l = left, r = right
void getFrustum(){
float angle = (float)Math.toRadians(22.5);
float tan = (float)Math.tan(angle);
float nearHeight = (float)(2 * tan * 0.00005);
float nearWidth = nearHeight * aspectRatio;
float farHeight = (float)(2 * tan * 100.0);
float farWidth = farHeight * aspectRatio;
camVector.y = (float)(Math.sin(Math.toRadians(player.cam.getXRot())));
camVector.z = (float)(Math.cos(Math.toRadians(player.cam.getYRot())));
camVector.x = (float)(Math.sin(Math.toRadians(player.cam.getYRot())));
farVector.x = player.cam.getX() + (camVector.x * 100.0f);
farVector.y = player.cam.getY() + (camVector.y * 100.0f);
farVector.z = player.cam.getZ() + (camVector.z * 100.0f);
nearVector.x = player.cam.getX() + (camVector.x * 0.00005f);
nearVector.y = player.cam.getY() + (camVector.y * 0.00005f);
nearVector.z = player.cam.getZ() + (camVector.z * 0.00005f);
float radX = (float)Math.toRadians(player.cam.getXRot());
float radY = (float)Math.toRadians(player.cam.getYRot());
float radZ = (float)Math.toRadians(player.cam.getZRot());
float a1 = (float)Math.cos(radX) * (float)Math.sin(radZ);
float a2 = (float)Math.cos(radY) * (float)Math.cos(radZ);
a2 += ((float)Math.sin(radX) * (float)Math.sin(radZ) * (float)Math.sin(radY));
float a3 = (float)-Math.sin(radX) * (float)Math.cos(radZ);
a3 += ((float)Math.cos(radX) * (float)Math.sin(radY) * (float)Math.sin(radZ));
upVector.set(a1,a2,a3);
rightVector = camVector.crossProduct(upVector);
rightVector.x = -rightVector.x;
ftl.set(farVector);
ftr.set(farVector);
fbl.set(farVector);
fbr.set(farVector);
tempVec1.set(upVector);
tempVec2.set(rightVector);
tempVec1.times(farHeight / 2.0f);
tempVec2.times(farWidth / 2.0f);
ftl.add(tempVec1);
ftl.sub(tempVec2);
ftr.add(tempVec1);
ftr.add(tempVec2);
fbl.sub(tempVec1);
fbl.sub(tempVec2);
fbr.sub(tempVec1);
fbr.add(tempVec2);
tempVec1.set(upVector);
tempVec2.set(rightVector);
tempVec1.times(nearHeight / 2.0f);
tempVec2.times(nearWidth / 2.0f);
ntl.set(nearVector);
ntr.set(nearVector);
nbl.set(nearVector);
nbr.set(nearVector);
ntl.add(tempVec1);
ntl.sub(tempVec2);
ntr.add(tempVec1);
ntr.add(tempVec2);
nbl.sub(tempVec1);
nbl.sub(tempVec2);
nbr.sub(tempVec1);
nbr.add(tempVec2);
front.set(ntl, ntr, nbl, nbr, camVector);
back.set(ftl, ftr, fbl, fbr, camVector.negate());
tempVec3.set(ftr);
tempVec3.sub(ntr);
tempVec4.set(nbr);
tempVec4.sub(ntr);
tempVec3 = tempVec3.crossProduct(tempVec4);
Vector3f norm = new Vector3f(tempVec3);
norm.normalize();
right.set(ntr, ftr, nbr, fbr, norm);
tempVec3.set(ftl);
tempVec3.sub(ntl);
tempVec4.set(nbl);
tempVec4.sub(ntl);
tempVec3 = tempVec3.crossProduct(tempVec4);
norm = new Vector3f(tempVec3);
norm.normalize();
left.set(ntl, ftl, nbl, fbl, norm.negate());
tempVec3.set(fbr);
tempVec3.sub(nbr);
tempVec4.set(nbl);
tempVec4.sub(nbr);
tempVec3 = tempVec3.crossProduct(tempVec4);
norm = new Vector3f(tempVec3);
norm.normalize();
bottom.set(nbr, fbr, nbl, fbl, norm.negate());
tempVec3.set(ftr);
tempVec3.sub(ntr);
tempVec4.set(ntl);
tempVec4.sub(ntr);
tempVec3 = tempVec3.crossProduct(tempVec4);
norm = new Vector3f(tempVec3);
norm.normalize();
top.set(ntl, ftl, ntr, ftr, norm.negate());
}
Any help would be greatly appreciated. Thanks.