backface culling not working

Hi all,
I am rendering a terrain using a heightmap with triangle strips. I’m trying to speed it up by enabling gl culling, but for some reason, nothing gets culled. Any suggestions?


What do you mean ? Hidden faces are still visible ? What have you done to do so ? Did you ensure your triangles are well winded ?

Basically, you only should to do that:

glCullFace (GL_BACK);

I do:

Is there a way to ensure the winding when using a triangle strip? Also, at the beginning, the camera is under the terrain, so I would think that the underside of the terrain would be culled. If not, the top should be culled, so I’m a little confused.

your winding might be clockwise.
try glFrontFace(GL_CW) and see if that fixes it.

1 Like

Just tried it. Still no luck. I would think that would have worked if the wrong things were getting culled, but the problem is nothing’s getting culled at all.

Post the relevant rendering code.

I got this out of a Terrain tutorial online somewhere. It does a ‘z’ pattern across the rows to create the triangle strip.

for( int j = 0; j < m_width-1; j++ ) {
	for( int i = 0; i < m_height-1; i++ ) {
		// draw vertex 0
		glVertex3d(m_data[i][j].x, m_data[i][j].y, m_data[i][j].z*m_scale);
		// draw vertex 1
	       glVertex3d(m_data[i+1][j].x, m_data[i+1][j].y, m_data[i+1][j].z*m_scale);

		// draw vertex 2
		glVertex3d(m_data[i][j+1].x, m_data[i][j+1].y, m_data[i][j+1].z*m_scale);

		// draw vertex3
		glVertex3d(m_data[i+1][j+1].x, m_data[i+1][j+1].y, m_data[i+1][j+1].z*m_scale);

Yeah, but where are you setting up your backface

I have the following initialization in a separate function that gets called once at the beginning of the program, after the window has been created:

	glClearColor(1.0f, 1.0f, 1.0f, 1.0f);


Here’s a little more info on my problem. I tried rendering the terrain with only one strip. I just changed the inner for loop to:

for( int i = 25; i < 26; i++ ) {

(just choosing 25 at random)
This actually works and culls the back face. I can tell because I render in wireframe and can observe it. However, if I do any more triangle strips the backface is not culled. Has anyone seen this behavior before?

Why have you got 4 vertices calls inside your triangle strip loop ??? I think the main problem is coming from there. Did you ensure you drawn strips correctly ?? Or maybe do you use many degenerated triangles.

What’s the way you went ?

Ok, it seems I’ve gotten it work…the problem was a lot simpler. The order of the loops had to be switched. Once I did that, the culling worked. I think 4 vertices in the loop is correct. However, the inner loop variable should increment a with j+=2. It does seem to work without this though.