I have some GLSL shaders that are compiling correctly, and being successfully attached to a program that links correctly. I have confirmed that other shaders work properly in my program. The strange thing is that the result I see seems to be the same behavior I would get out of default shaders (color, no lighting).
I will paste the source code of both the vertex and fragment shaders, as well as all excerpts from my C++ source code that pertain to the shaders. I will explain in comments any functions or classes that are unique to my program.
My vertex shader (contained in “dirlight.vs”)
uniform int lightcount;
uniform vec4 lightdirs[];
uniform vec4 lightdifs[];
uniform vec4 lightambs[];
uniform vec4 lightposs[];
void main()
{
vec3 normal = normalize(gl_NormalMatrix * gl_Normal);
vec4 diffuse = vec4(0.0, 0.0, 0.0, 0.0);
for(int i = 0; i < lightcount; i++)
{
//Adding the diffuse term multiplied by gl_Color for each light.
//There should be no color (0,0,0,0) if the lights are null
diffuse += lightdifs[i] * max(dot(normal, normalize(lightdirs[i])), 0.0) * gl_Color;
}
gl_FrontColor = diffuse;
gl_Position = ftransform();
}
My fragment shader (contained in “dirlight.fs”)
void main()
{
gl_FragColor = gl_Color;
}
Excerpt from initialization in C++ main…
//class program manages shaders
Program shaders = Program();
//attach a vertex shader, compiled from source in dirlight.vs
shaders.addShaderFile(GL_VERTEX_SHADER, "dirlight.vs");
//attach a fragment shader compiled from source in dirlight.fs
shaders.addShaderFile(GL_FRAGMENT_SHADER, "dirlight.fs");
//link program
shaders.link();
//use program
shaders.use();
//Program::getUniformLoc(const char* name) grabs the location
//of the uniform specified
GLint sTime = shaders.getUniformLoc("time");
GLint lightcount = shaders.getUniformLoc("lightcount");
GLint lightdir = shaders.getUniformLoc("lightdirs");
GLint lightdif = shaders.getUniformLoc("lightdifs");
GLint lightamb = shaders.getUniformLoc("lightambs");
glUniform1i(lightcount, 2);
GLfloat lightdirs[] = {-1.f, 1.f, 1.f, 1.f,
1.f, 1.f, -1.f, 1.f};
glUniform4fv(lightdir, 2, lightdirs);
GLfloat lightdifs[] = {1.f, 1.f, 1.f, 1.f,
1.f, 1.f, 1.f, 1.f};
glUniform4fv(lightdif, 2, lightdifs);
glUniform4f(lightamb, 0.4f, 0.4f, 0.4f, 1.f);
Excerpt from main loop in C++ main…
glUniform1f(sTime, newTime);
//This should cause the light directions to rotate around the origin
GLfloat lightdirs[] = {sinf(newTime * 2.f), 1.f, cosf(newTime * 2.0f), 1.f,
cosf(newTime * 2.f), 1.f, sinf(newTime * 2.0f), 1.f};
glUniform4fv(lightdir, 2, lightdirs);
That is not legal GLSL. I can’t explain why your compiler allows compiling (I’m guessing it’s an NVIDIA compiler, right? That of course assumes that you are checking the compilation/link status), but uniform arrays must have a length defined explicitly within the shader. If you want to have variable length arrays, you have to pick a maximum length and bake it into the shader. The shader can read fewer values (a length based on a uniform you pass in), but that’s about it.