Screen Space

Cameras | Sunday 5 October 2008 12:10 pm

The last step of transformations (world to screen) when rendering is to get the rendered objects to screen space. This is a 2D space with the origin in the middle of the screen. After the vertex shader but before the fragment shader the fragments will be normalized device coordinates and will automatically be transformed to screen space. The fragments x,y coordinates will be mapped to the x and y coordinates on the screen (the exact mapping is set by viewportbounds). And the z coordinates will be mapped to the depth buffer (z-buffer) as the following [-1] -> [near clip plane limit] and [1] -> [far clip plane limit].

The viewport mapping is the following [-1,1] in X is mapped to the left to right of the viewport range. The [-1,1] in Y is mapped to the bottom to top of the viewport range

Framents will automatically be transformed to screen space before the fragment shader, you just need to set the viewport mapping with the following OpenGL call.

glViewport(0, screenHeight/2, screenWidth/2, screenHeight/2);

You can access the current fragment depth in a fragment shader with the variable gl_FragDepth which in default is the same as gl_FragCoord.z. The screen coordinates can be accessed in the variable gl_FragCoord.xy.

Information about pixel shaders in DirectX (most is the same in OpenGL):
http://www.gamedev.net/columns/hardcore/dxshader3/

A quick reference guide to GLSL:
http://www.opengl.org/sdk/libs/OpenSceneGraph/glsl_quickref.pdf

Description of the mapping in DirectX:
http://msdn.microsoft.com/en-us/library/bb219690.aspx

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

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment