Jump to content
  • Advertisement
Sign in to follow this  
Xasz

Calculating viewing "cube"

This topic is 3829 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Using gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);, I need to find the coordinates of all 8 points of the viewing "cube" (Yes, I know it's not a cube, but a distorted prism) but I can't find any resources to help me do this using the width and height like I'm using. Yes, I've tried gluUnproject, and it just doesn't work. I don't need it to be dynamic based on a click, I just need to be able to calculate it when I move the window (I want to render a UI directly in front of the screen and a background directly behind it)

Share this post


Link to post
Share on other sites
Advertisement
Your "viewing cube" is called the (view/camera) frustum. Searching on goggle for frustum should give you lots of hits for constructing it.

Share this post


Link to post
Share on other sites
BTW, there is no necessity to render the UI with the same set-up as the scene. Assuming you speak of a usual 2D UI layer on top of the screen, you can simply switch to glOrtho after rendering the scene and then render the UI.

To answer your question: WIth the vertical field-of-view (fovy) angle (your 45°), the aspect ratio a := width/height, near clipping plane distance n (your 0.1) and far clipping plane distance f (your 100.0), the projection matrix made by gluPerspective will be

[ v/a 0 0 0 ]
[ 0 v 0 0 ]
[ 0 0 (n+f)/(n-f) 2nf/(n-f) ]
[ 0 0 -1 0 ]
where v := cotangent( fovy/2 )

Using the abbreviations
hn := n * tan( fovy/2 )
hf := f * tan( fovy/2 )
wn := a * hn
wf := a * hf
I think that the points are
[ wn hn n ]T
[ -wn hn n ]T
[ -wn -hn n ]T
[ wn -hn n ]T
for the near plane, and
[ wf hf f ]T
[ -wf hf f ]T
[ -wf -hf f ]T
[ wf -hf f ]T
for the far plane. These, of course are given in the camera co-ordinate frame. To yield in global co-ordinates (if you want that) you'll need to apply the inverse VIEW transformation.

Share this post


Link to post
Share on other sites
This may help you understand haegarr's post:

void
gluPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar)
{
GLdouble xmin, xmax, ymin, ymax;

ymax = zNear * tan(fovy * M_PI / 360.0);
ymin = -ymax;
xmin = ymin * aspect;
xmax = ymax * aspect;


glFrustum(xmin, xmax, ymin, ymax, zNear, zFar);
}



Not my orginal code, I think it is from Mesa 3D originally.

Share this post


Link to post
Share on other sites
Ahh, thanks, but I'm using selection with my UI and it refuses to play nicely with ortho. Nothing seems to work, and this seems like an easier solution to the problem. Though all the matrices are confusing me pretty badly

Quote:
Your "viewing cube" is called the (view/camera) frustum. Searching on goggle for frustum should give you lots of hits for constructing it.

Thanks, I'll give that a shot

Share this post


Link to post
Share on other sites
Yep, that implementation (shown in lexs post above) is reasonable and matches the posted matrix when the parameters are inserted in glFrustum's matrix.

However, just for furthur clarification :) The matrix isn't really needed for calculating the points but at most for determining the meaning of the parameters. The points can be derived from the ratio formula of tangent function:
tan( angle ) == (length of opposite) / ( length of adjacent)
where the adjacent is given as the near and far clipping plane distances, and the opposites are the half heights of the particular planes since gluPerspective uses the vertical field-of-view angle. The widhtes are then computed by the rule-of-three using the aspect ratio. The points are then given by proper combinations of those values. (See my previous post below "Using the abbreviations" and simply ignore the matrix.)

Share this post


Link to post
Share on other sites
Your calculations are perfect. Thanks a ton. This is exactly what I was after

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!