1
這裏是我的頂點着色器:
#version 400
layout (location = 0) in vec3 VertexPosition;
layout (location = 1) in vec3 VertexNormal;
out vec3 LightIntensity;
struct LightInfo {
vec3 Position; // Light position in eye coords.
vec3 La; // Ambient light intensity
vec3 Ld; // Diffuse light intensity
vec3 Ls; // Specular light intensity
};
uniform LightInfo Light;
struct MaterialInfo {
vec3 Ka; // Ambient reflectivity
vec3 Kd; // Diffuse reflectivity
vec3 Ks; // Specular reflectivity
float Shininess; // Specular shininess factor
};
uniform MaterialInfo Material;
uniform mat4 ModelViewMatrix;
uniform mat3 NormalMatrix;
uniform mat4 ProjectionMatrix;
uniform mat4 MVP;
void main()
{
mat4 normalMatrixBetter = transpose(inverse(ModelViewMatrix));
vec3 normalEyeSpace = normalize(vec3(normalMatrixBetter * vec4(VertexNormal, 0.0)));
//vec3 tnorm = normalize(normalMatrixBetter * VertexNormal);
vec4 vertexPositionInEyeCoords = ModelViewMatrix * vec4(VertexPosition, 1.0);
vec3 s = normalize(vec3(Light.Position.xyz - vertexPositionInEyeCoords.xyz));
vec3 v = normalize(-vertexPositionInEyeCoords.xyz);
vec3 r = reflect(-s, normalEyeSpace);
vec3 ambient = Light.La * Material.Ka;
float sDotN = max(dot(s, normalEyeSpace), 0.0);
vec3 diffuse = Light.Ld * Material.Kd * sDotN;
vec3 spec = vec3(0.0);
if (sDotN > 0.0)
{
spec = Light.Ls * Material.Ks * pow(max(dot(r, v), 0.0), Material.Shininess);
}
// LightIntensity = ambient;
// LightIntensity = diffuse;
// LightIntensity = spec;
// LightIntensity = ambient + diffuse;
LightIntensity = /*ambient +*/ diffuse + spec;
gl_Position = MVP * vec4(VertexPosition, 1.0);
}
這裏是片段:
#version 400
in vec3 LightIntensity;
layout(location = 0) out vec4 FragColor;
void main()
{
FragColor = vec4(LightIntensity, 1.0) * 0.3;
}
這裏有相應的光強和反射係數:
vec3 lightSourceAmbientIntensity(1.0f, 1.0f, 1.0f); // La, Light source ambient intensity
vec3 lightSourceDiffuseIntensity(10.0f, 10.0f, 10.0f); // Ld, Light source diffuse intensity
vec3 lightSourceSpecularIntensity(1.0f, 1.0f, 1.0f); // Ls, Light source specular intensity
vec3 ambientReflectivity(0.5f, 0.0f, 0.0f); // Ka,
vec3 diffuseReflectivity(1.0f, 0.0f, 0.0f); // Kd,
vec3 specularReflectivity(0.0f, 0.9f, 0.0f); // Ks,
GLfloat materialShineness = 1.0f;
當LightIntensity = spec;
我看到應該發光的綠色三角形,其餘的三角形是黑色的。 LightIntensity = diffuse;
當我看到所有三角形上的漫反射紅色陰影,這些三角形會照射到相機上並反射到相機位置。但是當我寫LightIntensity = /*ambient +*/ diffuse + spec;
三角形得到黑色是唯一的特殊陰影的情況下。這裏是我的問題:如何將紅色添加到綠色並變黑?爲什麼當我在Phong底紋顏色中添加鏡面光照時不能正確添加?
添加圖像。請考慮我的形狀圍繞Y軸旋轉。謝謝! – Narek