不知何故,修改 gl_FragColor 不会改变我的 3D 对象的最终颜色。这怎么可能?这只是突然发生的......因为之前完全相同的代码正在工作......
片段着色器:
#version 120
uniform vec4 ambient;
uniform vec4 diffuse;
uniform vec4 specular;
uniform vec4 objColor;
uniform float shininess;
uniform bool shade;
uniform sampler2D texMap;
varying vec2 st;
varying vec3 N;
varying vec3 L;
varying vec3 E;
void main(void) {
vec3 NN = normalize(N);
vec3 EE = normalize(E);
vec3 LL = normalize(L);
vec3 H = normalize(LL+EE); //Half vector
float Kd = dot(LL, NN);
float Ks = pow(max(dot(NN, H), 0.0), shininess);
vec4 amb, diff, spec;
amb = ambient;
diff = Kd*diffuse;
if(Kd <= 0.0) spec = vec4(0.0, 0.0, 0.0, 1.0);
else spec = specular * Ks;
if(shade) gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); <----------------- THIS LINE
else gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); <---------------------- THIS LINE
}
顶点着色器:
#version 120
uniform mat4 projection_matrix;
uniform mat4 modelview_matrix;
uniform mat4 transform_matrix;
uniform vec4 light_position;
attribute vec2 a_Textcoord;
attribute vec4 a_Vertex;
attribute vec4 a_Normal;
varying vec2 st;
varying vec3 N;
varying vec3 L;
varying vec3 E;
void main(void) {
vec4 clip_position = transform_matrix * a_Vertex;
gl_Position = projection_matrix * modelview_matrix * clip_position;
vec4 nn = transform_matrix * a_Normal;
N = normalize(nn.xyz);
L = light_position.xyz - clip_position.xyz;
if(light_position.w == 0.0) L = light_position.xyz;
E = clip_position.xyz;
st = a_Textcoord;
}
如片段着色器中所示,“<----- 这条线”,任何我着色器的对象,都是黑色而不是红色。我检查了所有发送到着色器的变量值,它们与预期相符,并且 ModelView Matrix 和 Projection Matrix 工作正常。有人知道为什么会这样吗?