OpenGl坐标系不在-1到1 [英] OpenGl coordinate system is not at -1 to 1

查看:204
本文介绍了OpenGl坐标系不在-1到1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用OpenGl和C ++创建一个基本的游戏,并且想要制作一个这样的游戏,以便当玩家到达屏幕边缘时他们不能再移动了.我无法确定屏幕边缘在哪里.我知道Windows通常具有1到-1之间的系统,但我的似乎更像是0.63至-0.63.播放器在屏幕上显示为一个具有x,y和z位置的框,但只能在2D空间中移动.

I am creating a basic game in OpenGl and C++ and want to make it so that when the player gets to the edge of the screen they can't move any further. I am having trouble working out where the edge of the screen is. I know that windows normally have a system between 1 and -1, but mine seems to be more like 0.63 to -0.63. The player is shown as a box on the screen which has an x, y, and z location, but it will only move in 2D space.

我想更改范围,使它们在-1和1之间,而不是一个奇数值.

I want to change the bounds so that they are between -1 and 1, not a odd value.

我该怎么做?

代码已上传到 http://pastebin.com/jxd5YhHa .

推荐答案

如果您不打算动态更改投影矩阵,那么最简单的方法就是调用

If you aren't going to be dynamically changing your projection matrix, the easiest thing to do would be to call

glScalef(.63f,.63f,1);

glScalef(.63f,.63f,1);

在您的投影矩阵上.

然后您可以基于这些值限制移动.

You can then restrict movement based on these values.

要随时计算世界空间坐标,应使用gluUnProject. 假设"x"和"y"分别是窗口的宽度和高度(您通过gluPerspective传递的值),则可以找到世界空间坐标,如下所示:

To compute the world space coordinates at any time you should make use of gluUnProject. assuming 'x' and 'y' are the width and height of your window respectively (the values you pass gluPerspective) you can find the world space coordinates like so:

double world_llx,world_lly,world_llz;
//world coordinates of lower left corner of window
gluUnProject(0, 0, 0, view_mat, proj_mat, viewport,&world_llx,&world_lly,&world_llz);
//world coordinate of upper right corner of window
double world_urx,world_ury,world_urz;
gluUnProject(x,y,0,view_mat,proj_mat,viewport,&world_urx,&world_ury,&world_urz);

view_mat是您的视图矩阵. proj_mat是您的投影矩阵.您可以使用带有GL_MODELVIEW_MATRIX和GL_PROJECTION_MATRIX的glGetDouble *来获得这两者.

view_mat is your view matrix. proj_mat is your projection matrix. You can get both of these using glGetDouble* with GL_MODELVIEW_MATRIX and GL_PROJECTION_MATRIX.

viewport参数可能与您的窗口具有相同的尺寸.无论如何,这是您使用glViewport设置的.

The viewport parameter will probably have the same dimensions as your window. In any event, this is what you set with glViewport.

这假设您的XZ平面位于z == 0.

This assumes your XZ plane is at z == 0.

这篇关于OpenGl坐标系不在-1到1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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