Reputation: 21
I try to code two object move independent which mean both are moving two different direction it's worked but it can't move continuous. when i put glTranslatef()
at outside the glPushMatrix()
... glPopMatrix()
it work normally.
void display()
{
glClearColor(0.356, 0.701, 0.0, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// if put gltransate() at here the object will moving continuous
glPushMatrix();
glTranslatef(.5, 0, 0);
glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 1.0);
glVertex2f(-0.8, 0.5);
glVertex2f(-0.8, 0.8);
glVertex2f(-0.2, 0.8);
glVertex2f(-0.2, 0.5);
glEnd();
glPopMatrix();
glPushMatrix();
glTranslatef(-.5, 0, 0);
glBegin(GL_QUADS);
glColor3f(.0, .0, .0);
glVertex2f(-0.8, 0.2);
glVertex2f(-0.8, 0.5);
glVertex2f(-0.2, 0.5);
glVertex2f(-0.2, 0.2);
glEnd();
glPopMatrix();
}
i expect the for the first square objects will go to the right constantly but it seem like just translate once then stop at the position.
Upvotes: 0
Views: 3295
Reputation: 210876
Note, in general I recommend to use a math library like OpenGL Mathematics to do the matrix calculations and glLoadMatrix()
to load a matrix of type glm::mat4
to the current matrix.
Anyway, an operation like glTranslatef()
creates a new matrix and multiplies the current matrix by the new matrix. That's why consecutive calls to glTranslatef
cause progressive "movement".
glPushMatrix / glPopMatrix
store and restore the matrix on the matrix stack. So the consecutive movement can't work, because the current matrix stays the same at the begin of each frame.
One solution would be to store the translation to a variable and increment the variable:
GLfloat trans_a = 0.0f;
GLflaot trans_b = 0.0f;
void display()
{
glClearColor(0.356, 0.701, 0.0, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
trans_a += 0.5f;
glTranslatef(trans_a, 0, 0);
glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 1.0);
glVertex2f(-0.8, 0.5);
glVertex2f(-0.8, 0.8);
glVertex2f(-0.2, 0.8);
glVertex2f(-0.2, 0.5);
glEnd();
glPopMatrix();
glPushMatrix();
trans_b += 0.5f;
glTranslatef(trans_b, 0, 0);
glBegin(GL_QUADS);
glColor3f(.0, .0, .0);
glVertex2f(-0.8, 0.2);
glVertex2f(-0.8, 0.5);
glVertex2f(-0.2, 0.5);
glVertex2f(-0.2, 0.2);
glEnd();
glPopMatrix();
}
A more generalized solution is to get and store the current matrix (after the translation) by glGetFloatv(GL_MODELVIEW_MATRIX, ...)
and to reload it by glLoadMatrix()
:
// init identity matrices
GLfloat mat_a[16] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 };
GLfloat mat_b[16] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 };
void display()
{
glClearColor(0.356, 0.701, 0.0, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glLoadMatrixf(mat_a)
glTranslatef(0.5f, 0, 0);
glGetFloatv(GL_MODELVIEW_MATRIX, mat_a);
glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 1.0);
glVertex2f(-0.8, 0.5);
glVertex2f(-0.8, 0.8);
glVertex2f(-0.2, 0.8);
glVertex2f(-0.2, 0.5);
glEnd();
glPopMatrix();
glPushMatrix();
glLoadMatrixf(mat_b)
glTranslatef(0.5f, 0, 0);
glGetFloatv(GL_MODELVIEW_MATRIX, mat_b);
glBegin(GL_QUADS);
glColor3f(.0, .0, .0);
glVertex2f(-0.8, 0.2);
glVertex2f(-0.8, 0.5);
glVertex2f(-0.2, 0.5);
glVertex2f(-0.2, 0.2);
glEnd();
glPopMatrix();
}
Note, in this case mat_a
and mat_b
should be loaded after the initialization of the view matrix and when the view matrix changes, then the matrices would not consider that.
Upvotes: 2