# glTranslated function question

Hello everyone,

I am learning about the glTranslate function and I was wondering if anyone here could give me insight on this function. I would like to note that I am drawing in 2D So far, I have two cases.

Case 1:

``````
if(event.GetKeyCode() == LETTER_W || event.GetKeyCode() == LETTER_w)
{
cameraY -= 16.0f;
}
else if(event.GetKeyCode() == LETTER_S || event.GetKeyCode() == LETTER_s)
{
cameraY += 16.0f;
}
else if(event.GetKeyCode() == LETTER_A || event.GetKeyCode() == LETTER_a)
{
cameraX -= 16.0f;
}
else if(event.GetKeyCode() == LETTER_d || event.GetKeyCode() == LETTER_D)
{
cameraX += 16.0f;
}

glMatrixMode(GL_MODELVIEW);
glPopMatrix();

glTranslated(cameraX, cameraY, 0.0f);
glPushMatrix();

``````

In the first case, we see glTranslate function between a pop and push matrix. When this occurs, the view is translated.

case 2:

``````
// Clears the color buffer
glClear(GL_COLOR_BUFFER_BIT);

//Reset to modelview matrix
glMatrixMode(GL_MODELVIEW);
glPopMatrix();

glPushMatrix();

glTranslatef(canvasWidth / 2.0f, canvasHeight / 2.0f, 0.0f);

glColor3f( 1.f, 1.f, 1.f );
glVertex2f( -canvasWidth / 4.f, -canvasHeight / 4.f );
glVertex2f(  canvasWidth / 4.f, -canvasHeight / 4.f );
glVertex2f(  canvasWidth / 4.f,  canvasHeight / 4.f );
glVertex2f( -canvasWidth / 4.f,  canvasHeight / 4.f );
glEnd();

//Move to the right of the screen
glTranslatef( canvasWidth, 0.f, 0.f );

glColor3f( 0.f, 1.f, 0.f );
glVertex2f( -canvasWidth / 4.f, -canvasHeight / 4.f );
glVertex2f(  canvasWidth / 4.f, -canvasHeight / 4.f );
glVertex2f(  canvasWidth / 4.f,  canvasHeight / 4.f );
glVertex2f( -canvasWidth / 4.f,  canvasHeight / 4.f );
glEnd();

``````

In the second case, first, a white square is drawn at the center (even though comment says red it is white) and then, a green square is drawn screen. Note that the glTranslate function is not in between push or pop; therefor, the view is not changed.

Are these reasonable conclusions on how to use glTranslate function. In order to change the view (in order to pan around my drawing), then I need to place glTranslate between a push and pop sequence. If I want to drawn something far off in the distance (or beyond my view), then I do not place glTranslate between a push and pop sequence. This will then only effect the modelâ€™s placement.

Are these reasonable conclusions that I should understand in order to use glTranslate function properly?

I do not specify glTranslated or glTranslatef becuase I am assuming that this applies to both forms.

In general:

1. glMatrixMode(GL_MODELVIEW);
3. DO GLOBAL TRANFORMATIONS EFFECTING ALL OBJECTS
4. glPushMatrix(); //stores the matrix as it is now
5. DO TRANSFORMATIONS FOR 1st OBJECT
6. RENDER 1st OBJECT
7. plPopMatrix(); //will restore the matrix as it was at point 4
8. glPushMatrix(); //stores the matrix as it is now (same like at point 4)
9. DO TRANSFORMATIONS FOR 2st OBJECT
10. RENDER 2st OBJECT
11. plPopMatrix(); //will restore the matrix as it was at point 8 (same like at point 4)

By this global transformations (point 3) will take effect to all the objects. So all your objects are translated if you call glTranslate there, while you can do individual transformations for each object at point 5 and 9.

If you render multiple objects, the glPush- and glPopMatrix around the last object may be left out, because the global matrix is not needed anymore after drawing it.

Regards,
Frank