Deferred PointLight in view space

Hi,

I got some problems getting my pointlight to work for my deferred shader.

I'm computing the viewspace position of the pixels from my depth-buffer:

Code :

float DepthToZPosition(in float depth) {
return -cameraRange.x / (cameraRange.y - depth * (cameraRange.y - cameraRange.x)) * cameraRange.y;
}

Code :

linearDepth = DepthToZPosition( depth );
screencoord = vec3(((gl_FragCoord.x/bufferSize.x)-0.5) * 2.0,((-gl_FragCoord.y/bufferSize.y)+0.5) * 2.0 ,linearDepth);
screencoord.x *= -screencoord.z * adjustXY.x;
screencoord.y *= screencoord.z * adjustXY.y;
lightdir = screencoord.xyz - lightPosition.xyz;

where cameraRange is the Near+Far plane of my camera.

I'm drawing a fullscreen quad, with texcoord [ 0 , 1 ]

When rendering just the length of lightdir/some factor, i get some strange results (i think the depth is not computed correctly, as when i pan the camera towards the ground it is lit (where it shouldn't), but not where it should..

And also, when rotating the camera on the y-axis the light moves to the left and to the right, but doesn't stay where it should

In my C++ code:

Code :

float top = c->getNearRange() * tan(c->getFOV()*PIOVER180);float right = top * c->getAspectRatio();
glm::vec4 v = (c->getViewMatrix() * getGlobalMatrix()) * glm::vec4(0,0,0,1);
_shader->setVec3(_lightPositionLocation, glm::vec3(v));
_shader->setVec2(_adjustLocation, glm::vec2(right, top));

where globalMatrix is the modelMatrix for the light (usually only translations)