我基本上是在OpenTK中尝试Specular Lighting(OpenGL的c#实现),我设法让Ambient和Diffuse照明工作,但是现在我对GL.getUniformLocation的调用返回-1并带有镜面矢量值(和我不确定“光泽”是否正确初始化。我已经研究了关于gl.getUniformLocation的OpenTK和OpenGL的许多类型的文档,以及为什么它有时会返回一个负值,并且我“几乎”确定我正确使用了镜面均匀值。
为清楚起见,以下是使用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);
}
答案 0 :(得分:1)
GL.getUniformLocation在找不到统一变量时返回-1。即使您在着色器中列出变量,编译器也许可以对其进行优化。
您没有显示片段着色器,但如果您不在片段着色器中使用Color,则整个计算将被优化,包括您定义的制服。 在这些情况下,GL.getUniformLocation也将返回-1。