re: Convert NSOpenGLView pixel coordinates to OpenGL world coordinates

  • Your technique works! I get world coordinates each
    time I click on the mouse. Here's the code that I
    implemented:

    - (void)clickedAtPoint:(NSPoint)p
    {
    GLdouble model[16];
    GLfloat proj[16];
    GLint  view[4];

    glGetDoublev( GL_MODELVIEW, model );
    glGetFloatv( GL_PROJECTION, proj );
    glGetIntegerv( GL_VIEWPORT, view );


    GLdouble wx, wy, wz;

    if ( gluUnProject( (GLdouble)p.x, (GLdouble)p.y, 0.0,
    model, proj, view, &wx, &wy, &wz ) == GL_TRUE )
    {
      NSLog(@"x: %f\ty: %f\t\twx: %f\twy: %f", p.x, p.y,
    wx, wy );
    }
    }

    Please note that the passed-in point value (p) has
    already been converted to view coordinates in the
    -mouseDown: method.

    NSPoint        p = [self convertPoint:[theEvent
    locationInWindow] fromView:nil];

    The only issue that I'm having is that I'm getting
    some rather strange values. Such as...

    This is clicking on the center of the view:
    x: 377.000000    y: 270.000000        wx: 0.175438    wy:
    -0.899123

    Which seems OK.

    This is the upper-left corner:
    x: 41.000000    y: 582.000000 wx: -987407225.612816    wy:
    942763954.976947

    And this is near the lower-right corner:
    x: 770.000000    y: 30.000000        wx: 0.000000    wy: -1.000000

    There is one last step that I seem to be missing...

    --- David Spooner <dave...> wrote:

    > Here is some psuedo code which may help...
    >
    > // First convert the point p from the NSView's
    > coordinate system into
    > screen (viewport) coordinates...
    > NSPoint q = p - [view visibleRect].origin;
    >
    > // Get the modelview and projection matrices and the
    > viewport from
    > OpenGL...
    > double M[16], P[16];
    > GLint V[4];
    > glGetDoublev(GL_MODELVIEW, M);
    > glGetFloatv(GL_PROJECTION, P);
    > glGetIntegerv(GL_VIEWPORT, V);
    >
    > // Then inverse-project from screen coordinates to
    > world coordinates...
    > double wx, wy, wz;
    > gluUnProject(q.x, q.y, 0, M, P, V, &wx, &wy, &wz);
    >
    >
    > dave
    >
    >


    ____________________________________________________________________________________
    Yahoo! oneSearch: Finally, mobile search
    that gives answers, not web links.
    http://mobile.yahoo.com/mobileweb/onesearch?refer=1ONXIC
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