Regarding Backface culling

Hello All!

I have a small doubt w.r.t backface culling. When it is disabled, is the fragment shader called twice for all the fragments belonging to the polygon? For eg, say we have a quad, is the fragment shader called for both sides of the quad? Front and back? If yes, how is the final color of the fragment decided?

Any links or explanation would be really helpful.

Thanks in advance!

Called once. Only one face of the same polygon is visible at a time.

A triangle does not have multiple faces. Front and back facing means whether that polygon is “facing” the camera or not. “Facing” is defined based on the ordering of the vertices of the triangle in screen space. One can define whether triangles that have their vertices clock-wise ordered or the ones that have their vertices counter-clock-wise ordered are considered front facing (see glFrontFace).

It is called only once. If backface culling is enabled and the face is facing away from the camera, the face will get trown away before rasterization.
You can check whether a fragment belonged to a front or backfacing polygon with gl_FrontFacing in GLSL (of course it will be always true if backface culling is enabled).

A triangle does not have multiple faces.

Mathematically speaking, it does have 2 faces assuming it’s defined in a Euclidean 3-dimensional space.

Correct, but the fragment shader is only called once, at most, for every pixel. And there is never more than one visible face. So in OpenGL, there is only one face. In graphics, if you can’t see it, it doesn’t exist. For example, the applications go to a great effort not to draw objects hidden behind other objects, or being behind the camera.

There are actually two kinds of “facing” mechanisms in OpenGL. One is a “facing” that is defined as depending on the order of the vertices. It is an artificial definition, and as far as I know only used for culling. The purpose is to improve performance as the fragment shader do not have to be called for these pixels.

There is also the “facing” mechanism where you define the normal of the the triangle. Using the normal, you can get different lighting effects depending on the angle to light sources and the normal(s). Still, pixels in the triangle are only rendered once. But the shader can now have the option to give different results for the backside of the triangle, from the point of view of the light source. That is, you compute the dot product of the normal and the light source. A negative result means the face is on the back side and could be treated as being in a shadow. But this facing mechanism is relative light sources, and not relative the camera.

Otherwise, as every pixel in a triangle is computed only once, independent of facing, it means the pixel will look the same from both sides.

Yep the order of vertices is a way to calculate a normal associated with one face. The power of vertex order shows when determining a face in screen space, where the signed area of the polygon/triangle is used.

Hello All,

Thanks a lot for the explanations.

@menzel:
>> If backface culling is enabled and the face is facing away from the camera, the face will get trown away before rasterization.

That was precisely my doubt. At what part of the pipeline is the culling exactly done. So, suppose i don’t have backface culling enabled, then are the vertices send to the vertex shader twice? i.e once in clockwise order and once in anticlockwise!?
I’m now confused. Can you please clarify this for me?

Thanks!

[QUOTE=myk45;1238519]Hello All,
So, suppose i don’t have backface culling enabled, then are the vertices send to the vertex shader twice? i.e once in clockwise order and once in anticlockwise!?
[/QUOTE]

No, The vertex shader is only executed once. The pipe line vertex shader -> fragment shader generates a set of fragments; each fragment either belongs to the back face or front face. With no culling, all fragments are delivered to the fragment shader.
Each fragment says whether it is part of the front or back face. If we think of a pixel on the screen being covered exactly by 1 fragment; that fragment is either from the front or that back of the triangle.

An other way to look at is to take a piece of paper that is red on one side and green on the other. You only see either red or green paper; never both at once. The render pipe line works the same way. It knows the fragment generated belongs to the back or front; it never belongs to both at once.

@tonyo_au

Thanks a lot! I think i finally get it. So, say ‘n’ fragments are generated per face, without backface culling, 2’n’ fragments are generated and sent to the fragment shader, whereas ‘n’ fragments in case of backface culling enabled. I hope i got it right this time. Please let me know.

Thanks a lot!

No, you still got it wrong:

  1. ‘n’ fragments are generated for a triangle without backface culling.
  2. ‘n’ fragments are generated for a triangle with backface culling if the triangle is determined to be front facing.
  3. ‘0’ fragments are generated for a triangle with backface culling if the triangle is determined to be back facing.

You never ever get two faces being rasterized for a single incoming triangle.

[QUOTE=myk45;1238554]@tonyo_au

Thanks a lot! I think i finally get it. So, say ‘n’ fragments are generated per face, without backface culling, 2’n’ fragments are generated and sent to the fragment shader, whereas ‘n’ fragments in case of backface culling enabled. I hope i got it right this time. Please let me know.

Thanks a lot![/QUOTE]

Still not quite right. If ‘n’ fragments are generated per face, with culling enabled, either ‘n’ or 0 fragments are sent to the fragment shader. And then, there are a numerous other ways that fragments can get dropped before they arrive to the fragment shader (tests of the depth buffer, stencil tests, etc).

Thanks aqnuep! Got it.