3

Recently, I sought help regarding 3d camera rotations in OpenGL. This answer and the comments that followed helped me greatly, but there is still one major issue: when moving the camera, the motion is often, but not always, in exactly the opposite direction it should be. For instance, when the camera's orientation matrix is the identity, the camera moves perfectly. However, if it is rotated in any direction, its motion on the axis perpendicular to the axis of rotation will have the opposite sign of the intended motion.

With this said, I think I have an idea why this inconsistent behavior is happening:

As we all know, OpenGL uses a Right-Handed coordinate system:

Illustration of a right-handed coordinate system

If I understand this diagram correctly, when the camera is oriented at the identity the z axis should point INTO the camera, and z-values should decrease as one moves away from the camera (apparently affirmed here). (coordinates measured in world space).

However, in my program, the Z axis points AWAY from the camera and z values increase as one moves away from the camera. Here is an example:

x = 0; y = 0; z = 0; a small blue square

x = 0; y = 0; z = 8; a large blue square

The camera has moved forward, along what should be the negative z axis but appears to be the positive z axis.

If I am correct in interpreting this behavior as abnormal, it would explain all of my problems with the sign of my camera motion, as the motion that currently appears "correct" would in fact be erroneous and I have consistent signs that I could simply invert to result in correct motion.

So the question is:

  • Is my Z axis backwards, or is it supposed to be this way?
  • If it is backwards, why? Judging by multiple discussions on the topic (1, 2, 3), the error is likely to lie where I define my perspective frustum, so I'll put that here:

    public static final int P_ZNEAR = 1, P_ZFAR = 500;
    
    public static void perspective()
    {
        int i = GL11.glGetInteger(GL11.GL_MATRIX_MODE);
    
        GL11.glMatrixMode(GL11.GL_PROJECTION);
    
        GL11.glLoadIdentity();
    
        double ymax, xmax;
    
        ymax = P_ZNEAR * Math.tan(FOV / 2);
    
        xmax = ymax * ASPECT_RATIO;
    
        GL11.glFrustum(xmax, -xmax, -ymax, ymax, P_ZNEAR, P_ZFAR);
    
        GL11.glMatrixMode(i);
    }
    
rene
  • 41,474
  • 78
  • 114
  • 152
ApproachingDarknessFish
  • 14,133
  • 7
  • 40
  • 79
  • Ah, screw it. I'm using gluLookAt and using matrices to calculate the up vector. It's been eight months and this works well enough. – ApproachingDarknessFish Nov 12 '13 at 20:53
  • Somewhat unrelated but, if I'm seeking to 'reset' camera to an XYZ position(with press of a button), along with changing the 'lookAt' vector, I should also modify associated yaw&pitch value as well depending on new XYZ right ??---I currently take screen-space x-y coords from input, take delta bw current and prev values, add them to yaw & pitch, and get XYZ values with trigonometric functions--rotation movements depend on yaw-pitch values. – Harshiv Oct 19 '19 at 18:03

0 Answers0