Jump to content
  • Advertisement

Archived

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

Raloth

frustum culling only half works

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

It does realize some things are visible but only some of the time. Mostly everything is always rendered, but it gives a ton of false positives. Sometimes in the distance on the edge it will say something is visible when it should be. Can anyone help? I am trying to find out if a section of terrain is visible. I drop the z coordinate in the test and the top/bottom planes to avoid complications with things on top of it extending over the view when the actual terrain isn't visible Extracting the planes:
void Frustum::CalculateViewFrustum() {
	float   proj[16];
	float   modl[16];
	float   clip[16];
	float   t;
	
	glGetFloatv(GL_PROJECTION_MATRIX,proj);
	glGetFloatv(GL_MODELVIEW_MATRIX,modl);
	
	// combine the matrices

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

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

	t = sqrt( planes[0].a * planes[0].a + planes[0].b * planes[0].b + planes[0].c * planes[0].c );
	planes[0].a /= t;
	planes[0].b /= t;
	planes[0].c /= t;
	planes[0].d /= t;

	// left plane

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

	t = sqrt( planes[1].a * planes[1].a + planes[1].b * planes[1].b + planes[1].c * planes[1].c );
	planes[1].a /= t;
	planes[1].b /= t;
	planes[1].c /= t;
	planes[1].d /= t;
	
	// bottom plane

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

	t = sqrt( planes[2].a * planes[2].a + planes[2].b * planes[2].b + planes[2].c * planes[2].c );
	planes[2].a /= t;
	planes[2].b /= t;
	planes[2].c /= t;
	planes[2].d /= t;
	
	// top plane

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

	t = sqrt( planes[3].a * planes[3].a + planes[3].b * planes[3].b + planes[3].c * planes[3].c );
	planes[3].a /= t;
	planes[3].b /= t;
	planes[3].c /= t;
	planes[3].d /= t;
	
	// far plane

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

	t = sqrt( planes[4].a * planes[4].a + planes[4].b * planes[4].b + planes[4].c * planes[4].c );
	planes[4].a /= t;
	planes[4].b /= t;
	planes[4].c /= t;
	planes[4].d /= t;
	
	// near plane

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

	t = sqrt( planes[5].a * planes[5].a + planes[5].b * planes[5].b + planes[5].c * planes[5].c );
	planes[5].a /= t;
	planes[5].b /= t;
	planes[5].c /= t;
	planes[5].d /= t;

}
Testing to see if a section is visible:
VISIBILITY Frustum::LandInFrustum(float x,float y,float size) {
	int c;
	int c2 = 0;

	for(int i=0;i<2;i++) {
		if(c2 != i) return partial;
		c = 0;
		if(planes[i].a * (x) + planes[i].b * (y) + planes[i].d > 0)
			c++;
		if(planes[i].a * (size+x) + planes[i].b * (y) + planes[i].d > 0)
			c++;
		if(planes[i].a * (size+x) + planes[i].b * (y) + planes[i].d > 0)
			c++;
		if(planes[i].a * (x) + planes[i].b * (y) + planes[i].d > 0)
			c++;
		if(planes[i].a * (size+x) + planes[i].b * (size+y) + planes[i].d > 0)
			c++;
		if(planes[i].a * (x) + planes[i].b * (size+y) + planes[i].d > 0)
			c++;
		if(planes[i].a * (x) + planes[i].b * (size+y) + planes[i].d > 0)
			c++;
		if(planes[i].a * (size+x) + planes[i].b * (size+y) + planes[i].d > 0)
			c++;
		if(c == 0)
			return hidden;
		if(c == 8)
			c2++;
	}
	for(int i=4;i<6;i++) {
		if(c2 != i) return partial;
		c = 0;
		if(planes[i].a * (x) + planes[i].b * (y) + planes[i].d > 0)
			c++;
		if(planes[i].a * (size+x) + planes[i].b * (y) + planes[i].d > 0)
			c++;
		if(planes[i].a * (size+x) + planes[i].b * (y) + planes[i].d > 0)
			c++;
		if(planes[i].a * (x) + planes[i].b * (y) + planes[i].d > 0)
			c++;
		if(planes[i].a * (size+x) + planes[i].b * (size+y) + planes[i].d > 0)
			c++;
		if(planes[i].a * (x) + planes[i].b * (size+y) + planes[i].d > 0)
			c++;
		if(planes[i].a * (x) + planes[i].b * (size+y) + planes[i].d > 0)
			c++;
		if(planes[i].a * (size+x) + planes[i].b * (size+y) + planes[i].d > 0)
			c++;
		if(c == 0)
			return hidden;
		if(c == 8)
			c2++;
	}
	return visible;
}
Currently I just mark visible sections like this since I'm still tracking down an illegal operation bug somewhere else:
for(int i=0;i<BottomNodesLong;i++) {
		for(int j=0;j<BottomNodesLong;j++) {
			if(frustum.LandInFrustum(i,j,BottomNodeSize) != hidden)
				visibleNodes[i][j] = true;
			else
				visibleNodes[i][j] = false;
		}
	}
I know that's a lot of code but I have no clue what's wrong . It's a lot harder to track down something that partially works than something that doesn't work at all. [edited by - Raloth on September 21, 2003 6:45:10 PM]

Share this post


Link to post
Share on other sites
Advertisement

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!