LWJGL Forum

Programming => OpenGL => Topic started by: gibson on May 15, 2018, 14:56:16

Title: Translation to world coordinates
Post by: gibson on May 15, 2018, 14:56:16
I have mouse coordinates: mousePos, a matrix view view, and a perspective projection matrix pMatrix.

I translate the coordinates into the world: I find the inverse projection matrix and the inverse matrix view and multiply by the coordinates of the mouse. The coordinates of the origin is z = 4, the coordinates of the end is z = -100.

In the first case, I get the coordinates mouseDir1 = (-0.1985 0.02887 4), and in the second case mouseDir2 = (-0.1985 0.02887 -100).

Why are the coordinates x, y the same?

private Vector3f getCoord(MouseInput mouseInput,float z){
        int wdwWitdh = 640;
        int wdwHeight =640;

        Vector2d mousePos = mouseInput.getCurrentPos();
        float x = (float)(2 * mousePos.x) / (float)wdwWitdh - 1.0f;
        float y = 1.0f - (float)(2 * mousePos.y) / (float)wdwHeight;

        Matrix4f invProjectionMatrix = new Matrix4f();
        invProjectionMatrix.set(pMatrix);
        invProjectionMatrix.invert();
        Vector4f tmpVec = new Vector4f();
        tmpVec.set(x, y, z, 0);
        tmpVec.mul(invProjectionMatrix);
        tmpVec.z = z;
        tmpVec.w = 0.0f;

        Matrix4f viewMatrix = new Matrix4f().set(view);
        Matrix4f invViewMatrix = new Matrix4f();
        invViewMatrix.set(viewMatrix);
        invViewMatrix.invert();
        tmpVec.mul(invViewMatrix);
        Vector3f mouseDir1 = new Vector3f();
        mouseDir1.set(tmpVec.x, tmpVec.y, tmpVec.z);

        ///ТЕСТОВАЯ ПРОВЕРКА Z=-100;
        //конеÃ'‡ кооÃ'â,¬ÃÂ´ÃÂ¸ÃÂ½ÃÂ°Ã'‚Ã'‹ лÃ'Æ'Ã'‡Ð°
        Vector4f tmpVec1 = new Vector4f();
        tmpVec1.set(x, y, -100, 1.0f);
        tmpVec1.mul(invProjectionMatrix);
        tmpVec1.z =-100f;
        tmpVec1.w = 0.0f;
        tmpVec1.mul(invViewMatrix);
        Vector3f mouseDir2 = new Vector3f();
        mouseDir2.set(tmpVec1.x, tmpVec1.y, tmpVec1.z);
        System.out.println();
        return mouseDir1;
    }
Title: Re: Translation to world coordinates
Post by: KaiHH on May 15, 2018, 18:57:38

  Vector3f worldCoords = new Matrix4f(pMatrix).mul(view).unproject(x, y, z,
    new int[] { 0, 0, wdwWidth, wdwHeight }, new Vector3f());
Title: Re: Translation to world coordinates
Post by: gibson on May 15, 2018, 20:35:26
Quote from: KaiHH on May 15, 2018, 18:57:38

  Vector3f worldCoords = new Matrix4f(pMatrix).mul(view).unproject(x, y, z,
    new int[] { 0, 0, wdwWidth, wdwHeight }, new Vector3f());

All the time in the center is displayed