在 OpenGL ES 中,如何将屏幕坐标转换为世界坐标? [英] In OpenGL ES, how do I convert screen coordinates to world coordinates?

查看:17
本文介绍了在 OpenGL ES 中,如何将屏幕坐标转换为世界坐标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是针对 2D 游戏的,因此只有 x 和 y 轴.游戏在 iPhone 上处于横向模式.我希望能够设置渲染纹理的屏幕 x 和 y 坐标.

This is for a 2D game so there are only an x and y axis. The game is in landscape mode on the iPhone. I wish to be able to set the screen x and y coordinates of where to render a texture.

推荐答案

如果你在做一个 2D 游戏,你可以设置你的投影和模型视图矩阵,这样你根本不需要转换:

If you're doing a 2D game, you can set up your projection and model-view matrices so that you don't need to convert at all:

// This goes in your init code somewhere
// Set up 480x320 orthographic projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrthof(-240.0f, 240.0f, -160.0f, 160.0f, -1.0f, 1.0f);

// Rotate into landscape mode
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(-90.0f, 0.0f, 1.0f, 0.0f);

这使得世界坐标 (-240, -160) 映射到屏幕的左上角(横向模式下为左下角),(240, 160) 映射到右下角(右上角在横向)等.由于 iPhone 的屏幕是 480x320,因此您不需要使用这样设置的矩阵在世界坐标和屏幕坐标之间进行转换.

This makes it so that world coordinates (-240, -160) maps to the top-left of the screen (bottom-left in landscape mode), (240, 160) maps to the bottom-right (top-right in landscape), etc. Since the iPhone's screen is 480x320, you won't need to convert between world and screen coordinates with your matrices set up thusly.

当然,如果您希望能够移动相机,那么您需要根据相机的位置进行偏移.

Of course, if you want to be able to move the camera, then you'll need to offset by the camera's location.

这篇关于在 OpenGL ES 中,如何将屏幕坐标转换为世界坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