0

I'm trying to use GLKMathUnproject() to translate screen coordinates to geometry, but my routine always returns whatever geometry is in the center of the screen.

My unproject code looks like this:

+ (GLKVector3) gluUnproject: (GLKVector3) screenPoint
                     inView: (UIView*) view
            withModelMatrix: (GLKMatrix4) model
                 projection: (GLKMatrix4) projection
{
    CGSize viewSize = view.bounds.size;
    int viewport[4];
    viewport[0] = 0.0f;
    viewport[1] = 0.0f;
    viewport[2] =  viewSize.width;
    viewport[3] =  viewSize.height;

    bool success;
    GLKVector3 result = GLKMathUnproject(screenPoint, model, projection, &viewport[0], &success);

    return result;
}

And my call to it looks like this:

- (void) handleSingleTap: (UIGestureRecognizer*) sender
{
    UITapGestureRecognizer *tapper = (UITapGestureRecognizer*) sender;
    CGPoint tapPoint = [tapper locationInView: self.view];

    // Find tapped point on geometry
    MyObject *bd = [worldObjects objectAtIndex: 0];  // the object on which I'm trying to sense the tap.
    // NOTE: bd is planar, along X/Z, with Y=0.  Visually, it's several little
    // ..squares (like a big checkerboard), and I'm trying to determine
    // ..in which square the user tapped.
    // At this point. eyesAt is { someX, y, someZ } and lookAt is { someX, 0, someZ } and
    // ..upVector is { 0, 0, -1 }.  We Zoom our view of the board in/out by
    // ..changing eyesAt.y
    float tapZ = (self.eyesAt.y - self.zNear) / (self.zFar - self.zNear);  // % of the Z-depth.  eyesAt.y = distance to geometry.
    GLKVector3 tapPoint3 = GLKVector3Make(tapPoint.x, tapPoint.y, tapZ);
    GLKVector3 tapAt = [MFglkUtils gluUnproject: tapPoint3 inView: self.view withModelMatrix: bd.modelviewMatrix projection: [self projectionMatrix]];
    // etc., snip -- do stuff with the tapAt point.

The problem is: my gluUnproject always returns the geometric point of whatever is in the middle of the screen, regardless of where I tap.

EDIT: The "always near the center" problem is that the returned X/Z values are always very-near zero, and my geometry is centered on the origin.

It looks as if the problem lies with my tapZ value. My understanding is that this should be a value from 0-1, where 0 means "at the nearZ plane" and 1 means "at the farZ plane" and in-between values are a percentage in between. Experimenting with various tapZ values, I've seen:

    // gives very-very small results
    tapZ = (self.eyesAt.y - self.zNear) / (self.zFar - self.zNear); // % deep through the view frustum 

    // gives Z=0, and very small results
    glReadPixels(viewPoint.x, viewPoint.y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &tapZ);

    // gives approximately-correct results
    tapZ = 0.99943;  // found through trial & error

    // gives Z=1, and too-big results
    glReadPixels(viewPoint.x, viewPoint.y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &tapZ);
    tapZ = 1. - tapZ;

Clearly, I need to pass a correct tapZ value, but I don't understand how to arrive at it!

Olie
  • 24,597
  • 18
  • 99
  • 131

1 Answers1

0

Ok, whew! It turns out that the problem was my tapZ value, and the problem is related to the fact that depth-buffer Z values are not linear. (More on that whackiness here.)

At any rate, using glProject() to project a piece of the geometry in question up to the screen (remember, I'm dealing with an overhead view of a flat checkerboard!), gives me the z-value I wanted. The end-result code turned out like this:

- (GLKVector3) vectorFromTapPoint: (CGPoint) tapPoint
{
    tapPoint.y = (self.view.bounds.size.height - tapPoint.y);

    BlockDiagram *bd = [worldObjects objectAtIndex: 0];

    // find Z-depth of center of board
    GLKVector3 v3Zero = { 0, 0, 0 };
    int viewport[4];
    glGetIntegerv(GL_VIEWPORT, viewport);
    GLKVector3 worldZero = GLKMathProject(v3Zero, bd.modelviewMatrix, [self projectionMatrix], viewport);

    // Find tapped point on geometry
    float tapZ = worldZero.z;
    GLKVector3 tapPoint3 = GLKVector3Make(tapPoint.x, tapPoint.y, tapZ);
    GLKVector3 tapAt = [MFglkUtils gluUnproject: tapPoint3 inView: self.view withModelMatrix: bd.modelviewMatrix projection: [self projectionMatrix]];

    return tapAt;
}

and gluUnProject was modified to:

+ (GLKVector3) gluUnproject: (GLKVector3) screenPoint
                     inView: (UIView*) view
            withModelMatrix: (GLKMatrix4) model
                 projection: (GLKMatrix4) projection
{
    int viewport[4];
    glGetIntegerv(GL_VIEWPORT, viewport);

    bool success;
    GLKVector3 result = GLKMathUnproject(screenPoint, model, projection, &viewport[0], &success);

    return result;
}

NOTE: asking glGet*() for the viewport gave the correctly oriented (my app is landscape) values, which also matters!

Olie
  • 24,597
  • 18
  • 99
  • 131