Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: Mouse projection failed

  1. #1
    Newbie Newbie
    Join Date
    Jun 2013
    Posts
    1

    Mouse projection failed

    Hi,

    I'm trying to project my cursor in 3D world since few days and I've got some problems to detect ray/box intersection.
    To present my problem, I will show you how I proceed:

    First, I create my ray from my camera informations:

    Code :
    const glm::vec3 position    = glm::vec3( mouse.x, screenH - mouse.y, 0.f );
    const glm::mat4& view       = camera.getViewMatrix();
    const glm::mat4& projection = camera.getProjectionMatrix();
    const glm::vec4& viewPort   = camera.getViewport();
     
    ray.origin    =  glm::unProject( glm::vec3( position.x, position.y, 0.f ),
                                                view,
                                                projection,
                                                viewPort );
     
    ray.direction =  glm::unProject( glm::vec3( position.x, position.y, 1.f ),
                                                view,
                                                projection,
                                                viewPort );

    After that, I compute bounding box for every model and I test intersection :
    Code :
    BoundingBox box;
    box.setScale(model->getScale());
    box.setTranslation(model->getPosition());
     
    if( box.intersectLine( ray.getOrigin(), ray.getDirection() ) )
    {
          // …
    }

    The problem appear when mouse is "far" from center, I think problem is here because I compare my ray (in world space) to my models (in local space),
    • How can I transform the result of glm::unproject to local space?
    • Am I using "glm::unproject" correctly?

    I've tried to draw the computed ray, it is correctly draw where I click.

    Thanks a lot for your time!

  2. #2
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    490
    Quote Originally Posted by IronBalls View Post
    How can I transform the result of glm::unproject to local space?
    Transform both points by the inverse of the object's transformation. I'm assuming that each object has a separate transformation, otherwise this should be accounted for by camera.getViewMatrix().

    If you're desperate to do it without using any additional matrix functions and don't care about performance, you can just execute your first block of code for each object, with the object's transformation incorporated into the camera's view matrix.

    But going forward, you're better off performing all of the matrix operations in the application, and just passing composed results to GL with glLoadMatrix() or glUniformMatrix(). With OpenGL 1.x, it's more efficient, with OpenGL 3.x core profile, you don't have a choice.

    Quote Originally Posted by IronBalls View Post
    Am I using "glm::unproject" correctly?
    Yes, assuming that glm::unProject() and the camera methods are trivial wrappers around the equivalent OpenGL functions (i.e. same argument order, same matrix storage convention, etc).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •