• Advertisement
Sign in to follow this  

Pixel Position From Linear Depth

This topic is 2623 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


I'm trying to write a deferred renderer program and would like to have some help :)
My program uses MRT (color, normals and depth). I can render linear depth texture and it looks like it should look but unfortunately I can't get correct 3D positions from linear depth (view space).

I provide you some code:
// vertex shader
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
unpro = (gl_ModelViewMatrix * gl_Vertex).xyz;

// fragment shader
float linearize_depth(float z) {
float n = 0.001;
float f = 16.0;
return (2.0 * n) / (f + n - z * (f - n));
void main() {
vec3 color = texture2D(color_buffer, gl_TexCoord[0].xy).rgb;
float depth = linearize_depth(texture2D(depth_buffer, gl_TexCoord[0].xy).r);
vec3 normal = texture2D(normal_buffer, gl_TexCoord[0].xy).rgb;
vec4 position = vec4(unpro, 1) * vec4(gl_FragCoord.xy * depth, depth, 1 - depth);
position.xyz /= position.w;
gl_FragColor = position;


EDIT: I updated code above and uploaded screenshot. Is it correct now? :)

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement