GLM unprojects returns wrong results

  c++, glm, opengl

In my project I use a fairly complex camera glass which doues trackball / arcball rotation arround the center. That works fine sofar. For the creation of the MVP matrix I use:

    // Get a handle for our "MVP" uniform
    GLuint MatrixID = glGetUniformLocation(shaderProgram, "MVP");

    // Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 <-> 100 units
    glm::mat4 projection = glm::perspective(glm::radians(45.0f), 4.0f / 3.0f, 0.1f, 50.f);
    // Camera matrix
    glm::mat4 view = camera->getMatrix();
    // Model matrix -> identity matrix (origin)
    glm::mat4 model = glm::mat4(1.0f);

    // our ModelViewProjection : multiplication of our 3 matrices
    glm::mat4 MVP = projection * view * model;

VertexShader is also not surprising (just showing a part here) v_position = MVP * vec4(vPos,1); and works well so far.

To get the normalized direction that the mouse is pointing at I use the following code:
direction = glm::normalize( glm::unProject(glm::vec3(mouseX,700 - mouseY, 1), model, projection, glm::vec4(0.0f, 0.0f, 700, 700))); – wehere 700 is the width and height of the screen.

At some other point I try to do intersections tests using the glm intersectRayTriangle function:
glm::intersectRayTriangle(cameraPosition, direction , v0, v1, v2, intersectionPos, dist)
And here is the problem: I don’t get right results, so I guess somewhere in the unProject code a error is existing. Maybe someone has an idea about that.

Source: Windows Questions C++

LEAVE A COMMENT