I just finished the chapter on lighting from the red book. So I figured id put some skillz to the test and rip out a program that modeled the solar system fairly accurately.
Well everything seemed to be going quite well. I got a window on the screen. I got opengl mapped to the window. I rendered a sphere at the origin of the screen. Now, ofcourse the sphere looked rather flat until I enabled the lighting. And so then even the lighting seemed to be working. But then I tried to position the light in various different positions and the way the sphere was being illuminated seemed rather bizarre. For example, with the modelview matrix always set to identity, i would set the light position to (0,1,0,0) and it would illuminate the sphere from the right.
Maybe im doing something stupid or maybe i really missed out on something, but i dont care cuz I really just wanna get it working. I tried to run all sorts of tests with no good results.
heres the main portion of the code:
void init()
{
glViewport(0, 0, width, height);
glClearColor(0.0, 0.0, 0.0, 1.0);
glClearDepth(1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.2, 1.2, -1.2, 1.2, -1.2, 1.2);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
float pos0[4] = {0.0, 0.0, 5.0, 0.0};
glLightfv(GL_LIGHT0, GL_POSITION, pos0);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glEnable(GL_LIGHTING);
glEnable(GL_NORMALIZE);
}
bool display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
float pos0[4] = {1.2, 0.0, 0.0, 1.0};
glRotatef(angle++, 1.0, 0.0, 1.0);
glLightfv(GL_LIGHT0, GL_POSITION, pos0);
glEnable(GL_LIGHT0);
glLoadIdentity();
glPointSize(10.0);
glDisable(GL_LIGHTING);
glBegin(GL_POINTS);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(4.5, 0.0, 0.0);
glColor3f(0.0, 1.0, 0.0);
glVertex3f(0.0, 4.5, 0.0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(0.0, 0.0, 4.5);
glEnd();
glEnable(GL_LIGHTING);
glLoadIdentity();
glRotatef(angle+=0.1f, 1.0, 0.0, 0.0);
DrawSphere(5);
return true;
}