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

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

问题描述

这适用于2D游戏,因此只有x和y轴。 iPhone上的游戏采用横向模式。我希望能够设置渲染纹理的位置的屏幕x和y坐标。

解决方案

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

  //这可以在你的初始代码中找到
//设置480x320正交投影
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrthof(-240.0f,240.0f,-160.0f,160.0f,-1.0f,1.0f);

//旋转到横向模式
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(-90.0f,0.0f,1.0f,0.0f);

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



当然,如果你想移动相机,那么你需要偏移相机的位置。 / p>

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.

解决方案

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

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天全站免登陆