Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When BACK faces are drawn, are their normals automatically reversed?

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.

like image 859
Jayesh Avatar asked Feb 20 '23 20:02

Jayesh


1 Answers

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.

like image 161
Tim Avatar answered Apr 28 '23 09:04

Tim