Hi everyone. =)
I am trying to create a solar system in OpenGL, and have some trouble with rotation.
I have managed to make planets rotate around the sun, by using
glRotatef(v, -0.1 , 1 , 0.2);
right before I create the sun and all the planet, and placing the sun with the center at the point 0,0,0. However, what I also want to do is to make it possible for moons to rotate around the rotating planets, and making the planets rotate around their own axis (like they do in reality when switching between day and night - one side isn´t always facing the sun).
This is the code for how I did to create the solar system:
//creates a planet, and binds the texture if tex isn´t higher than the texture index.
void createPlanet(GLfloat k, int x, int y, int tex) {
if (tex < MAX_NO_TEXTURES) { glBindTexture(GL_TEXTURE_2D, texture_id[tex]);};
glutSolidSphere(k, x, y);
}
//Draws and rotates the solar system.
void createSolarsystem()
{
glRotatef(v, -0.1 , 1 , 0.2);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
createPlanet(2.5, 12, 12, 0); //creates the sun (a bit bigger than the planets, texture 0).
glTranslatef(0.0, 0.0, 6.0);
createPlanet(1.0, 10, 10, 1); //creates the earth, texture 1, 40% of the sun´s size.)
glTranslatef(0.0, 0.0, -11.0);
createPlanet(1.0, 12, 12 , 2); //creates mars, texture 2, the same size as the earth).
}
//display function
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angleX,1.0,0.0,0.0);
glRotatef(angleY,0.0,1.0,0.0);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
createSolarsystem(); //creates the solar system
glPopMatrix();
glutSwapBuffers();
}
If any of you have any ideas about how I could do to rotate planets surface and moons, that would be great. Thanks. =)