OpenGL: Set position instead of translating?

2019-06-25 14:42发布

问题:

Can I set the current render position to be an arbitrary value, instead of just giving it an offset from the current location?

This is what I'm doing currently:

gl.glTranslatef(3.0f, 0.0f, 2.0f);

It allows me to say "I want to move left" but not "I want to move to point (2, 1, 2)". Is there a way to do the latter?

I'm using OpenGL with JOGL.

Update:

@Bahbar suggests the following:

gl.glLoadIdentity();
gl.glTranslatef(...);

When I do this, everything except six lines disappears. I'm not sure why. I'm having a problem with the far clipping plane being too close, so perhaps they're too far away to be rendered.

回答1:

Yes. Just start from the identity matrix.

gl.glLoadIdentity();
gl.glTranslatef(...);


回答2:

Yes, you can set your view position using the gluLookAt command. If you look at the OpenGL FAQ, there is a question that has the line most relevant to your problem: 8.060 How do I make the camera "orbit" around a point in my scene?

You can simulate an orbit by translating/rotating the scene/object and leaving your camera in the same place. For example, to orbit an object placed somewhere on the Y axis, while continuously looking at the origin, you might do this:

gluLookAt(camera[0], camera[1], camera[2], /* look from camera XYZ */
          0, 0, 0, /* look at the origin */
          0, 1, 0); /* positive Y up vector */ 
glRotatef(orbitDegrees, 0.f, 1.f, 0.f); /* orbit the Y axis */ 
          /* ...where orbitDegrees is derived from mouse motion */
glCallList(SCENE); /* draw the scene */

If you insist on physically orbiting the camera position, you'll need to transform the current camera position vector before using it in your viewing transformations.

In either event, I recommend you investigate gluLookAt() (if you aren't using this routine already).

Note that gluLookAt call: the author is storing the camera position in a 3 value array. If you do the same, you will be able to specify your viewpoint in absolute coordinates exactly as you wanted.

NOTE: if you move the eye position, it's likely that you're going to want to specify the view direction as well. In this example, the author has decided to keep the eye focused on the point (0, 0, 0).

There's a good chance that this question is related to what you're trying to do as well: GLU.gluLookAt in Java OpenGL bindings seems to do nothing.