I'm trying to set the values of the individual fields of structs in an array. The problem is that in the shader, every single field of the struct has the value 0 assigned to it.
Shader:
struct Light
{
vec3 position;
vec3 color;
float ambient;
float diffuse;
float specular;
float exponent;
};
uniform Light lights[8];
C++:
struct Light
{
vec3 position;
vec3 color;
float ambient;
float diffuse;
float specular;
float exponent;
};
...
std::vector<Light> activeLights;
Light l;
l.position = vec3(-50.0f, 50.0f, 50.0f);
l.color = vec3(1.0f, 1.0f, 1.0f);
l.ambient = 0.15f;
l.diffuse = 0.6f;
l.specular = 0.25f;
l.exponent = 8.0f;
activeLights.push_back(l);
...
for (int i = 0; i < activeLights.size(); i++)
{
glUniform3f(glGetUniformLocation(program, "lights[i].position"),
activeLights[i].position.x,
activeLights[i].position.y,
activeLights[i].position.z
);
glUniform3f(glGetUniformLocation(program, "lights[i].color"),
activeLights[i].color.r,
activeLights[i].color.g,
activeLights[i].color.b
);
glUniform1f(glGetUniformLocation(program, "lights[i].ambient"), activeLights[i].ambient);
glUniform1f(glGetUniformLocation(program, "lights[i].diffuse"), activeLights[i].diffuse);
glUniform1f(glGetUniformLocation(program, "lights[i].specular"), activeLights[i].specular);
glUniform1f(glGetUniformLocation(program, "lights[i].exponent"), activeLights[i].exponent);
}
I checked the values on the c++ side multiple times in the debugger, everything's ok there.
Any ideas?
"lights[i].position": that's a string containing exactly what is written there. i is not magically replaced by the number. If you want to replace i with the corresponding number, you'll have to use an std::stringstream or snprintf or something similar.