【问题标题】:How can I track a point on a texture in OpenGL ES1?如何在 OpenGL ES1 中跟踪纹理上的点?
【发布时间】:2015-08-20 16:31:28
【问题描述】:

在我的 iOS 应用程序中,我将纹理应用于在 OpenGLES1 中渲染的球体。球体可以由用户旋转。如何在任何给定时间跟踪纹理上给定点在二维空间中的位置?

例如,给定 1000px x 1000px 纹理上的点 (200, 200),我想在我的 OpenGL 视图顶部放置一个 UIButton,以便在操纵球体时跟踪该点。

最好的方法是什么?

在我的第一次尝试中,我尝试使用颜色选择技术,在屏幕外帧缓冲区中有一个单独的球体,该球体在点 (200, 200) 处使用带有红色方块的黑色纹理。然后,我使用glReadPixels() 跟踪红色方块的位置,并相应地移动了我的按钮。不幸的是,由于明显的性能原因,无法获取所有像素数据并每秒对其进行 60 次迭代。我尝试了多种方法来优化这个 hack(例如:只迭代红色像素,每 4 个红色像素迭代一次,等等),但它并没有被证明是可靠的。

我是一个 OpenGL 菜鸟,所以我很感激任何指导。有更好的解决方案吗?谢谢!

【问题讨论】:

  • 你有什么坐标信息。你有球体中心坐标吗?旋转角度?半径?给我一些工作。

标签: ios opengl-es textures framebuffer opengl-es-1.1


【解决方案1】:

我认为跟踪球的位置比使用像素搜索更容易。然后只需使用几个函数将球的坐标转换为视图的坐标(并返回),然后将子视图的中心设置为转换后的坐标。

CGPoint translatePointFromGLCoordinatesToUIView(CGPoint coordinates, UIView *myGLView){

    //if your drawing coordinates were between (horizontal {-1.0 -> 1.0} vertical {-1 -> 1})
    CGFloat leftMostGLCoord       = -1;
    CGFloat rightMostGLCoord      = 1;
    CGFloat bottomMostGLCoord     = -1;
    CGFloat topMostGLCoord        = 1;

    CGPoint scale;
    scale.x = (rightMostGLCoord - leftMostGLCoord) / myGLView.bounds.size.width;
    scale.y = (topMostGLCoord - bottomMostGLCoord) / myGLView.bounds.size.height;


    coordinates.x -= leftMostGLCoord;
    coordinates.y -= bottomMostGLCoord;


    CGPoint translatedPoint;
    translatedPoint.x = coordinates.x / scale.x;
    translatedPoint.y =coordinates.y / scale.y;

    //flip y for iOS coordinates
    translatedPoint.y = myGLView.bounds.size.height - translatedPoint.y;

    return translatedPoint;
}

CGPoint translatePointFromUIViewToGLCoordinates(CGPoint pointInView, UIView *myGLView){

    //if your drawing coordinates were between (horizontal {-1.0 -> 1.0} vertical {-1 -> 1})
    CGFloat leftMostGLCoord       = -1;
    CGFloat rightMostGLCoord      = 1;
    CGFloat bottomMostGLCoord     = -1;
    CGFloat topMostGLCoord        = 1;

    CGPoint scale;
    scale.x = (rightMostGLCoord - leftMostGLCoord) / myGLView.bounds.size.width;
    scale.y = (topMostGLCoord - bottomMostGLCoord) / myGLView.bounds.size.height;

    //flip y for iOS coordinates
    pointInView.y = myGLView.bounds.size.height - pointInView.y;

    CGPoint translatedPoint;
    translatedPoint.x = leftMostGLCoord + (pointInView.x * scale.x);
    translatedPoint.y = bottomMostGLCoord + (pointInView.y * scale.y);

    return translatedPoint;
}

在我的应用程序中,我也选择使用 iOS 坐标系进行绘图。我只是将投影矩阵应用于我的整个 glkView 来协调坐标系。

static GLKMatrix4 GLKMatrix4MakeIOSCoordsWithSize(CGSize screenSize){
    GLKMatrix4 matrix4 = GLKMatrix4MakeScale(
                                             2.0 / screenSize.width,
                                             -2.0 / screenSize.height,
                                             1.0);
    matrix4 = GLKMatrix4Translate(matrix4,-screenSize.width / 2.0, -screenSize.height / 2.0, 0);

    return matrix4;
}

这样你就不用翻译任何东西了。

【讨论】:

  • 这很有帮助,除了我不是在跟踪球体的位置,而是在球体旋转时跟踪球体上的一个点。我想我可以做类似的事情,但我可能需要一些数学帮助......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-09
  • 1970-01-01
  • 2014-04-23
  • 2011-08-11
  • 2013-04-07
  • 1970-01-01
相关资源
最近更新 更多