Something I'd like to share with all of you: simple frustum culling
I've been working on some simple frustum culling for a little while and now it finally works. I'm going to describe the algorithm and then a code snippet. What it 'actually' does right now is test whether or not an object's position is inside or outside a cone with the specified number of degres as its radius. It's good for amateur projects because its still useful, but extremely easy to understand.
I would just like to specify a difference between frustum culling and frustum clipping: I asked the people at gamedev and they said that OpenGL automatically does frustum clipping for you after calling glFrustum. However it still takes time for your hardware to determine if the objects passed to it are inside or outside the view frustum. That is why it is important to do culling, which means you determine if an object is visible in software and don't even pass it to GL. With that said and done, here is my simple method (and it works!)
Using the equation (Mr. Wizard told me this equation)
costheta = DotProduct(A, B) / ||A|| * ||B||
A and B are vectors, ||A|| denotes the magnitude of , the DotProduct is the sum of the products of the components of the vectors
1) Get a vector from the object's position to the camera's position
2) Normalize it
3) Get just the view component of your View vector (View - Position)
4) Because your View and Normalized (View - Player.CamPos) vectors already have a length of 1, you can boil down the equation to just:
costheta = DotProduct(A, B)
5) Use arcsine function to get the number of radians between the two vectors
6) Optional step: convert to degrees for readability (which is what I did)
7) If the degrees between these two vectors is greater than the radius of your cone, then it isn't visible.
Here is my IsVisible() function, if it returns false, the object doesn't get drawn
Code:
bool GameEntity::IsVisible() {
Vector ObjectToCamera;
ObjectToCamera = Position - Player1->CamPos;
ObjectToCamera.Normalize();
float CosTheta = DotProduct(ObjectToCamera, (Player1->View - Player1->Position));// / (ObjectToCamera.GetLength() * (Player1->View - Player1->Position).GetLength());
float Degrees = acosf(CosTheta);
Degrees = RadiansToDegrees(Degrees);
if(Degrees > 50) {
return false;
}
It's definitely not perfect, but it works for me for right now. I wanted to share that with other beginners so they could use it or something like it in their projects (in the time being until doing real frustum culling).