将屏幕坐标转换为世界坐标

发布于 2025-01-06 22:12:14 字数 618 浏览 1 评论 0原文

我使用这个获取屏幕坐标:

@Override
public boolean onTouchEvent(MotionEvent ev) {
x = ev.getX(0);
y = ev.getY(0);
return true;
}

这些是我的 openGL 1.0 正方形的顶点:

private float vertices[] = {
        -1.0f, -1.0f,  0.0f,        // V1 - bottom left
        -1.0f,  1.0f,  0.0f,        // V2 - top left
         1.0f, -1.0f,  0.0f,        // V3 - bottom right
         1.0f,  1.0f,  0.0f         // V4 - top right
};

使用过 openGL 的每个人都知道,如果我粘贴 x 和 y 变量而不是顶点,我会得到绝对的废话。我的问题是:我应该使用什么公式将屏幕坐标 x 和 y 转换为世界坐标,以便我可以使用它们将我的方块定位到触摸点?

编辑:

哎呀,我忘了说,这是一款 2D 游戏......

I'm getting screen coordinates using this:

@Override
public boolean onTouchEvent(MotionEvent ev) {
x = ev.getX(0);
y = ev.getY(0);
return true;
}

And these are the verticles of my openGL 1.0 square:

private float vertices[] = {
        -1.0f, -1.0f,  0.0f,        // V1 - bottom left
        -1.0f,  1.0f,  0.0f,        // V2 - top left
         1.0f, -1.0f,  0.0f,        // V3 - bottom right
         1.0f,  1.0f,  0.0f         // V4 - top right
};

Everybody who have worked with openGL knows, that if i would paste x and y variables instead of verticles, i would get absolute nonsense. My question is: what formula should i use to convert screen coordinates x and y to world coordinates so i could use them to position my square to the touched point?

EDIT:

Oops, i forgot to say, that it's a 2D game...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

满栀 2025-01-13 22:12:14

实际上我自己找到了一种方法,而 glUnProject 并不是 android 平台上最好的方法......
http://magicscrollsofcode.blogspot.com/2010/10/3d -picking-in-android.html

Actualy i found a way myself, and the glUnProject is not the best way on android platform...
http://magicscrollsofcode.blogspot.com/2010/10/3d-picking-in-android.html

云淡月浅 2025-01-13 22:12:14

有一个名为“gluunproject”的函数可以为您完成此操作。链接在这里。

http://www.opengl.org/sdk/docs/man/xhtml /gluUnProject.xml

顺便说一句,屏幕坐标将对应于从相机中心穿过屏幕坐标(图像平面)的 3D 线。

ModelView、投影和视口输入可以通过查询 OpenGL 当前矩阵来获得。请参阅相同的链接(指定了函数调用)。

除了 x 和 y 屏幕参数之外,您还需要深度参数或 z 参数。您可以使用深度范围将正方形放置在特定的 z 平面中。或者给一个默认值。但要确保它在可见区域内。

收到对象坐标后,将其视为正方形的中心并绘制所需长度的正方形。

萨蒂什

There is a function called 'gluunproject', that can do this for you. Link is here.

http://www.opengl.org/sdk/docs/man/xhtml/gluUnProject.xml

By the way, the screen coordinates will correspond to a 3D line passing from center of camera through the screen coordinates (image plane).

The ModelView, projection and viewport inputs can be obtained by querying OpenGL the current matrices. Refer the same link (function calls are specified).

Other than the x and y screen parameters, you need the depth parameter or z parameter. You can use the depth range to place the square in a particular z plane. Or give a default value. But make sure it is inside the visible region.

Once you receive the object co-ordinates, consider it as the center of square and draw a square of required length.

Satish

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文