Sphere in Frustum

Started by
12 comments, last by steg 22 years, 2 months ago
Interesting! I hve been thinking of converting that same tutorial into DirectX myself.

Moe''s Site
Advertisement
Hi,
Objects are at various world positions (they are static).

The code I use to get the planes is converted to dx as shown below :

D3DXMATRIX CD3DManager::GetViewMatrix()
{
D3DXMATRIX view;
m_pDevice->GetTransform(D3DTS_VIEW, &view);
return view;
}

D3DXMATRIX CD3DManager::GetProjectionMatrix()
{
D3DXMATRIX projection;
m_pDevice->GetTransform(D3DTS_PROJECTION, &projection);
return projection;
}

D3DXMATRIX CD3DManager::GetViewFrustum()
{
D3DXMATRIX ViewFrustum;
D3DXMatrixMultiply( &ViewFrustum, &GetViewMatrix(), &GetProjectionMatrix() );
return ViewFrustum;
}

void CD3DManager::ExtractFrustum()
{
double t;
D3DXMATRIX clip;
clip = GetViewFrustum();

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

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

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

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

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

// NEAR PLANE
frustum[5][0] = clip._14 + clip._13;
frustum[5][1] = clip._24 + clip._23;
frustum[5][2] = clip._34 + clip._33;
frustum[5][3] = clip._44 + clip._43;
t = sqrt( frustum[5][0] * frustum[5][0] + frustum[5][1] * frustum[5][1] + frustum[5][2] * frustum[5][2] );
frustum[5][0] /= t;
frustum[5][1] /= t;
frustum[5][2] /= t;
frustum[5][3] /= t;
}

Does this look correct, it all works, but again, I still don''t need the sphere''s centre coordinates in the following method : ?!

BOOL CD3DManager::SphereInFrustum( float x, float y, float z, float radius, float rx, float ry, float rz )
{
int p;

for( p = 0; p < 6; p++ )
{
if( frustum[p][0] * (x + rx) + frustum[p][1]
* (y + ry) + frustum[p][2]
* (z + rz) + frustum[p][3] <= -radius )
return false;
}
return true;
}

This is really weird ?!?!?

Reg''ds,
sTeVe

If it isn't working, take a bath, have a think and try again...

Thats some pretty interesting stuff. I would like to use boxes so I can calculate my visibility in my world on the fly.

Moe''s Site
Hi Moe,

If u''d like the code I could send it to you ?

Reg''ds,
sTeVe

If it isn't working, take a bath, have a think and try again...

This topic is closed to new replies.

Advertisement