PDA

View Full Version : glTranslatef



tabaltprintscreen
03-20-2006, 02:29 PM
When I set glTranslate to move backward or foward greater than 1.0f it doesn't display anything. IF it is set at 1.0f it appears but isn't changed at all. What is going on?

Heres my code:

/* OpenGL animation code goes here */
glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
glShadeModel(GL_SMOOTH);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

glPushMatrix ();
glColor3f ( 1.0f, 1.0f, 1.0f);
glTranslatef (0.0f,0.0f,0.0f);
glBegin (GL_TRIANGLES);
glVertex2f (0.0f, 1.0f);
glVertex2f (0.87f, -0.5f);
glVertex2f (-0.87f, -0.5f);
glEnd ();
glPopMatrix ();

SwapBuffers (hDC);

Ehsan Kamrani
03-20-2006, 03:43 PM
You are drawing your object in camera position. So you can not see anything. However, as you are drawing a 2D object, you can solve this problem by using the function gluOrtho2D().
-Ehsan-

CatDog
03-20-2006, 07:24 PM
As Ehsan already stated: you are not applying a projection. You are loading the identity matrix and draw a 2D triangle (glVertex2), setting the z values to zero implicitly.

Moving "backward and forward" makes no sense then. (My assumption is, that you mean glTranslatef(0,0,x) and that you don't do anything else to the matrix stacks.)

Without a projection applied, OpenGL maps z coordinates in the range [-1..1] to the depth buffer range [0..1]. See glDepthRange in the spec. Everything outside of the depth range gets clipped.

This explains, why the triangle disappears, when you shift it outside of the depth range using glTranslatef. And the shape doesn't change because of the lack of a perspective projection.

Read Chapter 3 of the Red Book (http://glprogramming.com/red/chapter03.html) !

CatDog