TheCat
TheCat

Reputation: 737

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?

Upvotes: 6

Views: 24424

Answers (2)

Rabbid76
Rabbid76

Reputation: 211166

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

Upvotes: 12

DrakkLord
DrakkLord

Reputation: 645

The documentation uses 'genType' for generic type and mostly it shows all functions accepting this, meaning that it could be any of the base types.

I don't know why it is not more specific when it clearly says that it's a vector operation.

I think most probably it simply returns the input value if it's a 1-dimensional vector which is just one number and it will calculate the length of 2-,3- dimension vectors properly.

Here the length means the euclidean distance of a vector, not the length or count of the element's it has.

Upvotes: 1

Related Questions