1. ## Lighting problem

Hi people, I was just wondering how to draw a lit polygon correctly. I draw a plate in clockwise orientation and doing this I'm telling Opengl that the face I'm looking is back. This way is right because my "sun" is in the other side of the plate and all light goes well. But, if I rotate the plate, the back side should be lit, but because it is the backside, even in front of the "sun" it is not enlightened. In addition, the side that doesn't face the sun, the front one, is enlightened.

My question is, what is the secret to do Opengl control this automatically, or, how can I draw a polygon in such a manner that the lighting can lit the faces correctly? In real life it doesn't matter what side is the front or back one, what matter is that if a side faces light, it will be enlightened. How to do it?

2. ## Re: Lighting problem

Lighting is governed by normals, do you supply them for your polygon? (glNormal3f)

3. ## Re: Lighting problem

Can you put some screen shot of your plate with normals visible? For my part, it is clearly not understandable.

All normals should point out of the objet if you want that its surface is correctly lit.

4. ## Re: Lighting problem

I supply the normals. But I can't see at where it points. All I did was:

Code :

GLfloat v1[] = {0,0,0};
GLfloat v2[] = {3,0,0};
GLfloat v3[] = {3,3,0};
GLfloat v4[] = {0,3,0};

glBegin(GL_POLYGON);

//counterclockwise

glNormal3f(v1);
glVertex3f(v1);

glNormal3f(v2);
glVertex3f(v2);

glNormal3f(v3);
glVertex3f(v3);

glNormal3f(v4);
glVertex3f(v4);

glEnd();

Obs: it is in a display list.

The light source is at (0,30,-100) and the cam is at (0,0,5). In that way it is correct. But, if I rotate the object 180 degrees, the other side, the one who was hidden from the light, now faces the light, right? But, instead of being lit, it stays unlit.

5. ## Re: Lighting problem

GLfloat v1[] = {0,0,0};
glNormal3f(v1);
glVertex3f(v1);
See this? Normal is vector with length of 1.0, not zero, not 3. Supply proper normals (0, 0, 1).

6. ## Re: Lighting problem

Your quad is in the XY plane so normals should be by definition orthogonal to this plane and point in the Z or -Z direction in your case.

7. ## Re: Lighting problem

hm.. the normal should be defined in a point different from the vertex? I though the function glnormal calculates automatically this. So, in my example above, what is the correct normal coordinates I should pass?

and if a point doesn't lie only in XY plane, but in the Z too?

thanks

8. ## Re: Lighting problem

I think you need to read about 3D rendering basics. Without wanting to be offensive, you are trying to draw some 3D stuff without knowing what you really do.

A normal is a vector orthogonal to a polygon and be should normalized for lighting computation. So if you had read the glNormal spec, you would have seen that you need to supply a normal vector which is independant of vertices position.

9. ## Re: Lighting problem

No, I know about 3d basics, I just don't know how the function glnormal actually works. I know what is a normal vector, I know how to find it manually (cross product betwenn 2 vectors in a plane). Please, correct my example above, giving me the proper coordinates to normal in that case so I can figure out by myself how I have to proceed with glnormal.

By the way, I normalize automatically all vertices by enabling GL_NORMALIZE.

10. ## Re: Lighting problem

Yes you can normalize normals with glEnable(GL_NORMALIZE) but that does not work for normals with a length equal to zero.

glNormal is very easy to use, you have 3 parameters that are the normal coordinates. In you example, normals are all eaqual to (0,0,1).
So you set normals for each vertex like this:

glNormal3f(0,0,1);

this is what you would have found calculating the cross product... as you said.

http://www.opengl.org/sdk/docs/man/xhtml/glNormal.xml

By the way, your glNormal3f and glVertex3f calls are wrong, you should call glNormal3fv and glVertex3fv since you set parameters as arrays.

Page 1 of 2 12 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•