PDA

View Full Version : Solar System



maui059
03-15-2001, 08:52 PM
I am trying to draw the solar system. However I have a problem in that every time I add a new planet it revoles around the previous one. Here is a piece of code with the sun, earth and moon. Can someone tell me how to fix this?

// draw the sun
glColor3fv(Color[YELLOW]);
glutSolidSphere(0.7, 20, 16);

// draw the planets


glRotatef((GLfloat) year, 0.0f, 1.0f, 0.222f);
glTranslatef(2.0f, 0.0f, 0.0f);
glRotatef((GLfloat) day, 0.0f, 1.0f, 0.222f);
glColor3fv(Color[GREEN]);
glutSolidSphere(0.2, 10, 8); // earth?

//draw the moon
glRotatef((GLfloat) year, 0.0f, 1.0f, 0.0f);
glTranslatef(0.5f, 0.0f, 0.0f);
glRotatef((GLfloat) day, 0.0f, 1.0f, 0.0f);
glColor3fv(Color[WHITE]);
glutSolidSphere(0.1,6,4);

BwB
03-15-2001, 09:13 PM
I did something similar when I first started with OpenGL. The problem resides in that you are building the current position/orientation of each planetary body on top of the previous one. You want to build pos/orient of each planetary body based on it's parent (such as each planet's parent is the sun, and each moon's parent is the planet that it revolves around). This is alot easier than it sounds http://www.opengl.org/discussion_boards/ubb/smile.gif glPushMatrix() and glPopMatrix() allow you to save and restore the state of your position/orientation matrix. See the example below.





// draw the sun
glColor3fv(Color[YELLOW]);
glutSolidSphere(0.7, 20, 16);

// draw the planets

for each plaent
{
// save the matrix (so that the sun is the parent)
glPushMatrix();

glRotatef((GLfloat) year, 0.0f, 1.0f, 0.222f);
glTranslatef(2.0f, 0.0f, 0.0f);
glRotatef((GLfloat) day, 0.0f, 1.0f, 0.222f);
glColor3fv(Color[GREEN]);
glutSolidSphere(0.2, 10, 8); // earth?

for each moon
{
//draw the moon
// save the matrix (so that the planet is the parent)
glPushMatrix();
glRotatef((GLfloat) year, 0.0f, 1.0f, 0.0f);
glTranslatef(0.5f, 0.0f, 0.0f);
glRotatef((GLfloat) day, 0.0f, 1.0f, 0.0f);
glColor3fv(Color[WHITE]);
glutSolidSphere(0.1,6,4);
glPopMatrix();
} // end for each moon

glPopMatrix();
} // end for each planet


Hope that helps.

maui059
03-15-2001, 09:17 PM
That helps so much. Thanks a billion!

maui059
03-15-2001, 09:24 PM
Ok, I now have a new question. I am drawing the planets with glutSolidSphere(); obviously. and I am using:
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glEnable(GL_DEPTH_TEST);
glClear(GL_DEPTH_BUFFER_BIT);

I want partial line hiding. But its not working. Any ideas?

BwB
03-15-2001, 11:55 PM
What do you mean by partial line hiding? If you are using solid spheres you shouldnt have lines at all.