我基本上是在 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);
}