Search code examples
glslshaderwebgl

GLSL - length function


From the GLSL documentation (https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/length.xhtml), the length function "calculate the length of a vector".

But I don't get it, what does "length" mean here ?

For instance:

length(.5); // returns .5
length(1.); // returns 1.

So how and why are you supposed to use this function?


Solution

  • See The OpenGL ES Shading Language

    8 Built-in Functions, page 63

    When the built-in functions are specified below, where the input arguments (and corresponding output) can be float, vec2, vec3, or vec4, genType is used as the argument.

    8.4 Geometric Functions, page 68

    float length (genType x)
    

    Returns the length of vector x, i.e.,
    enter image description here


    This means the result of length(.5) is:

    sqrt(0.5 * 0.5) = 0.5
    

    and the result of length(1.) is

    sqrt(1.0 * 1.0) = 1.0