OpenGL中的对象,世界,相机和投影空间 [英] Object, world, camera and projection spaces in OpenGL

查看:119
本文介绍了OpenGL中的对象,世界,相机和投影空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解在OpenGL中创建空间:


  1. 对象空间


  2. 世界空间


  3. 相机空间


  4. space







我对这些阶段的理解是否正确?


  1. 通过输入顶点坐标,直接在程序内部创建立方体,位于笛卡尔坐标系的中心。 p>


  2. 坐标转换为世界内的坐标,这意味着将其移动到屏幕上的任何位置。 $ b

好吧,其实我希望你能检查我对这两个术语的理解。




现在,我正在黑屏上创建一个三角形。



默认情况下,它适用于 GL_MODELVIEW 标志,但这是第二阶段 - 世界空间。这是否意味着调用 glVertex3f()会在对象空间创建一个三角形?



世界空间部分在哪里?






另外,我读过最后两个空格不是openGL管线的一部分)。
但是,OpenGL包含诸如 GL_PROJECTION 之类的标志,例如:

  glMatrixMode(GL_PROJECTION); 
glLoadIdentity();
glViewport(0,0,w,h); //宽度,高度
gluPerspective(45,ratio,1,100); // radio = w / h
glMatrixMode(GL_MODELVIEW);

这段代码做了什么?它设定了角度。它创建z轴吗?但是它不是已经是对象空间的一部分吗?解决方案1)对象空间是对象相对于对象原点的顶点。在1x1x1立方体的情况下,您的顶点应该是:

$ $ $ $ $ $ $ $ $ $($ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 0.5,0.5,0.5)
(0.5,-0.5,0.5)
(-0.5,-0.5,0.5)

等。

<2>世界空间是物体在你的世界中的地方。如果你希望这个立方体在(15,10)上,你可以创建一个平移矩阵,当它与每个顶点相乘时,将顶点居中在(15,10)。例如,第一个顶点将变成(15.5,10.5,0.5)。从对象到世界空间的矩阵称为模型矩阵。


3)眼睛空间(有时称为相机空间)是世界相对于观众。由于这必须是每个顶点相乘的矩阵,因此它在技术上与相机的方向相反。也就是说,如果你需要一个(0,0,-10)的相机,你的视图矩阵必须是(0 ,0,10)。这样,世界上的所有物体都向前移动10个单位,使它看起来像是向后10个单位。


<4>投影空间是我们如何应用正确的视角一个场景(假设你没有使用正交投影)。这几乎总是由一个平截头体来表示,这篇文章可以解释得比我更好。实际上,您将3D空间映射到另一个倾斜的空间。



OpenGL然后处理剪辑空间和屏幕空间。







它默认工作于 GL_MODELVIEW 标志,但这是第二阶段 - 世界空间。这是否意味着调用 glVertex3f()会在对象空间中创建一个三角形?


您始终使用 glVertex3f()在对象空间中设置顶点。 (这实际上是一种非常古老而且速度很慢的方法,但这不是问题的关键)

设置 GL_MODELVIEW ,它只改变模型矩阵(可以用 glLoadMatrix glTranslate glRotate glScale 等。)。






