我的问题是,我无法获得某些制服的位置,而其他制服的位置我却没有问题。
例如,我的 VS 制服称为“MVP”和“模特”,并且“MVP”位置没有问题,但我没有对称使用的“模特”位置。
以同样的方式,我无法从 FS 中的 Light 结构中获取字段的位置。
其代码将数据传递到着色器:
bool Shader::SetShaderParameters(mat4 MVPMatrix,mat4 Model,GLint textureUnit)
{
GLuint matrixLocation = glGetUniformLocation(programID,"MVP");
if(matrixLocation==-1)
return false;
glUniformMatrix4fv(matrixLocation,1,GL_FALSE,&MVPMatrix[0][0]);
GLuint modelLocation = glGetUniformLocation(programID,"Model");
if(modelLocation==-1)
return false;
glUniformMatrix4fv(modelLocation,1,GL_FALSE,&Model[0][0]);
return true;
}
bool Shader::SetShaderLights(vec3 lightColor,vec3 ambientIntensity,vec3 direction,vec3 diffuseIntensity)
{
GLuint lightColorLocation = glGetUniformLocation(programID,"light.lightColor");
if(lightColorLocation==-1)
return false;
glUniform3fv(lightColorLocation,1,&lightColor[0]);
GLuint ambientIntensityLocation = glGetUniformLocation(programID,"light.ambientIntensity");
if(ambientIntensityLocation==-1)
return false;
glUniform3fv(ambientIntensityLocation,1,&ambientIntensity[0]);
GLuint directionLocation = glGetUniformLocation(programID,"light.direction");
if(directionLocation==-1)
return false;
glUniform3fv(directionLocation,1,&direction[0]);
GLuint diffuseIntensityLocation = glGetUniformLocation(programID,"light.diffuseIntensity");
if(diffuseIntensityLocation==-1)
return false;
glUniform3fv(diffuseIntensityLocation,1,&diffuseIntensity[0]);
return true;
}
这是着色器的代码:
VS:
#version 440 core
layout(location=0) in vec3 vertexPosition;
layout(location=1) in vec2 vertexUV;
layout(location=2) in vec3 normal;
out vec2 uv;
out vec3 norm;
uniform mat4 Model;
uniform mat4 MVP;
void main()
{
gl_Position = MVP*vec4(vertexPosition,1.0);
uv = vertexUV;
norm = (Model*vec4(normal,0.0)).xyz;
}
FS:
#version 440 core
struct Light
{
vec3 lightColor;
vec3 ambientIntensity;
vec3 direction;
vec3 diffuseIntensity;
};
in vec2 uv;
in vec3 norm;
out vec3 color;
uniform sampler2D texSamp;
uniform Light light;
void main()
{
color = texture(texSamp,uv).rgb*light.ambientIntensity;
}
在获取制服位置时,各种行为可能会出现什么问题?也许我使用了不好的着色器版本,或者是着色器中数据的优化。我正在调试我的解决方案,并且非常确定着色器程序 ID 是正确的。
[编辑]:
当我尝试在 FS 中使用范数时,没有任何变化,并且在无法获取位置的 glGetUniformLocation() 之后,我得到零错误代码。