0

I'm working on a lighting system for OpenGL and for some reason, OpenGL can't find some uniform variables that I use in the fragment shader. The variables names are reflection and useReflectionMap and this is my fragment shader:

#version 330 core

out vec4 color;

in vec3 Vertex;
in vec2 UV;
in vec3 Normal;


uniform sampler2D tex;
uniform vec3 viewPos;


const float linear = 0.09f;
const float quadratic = 0.032f;

const float specularStrength = 1.0f;
const vec3 ambientLight = vec3(0.2f);


struct LightSourceData {
    vec3 lightColor;
    vec3 lightPos;
};

uniform LightSourceData lights[30];
uniform int validObjects;
uniform int reflection;
uniform int useReflectionMap;
uniform sampler2D reflectionMap;

void main() {
    vec3 result = vec3(0.0f);

    color = vec4(viewPos, 1.0f);

    for(int i = 0; i < validObjects; i++) {
        vec3 normal = normalize(Normal);

        vec3 lightDir = normalize(lights[i].lightPos - Vertex);
        vec3 diffuse = max(dot(normal, lightDir), 0.0) * lights[i].lightColor;

        vec3 viewDir = normalize(viewPos - Vertex);
        vec3 reflectDir = reflect(-lightDir, normal);

        vec3 specular;
        if(useReflectionMap == 0) {
            specular = specularStrength * pow(max(dot(viewDir, reflectDir), 0.0), reflection) * lights[i].lightColor;
        }
        else {
            vec3 ref = texture(reflectionMap, UV).xyz;
            specular = specularStrength * pow(max(dot(viewDir, reflectDir), 0.0), (ref.x + ref.y + ref.z) / 3.0f * 64) * lights[i].lightColor;
        }


        float dist = length(lights[i].lightPos - Vertex);
        float attenuation = 1.0 / (linear * dist + quadratic * (dist * dist));


        result += (diffuse + specular) * attenuation;
    }


    color = vec4(ambientLight + result, 1.0f) * texture(tex, UV);
}

I made a class for UniformVariables, this is how it looks like:

static GLint getLoc(GLuint program, std::string name) {
    return glGetUniformLocation(program, name.c_str());
}

UniformVar<int>::UniformVar(Shader *shader, std::string varName, int *var):
shader(shader), var(var) {
    location = getLoc(shader->getProgram(), varName);

    if(location == -1) {
        printf(PRINTF_RED);
        printf("Variable %s was not found!\n", varName.c_str());
        printf(PRINTF_DEFAULT);
        exit(1);
    }
}

void UniformVar<int>::getVar() {
    glGetUniformiv(shader->getProgram(), location, var);
}

void UniformVar<int>::setVar() {
    glUniform1i(location, *var);
}

I'm calling the variables like this

reflectionUniform(shader, "reflection", &reflection)

where reflectionUniform is my UniformVar and reflection is my variable.

Listing all uniform variables of my shader yields this result, which seems to be ok:

Uniform #0 Type: 5124 Name: validObjects
Uniform #1 Type: 35678 Name: reflectionMap
Uniform #2 Type: 35665 Name: lights[0].lightColor
Uniform #3 Type: 35665 Name: lights[0].lightPos
//0 - 29
Uniform #42 Type: 35676 Name: projection
Uniform #43 Type: 35676 Name: model
Uniform #44 Type: 35678 Name: tex
Uniform #45 Type: 35676 Name: view
Uniform #46 Type: 35665 Name: viewPos
Uniform #47 Type: 5124 Name: reflection
Uniform #48 Type: 5124 Name: useReflectionMap
user11914177
  • 885
  • 11
  • 33
  • What does this mean? What should I try? – user11914177 Jan 26 '20 at 11:34
  • 3
    I would reduce the shader code to a minimum and see if the uniform variable gets set at all. If not, your mistake is most likely not in the shader. Try removing everything and set color to vec4(reflection, 0, 0, 1) or something like that – DJSchaffner Jan 26 '20 at 11:37
  • 1
    What do you mean *"can't find some uniform variables"*. Do you mean that you do not get an resource index (`glGetUniformLocation`)? I asked you to use `reflection` somewhere else than in the `if` statement – Rabbid76 Jan 26 '20 at 11:38
  • @DJSchaffner I tried `color = vec4(viewPos + vec3(validObjects, reflection, useReflectionMap), 1.0f);` with the same result – user11914177 Jan 26 '20 at 11:43
  • @Rabbid76 my `glGetUniformLocation ` returns -1. I already tried something like ` int ii = reflection;` but it didn't help – user11914177 Jan 26 '20 at 11:45
  • 1
    @user11914177: We need the rest of your code. Provide a [mcve]. – Nicol Bolas Jan 26 '20 at 15:42
  • @Nicol Bolas I will, when I am Home – user11914177 Jan 26 '20 at 18:22
  • @NicolBolas I added more code – user11914177 Jan 26 '20 at 20:57
  • 2
    Try this: enumerate all of the uniforms and see what comes out. It will be useful information. See https://stackoverflow.com/questions/440144/in-opengl-is-there-a-way-to-get-a-list-of-all-uniforms-attribs-used-by-a-shade which contains an implementation. – 3Dave Jan 26 '20 at 21:05
  • Also: make sure you're compiling the correct version of your shader. I've had this pop up when I was (quite accidentally) compiling an older version of the shader. The easiest way to check this is to enter something like ASDFASDFASDFAD on the first line of the shader and make sure it fails to build. – 3Dave Jan 26 '20 at 21:06
  • Is the type of `reflectionUniform` `UniformVar`? Is the shader compiled and linked when `reflectionUniform` is constructed? – Rabbid76 Jan 26 '20 at 22:24
  • @Rabbid of course the variable type is correct and the shader is valid! – user11914177 Jan 27 '20 at 05:59
  • @3Dave I made sure, that the shader is up to date and that is my output for the uniforms made shorter: `Active Uniforms: 67 Uniform #0 Type: 5124 Name: validObjects Uniform #1 Type: 35665 Name: lights[0].lightColor Uniform #2 Type: 35665 Name: lights[0].lightPos Uniform #3 Type: 35665 Name: lights[1].lightColor Uniform #4 Type: 35665 Name: lights[1].lightPos Uniform #61 Type: 35676 Name: projection Uniform #62 Type: 35676 Name: model Uniform #63 Type: 35678 Name: tex Uniform #64 Type: 35676 Name: view Uniform #65 Type: 35665 Name: viewPos Uniform #66 Type: 5124 Name: reflection` – user11914177 Jan 27 '20 at 06:23

1 Answers1

0

The problem is, that some objects used other shaders, that don't have the setup for lighting. So there was no real problem from OpenGL.

user11914177
  • 885
  • 11
  • 33