Search code examples
vectorglslopengl-3variable-length

illegal vector field selection length


I am receiving this error on a pc where I cannot have access.

The error is on the Vertex Shader of the init program of the depth peeling technique.

ERROR: 0:42: error(#222) Illegal vector field selection length
ERROR: 0:42: error(#222) Illegal vector field selection length
ERROR: error(#273) 2 compilation errors. No code generated

This is the VS:

#version 330

layout (location = 0) in vec3 position;
layout (location = 1) in vec3 normal;
layout (location = 2) in vec2 uv;

out vec3 cameraSpacePosition;
out vec3 cameraSpaceNormal;

out vec2 oUV;

uniform mat4 modelToWorldMatrix;
/*
*   Layout {lighting, normal orientation, hasTexture, selected}
*/
uniform ivec4 settings;

layout(std140) uniform vehicleMatrices {
    mat4 worldToCameraMatrix;
    mat4 cameraToClipMatrix;
};

void main()
{
    vec4 c = worldToCameraMatrix * (modelToWorldMatrix * vec4(position, 1.0));
    gl_Position = cameraToClipMatrix * c;

    cameraSpacePosition = c.xyz;
    cameraSpaceNormal = mat3(worldToCameraMatrix) * (mat3(modelToWorldMatrix) * normal);

    switch (settings.y) {

        case 0:
            cameraSpaceNormal = -cameraSpaceNormal;
            break;

        case 1:
            float cosine = dot(cameraSpacePosition, cameraSpaceNormal) / (cameraSpacePosition.length * cameraSpaceNormal.length);
            if ( cosine > 0) 
                cameraSpaceNormal = -cameraSpaceNormal;
            break;
    }
    oUV = uv;
}

I never had problem with it before, I dev mostly on Nvidia and this one comes from a machine equipped with an ATI FirePro M7740

I also tried to google, of course, but there is no result with the quoted sentence: "illegal vector field selection length"..

I checked tens of time and it looks fine to me, no error on any vector length..

Any clue?


Solution

  • The problem is exactly what the error message says:

    Illegal vector field selection length

    The only line where you try to use length as a field selector is this one (line break added):

    float cosine = dot(cameraSpacePosition, cameraSpaceNormal) /
                   (cameraSpacePosition.length * cameraSpaceNormal.length);
    

    This usage of length is invalid. There are two valid usages of length in GLSL:

    1. As a method on array variables, to find the number of elements in an array. Say if a is an array variable, you can write a.length() to get the number of elements. Note the parentheses.
    2. As a built-in function to calculate the length of a vector. If v is a vector, the length is calculated as length(v). Note the function syntax.

    Based on the context, you're looking for option 2. The correct syntax for that line is:

    float cosine = dot(cameraSpacePosition, cameraSpaceNormal) /
                   (length(cameraSpacePosition) * length(cameraSpaceNormal));