Skip to content

2. Swizzling

Giuseppe Barbieri edited this page Aug 5, 2017 · 3 revisions

2. Swizzling

Shader languages like GLSL often feature so-called swizzle expressions, which may be used to freely select and arrange a vector's components. For example, variable.x, variable.xzy and variable.zxyy respectively form a scalar, a 3D vector and a 4D vector. The result of a swizzle expression in GLSL can be either an R-value or an L-value. Swizzle expressions can be written with characters from exactly one of xyzw (usually for positions), rgba (usually for colors), and stpq (usually for texture coordinates).)

vec4 A; vec2 B;

B.yx = A.wy; B = A.xx; vec3 C = A.bgr; vec3 D = B.rsz; // Invalid, won't compile

GLM optionally supports some of this functionality via the methods described in the following sections. Swizzling can be enabled by defining GLM_FORCE_SWIZZLE before including any GLM header files, or as part of a project's build process.

Note that enabling swizzle expressions will massively increase the size of your binaries and the time it takes to compile them!

Clone this wiki locally