Archived

This topic is now archived and is closed to further replies.

Frustum Culling Problem

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

Hi to all, i''m new of this forum and i''m new in the 3D programming..I have implemented the frustum culling in OpenGL like Mark Morley done in his tutorial. All work and i don''t have problems. Now i try to port my code on Direct 3D and the function doesn''t work. I know that in D3D the matrices are handle differently from OpenGL, but i don''t know where is the problem. Here is the code that i use: D3DXMATRIX matCombined; D3DXMatrixMultiply(&matCombined,&matView,&matProj); //left plane frustum[0].normal[0]=matCombined._14+matCombined._11; frustum[0].normal[1]=matCombined._24+matCombined._21; frustum[0].normal[2]=matCombined._34+matCombined._31; frustum[0].dist=matCombined._44+matCombined._41; //right plane frustum[1].normal[0]=matCombined._14-matCombined._11; frustum[1].normal[1]=matCombined._24-matCombined._21; frustum[1].normal[2]=matCombined._34-matCombined._31; frustum[1].dist=matCombined._44-matCombined._41; //top plane frustum[2].normal[0]=matCombined._14-matCombined._12; frustum[2].normal[1]=matCombined._24-matCombined._22; frustum[2].normal[2]=matCombined._34-matCombined._32; frustum[2].dist=matCombined._44-matCombined._42; //bottom plane frustum[3].normal[0]=matCombined._14+matCombined._12; frustum[3].normal[1]=matCombined._24+matCombined._22; frustum[3].normal[2]=matCombined._34+matCombined._32; frustum[3].dist=matCombined._44+matCombined._42; //near plane frustum[4].normal[0]=matCombined._14+matCombined._13; frustum[4].normal[1]=matCombined._24+matCombined._23; frustum[4].normal[2]=matCombined._34+matCombined._33; frustum[4].dist=matCombined._44+matCombined._43; //far plane frustum[5].normal[0]=matCombined._14-matCombined._13; frustum[5].normal[1]=matCombined._24-matCombined._23; frustum[5].normal[2]=matCombined._34-matCombined._33; frustum[5].dist=matCombined._44-matCombined._43; //normalize the planes for(int i=0;i<6;i++) FastNormVect4(frustum); matView and matProj are passed to the function and before i don''t perform any trasformation. Thanks, Alex

Share this post


Link to post
Share on other sites
I don''t know much about matrices, but I''ll try and help. I''m pretty sure that OpenGL matrices are COLUMN MAJOR, whereas D3D is ROW MAJOR. You just need to convert your code to work on matrices that are row major .

- Weasalmongler

Share this post


Link to post
Share on other sites
I know... But i think that this code would work.. Not is the OpenGL version, i did some changes.

Share this post


Link to post
Share on other sites
Right handed coordinates versus Left handed coordinates.

[Check out the matrices section of the SDK docs for more details]

--
Simon O''Connor
Creative Asylum Ltd
www.creative-asylum.com

Share this post


