如何设置在OpenGL中显示的音量 [英] How to set up what volume is displayed in opengl

查看:85
本文介绍了如何设置在OpenGL中显示的音量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试显示特定的空间量,即从10,10,10到30,30,30的立方体.从我阅读的所有内容中,我都会这样做:

I'm trying to display a particular volume of space, the cube from 10,10,10 to 30,30,30. From everything I read, I'd do this like so:

glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(10, 30, 10, 30, 10, 30);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(30.0f, 30.0f, 30.0f,
    20.0f, 20.0f, 20.0f,
    0.0f, 1.0f, 0.0f);

glColor3f(0.0, 0.0, 0.0);  //color = black
glPushMatrix();               
    glTranslated(20,20,20);
    glutSolidSphere(.3, 50, 50);
glPopMatrix();

glOrtho设置我可以看到的音量,gluLookAt表示相机位于30,30,30,指向20,20,20.但这是行不通的.在一些帮助和一些帮助下,我设法通过以下代码使此工作正常进行:

glOrtho sets up what volume I can see, and gluLookAt says that the camera is at 30,30,30 and is pointed at 20,20,20. But this doesn't work. With some help and some playing around, I managed to get this working, with this code:

glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(10, 30, 10, 30, -1000, 1000);  //CHANGED THESE BOUNDS

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslated(20,20,20);                //ADDED THIS LINE 
gluLookAt(30.0f, 30.0f, 30.0f,
    20.0f, 20.0f, 20.0f,
    0.0f, 1.0f, 0.0f);


glPushMatrix();               
    glTranslated(20,20,20);
    glutSolidSphere(.3, 50, 50);
glPopMatrix();

为什么我需要这些更改?为什么第一个版本的代码无法正常工作?

Why do I need those changes? Why doesn't the first version of the code work?

推荐答案

此代码

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(10, 30, 10, 30, 10, 30);

将建立一个矩阵,将长方体x = [10,30],y = [10,30]和z = [-10,-30]转换为[-1,1] ^ 3剪辑空间体积(这是OpenGL的查看体积).注意这里的负号.

will set up a matrix which will transform the cuboid x=[10,30], y=[10,30] and z=[ -10, -30] to the [-1,1]^3 clip space volume (which is OpenGL's viewing volume). Note the negative sign here.

此代码

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(30.0f, 30.0f, 30.0f,
    20.0f, 20.0f, 20.0f,
    0.0f, 1.0f, 0.0f);

设置(-30,.- 30,-30)所有对象的平移并旋转一些以查看您指定的方向.

set up a translation of all objects of (-30,. -30, -30) and some rotation to look into the direction you specified.

两个矩阵都将被应用,首先是ModelView进入眼睛空间,而不是Projection矩阵进入剪辑空间.因此,现在,如果您想以全球世界空间"来思考,那么最终得到的视线就是宽,高和深的20个单位,但您却不在真正期望的位置.它将以您设置为视图方向轴的位置后面 10个单位开始.并沿着相机的本地x和y轴移动-您指定相机要查看的点甚至不会出现在屏幕上.

Both matrices will be applied, first the ModelView to get into eye space, than the Projection matrix to get into clip space. So now, if you want to think in terms of a global "world space", you end up with a viewing volume which is the 20 units wide, high and deep, but lies somewhere you don't really expect it. It will begin 10 units behind at what you set as the view direction axis. and also be shifted along the camera's local x and y axis - the point you specified your camera to look at will not even end up on the screen.

如果您只想查看[10,30] ^ 3的观看量,则只需使用

If you just wanted the [10,30]^3 viewing volume, you could simply use

glOrtho(10, 30, 10, 30, -10, -30);

一起删除该gluLookAt() .至少在简单的情况下,这是可行的,但是由于相机"是原始的,因此可能会增加照明和雾气.您可能想要设置一些对称的查看音量,以便实际上可以进行任何合理的摄像头设置.

and remove that gluLookAt() alltogether. This will at least work for simple cases, but it might screw up lighting and fog, since the "camera" is now at original. You probably want to set up some symmetric viewing volume, so that you actually can have any sane camera setup.

编辑

为了使事情更清楚:您在投影矩阵中定义的查看体积相对于该设置中的视图转换相对进行了定义.因此,例如,您可能想要一个20x20x20大小的查看体积,其中您的查看轴位于对象的中心,并显示直接位于相机前面的体积:

To make things more clear: the viewing volume you define in the projection matrix is defined relative to the view transformation in that setup. So, you might for examle want to a viewing volume of size 20x20x20, where your viewing axis lies in the center of the porjection, and showung the volume which lies directly in front of the camera:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-10, 10, -10, 10, 0, 20);

现在,您可以像以前一样设置相机位置: glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(30.0f,30.0f,30.0f, 20.0f,20.0f,20.0f, 0.0f,1.0f,0.0f);

Now you can set up your camera location as before: glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(30.0f, 30.0f, 30.0f, 20.0f, 20.0f, 20.0f, 0.0f, 1.0f, 0.0f);

现在,点(20,20,20)(绘制球体的位置)实际上将投影到屏幕的中心.但是,它不会位于观看区的中心,因为它沿当前观看轴的范围是0到20个单位,但是(20,20,20)距离当前相机位置(30, 30,30).所以你可以将相机设置为 gluLookAt(25.774f,25.774f,25.774f, 20.0f,20.0f,20.0f, 0.0f,1.0f,0.0f);

Now, the point (20,20,20) (where you draw your sphere) will actually be projected to the center of the screen. However, it will be not at the center of the viewing voulme, since it wall range from 0 to 20 units along the current viewing axis, but (20,20,20) is 17.32 units away from the current camera position of (30,30,30). So you could set the camera to gluLookAt(25.774f, 25.774f, 25.774f, 20.0f, 20.0f, 20.0f, 0.0f, 1.0f, 0.0f);

现在,该对象位于查看区域的中心.

Now, the object is exaclty in the center of the viewing volume.

这篇关于如何设置在OpenGL中显示的音量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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