Sign in to follow this  

GLSL multiple lights

Recommended Posts

I am now trying to code a complex lightmodel shader to handle all types of lights in my engine. I want to support unlimited amount of lights. Lights *will* be additive, so a loop inside the shader should do the job. The problem is: how to transfer the light parameters from C++ into GLSL. I know I could do it with mat4 and decode it somehow inside the shader, this solution is ugly and limited though ... Can I use some "elegant" way of doing that? btw: I know about gl_LightSources array, but that is limited and kinda unextendable. [Edited by - kulik on October 5, 2005 11:11:43 AM]

Share this post

Link to post
Share on other sites
Unless you're programming for Geforce 6800 and similar cards(ps3.0) that support true branching and texture/memory lookups in vertex shaders, you can't do it, not in a way that is practically usefull anyway. In any other card, a loop is not real, GLSL just duplicates the loop body many times. Ang generally, most cards don't have the possibility to transfer the control from one instruction to another, they execute the whole program from beginning to the end. If you write:

for (int i=0;i<5;++i)

It will be translated(before compilation) into:


So you see the compiler must even know the number of the loops before compilation(it can't be dynamic) and after a number of lights, you will exceed the instruction limit. Also, if you try to handle all light types inside the shader, all calculations(for directional, spot,point lights...) will take place.
For example:

if (pointlight) col=dot(V-L,N);else col=dot(V,N);

In this case, both "col=dot(V-L,N)" and "col=dot(V,N)" will be executed and the results will be stored in temporary registers, and at the end col will be assigned the correct value by masking or something like that.

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