Results 1 to 7 of 7

Thread: Problems with glPushMatrix and glPopMatrix()

  1. #1
    Junior Member Newbie
    Join Date
    May 2008
    Posts
    7

    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:
    Code :
    #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(&amp;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.

  2. #2
    Senior Member Frequent Contributor
    Join Date
    Apr 2004
    Posts
    990

    Re: Problems with glPushMatrix and glPopMatrix()

    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.

  3. #3
    Junior Member Newbie
    Join Date
    May 2008
    Posts
    7

    Re: Problems with glPushMatrix and glPopMatrix()

    uhm.. But in red book It's ok.
    Code :
    #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(&amp;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;
    }

  4. #4
    Senior Member Frequent Contributor
    Join Date
    Apr 2004
    Posts
    990

    Re: Problems with glPushMatrix and glPopMatrix()

    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.

  5. #5
    Junior Member Newbie
    Join Date
    May 2008
    Posts
    7

    Re: Problems with glPushMatrix and glPopMatrix()

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

  6. #6
    Super Moderator OpenGL Guru imported_dorbie's Avatar
    Join Date
    Jul 2000
    Location
    Bay Area, CA, USA
    Posts
    3,966

    Re: Problems with glPushMatrix and glPopMatrix()

    Quote Originally Posted by Ngoc Sang
    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.

  7. #7
    Junior Member Newbie
    Join Date
    May 2008
    Posts
    7

    Re: Problems with glPushMatrix and glPopMatrix()

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

Similar Threads

  1. How to use glPushMatrix and glPopMatrix?
    By Structural in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 11-22-2002, 05:43 AM
  2. glPopMatrix, glPushMatrix??
    By sar22 in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 03-24-2002, 02:10 AM
  3. glPushMatrix and glPopMatrix?
    By newmannium in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 03-01-2001, 12:02 PM
  4. glPushMatrix() and glPopMatrix
    By zzapatista in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 02-19-2001, 02:45 AM
  5. glPushMatrix() & glPopMatrix()
    By glbeginner in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 01-07-2001, 10:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean