Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a solar system in opengl - lighting not working as planned

Tags:

c

opengl

lighting

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.

enter image description here

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?

like image 835
stainless Avatar asked Nov 02 '22 07:11

stainless


2 Answers

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!

like image 158
stainless Avatar answered Nov 15 '22 05:11

stainless


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

like image 39
invalid_id Avatar answered Nov 15 '22 04:11

invalid_id