Shader Lighting - A generic question

Started by
6 comments, last by Narf the Mouse 14 years, 5 months ago
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.
Advertisement
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.)
...I think, perhaps, I am in need of a good introductory tutorial. Does anyone have a link to one?
One of the best introductory lighting tutorials: NVidia CG Tutorial, Chapter 5.
You may want to start reading from the first chapter, tho.
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?'
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.
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.
Thanks again. Looks like lots of reading to do.

This topic is closed to new replies.

Advertisement