OpenGL 找不到一些统一变量
OpenGL can't find some uniform variables
我正在为 OpenGL 开发照明系统,但出于某种原因,OpenGL 无法找到我在片段着色器中使用的一些统一变量。变量名称是 reflection
和 useReflectionMap
这是我的片段着色器:
#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 做了一个 class,这是它的样子:
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 没有真正的问题。
我正在为 OpenGL 开发照明系统,但出于某种原因,OpenGL 无法找到我在片段着色器中使用的一些统一变量。变量名称是 reflection
和 useReflectionMap
这是我的片段着色器:
#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 做了一个 class,这是它的样子:
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 没有真正的问题。