Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 6 of 6

Thread: glTranslatef() not working

  1. #1
    Guest

    glTranslatef() not working

    OK, so I'm going through Nehe's tutorial, and the second lesson doesn't work. It seems to be the glTranslate() function that doesn't work. It doesn't properly translate in the z-axis, so I just get a black screen. If I set the z-translation to zero, I can see the two object up close, and properly translated in the x-axis. I'm not really sure what's wrong, and I'm using Windows with VC++ if that matters. Here's the code that should work:

    Code :
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear Screen And Depth Buffer
    	glLoadIdentity();									// Reset The Current Modelview Matrix
    	glTranslatef(-1.5f,0.0f,-6.0f);			
    	glBegin(GL_TRIANGLES);	
    		glVertex3f( 0.0f, 1.0f, 0.0f);	
    		glVertex3f(-1.0f,-1.0f, 0.0f);	
    		glVertex3f( 1.0f,-1.0f, 0.0f);
    	glEnd();			
    	glTranslatef(3.0f,0.0f,0.0f);
    	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();

  2. #2
    Member Regular Contributor
    Join Date
    Jan 2004
    Posts
    322

    Re: glTranslatef() not working

    Well, simple mistake: positive Z points into the screen.

    And since the camera is located at 0,0,0 by default, that means that the objects are rendered "behind the viewer".

    In other words: try a positive Z instead of a negative Z. That's all you need to know for this tutorial.

  3. #3
    Senior Member OpenGL Guru
    Join Date
    Feb 2000
    Location
    Sweden
    Posts
    3,115

    Re: glTranslatef() not working

    No, the positive Z, by default at least, points out of the screen, Negative Z is into the screen, so unless he have changed the default orientation, he's translating in the correct direction.

    Post some more code, at least everything that has something to do with the projection and modelview matrix. That code alone is not enough to locate the problem without guessing.

  4. #4
    Member Regular Contributor
    Join Date
    Jan 2004
    Posts
    322

    Re: glTranslatef() not working

    If the code is identical to the example, there's a gluPerspective in there:

    Code :
    gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
    Now call me crazy, but that sounds like it will make higher z values end up deeper into the screen.

    OK, so that does not mean that Z is always into the screen. But it is always into the screen when you use glOrtho or gluPerspective in the "usual" way (i.e. with zNear < zFar).

  5. #5
    Member Regular Contributor
    Join Date
    Aug 2003
    Posts
    368

    Re: glTranslatef() not working

    Nope, it just sets the near and far clipping planes for the view frustum. Bob's right. Check out the red book. The reason you sometimes can see this the wrong way (oposite actually) is if you set up your camera with gluLookAt looking at the wrong direction. But by default z increases as we go from the screen to the user's eyes.

  6. #6
    Advanced Member Frequent Contributor plasmonster's Avatar
    Join Date
    Mar 2004
    Posts
    750

    Re: glTranslatef() not working

    Yeah this get confusing when you don't distinguish between screen-space z's and the camera's frame vectors (eye-space z). The GL keeps a negative z-axis for forward so the coordinate system remains right handed (unlike the other API). I like the way the GL does this.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •