Jump to content
  • Advertisement
Sign in to follow this  

GLSL multiple lights & for cycle

This topic is 3032 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I am trying to follow up my shader to n number of lights.
This is my idea, but unfortunately this doesn't work:

#define NUM_LIGHTS 2

varying vec3 VertexPosition;
varying vec3 NormalVector;
varying vec4 LightPosition[NUM_LIGHTS];
varying vec3 LightVector[NUM_LIGHTS];
varying vec4 AmbientGlobal;
varying vec4 Diffuse[NUM_LIGHTS];

int Face;
float NVdotLV;
float Distance;
vec4 Color;
float Attenuation;
float SpotCosCutoff;
float SpotEffect;

void PointLight(int i) {
Distance = length(LightVector);
SpotEffect = 1.0;
Attenuation = SpotEffect / (gl_LightSource.constantAttenuation +
gl_LightSource.linearAttenuation * Distance +
gl_LightSource.quadraticAttenuation * Distance * Distance);
Color += Attenuation * (Diffuse * NVdotLV);
void main() {
Face = -(1 - int(gl_FrontFacing) * 2);
Color = AmbientGlobal;
for(int i = 0; i < NUM_LIGHTS; i++) {
NVdotLV = dot(normalize(NormalVector) * Face, normalize(LightVector));
if(NVdotLV > 0.0) {
gl_FragColor = Color;

This way all lighted surfaces are red (color of first light). It looks like gl_LightSource doesn't accept variable i. Would anyone suggest some fix? I really don't want to make new function to every light.
I'm using Intel X4500.

Please help.
Thanks in advance.

[Edited by - Karmux on August 22, 2010 5:01:12 PM]

Share this post

Link to post
Share on other sites
You should use some other lighting technique. Such as deferred shading or blending. Using loops will cause your shader to use a lot of fragment operations (since loops are unrolled)

So if your shaders support two lights, the PointLight function will be inlined twice.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!