Results 1 to 5 of 5

Thread: Mouse Cursor Position in OpenGL coordinates

  1. #1
    Member Newbie
    Join Date
    Nov 2011
    Location
    Fairborn, OH
    Posts
    31

    Mouse Cursor Position in OpenGL coordinates

    Is there an easy way to get the current mouse position in OpenGL coordinates without using GLUT?

    I'm using an ortho projection and everyting is drawn at -1 so the Z coordinate doesn't really matter to me, but if I could somehow read the mouse position in opengl coordinates that would be super helpful in moving forward.

  2. #2
    Super Moderator OpenGL Lord
    Join Date
    Dec 2003
    Location
    Grenoble - France
    Posts
    5,574

    Re: Mouse Cursor Position in OpenGL coordinates

    "without using GLUT" -> and using what instead ? GL alone does not handle a mouse.

  3. #3
    Member Newbie
    Join Date
    Dec 2010
    Posts
    33

    Re: Mouse Cursor Position in OpenGL coordinates

    You can do this by math calc or by using the glReadPixels

    Code :
    Vector2 ScreenToGlobal(Integer X; Integer Y);
    {
    var
      viewport:   array [1..4]  of Integer;
      modelview:  array [1..16] of Double;
      projection: array [1..16] of Double;
      Point:      array [0..2]  of Double;
      winZ:       Double;
      glGetDoublev(GL_MODELVIEW_MATRIX, @modelview);
      glGetDoublev(GL_PROJECTION_MATRIX, @projection);
      glGetIntegerv(GL_VIEWPORT, @viewport);
     
      if (Y == 0) {Y := 1};
     
      glReadPixels(X, -Y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, @winZ );
      gluUnProject(X, viewport[4]-Y, winZ, @modelview, @projection, @viewport, Point[0], Point[1], Point[2]);
     
      Result[0] = Point[0];
      Result[1] = Point[1];
    }

    using math, you need to know what's your zoom value, your current camera position and your mouse position

  4. #4
    Member Newbie
    Join Date
    Nov 2011
    Location
    Fairborn, OH
    Posts
    31

    Re: Mouse Cursor Position in OpenGL coordinates

    hmmm...I for some reason thought gluUnProject was GLUT...apparently its not...I seem to have gluUnProject. I've found plenty of examples of gluUnproject...I guess I just need to know now how to get my mouse cursor position in screen coordinates.

  5. #5
    Member Newbie
    Join Date
    Nov 2011
    Location
    Fairborn, OH
    Posts
    31

    Re: Mouse Cursor Position in OpenGL coordinates

    Actually, I figured it out...thanks!

Similar Threads

  1. Replies: 0
    Last Post: 01-17-2018, 12:58 AM
  2. Terrain 3D position from mouse coordinates
    By glararan in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 08-12-2013, 07:59 AM
  3. How to get cursor position visibl in opengl screen
    By revathy in forum OpenGL: Linux
    Replies: 10
    Last Post: 01-20-2010, 09:31 PM
  4. Replies: 0
    Last Post: 10-26-2005, 06:06 AM
  5. how to set the position of mouse cursor?
    By kirchhoff in forum OpenGL: Linux
    Replies: 2
    Last Post: 04-12-2005, 06:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean