I have created a room, actually rectangular with two windows and one door. I have been trying to light the matarials with their material properties. I have used the below code but it does not work; why?
NOTE: light is in the room, as expected
in myinit ()
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT1, GL_AMBIENT, light[0]);
glLightfv(GL_LIGHT1, GL_DIFFUSE, light[1]);
glLightfv(GL_LIGHT1, GL_SPECULAR,light[2]);
glLightfv(GL_LIGHT1, GL_POSITION,light[3]); // position of the light
glEnable(GL_LIGHT1);
in draw room ()
glMaterialfv ( GL_FRONT, GL_AMBIENT ,material[0] ) ;
glMaterialfv ( GL_FRONT, GL_DIFFUSE ,material[1] ) ;
glMaterialfv ( GL_FRONT, GL_SPECULAR ,material[2] ) ;
glBegin (GL_TRIANGLES);
glColor3f ( /*...*/ )
// ...
glEnd ( ) ;
in main ()
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
Have you enabled the lighting itself? Enabling just a light isn’t enough, you should enable the whole light system as well (it’s off by default)
It should happen in the initialization function:
Also, don’t forget:
If you want material colors. (You have it now, but it’s another little flag that can cause problems)
Another vital element one must understand in order to understand how lighting works in GL is normals. For every vertex you have in your geometry, you must also tell OpenGL which way it’s facing, so that it can calculate its direction relative to the light position, in order to determine how bright it should be. This looks like a decent introduction to the concept in OpenGL.
When I first started with OpenGL, I thought that these normals would be computed automatically from the given triangles. I was wrong. OpenGL doesn’t automatically compute the normals of a model, even though, in theory, it could (albeit with blocky results – maybe smooth with more advanced techniques).
And another recommendation – it would be a good idea to avoid using fixed pipeline functionality such as
glBegin(...) ... glEnd()blocks (also known as immediate mode). It’s outdated and slow. Check out this great tutorial series for a good introduction to modern techniques also used in modern games.