glOrtho 制作顶视图的正确参数是什么?
我尝试通过使用 Visual C++ 创建某种 3D Modeler 应用程序来学习 OpenGL。屏幕将分为三个窗口:透视、正面和顶部(等距)。 我几乎放弃了实现顶视图的尝试。这是我当前的顶视图代码:
glOrtho(-1,1,-1,1,1,3.5f);
gluLookAt(
0,2,0,//eye
0,0,0,//center
0,0,-1);//up
但是如果我将对象移动到远 Z,它肯定会消失。如何使顶视图能够看到场景中的所有对象???
如何计算 glOrtho 和 glLookAt 所需的参数?
I try to learn OpenGL by creating some sort of 3D Modeler application using Visual C++. The screen will be divied into three window : perspective, front and top (isometric).
And I almost give up try to implement the top view. This is my current top view code :
glOrtho(-1,1,-1,1,1,3.5f);
gluLookAt(
0,2,0,//eye
0,0,0,//center
0,0,-1);//up
But if I move the object to to the far Z, it will certainly disappear. How can I make the top view able to look at all object in the scene???
How can I compute the required parameter fo glOrtho and glLookAt?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不应该使用 gluLookAt(请注意“glu”中的“u”,gluLookAt 不是 OpenGL 的一部分),而是直接设置模型视图矩阵。首先,您必须定义远处和侧面发生了什么。大多数使用 OpenGL 的人更喜欢右手坐标系 (RHCS)。第一个底座是 X,第二个是 Y,第三个是 Z。在 RHCS 中,X 是侧向(向右),Y 是向远方,Z 是向上。 OpenGL的默认投影是,就好像相机以负方向向下看Z轴,即向下看。
所以我们可以说矩阵
是向下看的。眺望远方,你不得不仰起头来。 X 保持不变,但 Y 和 Z 交换。与此相对应的矩阵为
向侧面看,全局 Y 映射到视图 X,全局 X 映射到 Z,全局 Z 映射到 Y,结果是
现在您可能想要平移和缩放该视图。您可以使用 glScale 和 glTranslate 或直接在矩阵中执行此操作。
现在,在正交投影中沿 Z 方向平移几乎没有意义。所以你可以将 pan.z 保留为零。
现在让我们解决您的深度裁剪问题。正投影的优点之一是,深度剪辑范围将线性映射到深度缓冲区,而在透视投影中,它遵循 1/x 定律。远距离和近距离的混合符号也是完全允许的,并且也没有 |near| 的限制。 < |远|。因此,对于每个视图,您所要做的就是确定所有对象变换顶点的最小和最大位置,并使用它们在每个视图的投影中设置适当的剪辑值。一个很好的近似,这样您就不必迭代每个顶点,使用边界球体和位置的对象(将边界球体半径添加/减去其位置)来获取最小/最大范围。
You should not use gluLookAt (note the 'u' in 'glu', gluLookAt is not a part of OpenGL), but set the modelview matrices directly. First you must define what's up, far and sideways. Most people using OpenGL prefer a right handed coordinate system (RHCS). The first base is X, the second Y and the third Z. In a RHCS X is sideways (to the right), Y is into the far and Z is upwards. The default projection of OpenGL is, as if the camera looks down the Z axis in negative direction, i.e. the look goes down.
So we can say the matrix
is looking down. Looking into the far you've to yaw up. The X remains the same, but Y and Z are swapped. The matrix corresponding to this would be
And looking sideways the global Y maps to the view X, global X maps to Z and global Z maps to Y resulting in
Now you probably want to pan and zoom that view. You can do this either using glScale and glTranslate, or directly in the matrix.
Now, panning in the Z direction makes little sense in an orthographic projection. So you can leave pan.z zero.
Now lets address your depth clipping problem. One of the nice properties of othographic projection is, that the depth clip-range will be mapped linearily into depth buffer, whereas in perspective projection it follows a 1/x law. Also mixed signs for the far and near distance are perfectly allowed and there is also no constraint that |near| < |far|. So for each of your views all you've to do is determine the minimum and maximum positions of all objects' transformed vertices and use those to set the apropriate clip values in each view's projection. A good approximation, so that you don't have to iterate over each and every vertex is using the objects bounding spheres and positions (add/subract the bounding sphere radius to it's position) to get min/max ranges.
您需要根据您的几何形状调整 z far 值。您应该计算模型的边界球或边界框,找到 Z 范围并根据它调整相机。
You need to adapt the z far value according to your geometry. You should compute a bounding sphere or bounding box of your model, find the Z range and adapt your camera according to it.
glm(GL数学,http://glm.g-truc.net/)可能就是datenwolf写答案时正在寻找。这提供了 GLU 的设施,但旨在与现代 GL 一起使用。例如,有一个 glm::ortho 函数。例如,这是 glm,用于使用正交视图通过 GLM 计算和设置顶点着色器统一的模型-视图-投影矩阵:
glm (GL mathematics, http://glm.g-truc.net/) is probably what datenwolf was looking for at the time the answer was written. This provides the facilities of GLU but intended to work with modern GL. For example, there is a glm::ortho function. For example, this is glm used to compute and set the model-view-projection matrix for a vertex shader uniform using an orthographic view, with GLM: