WebGL:glsl 属性问题,getProgramParameter 返回错误的属性数量
我正在制作一个简单的 WebGL 演示。我有一个简单的顶点着色器,它需要两个属性和一些制服。代码如下:
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);
我应该得到 2 的计数却得到了 1;
我不确定这里出了什么问题。如果您需要它,这是与之配套的片段着色器:
#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);
}
I'm making a simple WebGL demo. I have a simple vertex shader that takes two attributes and some uniforms. Here is the code:
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);
}
For some reason when I call
gl.getProgramParameter(shaderProgram, gl.ACTIVE_ATTRIBUTES);
I get a count of 1 when I should be getting 2;
I'm not sure what is wrong here. If you need it this is the fragment shader that goes with it:
#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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这是由于 GLSL 编译器的智能所致。在片段着色器中,您为最后一行中的
gl_FragColor
指定一个常量颜色。因此,所有好的计算和所有的变化都会被优化掉。因此,由于transformedNormal
已经被优化掉,您也不需要在顶点着色器中计算它的值。因此,您的v_normal
属性也被优化掉了(您的 GLSL 编译器多么智能,将两个着色器减少到一行,这不是很好吗)。这就是它被称为ACTIVE_ATTRIBUTES
的原因,而不仅仅是ATTRIBUTES
或DECLARED_ATTRIBUTES
(这些常量不存在,是我编造的)。尝试将
out_color
分配给gl_FragColor
,什么都不会被优化。This is due to the smartness of your GLSL compiler, I think. In your fragment shader you assign a constant color to
gl_FragColor
in the last line. Therefore all your nice computations, and all the varyings get optimized away. So astransformedNormal
has been optimized away, you also don't need to compute its value in the vertex shader. So yourv_normal
attribute is also optimized away (isn't it nice how smart your GLSL compiler is, to reduce both shaders to a single line). That's the reason it's calledACTIVE_ATTRIBUTES
and not justATTRIBUTES
orDECLARED_ATTRIBUTES
(those constants don't exist, I made them up).Try to assign
out_color
togl_FragColor
and nothing should get optimized away.