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告诉你一个属性没有被使用,因为它没有被激活。 –