GLSL Light structure

Started by
5 comments, last by dpadam450 6 years, 3 months ago

I have this code below in both my vertex and fragment shader, however when I request glGetUniformLocation("Lights[0].diffuse") or "Lights[0].attenuation", it returns -1. It will only give me a valid uniform location if I actually use the diffuse/attenuation variables in the VERTEX shader. Because I use position in the vertex shader, it always returns a valid uniform location. I've read that I can share uniforms across both vertex and fragment, but I'm confused what this is even compiling to if this is the case.

 

#define NUM_LIGHTS 2

struct Light
{
    vec3 position;
    vec3 diffuse;
    float attenuation;
};
uniform Light Lights[NUM_LIGHTS];

 

 

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

Advertisement
18 minutes ago, dpadam450 said:

It will only give me a valid uniform location if I actually use the diffuse/attenuation variables in the VERTEX shader.

Did you use diffuse/attenuation variables in fragment shader too when getting -1? If not, probably compiler removed them as unused code so you can't get their locations

That's correct. Using it in the fragment shader seems to not effect anything. Copy/pasted the structure declarations, they are identical.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

I tried the same it worked for me. Copy-pasting structure is not enough it must be used in frag or vert shader, also I tried to use only position in vertex shader even that case I got others' locations (0, 1, 2). It seems GLSL compiler don't remove individual members of  structure, if I don't use any structure member then I get all -1

if you are using uniform blocks you must use layout( std140 ) or layout( shared ) to ensure the compiler will not optimize away unused uniforms

I must have been smoking crack. It all works now with no changes. I think I was editing the wrong fragment shader for the vertex shader. And therefore it was bad testing.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

This topic is closed to new replies.

Advertisement