Link to post
Share on other sites
Here''s some old code that should work:

  
void ExtractFrustum()
{
if( g_LockPVS )
return;

D3DXMATRIX clip;
float t;

D3DXMatrixMultiply( &clip, &g_ViewMatrix, &g_ProjectionMatrix );

// RIGHT PLANE

g_Frustum[0][0] = clip._14 - clip._11;
g_Frustum[0][1] = clip._24 - clip._21;
g_Frustum[0][2] = clip._34 - clip._31;
g_Frustum[0][3] = clip._44 - clip._41;
t = sqrtf( g_Frustum[0][0] * g_Frustum[0][0] + g_Frustum[0][1] * g_Frustum[0][1] + g_Frustum[0][2] * g_Frustum[0][2] );
g_Frustum[0][0] /= t;
g_Frustum[0][1] /= t;
g_Frustum[0][2] /= t;
g_Frustum[0][3] /= t;

// LEFT PLANE

g_Frustum[1][0] = clip._14 + clip._11;
g_Frustum[1][1] = clip._24 + clip._21;
g_Frustum[1][2] = clip._34 + clip._31;
g_Frustum[1][3] = clip._44 + clip._41;
t = sqrtf( g_Frustum[1][0] * g_Frustum[1][0] + g_Frustum[1][1] * g_Frustum[1][1] + g_Frustum[1][2] * g_Frustum[1][2] );
g_Frustum[1][0] /= t;
g_Frustum[1][1] /= t;
g_Frustum[1][2] /= t;
g_Frustum[1][3] /= t;

// BOTTOM PLANE

g_Frustum[2][0] = clip._14 + clip._12;
g_Frustum[2][1] = clip._24 + clip._22;
g_Frustum[2][2] = clip._34 + clip._32;
g_Frustum[2][3] = clip._44 + clip._42;
t = sqrtf( g_Frustum[2][0] * g_Frustum[2][0] + g_Frustum[2][1] * g_Frustum[2][1] + g_Frustum[2][2] * g_Frustum[2][2] );
g_Frustum[2][0] /= t;
g_Frustum[2][1] /= t;
g_Frustum[2][2] /= t;
g_Frustum[2][3] /= t;

// TOP PLANE

g_Frustum[3][0] = clip._14 - clip._12;
g_Frustum[3][1] = clip._24 - clip._22;
g_Frustum[3][2] = clip._34 - clip._32;
g_Frustum[3][3] = clip._44 - clip._42;
t = sqrtf( g_Frustum[3][0] * g_Frustum[3][0] + g_Frustum[3][1] * g_Frustum[3][1] + g_Frustum[3][2] * g_Frustum[3][2] );
g_Frustum[3][0] /= t;
g_Frustum[3][1] /= t;
g_Frustum[3][2] /= t;
g_Frustum[3][3] /= t;

// FAR PLANE

g_Frustum[4][0] = clip._14 - clip._13;
g_Frustum[4][1] = clip._24 - clip._23;
g_Frustum[4][2] = clip._34 - clip._33;
g_Frustum[4][3] = clip._44 - clip._43;
t = sqrtf( g_Frustum[4][0] * g_Frustum[4][0] + g_Frustum[4][1] * g_Frustum[4][1] + g_Frustum[4][2] * g_Frustum[4][2] );
g_Frustum[4][0] /= t;
g_Frustum[4][1] /= t;
g_Frustum[4][2] /= t;
g_Frustum[4][3] /= t;

// NEAR PLANE

g_Frustum[5][0] = clip._13;
g_Frustum[5][1] = clip._23;
g_Frustum[5][2] = clip._33;
g_Frustum[5][3] = clip._43;
t = sqrtf( g_Frustum[5][0] * g_Frustum[5][0] + g_Frustum[5][1] * g_Frustum[5][1] + g_Frustum[5][2] * g_Frustum[5][2] );
g_Frustum[5][0] /= t;
g_Frustum[5][1] /= t;
g_Frustum[5][2] /= t;
g_Frustum[5][3] /= t;

}

char AABBVisible( AABB* aabb )
{
int p, c, c2=0;

for( p=0; p<6; p++ )
{
c = 0;
if( g_Frustum[p][0] * aabb->minx + g_Frustum[p][1] * aabb->miny + g_Frustum[p][2] * aabb->minz + g_Frustum[p][3] > 0 )
c++;
if( g_Frustum[p][0] * aabb->maxx + g_Frustum[p][1] * aabb->miny + g_Frustum[p][2] * aabb->minz + g_Frustum[p][3] > 0 )
c++;
if( g_Frustum[p][0] * aabb->minx + g_Frustum[p][1] * aabb->maxy + g_Frustum[p][2] * aabb->minz + g_Frustum[p][3] > 0 )
c++;
if( g_Frustum[p][0] * aabb->maxx + g_Frustum[p][1] * aabb->maxy + g_Frustum[p][2] * aabb->minz + g_Frustum[p][3] > 0 )
c++;
if( g_Frustum[p][0] * aabb->minx + g_Frustum[p][1] * aabb->miny + g_Frustum[p][2] * aabb->maxz + g_Frustum[p][3] > 0 )
c++;
if( g_Frustum[p][0] * aabb->maxx + g_Frustum[p][1] * aabb->miny + g_Frustum[p][2] * aabb->maxz + g_Frustum[p][3] > 0 )
c++;
if( g_Frustum[p][0] * aabb->minx + g_Frustum[p][1] * aabb->maxy + g_Frustum[p][2] * aabb->maxz + g_Frustum[p][3] > 0 )
c++;
if( g_Frustum[p][0] * aabb->maxx + g_Frustum[p][1] * aabb->maxy + g_Frustum[p][2] * aabb->maxz + g_Frustum[p][3] > 0 )
c++;
if( c == 0 )
return 0;
if( c == 8 )
c2++;
}
return (c2 == 6) ? 2 : 1;
}

Share this post


Link to post
Share on other sites