Jump to content

  • Log In with Google      Sign In   
  • Create Account

[SOLVED] Calculating view space point light position


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 Brejlounek   Members   -  Reputation: 169

Like
0Likes
Like

Posted 14 January 2013 - 04:16 PM

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.



Sponsor:

#2 NumberXaero   Prime Members   -  Reputation: 1459

Like
0Likes
Like

Posted 14 January 2013 - 04:52 PM

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.


#3 Brejlounek   Members   -  Reputation: 169

Like
0Likes
Like

Posted 14 January 2013 - 05:41 PM

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.



#4 NumberXaero   Prime Members   -  Reputation: 1459

Like
0Likes
Like

Posted 14 January 2013 - 06:00 PM

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?



#5 Brejlounek   Members   -  Reputation: 169

Like
0Likes
Like

Posted 14 January 2013 - 06:19 PM

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.



#6 NumberXaero   Prime Members   -  Reputation: 1459

Like
0Likes
Like

Posted 14 January 2013 - 11:26 PM

If you plan on doing your lighting in view space then

 

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

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

 

Should work



#7 Brejlounek   Members   -  Reputation: 169

Like
0Likes
Like

Posted 15 January 2013 - 09:27 AM

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?



#8 Brejlounek   Members   -  Reputation: 169

Like
0Likes
Like

Posted 16 January 2013 - 08:46 AM

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!



#9 L. Spiro   Crossbones+   -  Reputation: 13600

Like
0Likes
Like

Posted 16 January 2013 - 09:52 PM

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
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#10 Brejlounek   Members   -  Reputation: 169

Like
0Likes
Like

Posted 17 January 2013 - 12:08 PM

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. ^^



#11 Brejlounek   Members   -  Reputation: 169

Like
0Likes
Like

Posted 21 January 2013 - 05:00 AM

Oh, by the way, this screen space point light is a bad idea, the xy-coordinates should be from much larger range in larger distances. Instead, I calculate world-space position from depth map with distance from camera using formula

pos=pEye+IN.cpos*depth

where IN.cpos is interpolated position of the screen corner  and pEye is eye position.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS