iPhone OpenGL:使用gluUnProject(端口)并检测对对象的单击 [英] iPhone OpenGL : Using gluUnProject (port) and detecting clicking on an object

查看:52
本文介绍了iPhone OpenGL:使用gluUnProject(端口)并检测对对象的单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助,这是我的第四个问题,我非常努力地尝试了一切!

please help, this is my 4th question about this, I trying so hard I tried everything!

我要做的就是检测单击3D世界(已创建3D世界)中的对象(多维数据集).
做到了 http://blog.nova- box.com/2010/05/iphone-ray-picking-glunproject-sample.html ,但应用程序结构完全不同,并且在渲染缓冲区等方面有很多用途.

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.

我正在尝试使用gluUnProject(有人移植了它)使用.

I am trying to use using gluUnProject (someone has ported it).

触摸

    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; 
} 

如果有人可以解决错误,我什至会支付一些现金(如果允许的话),这让我头疼了好几天.当我获得投影和Modelview矩阵时,我认为这应该做

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

推荐答案

我不确定,但是看来坐标系是错误的.我使用(用于2D)是这样的:

I cant't be sure, but It seems that the coordinate system is wrong. I use (for 2D) something like this:

- (CGPoint) unProject: (CGPoint) point
{
    GLfloat x=0, y=0, z=0;
    GLfloat modelMatrix[16]; 
    GLfloat projMatrix[16];
    GLint viewport[4];
    glGetFloatv(GL_MODELVIEW_MATRIX, modelMatrix);
    glGetFloatv(GL_PROJECTION_MATRIX, projMatrix);
    glGetIntegerv(GL_VIEWPORT, viewport);

    CGPoint result = { 0.0f/0.0f, 0.0f/0.0f };
    if(gluUnProject(point.x, size.height-point.y, 0, modelMatrix, projMatrix, viewport, &x, &y, &z) == GL_TRUE) { 
        result.x = x;
        result.y = y;
        //Since we're using an ortho projection, we can ignore z.
        UNUSED(z);
    }
    return result;
}

请注意:size.height-point.y,其中大小包含屏幕的size.您必须记住,iOS的Core Animation中屏幕的默认坐标系与OpenGL使用的默认坐标系不同.

Note this: size.height-point.y where size contains the size 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.

我使用以下gluUnProject实现: http://www.codng.com/2011/02/gluunproject-for -iphoneios.html (免责声明:这是我的个人博客).

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.

这篇关于iPhone OpenGL:使用gluUnProject(端口)并检测对对象的单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