# Directional lighting

This topic is 4554 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I'm having a problem with directional lighting. The color of the cube on the screenshots below is rgb(1,0.973,0.788). As you can see on the two screenshots below, some faces get the same color, while I would expect all faces in a different color. It doesn't look very good and I have no idea how it's possible. I'm quite sure the normals are correct as I didn't had this problem in the 3D modeler. Does somebody knows a solution for this problem? From one side: From the other side: Initialization of the light:
light0Position: Array[0..3] of GLfloat = (0.030153689906001,0.171010076999664,0.984807729721069,0);
LightAmbient : array[0..3] of GLfloat = (1,1,1,1);
LightDiffuse : array[0..3] of GLfloat = (1,1,1,1);
lightSpecular: Array[0..3] of GLfloat = (1.0,1.0,1.0,1.0);

glEnable(GL_LIGHTING);  //Enable Lighting
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lightAmbient) ;
glLightfv(GL_LIGHT0, GL_AMBIENT,@lightAmbient);
glLightfv(GL_LIGHT0, GL_DIFFUSE,@lightDiffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR,@lightSpecular);
glEnable(GL_LIGHT0);


Every frame, i transform the scene, reset the light position and draw the scene:
sceneTransform();
glLightfv(GL_LIGHT0, GL_POSITION,light0Position);
sceneDraw();



##### Share on other sites
As far as I know, a single light source (be it directional or point) can light only 3 faces of a cube. The other faces are not lit and will be assigned the ambient color of your cube, because your light ambient is set to white. So, unless your light somehow moves with the camera, the pictures show correct behaviour.

Tom

EDIT: if you want all faces lit, you'll require a second light in the scene.

##### Share on other sites
The lighting formula is just a dot product, isn't it? If it is, it should be possible for the six different planes of the cubes to have other colors.

##### Share on other sites
Quote:
 Original post by BTierensThe lighting formula is just a dot product, isn't it? If it is, it should be possible for the six different planes of the cubes to have other colors.

Yes, it is. But when the normal is facing away from the light (e.g. light_dir = (0,0,1), normal_dir = (0,0,-1)) the result will be negative (-1) and is zeroed. This is a simplified version of the lighting calculation:
vertex color =    emissionmaterial +    ambientlight * ambientmaterial +    sum over all the lights 0 to N - 1:        max( l * n , 0 ) * diffuselight * diffusematerial +        max( s * n , 0 ) * shininess * specularlight * specularmaterialN = the number of lights in the scenel = (lx, ly, lz), the unit vector that points from the vertex to the light positionn = (nx, ny, nz), the unit normal vector at the vertexs = (sx, sy, sz), the normalized sum of the vector from the vertex to the lightposition and the vertex to the viewpoint

Note the max() functions. For away facing triangles, this amounts to:
vertex color =    emissionmaterial +    ambientlight * ambientmaterial

Check this faq on the details.

Tom