关于NinevehGL框架中导入的三维模型的触摸事件

时间:2012-12-05 08:58:32

标签: iphone ios opengl-es 3d-rendering

我正在使用以下代码旋转三维模型:

_mesh = [[NGLMesh alloc] initWithFile:@"01.obj" settings:settings delegate:self];

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

    [super touchesMoved:touches withEvent:event];

    UITouch *touch;
    CGPoint pointA,pointB;

    if ([touches count]== 1) {

        touch = [[touches allObjects]objectAtIndex:0];
        pointA = [touch locationInView:self.view];
        pointB = [touch previousLocationInView:self.view];
        //      _mesh.rotateY -= (pointA.x - pointB.x) * 0.5f;
        //      _mesh.rotateX -= (pointA.y - pointB.y) * 0.5f;

        _mesh.rotateY += (pointA.x - pointB.x) * 0.5f;
    _mesh.rotateX += (pointA.y - pointB.y) * 0.5f;
    }
}

代码按照预期旋转,但无论我在视图中的哪个位置旋转,它都会旋转。我希望它只在触摸导入的模型内部时旋转。我该怎么做?

2 个答案:

答案 0 :(得分:1)

为此,您必须使用 {{1}中的 CGRectContainsPoint 手动获取3d模型对象框(对应于屏幕)并与当前触摸位置进行比较委托。

仅在结果touchesMoved返回YES时编写代码 _mesh.rotate

注意: NinevehGL有own forum来讨论与框架相关的事情。请在那里发布查询以获得良好和快速的回​​复。

答案 1 :(得分:1)

以下代码可能会在某种程度上帮助您,

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{  
[super touchesMoved:touches withEvent:event];

UITouch *touch;
CGPoint pointA,pointB;
if ([touches count]== 1)
{
    touch = [[touches allObjects]objectAtIndex:0];
    pointA = [touch locationInView:self.view];
    pointB = [touch previousLocationInView:self.view];

    NGLTouching touchingStruct = [_camera touchingUnderPoint: pointA];

    if (touchingStruct.mesh)   
    {
        _mesh.rotateY += (pointA.x - pointB.x) * 0.5f;
        _mesh.rotateX += (pointA.y - pointB.y) * 0.5f;
    }
}

但它仍然不会改变网格的边界框。您可以参考this博文。