Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GLSL - length function

Tags:

glsl

shader

webgl

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?

like image 904
TheCat Avatar asked Jul 20 '26 02:07

TheCat


1 Answers

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
like image 97
Rabbid76 Avatar answered Jul 21 '26 16:07

Rabbid76