Skip to main content
GameDev.net gamedev.net
🔒 Locked

Problem understanding GL_LIGHTING

Started by Merowingian Jul 3, 2007 at 8:57 AM 2 replies 2.1k views
Original Post
Merowingian
Merowingian
Hey, I have written the following code to help me understand how lighting works in OpenGL: void initGL() { glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black background glMatrixMode(GL_PROJECTION); glLoadIdentity(); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); GLfloat lightPos[4] = {1.0f, 1.0f, 1.0f}; glLightfv(GL_LIGHT0, GL_POSITION, lightPos); GLfloat noLight[] = {0.0f, 0.0f, 0.0f, 1.0f}; GLfloat yellowLight[] = {1.0f, 1.0f, 0.0f, 1.0f}; glLightfv(GL_LIGHT0, GL_AMBIENT, noLight); glLightfv(GL_LIGHT0, GL_DIFFUSE, yellowLight); glLightfv(GL_LIGHT0, GL_SPECULAR, noLight); GLfloat matAmbient[] = {1.0f, 1.0f, 0.0f, 1.0f}; GLfloat matDiffuse[] = {1.0f, 1.0f, 0.0f, 1.0f}; glMaterialfv(GL_FRONT, GL_AMBIENT, yellowLight); glMaterialfv(GL_FRONT, GL_DIFFUSE, noLight); glMaterialfv(GL_FRONT, GL_SPECULAR, noLight); } void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear screen and depth buffer glColor3f(1.0f, 0.0f, 0.0f); glutSolidSphere(0.5, 100, 100); glFlush(); } As you can see, I've turned off all but the diffuse light for GL_LIGHT0. As for the material, I've turned on ONLY the ambient light reflectance, that is no reflectance of diffuse or specular light. I would then expect to see nothing at all, since there is NO ambient light coming in from GL_LIGHT0, which means NO ambient light to be reflected. The material shouldn't be able to reflect ANY ambient light, despite ambient light being turned on for the material. The only light coming in from GL_LIGHT is diffuse light, but reflectance for THAT light is turned off for the material, so no reflectance of that either. But what I get when running the program is a dark yellow, equally spreaded across the sphere. I don't see the logic here, I should be seeing nothing but a black window. It looks like the ambient material value is not only responsible for the ambient light, but somehow also for all the other ones. Well, I would like to know how the light reflectance values are calculated. Furthermore, I don't really understand the logic of having different "kinds" of light sources, although I tried to apply that in my program. I used to think that it's the MATERIAL ONLY that decides how ANY INCOMING LIGHT is reflected. The idea of "diffuse and specular" light sources suggests to me the some lights "want to be reflected diffusely only, some only want to be reflected specularly". What is supposed to be the technical difference of those "various light sources", and how is a material supposed to "distinguish" between them? Cheers
bakery2k1
bakery2k1
As well as an ambient component of each light, there is a global ambient light level, which defaults to (0.2, 0.2, 0.2, 1.0). This can be controlled using glLightModel.

If you turn global ambient light off using
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, noLight);
you should then see the black window you expected.
Merowingian
Merowingian
Hey thanks, that solved my problem. I'm now getting exacly what I'm expecting.

Cheers
Marvin
V-man
V-man
GLfloat lightPos[4] = {1.0f, 1.0f, 1.0f};

that should be

GLfloat lightPos[4] = {1.0f, 1.0f, 1.0f, 1.0};

or

GLfloat lightPos[4] = {1.0f, 1.0f, 1.0f, 0.0};

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.