Skip to main content
GameDev.net gamedev.net
🔒 Locked

GLSL multiple lights

Started by kulik Oct 5, 2005 at 10:52 AM 1 replies 6.3k views
Original Post
kulik
kulik
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]
mikeman
mikeman
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)
{
value=value+gl_LightSource.diffuse;
}

It will be translated(before compilation) into:

value=value+gl_LightSource[0].diffuse;
value=value+gl_LightSource[1].diffuse;
value=value+gl_LightSource[2].diffuse;
value=value+gl_LightSource[3].diffuse;
value=value+gl_LightSource[4].diffuse;

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.
s_p_oneil
s_p_oneil
Check this out: http://www.gamasutra.com/features/20050729/lacroix_01.shtml

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.