在OpenGl中检测图像纹理上的触摸



我正在尝试使用openGL开发一款游戏,我使用GLTextureLoader类加载图像,这些精灵以一定的计算速度从左向右移动,我需要检测这些图像上的触摸。

因为你的目的很简单,所以你所要做的就是在不可见的缓冲区上画两次你拥有的任何对象,一个是可见的,另一个是只有颜色的。然后检查用户在不可见缓冲区中按下的位置,看看它是什么颜色,你就有了对象。

http://www.lighthouse3d.com/opengl/picking/index.php3?color1

这是基本理论。

OpenGL是一个渲染API。它只画东西。像lighthouse3d这样的技术确实有效,但glReadPixels速度较慢。

你应该在CPU上检查这个;也就是说,对于每个绘制的精灵,测试触摸位置是否在里面。

根据我的要求,我找到了如何做到这一点,正如我所说,我不是openGL的专家,但我设法做到了。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[touches allObjects] objectAtIndex:0];
CGPoint touchLocation = [touch locationInView:self.view];
touchLocation = touchLocation = CGPointMake(touchLocation.x, 320 - touchLocation.y);
//self.player.moveVelocity = GLKVector2Make(50, 50);
//NSLog(@"Location in view %@", NSStringFromCGPoint(touchLocation));
//NSLog(@"bounding box is %@",NSStringFromCGRect([self.player boundingBox]));
GameSprite *temp;
for (GameSprite *tempSprite in self.children) {
    if (CGRectContainsPoint([tempSprite boundingBox], touchLocation)) {
        NSLog(@"touched the player");
        temp =tempSprite;
    }  
}
[self.children removeObject:temp];
}
- (CGRect)boundingBox {
CGRect rect = CGRectMake(self.position.x, self.position.y, self.contentSize.width, self.contentSize.height);
return rect;
}

最新更新