Calculating view space point light position

Started by
0 comments, last by SIIYA 11 years, 3 months ago
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.
Advertisement
It seems that you are transforming light position into projection space. You should multiply light position by matView not res.

This topic is closed to new replies.

Advertisement