Linear depth texture

Rendering Methods | Sunday 28 September 2008 11:06 pm

In some occasions a linear depth texture is preferred over the usual non-linear depth buffer (z-buffer). The linearized depth could for example be used when doing SSAO or depth of field. Rendering linear z-buffers is very easy and only the following lines of code is required ( a float texture should be set as render target):

Vertex shader:

varying float depth;
void main()
{
    vec4 viewPos = gl_ModelViewMatrix * gl_Vertex; // this will transform the vertex into eyespace
    depth = -viewPos.z; // minus because in OpenGL we are looking in the negative z-direction
    gl_Position = ftransform();
}

Fragment shader:

varying float depth;
void main()
{
    gl_FragColor.r = depth;
}

 
You might want to scale the depth in the vertex shader to a more appropriate range as the the distance will otherwise be the same as the distance from the camera to the vertex. The following vertex shader will map the near and far distances to 0..1 which is often a good idea.

varying float depth;
void main()
{
    vec4 viewPos = gl_ModelViewMatrix * gl_Vertex; // this will transform the vertex into eyespace
    depth = (-viewPos.z-near)/(far-near); // will map near..far to 0..1
    gl_Position = ftransform();
}

Here’s how the shader looks like if you render a cube.

Linear Depth Rendering of a Cube

Link to a page about the depth buffer in DirectX
http://www.mvps.org/directx/articles/linear_z/linearz.htm

A good introduction to the depth buffer in OpenGL
http://www.sjbaker.org/steve/omniv/love_your_z_buffer.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

Packing a float value in RGBA

Rendering Methods | Thursday 25 September 2008 2:13 am

Here’s the way to pack a float value in a RGBA8 texture. You should try to use float textures instead but if that’s not possible then this might be a good work around.

// Packing a [0-1] float value into a 4D vector where each component will be a 8-bits integer
vec4 packFloatToVec4i(const float value)
{
   const vec4 bitSh = vec4(256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0);
   const vec4 bitMsk = vec4(0.0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0);
   vec4 res = fract(value * bitSh);
   res -= res.xxyz * bitMsk;
   return res;
}
 
// Unpacking a [0-1] float value from a 4D vector where each component was a 8-bits integer
float unpackFloatFromVec4i(const vec4 value)
{
   const vec4 bitSh = vec4(1.0 / (256.0 * 256.0 * 256.0), 1.0 / (256.0 * 256.0), 1.0 / 256.0, 1.0);
   return(dot(value, bitSh));
}

The source was posted on the GameDev forum:
http://www.gamedev.net/community/forums/topic.asp?topic_id=463075&whichpage=1&#3054958

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

Rendering normals in eye space

Rendering Methods | Thursday 25 September 2008 12:46 am

Here’s a simple shader for rendering the normals in a scene in eye space. This shader can be useful for debugging or then you want to do SSAO or edge detection. Below is a rendering of the normals of a sphere in eye space.

Normals in screen space

Vertex shader:

varying vec3 normal;
void main()
{
   gl_Position = ftransform();
   normal = gl_NormalMatrix * gl_Normal;
}

Fragment shader:

varying vec3 normal;
void main()
{
   gl_FragColor.rgb = normal;
}

If you want to save it in a RGB texture then you need to convert the normal to the 0..1 range with the following normal = (normal + 1.0) / 2.0 .

The same shader can be used to render the tangent or binormal in screen space too. Just switch the gl_Normal to your other input.

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