gpt4 book ai didi

ios - 如何在 OpenGL ES1 中跟踪纹理上的点?

转载 作者:可可西里 更新时间:2023-11-01 06:14:17 25 4
gpt4 key购买 nike

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

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

执行此操作的最佳方法是什么?

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

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

最佳答案

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

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;
}

这样您就无需翻译任何内容。

关于ios - 如何在 OpenGL ES1 中跟踪纹理上的点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30671349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com