Skip to content

Commit

Permalink
2D screen point from 3D world point, closes #19, thanks @autresphere !
Browse files Browse the repository at this point in the history
  • Loading branch information
mayakraft committed Feb 2, 2015
1 parent d19b661 commit 992e0e6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
18 changes: 13 additions & 5 deletions Panorama/PanoramaView.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,23 @@

/// Dynamic overlay of latitude and longitude intersection lines for all touches
@property (nonatomic) BOOL showTouches;

/**
* Convert a 3D world-coordinate (specified by a vector distance from origin) to a 2D on-screen coordinate
*
* @param GLKVector3 coordinate location from origin. Use with CGRectContainsPoint( [[UIScreen mainScreen] bounds], screenPoint )
* @return a screen pixel coordinate representation of a 3D world coordinate
*/
-(CGPoint)screenLocationFromVector:(GLKVector3)vector;

/**
* Converts screen coordinate to image pixel coordinate
* Converts a 2D on-screen coordinate to a pixel (x,y) of the loaded panorama image
*
* @param CGPoint device screen coordinate
* @return CGPoint image coordinate in pixels, or betwee 0.0 and 1.0 if no image
* @param CGPoint screen coordinate
* @return CGPoint image coordinate in pixels. If no image, between 0.0 and 1.0
*/
-(CGPoint) imagePixelAtScreenLocation:(CGPoint)point;

/**
* Hit-detection for all active touches
*
Expand All @@ -92,6 +102,4 @@
*/
-(BOOL) touchInRect:(CGRect)rect;


@end

18 changes: 18 additions & 0 deletions Panorama/PanoramaView.m
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,24 @@ -(GLKVector3) vectorFromScreenLocation:(CGPoint)screenTouch inAttitude:(GLKMatri
GLKVector4 vec = GLKMatrix4MultiplyVector4(inverse, screen);
return GLKVector3Normalize(GLKVector3Make(vec.x, vec.y, vec.z));
}
-(CGPoint)screenLocationFromVector:(GLKVector3)vector{
GLKMatrix4 matrix = GLKMatrix4Multiply(_projectionMatrix, _attitudeMatrix);
GLKVector3 screenVector = GLKMatrix4MultiplyVector3(matrix, vector);
return CGPointMake( (screenVector.x/screenVector.z/2.0 + 0.5) * self.frame.size.width,
(0.5-screenVector.y/screenVector.z/2) * self.frame.size.height );
}
-(BOOL)computeScreenLocation:(CGPoint*)location fromVector:(GLKVector3)vector inAttitude:(GLKMatrix4)matrix{
GLKVector4 screenVector;
GLKVector4 vector4;
if(location == NULL)
return NO;
matrix = GLKMatrix4Multiply(_projectionMatrix, matrix);
vector4 = GLKVector4Make(vector.x, vector.y, vector.z, 1);
screenVector = GLKMatrix4MultiplyVector4(matrix, vector4);
location->x = (screenVector.x/screenVector.w/2.0 + 0.5) * self.frame.size.width;
location->y = (0.5-screenVector.y/screenVector.w/2) * self.frame.size.height;
return (screenVector.z >= 0);
}
-(CGPoint) imagePixelFromVector:(GLKVector3)vector{
CGPoint pxl = CGPointMake((M_PI-atan2f(-vector.z, -vector.x))/(2*M_PI), acosf(vector.y)/M_PI);
CGPoint tex = [sphere getTextureSize];
Expand Down

0 comments on commit 992e0e6

Please sign in to comment.