Culling - how do I know which to use (FRONT, BACK, FRONT_AND_BACK)?

I know that by default there is BACK face culling but what if I’m loading a set of triangles that I don’t know which way the winding goes and I want to cull both sides, how do I get this to work?

I did set in my init():
gl.glEnable(GL.GL_CULL_FACE);

also, when rendering I tried to set


      // cull both sides
      gl.glCullFace(GL.GL_FRONT_AND_BACK);

      .... render my triangles ....

      // reset the culling to be back face again
      gl.glCullFace(GL.GL_BACK);

When I do GL.GL_FRONT_AND_BACK culling then I don’t see anything in my scene…none of the triangles.
When I simply use GL.GL_FRONT I now see the triangles that I didn’t when using GL.GL_BACK.

I don’t know what else I can do to get the triangles to show up correctly…I can’t believe that I would have to do a winding test on each triangle that I draw and then set the appropriate culling based on that winding because that would make no sense, especially since I’m currently trying to use VBOs and this would not be possible, per se. I know there must be some other problem that I’m encountering and why GL.GL_FRONT_AND_BACK is not working as I had expected…anyone have ideas?

FRONT_AND_BACK culls both sides, so doesn’t draw anything. What were you expecting to see?

If you don’t want to cull any faces, then use GL_NONE.

Edit: My bad. See below.

Yeah, I just realized that GL_FRONT_AND_BACK hides both sides…holy crap that was a stupid question. Thank you for you confirmation. I didn’t notice the the GL_NONE in the glCullFace() man page and so I stupidly assumed that GL_FRONT_AND_BACK would display both but just a couple of minutes after writing this post I found what you just confirmed.

I tried the GL_NONE but it still culls the back and hence some of my triangles are still not showing up.

This glCullFace() man page http://www.ncrg.aston.ac.uk/~cornfosd/graphics/opengl/htmlman/cullface.html doesn’t mention that GL_NONE can even be used.

I just realized that I can simply disable the culling in total before my rendering, as in:


gl.glDisable(GL.GL_CULL_FACE);

... do triangle rendering ...

gl.glEnable(GL.GL_CULL_FACE);

and that does what I expect…it draws both sides of the triangles and doesn’t cull them. Why did they have to use a word like ‘cull’ to confuse my little brain…change the word to ‘hide’ :slight_smile:

btw, as a followup…I have 3DS models for which if I disable the culling then the model looks good but for some models I need to enable culling else they don’t look correct.

In particular, I have a model that has a bunch of think and flat boards, for a lack of a better word…well, the distance between the top part of the board and bottom part of the board is small and when I zoom out far enough and have disabled culling, then the back of the board bleeds through the front of the board because of the floating point precision is higher than the distance between the triangles of the front and back of the board at the zoom level (at least that’s what I think is happening).

I’m not sure I have a question, per se, because I don’t know if there could even be a simple solution to this problem…how do I know when to use culling and when not to without having to do a visual inspection.

All your models should have a well-defined winding order when created. If they don’t then that’s a bug in your file format (or possibly whatever exporter you use to create the file). For consistency it’s easiest to use the same winding order across all models.

Alternatively if you’re having zfighting trouble then try pushing your near plane out a bit more. A common mistake is having the near plane at 0 or something very low like 0.1 when a larger value will do. Since the precision is not stored linearly you loose a lot of precision by having it too close.

I can’t guarantee that the models will have a well defined winding…unfortunately I didn’t create the models that I’m using.

I think it might be with the near plane being too close, as you mention. I do have a close near plane (0.01) because of the scaling of the view that I have selected. Unfortunately if I increase this then I can’t zoom in as close to the model as I would like (ie translate towards it as close before clipping occurs … I don’t scale to zoom).

Thanks for you reply, that helps me understand better the issues I need to address.

As for not being able to guarantee winding order, the test for winding isn’t too hard to perform once. As you read in a file, what’s stopping you from manipulating the winding order of a given triangle so that for an entire model the winding is the same (or so that all models use the same winding). Then when you render, you know that having culling enabled will have everything display properly (assuming you’re telling gl to use the correct cull face).

when I read a file, and I read two consecutive triangles (ie their vertices) what does that tell me? if I don’t know which way the triangles face based on some other info it doesn’t help me to test for winding on my own. I would have to test each triangle against every other triangle.

I mean, the triangle only gives me vertices and the order of those vertices from which I could, at best, get a normal vector to that triangle face but I can’t guarantee that the corresponding face is a BACK or FRONT face unless I had other info provided in the model file that told me this. So, if the entire model has problems in the order of the vertices of triangles (ie they don’t protect for the same winding across all triangles) then I can’t do this myself unless I literally calculate which is the outer face of every triangle and that’s nearly impossible.

Unfortunately there’s no guaranteed way of figuring out the winding order, the best you can do is take a guess based on some statistics (like comparing the winding order to the averaged vertex normals, or trying to figure out the volume that the triangles enclose and mark that as “inside”). This is why I say that it’s a bug in the model if it’s got an inconsistant/unknown winding. If it’s something your artist has given you then go give them a cuff around the ear. If it’s user-supplied content then either give them an option to specify the winding order or just turn off back face culling.

Alternatively, if you want to zoom in really close then instead of translating really close you can reduce the field of view which might be an acceptable alternative.

Oops, I assumed too much when I posted that. I thought that in the files, the winding of a given triangle would be given, but that the windings might not be consistent (instead of being uncomputed) throughout the file.