Jump to content
  • Advertisement

Archived

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

SimDemon

OpenGL Converting OpenGL Frustum Culling Tutorial to Direct3D? Help, please ...

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

Hey there. Thanks for reading my post, and I hope you can offer me some help. I am trying to learn, and get the hang of, frustum culling. I currently have the DirectX 8 SDK. I haven''t taken the time to get the DX 9 SDK . Anyhow, I wanted to try something new, so I went looking for frustum culling tutorials. I didn''t really find anything DirectX (D3D) specific, so I decided I would convert an OpenGL frustum culling tutorial. Well, here''s the problem. I don''t think I am calculating the frustum''s planes correctly. Here is the OpenGL code:
void CFrustum::CalculateFrustum(float *md, float *proj)
{
   // Error checking.

   if(md == 0 || proj == 0)
      return;

   // Create the clip.

   float clip[16] = {0};

   clip[0] = md[0] * proj[0] + md[1] * proj[4] + md[2] * proj[8]  + md[3] * proj[12];
	clip[1] = md[0] * proj[1] + md[1] * proj[5] + md[2] * proj[9]  + md[3] * proj[13];
	clip[2] = md[0] * proj[2] + md[1] * proj[6] + md[2] * proj[10] + md[3] * proj[14];
	clip[3] = md[0] * proj[3] + md[1] * proj[7] + md[2] * proj[11] + md[3] * proj[15];

	clip[4] = md[4] * proj[0] + md[5] * proj[4] + md[6] * proj[8]  + md[7] * proj[12];
	clip[5] = md[4] * proj[1] + md[5] * proj[5] + md[6] * proj[9]  + md[7] * proj[13];
	clip[6] = md[4] * proj[2] + md[5] * proj[6] + md[6] * proj[10] + md[7] * proj[14];
	clip[7] = md[4] * proj[3] + md[5] * proj[7] + md[6] * proj[11] + md[7] * proj[15];

	clip[8]  = md[8] * proj[0] + md[9] * proj[4] + md[10] * proj[8]  + md[11] * proj[12];
	clip[9]  = md[8] * proj[1] + md[9] * proj[5] + md[10] * proj[9]  + md[11] * proj[13];
	clip[10] = md[8] * proj[2] + md[9] * proj[6] + md[10] * proj[10] + md[11] * proj[14];
	clip[11] = md[8] * proj[3] + md[9] * proj[7] + md[10] * proj[11] + md[11] * proj[15];

	clip[12] = md[12] * proj[0] + md[13] * proj[4] + md[14] * proj[8]  + md[15] * proj[12];
	clip[13] = md[12] * proj[1] + md[13] * proj[5] + md[14] * proj[9]  + md[15] * proj[13];
	clip[14] = md[12] * proj[2] + md[13] * proj[6] + md[14] * proj[10] + md[15] * proj[14];
	clip[15] = md[12] * proj[3] + md[13] * proj[7] + md[14] * proj[11] + md[15] * proj[15];
	

	// Calculate the right side of the frustum.

   Frustum[0].a = clip[3]  - clip[0];
	Frustum[0].b = clip[7]  - clip[4];
	Frustum[0].c = clip[11] - clip[8];
	Frustum[0].d = clip[15] - clip[12];

	// Calculate the left side of the frustum.

	Frustum[1].a = clip[3]  + clip[0];
	Frustum[1].b = clip[7]  + clip[4];
	Frustum[1].c = clip[11] + clip[8];
	Frustum[1].d = clip[15] + clip[12];

	// Calculate the bottom side of the frustum.

	Frustum[2].a = clip[3]  + clip[1];
	Frustum[2].b = clip[7]  + clip[5];
	Frustum[2].c = clip[11] + clip[9];
	Frustum[2].d = clip[15] + clip[13];

	// Calculate the top side of the frustum.

	Frustum[3].a = clip[3]  - clip[1];
	Frustum[3].b = clip[7]  - clip[5];
	Frustum[3].c = clip[11] - clip[9];
	Frustum[3].d = clip[15] - clip[13];

	// Calculate the far side of the frustum.

	Frustum[4].a = clip[3]  - clip[2];
	Frustum[4].b = clip[7]  - clip[6];
	Frustum[4].c = clip[11] - clip[10];
	Frustum[4].d = clip[15] - clip[14];

	// Calculate the near side of the frustum.

	Frustum[5].a = clip[3]  + clip[2];
	Frustum[5].b = clip[7]  + clip[6];
	Frustum[5].c = clip[11] + clip[10];
	Frustum[5].d = clip[15] + clip[14];

	// Normalize the sides of the frustum.

   NormalizeFrustum();
}
''md'' is retrieved with ''glGetFloatv(GL_MODELVIEW_MATRIX, model);'', while ''proj'' is retrieved with ''glGetFloatv(GL_PROJECTION_MATRIX, proj);''. ''CFrustum::CalculateFrustum()'' is then called, passing those two variables as the parameters. And if I am right, I believe ''GL_MODELVIEW_MATRIX'' is just the view matrix (D3D: ''D3DTS_VIEW''). It''s using the view (I think that is what ''md'' is) and the projection matrices to calculate the planes of the frustum. Now, here''s my code:
void CFrustum::CalculateFrustum(D3DXMATRIX *matView, D3DXMATRIX *matProj) 
{
	// TODO: Calculate the view frustum from the matrices specified

	assert(matView != NULL);      // Check the view matrix

	assert(matProj != NULL);      // Check the projection matrix


	// Now we create the clip

	float fClip[16] = { 0 };      // Array of the clip data


	// Initialize the clip data

	fClip[0]  = matView->_11 * matProj->_11 + matView->_12 * matProj->_21 + matView->_13 * matProj->_31 + matView->_14 * matProj->_41;
	fClip[1]  = matView->_11 * matProj->_12 + matView->_12 * matProj->_22 + matView->_13 * matProj->_32 + matView->_14 * matProj->_42;
	fClip[2]  = matView->_11 * matProj->_13 + matView->_12 * matProj->_23 + matView->_13 * matProj->_33 + matView->_14 * matProj->_43;
	fClip[3]  = matView->_11 * matProj->_14 + matView->_12 * matProj->_24 + matView->_13 * matProj->_34 + matView->_14 * matProj->_44;

	fClip[4]  = matView->_21 * matProj->_11 + matView->_22 * matProj->_21 + matView->_23 * matProj->_31 + matView->_24 * matProj->_41;
	fClip[5]  = matView->_21 * matProj->_12 + matView->_22 * matProj->_22 + matView->_23 * matProj->_32 + matView->_24 * matProj->_42;
	fClip[6]  = matView->_21 * matProj->_13 + matView->_22 * matProj->_23 + matView->_23 * matProj->_33 + matView->_24 * matProj->_43;
	fClip[7]  = matView->_21 * matProj->_14 + matView->_22 * matProj->_24 + matView->_23 * matProj->_34 + matView->_24 * matProj->_44;

	fClip[8]  = matView->_31 * matProj->_11 + matView->_32 * matProj->_21 + matView->_33 * matProj->_31 + matView->_34 * matProj->_41;
	fClip[9]  = matView->_31 * matProj->_12 + matView->_32 * matProj->_22 + matView->_33 * matProj->_32 + matView->_34 * matProj->_42;
	fClip[10] = matView->_31 * matProj->_13 + matView->_32 * matProj->_23 + matView->_33 * matProj->_33 + matView->_34 * matProj->_43;
	fClip[11] = matView->_31 * matProj->_14 + matView->_32 * matProj->_24 + matView->_33 * matProj->_34 + matView->_34 * matProj->_44;

	fClip[12] = matView->_41 * matProj->_11 + matView->_42 * matProj->_21 + matView->_43 * matProj->_31 + matView->_44 * matProj->_41;
	fClip[13] = matView->_41 * matProj->_12 + matView->_42 * matProj->_22 + matView->_43 * matProj->_32 + matView->_44 * matProj->_42;
	fClip[14] = matView->_41 * matProj->_13 + matView->_42 * matProj->_23 + matView->_43 * matProj->_33 + matView->_44 * matProj->_43;
	fClip[15] = matView->_41 * matProj->_14 + matView->_42 * matProj->_24 + matView->_43 * matProj->_34 + matView->_44 * matProj->_44;

	// Calculate the right side of the frustum

	m_Frustum[0].a = fClip[3]  - fClip[0];
	m_Frustum[0].b = fClip[7]  - fClip[4];
	m_Frustum[0].c = fClip[11] - fClip[8];
	m_Frustum[0].d = fClip[15] - fClip[12];

	// Calculate the left side of the frustum

	m_Frustum[1].a = fClip[3]  + fClip[0];
	m_Frustum[1].b = fClip[7]  + fClip[4];
	m_Frustum[1].c = fClip[11] + fClip[8];
	m_Frustum[1].d = fClip[15] + fClip[12];

	// Calculate the bottom side of the frustum

	m_Frustum[2].a = fClip[3]  + fClip[1];
	m_Frustum[2].b = fClip[7]  + fClip[5];
	m_Frustum[2].c = fClip[11] + fClip[9];
	m_Frustum[2].d = fClip[15] + fClip[13];

	// Calculate the left side of the frustum

	m_Frustum[3].a = fClip[3]  - fClip[1];
	m_Frustum[3].b = fClip[7]  - fClip[5];
	m_Frustum[3].c = fClip[11] - fClip[9];
	m_Frustum[3].d = fClip[15] - fClip[13];

	// Calculate the far side of the frustum

	m_Frustum[4].a = fClip[3]  - fClip[2];
	m_Frustum[4].b = fClip[7]  - fClip[6];
	m_Frustum[4].c = fClip[11] - fClip[10];
	m_Frustum[4].d = fClip[15] - fClip[14];

	// Calculate the near side of the frustum

	m_Frustum[5].a = fClip[3]  + fClip[2];
	m_Frustum[5].b = fClip[7]  + fClip[6];
	m_Frustum[5].c = fClip[11] + fClip[10];
	m_Frustum[5].d = fClip[15] + fClip[14];

	NormalizeFrustum();     // Normalize the sides of the frustum

}
I''m sorry if you need more information (other functions in the ''CFrustum'' class, etc.) but I think that''s all you need to know. If anything else is needed, let me know =). Anyhow, I don''t think I am calculating the frustum correctly (filling the ''clip[16]'' array) with the correct information. I just thought that ''md[0]'' would equal ''matView->_11''. If you can help me solve this problem, I would really appreciate it. =) Thanks very much, Matt U.

Share this post


Link to post
Share on other sites
Advertisement
right heres what the matrixes are in D3D:

Projection Matrix is the screen space matrix, it is declared during setup usually, i think that will be usefull to you..

View Matrix is the camera view so to speak, it is what your looking at so to speak

World Matrix is basically the modelview matrix, each 3d object such as a mesh or shape should have its own world matrix...


So if you would be using the ModelView Matrix in OpenGL i think in D3D you would use D3DTS_WORLD matrix for that...

im not too sure about it though, i aint done frustrum culling and i was asking about matrixes on here the other day... so best listen to someone else...LOL but theres my input..haha

Share this post


Link to post
Share on other sites
Hey, thanks for the reply, and the attempt to help me, but that didn''t work. If you or anyone can offer me some help, please do.

Thanks,
Matt U.

Share this post


Link to post
Share on other sites

  • 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!