Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 4 of 4

Thread: Remembering coordinates (Push/Pop Matrix ?)

  1. #1
    Newbie Newbie
    Join Date
    Jan 2013
    Posts
    2

    Remembering coordinates (Push/Pop Matrix ?)

    Hello,
    This is my first post so I would like to say Hello for everyone.
    In school I'm programming in openlg and I have problem. That's my code:
    Code :
    int DrawGLScene(GLvoid)									
    {	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	
     
     
    x=0.0f;
     
    for (i = 0; i < 3; i++)
    {
     
    	if (keys['1'])	a = 1;
     
    	if (keys['2'])	a = 2;
     
     		if (a == 1)
    		{	
     
    		glLoadIdentity();
    		glTranslatef(0, 0.0f,-25.0f);
    		glRotatef(rtri,0.0f,1.0f,0.0f);
    	//	glPushMatrix();
    		glTranslatef(x, 0.0f,0.0f);
     
    		glBegin(GL_TRIANGLES);								
    		glColor3f(1.0f,0.0f,0.0f);						
    		glVertex3f( 0.0f, 1.0f, 0.0f);					 
    		glColor3f(0.0f,1.0f,0.0f);						 
    		glVertex3f(-1.0f,-1.0f, 1.0f);				 
    		glColor3f(0.0f,0.0f,1.0f);						 
    		glVertex3f( 1.0f,-1.0f, 1.0f);					 
    		glColor3f(1.0f,0.0f,0.0f);						 
    		glVertex3f( 0.0f, 1.0f, 0.0f);				 
    		glColor3f(0.0f,0.0f,1.0f);						 
    		glVertex3f( 1.0f,-1.0f, 1.0f);					 
    		glColor3f(0.0f,1.0f,0.0f);						 
    		glVertex3f( 1.0f,-1.0f, -1.0f);					 
    		glColor3f(1.0f,0.0f,0.0f);						 
    		glVertex3f( 0.0f, 1.0f, 0.0f);					 
    		glColor3f(0.0f,1.0f,0.0f);						 
    		glVertex3f( 1.0f,-1.0f, -1.0f);				  
    		glColor3f(0.0f,0.0f,1.0f);						 
    		glVertex3f(-1.0f,-1.0f, -1.0f);					 
    		glColor3f(1.0f,0.0f,0.0f);						 
    		glVertex3f( 0.0f, 1.0f, 0.0f);					 
    		glColor3f(0.0f,0.0f,1.0f);						 
    		glVertex3f(-1.0f,-1.0f,-1.0f);					 
    		glColor3f(0.0f,1.0f,0.0f);						 
    		glVertex3f(-1.0f,-1.0f, 1.0f);
    		glEnd();
     
    		if (x == 0) x = -3;
    		else if (x < 0 ) x = 3;
     
     
    		rtri+=0.05f;
     
    		}
    		if (a == 2) 
     	{	
     
    		glLoadIdentity();
    		glTranslatef(x, 0.0f,-25.0f);
    	//	glPopMatrix();
    		glRotatef(rtri,0.0f,1.0f,0.0f);
    		glBegin(GL_TRIANGLES);								
    		glColor3f(1.0f,0.0f,0.0f);						
    		glVertex3f( 0.0f, 1.0f, 0.0f);					 
    		glColor3f(0.0f,1.0f,0.0f);						 
    		glVertex3f(-1.0f,-1.0f, 1.0f);				 
    		glColor3f(0.0f,0.0f,1.0f);						 
    		glVertex3f( 1.0f,-1.0f, 1.0f);					 
    		glColor3f(1.0f,0.0f,0.0f);						 
    		glVertex3f( 0.0f, 1.0f, 0.0f);				 
    		glColor3f(0.0f,0.0f,1.0f);						 
    		glVertex3f( 1.0f,-1.0f, 1.0f);					 
    		glColor3f(0.0f,1.0f,0.0f);						 
    		glVertex3f( 1.0f,-1.0f, -1.0f);					 
    		glColor3f(1.0f,0.0f,0.0f);						 
    		glVertex3f( 0.0f, 1.0f, 0.0f);					 
    		glColor3f(0.0f,1.0f,0.0f);						 
    		glVertex3f( 1.0f,-1.0f, -1.0f);				  
    		glColor3f(0.0f,0.0f,1.0f);						 
    		glVertex3f(-1.0f,-1.0f, -1.0f);					 
    		glColor3f(1.0f,0.0f,0.0f);						 
    		glVertex3f( 0.0f, 1.0f, 0.0f);					 
    		glColor3f(0.0f,0.0f,1.0f);						 
    		glVertex3f(-1.0f,-1.0f,-1.0f);					 
    		glColor3f(0.0f,1.0f,0.0f);						 
    		glVertex3f(-1.0f,-1.0f, 1.0f);
     
    		glEnd();
     
    		if (x == 0) x = -3;
    		else if (x < 0 ) x = 3;
     
     
    		rtri+=0.05f;
     
    		}
    }
     
    	return TRUE;										
    }

    Key '1' nad '2' are changing mode and my problem is how to do, to remember coordinates ? When I put key '2' everything is back to position 0, 0, 0 (efects the same like "translatef (0.0f, 0.0f, 0.0f)". I tried to use glPushMatrix and glPopMatrix but it doesn't work (probably I used these functions wrong). Can help me and correct this code ? Is very very importand for me...
    Big Thanks.
    ps. Sorry for my english... :/

    Best Regards

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,105
    glPush and glPop save and restore the current select matrix (in you case you want the VIEWMODEL matrix). So you need to select the view model matrix with glMatrixMode(GL_VIEWMODEL) and call glPushMatrix; now do what ever you want to draw your objects; then call glPopMatrix() and you have the original matrix back.

  3. #3
    Newbie Newbie
    Join Date
    Jan 2013
    Posts
    2
    Thanks for replay.
    I understood, but I did something wrong because it isn't working. Can you correct this code, please ?
    Code :
    int DrawGLScene(GLvoid)									
    {	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	
     
     
    x=0.0f;
     
    for (i = 0; i < 3; i++)
    {
     
    	if (keys['1'])	a = 1;
     
    	if (keys['2'])	a = 2;
     
     		if (a == 1)
    		{	
     
    		glLoadIdentity();
    		glTranslatef(0, 0.0f,-25.0f);
    		glRotatef(rtri,0.0f,1.0f,0.0f);
     
    	 	glPushMatrix();
    		glTranslatef(x, 0.0f,0.0f);
    		glMatrixMode(GL_MODELVIEW);
    		glBegin(GL_TRIANGLES);								
    		glColor3f(1.0f,0.0f,0.0f);						
    		glVertex3f( 0.0f, 1.0f, 0.0f);					 
    		glColor3f(0.0f,1.0f,0.0f);						 
    		glVertex3f(-1.0f,-1.0f, 1.0f);				 
    		glColor3f(0.0f,0.0f,1.0f);						 
    		glVertex3f( 1.0f,-1.0f, 1.0f);					 
    		glColor3f(1.0f,0.0f,0.0f);						 
    		glVertex3f( 0.0f, 1.0f, 0.0f);				 
    		glColor3f(0.0f,0.0f,1.0f);						 
    		glVertex3f( 1.0f,-1.0f, 1.0f);					 
    		glColor3f(0.0f,1.0f,0.0f);						 
    		glVertex3f( 1.0f,-1.0f, -1.0f);					 
    		glColor3f(1.0f,0.0f,0.0f);						 
    		glVertex3f( 0.0f, 1.0f, 0.0f);					 
    		glColor3f(0.0f,1.0f,0.0f);						 
    		glVertex3f( 1.0f,-1.0f, -1.0f);				  
    		glColor3f(0.0f,0.0f,1.0f);						 
    		glVertex3f(-1.0f,-1.0f, -1.0f);					 
    		glColor3f(1.0f,0.0f,0.0f);						 
    		glVertex3f( 0.0f, 1.0f, 0.0f);					 
    		glColor3f(0.0f,0.0f,1.0f);						 
    		glVertex3f(-1.0f,-1.0f,-1.0f);					 
    		glColor3f(0.0f,1.0f,0.0f);						 
    		glVertex3f(-1.0f,-1.0f, 1.0f);
    		glEnd();
     
    		if (x == 0) x = -3;
    		else if (x < 0 ) x = 3;
     
     
    		rtri+=0.05f;
     
    		}
    		if (a == 2) 
     	{	
     
     		glLoadIdentity();
     		glTranslatef(x, 0.0f,-25.0f);
    		glPopMatrix();
    		glRotatef(rtri,0.0f,1.0f,0.0f);
    		glBegin(GL_TRIANGLES);								
    		glColor3f(1.0f,0.0f,0.0f);						
    		glVertex3f( 0.0f, 1.0f, 0.0f);					 
    		glColor3f(0.0f,1.0f,0.0f);						 
    		glVertex3f(-1.0f,-1.0f, 1.0f);				 
    		glColor3f(0.0f,0.0f,1.0f);						 
    		glVertex3f( 1.0f,-1.0f, 1.0f);					 
    		glColor3f(1.0f,0.0f,0.0f);						 
    		glVertex3f( 0.0f, 1.0f, 0.0f);				 
    		glColor3f(0.0f,0.0f,1.0f);						 
    		glVertex3f( 1.0f,-1.0f, 1.0f);					 
    		glColor3f(0.0f,1.0f,0.0f);						 
    		glVertex3f( 1.0f,-1.0f, -1.0f);					 
    		glColor3f(1.0f,0.0f,0.0f);						 
    		glVertex3f( 0.0f, 1.0f, 0.0f);					 
    		glColor3f(0.0f,1.0f,0.0f);						 
    		glVertex3f( 1.0f,-1.0f, -1.0f);				  
    		glColor3f(0.0f,0.0f,1.0f);						 
    		glVertex3f(-1.0f,-1.0f, -1.0f);					 
    		glColor3f(1.0f,0.0f,0.0f);						 
    		glVertex3f( 0.0f, 1.0f, 0.0f);					 
    		glColor3f(0.0f,0.0f,1.0f);						 
    		glVertex3f(-1.0f,-1.0f,-1.0f);					 
    		glColor3f(0.0f,1.0f,0.0f);						 
    		glVertex3f(-1.0f,-1.0f, 1.0f);
     
    		glEnd();
     
    		if (x == 0) x = -3;
    		else if (x < 0 ) x = 3;
     
     
    		rtri+=0.05f;
     
    		}
    }
     
    	return TRUE;										
    }
    Where I did mistake ?

  4. #4
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,105
    Your push and pops are not paired - you push if a == 1 and only pop if it is equal to 2 - this look wrong.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •