c++ - OpenGL找不到一些统一变量

标签 c++ opengl glsl opengl-3

我正在为OpenGL设计照明系统,由于某种原因,OpenGL无法找到我在片段着色器中使用的某些统一变量。变量名称是reflectionuseReflectionMap,这是我的片段着色器:

#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);
}

我为UniformVariables创建了一个类,它看起来像这样:
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);
}

我这样称呼变量
reflectionUniform(shader, "reflection", &reflection)

其中reflectionUniform是我的UniformVar,而reflection是我的变量。

列出我的着色器的所有统一变量都会产生此结果,这似乎可以:
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

最佳答案

问题在于,某些对象使用了其他着色器,而这些着色器没有用于照明的设置。因此,OpenGL没有真正的问题。

关于c++ - OpenGL找不到一些统一变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59917509/

相关文章:

c++ - 在 C++ 中减少 `constexpr` 对象生命周期是否合法?

c++ - GLSL(1.2) + VBO + 纹理

c++ - 将clamp()添加到片段着色器时,OpenGL GLSL在标记 "::"处期望 "<int-const>"

opengl - 从clipspace.xyz和(inv)投影矩阵计算clipspace.w

c++ - 使用 ccache/clang 编译 Qt 代码时避免多余的警告

c++ - 从具有多种类型的 C++ 文件中读取结构

c++ - 每像素照明黑点/奇怪的伪影

c - 向 OpenGL 立方体添加纹理?

c++ - OpenGL XUbuntu 14.04 glShaderSource、glCompileShader、glCreateProgram 函数未声明

c++ - 是否有可以在 iOS 上运行的本地键值存储数据库(与 Redis 不同)?