2010-05-24 22 views
2

首先是我的touchesBegan函数,然后是存储我的对象的值的结构。我有这些对象的数组,我试图弄清楚当我触摸屏幕时,如果我触摸屏幕上的对象。Opengl Iphone SDK:如何判断您是否触摸屏幕上的对象?

我不知道是否需要通过遍历所有对象并确定是否以这种方式触摸对象或者可能存在更简单更有效的方式来执行此操作。

这是如何处理的?

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 

    [super touchesEnded:touches withEvent:event]; 
    UITouch* touch = ([touches count] == 1 ? [touches anyObject] : nil); 

    CGRect bounds = [self bounds]; 
    CGPoint location = [touch locationInView:self]; 
    location.y = bounds.size.height - location.y; 


    float xTouched = location.x/20 - 8 + ((int)location.x % 20)/20; 
    float yTouched = location.y/20 - 12 + ((int)location.y % 20)/20; 
} 



typedef struct object_tag    // Create A Structure Called Object 
{ 
    int tex;  // Integer Used To Select Our Texture 
    float x;  // X Position 
    float y;  // Y Position 
    float z;  // Z Position 
    float yi;  // Y Increase Speed (Fall Speed) 
    float spinz; // Z Axis Spin 
    float spinzi; // Z Axis Spin Speed 
    float flap;  // Flapping Triangles :) 
    float fi;  // Flap Direction (Increase Value) 
} object; 
+1

看看iPhone是否支持glRenderMode(GL_SELECT)。如果是这样,谷歌glSelectBuffer文档和教程。 – SigTerm 2010-05-24 02:57:25

+0

很感谢。我会去做。 – TheGambler 2010-05-24 13:06:27

+1

glRenderMode似乎没有被iPhone SDK支持。 – TheGambler 2010-05-25 13:37:34

回答

4

有几种方法可以做到这一点。

  1. 可以执行使用独特的色彩描绘的实所有对象的场景额外的绘图,然后使用glReadPixels读取选定像素的颜色,并将其与对象相关联。

  2. 使用类似unproject的东西来获取场景中的光线,然后找到光线三角形的交点。可以在glm库中找到非投影函数以及光线三角相交函数,非常适合开放式开发。