View Full Version : Simple lighting issue

01-31-2004, 03:22 AM
I am trying to implement lighting into a demo I am working on but even though I feel I understand the way the light model works, it doesn't seem to want to. I am using the following code in my Init() function, when it then comes to the renderscene function, the models I draw are all black instead of being the colour declared. I have removed all the texture mapping in the scene, tried using the (GL_COLOR_MATERIAL) function and played around with the colour of the light but never seems to make any difference either.

I was wondering if anyone out there has ever come across a similar problem and if so, how did it happen?

Many thanks for any replies.


GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 50.0 };
GLfloat light_position[] = { 0.0, 48.0, 500.0, 0.0 }; (just above position of object)

glClearColor (1.0, 0.0, 0.0, 0.0);
glShadeModel (GL_SMOOTH);

glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);


01-31-2004, 04:42 AM
Don't forget to set the material setting for your objects also. You have both lighting and object settings.

glEnable(GL_COLOR_MATERIAL); // Turn on setting object material via glcolor
glColorMaterial(GL_FRONT, GL_AMBIENT); // Set Ambient material for object
glColor4f(0.15, 0.15, 0.15, 1.0); // Color command now set's Ambient setting
glColorMaterial(GL_FRONT, GL_EMISSION); // Object Emission
glColor4f(0.0, 0.0, 0.0, 1.0); // Color command now set's Emission.
glColorMaterial(GL_FRONT, GL_SPECULAR);
glColor4f(0.5, 0.5, 0.5, 1.0);
glColorMaterial(GL_FRONT, GL_DIFFUSE);
glColor4f(0.85, 0.85, 0.85, 1.0);

Just remember to turn off GL_COLOR_MATERIAL, when you want to go back to adjusting color only. glDisable(GL_COLOR_MATERIAL);

Else the next post will be on why my I can't get my object color to change....

01-31-2004, 05:01 AM
And you need to use normals for your vertices...

01-31-2004, 08:14 PM
You seem to provide a direction light for your light pos vector. Ie if light pos ends with 0, than you should specify it is a direction vector for parallel light.

If you wanted a positional light, then you should set the last component to 1.

If your going for directional light, try passing a normalized direction.

02-02-2004, 01:05 AM
-- sorry about the multiple posts, they didn't appear on my screen until today for some reason -

Thanks Maximian, does that mean that everthing in the scene would be lit from one side - that is what I am after. I don't quite understand what you mean by declare a normalised vector though, I have added a 1 to the end of the position vector but it has no effect :o( Where would I position the light source if I wanted directional? Would it need to be really far away?



02-02-2004, 09:25 AM
A directional light source, generate parellel light rays, which would create the effect you would like. A positional light is like
a small light source, so that the position, ie the distance from the point light source to an object determines how brightly the object is lit.

For a directional light source, only the direction of the light source, and the direction of the face of a model are important.

For a directional light, you should set the last element to 0, not 1. But when you pass
the direction vector you should normalize.

mag = pos[0]*pos[0] + pos[1]*pos[1] + pos[2]*pos[2];

pos[0] /= mag;
pos[1] /= mag;
pos[2] /= mag;

Also remember a direction light, does not have a position. It is not located at some point, x,y,z. A directional light simply says that parallel light rays will shine along some direction. So <0,1,0> means light rays will shine toward the pos y direction.
That is you need to carefully pick the direction of your light source.