PDA

View Full Version : backface culling not working



ag
04-13-2005, 05:42 AM
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?

thanks,
ag

jide
04-13-2005, 05:47 AM
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);

ag
04-13-2005, 05:53 AM
I do:
glFrontFace(GL_CCW);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);

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.

Aeluned
04-13-2005, 06:08 AM
your winding might be clockwise.
try glFrontFace(GL_CW) and see if that fixes it.

ag
04-13-2005, 06:13 AM
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.

Aeluned
04-13-2005, 08:56 AM
Post the relevant rendering code.

ag
04-13-2005, 09:02 AM
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++ ) {
glBegin(GL_TRIANGLE_STRIP);
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);
}
glEnd();
}

Aeluned
04-13-2005, 09:04 AM
Yeah, but where are you setting up your backface
culling?

ag
04-13-2005, 09:12 AM
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);
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);

glFrontFace(GL_CW);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);

ag
04-13-2005, 11:51 AM
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?

jide
04-14-2005, 12:45 AM
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 ?

ag
04-14-2005, 04:14 AM
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.