Jump to content
  • Advertisement
Sign in to follow this  
QuadMV

Calculating clipping plane from the projection matrix

This topic is 4860 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

I have my projection matrix setup something like this. fAspect = Width / Height; D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, fAspect, minZ, maxZ ); What would be the right formula for calculating the X and Y values to build a plane along any side of the screen so I can test if an object is in the visible viewing area before rendering it? Is there another/better way to do it other than using 4 clipping planes that represent the edges of the screen relative to the player/camera Quad

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by QuadMV
What would be the right formula for calculating the X and Y values to build a plane along any side of the screen so I can test if an object is in the visible viewing area before rendering it?

There used to be some of this code in the SDK framework, but I can't find it anymore. The following fragment is something I just dug up from my own codebase - no guarantees, but it should do what you want [grin]

//0. Variables:
D3DXMATRIXA16 mat;
D3DXVECTOR3 vecFrustum[8];

//1. multiply the matrices together
D3DXMatrixMultiply( &mat, matView, matProj );
D3DXMatrixInverse( &mat, NULL, &mat );

//2. create the 8 points of a cube in unit-space
vecFrustum[0] = D3DXVECTOR3(-1.0f, -1.0f, 0.0f); // xyz
vecFrustum[1] = D3DXVECTOR3( 1.0f, -1.0f, 0.0f); // Xyz
vecFrustum[2] = D3DXVECTOR3(-1.0f, 1.0f, 0.0f); // xYz
vecFrustum[3] = D3DXVECTOR3( 1.0f, 1.0f, 0.0f); // XYz
vecFrustum[4] = D3DXVECTOR3(-1.0f, -1.0f, 1.0f); // xyZ
vecFrustum[5] = D3DXVECTOR3( 1.0f, -1.0f, 1.0f); // XyZ
vecFrustum[6] = D3DXVECTOR3(-1.0f, 1.0f, 1.0f); // xYZ
vecFrustum[7] = D3DXVECTOR3( 1.0f, 1.0f, 1.0f); // XYZ

//3. transform all 8 points by the view/proj matrix. Doing this
// gives us that ACTUAL 8 corners of the frustum area.
for( int i = 0; i < 8; i++ )
D3DXVec3TransformCoord( &vecFrustum, &vecFrustum, &mat );

//4. generate and store the 6 planes that make up the frustum
D3DXPlaneFromPoints( &FrustumPlanes[0], &vecFrustum[0],
&vecFrustum[1], &vecFrustum[2] ); // Near

D3DXPlaneFromPoints( &FrustumPlanes[1], &vecFrustum[6],
&vecFrustum[7], &vecFrustum[5] ); // Far

D3DXPlaneFromPoints( &FrustumPlanes[2], &vecFrustum[2],
&vecFrustum[6], &vecFrustum[4] ); // Left

D3DXPlaneFromPoints( &FrustumPlanes[3], &vecFrustum[7],
&vecFrustum[3], &vecFrustum[5] ); // Right

D3DXPlaneFromPoints( &FrustumPlanes[4], &vecFrustum[2],
&vecFrustum[3], &vecFrustum[6] ); // Top

D3DXPlaneFromPoints( &FrustumPlanes[5], &vecFrustum[1],
&vecFrustum[0], &vecFrustum[4] ); // Bottom


Quote:
Original post by QuadMV
Is there another/better way to do it other than using 4 clipping planes that represent the edges of the screen relative to the player/camera

There's loads of possibilities [grin]

I forget all the names of them, but there are a few that make approximations using cylinders and cones.

One technique I was quite fond of for a while was projection testing - using various coordinate space trickery you could work out where a triangle appears on the final rendered image, so you can reject it as a screenspace operation... unfortunately the trivial implementation is quite slow - whereas the optimized one is a complete pain to read/write [oh]

hth
Jack

Share this post


Link to post
Share on other sites
Thanks to both.

JohnBolton,

How are you supposed to combine the view and projection matrix? Should the be multiplied, or is it an inverse of the multiplication as in jolly's example?

Also, assuming it does get multipled, I want to make sure of the order:

view, proj

or

proj, view

I used the sample code supplied but it's not working. I'm trying to figure out where I might have gone wrong.

Thanks

Share this post


Link to post
Share on other sites
Jack,

I tried your solution, and it seems to be working (kind of) I'm still trying to figure out if it really is working when I turn to different orientations.

One anomaly I did find is that the distance calculation seems to be off by about 1000. If I take the inverse out it seems better, but then it doesn’t seem to be working right when I turn (see note above). So I’m not sure if it’s the inverse or something else I have wrong. Any ideas would be appreciated.

Thanks


Share this post


Link to post
Share on other sites
Thanks to both. I got things working. A few of the problems I was having were both on my end.

Thanks

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!