iPhone OpenGL:使用 gluUnProject(端口)并检测对象上的点击
请帮忙,这是我的第四个问题,我很努力,我尝试了一切!
我想做的就是检测 3D 世界(创建的 3D 世界)中对象(立方体)的点击。
这就是http://blog.nova- box.com/2010/05/iphone-ray-picking-glunproject-sample.html 但具有完全不同的应用程序结构,并且在渲染缓冲区等方面做了很多工作。
我正在尝试使用 gluUnProject (有人已经移植它)。
触摸
CGPoint pos = [[touches anyObject] locationInView:self.view];
glGetIntegerv( GL_VIEWPORT, __viewport );
glGetFloatv( GL_MODELVIEW_MATRIX, __modelview );
glGetFloatv( GL_PROJECTION_MATRIX, __projection );
int i = 0;
for (NSDictionary *item in self.players) {
IMPoint3D playerPos;
playerPos.x = [[item objectForKey:@"posX"] floatValue];
playerPos.z = [[item objectForKey:@"posZ"] floatValue];
playerPos.y = 1.0f;
if([self checkCollission:pos object:playerPos])
{
NSLog(@"FIRE I LOVE YOU MAN %i", i);
}
i ++;
}
来自其他项目的
#define RAY_ITERATIONS 1000
#define COLLISION_RADIUS 0.1f
-(Boolean) checkCollission:(CGPoint)winPos object:(IMPoint3D) _object {
winPos.y = (float)__viewport[3] - winPos.y;
Point3D nearPoint;
Point3D farPoint;
Point3D rayVector;
//Retreiving position projected on near plan
gluUnProject( winPos.x, winPos.y , 0, __modelview, __projection, __viewport, &nearPoint.x, &nearPoint.y, &nearPoint.z);
//Retreiving position projected on far plan
gluUnProject( winPos.x, winPos.y, 1, __modelview, __projection, __viewport, &farPoint.x, &farPoint.y, &farPoint.z);
//Processing ray vector
rayVector.x = farPoint.x - nearPoint.x;
rayVector.y = farPoint.y - nearPoint.y;
rayVector.z = farPoint.z - nearPoint.z;
float rayLength = sqrtf(POW2(rayVector.x) + POW2(rayVector.y) + POW2(rayVector.z));
//normalizing ray vector
rayVector.x /= rayLength;
rayVector.y /= rayLength;
rayVector.z /= rayLength;
Point3D collisionPoint;
Point3D objectCenter = {_object.x, _object.y, _object.z};
//Iterating over ray vector to check collisions
for(int i = 0; i < RAY_ITERATIONS; i++)
{
collisionPoint.x = rayVector.x * rayLength/RAY_ITERATIONS*i;
collisionPoint.y = rayVector.y * rayLength/RAY_ITERATIONS*i;
collisionPoint.z = rayVector.z * rayLength/RAY_ITERATIONS*i;
//Checking collision
if([Tools poinSphereCollision:collisionPoint center:objectCenter radius:COLLISION_RADIUS])
{
return TRUE;
}
}
return FALSE;
}
碰撞功能如果有人能解决这个错误,我什至会支付一些现金(如果允许的话),这让我头疼了好几天。我认为当我得到投影和模型视图矩阵时需要做一些事情
please help, this is my 4th question about this, I trying so hard I tried everything!
All I want to do is detect clicking on a object(cube) in a 3D World (3D world created).
This does it http://blog.nova-box.com/2010/05/iphone-ray-picking-glunproject-sample.html but has a completely different app structure and does a lot with render buffers etc.
I am trying to use using gluUnProject (someone has ported it).
On touch
CGPoint pos = [[touches anyObject] locationInView:self.view];
glGetIntegerv( GL_VIEWPORT, __viewport );
glGetFloatv( GL_MODELVIEW_MATRIX, __modelview );
glGetFloatv( GL_PROJECTION_MATRIX, __projection );
int i = 0;
for (NSDictionary *item in self.players) {
IMPoint3D playerPos;
playerPos.x = [[item objectForKey:@"posX"] floatValue];
playerPos.z = [[item objectForKey:@"posZ"] floatValue];
playerPos.y = 1.0f;
if([self checkCollission:pos object:playerPos])
{
NSLog(@"FIRE I LOVE YOU MAN %i", i);
}
i ++;
}
Collision function taken from other project
#define RAY_ITERATIONS 1000
#define COLLISION_RADIUS 0.1f
-(Boolean) checkCollission:(CGPoint)winPos object:(IMPoint3D) _object {
winPos.y = (float)__viewport[3] - winPos.y;
Point3D nearPoint;
Point3D farPoint;
Point3D rayVector;
//Retreiving position projected on near plan
gluUnProject( winPos.x, winPos.y , 0, __modelview, __projection, __viewport, &nearPoint.x, &nearPoint.y, &nearPoint.z);
//Retreiving position projected on far plan
gluUnProject( winPos.x, winPos.y, 1, __modelview, __projection, __viewport, &farPoint.x, &farPoint.y, &farPoint.z);
//Processing ray vector
rayVector.x = farPoint.x - nearPoint.x;
rayVector.y = farPoint.y - nearPoint.y;
rayVector.z = farPoint.z - nearPoint.z;
float rayLength = sqrtf(POW2(rayVector.x) + POW2(rayVector.y) + POW2(rayVector.z));
//normalizing ray vector
rayVector.x /= rayLength;
rayVector.y /= rayLength;
rayVector.z /= rayLength;
Point3D collisionPoint;
Point3D objectCenter = {_object.x, _object.y, _object.z};
//Iterating over ray vector to check collisions
for(int i = 0; i < RAY_ITERATIONS; i++)
{
collisionPoint.x = rayVector.x * rayLength/RAY_ITERATIONS*i;
collisionPoint.y = rayVector.y * rayLength/RAY_ITERATIONS*i;
collisionPoint.z = rayVector.z * rayLength/RAY_ITERATIONS*i;
//Checking collision
if([Tools poinSphereCollision:collisionPoint center:objectCenter radius:COLLISION_RADIUS])
{
return TRUE;
}
}
return FALSE;
}
If someone can work out the error, I will even paypal some cash over (if that's allowed), this has given me a headache for days. I think its something to do when I get the projection and modelview matrix
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不能确定,但坐标系似乎是错误的。我使用(对于 2D)类似的东西:
注意:
size.height-point.y
其中 size 包含屏幕的size
。你必须记住,Core Animation for iOS 中屏幕的默认坐标系与 OpenGL 使用的不同。如果您应用了翻译,则很难弄清楚发生了什么。
我使用 gluUnProject 的实现:
http://www.codng.com/2011/02/gluunproject-for -iphoneios.html
(免责声明:这是我的个人博客)。
我知道这并不能完全回答您的问题,但可能会有所帮助。
I cant't be sure, but It seems that the coordinate system is wrong. I use (for 2D) something like this:
Note this:
size.height-point.y
where size contains thesize
of the screen. You have to remember that the default coordinate system of the screen in Core Animation for iOS is different from the one used by OpenGL.If you have a translation applied, it is very difficult to figure out what's going on.
I use this implementation of gluUnProject:
http://www.codng.com/2011/02/gluunproject-for-iphoneios.html
(disclaimer: it is my personal blog).
I know this doesn't fully answer your question, but it might help.
我将其发布为课程的完整源代码,我确信它与项目矩阵不起作用有关
I posting this as the complete source of the class, I sure its something to do with the project matrix not working