传递太多参数有时会起作用,为什么?

Passing too many arguments works sometimes, why?

我正在使用这个网站进行测试:http://glslsandbox.com/

这显示红色:

#ifdef GL_ES
precision mediump float;
#endif

void main( void ) {
    vec4 c = vec4(1.0, 0.0, 0.0, 1.0);
    gl_FragColor = c;
}

我可以用不同的方式改变颜色线,有时编译有时不编译:

    vec4 c = vec4(1.0, vec2(0.0), vec4(1.0)); // works
    vec4 c = vec4(vec2(1.0), vec2(0.0), 0.0); // doesn't compile
    vec4 c = vec4(1.0, vec2(0.0), vec2(1.0)); // works
    vec4 c = vec4(1.0, vec4(0.0), 0.0); // doesn't compile
    vec4 c = vec4(vec4(1.0), vec4(0.0)); // doesn't compile

为什么传递太多参数有时有效有时无效?

OpenGL Shading Language 4.60 Specification (HTML) - 5.4.2. Vector and Matrix Constructors:

[...] The arguments will be consumed left to right, and each argument will have all its components consumed, in order, before any components from the next argument are consumed. [...]
In these cases, there must be enough components provided in the arguments to provide an initializer for every component in the constructed value. It is a compile-time error to provide extra arguments beyond this last used argument.

因此,允许以下内容:

vec4 c = vec4(1.0, 2.0, 3.0, vec4(4.0));
vec3 c = vec3(vec4(4.0));

然而,以下是不允许的,因为构造函数中的最后一个元素(vec4(4.0))会导致编译时错误(提供超出此范围的额外参数是编译时错误最后使用的参数。):

vec3 c = vec3(1.0, 2.0, 3.0, vec4(4.0));

这样做的原因是应该允许从较大向量(或矩阵)。例如:

vec4 v4;
vec3 v3 = vec3(v4);