openglmatrixlwjglperspectivecameraray-picking

How to calculate UP and position of my camera?


I want to implement in my game.
I follow this guide to implement it.

To calculate the actual ray I lack the lookAt and the up Vector.

The problem

I use glRotate and glTranslate instead of gluLookAt(eye,target,up) to rotate and move the camera.

The question

How can I calculate lookAt and up from my rotations and translations / matrix?

Code snippets

Move camera with Keyboard

if (game.keys[Keyboard.KEY_W]) {
    position.x -= (float) (Math.sin(-rotation.y * Math.PI / 180) * speed);
    position.z -= (float) (Math.cos(-rotation.y * Math.PI / 180) * speed);
}
if (game.keys[Keyboard.KEY_S]) {
    position.x += (float) (Math.sin(-rotation.y * Math.PI / 180) * speed);
    position.z += (float) (Math.cos(-rotation.y * Math.PI / 180) * speed);
}
if (game.keys[Keyboard.KEY_A]) {
    position.x += (float) (Math.sin((-rotation.y - 90) * Math.PI / 180) * speed);
    position.z += (float) (Math.cos((-rotation.y - 90) * Math.PI / 180) * speed);
}
if (game.keys[Keyboard.KEY_D]) {
    position.x += (float) (Math.sin((-rotation.y + 90) * Math.PI / 180) * speed);
    position.z += (float) (Math.cos((-rotation.y + 90) * Math.PI / 180) * speed);
}

Rotate camera with Mouse

if (Mouse.isGrabbed()) {
    float mouseDX = Mouse.getDX() * 0.8f * 0.16f;
    float mouseDY = Mouse.getDY() * 0.8f * 0.16f;
    if (rotation.y + mouseDX >= 360) {
        rotation.y = rotation.y + mouseDX - 360;
    } else if (rotation.y + mouseDX < 0) {
        rotation.y = 360 - rotation.y + mouseDX;
    } else {
        rotation.y += mouseDX;
    }
    if (rotation.x - mouseDY >= -89 && rotation.x - mouseDY <= 89) {
        rotation.x += -mouseDY;
    } else if (rotation.x - mouseDY < -89) {
        rotation.x = -89;
    } else if (rotation.x - mouseDY > 89) {
        rotation.x = 89;
    }
}

Update camera

glRotatef(rotation.x, 1, 0, 0);
glRotatef(rotation.y, 0, 1, 0);
glRotatef(rotation.z, 0, 0, 1);
glTranslatef(-position.x + zoom, -position.y - 7 + zoom * -1.05f, -position.z);






My Implementation

Camera

private void updateCamera() {
    DoubleBuffer m = createDoubleBuffer(16);
    glGetDouble(GL_MODELVIEW_MATRIX, m);
    vUp.x = (float) m.get(1);
    vUp.y = (float) m.get(5);
    vUp.z = (float) m.get(9);

    vPos.x = (float) -(m.get(0) * m.get(12) + m.get(1) * m.get(13) + m.get(2) * m.get(14));
    vPos.y = (float) -(m.get(4) * m.get(12) + m.get(5) * m.get(13) + m.get(6) * m.get(14));
    vPos.z = (float) -(m.get(8) * m.get(12) + m.get(9) * m.get(13) + m.get(10) * m.get(14));

    vBack.x = (float) m.get(2);
    vBack.y = (float) m.get(6);
    vBack.z = (float) m.get(10);

    glRotatef(rotation.x, 1, 0, 0);
    glRotatef(rotation.y, 0, 1, 0);
    glRotatef(rotation.z, 0, 0, 1);
    glTranslatef(-position.x + zoom, -position.y, -position.z);
}

Picking

public void picking(float screenX, float screenY, PickingRay pickingRay) {
    helper.Vector3f view = camera.vBack;
    view.x *= -1;
    view.y *= -1;
    view.z *= -1;
    helper.Vector3f screenHorizontally = new helper.Vector3f(0, 0, 0);
    helper.Vector3f screenVertically = new helper.Vector3f(0, 0, 0);
    float viewAngle = 150; // fovy 150
    float nearClippingPlaneDistance = 1; // was mus hier rein?
    int viewportWidth = width;         // 1200
    int viewportHeight = height;       // 900
    screenHorizontally.crossAndAssign(view, camera.vUp).normalize();
    screenVertically.crossAndAssign(screenHorizontally, view).normalize();

    final float radians = (float) (viewAngle * Math.PI / 180f);
    float halfHeight = (float) (Math.tan(radians / 2) * nearClippingPlaneDistance);
    float halfScaledAspectRatio = halfHeight * getViewportAspectRatio();

    screenVertically.scale(halfHeight);
    screenHorizontally.scale(halfScaledAspectRatio);

    pickingRay.getClickPosInWorld().set(camera.vPos);
    pickingRay.getClickPosInWorld().add(view);

    screenX -= (float) viewportWidth / 2f;
    screenY -= (float) viewportHeight / 2f;
    screenX /= ((float) viewportWidth / 2f);
    screenY /= ((float) viewportHeight / 2f);

    pickingRay.getClickPosInWorld().x += screenHorizontally.x * screenX + screenVertically.x * screenY;
    pickingRay.getClickPosInWorld().y += screenHorizontally.y * screenX + screenVertically.y * screenY;
    pickingRay.getClickPosInWorld().z += screenHorizontally.z * screenX + screenVertically.z * screenY;

    pickingRay.getDirection().set(pickingRay.getClickPosInWorld());
    // pickingRay.getDirection().sub(cpos); // cpos is always [0,0,0]
    System.out.println("x: " + pickingRay.getClickPosInWorld().x + " y: " + pickingRay.getClickPosInWorld().y + " z: " + pickingRay.getClickPosInWorld().z);  // ends in [0, -3.7, 0] to [0, 3.7, 0]
}

Solution

  • You can get it from OpenGL's modelview matrix.

    Retrieve the matrix (after setting up the view but before applying any object transformations) like so:

    GLfloat m[16];
    glGetFloatv(GL_MODELVIEW_MATRIX, m);
    

    Then the X, Y and Z components of your up vector are in m[1], m[5], m[9]. The 'Right' and 'Back' vector are int m[0], m[4], m[8] and m[2], m[6], m[10].

    See this link for more information.

    To get the camera position you have to use m[12], m[13] and m[14] as described here. For example:

    camX = -(m[0] * m[12] + m[1] * m[13] + m[2] * m[14]);
    camY = -(m[4] * m[12] + m[5] * m[13] + m[6] * m[14]);
    camZ = -(m[8] * m[12] + m[9] * m[13] + m[10] * m[14]);