Reputation: 897
I'm trying to make the solar system in OpenGL. Everytime I add a planet I need to pop the transformationmatrix so I can start over with the next one. This is working for only 2 planets. I am able to add a first planet (earth) and let is spin around the sun. Then I use glPopMatrix() and add a second planet spinning around the sun, this again works fine. But when I try to add a 3rd planet and do the exact same thing (pop the stack first and make it spin around the sun), it looks like the transformationMatrix isn't being reseted and the 3rd planet spins around the 2nd one like the 1st and 2nd planet spin around the sun.
Here is the code of my paintGL():
void PlanetsView::paintGL ()
{
this->dayOfYear = (this->dayOfYear+1);
this->hourOfDay = (this->hourOfDay+1) % 24;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
// store current matrix
glMatrixMode( GL_MODELVIEW );
gluLookAt(camPosx ,camPosy ,camPosz,
camViewx,camViewy,camViewz,
camUpx, camUpy, camUpz );
//Draw Axes
glDisable( GL_LIGHTING );
glBegin(GL_LINES);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(10.0, 0.0, 0.0);
glColor3f(0.0, 1.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 10.0, 0.0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 10.0);
glEnd();
glEnable( GL_LIGHTING );
glPushMatrix();
// rotate the plane of the elliptic
glRotated ( 5.0, 1.0, 0.0, 0.0 );
// draw the sun
GLfloat diff [] = { 0.7f , 0.5f , 0.0f };
glMaterialfv ( GL_FRONT, GL_DIFFUSE, diff );
//glutSolidSphere( 3.0, 25, 25 );
solidSphere(3.0, 25, 25);
// rotate the earth around the sun
glRotated( (GLdouble)(360.0 * dayOfYear /365.0), 0.0, 1.0, 0.0 );
glTranslated ( 4.0, 0.0, 0.0 );
// rotate the earth around its axis
glRotated( (GLdouble)(360.0 * hourOfDay/24.0), 0.0, 1.0, 0.0 );
// draw the earth
GLfloat diff2 [] = { 0.2f , 0.2f , 0.8f };
glMaterialfv ( GL_FRONT, GL_DIFFUSE, diff2 );
solidSphere(0.3, 25, 25);
glPopMatrix();
// rotate the new planet around the sun
glRotated( (GLdouble)(360.0 * dayOfYear /150.0), 0.0, 1.0, 0.0 );
glTranslated ( 6.0, 0.0, 0.0 );
// rotate the new planet around its axis
glRotated( (GLdouble)(360.0 * hourOfDay/36.0), 0.0, 1.0, 0.0 );
// draw the new planet
GLfloat diff3 [] = { 1.0f , 0.0f , 0.0f }; // red color
glMaterialfv ( GL_FRONT, GL_DIFFUSE, diff3 );
solidSphere(0.4, 25, 25);
glPopMatrix(); // looks like this pop doesn't do anything
/* From here, when adding a 3rd planet, it fails */
// rotate a 3rd planet around the sun
glRotated( (GLdouble)(360.0 * dayOfYear /800.0), 0.0, 1.0, 0.0 );
glTranslated ( 6.0, 0.0, 0.0 );
// rotate a 3rd planet around its axis
glRotated( (GLdouble)(360.0 * hourOfDay/12.0), 0.0, 1.0, 0.0 );
// draw the 3rd planet
GLfloat diff4 [] = { 1.0f , 1.0f , 0.0f }; // red color
glMaterialfv ( GL_FRONT, GL_DIFFUSE, diff4 );
solidSphere(0.3, 25, 25);
glPopMatrix();
}
And here is the code of solidSphere:
void solidSphere(GLdouble radius, GLint slices, GLint stacks)
{
glBegin(GL_LINE_LOOP);
GLUquadricObj* quadric = gluNewQuadric();
gluQuadricDrawStyle(quadric, GLU_FILL);
gluSphere(quadric, radius, slices, stacks);
gluDeleteQuadric(quadric);
glEnd();
}
Upvotes: 0
Views: 244
Reputation: 43319
glPopMatrix(); // looks like this pop doesn't do anything
You already popped the last thing off the stack a few lines prior to that.
If your intention was to have the second call to glPopMatrix ()
restore the matrix as it existed after calling glEnable( GL_LIGHTING )
, then you need to add a call to glPushMatrix ()
immediately after your first glPopMatrix ()
. And you need to repeat that process one more time so that your third call to glPopMatrix ()
does not also underflow the stack.
I get the impression you are not quite clear how stacks work.
Upvotes: 1