Light problem

Hi.
I have a problem with lights, and I can´t solve it.
I´m drawing several shapes which are composed by polygons. I apply the same colors and lights to all shapes. All polygons have one face lighted and the other shaded. The problem is that in some shapes the faces lighted are just the opposite that in the others, and this causes an annoyed scene.
I have made some tests, and I can change the face lighted inserting the points just in the opposite order, but I can´t control this with a lot of shapes.

Is there any way to light the two faces of the polygons ( I have tried with glMaterialfv(GL.GL_FRONT_AND_BACK, doesn´t work to me ), or how can I get to light always the face in the same orientation??

Thanks.

Within OpenGL, the orientation of a polygon is determined by the ‘winding’ or ordering of the vertices as they’re given to OpenGL. If the points are given in counter-clockwise order when projected onto the screen, the face is considered to be front-facing, if the ordering is clockwise it’s a backfacing polygon.

This can be switched by using the command, glFrontFace() with GL_CW or GL_CCW to change which ordering is considered as being front-facing. However, IMO it is much better to specify the ordering that your shapes must abide by. Most modeling tools use CCW ordering already, and if you’re writing your own shapes that have inconsistent ordering, then it’s a bug within your model specification.

You can enable two-sided lighting, though, by using glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE)

Thanks for the answer lhkbob.
I have no control to create the shapes, so I can´t choose the order of vertices. I have tried with glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE), but it change the face which is lighted, but it doesn´t light the two faces of shapes.

If you don’t have control over creating the shapes, you need to specify the ordering policy otherwise there’s not much you can do. It is a lot more work for the graphics card if it can’t cull back faces or has to light the back faces, too. The winding of triangles is not a bad requirement, as said before, most modeling tools already do this.

Could post a screenshot though, the two-sided lighting should do two-sided lighting. I haven’t used it too much though, so I’m wondering what it looks like, or I might be able to notice some other problem.

I´m trying to get access to the process of creating shapes, perhaps I can transform some code. But I can´t solve one problem. How to know when a polygon is given in counter-clockwise order, or if the ordering is clockwise.
Thanks for your answers :wink:

Triangle3D tr3 = ...;
Triangle2D tr2 = matrix.transform(tr3);
Point2D p0 = tr2.getPoint(0);
Point2D p1 = tr2.getPoint(1);
Point2D p2 = tr2.getPoint(2);

if(crossProduct2D(subtract(p1,p0), subtract(p2,p0)) > 0)
  // CW
else
  // CCW

Hi again.
Thanks for the answer.

Now I have all my polygons directed to the same side, and the most of the polygons show correctly. But I continue with a problem. Some of the polygons appear black, they doesn´t apply the color that I have put to gl. I have made some tests and it seems to be related with concave and convex polygons, because when I delete some point it works. I delete the points which go toward the inside of the polygon.
I´m going to try to show an example of two polygons (with extrude) that execute the same code, the only difference is one point. Do you have some idea how to solve it??
Regards

http://lh5.ggpht.com/toyHrbwWROk/TDbUH5r_guI/AAAAAAAAABc/Bx2tEf-gSU/poligono2.JPG

http://lh6.ggpht.com/_toyHrbwWROk/TDbUHmTpbII/AAAAAAAAABY/qYVkxolbUo4/poligono.JPG

The normal is probably pointing in the wrong direction.

Your are right. It is a problem of the normal. If I change the points to calculate the normal, sometimes it draws correctly, but others it doesn´t, but I don´t know how to know if the normal is correctly calculated. I use always the same code to calculate it. Some idea?

I’m assuming you’re triangulating the concave polygons, so it’s likely that there’s a bug in your normal generation during the triangulation.

That’d odd. If you swap two points (or in general, change the winding order), you must invert the normal, always.