WebGL:glsl 属性问题,getProgramParameter 返回错误的属性数量

发布于 2024-12-03 09:15:41 字数 1843 浏览 1 评论 0原文

我正在制作一个简单的 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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

潇烟暮雨 2024-12-10 09:15:41

我认为这是由于 GLSL 编译器的智能所致。在片段着色器中,您为最后一行中的 gl_FragColor 指定一个常量颜色。因此,所有好的计算和所有的变化都会被优化掉。因此,由于 transformedNormal 已经被优化掉,您也不需要在顶点着色器中计算它的值。因此,您的 v_normal 属性也被优化掉了(您的 GLSL 编译器多么智能,将两个着色器减少到一行,这不是很好吗)。这就是它被称为 ACTIVE_ATTRIBUTES 的原因,而不仅仅是 ATTRIBUTESDECLARED_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 as transformedNormal has been optimized away, you also don't need to compute its value in the vertex shader. So your v_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 called ACTIVE_ATTRIBUTES and not just ATTRIBUTES or DECLARED_ATTRIBUTES (those constants don't exist, I made them up).

Try to assign out_color to gl_FragColor and nothing should get optimized away.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文