C# GL.getUniformLocation镜面反射照明返回负值

C# GL.getUniformLocation镜面反射照明返回负值,c#,opentk,C#,Opentk,我基本上是在OpenTK(OpenGL的c#实现)中尝试镜面照明,并且我已经设法使环境照明和漫反射照明工作,但是现在我对GL.getUniformLocation的调用返回了-1和镜面反射向量值(我不确定是否正确初始化了“反光度”)。我已经研究了OpenTK和OpenGL上与gl.getUniformLocation有关的许多类型的文档,以及为什么它有时会返回负值,我“几乎”确信我正确地使用了镜面反射均匀值 为清晰起见,以下是直接来自使用Microsoft Visual Studio 2010的

我基本上是在OpenTK(OpenGL的c#实现)中尝试镜面照明,并且我已经设法使环境照明和漫反射照明工作,但是现在我对GL.getUniformLocation的调用返回了-1和镜面反射向量值(我不确定是否正确初始化了“反光度”)。我已经研究了OpenTK和OpenGL上与gl.getUniformLocation有关的许多类型的文档,以及为什么它有时会返回负值,我“几乎”确信我正确地使用了镜面反射均匀值

为清晰起见,以下是直接来自使用Microsoft Visual Studio 2010的解决方案的代码:

Vector3 lightDirection = new Vector3(0.1f, 0.0f, 0.0f);
Vector3 ambientIllumination = new Vector3(0.3f, 0.3f, 0.3f);
Vector3 ambientReflection = new Vector3(0.3f, 0.3f, 0.3f);
Vector3 diffuseIllumination = new Vector3(0.7f, 0.7f, 0.7f);
Vector3 diffuseReflection = new Vector3(0.7f, 0.7f, 0.7f);
Vector3 specularIllumination = new Vector3(0.5f, 0.5f, 0.5f);
Vector3 specularReflection = new Vector3(0.5f, 0.5f, 0.5f);
float shininess = 0.5f;

int lightDirectionLocation = GL.GetUniformLocation(shaderProgramID, "lightDirection");
GL.Uniform3(lightDirectionLocation, ref lightDirection);

int ambientIlluminationLocation = GL.GetUniformLocation(shaderProgramID, "ambientIllumination");
GL.Uniform3(ambientIlluminationLocation, ref ambientIllumination);

int ambientReflectionLocation = GL.GetUniformLocation(shaderProgramID, "ambientReflection");
GL.Uniform3(ambientReflectionLocation, ref ambientReflection);            

int diffuseIlluminationLocation = GL.GetUniformLocation(shaderProgramID, "diffuseIllumination");
GL.Uniform3(diffuseIlluminationLocation, ref diffuseIllumination);

int diffuseReflectionLocation = GL.GetUniformLocation(shaderProgramID, "diffuseReflection");
GL.Uniform3(diffuseReflectionLocation, ref diffuseReflection);

int specularIlluminationLocation = GL.GetUniformLocation(shaderProgramID, "specularIllumination");
GL.Uniform3(specularIlluminationLocation, ref specularIllumination);

int specularReflectionLocation = GL.GetUniformLocation(shaderProgramID, "specularReflection");
GL.Uniform3(specularReflectionLocation, ref specularReflection);

int shininessLocation = GL.GetUniformLocation(shaderProgramID, "shininess");
GL.Uniform1(shininessLocation, shininess);
以及我正在使用的着色器代码:

#version 150

uniform mat4 viewMatrix;
uniform mat4 modelMatrix;
uniform mat4 projMatrix;

// Light properties
uniform vec3 ambientIllumination;
uniform vec3 diffuseIllumination;
uniform vec3 specularIllumination;
uniform vec3 lightDirection;

// Material properties
uniform vec3 ambientReflection;
uniform vec3 diffuseReflection;
uniform vec3 specularReflection;

uniform float shininess;

in vec3 position;
in vec3 normal;

out vec3 Colour;

void main()
{
    vec3 transformedNormal = normalize(vec3(viewMatrix * modelMatrix * vec4(normal, 0.0)));
    vec3 surfaceToLight = normalize(vec3(viewMatrix * vec4(-lightDirection, 0.0)));
    Colour = ambientIllumination * ambientReflection;
    float normalDotLight = dot(transformedNormal, surfaceToLight);
    if(normalDotLight > 0.0)
    {
        vec3 surfaceToEye = normalize(vec3(viewMatrix * modelMatrix * vec4(position, 1.0)));
        Colour += specularIllumination * specularReflection * pow(max(dot(reflect(surfaceToLight, transformedNormal), surfaceToEye), 0.0), shininess);
        Colour += diffuseIllumination * diffuseReflection * normalDotLight;
    }
    gl_Position = projMatrix * viewMatrix * modelMatrix * vec4(position, 1);
}

GL.getUniformLocation在找不到统一变量时返回-1。即使您在着色器中列出了变量,编译器也可以对其进行优化

您不会显示片段着色器,但如果在片段着色器中不使用颜色,则整个计算将得到优化,包括您定义的制服。 在这些情况下,GL.getUniformLocation也将返回-1