# Problem reconstruction position form depth

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

## Recommended Posts

Hi everyone,

I'm trying for a few days removing my position texture from my deferred system. To achive this I have to reconstruct pixel position from depth value.
I spend some days now (many houres) tring to solve the problem without luck, so I'm somehow frustated.

Let's see if someone with better math can help me

First how i fill my old position texture (geometry pass). [With this texture the lighting works fine.]

 Geometry pass - Vertex: vsPosition = ( ModelViewMatrix * vec4(in_Position, 1.0) ).xyz; Geometry pass - Fragment: fsPosition = vsPosition 

Where ModelViewMatrix is ViewMatrix * ModelMatrix.

From here all work, and accessing pixel position in the light pass is trivial.The problem occurs when I try to compute pixel position with depth value.
I tried many things (many...), and what I have at the moment is:

 vec2 calcTexCoord() { return gl_FragCoord.xy / ScreenSize; } 

 vec3 positionFromDepth() { vec2 sp = calcTexCoord(); float depth = texture2D(Texture4, sp).x * 2.0 - 1.0; vec4 pos = InvProjectionMatrix * vec4( sp*2.0-1.0, depth, 1.0); return pos.xyz/pos.w; } 

Where InvProjectionMatrix is the inverse of the projection matrix, and Texture4 is depth texture.

The strange thing is that if I output the absolute diference from both (using texture and using depth), i see black output (no diferences).
Following code outputs black screen: (Texture0 is position texture)
 vec3 pos1 = positionFromDepth(); vec3 pos2 = texture2D(Texture0, calcTexCoord()).xyz; fragColor = vec4(abs(pos2.x-pos1.x), abs(pos2.y-pos1.y), abs(pos2.z-pos1.z), 1.); 

Here is my point light calculation: (With pixel position from depth doesn't work)
 uniform struct { vec3 diffuse; vec3 specular; vec3 ambient; float constantAtt; float linearAtt; float quadraticAtt; float spotInnerCutOff; float spotOuterCutOff; float spotFallOff; vec3 position; // For Point light. Computed as ViewMatrix * light_position vec3 direction; // For Spot and Directional lights int type; // 0-> Point, 1-> Spot, 2-> Directional } Light; ... vec3 computePhongPointLight() { vec3 color = vec3(0.0); vec2 texCoord = calcTexCoord(); //vec3 position = texture2D( Texture0, texCoord ).xyz; vec3 position = positionFromDepth(); vec3 difColor = texture2D( Texture1, texCoord ).xyz; vec3 specColor = texture2D( Texture2, texCoord ).xyz; vec3 normColor = texture2D( Texture3, texCoord ).xyz; vec3 lightDir = Light.position.xyz - position; vec3 lightDirNorm = normalize( lightDir ); float sDotN = max( dot(lightDirNorm, normColor), 0.0); float att = 1.0; float distSqr = dot(lightDir, lightDir); float invAtt = (Light.constantAtt + (Light.linearAtt*sqrt(distSqr)) + (Light.quadraticAtt*distSqr)); att = 0.0; if (invAtt != 0.0) att = 1.0/invAtt; vec3 diffuse = difColor.rgb * Light.diffuse * sDotN; vec3 ambient = difColor.rgb * Light.ambient; // Cheat here vec3 vertexToEye = -normalize(position); vec3 r = normalize(reflect(lightDirNorm, normColor)); // SpecularPower vec3 specular = vec3(0.0); if ( sDotN > 0.0 ) { specular = Light.specular.rgb * specColor * pow( max( dot(vertexToEye, r), 0.0), 60.0 ); // Change specular here!!!! value 60 must be an uniform } return (diffuse + specular + ambient)*att; } 

I will apraciate any help, so feel free to answer
Sorry for my bad English and thanks in advance. Edited by junkyska

##### Share on other sites
Our engine uses one of the methods that are proposed on this website: http://mynameismjp.wordpress.com/2009/03/10/reconstructing-position-from-depth/
The link here though gives you directly to the one you seem to be trying to achieve.

The difference between GLSL and HLSL is pretty small so hopefully there shouldn't be any problems. But if you do get problems or you find what he has written hard to understand I'll help you out. Though give that link a try first

##### Share on other sites
Thanks for you response, but yestarday I finally solve it!! (The algorithm I posted here it's not right, i use another one from dark photon).
Here there is the resolution: http://www.opengl.or...995#post1244995

Thank's for the link I get it before while searching on google. Edited by junkyska

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 11
• 15
• 11
• 11
• ### Forum Statistics

• Total Topics
634149
• Total Posts
3015830
×