PDA

View Full Version : Regarding Backface culling

myk45
06-06-2012, 09:18 AM
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.

Janika
06-06-2012, 10:54 AM
Called once. Only one face of the same polygon is visible at a time.

aqnuep
06-06-2012, 12:14 PM
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 (http://www.opengl.org/sdk/docs/man/xhtml/glFrontFace.xml)).

menzel
06-06-2012, 12:15 PM
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).

Janika
06-06-2012, 01:13 PM
A triangle does not have multiple faces.

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

Kopelrativ
06-06-2012, 11:02 PM
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.

Janika
06-07-2012, 07:12 AM
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.

myk45
06-07-2012, 09:55 PM
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!

tonyo_au
06-08-2012, 01:30 AM
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!?

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.

myk45
06-08-2012, 10:42 AM
@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!

aqnuep
06-08-2012, 11:01 AM
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.

myk45
06-09-2012, 11:10 AM
Thanks aqnuep! Got it.