我正在尝试在 GLSL 中实现 phong 着色,但镜面反射组件存在一些问题。
绿光是镜面反射分量。光(点光)在平面上方的圆圈中传播。镜面高光总是向内指向 Y 轴,光线围绕 Y 轴旋转,并向图像中看到的漫反射扇形散开。它似乎完全不受相机位置的影响,我不确定我哪里出错了。
顶点着色器代码:
#version 330 core
/*
* Phong Shading with with Point Light (Quadratic Attenutation)
*/
//Input vertex data
layout(location = 0) in vec3 vertexPosition_modelSpace;
layout(location = 1) in vec2 vertexUVs;
layout(location = 2) in vec3 vertexNormal_modelSpace;
//Output Data; will be interpolated for each fragment
out vec2 uvCoords;
out vec3 vertexPosition_cameraSpace;
out vec3 vertexNormal_cameraSpace;
//Uniforms
uniform mat4 mvMatrix;
uniform mat4 mvpMatrix;
uniform mat3 normalTransformMatrix;
void main()
{
vec3 normal = normalize(vertexNormal_modelSpace);
//Set vertices in clip space
gl_Position = mvpMatrix * vec4(vertexPosition_modelSpace, 1);
//Set output for UVs
uvCoords = vertexUVs;
//Convert vertex and normal into eye space
vertexPosition_cameraSpace = mat3(mvMatrix) * vertexPosition_modelSpace;
vertexNormal_cameraSpace = normalize(normalTransformMatrix * normal);
}
片段着色器代码:
#version 330 core
in vec2 uvCoords;
in vec3 vertexPosition_cameraSpace;
in vec3 vertexNormal_cameraSpace;
//out
out vec4 fragColor;
//uniforms
uniform sampler2D diffuseTex;
uniform vec3 lightPosition_cameraSpace;
void main()
{
const float materialAmbient = 0.025; //a touch of ambient
const float materialDiffuse = 0.65;
const float materialSpec = 0.35;
const float lightPower = 2.0;
const float specExponent = 2;
//--------------Set Colors and determine vectors needed for shading-----------------
//reflection colors- NOTE- diffuse and ambient reflections will use the texture color
const vec3 colorSpec = vec3(0,1,0); //Green spec color
vec3 diffuseColor = texture2D(diffuseTex, uvCoords).rgb; //Get color from the texture at fragment
const vec3 lightColor = vec3(1,1,1); //White light
//Re-normalize normal vectors : after interpolation they make not be unit length any longer
vec3 normVertexNormal_cameraSpace = normalize(vertexNormal_cameraSpace);
//Set camera vec
vec3 viewVec_cameraSpace = normalize(-vertexPosition_cameraSpace); //Since its view space, camera at origin
//Set light vec
vec3 lightVec_cameraSpace = normalize(lightPosition_cameraSpace - vertexPosition_cameraSpace);
//Set reflect vect
vec3 reflectVec_cameraSpace = normalize(reflect(-lightVec_cameraSpace, normVertexNormal_cameraSpace)); //reflect function requires incident vec; from light to vertex
//----------------Find intensity of each component---------------------
//Determine Light Intensity
float distance = abs(length(lightPosition_cameraSpace - vertexPosition_cameraSpace));
float lightAttenuation = 1.0/( (distance > 0) ? (distance * distance) : 1 ); //Quadratic
vec3 lightIntensity = lightPower * lightAttenuation * lightColor;
//Determine Ambient Component
vec3 ambientComp = materialAmbient * diffuseColor * lightIntensity;
//Determine Diffuse Component
float lightDotNormal = max( dot(lightVec_cameraSpace, normVertexNormal_cameraSpace), 0.0 );
vec3 diffuseComp = materialDiffuse * diffuseColor * lightDotNormal * lightIntensity;
vec3 specComp = vec3(0,0,0);
//Determine Spec Component
if(lightDotNormal > 0.0)
{
float reflectDotView = max( dot(reflectVec_cameraSpace, viewVec_cameraSpace), 0.0 );
specComp = materialSpec * colorSpec * pow(reflectDotView, specExponent) * lightIntensity;
}
//Add Ambient + Diffuse + Spec
vec3 phongFragRGB = ambientComp +
diffuseComp +
specComp;
//----------------------Putting it together-----------------------
//Out Frag color
fragColor = vec4( phongFragRGB, 1);
}
请注意,在顶点着色器中看到的 normalTransformMatrix 是模型视图矩阵的逆转置。
我正在设置一个从顶点位置到灯光、到相机和反射矢量的矢量,所有这些都在相机空间中。对于漫反射计算,我采用光矢量和法线矢量的点积,对于镜面反射分量,我采用反射矢量和视图矢量的点积。也许我对算法有一些基本的误解?
起初我以为问题可能是我没有对插值后进入片段着色器的法线进行归一化,但是添加一条线进行归一化并不会影响图像。我不知道在哪里看。
我知道网站上有很多 phong shading 问题,但每个人似乎都有一个有点不同的问题。如果有人能看出我哪里出错了,请告诉我。任何帮助表示赞赏。
编辑:好的,现在可以工作了!正如 jozxyqk 下面建议的那样,我需要对我的顶点位置进行 mat4*vec4 操作,否则会丢失翻译信息。当我第一次进行更改时,我得到了奇怪的结果,直到我意识到我在将 lightPosition_cameraSpace 传递给着色器之前在我的 OpenGL 代码中犯了同样的错误(错误是我将视图矩阵转换为 mat3用于计算,而不是将光位置矢量设置为 vec4)。一旦我编辑了这些行,着色器似乎工作正常!感谢您的帮助,jozxqk!