PDA

View Full Version : Building any (not view-) frustum with OGL?



B_old
03-17-2003, 06:47 AM
Hello!
I read the markmorley tutorial on a viewfrustum with OGL. Now I have the desire to build any frustum with a similiar method but have failed.
EDIT:
In case you wonder, I want to use this to cull a spotlight (no, I don't want to use a cone).



//Radius is how far the frustum goes
//Angle is the FOV, kind of
void cFrustum::Update(cVector3 Pos, cVector3 Dir, float Radius, float Angle)
{
float proj[16];
float modl[16];
float clip[16];
float t;

glMatrixMode(GL_PROJECTION);
glPushMatrix();

glLoadIdentity();
gluPerspective(Angle, (float)Config.ScreenWidth / Config.ScreenHeight, 0.0f, Radius);
gluLookAt(Pos.X, Pos.Y, Pos.Z,
Dir.X, Dir.Y, Dir.Z,
0, 1, 0);

glGetFloatv(GL_PROJECTION_MATRIX, proj);
glGetFloatv(GL_MODELVIEW_MATRIX, modl);

glPopMatrix();
//and so on as markmorley describes

This gives ugly artifacts when I call it and does not really work out anyway.
Maybe should I use gluProjection instead of gluPerspective? I do not know though and am confused.
Some code snippet would be great, any help is appreciated. Thanks!



[This message has been edited by B_old (edited 03-17-2003).]

Ysaneya
03-17-2003, 07:34 AM
Using gluLookAt in the projection matrix ? You're asking for trouble :) in addition you're not setting anything to the modelview matrix before reading it back. I'd suggest that you read more documentation about using the matrix stacks in OpenGL, because your code really looks suspicious. I'm guessing it should look like:




glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluPerspective(...);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
gluLookAt(...);
glGetFloatv(GL_PROJECTION_MATRIX, proj);
glGetFloatv(GL_MODELVIEW_MATRIX, modl);
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();


Next, the near and far clipping distances in your gluPerspective aren't correct, again i'd suggest looking at the specification. The before-last parameter should be the near distance, stricly positive. The last one is the far distance, and i'm guessing it has nothing to do with the radius of your cone..

Finally, even your gluLookAt call looks suspicious. The second triplet (Dir) is actually a position in the world, *not* a direction.

Y.

B_old
03-17-2003, 08:27 AM
Thanks for the reply!
By Radius I mean how far the light can shine, this should be the farplane right?
I know that what I call Dir is in fact a point in the world. Thanks for the hints though.

Your code gives some better results than mine http://www.opengl.org/discussion_boards/ubb/smile.gif.
But I still have those big artifacts (they look really wrong, no way to ignore them)
and the frustum does not have quite the shape I expected from the arguments I passed. (Unless of course my culling code is wrong which seems unlikely because view-frustum-culling works nice).

So, thanks for your post it is a good help already! But I think I need some more.

Ysaneya
03-17-2003, 09:36 AM
Here's a small part of the code i used to do frustum-culling. It's different than yours but might be helpfull:
http://www.fl-tw.com/opengl/frustum.txt

I'm using glLoadMatrix to load the matrix (whereas you set them up respectively with gluPerspective and gluLookat), and i'm then using gluUnproject to get the frustum vertex coordinates. It's also possible to avoid the gluUnproject call and do the math yourself but it's a bit more complicated.

Y.

Ysaneya
03-17-2003, 09:38 AM
Btw, you are right about the far plane. Just don't forget that a negative or zero near plane is invalid. It's generally recommenced to push away the near plane as much as possible, and to keep the zfar/znear ratio around 1:10000 for a 24 bits Z-Buffer.

The gluLookAt call should work fine if Dir is actually a point looked at.

Y.