glPushMatrix() & glPopMatrix()

Dear All,

I am sorry to bother with such primitive question, but really I spent long enough time of reading books & web and I got enough. Probably I am missing some information, or I simply do not understand a point. I tried it on 2 absolutely different machines and it was not working according to my expectations anywhere, so I know the problem is simply in my code :slight_smile: .

  glLoadIdentity();		
  
  glTranslatef(0.0f,0.0f,-6.0f);	
  glRotatef(rtri,0.0f,0.0f,1.0f);	
  glBegin(GL_LINES);				
  glColor3f(1.0f,0.0f,0.0f);			
  glVertex3f(0.0f, 1.0f, 0.0f);		        
  glColor3f(0.0f,1.0f,0.0f);				
  glVertex3f(0.0f,0.0f, 0.0f);				
  glEnd();

			
  glTranslatef(0.0f,1.0f,0.0f);		
  glRotatef(rquad,0.0f,0.0f,1.0f);
  glBegin(GL_LINES);				
  glColor3f(1.0f,0.0f,0.0f);			
  glVertex3f(0.0f, 1.0f, 0.0f);		        
  glColor3f(0.0f,1.0f,0.0f);				
  glVertex3f(0.0f,0.0f, 0.0f);				
  glEnd();					
  
  glPushMatrix();		//FIRST PUSH
  
    	glTranslatef(0.0f,1.0f,0.0f);		
    	glRotatef(rquad,0.0f,0.0f,1.0f);
    	glBegin(GL_LINES);				
    	glColor3f(1.0f,0.0f,0.0f);			
    	glVertex3f(0.0f, 1.0f, 0.0f);		        
    	glColor3f(0.0f,1.0f,0.0f);				
    	glVertex3f(0.0f,0.0f, 0.0f);				
    	glEnd();		
  			
    	glTranslatef(0.0f,1.0f,0.0f);		
    	glRotatef(rquad,0.0f,0.0f,1.0f);
    	glBegin(GL_LINES);				
    	glColor3f(1.0f,0.0f,0.0f);			
    	glVertex3f(0.0f, 1.0f, 0.0f);		        
    	glColor3f(0.0f,1.0f,0.0f);				
    	glVertex3f(0.0f,0.0f, 0.0f);				
    	glEnd();	
				
    	glPushMatrix();		//SECOND PUSH
    
      		glTranslatef(0.0f,1.0f,0.0f);		
      		glRotatef(rquad,0.0f,0.0f,1.0f);
      		glBegin(GL_LINES);				
      		glColor3f(1.0f,0.0f,0.0f);			
      		glVertex3f(0.0f, 1.0f, 0.0f);		        
      		glColor3f(0.0f,1.0f,0.0f);				
      		glVertex3f(0.0f,0.0f, 0.0f);				
      		glEnd();
  					
 
      		glPushMatrix();		//THIRD PUSH
        
         		glTranslatef(0.0f,1.0f,0.0f);		
  	 		glRotatef(rquad,0.0f,0.0f,1.0f);
  	 		glBegin(GL_LINES);				
  	 		glColor3f(1.0f,0.0f,0.0f);			
	 		glVertex3f(0.0f, 1.0f, 0.0f);		        
 	 		glColor3f(0.0f,1.0f,0.0f);				
	 		glVertex3f(0.0f,0.0f, 0.0f);				
	 		glEnd();					
      
 		glPopMatrix();
		
  		glColor3f(1.0f,1.0f,0.5f);
  		glutWireSphere(0.2,6,8);
	
  rtri+=0.1f;					
  rquad-=0.1f;					

glutSwapBuffers();

Well, what I expected from code to happen is, that when I call glPopMatrix() at the end, then Shere will be drawn at the translation point of Third glPushMatrix().

And If I call it twice then the position of second PushMatrix would be recalled and finaly the first position…

But it never happened. Whenever I call PopMatrix it recalls the first PushMatrix position. any further PopMatrix do nothing then.

Well, I thought that probably my GL_MODELVIEW has only two stack positions instead of 32, but I think this is not likely to be truth. hmm? This should not happen.

Actually I do know nothing about it :slight_smile: . I just, why other PushMatrix methods are ignored by PopMatrix?

Thank you very much for any help. I am really stucked. Maybe I got the idea about Push&Pop wrong.

Hi innermostsanctum,

I’m not an expert, but I have feeling the number of Push and Pop matrixes must match in each frame, or it will have … terrible consequences :slight_smile: .

So try to glPopMatrix twice before swapping the buffers.

Hope it helps.

glPetr

glPetr, thank you, that was it

it has some logic in it after all, and i really did not realize this :slight_smile:

thank you a lot