2014-04-14 9 views
1

에 탭을 검출, 내가 있고, 내가 그것을 얻 기운, GLKit 당신이GLKMathUnproject는 : 오브젝트 그래서

그러나 (멋진) 3D 공간에서 클릭 한 위치 해결하는 데 사용할 수있는 GLKMathUnproject을 것 같다 몇 가지 예제를 복사했는데 여전히 0,0,0에서 내 큐브를 클릭하는 것을 감지하지 못합니다. 기본적으로 다음 루프를 수행하고 내 레이가 내 큐브에 닿는 지 확인합니다.

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    NSSet* allTouches = [event allTouches]; 
    UITouch*touch1 = [[allTouches allObjects] objectAtIndex:0]; 
    CGPoint touch1Point = [touch1 locationInView:self.view]; 


    GLKVector3 window_coord = GLKVector3Make(touch1Point.x,touch1Point.y, 0.0f); 
    bool result; 

    GLint viewport[4] = {}; 
    glGetIntegerv(GL_VIEWPORT, viewport); 

    GLKVector3 near_pt = GLKMathUnproject(window_coord, _baseModelViewMatrix, _projectionMatrix, &viewport[0], &result); 

    window_coord = GLKVector3Make(touch1Point.x,touch1Point.y, 1.0f); 

    GLKVector3 far_pt = GLKMathUnproject(window_coord, _baseModelViewMatrix, _projectionMatrix, &viewport[0], &result); 

    //need to get z=0 from 
    //assumes near and far are on opposite sides of z=0 
    float z_magnitude = fabs(far_pt.z-near_pt.z); 
    float near_pt_factor = fabs(near_pt.z)/z_magnitude; 
    float far_pt_factor = fabs(far_pt.z)/z_magnitude; 

    GLKVector3 final_pt = GLKVector3Add(GLKVector3MultiplyScalar(near_pt, far_pt_factor), GLKVector3MultiplyScalar(far_pt, near_pt_factor)); 


    float xDif = (final_pt.x - near_pt.x)/1000; 
    float yDif = (final_pt.y - near_pt.y)/1000; 
    float zDif = (final_pt.z - near_pt.z)/1000; 

    for (int i = 0; i < 100; i ++) 
    { 
     if ((near_pt.x + (xDif * i)) > self.cube.position.x - self.cube.scale.x && (near_pt.x + (xDif * i)) < self.cube.position.x + self.cube.scale.x && 
      (near_pt.y + (yDif * i)) > self.cube.position.y - self.cube.scale.y && (near_pt.y + (yDif * i)) < self.cube.position.y + self.cube.scale.y && 
      (near_pt.z + (zDif * i)) > self.cube.position.z - self.cube.scale.z && (near_pt.z + (zDif * i)) < self.cube.position.z + self.cube.scale.z) 
     { 
      NSLog(@"%f %f %f", final_pt.x, final_pt.y, final_pt.z); 
      NSLog(@"Hit cube"); 
     } 

    } 

} 

답변

2

나는 여기에 대답 Updating OpenGL ES Touch Detection (Ray Tracing) for iPad Retina?

- (void)handleTap: (UITapGestureRecognizer *)recognizer 
{ 

    CGPoint tapLoc = [recognizer locationInView:self.view]; 
    tapLoc.x *= [UIScreen mainScreen].scale; 
    tapLoc.y *= [UIScreen mainScreen].scale; 

    bool testResult; 

    GLint viewport[4]; 
    glGetIntegerv(GL_VIEWPORT, viewport); 

    float uiKitOffset = 113; //Need to factor in the height of the nav bar + the height of the tab bar at the bottom in the storyboard. 

    GLKVector3 nearPt = GLKMathUnproject(GLKVector3Make(tapLoc.x, (tapLoc.y-viewport[3]+uiKitOffset)*-1, 0.0), modelViewMatrix, projectionMatrix, &viewport[0] , &testResult); 

    GLKVector3 farPt = GLKMathUnproject(GLKVector3Make(tapLoc.x, (tapLoc.y-viewport[3]+uiKitOffset)*-1, 1.0), modelViewMatrix, projectionMatrix, &viewport[0] , &testResult); 

    farPt = GLKVector3Subtract(farPt, nearPt); 

    .... 
} 
발견