Sign in to follow this  
Nairb

Frustum corner point calculation

Recommended Posts

I know there has to be a relatively easy way to do this, but my maths are escaping me presently and Google is surprisingly sparse: I want to render an outline of a camera's frustum. I have camera position, orientation, FOV, near plane, and far plane. How do I find the eight points specifying the frustum? Cheers, --Brian

Share this post


Link to post
Share on other sites
Quote:
Original post by Nairb
I know there has to be a relatively easy way to do this, but my maths are escaping me presently and Google is surprisingly sparse:

I want to render an outline of a camera's frustum. I have camera position, orientation, FOV, near plane, and far plane. How do I find the eight points specifying the frustum?

Cheers,
--Brian
There are a couple of ways to do this:

1. If you have the frustum planes available (which you probably do if you're doing frustum culling), simply intersect the frustum planes in sets of three to yield the frustum corners.

2. Although I haven't tried this method, I think you can also apply an inverse transform to the corners of the frustum as expressed in NDC space to yield the corners in world space.

3. Another option is to just compute the corners directly using some trig and vector algebra.

I generally use method 1.

Share this post


Link to post
Share on other sites
This tutorial will show you how to calculate the frustum corners in view-space using the parameters you've specified. Once you've done that, you just need to transform all 8 corners by the world matrix of the camera (inverse of the view matrix) and you'll get the world-space coordinates.

Share this post


Link to post
Share on other sites
If the matrix is invertable (non-orthographic), you can transform the 8 vertices making up the unit cube by this matrix to get their positions. The 'extract the planes' method is faster and more accurate, but more work to implement.

Share this post


Link to post
Share on other sites
It's probably the result of how my camera class is working (storing it's transformation matrix just like any other game object and knowing it's width and height at z=1 to do frustum culling). In other words: no planes required. In more other words: it only works for a symmetrical view frustum. Some bits and pieces...

[code]
//Called "Fac" because it's the factor to multiply with a given z to get the frustum width and height for that distance
yFac = tanf(FoV * PI/360.0f);
xFac = yFac*aspectRatio;

Vector3 Forward, Right, Up; //Simply the three columns from your transformation matrix (or the inverse of your view matrix)

Vector3 farLeftTop = Position + Forward*far - far*Right*xFac*far + Up*yFac*far;
Vector3 farRightTop = Position + Forward*far + far*Right*xFac*far + Up*yFac*far;
Vector3 farLeftBottom = Position + Forward*far - far*Right*xFac*far - Up*yFac*far;
Vector3 farLeftBottom = Position + Forward*far + far*Right*xFac*far - Up*yFac*far;
// and so on for the near plane
}
[/code]

I uploaded the entire thing for more context: [url="http://festini.device-zero.de/camera.zip"]http://festini.devic...o.de/camera.zip[/url]

But: it's OpenGL (same principles apply for D3D), it's old and messy and I don't know how much functionality I broke when changing it back to a right handed coordinate system (I generally prefer left handed with positive z being into the screen... a positive number pointing "backwards" just feels wrong).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this