简体   繁体   中英

OpenGL can't find some uniform variables

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM