7

I am working on the solar system and I am trying to get the sun to be the central light source of this program but it's not working the way I thought it would.

Here is a picture of what I have without lighting. enter image description here

Here is the same program with lighting.

enter image description here

A different angle here so you can see that the Earth has no shadow as it is supposed to (ignore the red on the moon, that's for my reference)

enter image description here

I don't know if you can tell, but it looks like the light is centered in each sphere, and not in the Sun. The shadow on the Earth is as if the light was coming from the top. Same with the Sun. The Sun here is not a light source, it's just a sphere that is also being lit by some some source. There is no shadow from the Earth on the moon or from the moon on the Earth.

This here is the code that draws the system

GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat pos[] = { 0.0, 0.0, 1.0, 0.0 };
glEnable(GL_LIGHTING);  
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightf(GL_LIGHT0, GL_POSITION, pos);
//SUN
        //Picture location, major radius, minor radius, major orbit, minor orbit, angle
Planet Sun ("/home/rodrtu/Desktop/SolarSystem/images/Sun.png", 
             100, 99, 200.0, 0.0, 0.0);
double sunOrbS = 0;
double sunRotS = rotatSpeed/10;
//orbit speed, rotation speed, moon reference coordinates (Parent planet's major and minor Axis)
Sun.displayPlanet(sunOrbS, sunRotS, 0.0, 0.0);

//EARTH
Planet Earth ("/home/rodrtu/Desktop/SolarSystem/images/EarthTopography.png", 
           50, 49, 500.0, 450.0, 23.5);
double eaOrbS = orbitSpeed*2;
double eaRotS = rotatSpeed*5;

Earth.displayPlanet(eaOrbS, eaRotS, 0.0, 0.0);
//Orbit path
drawCircle(800, 720, 1, 50);

//EARTH'S MOON
Planet Moon ("/home/rodrtu/Desktop/SolarSystem/images/moonTest.png", 
           25, 23, 100.0, 100.0, 15);
double moOrbS = rotatSpeed*4;
double moRotS = eaOrbS;

Moon.displayPlanet(moOrbS, moRotS, Earth.getMajorAxis(), Earth.getMinorAxis());

orbitSpeed+=.9;
if (orbitSpeed > 359.0)
 orbitSpeed = 0.0;

rotatSpeed+=2.0;
if (rotatSpeed > 719.0)
 rotatSpeed = 0.0;

These next two functions are responsible for coordinates and drawing the spheres

void Planet::setOrbit(double orbitSpeed, double rotationSpeed, 
              double moonOrbitX, double moonOrbitY) 
{
    majorAxis = orbitSemiMajor * cos(orbitSpeed / 180.0 * Math::Constants<double>::pi);
    minorAxis = orbitSemiMinor * sin(orbitSpeed / 180.0 * Math::Constants<double>::pi);

    glTranslate(majorAxis+moonOrbitX, minorAxis+moonOrbitY, 0.0);
    glRotatef(orbitAngle, 0.0, 1.0, 1.0);
    glRotatef(rotationSpeed, 0.0, 0.0, 1.0);

}

void Planet::displayPlanet(double orbitSpeed,double rotationSpeed, 
               double moonOrbitX, double moonOrbitY)
{
    GLuint surf;
    Images::RGBImage surfaceImage;
    surfaceImage=Images::readImageFile(texture);
    glEnable(GL_TEXTURE_2D);
    glGenTextures(0, &surf);
    glBindTexture(GL_TEXTURE_2D, surf);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    surfaceImage.glTexImage2D(GL_TEXTURE_2D,0,GL_RGB);

    glPushMatrix();
    setOrbit(orbitSpeed,rotationSpeed, moonOrbitX, moonOrbitY);
    drawSolidPlanet(equatRadius, polarRadius, 1, 40, 40); 
    glPopMatrix();

}

If I'm way off on what I am doing could you point me to a good tutorial? I have read a few but I guess I didn't understand them as I thought I did. If I'm on track, could you show me where I'm wrong and what I need to do right?

TRod
  • 295
  • 2
  • 6
  • 18
  • This looks fine to me, what exactly are you trying to achieve? I may not have understood correctly. – Jesus Ramos May 23 '13 at 20:45
  • I'll put another picture here... The Earth's light is not coming from the Sun. – TRod May 23 '13 at 20:46
  • Oh, you mean a shadow from the moon on the Earth. It was hard to tell from the picture. – Jesus Ramos May 23 '13 at 20:49
  • 1
    the shadow on the Earth is as if the light was coming from the top. Same with the Sun. The Sun here is not a light source, it's just a sphere that is also being lit by some some source. There is no shadow from the Earth on the moon or from the moon on the Earth. – TRod May 23 '13 at 20:58
  • You need to set the position of the light source, check my answer for how to do that. It's placing the light at the default position I believe so it's illuminating everything. – Jesus Ramos May 23 '13 at 20:59

1 Answers1

3

You need to call glLightfv(GL_LIGHT0, GL_POSITION, pos); and set the position of the light source to the center of the sun.

Jesus Ramos
  • 22,120
  • 9
  • 53
  • 82
  • 1
    what kind of variable is `pos`? I keep getting an error when I do this. Error below: In member function ‘virtual void SolarSystem::display(GLContextData&) const’: solarSystem.cpp:31:40: error: cannot convert ‘float’ to ‘const GLfloat* {aka const float*}’ for argument ‘3’ to ‘void glLightfv(GLenum, GLenum, const GLfloat*)’ make: *** [o/g++-3.g0.O3/solarSystem.o] Error 1 – TRod May 23 '13 at 21:01
  • 2
    The W component of the position is also important to mention though. If the w component of the position is 0, the light is treated as a directional source. – Grimmy May 23 '13 at 21:01
  • I thought that that's what I was doing with `GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };` – TRod May 23 '13 at 21:09
  • @TiagoRodrigues That just sets the RGBA values for the diffuse light, you need to set the position of the light. – Jesus Ramos May 23 '13 at 21:10
  • This helped, but I get no shadow from the moon on the earth or from the earth on the moon. Can I add a new picture and modify my question here or should I add a new question? I haven't been using stackoverflow for very long – TRod May 23 '13 at 21:20
  • @TiagoRodrigues as grimmy said did u set the correct W component on the light? – Jesus Ramos May 23 '13 at 21:27
  • I'm reading up on it because I don't really understand what this W component is. – TRod May 23 '13 at 21:36
  • @TiagoRodrigues Yeah reading up on this is the best thing to do. Learn some more in the process. If you still have troubles you can post a new question since this one already has quite a bit of content. – Jesus Ramos May 23 '13 at 21:37
  • The w component is the 4th value of a 4 dim vector. X, Y, Z, W. You'll get to know this friend a lot more as you keep learning :) – Grimmy May 23 '13 at 22:15