Problems with glPushMatrix and glPopMatrix()

I begin to learn Opengl. I creat a Object and rotate it. But it has problem when I use glPushMatrix and glPopMatrix. This is my source:


#include <GL/glut.h>
#include <stdlib.h>
#include <iostream.h>
void Init();
void Display();
void Reshape(int Width,int Height);
void Keyboard(unsigned char Key,int X,int Y);
void Mouse(int button,int state,int x,int y);
void Time(int value);

int radian=10;
void main(int Argc,char** Argv)
{
	glutInit(&Argc,Argv);
	glutInitDisplayMode(GLUT_DOUBLE| GLUT_RGB| GLUT_DEPTH);
	glutInitWindowSize(500,500);
	glutInitWindowPosition(200,100);
	glutCreateWindow("Solar System Viewer .");
	Init();
	
	glutDisplayFunc(Display);
	glutReshapeFunc(Reshape);
	glutKeyboardFunc(Keyboard);
	glutMouseFunc(Mouse);
	glutTimerFunc(100,Time,radian);
	glCullFace(GL_BACK);
	glutMainLoop();
	
	
}
 
void Init()
{
	glClearColor(0.0,0.0,0.0,0.0);
}
void Display()
{
	glClearColor(0.0,0.0,0.0,0.0);
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_CULL_FACE);
	
	
	glPushMatrix();
	glRotated(10,0.0,0.0,1.0);
	glutWireSphere(GLdouble (0.3),25,25);
	glPopMatrix();

	glDisable(GL_DEPTH_TEST);
	glDisable(GL_CULL_FACE);
	glutSwapBuffers();
}
void Reshape(int w,int h)
{

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glViewport(0,0,w,h);
	gluPerspective(30.0,1.0,1.0,10.0);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(3.0,4.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0);


}
/* key event */
void Keyboard(unsigned char Key,int X,int Y)
{
	
}
/* mouse event */
void Mouse(int button,int state,int x,int y)
{

}
	
void Time(int value)
{
	Display();
	glutTimerFunc(50,Time,value);
		
}

 

if i remove glPushMatrix and glPopMatrix. it’s move But i use it, it stop.

// sorry for my poor english. Talk to me if u don’t understand what i said.

If you push the matrix, it makes a copy of the current matrix and puts it on the top of the stack. Then you call glRotate, so the one on top gets altered. After that you pop the matrix so that the original matrix gets restored.

If you don’t use push pop it will continuously alter the matrix on top of the stack without resetting so your object will move every time the display method is called.

uhm… But in red book It’s ok.


#include <GL/glut.h>
#include <stdlib.h>

static int year = 0, day = 0;

void init(void) 
{
   glClearColor (0.0, 0.0, 0.0, 0.0);
   //glShadeModel (GL_FLAT);
}

void display(void)
{
   glClear (GL_COLOR_BUFFER_BIT);
   glColor3f (1.0, 1.0, 1.0);

   glPushMatrix();
   glutWireSphere(1.0, 20, 16);   /* draw sun */
   glRotatef ((GLfloat) year, 0.0, 1.0, 0.0);
   glTranslatef (2.0, 0.0, 0.0);
   glRotatef ((GLfloat) day, 0.0, 1.0, 0.0);
   glutWireSphere(0.2, 10, 8);    /* draw smaller planet */
   glPopMatrix();
   glutSwapBuffers();
}

void reshape (int w, int h)
{
   glViewport (0, 0, (GLsizei) w, (GLsizei) h); 
   glMatrixMode (GL_PROJECTION);
   glLoadIdentity ();
   gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
   gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}

void keyboard (unsigned char key, int x, int y)
{
   switch (key) {
      case 'd':
         day = (day + 10) % 360;
         glutPostRedisplay();
         break;
      case 'D':
         day = (day - 10) % 360;
         glutPostRedisplay();
         break;
      case 'y':
         year = (year + 5) % 360;
         glutPostRedisplay();
         break;
      case 'Y':
         year = (year - 5) % 360;
         glutPostRedisplay();
         break;
      case 27:
         exit(0);
         break;
      default:
         break;
   }
}

int main(int argc, char** argv)
{
   glutInit(&argc, argv);
   glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
   glutInitWindowSize (500, 500); 
   glutInitWindowPosition (100, 100);
   glutCreateWindow (argv[0]);
   init ();
   glutDisplayFunc(display); 
   glutReshapeFunc(reshape);
   glutKeyboardFunc(keyboard);
   glutMainLoop();
   return 0;
}


Yes, that’s because they change the value of ‘year’ and ‘day’ if they want to draw it in a different position. In your case the value ‘10’ in glRotated is a constant.

ok. I see. Thank you again. I hope I will help u something in the future. Call me if u need my help.

The way you offer help back is contribute here on questions you know the answers to. Even if you don’t know any answers now, one day you will, and your replies will be appreciated.

OK! I promise. Hope to help any ones in future.