Hi guys!
I´m having problems with deferred shading. First the code, then the problem:
Position reconstruction
/*Reconstruct position from depth and screen coordinates (800x600), far plane at 512.0*/
vec3 posFromDepth(vec2 coord){
float d = texture2D(gdepth, coord).r;
vec3 vray = vec3((gl_FragCoord.x-400.0)*2.0,(gl_FragCoord.y-300.0)*2.0,512.0);
return vray*d;
}
//actual lighting:
vec3 p = posFromDepth(gl_TexCoord[0].st);
float len = length(lightpos.xyz - p);
vec3 color2 = vec3(1,1,0)*1.0/(1.0+len*len);
(My depth buffer is linear, so 0 depth at near plane and 1 depth at far plane.)
Passing the light position to the shader:
shader->passVec4(getNode("Camera")->getWorldTransform()*maths::CVector4(0,0,0,1));
If i don´t transform by the view matrix, the lights shows up fine, right in the camera´s position, and moves with it which is supposed to happen. But when i multiply with the camera´s transform to get the coordinate in view space, then it disappears or fills the entire screen, depending on i don´t know what.
I can´t use the frustum corners method to get the view ray because i want to find the position for more than the current pixel. So i must use the frag coord.
Any hints on what might be happening?