Search code examples
htmlopengl-esglslwebgl

'Constructor' has too many arguments [vertex shader]


I am working with WebGL and am writing up the vertex shader in my .html file that goes along with my .js file for my program. This mainly deals with lighting.

The error I receive is: Vertex shader failed to compile. The error log is:ERROR: 0:29: 'constructor' : too many arguments ERROR: 0:32: 'dot' : no matching overloaded function found

29 and 32 correspond to the ones in the code below (see comments)

Here is my vertex shader code

<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>

Why is this happening? Let me know if you'd like to see anything else.


Solution

  • You most probably marked the wrong line for 29. The error happens two lines below:

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

    The problem is, that a_Position is already a vec4, thus you try to call a constructor of the form vec4(vec4, float) which is not existing. Maybe you wanted to pass only the first three axis for a_Position in which case the code would be:

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

    The second error comes because you have a type mismatch. In the dot method normal is a vec3 but eyeLightPos is a vec4. The dot function is only defined for two parameters of the same type.