PDA

View Full Version : Remembering coordinates (Push/Pop Matrix ?)



janemba
01-12-2013, 09:07 AM
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:

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

tonyo_au
01-12-2013, 09:38 PM
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.

janemba
01-14-2013, 09:55 AM
Thanks for replay.
I understood, but I did something wrong because it isn't working. Can you correct this code, please ?

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 ?

tonyo_au
01-16-2013, 02:08 AM
Your push and pops are not paired - you push if a == 1 and only pop if it is equal to 2 - this look wrong.