Sign in to follow this  
Narf the Mouse

Shader Lighting - A generic question

Recommended Posts

So, supposing one has shader-based lights and 3D models. The situation: My current understanding: In order to properly shade the 3D model based on the light, the Shader code needs the World matrix of the Model. The Shader code also needs light data from the Light object. The problem: The Shader code has two different, necessary sources of data. How do I 'hook' into both sources of data - Elegantly? I suspect that there's an elegant solution, that doesn't involve some form of 'global' list, if only I looked at the problem in a different way. Perhaps it may help if I received some information on how shader lighting is normally handled, as far as program flow/code objects go? Thanks for any and all help.

Share this post


Link to post
Share on other sites
If you actually have a shader that takes the lights as parameters, then yes, you will need to have some code that finds, say the nearest N lights (where N is the number of lights your shader handles) to the object, and sends their data to the shader.

You can be in a situation where the scene geometry and lighting are decoupled, when doing deferred shading or deferred lighting (they're different). In that case the light information isn't sent to the shader that draws the geometry. (Although in deferred lighting, a screen-sized texture is sent to the geometry shader, but it only needs to sample it for the result of all lights affecting that pixel.)

Share this post


Link to post
Share on other sites
Thanks - However, while that is helpful and I intend to read it, I was hoping more for something on integrating shader use into the code. Basically, a 'Now you've got your shaders; where do you go from there?'

Share this post


Link to post
Share on other sites
I recommend reading the DirectX SDK samples (maybe, "Basic HLSL" will do), for integrating HLSL into your code.
For Cg, you can try NVidia's SDK samples.

As for the elegant solution - this is task of graphics engine. The best way to learn this, IMHO, would be to download quake-3 (or any other PROFESSIONAL engine) sources (which are free) - and check out how have they done it.

Share this post


Link to post
Share on other sites
Quote:
Original post by Narf the Mouse
The situation: My current understanding: In order to properly shade the 3D model based on the light, the Shader code needs the World matrix of the Model.
The Shader code also needs light data from the Light object.

Hopefully you will also want a camera to frame the view :)

Quote:
Original post by Narf the Mouse
The problem: The Shader code has two different, necessary sources of data. How do I 'hook' into both sources of data - Elegantly?

Dependency Injection, Inversion of Control, or Hollywood Principle - "don't call me, I'll call you". Some class (the renderer or whatever) sits above the shader and knows these things (stores lights and renderable chunks). It is in a perfect position to tell the shader where to source data from.

Quote:
Original post by Narf the Mouse
I suspect that there's an elegant solution, that doesn't involve some form of 'global' list,

Whatever solution you pick, the one with the global list is wrong.

Quote:
Original post by Narf the Mouse
if only I looked at the problem in a different way.
Perhaps it may help if I received some information on how shader lighting is normally handled, as far as program flow/code objects go?

There are many ways, from my very limited understanding. For instance, you can sort by light and render, for each light, all geometry affected by that light. Or you can sort by state and render each object repeatedly for all lights that affect it. Or you can look into deferred shading.

Quote:
Original post by Narf the Mouse
I was hoping more for something on integrating shader use into the code. Basically, a 'Now you've got your shaders; where do you go from there?'

I refer you to the material/shader implementation thread. Unfortunately, it is very advanced and also quite old. Read carefully, there's a lot of info there.

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