Hi, does anyone know how to implement multiple lights in GLSL?
I have implemented the following code:
vertex shader:
Code :varying vec3 normal, lightDir; varying vec4 diffuse; void main() { gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex; vec3 transformedNormal = gl_NormalMatrix * gl_Normal; normal = gl_NormalMatrix * gl_Normal; if(vec3(gl_LightSource[0].position) != vec3(0.0, 0.0, 0.0)) { lightDir = vec3(gl_LightSource[0].position); diffuse = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse; } }
fragment shader:
Code :varying vec3 normal, lightDir; varying vec4 diffuse; void main() { vec4 color; float dotProduct; dotProduct = dot(normal, lightDir); color = dotProduct * diffuse; gl_FragColor = color; }
So now I want to add 3 other lights, gl_LightSource[1], gl_LightSource[2] and gl_LightSource[3] which comes from different directions and have different colors and are defined in the .c file. How do I do this? You turn the lights on and off with the F1, F2, F3 and F4 keys and my if statement is supposed to check if the light is activated or not. I managed to get all the lights to work with a for statement before the if statement but the lights couldn't be activated at the same time with that solution, just one at a time. I want to be able to have blue light on one side of an object and red light on the other side, for an example.



