# Calculating clipping plane from the projection matrix

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

## 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 on other sites
Quote:
 Original post by QuadMVWhat 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 togetherD3DXMatrixMultiply( &mat, matView, matProj );D3DXMatrixInverse( &mat, NULL, &mat );//2. create the 8 points of a cube in unit-spacevecFrustum[0] = D3DXVECTOR3(-1.0f, -1.0f,  0.0f); // xyzvecFrustum[1] = D3DXVECTOR3( 1.0f, -1.0f,  0.0f); // XyzvecFrustum[2] = D3DXVECTOR3(-1.0f,  1.0f,  0.0f); // xYzvecFrustum[3] = D3DXVECTOR3( 1.0f,  1.0f,  0.0f); // XYzvecFrustum[4] = D3DXVECTOR3(-1.0f, -1.0f,  1.0f); // xyZvecFrustum[5] = D3DXVECTOR3( 1.0f, -1.0f,  1.0f); // XyZvecFrustum[6] = D3DXVECTOR3(-1.0f,  1.0f,  1.0f); // xYZvecFrustum[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 frustumD3DXPlaneFromPoints( &FrustumPlanes[0], &vecFrustum[0], 			&vecFrustum[1], &vecFrustum[2] ); // NearD3DXPlaneFromPoints( &FrustumPlanes[1], &vecFrustum[6], 			&vecFrustum[7], &vecFrustum[5] ); // FarD3DXPlaneFromPoints( &FrustumPlanes[2], &vecFrustum[2], 			&vecFrustum[6], &vecFrustum[4] ); // LeftD3DXPlaneFromPoints( &FrustumPlanes[3], &vecFrustum[7], 			&vecFrustum[3], &vecFrustum[5] ); // RightD3DXPlaneFromPoints( &FrustumPlanes[4], &vecFrustum[2], 			&vecFrustum[3], &vecFrustum[6] ); // TopD3DXPlaneFromPoints( &FrustumPlanes[5], &vecFrustum[1], 			&vecFrustum[0], &vecFrustum[4] ); // Bottom

Quote:
 Original post by QuadMVIs 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

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 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 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 on other sites
Thanks to both. I got things working. A few of the problems I was having were both on my end.

Thanks

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 12
• 11
• 15
• 11
• 11
• ### Forum Statistics

• Total Topics
634149
• Total Posts
3015835
×