Search code examples
openglgraphicsshader

OpenGL: do front and back side of a single triangle plane have different light effects?


Say, we have a mesh with a single triangle(three vertices). Give a light source facing the triangle directly.

This single triangle facet(only one) in space conceptionally has two faces depending on the camera position. Let's call the face facing the light front and the other one back. We also pass to OpenGL the normal vector, whose direction is from the front face to light source.

My question is: with the lighting applied to the triangle, do the front and back face of the triangle appear the same? Or they appear differently with the back side darker and front side brighter? And why? I am curious about both the modern openGL and the older fixing-function opengl if it is related.

Also, does this has anything to do with the face-culling, where we treat CCW triangle front? I believe not. And the "front" concept in term of face-culling is different. It tells if a triangle(as a whole) front or back, not mentioning the two sides of it. Please point me out if i am wrong.


Solution

  • I am curious about both the modern openGL and the older fixing-function opengl if it is related.

    With modern GL, it is quite easy: You write the shaders. If you want to treat the front and back sides differently, you can do so. If you don't want that, you can do that too. Note that the fragment shader has access to the gl_FrontFacing input, which tells it which side of the face it is dealing with.

    In the legacy fixed-function pipeline, this is controlled by the GL_LIGHT_MODEL_TWO_SIDE parameter settable via glLightModel:

    If params [it refers to GL_LIGHT_MODEL_TWO_SIDE here] is 0 (or 0.0), one-sided lighting is specified, and only the front material parameters are used in the lighting equation. Otherwise, two-sided lighting is specified. In this case, vertices of back-facing polygons are lighted using the back material parameters and have their normals reversed before the lighting equation is evaluated. Vertices of front-facing polygons are always lighted using the front material parameters, with no change to their normals. The initial value is 0.

    So with the fixed-function pipeline, you also can do both.

    My question is: with the lighting applied to the triangle, do the front and back face of the triangle appear the same? Or they appear differently with the back side darker and front side brighter?

    Neither is really the case. Whether a face appears bright or dark hugely depends on the angle between the incident light from some light source and the surface normal (which you provide). The " do the front and back face of the triangle appear the same" is really a weird question because you by definition only see one of these surfaces. Now you could ask which appearance the other side would have, but since lighting is also view-dependent, this is not a meaningful question. To actually see the other side, you need to either:

    • Flip the triangle orientation. But it you just change the order of vertices without changing anything else (especially not the normal), the resulting lighting would be the same. This is true for both one-sided and two-sided lighting mode (assuming you use the same materials for both sides).

    • Change the viewer position to see the triangle from the other side. In two-sided lighting mode, this will make a huge difference in appearance, as the normal is reversed. In one-sided lighting, you also will see differences, because the specular component is dependent on the view angle too, but the diffuse part actually would not change.

    • You actually mirror the triangle in the world (including its normal). If you do that in two-sided lighting mode, and assuming the provided normal was perpendicular to the triangle's plane, will result in the same lighting as seen before, as effectively, it will use the same normal as before. In one-sided lighting mode, the normal will be reversed to what you had before, so there will be a huge difference again.