[SOLVED] Calculating view space point light position

Started by
9 comments, last by Brejlounek 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. It's 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 direction
D3DXMatrixPerspectiveFovLH(&matProjection,						   
fov,    // the horizontal field of view						   
asp,    // aspect ratio						   
znear,    // the near view-plane						   
zfar);    // the far view-plane
D3DXMATRIX vysl=matView*matProjection;eff->SetMatrix("worldViewProj",&vysl); //vertices are transformed ok ín shader
//render g-buffer
D3DXVECTOR4 lpos; 
D3DXVECTOR3 lpos2(0,0,0);
D3DXVec3Transform(&lpos,&lpos2,&vysl); //transforming lpos into lpos2 using vysl, still the same matrix
eff->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.

P,S, . Sorry for second same topic, my fault, I can't get rid of the old one.

Advertisement

Well "vysl" seems to be the view projection matrix, which you transform the light position with here
"D3DXVec3Transform(&lpos,&lpos2,&vysl);". If you want the light in view space, then just use the view matrix to transform the lights
world position.

Well "vysl" seems to be the view projection matrix, which you transform the light position with here
"D3DXVec3Transform(&lpos,&lpos2,&vysl);". If you want the light in view space, then just use the view matrix to transform the lights
world position.

Thank you for your reply, I have already tried that. I rewrote


D3DXMATRIX vysl=matView*matProjection;

with


D3DXMATRIX vysl=matView;

, did you mean that? The result looks the same. But there should be the projection matrix involved too, right? I mean, I need some information about fov, asp and near/far plane to work out the position in view space.

What vertices are you transforming with this in the shader

D3DXMATRIX vysl=matView*matProjection;

eff->SetMatrix("worldViewProj",&vysl); //vertices are transformed ok ín shader

and you are trying/doing your lighting in view space?

What vertices are you transforming with this in the shader

D3DXMATRIX vysl=matView*matProjection;

eff->SetMatrix("worldViewProj",&vysl); //vertices are transformed ok ín shader

and you are trying/doing your lighting in view space?

I am transforming a few models with different materials I put together in 3dsmax and exported to .X format. The are transformed correctly.

As you can see in the shader, I am just testing x,y coordinates of "poslight", not really any lighting yet, just some plain /depth. I am transforming normals to view space (in fact only rotating them), if you mean that, but that has no connection with bad lightpos transform. I am sure the mistake is some no brainer I forgot.

If you plan on doing your lighting in view space then

D3DXVec3Transform(&lposView,&lpos2World,&matView);

eff->SetVector("poslight",&lpos);

Should work

If you plan on doing your lighting in view space then

D3DXVec3Transform(&lposView,&lpos2World,&matView);

eff->SetVector("poslight",&lpos);

Should work

Yeah, that's true. Unfortunately, it doesn't for me, as stated above. :(

To be clear: The initial shader vertices are transformed using


OUT.hposition = mul( float4(IN.position, 1), worldViewProj);

and it works ok. worldViewProj here has the same value as "vysl". I tried even transforming light position in shader and the result is exactly the same as using D3DXVec3Transform. So I think there is not a problem with D3DXVec3Transform but with matrix "vysl" itself. Which is strange, because EXACTLY the same matrix is used for vertices transform. Does vertex processing do something else with the matrix? Shouldn't I manipulate it somehow? I don't know, I am lost here... transponse it?

I figured it out, thanks to kerim from http://stackoverflow.com!

Result of this transform D3DXVec3Transform(&lpos,&lpos2,&vysl); is a vector in homogeneous space(i.e. projected vector but not divided by w). But in you shader you use it's xy components without respecting this(w). This is (quite probably) the problem. You could divide vector by its w yourself or use D3DXVec3Project instead of D3DXVec3Transform.

It's working fine for vertices as (I suppose) you mul them by the same viewproj matrix in the vertex shader and pass transformed values to interpolator where hardware eventually divides it's xyz by interpolated 'w'.

I just used D3DXVec3Project, adjusted coordinates into -1,1 ramge and it works great! Yaay!

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.
You need to brush up on your terminology. The reason no one could help you is because you said you wanted your light in view space, which is exactly where it was.
You meant to say you wanted your light in “screen space”. That would have made it a lot easier to show you the way.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

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.
You need to brush up on your terminology. The reason no one could help you is because you said you wanted your light in view space, which is exactly where it was.
You meant to say you wanted your light in “screen space”. That would have made it a lot easier to show you the way.


L. Spiro

Thank you for the cleanup, I indeed meant screen space because I wanted the xy coordinates to be between -1 and 1. But I think what I wanted to do was obvious anyway. ^^

This topic is closed to new replies.

Advertisement