2010-05-24 50 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庫中找到非投影函數以及光線三角相交函數,非常適合開放式開發。