3
我在做一個簡單的WebGL演示。我有一個簡單的頂點着色器,它有兩個屬性和一些制服。下面是代碼:WebGL:glsl屬性問題,getProgramParameter返回錯誤屬性數
attribute vec3 v_position;
attribute vec3 v_normal;
uniform mat4 mvMatrix;
uniform mat4 pMatrix;
uniform mat3 normalMatrix;
uniform vec3 lightPosition;
// Color to fragment program
varying vec3 transformedNormal;
varying vec3 lightDir;
void main(void)
{
// Get surface normal in eye coordinates
transformedNormal = normalMatrix * v_normal;
// Get vertex position in eye coordinates
vec4 position4 = mvMatrix * vec4(v_position.xyz,1.0);
vec3 position3 = position4.xyz/position4.w;
// Get vector to light source
lightDir = normalize(lightPosition - position3);
// Don't forget to transform the geometry!
gl_Position = pMatrix * mvMatrix * vec4(v_position.xyz,1.0);
}
出於某種原因,當我打電話
gl.getProgramParameter(shaderProgram, gl.ACTIVE_ATTRIBUTES);
我收到的1計數時,我應該得到2;
我不確定這裏有什麼問題。如果你需要它,這是片段着色器與它去:
#ifdef GL_ES
precision highp float;
#endif
uniform vec4 ambientColor;
uniform vec4 diffuseColor;
uniform vec4 specularColor;
varying vec3 transformedNormal;
varying vec3 lightDir;
void main(void)
{
// Dot product gives us diffuse intensity
float diff = max(0.0, dot(normalize(transformedNormal), normalize(lightDir)));
// Multiply intensity by diffuse color, force alpha to 1.0
vec4 out_color = diff * diffuseColor;
// Add in ambient light
out_color += ambientColor;
// Specular Light
vec3 vReflection = normalize(reflect(-normalize(lightDir), normalize(transformedNormal)));
float spec = max(0.0, dot(normalize(transformedNormal), vReflection));
if(diff != 0.0) {
float fSpec = pow(spec, 128.0);
out_color.rgb += vec3(fSpec, fSpec, fSpec);
}
gl_FragColor = vec4(1.0,0.0,0.0, 1.0);
}
哎呀,我知道這很簡單。我正在調試其他東西,並將該行更改爲測試,我猜這個問題在改變之前並不存在。謝謝。如果有辦法發現這種情況,這將是很酷的。 – nkassis
@nkassis:你有辦法找出答案。你剛纔的方式; OpenGL告訴你一個屬性沒有被使用,因爲它沒有被激活。 –