I'm currently writing an OpenGL application with a lot of shaders in it, and amongst those shaders, most of them use the same lighting algorithm. I read in the OpenGL Shading Language that you can link multiple shaders of the same type (ie: multiple fragment shaders) into a single shader program, unfortunately, I can't find anywhere for those fragment shaders to share global variables.
I'm using Opengl 2.0 at the moment, and due to the target platform, moving to OpenGL 3 or above is not currently an option.
What I have is a lighting shader containing two functions ... initLighting and applyLighting. InitLighting initializes the lighting variables, and applyLighting will add a single light source to the lighting variables. Then I have the main function which applies the texturing.
Since the lighting functions are used in almost every shader, I want them to be compiled into their own lighting shader, and linked with the main function (as opposed to what I'm doing now which is including the source code for the lighting into every fragment shader, which is causing really long processing times for compiling shaders). The problem is that I can't find any references on how to share the global lighting variables between the lighting shader and the one that applies texturing.
Here's some simplified pseudocode of what I'm trying to do
Lighting Fragment Shader
Code:
vec4 lightDiffuse, lightSpecular;
void applyLighting(int lightID)
{ ...
lightDiffuse += diffuseCalculation;
lightSpecular += specularCalculation;
}
void initLighting(void)
{ lightDiffuse=vec4(0,0,0,0);
lightSpecular=vec4(0,0,0,0);
applyLighting(0); // Add light source 0
applyLighting(1); // Add light source 1
}
Texturing Fragment Shader
Code:
void initLighting(void);
extern vec4 lightDiffuse, lightSpecular; // This is what I'm trying to get to work
void main(void)
{ initLighting();
lightDiffuse *= texture2D(tex0, gl_TexCoord[0].xy);
lightSpecular *= texture2D(tex1, gl_TexCoord[1].xy);
gl_FragColor = lightDiffuse + lightSpecular;
}
Any help would be greatly appreciated.