# Calculating view space point light position

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

## Recommended Posts

Hi there,

I am working on my own deffered rendering engine. I am rendering the scene to the g-buffer containing diffuse color, view space normals and depth (for now). I have implemented directional light for the second rendering stage and it works great. Now I want to render a point light, which is a bit harder.

I need the point light position for the shader in view space because I have only depth in the g-buffer and I can't afford a matrix multiplication in every pixel. I took the light position and transformed it by the same matrix, by which I transform every vertex in shader, so it should align with verices in the scene (using D3DXVec3Transform). But that isn't the case: transformed position doesn't represent viewspace position nearly at all. Its x,y coordinates are off the charts, they are often way out of the (-1,1) range. The transformed position respects the camera orientation somewhat, but the light moves too quick and the y-axis is inverted. Only if the camera is at (0,0,0), the light stands at (0,0) in the center of the screen. Here is my relevant rendering code executed every frame:
D3DXMATRIX matView;    // the view transform matrixD3DXMATRIX matProjection;    // the projection transform matrixD3DXMatrixLookAtLH(&matView,                   &D3DXVECTOR3 (x,y,z),    // the camera position                   &D3DXVECTOR3 (xt,yt,zt),    // the look-at position                   &D3DXVECTOR3 (0.0f, 0.0f, 1.0f));    // the up directionD3DXMatrixPerspectiveFovLH(&matProjection,                           fov,    // the horizontal field of view                           asp,    // aspect ratio                           znear,    // the near view-plane                           zfar);    // the far view-planeD3DXMATRIX res=matView*matProjection;eff->SetMatrix("worldViewProj",&res); //vertices are transformed ok ín shader//render g-bufferD3DXVECTOR4 lpos; D3DXVECTOR3 lpos2(0,0,0);D3DXVec3Transform(&lpos,&lpos2,&res); //transforming lpos into lpos2 using res, still the same matrixeff->SetVector("poslight",&lpos); //but there is already a mess in lpos at this time//render the fullscreen quad with wrong lighting
Not that relevant shader code, but still, I see the light position this way (passing IN.texture is just me being lazy):
float dist=length(float2(IN.texture0*2-1)-float2(poslight.xy));OUT.col=tex2D(Sdiff,IN.texture0)/dist;
Can you please take a look at the code and tell me where the mistake is? It seems to me it should work ok, but it doesn't. Thanks in advance. Edited by Brejlounek

##### Share on other sites
It seems that you are transforming light position into projection space. You should multiply light position by matView not res.

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633731
• Total Posts
3013582
×