OpenGL Polygon Sorting Problem?


A friend of mine is using some modified sample OpenGL code to see if he can port his software engine over to GL. This is for a game that I’m helping him create models for. I’d really like to see this work so we can utilize OpenGL features.

As of right now, we’ve got the model displaying, but the polygons don’t seem to be sorting correctly. He seems to think it’s a mistake with the zbuffers, but is unsure. When the model is rotated, some (random?) polygons black out.

Any ideas on how to solve this? Thanks!

Project Zip File -

You’re right. It’s a classic case of z fighting. You should never have the zNear at 0.0f. In the following line of your program I changed the second last parameter (zNear) from 0.0f to 1.0f and it sort of works. You don’t get the z fighting anymore but there are some other transparency issues (unless it’s meant to look like that?). I don’t have time to look at it now - off to uni.

// Calculate The Aspect Ratio Of The Window

Hope that helps.

Thanks for the fast response!

I tried the recommended change and it did indeed eliminate the random poly problem. As for the odd transparency issue, I’d have to ask my friend, but I noticed that if I tried making the change from:




– it darkens the scene, but seems to fix the transparency problem. Any gurus out there know if this is displaying correctly now?

Ok, no. My attempt at fixing the transparency only hid the front faces and caused the scene to light the back faces of the polygons, inverting the model. Looked great, but that won’t work! So, anyone know what’s wrong with the code that’s causing it to incorrectly display (odd transparency) using the Front Faces of the polygons?

Please help! Thanks.

I don’t know how you’re generating your models (I suck at 3D modelling) but at a guess, I’d say you have to be careful how the triangles in your model are saved. If I have an application with a model like that, I make sure that all outwards facing polygons are defined with vertices in an anti-clockwise order (counter-clockwise for all the Americans out there ). You can also define them clockwise, as long as you’re consistent with the ordering. If you have clockwise ordering, then you use glFrontFace(GL_CW); otherwise use the default, GL_CCW.

It may be in your model file that the top of the “tank” is defined differently from the base. That may account for the odd transparency.

Hope that helps.

The problem was not in the data, but how the near/far clipping planes were configured. The model was rendering correctly, but openGL could not sort because of bad depth clear and near/far plane values.

Setting depth clear to 1.0, and setting near/far planes to 1.0/400.0 fixed it, as well as setting the depth sort function to GL_LEQUAL

It looks as if you’re having problems with transparant surfaces being culled (I haven’t looked at the code - its just from what you’ve said)…to prevent this you should switch back face culling off when rendering transparant polys - have a look a one of the earlier NeHe tutorials that illustrates transparancy

Ok, I fixed it (in like 5 minutes :P)

Your problem was you were using a backward depth test, but that 0.0 near z did you in with the flickering. By changing your depth test to GL_GEQUAL and setting the ClearDepth() to 0.0f, the reverse draw is fixed. Not too bad a program though… can I keep it? :stuck_out_tongue: