Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object, world, camera and projection spaces in OpenGL

Tags:

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?

like image 880
user2252786 Avatar asked Apr 25 '13 22:04

user2252786


People also ask

What is object space in OpenGL?

1) Object space is the object's vertices relative to the object's origin.

How is the camera position implemented in OpenGL?

As far as OpenGL is concerned, there is no camera. More specifically, the camera is always located at the eye space coordinate (0.0, 0.0, 0.0). To give the appearance of moving the camera, your OpenGL application must move the scene with the inverse of the camera transformation by placing it on the MODELVIEW matrix.

How do I get the projection matrix in OpenGL?

Setting up the perspective projection matrix in OpenGL was done through a call to glFrustum. The function took six arguments: glFrustum(float left, float right, float bottom, float top, float near, float far); The implementation of this function can be found in the code below (line 20).

What is projection matrix in OpenGL?

A 3D scene rendered by OpenGL must be projected onto the computer screen as a 2D image. GL_PROJECTION matrix is used for this projection transformation. First, it transforms all vertex data from the eye coordinates to the clip coordinates.


1 Answers

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

like image 176
Robert Rouhani Avatar answered Sep 23 '22 15:09

Robert Rouhani