• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Brejlounek

[SOLVED] Calculating view space point light position

10 posts in this topic

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.

0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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.

0

Share this post


Link to post
Share on other sites

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?

0

Share this post


Link to post
Share on other sites
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.

0

Share this post


Link to post
Share on other sites

If you plan on doing your lighting in view space then

 

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

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

 

Should work

0

Share this post


Link to post
Share on other sites
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?

0

Share this post


Link to post
Share on other sites

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!

0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
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. ^^

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0