【问题标题】:'Constructor' has too many arguments [vertex shader]“构造函数”有太多参数 [顶点着色器]
【发布时间】:2016-02-28 21:32:48
【问题描述】:

我正在使用 WebGL,并在我的 .html 文件中编写顶点着色器,该文件与我的程序的 .js 文件一起使用。这主要涉及照明。

我收到的错误是:顶点着色器编译失败。错误日志是:ERROR: 0:29: 'constructor' : too many arguments 错误:0:32:“点”:找不到匹配的重载函数

29 和 32 对应下面代码中的(见 cmets)

这是我的顶点着色器代码

<script id="vertex-shader" type="x-shader/x-vertex">

attribute vec4 a_Position;
attribute vec4 a_Color;
attribute vec3 a_Normal;
attribute vec2 a_Texture;

uniform mat4 u_MvpMatrix;
uniform mat3 u_NormalMatrix;

uniform vec3 uAmbientColor; // all 3 of these passed in .js
uniform vec3 uLightPosition; //
uniform vec3 uLightColor; //

varying vec4 v_Color;
varying vec2 v_Texture;

varying vec3 vLightWeighting; 

void
main()
{

    vec4 eyeLightPos = vec4(0.0, 200.0, 0.0, 1.0); // Line 29***

    vec4 eyePosition = u_MvpMatrix * vec4(a_Position, 1.0); // vertex position in the eye space

    vec3 normal = normalize(u_NormalMatrix * a_Normal);
    float nDotL = max(dot(normal, eyeLightPos), 0.0);  // Line 32***
    v_Color = vec4(a_Color.rgb * nDotL, a_Color.a);

    v_Texture = a_Texture;


    ////*************
    vec3 eyeLightVector = normalize(eyeLightPos.xyz - eyePosition.xyz);

    vec3 eyeViewVector = -normalize(eyePosition.xyz); // eye position is at (0, 0, 0) in the eye space
    vec3 eyeReflectVector = -reflect(eyeLightVector, normal);

    float shininess = 16.0;
    float specular = pow(max(dot(eyeViewVector, eyeReflectVector),0.0), shininess);;
    vLightWeighting = uAmbientColor + uLightColor * nDotL + uLightColor * specular;
}
</script>

为什么会这样?让我知道您是否还想看其他东西。

【问题讨论】:

    标签: html opengl-es glsl webgl


    【解决方案1】:

    您很可能将错误的行标记为 29。错误发生在以下两行:

    vec4 eyePosition = u_MvpMatrix * vec4(a_Position, 1.0);
    

    问题是,a_Position 已经是 vec4,因此您尝试调用不存在的 vec4(vec4, float) 形式的构造函数。也许您只想为a_Position 传递前三个轴,在这种情况下,代码将是:

    vec4 eyePosition = u_MvpMatrix * vec4(a_Position.xyz, 1.0);
    

    第二个错误是因为你的类型不匹配。在点法中,normal 是 vec3,但 eyeLightPos 是 vec4。 dot函数只定义了两个相同类型的参数。

    【讨论】:

    • vec4 eyePosition = u_MvpMatrix * a_Position; 也可能会起作用。对于w 属性,如果未提供,则默认为1。
    • vec4 eyePosition = u_MvpMatrix * vec4(a_Position); // vertex position in the eye space
    【解决方案2】:
    vec4 eyePosition = u_MvpMatrix * vec4(a_Position);
    

    a_Position 已经有 4 个向量,而在 eyePosition 中,您将乘以 5 个向量

    是否删除最后一个轴 vec4(a_Position); 或 vec4(a_Position.xyz, 1.0);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-04
      • 2011-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多