Object, world, camera and projection spaces in OpenGL

前端 未结 1 406
逝去的感伤
逝去的感伤 2021-01-30 18:28

I\'m trying to understand creating spaces in OpenGL:

  1. Object space

  2. World space

  3. Camera space

  4. Projection space

    <
1条回答
  •  走了就别回头了
    2021-01-30 18:57

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

    0 讨论(0)
提交回复
热议问题