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?
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 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();
//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
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?