3D solar system

When we begin with openGL, we always try to create the solar system. Why? Because the simulation of the Solar system is possible with basic openGL functions, ie: glRotate() and glTranslate(). So, I explain in this article how use this 2 functions to make the Solar system. I want to specify I won’t demonstrate how openGL transformations work in this article. If you need help go to: OpenGL transformations and translations .

Technical stack

openGL + SDL + C++ with codeBlock

Step1: some planets and a star

Damien Leroux Solar System explication1

Solar system means severals spheres turning around one unique static sphere: Sun! In order to create planet (ie a sphere), information are required: planet rotation speed, planet orbital rotation speed, distance from Sun, sphere’s radius, the tilt regarding the sun and the tilt regarding the planet. We use the planet’s radius to create a sphere with the right size (thanks to glutSolidSphere() or gluSphere()) in the center of the openGL world. We move it with glTranslatef() on an axis (x for example), with the right distance from the Sun ( don’t forget to add the Sun’s radius to the distance). We had to repeat this task for each planet.

It is important to remember that in openGL, all modifications on the MODELVIEW matrix (glTranslate, glRotate, glScale) are accomplished in reverse order of the program writing. We use glPushMatrix() before every movements and glPopMatrix() after saving the MODELVIEW matrix state. We had to bear it in mind for each planet and it movements. Without glPushMatrix() and glPopMatrix(), each rotation or translation will affect every following shapes.

Damien Leroux Solar System explication2

Once every planet are displayed, we put a last sphere in the center of the openGL world to simulate the Sun. To create satellites (the moon for example), we had to create the satellite, then to move it, to finish with the planet creation. To create a ring around Saturne, I used a quadric. You can find the differents existing quadrics at: openGL quadric

Step 2: the planets rotate

Damien Leroux Solar System explication 5

To make planets turn on itself, we add just before the drawing of the shape: glRotatef(). To have this planet turn around the Sun, we use before glTranslate(): glrotate(). Two other rotations are used to change inclines.

Step 3: Texture

Damien Leroux Solar System explication3

To use texture with sphere, it is better to use gluSphere() than glutSolidSphere() because texture doesn’t work properly with glutSolidSphere(). Texture is not fixed with glutSolidSphere() so the sphere turns (for example) but the texture doesn’t. To load a texture, I used this OpenGL textures frensh tutorial . It is easy to find textures for each planet on the Internet, then to modify them with Photoshop to improve quality or details.

Step 4: Light

Damien Leroux Solar System explication4

It is really easy to use it: first, we had to enable light: glEnable(GL_LIGHTING) and glEnable(GL_LIGHT1) then to direct the light thanks to glLightiv(). To learn more, go to this OpenGL light tutorial.

Example to show how planet is create:

void draw_planet(.){
    //texture mode activated
    glEnable(GL_TEXTURE_2D);
    //light mode activated
    glEnable(GL_LIGHTING);

    //save MODELVIEW matrix state
    glPushMatrix();

    //the orbital incline
    glRotatef(inclinaison_orbital,1,0,0);

    //rotation around the Sun
    glRotatef(angle_rotation_planete_soleil,0,1,0);

    //translates on an axis
    glTranslatef(-distance_planete_soleil,0,0);
    //activates light 1
    glEnable(GL_LIGHT1);
    //position the light 1
    glLightiv(GL_LIGHT1,GL_POSITION,lumiere_position1);
    //uses the texture
    glBindTexture(GL_TEXTURE_2D,text_planete);
    // the Sun incline
    glRotatef(inclinaison_planete_soleil,0,0,1);

    //rotates on itself
    glRotatef(angle_rotation_planete_planete,0,1,0);

    //draw sphere
    gluSphere(params,rayon_planete,QUALITE_RENDU,QUALITE_RENDU);

    glDisable(GL_LIGHT1);
    glPopMatrix();

    //switch off light mode
    glDisable(GL_LIGHTING);
    //switch off texture mode
    glDisable(GL_TEXTURE_2D);
}

Step 5 : trackball

To see what we want in the Solar system, we use a trackball ( like google Earth). I used this frensh trackball tutorial to create a trackball. I modified this one to turn easier in space.

Step 6 : skybox

Damien Leroux Solar System explication 6

To simulate the space (with stars) behind the Solar system, I chose a skybox: a simple way to create a background in openGL. I used this skybox tutorial to learn to use it. I had to change the position of this skybox because of my trackball. I modified the load of picture to replace the bitmap loading by a jpeg loading. I created each picture for this skybox with Photoshop.

Step 7: finishes

Damien Leroux Solar System explication 7

To display the planet names, I drew a quad where I fixed a texture with the name. I draw this one close to the corresponding planet. I used the camera angles to incline the quad to the camera.

Damien Leroux Solar System 2

To display trajectories, I drew circles. These circles are composed of several lines which make a whole turn: 360 degrees.

glBegin(GL_LINE_LOOP);
for(float k=0;k<(M_PI*2);k+=M_PI/180){
    x=sin(k)*rayon;
    z=cos(k)*rayon;
    glVertex3f(x,0,z);
}
glEnd();

To obtain an elliptical circle, we just had to multiply x or y by a value.

Final Result

Damien Leroux Solar System rendu1

Damien Leroux Solar System rendu2

Damien Leroux Solar System rendu3

Damien Leroux Solar System rendu4