5

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?

Raptor
  • 53,206
  • 45
  • 230
  • 366
stainless
  • 69
  • 1
  • 7
  • 1
    You probably have some problems with your normals. Maybe they are reversed? If you post that relevant code, we can have a look at it (**EDIT** Just saw you are using `glutSolidSpheres` I personally do not know how normals are setup with it) – user1781290 Jan 13 '14 at 10:41
  • 2
    or the light position is not transformed and put on the same place as the camera (the darker borders is a strong indication of this) – ratchet freak Jan 13 '14 at 11:20
  • 1
    The Dark Borders go away when i set Ambient_color to 1,1,1,1. Also the Camera is on a fixed position and the light is also fix at the origin.. – stainless Jan 13 '14 at 11:31
  • If you set ambient to full white you get full illumination of everything, that is why the dark borders disappear. You need to be careful about the Matrices when setting all values. Maybe you set the light with another ModelViewMatrix than the sun? Or the normals are scaled by some operation (you can activate `GL_NORMALIZE` to see if that's your issue) – user1781290 Jan 13 '14 at 13:08
  • Sorry, but none of that worked either. I don't have any normals set, because i'm using glutSolidSphere, so thats not necessary. When im using a directive light that shines from above with light_position[] = {0.0, 1.0, 0.0, 0.0} it works. The upper half of the spheres is lit, the bottom one is dark. But when i change the w-value of the pos-vector to a 1 again to get a positional light, it doesn't work anymore. I understood the positional light like this: it is at x,y,z coordinates and shines from there like a lightbulb in any direction, while the diretional light shines from origin to x,y,z. – stainless Jan 13 '14 at 16:33

3 Answers3

2

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!

stainless
  • 69
  • 1
  • 7
1

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

invalid_id
  • 804
  • 8
  • 12
-1
int i=0,j=260,k=30,l=150,m=90;
int n=230,o=10,p=280,q=220;
float pi=3.1424,a,b,c,d,e,f,g,h,z;



int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
outtextxy(0,10,"SOLAR SYSTEM -Mount Carmel");
outtextxy(400,10,"press the key to move");
circle(320,240,20);             
setfillstyle(1,4);
floodfill(320,240,15);
outtextxy(310,237,"sun");
circle(260,240,8);
setfillstyle(1,2);
floodfill(258,240,15);
floodfill(262,240,15);
outtextxy(240,220,"mercury");
circle(320,300,12);
setfillstyle(1,1);
floodfill(320,298,15);
floodfill(320,302,15);
outtextxy(335,300,"venus");
circle(320,160,10);
setfillstyle(1,5);
floodfill(320,161,15);
floodfill(320,159,15);
outtextxy(332,150, "earth");
circle(453,300,11);
setfillstyle(1,6);
floodfill(445,300,15);
floodfill(448,309,15);
outtextxy(458,280,"mars");
circle(520,240,14);
setfillstyle(1,7);
floodfill(519,240,15);
floodfill(521,240,15);
outtextxy(500,257,"jupiter");
circle(169,122,12);
setfillstyle(1,12);
floodfill(159,125,15);
floodfill(175,125,15);
outtextxy(130,137,"saturn");
circle(320,420,9);
setfillstyle(1,13);
floodfill(320,417,15);
floodfill(320,423,15);
outtextxy(310,400,"urenus");
circle(40,240,9);
setfillstyle(1,10);
floodfill(38,240,15);
floodfill(42,240,15);
outtextxy(25,220,"neptune");
circle(150,420,7);
setfillstyle(1,14);
floodfill(150,419,15);
floodfill(149,422,15);
outtextxy(120,430,"pluto");
a=(pi/180)*i;
b=(pi/180)*j;
c=(pi/180)*k;
d=(pi/180)*l;
e=(pi/180)*m;
f=(pi/180)*n;
g=(pi/180)*o;
h=(pi/180)*p;
z=(pi/180)*q;
cleardevice();
circle(320,240,20);
setfillstyle(1,4);
floodfill(320,240,15);
outtextxy(310,237,"sun");

circle(320+60*sin(a),240-35*cos(a),8);
setfillstyle(1,2);
pieslice(320+60*sin(a),240-35*cos(a),0,360,8);
circle(320+100*sin(b),240-60*cos(b),12);
setfillstyle(1,1);
pieslice(320+100*sin(b),240-60*cos(b),0,360,12);
circle(320+130*sin(c),240-80*cos(c),10);
setfillstyle(1,5);
pieslice(320+130*sin(c),240-80*cos(c),0,360,10);
circle(320+170*sin(d),240-100*cos(d),11);
setfillstyle(1,6);
pieslice(320+170*sin(d),240-100*cos(d),0,360,11);
circle(320+200*sin(e),240-130*cos(e),14);
setfillstyle(1,7);
pieslice(320+200*sin(e),240-130*cos(e),0,360,14);
circle(320+230*sin(f),240-155*cos(f),12);
setfillstyle(1,12);
pieslice(320+230*sin(f),240-155*cos(f),0,360,12);
circle(320+260*sin(g),240-180*cos(g),9);
setfillstyle(1,13);
pieslice(320+260*sin(g),240-180*cos(g),0,360,9);
circle(320+280*sin(h),240-200*cos(h),9);
setfillstyle(1,10);
pieslice(320+280*sin(h),240-200*cos(h),0,360,9);
circle(320+300*sin(z),240-220*cos(z),7);
setfillstyle(1,14);
pieslice(320+300*sin(z),240-220*cos(z),0,360,7);
delay(20);
i++;
j++;
k++;
l++;
m++;
n++;
o++;
p++;
q+=2;