# help with solar system

i have managed to rotate my planets aorund the sun but wat i want to do is rotate the planet about its on axis any help from people out there?

RedBook example 3.6 planet.c explains transformation hierarchies.
Add another rotation which only affects the planet and put all other transfomrations around that inner.

Here is a simple example, we use push/pop matrix to store our past matrix:

// Start Solar system
glPushMatrix();
glTranslate(…) // location of the center of the system
glRotate(…) // Rotation of the system
// Draw Sun
glPushMatrix(); // Save matrix so spinning sun on axis will not spin other planets
glRotate(…) // Spin sun on axis
DrawSun();
glPopMatrix() // Restore matrix to before sun was rotated.

// repeat next for each planets
glPushMatrix();
glRotate(…); // Orbit location
glTranslate(…); // Distance from center of system(Sun)
glRotate(…); // spin planet on axis
DrawPlanet();

// if planet has a moon, it here and repeat code for each moon
glPushMatrix();
glRotate(); // Moon’s orbit
glTranslate(); // Moon distance from planet
glRotate(); // Moon’s spin on axis
DrawMoon();
glPopMatrix();
// end moon

glPopMatrix();
// end planet

glPopMatrix();
//end solar system

I am working on the same thing. I was wondering if someone could explain in more detail about the rotations (feel free to refer to Ex. 3-6)? I am a little confused since I am using GLUT not aux as the example uses.

Thank you.

if anyone would like the executable, i can send someone the solar system executable i did, all the planets and the moon for the earth. its on linux but could easily port to windows with just a few changes. thanks!

Does it make sense to have a push/pop within a for loop that loops through a data stucture containing planets, etc.?

For example (some pseudocode):

for (…) //loop through data struct
{
glPushMatrix()

if planet1 found in data stuct
then:
glPushMatrix()
draw_planet1
glPopMatrix()

if planet2 found in data struct
then:
glPushMatrix()
draw_planet2
glPopMatrix()

glPopMatrix()

} // end for loop

I am trying to follow the logic posted by Nexusone (at least to some extent) however, I am having a difficult time with putting the constructs of looping through a data struct together with what is suggested.

Appreciate any feedback. Thanks.

Maybe you need to look at a diffrent aproach to how you load your planets.

You need to create a structure in which to load you planet data.

typedef struct PLANETS
{
int x, y, z; // Location of planet
int rx, ry, rz; // Rotation of Planet
int ox, oy, oz; // Orbit of planet
int size;
};

PLANETS Planet; // Create space to store data on 10 planets.

// Draw planets in Display routine

for(i = 0; i < Max_planets; i++)
{

``````DrawPlanet( i ); // Where "i" tell's what planet to draw
``````

}

// End planet draw

void DrawPlanet( int p )
{

glPushMatrix();
glTranslatef(Planet[p].x, Planet[p].y, Planet[p].z); // Location of planet, sun would be 0,0,0
glRotatef(Planet[p].rx, 1, 0, 0); Rotate planet
glRotatef(Planet[p].ry, 0, 1, 0);
glRotatef(Planet[p].rz, 0, 0, 1);
glTranslatef(Planet[p].ox, 0, 0); // Set orbit from center of system
// here set planet texture to apply to sphere
Draw_sphere(Planet[p].size);

glPopMatrix();
// End planet routine
}

Reading data structure should be done before drawing.

int get_planets( void ) // We return a int to tell if we did load planet data.
{

OpenFile(…);
do {

Max_planets++; //Global variable tracks how many planets where loaded
while( !EOF )

return (error)
}

I wanted to add that really did not take moom’s into acount on that.

typedef struct PLANETS
{
int x, y, z; // Location of planet
int rx, ry, rz; // Rotation of Planet
int ox, oy, oz; // Orbit of planet
int size;
int moons; // How many moons to draw for this planet.
};

void DrawPlanet( int p )
{

glPushMatrix();
glTranslatef(Planet[p].x, Planet[p].y, Planet[p].z); // Location of planet, sun would be 0,0,0
glRotatef(Planet[p].rx, 1, 0, 0); Rotate planet
glRotatef(Planet[p].ry, 0, 1, 0);
glRotatef(Planet[p].rz, 0, 0, 1);
glTranslatef(Planet[p].ox, 0, 0); // Set orbit from center of system
// here set planet texture to apply to sphere
Draw_sphere(Planet[p].size);

for (t=0; t < Planet[p].moons; t++)
{
Draw_Moons( t );
}

glPopMatrix();
// End planet routine
}

Note code is not complete just an example

Figured this out - didn’t do exactly as suggested due to not having time to redesign the data structure but works ok.

Thanks all.