Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 9 of 9

Thread: help with solar system

  1. #1
    Guest

    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?

  2. #2
    Senior Member OpenGL Guru Relic's Avatar
    Join Date
    Apr 2000
    Posts
    2,527

    Re: help with solar system

    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.

  3. #3
    Senior Member OpenGL Guru
    Join Date
    Jun 2000
    Location
    Gastonia, NC, USA
    Posts
    2,096

    Re: help with solar system

    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

  4. #4
    Junior Member Regular Contributor
    Join Date
    Mar 2003
    Location
    Chicago, IL US
    Posts
    100

    Re: help with 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.

  5. #5
    Intern Contributor
    Join Date
    Oct 2003
    Posts
    99

    Re: help with solar system

    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!

  6. #6
    Junior Member Regular Contributor
    Join Date
    Mar 2003
    Location
    Chicago, IL US
    Posts
    100

    Re: help with solar system

    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.

  7. #7
    Senior Member OpenGL Guru
    Join Date
    Jun 2000
    Location
    Gastonia, NC, USA
    Posts
    2,096

    Re: help with solar system

    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[10]; // 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 {

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

    return (error)
    }

  8. #8
    Senior Member OpenGL Guru
    Join Date
    Jun 2000
    Location
    Gastonia, NC, USA
    Posts
    2,096

    Re: help with solar system

    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);

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

    glPopMatrix();
    // End planet routine
    }


    Note code is not complete just an example

  9. #9
    Junior Member Regular Contributor
    Join Date
    Mar 2003
    Location
    Chicago, IL US
    Posts
    100

    Re: help with solar system

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

    Thanks all.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •