Light to Object or Normal to World

Started by
6 comments, last by jollyjeffers 18 years, 5 months ago
Hi, I've have recently been perusing some sample HLSL shaders and noticed that, with regularity, the vertex normal is rotated (in the shader) to the world coordinate system to be dot'ed with the light vector in world coords. I've always rotated the light to obj coords and passed it in as a constant, thus (theoretically) avoiding additional mat multiplies in the shader. My results always seem to "look" fine so I'm curious... is my approach invalid or otherwise prone to singularities? Thanks for any thoughts! Brad
Advertisement
I don't see anything wrong with your approach, it doesn't really matter where you do your lighting calculations and if you think you'll need the least instructions doing it all in object space then that's fine.
Thanks for confirming what I figgered was the situation. I'm converting most of my old asm shaders to HLSL and it seemed like a good time to make sure.

Thanks,
Brad
One of the common mistakes is mis-matching coordinate spaces (e.g. normal in world space and light in object space), such that the only really key thing is to make sure that they're both in the same space. Anything beyond that should just be an implementation detail - go with whatever works for you [smile]

Whether it's better/worse I don't know, but if you pass your light in object space then you'll probably have to keep changing that constant for each object, right? Changing constants can be quite expensive...

hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

Jack, thanks for your comments.

Yes, I redefine the light vector for each of my objects. Generally, tho', each object has a healthy number of vertices. I assume (perhaps incorrectly?) the relative number of light constant redefinitions is not a significant performance impact, considering the other constants, textures, etc. that must be changed.

Thanks!,
Brad
Quote:Original post by Brad Sweet
Yes, I redefine the light vector for each of my objects. Generally, tho', each object has a healthy number of vertices. I assume (perhaps incorrectly?) the relative number of light constant redefinitions is not a significant performance impact, considering the other constants, textures, etc. that must be changed.

You've probably got the better optimization - I doubt what I mentioned would actually cause a problem as such (even then, from what I understand most games are fill-rate or cpu bound).

I was more pointing it out in reference to your observation on performance - pointing out that even your way might have a cost (Not sure if setting VS constants can cause a pipeline stall, but if it did that would be bad!), and that it is generally bad practice to assume anything about the performance characteristics of a system [smile]

Cheers,
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

Forgive me if I'm wrong as I have little practical experience with shaders but don't you have to set the transformation matrix constants for every object anyway? Surely if you're setting one constant setting another should have a pretty minimal cost (i.e. if it did cause a pipeline stall, setting another can't cause another pipeline stall and hence another relatively large speed hit as it's already stalled).
Quote:Original post by Monder
Forgive me if I'm wrong as I have little practical experience with shaders but don't you have to set the transformation matrix constants for every object anyway? Surely if you're setting one constant setting another should have a pretty minimal cost (i.e. if it did cause a pipeline stall, setting another can't cause another pipeline stall and hence another relatively large speed hit as it's already stalled).

Sounds good to me - I think you've caught me out here [wink]

Cheers,
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

This topic is closed to new replies.

Advertisement