Convert NSOpenGLView pixel coordinates to OpenGL world coordinates

  • I've googled this one and have found some interesting
    info, but no clear-cut way to convert an
    NSOpenGLView's X,Y coordinate to an OpenGL world's X,Y
    coordinate (not too concerned with the Z coordinate).
    I need to know if two dimensional objects are getting
    mouse clicks.

    Has anyone done this before or know of any sample
    code? Thanks.

          ____________________________________________________________________________________
    Shape Yahoo! in your own image.  Join our Network Research Panel today!  http://surveylink.yahoo.com/gmrs/yahoo_panel_invite.asp?a=7
  • On 4 Oct 2007, at 17:36, Ian was here wrote:

    > I've googled this one and have found some interesting
    > info, but no clear-cut way to convert an
    > NSOpenGLView's X,Y coordinate to an OpenGL world's X,Y
    > coordinate (not too concerned with the Z coordinate).
    > I need to know if two dimensional objects are getting
    > mouse clicks.
    >
    > Has anyone done this before or know of any sample
    > code? Thanks.

    It depends on the GL projection matrix and viewport you've
    established.  In our code, we set ours up so that there was a 1:1
    mapping from GL co-ordinates to points (not pixels, mind...):

      NSRect bounds = [self bounds];

      // Update the viewport
      glViewport (0, 0, NSWidth (bounds), NSHeight (bounds));

      // Update the projection
      glMatrixMode (GL_PROJECTION);
      glLoadIdentity ();
      gluOrtho2D (0.0, NSWidth (bounds), 0.0, NSHeight (bounds));

      glMatrixMode (GL_MODELVIEW);

    If you do that, then you can just use the same co-ordinate system in
    both places.  Without that, you'll have to transform either the mouse
    co-ordinates or the co-ordinates of the things you're testing against
    to get them into the same co-ordinate space.

    Kind regards,

    Alastair.

    --
    http://alastairs-place.net
previous month october 2007 next month
MTWTFSS
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        
Go to today