i'm trying to create a solar system (only sun, earth and moon) in openGL. My only light source is the sun, which should light the other planets up. I got it working so far, but the faces of the planet which face away from the sun are also lit, i want them to be dark.
The objects are glutSolidSpheres
, the Sun is at the origin and there is also my light source.
Here is the code of my lighting setup:
GLfloat light_position[] = {0.0, 0.0, 0.0, 1.0};
GLfloat light_ambient_color[] = {0.0, 0.0, 0.0, 0.0};
GLfloat light_diffuse_color[] = {1.0, 1.0, 1.0, 1.0};
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient_color);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse_color);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glColorMaterial ( GL_FRONT, GL_DIFFUSE ) ;
glEnable(GL_COLOR_MATERIAL);
What am i doing wrong? Can you guys give me a hint?
Actually i just realised that ratchet freak was right in the comments. I called gluLookAt after i set the lights position and so it "rotated" it away. I now called it afterwards and it works now!
The light source is inside the sun, which has normals pointing in the "wrong" direction compared to the light source. That is, no light comes from your sphere, but because you have diffuse light turned on you can see the spheres nevertheless. Try flipping the normals of the "sun" by using the "GL_LIGHT_MODEL_TWO_SIDE" property for your light model. To test whether it will work you can simply not draw the sun, and add it later again.
BTW this guy had the exact same problem, maybe it helps: http://www.opengl.org/discussion_boards/showthread.php/122872-Reversing-normals-for-glutSphere
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With