5

I am drawing only back faces of a geometry by invoking gl.cullFace(gl.FRONT). I noticed that the shade of light on these surfaces is opposite to what I expect. For correct rendering, do I have to explicitly reverse the direction of surface normals on back faces, or does the OpenGL subsystem automatically do that?

EDIT: Coming to think of it, if I reverse their normals manually they will become front faces and will be culled.

genpfault
  • 51,148
  • 11
  • 85
  • 139
Jayesh
  • 51,787
  • 22
  • 76
  • 99

1 Answers1

8

OpenGL front/back detection is based on winding, not the normal. The normal vector does not have any effect on whether the polygon is considered front or back facing.

I think what you want to do is set the GL_LIGHT_MODEL_TWO_SIDE option of glLightModel.

GL_LIGHT_MODEL_TWO_SIDE

params is a single integer or floating-point value that specifies whether one- or two-sided lighting calculations are done for polygons. It has no effect on the lighting calculations for points, lines, or bitmaps. If params 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.

Note that this only applies to the fixed pipeline.

===EDIT===

Using custom shaders (GLES2.0 or OpenGL3+), then in the fragment shader you have access to the special boolean gl_FrontFacing. To emulate two sided lighting in a shader just test for gl_FrontFacing and multiply normal by negative one if false.

Tim
  • 35,413
  • 11
  • 95
  • 121