如何在OpenGL ES1中跟踪纹理上的一个点?

4
在我的iOS应用程序中,我有一个纹理应用于在OpenGLES1中呈现的球体。用户可以旋转球体。如何跟踪给定点在任何时候的2D空间中的位置?
例如,给定一张1000px x 1000px的纹理上的点(200, 200),我想在我的OpenGL视图上方放置一个UIButton来跟踪该点,当球体被操作时,按钮也会移动。
做到这一点的最佳方法是什么?
在第一次尝试中,我尝试使用颜色拾取技术,在屏幕外的帧缓冲区中使用黑色纹理和在点(200,200)处有一个红色正方形的单独球体。然后,我使用glReadPixels()跟踪红色正方形的位置,并相应地移动我的按钮。不幸的是,由于明显的性能原因,抓取所有像素数据并对其进行60次迭代是不可能的。我尝试了许多优化此黑客的方法(例如:只迭代红色像素,每4个迭代一个红色像素等),但没有证明是可靠的。
作为OpenGL新手,我需要指导。是否有更好的解决方案?谢谢!

你有什么坐标信息?你有球心坐标吗?旋转角度?半径?给我一些可以使用的信息。 - dave234
1个回答

0

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

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,即可调和坐标系统。
(在我的应用程序中,我选择使用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;
}

这样您就不必翻译任何内容。


这很有用,但是我不是在跟踪球体的位置,而是在跟踪球体旋转时球体上的一个点。不过我想我可以做类似的事情,但我可能需要一些数学方面的帮助... - user2393462435

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接