PDA

View Full Version : Translation in Z



RitchieR
08-20-2011, 05:40 PM
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!

Snow_56767
08-20-2011, 06:41 PM
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.

ZeppelinDestruction
09-25-2011, 09:02 PM
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();

ZeppelinDestruction
09-25-2011, 11:30 PM
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.