Archived

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

Help with code

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

Having problems with the frustum culling in a landscape program I am trying to make. Below are the parts of the code. Any help would be greatly appreciated, as I am new to DIrectX and C++.
//This is where I calculate the bounding parameters for the node:

float cTERRAIN::BoundingSphere(cQUADNODE* pQuadNode)
{
	int minHeight, maxHeight;
	minHeight = 0;
	maxHeight = 0;

	for (int i = pQuadNode->iMinZIndex; i <= pQuadNode->iMaxZIndex; i++)
	{
		for(int j = pQuadNode->iMinXIndex; j <= pQuadNode->iMaxXIndex; j ++)
		{
			if(minHeight > getHeightmapEntry(j,i)) minHeight = getHeightmapEntry(j,i);
			if(maxHeight < getHeightmapEntry(j,i)) maxHeight = getHeightmapEntry(j,i);
		}
	}

	//Bounding box for node

	//D3DXVECTOR3 box[8];

	pQuadNode->box[0] = D3DXVECTOR3((float)((pQuadNode->iMinXIndex - SizeXZ/2)*cellSpacing),(float)minHeight,(float)((SizeXZ/2) - pQuadNode->iMinZIndex)*cellSpacing);
	pQuadNode->box[1] = D3DXVECTOR3((float)((pQuadNode->iMinXIndex - SizeXZ/2)*cellSpacing),(float)minHeight,(float)((SizeXZ/2) - pQuadNode->iMaxZIndex)*cellSpacing); //Minimum values

	pQuadNode->box[2] = D3DXVECTOR3((float)((pQuadNode->iMaxXIndex - SizeXZ/2)*cellSpacing),(float)minHeight,(float)((SizeXZ/2) - pQuadNode->iMaxZIndex)*cellSpacing);
	pQuadNode->box[3] = D3DXVECTOR3((float)((pQuadNode->iMaxXIndex - SizeXZ/2)*cellSpacing),(float)minHeight,(float)((SizeXZ/2) - pQuadNode->iMinZIndex)*cellSpacing);
	pQuadNode->box[4] = D3DXVECTOR3((float)((pQuadNode->iMinXIndex - SizeXZ/2)*cellSpacing),(float)maxHeight,(float)((SizeXZ/2) - pQuadNode->iMinZIndex)*cellSpacing);
	pQuadNode->box[5] = D3DXVECTOR3((float)((pQuadNode->iMinXIndex - SizeXZ/2)*cellSpacing),(float)maxHeight,(float)((SizeXZ/2) - pQuadNode->iMaxZIndex)*cellSpacing);
	pQuadNode->box[6] = D3DXVECTOR3((float)((pQuadNode->iMaxXIndex - SizeXZ/2)*cellSpacing),(float)maxHeight,(float)((SizeXZ/2) - pQuadNode->iMaxZIndex)*cellSpacing);
	pQuadNode->box[7] = D3DXVECTOR3((float)((pQuadNode->iMaxXIndex - SizeXZ/2)*cellSpacing),(float)maxHeight,(float)((SizeXZ/2) - pQuadNode->iMinZIndex)*cellSpacing); //Maximum values


	//Find center of bounding sphere

	pQuadNode->vCenter = D3DXVECTOR3(0,0,0);
	for(i = 0; i < 8; i++)
		pQuadNode->vCenter += pQuadNode->box[i];
	pQuadNode->vCenter /= 8;

	//Find furthest point in set

	float radius = 0.0f;
	for (i = 0; i < 8; i++)
	{
		D3DXVECTOR3 v;
		v = pQuadNode->box[i] - pQuadNode->vCenter;
		float distSq = D3DXVec3LengthSq(&v);
		if (distSq > radius)
			radius = distSq;
	}
	radius = sqrtf(radius);

	radius = sqrtf(radius);

	return radius;
}

//This is where i setup the view frustum:

VOID CAMERA::UpdateFrustum()
{

//	D3DXVECTOR3 t_vec(1.0f, 1.0f, 1.0f);

	D3DXMATRIXA16 t_mat =*D3DXMatrixIdentity(&t_mat);

	//multilply view and projection matrixs to give current view frustum

	D3DXMatrixMultiply(&t_mat, &m_matView, &m_matProjection);

	//left side

	bc_frustum[0] = D3DXPLANE(t_mat._14 + t_mat._11, t_mat._24 + t_mat._21,
		t_mat._34 + t_mat._31, t_mat._44 + t_mat._41);
	D3DXPlaneNormalize(&bc_frustum[0], &bc_frustum[0]);

	//right side

	bc_frustum[1] = D3DXPLANE(t_mat._14 - t_mat._11, t_mat._24 - t_mat._21,
		t_mat._34 - t_mat._31, t_mat._44 - t_mat._41);
	D3DXPlaneNormalize(&bc_frustum[1], &bc_frustum[1]);

	//bottom

	bc_frustum[2] = D3DXPLANE(t_mat._14 + t_mat._12, t_mat._24 + t_mat._22,
		t_mat._34 + t_mat._32, t_mat._44 + t_mat._42);
	D3DXPlaneNormalize(&bc_frustum[2], &bc_frustum[2]);

	//top

	bc_frustum[3] = D3DXPLANE(t_mat._14 - t_mat._12, t_mat._24 - t_mat._22,
		t_mat._34 - t_mat._32, t_mat._44 - t_mat._42);
	D3DXPlaneNormalize(&bc_frustum[3], &bc_frustum[3]);

	//near

	bc_frustum[4] = D3DXPLANE(t_mat._14 + t_mat._13, t_mat._24 + t_mat._23,
		t_mat._34 + t_mat._33, t_mat._44 + t_mat._43);

	D3DXPlaneNormalize(&bc_frustum[4], &bc_frustum[4]);

	//far

	bc_frustum[5] = D3DXPLANE(t_mat._14 - t_mat._13, t_mat._24 - t_mat._23,
		t_mat._34 - t_mat._33, t_mat._44 - t_mat._43);
	D3DXPlaneNormalize(&bc_frustum[5], &bc_frustum[5]);
	
	return;
}
// And finally where I check the bounding Sphere:

int CAMERA::CheckSphereFrustum(D3DXVECTOR3 center, float radius)
{
	//Make sure radius is in frustum

	for(short i = 0; i < 6; i++)
	{
		FLOAT fPlaneDotCoord = D3DXPlaneDotCoord(&bc_frustum[i], ¢er);
		if((float)(fPlaneDotCoord) < radius)
		{
			return OUTSIDE;
		}
	}
	return INSIDE;
}
Thank you in advance for your help. [Please use [ source ] and [ /source ] tags when posting a lot of code. -Superpig] [edited by - Superpig on May 29, 2004 1:38:12 PM]

Share this post


Link to post
Share on other sites
Sorry, was my first post. The culling routine seems to over cull. When on the level of the terrain it looks like this:

| |
| \ / |
| \ / |
| --- |
|__________|

And when on a point of level terrain the culling is completly messed up. I am a begginer and new to 3D programming so not sure where I should look.

Thanks again

Share this post


Link to post
Share on other sites