Jump to content
  • Advertisement
Sign in to follow this  
MARS_999

OpenGL Frutsum Culling

This topic is 4389 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 am trying to get my frutsum culling working and think its close but the terrain jumps in and out of view when I move the camera around. I think it has to do with my min,max values or I have the math wrong for the frutsum?
bool Camera::pointInFrustum(float x, float y, float z)
{
	for(unsigned long x = 0; x < 6; ++x)
	{
        float dist = frustumPlanes[x].a * x +
					 frustumPlanes[x].b * y +
					 frustumPlanes[x].c * z +
					 frustumPlanes[x].d;

		// If the distance was negative, the point is outside the frustum.
		if(dist < 0)
			return false;
	}
	// the point was inside the frustum.
	return true;
}

void Camera::buildWorldFrustumPlanes(void)
{
	// Note: Extract the frustum planes in world space.
	D3DXMATRIX VP = mView * mProj;

	D3DXVECTOR4 col0(VP(0,0), VP(1,0), VP(2,0), VP(3,0));
	D3DXVECTOR4 col1(VP(0,1), VP(1,1), VP(2,1), VP(3,1));
	D3DXVECTOR4 col2(VP(0,2), VP(1,2), VP(2,2), VP(3,2));
	D3DXVECTOR4 col3(VP(0,3), VP(1,3), VP(2,3), VP(3,3));

	// Planes face inward.
	frustumPlanes[0] = (D3DXPLANE)(col2);        // near
	frustumPlanes[1] = (D3DXPLANE)(col3 - col2); // far
	frustumPlanes[2] = (D3DXPLANE)(col3 + col0); // left
	frustumPlanes[3] = (D3DXPLANE)(col3 - col0); // right
	frustumPlanes[4] = (D3DXPLANE)(col3 - col1); // top
	frustumPlanes[5] = (D3DXPLANE)(col3 + col1); // bottom

	for(int i = 0; i < 6; i++)
		D3DXPlaneNormalize(&frustumPlanes, &frustumPlanes);
}

//rendering

for(unsigned int z = 0; z < (mapData.map_Z - 1) / (PATCH_SIZE - 1); ++z)
		{
			for(unsigned int x = 0; x < (mapData.map_X - 1) / (PATCH_SIZE - 1); ++x)
			{
				maxX = patch[z * ((mapData.map_X - 1) / (PATCH_SIZE - 1)) + x].maxX;
				maxY = patch[z * ((mapData.map_X - 1) / (PATCH_SIZE - 1)) + x].maxY;
				maxZ = patch[z * ((mapData.map_X - 1) / (PATCH_SIZE - 1)) + x].maxZ;
				minX = patch[z * ((mapData.map_X - 1) / (PATCH_SIZE - 1)) + x].minX;
				minY = patch[z * ((mapData.map_X - 1) / (PATCH_SIZE - 1)) + x].minY;
				minZ = patch[z * ((mapData.map_X - 1) / (PATCH_SIZE - 1)) + x].minZ;

				// Check to see if the corners of the patches are within the frustum.
				if (!gCamera.pointInFrustum(maxX, maxY, maxZ) &&
					!gCamera.pointInFrustum(minX, maxY, minZ) &&
					!gCamera.pointInFrustum(minX, maxY, maxZ) &&
					!gCamera.pointInFrustum(maxX, maxY, minZ) &&
					!gCamera.pointInFrustum(maxX, minY, maxZ) &&
					!gCamera.pointInFrustum(minX, minY, minZ) &&
					!gCamera.pointInFrustum(minX, minY, maxZ) &&
					!gCamera.pointInFrustum(maxX, minY, minZ))
					continue;

				dxApp.d3dDevice->DrawIndexedPrimitive(type, 0, 0, NUM_VERTICES, indexSum, NUM_POLYGONS);
				indexSum += INDEX_SUM_OFFSET;
			}
		}

On the DrawIndexPrimitive is that the correct way to call it in DX with the setu I have? I know in OpenGL it is, I figured it's got to be similar? Thanks

Share this post


Link to post
Share on other sites
Advertisement
Erm, if that's your actual code, I can see one obvious problem straight away:


bool Camera::pointInFrustum(float x, float y, float z)
{
for(unsigned long x = 0; x < 6; ++x)
{
float dist = frustumPlanes[x].a * x +


Due to C++ scoping, the variable 'x' used for your for() loop will take precedence over the parameter 'x' passed into the function, so pointInFrustum will be pretty wrong most of the time...


Regarding the parameters for DrawIndexedPrimitive(), Tom Forsyth's mini-DirectX FAQ contains a useful explanation: http://tomsdxfaq.blogspot.com/2002_05_01_tomsdxfaq_archive.html#77028718

Share this post


Link to post
Share on other sites
Quote:
Original post by S1CA
Erm, if that's your actual code, I can see one obvious problem straight away:


bool Camera::pointInFrustum(float x, float y, float z)
{
for(unsigned long x = 0; x < 6; ++x)
{
float dist = frustumPlanes[x].a * x +


Due to C++ scoping, the variable 'x' used for your for() loop will take precedence over the parameter 'x' passed into the function, so pointInFrustum will be pretty wrong most of the time...


Regarding the parameters for DrawIndexedPrimitive(), Tom Forsyth's mini-DirectX FAQ contains a useful explanation: http://tomsdxfaq.blogspot.com/2002_05_01_tomsdxfaq_archive.html#77028718




Mars pulls out gun points at head and pulls trigger! Yes you are correct that should have been obvious, but I am in such a mess trying to learn DX as fast as possible from OpenGL I am over looking simple mistakes... Argh. Thanks I will try this out and see if that works and let you know.

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!