Clip Space

Cameras | Sunday 5 October 2008 2:53 am

Before rendering something all objects need to be projected to the screen ( a plane ). The first step, to project to the screen, is to project entities to clip space.

The mostly used projection type in games is perspective projection and therefore only this kind of projection will be explained. An other type of projection is parallel projection which is a projection that will keep parallel lines parallel. Perspective projection gives a field of view and objects appears to be smaller when further away which will feel realistic. The projection volume is set up as the following image shows.

The Projection Volume

You apply a projection to the entities that is in eye space to get them to clip space. This is the space that will soon decide if the vertex will be culled or not. Together with homogenization it can be thougt as a mapping from the projection volume to a cube with sizes 2 (in OpenGL) and with origin at 0,0,0. This mapping is showed in the image below.

Projection volume mapped to a cube

In OpenGL you can access the projection matrix if you set the matrix mode to projection:

glMatrixMode(GL_PROJECTION);

The following vertex shaders shows some ways to transform a vertex to clip space. The fastest way is the third shader that uses the highly optimized ftransform() function.

void main()
{
    // vertex in clip space
    gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex;
}
void main()
{
    // vertex in clip space
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
void main()
{
    // vertex in clip space
    gl_Position = ftransform();
}

One side effect from applying perspective projection is that you end up with a non-linear z-buffer. The buffer will be more detailed closer to the screen which is often good. More about this in the z-buffer article.

After applying projection to a point p = (x,y,z,w) you will end up with a w value that is most probably not zero or one. This means you need to divide all components by this value to obtain normalized device coordinates. This will happen automatically between the vertex shader and fragment shader. Clipping will also be performed between these.

Information about different ways to transform vertices to clip space inside of a vertex shader:
http://www.lighthouse3d.com/opengl/glsl/index.php?minimal

Description of different 3D spaces:
http://vesta.astro.amu.edu.pl/Library/Linux/LinFocus/May1998/article6.html

Please share:
  • Print this article!
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Current
  • LinkedIn
  • Live
  • MySpace
  • Netvibes
  • StumbleUpon
  • Twitter
  • Reddit
  • Technorati
  • Yahoo! Bookmarks

Eye Space

Cameras | Sunday 5 October 2008 1:56 am

Eye Space or often called View Space or Camera Space is the space where the camera is located in origin (0,0,0) and all objects are transformed accordingly. More specifically the camera (in OpenGL) is located in origin, looking down the negative z-axis and has the x-axis pointing to the right and the y-axis pointing up. This mean that objects further away from the camera (when looking at them) has lower z-value. In OpenGL the ModelView determines how the objects should be transformed to be end up in eye space.

A vertex can be transformed to eye space with the following vertex shader (GLSL):

void main(void)
{
   vec4 eyeSpaceVertex = gl_ModelViewMatrix * gl_Vertex;
   // now do what you want with it
}

A normal can be transformed to eye space with the following vertex shader (GLSL):

void main(void)
{
   vec3 eyeSpaceNormal = normalize(gl_NormalMatrix * gl_Normal);
   // now do what you want with it
} 

The gl_NormalMatrix can also be used to transform tangents and binormals to eye space.

void main(void)
{
   vec3 eyeSpaceNormal = normalize(gl_NormalMatrix * gl_Normal);
   vec3 eyeSpaceTangent = normalize(gl_NormalMatrix * Tangent);
   vec3 eyeSpaceBinormal = normalize(gl_NormalMatrix * Binormal);
   // now do what you want with it
} 

Note that neither of these shaders are complete because they don’t write anything to gl_Position.

Lights are passed to the shader in eye space as default and most lightning calculations take place in eye space.

Description of different 3D spaces:
http://vesta.astro.amu.edu.pl/Library/Linux/LinFocus/May1998/article6.html

Please share:
  • Print this article!
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Current
  • LinkedIn
  • Live
  • MySpace
  • Netvibes
  • StumbleUpon
  • Twitter
  • Reddit
  • Technorati
  • Yahoo! Bookmarks

World Space

Cameras | Sunday 5 October 2008 1:17 am

The world space is the space where all objects are located in your game.  You can have objects, lights and cameras in world space. OpenGL doesn’t really care about world space. This is because there is no difference between transforming the world or the camera. For example translating the camera with the vector t is the same as translating the world with the vector -t. OpenGL uses therefore a single matrix called ModelView that holds the transformation from world space to eye space.

This matrix can be manipulated after setting the matrix mode to ModelView with this line of code:

glMatrixMode(GL_MODELVIEW);

Description of the different 3D spaces:
http://vesta.astro.amu.edu.pl/Library/Linux/LinFocus/May1998/article6.html
http://pages.cs.wisc.edu/~psilord/docs/local_axis.html

Please share:
  • Print this article!
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Current
  • LinkedIn
  • Live
  • MySpace
  • Netvibes
  • StumbleUpon
  • Twitter
  • Reddit
  • Technorati
  • Yahoo! Bookmarks

Object Space

Cameras | Sunday 5 October 2008 1:02 am

This is the space you create your models in within a modelling program. The origin is often set to the center of rotation of the object. The vertices of the models are often saved in this space and later transformed or rotated to the correct position in world space.

Information about spaces in 3D:
http://www.gamedev.net/reference/articles/article673.asp

Please share:
  • Print this article!
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Current
  • LinkedIn
  • Live
  • MySpace
  • Netvibes
  • StumbleUpon
  • Twitter
  • Reddit
  • Technorati
  • Yahoo! Bookmarks
« Previous Page