Translation in Z

I´ve been doing some basic code in OpenGL with Mac OS. I´m currently using Xcode 4.0.2 with the integrated OpenGL Framework (I really don´t know which version it is or where to find out)

anyway… I am having hard time to understand (or get to work) the glTranslatef function with z values. Here´s my code:

glLoadIdentity();
glTranslatef(-0.5, 0.0, 0.0);

glBegin(GL_QUADS);
{
    
    //Front
    glTexCoord2f(0.0, 0.0);
    glVertex3f(-0.5,-0.5,0.0);
    glTexCoord2f(1.0, 0.0);
    glVertex3f(0.5,-0.5,0.0);
    glTexCoord2f(1.0, 1.0);
    glVertex3f(0.5,0.5,0.0);
    glTexCoord2f(0.0, 1.0);
    glVertex3f(-0.5,0.5,0.0);
}
glEnd();

glLoadIdentity();
glTranslatef(0.0, 0.0, 1.0);
glRotatef(45.0, 0.0, 0.0, 1.0);

glBegin(GL_QUADS);
{

    //Front
    glTexCoord2f(0.0, 0.0);
    glVertex3f(-0.5,-0.5,0.0);
    glTexCoord2f(1.0, 0.0);
    glVertex3f(0.5,-0.5,0.0);
    glTexCoord2f(1.0, 1.0);
    glVertex3f(0.5,0.5,0.0);
    glTexCoord2f(0.0, 1.0);
    glVertex3f(-0.5,0.5,0.0);
}

glEnd();

Now, I change values of the glTranslatef in z, expecting the second square to move either forward or backward (thus, increasing or reducing it´s size since its closer or further from the view and being drawn either in front or in the back of the other square) but all I get is either the same size of the square or no square at all and always in front of the first square… any ideas?

Thanks in advance!

What view type are you using? Perspective or Orthogonal?

Also, try to increase the Z-Translation more to see if it is actually working -the movement is just small enough to not really make a difference- or if there is a bug somewhere.

Another thing, try calling glLoadIdentity() after your translation and rotation. If that doesn’t work, then try removing glLoadIdentity() completely except after you call glClear().

Just little stuff to see what is and what isn’t.

Hi, I’m having the same problem. I haven’t set any projections (I assume there’s a default projection?). Translating by X or Y moves the “camera” as I would expect, showing the edges of the quad. But translating the view back causes the quad to disappear if Z is less than -1.0. In every explanation I’ve seen about viewing transformations, there seems to be some critical piece of information missing but I’m not sure what it is (or I’m completely blind!)

This is the complete code inside my update function (I’m using lwjgl):

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();

   glTranslatef(0.0f,0.0f,-5.0f);

   glColor3d(255,255,0);

   glBegin(GL_QUADS);
     glVertex3f(-1.0f,-1.0f,0.0f);
     glVertex3f(-1.0f,1.0f,0.0f);
     glVertex3f(1.0f,1.0f,0.0f);
     glVertex3f(1.0f,-1.0f,0.0f);
   glEnd();

I think I found the missing bit (in the Superbible 4th edition, pg 146):

“In OpenGL, when the projection matrix is loaded with the identity matrix, the diagonal line of 1s specifies that the clipping planes extend from the origin to +1 or -1 in all directions.”

So it seems that we need to set up a projection matrix with some sensible clipping planes otherwise the objects will be culled when we move the view:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

float aspect=800.0f/600.0f;
GLU.gluPerspective(60.0f,aspect,1.0f,400.0f);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glTranslatef(0.0f,0.0f,-5.0f);

I think the reason this gets missed is because this code is usually inside a glut window resize function which we gloss over if we’re not using glut.