Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've been trying to make a structure that will contain all the lights of my WebGL app, and I'm having troubles setting up it's values from JS. 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];

After testing LOTS of things I made it work but I'm not happy with the code I wrote:

program.uniform.lights = []; 
        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'm sorry for making you look at this code, I know it's horrible but I can't find a better way.

Is there a standard or recommended way of doing this properly? Can anyone enlighten me?

share|improve this question
up vote 1 down vote accepted

How about something like this?

var props = ['position', 'diffuse', 'specular', 'ambient']
for (var i = 0; i!=maxLights; ++i) {
  for (var j in props) {
    prop = props[j];
    eval('program.uniform.lights[' + i + '].' + prop + ' = gl.getUniformLocation(program, "lights[' + i + ']." + prop);
share|improve this answer
I'm gonna check this out this afternoon an get back to you. Thanks! – mikidelux Nov 21 '11 at 9:56

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.