Struggling to configure the values of a structure containing all the lights in my WebGL app using JavaScript. The layout of the structure is as follows:
struct Light {
vec4 position;
vec4 ambient;
vec4 diffuse;
vec4 specular;
vec3 spotDirection;
float spotCutOff;
float constantAttenuation;
float linearAttenuation;
float quadraticAttenuation;
float spotExponent;
float spotLightCosCutOff;
};
uniform Light lights[numLights];
Despite numerous attempts, I managed to make it work but I'm not pleased with the code quality:
program.uniform.lights = [];
program.uniform.lights.push({
position: "",
diffuse: "",
specular: "",
ambient: "",
spotDirection: "",
spotCutOff: "",
constantAttenuation: "",
linearAttenuation: "",
quadraticAttenuation: "",
spotExponent: "",
spotLightCosCutOff: ""
});
program.uniform.lights[0].position = gl.getUniformLocation(program, "lights[0].position");
program.uniform.lights[0].diffuse = gl.getUniformLocation(program, "lights[0].diffuse");
program.uniform.lights[0].specular = gl.getUniformLocation(program, "lights[0].specular");
program.uniform.lights[0].ambient = gl.getUniformLocation(program, "lights[0].ambient");
... and so on
I apologize for making you review this messy code; I acknowledge its shortcomings but am struggling to find a cleaner solution.
Is there an industry standard or best practice for handling this situation more effectively? Could someone provide guidance on this matter?