1

I'm trying to create some sort of basic lighting system. I have this struct set up:

struct Light
{
  vec3 position;
  vec4 diffuse;
  vec4 ambient;
  bool enabled;
};

And then i define 4 disabled lights:

uniform Light light0 = Light(vec3(0.0, 0.0, 0.0), vec4(0.0, 0.0, 0.0, 0.0), vec4(0.0, 0.0, 0.0, 0.0), false);
uniform Light light1 = Light(vec3(0.0, 0.0, 0.0), vec4(0.0, 0.0, 0.0, 0.0), vec4(0.0, 0.0, 0.0, 0.0), false);
uniform Light light2 = Light(vec3(0.0, 0.0, 0.0), vec4(0.0, 0.0, 0.0, 0.0), vec4(0.0, 0.0, 0.0, 0.0), false);
uniform Light light3 = Light(vec3(0.0, 0.0, 0.0), vec4(0.0, 0.0, 0.0, 0.0), vec4(0.0, 0.0, 0.0, 0.0), false);

In my program I'm trying to get the uniform locations so I can change the lights, so I use this code.

int[][] lightLocs = new int[MAX_LIGHTS][4]; //Position, Diffuse, Ambient, Enabled
for(int i = 0; i < MAX_LIGHTS; i++) { //MAX_LIGHTS is set to 4
    lightLocs[i][0] = GL20.glGetUniformLocation(shaderProgram, "light"+i+".position");
    lightLocs[i][1] = GL20.glGetUniformLocation(shaderProgram, "light"+i+".diffuse");
    lightLocs[i][2] = GL20.glGetUniformLocation(shaderProgram, "light"+i+".ambient");
    lightLocs[i][3] = GL20.glGetUniformLocation(shaderProgram, "light"+i+".enabled");
    //Print locations just for bug testing
    for(int j = 0; j < 4; j++) {
        System.out.print(lightLocs[i][j] + ", ");
    }
    System.out.println();
}

But I'm getting some strange results. All 4 locations for the first 3 lights (0-2) return -1. the position location on light3 also returns -1, but the last 3 values return 4, 5, and 6. The 4, 5, and 6 make sense because I have 4 uniforms defined before the lights in the vertex shader, but why am I getting -1 for the other locations? I have also tried this:

uniform Light light0;
uniform Light light1;
uniform Light light2;
uniform Light light3;

to see if it would work, but then the rest of the uniform locations for the lights return as -1.

Unknownloner
  • 107
  • 8

1 Answers1

2

Uniforms that are not used in your shader code will often be optimized out. These uniforms are not considered "active" and therefore have a location of -1.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • Later in my code I call `Light[] lights = Light[4](light0, light1, light2, light3);` and then iterate through them. I just tried referencing 1 light directly, and was able to get uniform locations for it, but if I were to add more lights I wouldn't want to have to copy paste code for each one. Is there a better way to do operations with all the lights and not have them optimized out? – Unknownloner Jan 08 '13 at 22:35
  • @Unknownloner: Why would you copy 4 variables into an array? Why not make the uniform an array and then not doing any copying? – Nicol Bolas Jan 08 '13 at 23:24