逐行,您的一段代码执行以下操作:


  1. 所有转换现在都会影响投影矩阵。
  2. 清除旧的投影矩阵并用单位矩阵代替它。
  3. 使用整个屏幕作为视口。
  4. li>
  5. 将投影矩阵设置为具有45度垂直视野的透视图,纵横比为w / h,近剪辑平面1单位,远剪平面100单位


  6. Z轴已经存在,这只是设置给你视角的投影矩阵,告诉OpenGL使用整个窗口进行渲染。这不是对象空间,它是将对象空间转换为投影空间的方式。






    另外,你使用真正的,非常旧的OpenGL(1992年的旧版本)。 glTranslate等很久以前都被弃用,现在只是从API中删除。您仍然可以使用它们的唯一原因是因为驱动程序为了兼容性而将它们保留在那里。我建议你看看使用现代(3.0+)OpenGL。现代图形流水线比立即模式快几个数量级(glBegin,glVertex,glEnd)。


    I'm trying to understand creating spaces in OpenGL:

    1. Object space

    2. World space

    3. Camera space

    4. Projection space


    Is my understanding of these stages correct?

    1. The "cube" is being created in the center of the cartesian coordinate system, directly inside the program by typing the vertices coordinates.

    2. The coordinates are transformed into coordinates inside of the "world", which means moving it to any place on the screen.

    Well, actually I'd like you to check my understanding of those two terms.


    Now, I'm creating a triangle on the black screen. How does openGL code fits to these spaces?

    It works on GL_MODELVIEW flag by default, but that's the second stage - world space. Does that mean that calling glVertex3f() creates a triangle in the object space?

    Where is the world space part?


    Also, I've read that the last two spaces are not part of the openGL pipeline (or whatever it's called). However, OpenGL contains flags such as the GL_PROJECTION, for example:

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glViewport(0, 0, w, h); // w - width, h - height
    gluPerspective(45, ratio, 1, 100); // radio = w/h
    glMatrixMode(GL_MODELVIEW);
    

    What does this code do? It sets the perspective. Does it create the z axis? But isn't it already the object space part?

    解决方案

    1) Object space is the object's vertices relative to the object's origin. In the case of a 1x1x1 cube, your vertices would be:

    ( 0.5,  0.5, 0.5)
    (-0.5,  0.5, 0.5)
    ( 0.5, -0.5, 0.5)
    (-0.5, -0.5, 0.5)
    

    etc.

    2) World space is where the object is in your world. If you want this cube to be at (15, 10), you'd create a translation matrix that, when multiplied with each vertex, would center your vertices around (15, 10). For example, the first vertex would become (15.5, 10.5, 0.5). The matrix to go from object to world space is called the "model" matrix.

    3) Eye Space (sometimes called Camera space) is the world relative to the location of the viewer. Since this has to be a matrix that each vertex is multiplied by, it's technically the inverse of the camera's orientation. That is, if you want a camera at (0, 0, -10), your "view" matrix has to be a translation of (0, 0, 10). That way all the objects in the world are forward 10 units, making it look like you are backwards 10 units.

    4) Projection space is how we apply a correct perspective to a scene (assuming you're not using an orthographic projection). This is almost always represented by a frustum, and this article can explain that better than I can. Essentially you are mapping 3d space onto another skewed space.

    OpenGL then handles clip space and screen space.


    It works on GL_MODELVIEW flag by default, but that's the second stage - world space. Does that mean that calling glVertex3f() creates a triangle in the object space?

    You set vertices with glVertex3f() in object space always. (this is actually a very old and slow way to do it, but that's not really the point of this question)

    When you set GL_MODELVIEW, it's only changing the model matrix (which can be manipulated with glLoadMatrix, glTranslate, glRotate, glScale, etc.).


    Line by line, your piece of code is doing the following:

    1. All transformations will now be affecting the projection matrix.
    2. Clear out the old projection matrix and replace it with the identity matrix.
    3. Use the entire screen as the viewport.
    4. Set the projection matrix to a perspective with a 45 degree vertical field of view with an aspect ratio of w/h, the near-clip plane 1 unit away, and the far-clip plane 100 units away.
    5. All transformations are now affecting the modelview matrix again.

    The Z axis already exists, this just sets the projection matrix that gives you perspective, tells OpenGL to use the entire window to render. This isn't the object space, it's the way you transform object space to projection space.


    Also a side note, you're using really, really old OpenGL (1992 old). glTranslate, etc. were deprecated a long time ago and are now just removed from the API. The only reason you can still use them is because drivers keep them there for compatibility's sake. I'd recommend you look into using modern (3.0+) OpenGL. Modern graphics pipelines are several orders of magnitude faster than immediate mode (glBegin, glVertex, glEnd).

    这篇关于OpenGL中的对象,世界,相机和投影空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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