The sun as a light source using opengl and C ++

I am working on a solar system and I am trying to make the sun be the central light source of this program, but it does not work the way I thought.

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

Here is the same program with backlight.

enter image description here

The different angle is here so you can see that the Earth does not have a shadow, as it should (ignore the red on the moon, which is for my reference)

enter image description here

I don’t know if you can tell, but it seems that the light is concentrated in every sphere, and not on the Sun. Shadow on Earth, as if light was coming from the top. Same thing with the sun. The sun here is not a source of light, it is just a sphere that is also illuminated by some source. There is no shadow from the Earth on the Moon or from the Moon on the Earth.

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 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 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 the coordinates and draw 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 get away from what I'm doing, can you tell me a good tutorial? I read a few, but I think I did not understand them, as I thought I did. If I am on the way, could you show me where I am wrong and what I need to do right?

+7
source share
1 answer

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

+3
source

All Articles