GLM does not depend on external libraries or headers such as `<GL/gl.h>`, [`<GL/glcorearb.h>`](http://www.opengl.org/registry/api/GL/glcorearb.h), `<GLES3/gl3.h>`, `<GL/glu.h>`, or `<windows.h>`. However, if we include `<boost/static_assert.hpp>`, then [`Boost.StaticAssert`](http://www.boost.org/doc/libs/release/libs/static_assert) will be used to provide compile-time errors. Otherwise, if using a C++11 compiler, the standard `static_assert` will be used instead. If neither is available, GLM will use its own implementation of `static_assert`.
GLM does not depend on external libraries or headers such as `<GL/gl.h>`, [`<GL/glcorearb.h>`](http://www.opengl.org/registry/api/GL/glcorearb.h), `<GLES3/gl3.h>`, `<GL/glu.h>`, or `<windows.h>`. However, if we include `<boost/static_assert.hpp>`, then [`Boost.StaticAssert`](http://www.boost.org/doc/libs/release/libs/static_assert) will be used to provide compile-time errors. Otherwise, if using a C++11 compiler, the standard `static_assert` will be used instead. If neither is available, GLM will use its own implementation of `static_assert`.
---
---
## <aname="section2"></a> 2. Swizzle operators
## <aname="section2"></a> 2. Swizzling
A common feature of shader languages like GLSL is the swizzle operators. Those allow selecting multiple components of a vector and change their order. For example, “variable.x”, “variable.xzy” and “variable.zxyy”
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).
form respectively a scalar, a three components vector and a four components vector. With GLSL, swizzle operators can be both R-values and L-values. Finally, vector components can be accessed using “xyzw”,
“rgba” or “stpq”.
```cpp
```glsl
vec4 A;
vec4 A;
vec2 B;
vec2 B;
...
B.yx = A.wy;
B.yx = A.wy;
B = A.xx;
B = A.xx;
vec3 C = A.bgr;
vec3 C = A.bgr;
vec3 D = B.rsz; // Invalid, won't compile
```
```
GLM supports a subset of this functionality as described in the following sub-sections. Swizzle operators are disabled by default. To enable them GLM\_SWIZZLE must be defined before any inclusion of
GLM optionally supports some of this functionality via the methods described in the following sections. Swizzling can be enabled by defining `GLM_SWIZZLE` before including any GLM header files, or as part of a project's build process.
<glm/glm.hpp>. Enabling swizzle operators will massively increase the size of compiled files and the compilation time.
### <aname="section2_1"></a> 2.1. Standard C++98 implementation
*Note that enabling swizzle expressions will massively increase the size of your binaries and the time it takes to compile them!*
The C++98 implementation exposes the R-value swizzle operators as member functions of vector types.
When compiling GLM as C++98, R-value swizzle expressions are simulated through member functions of each vector type.
```cpp
```cpp
#define GLM_SWIZZLE
#define GLM_SWIZZLE // Or defined when building (e.g. -DGLM_SWIZZLE)
#include<glm/glm.hpp>
#include<glm/glm.hpp>
void foo()
void foo()
{
{
glm::vec4 ColorRGBA(1.0f, 0.5f, 0.0f, 1.0f);
glm::vec4 ColorRGBA(1.0f, 0.5f, 0.0f, 1.0f);
glm::vec3 ColorBGR = ColorRGBA.bgr();
glm::vec3 ColorBGR = ColorRGBA.bgr();
...
glm::vec3 PositionA(1.0f, 0.5f, 0.0f, 1.0f);
glm::vec3 PositionA(1.0f, 0.5f, 0.0f, 1.0f);
glm::vec3 PositionB = PositionXYZ.xyz() \* 2.0f;
glm::vec3 PositionB = PositionXYZ.xyz() * 2.0f;
...
glm::vec2 TexcoordST(1.0f, 0.5f);
glm::vec2 TexcoordST(1.0f, 0.5f);
glm::vec4 TexcoordSTPQ = TexcoordST.stst();
glm::vec4 TexcoordSTPQ = TexcoordST.stst();
...
}
}
```
```
Swizzle operators return a copy of the component values hence they can’t be used as L-values to change the value of the variables.
Swizzle operators return a **copy** of the component values, and thus *can't* be used as L-values to change a vector's values.
```cpp
```cpp
#define GLM_FORCE_SWIZZLE
#define GLM_SWIZZLE
#include<glm/glm.hpp>
#include<glm/glm.hpp>
void foo()
void foo()
{
{
glm::vec3 A(1.0f, 0.5f, 0.0f);
glm::vec3 A(1.0f, 0.5f, 0.0f);
// /!\\ No compiler error but A is not affected
// No compiler error, but A is not modified.
// This code modify the components of an anonymous copy.
// An anonymous copy is being modified (and then discarded).
A.bgr() = glm::vec3(2.0f, 1.5f, 1.0f); // A is not modified!
A.bgr() = glm::vec3(2.0f, 1.5f, 1.0f); // A is not modified!
...
}
}
```
```
### <aname="section2_2"></a> 2.2. Anonymous union member implementation
### <aname="section2_2"></a> 2.2. Anonymous union member implementation
Visual C++ supports anonymous structures in union, which is a non-standard language extension, but it enables a very powerful implementation of swizzle operators on Windows supporting both L-value
Visual C++ supports, as a _non-standard language extension_, anonymous `struct`s as `union` members. This permits a powerful swizzling implementation that both allows L-value swizzle expressions and GLSL-like syntax. To use this feature, the language extension must be enabled by a supporting compiler and `GLM_SWIZZLE` must be `#define`d.
swizzle operators and a syntax that doesn’t require parentheses in some cases. This implementation is only enabled when the language extension is enabled and GLM\_SWIZZLE is defined.
```cpp
```cpp
#define GLM_FORCE_SWIZZLE
#define GLM_SWIZZLE
#include<glm/glm.hpp>
#include<glm/glm.hpp>
// Only guaranteed to work with Visual C++!
// Some compilers that support Microsoft extensions may compile this.
void foo()
void foo()
{
{
glm::vec4 ColorRGBA(1.0f, 0.5f, 0.0f, 1.0f);
glm::vec4 ColorRGBA(1.0f, 0.5f, 0.0f, 1.0f);
// l-value:
// l-value:
glm::vec4 ColorBGRA = ColorRGBA.bgra;
glm::vec4 ColorBGRA = ColorRGBA.bgra;
// r-value:
// r-value:
ColorRGBA.bgra = ColorRGBA;
ColorRGBA.bgra = ColorRGBA;
// Both l-value and r-value
// Both l-value and r-value
ColorRGBA.bgra = ColorRGBA.rgba;
ColorRGBA.bgra = ColorRGBA.rgba;
...
}
}
```
```
Anonymous union member swizzle operators don’t return vector types (glm::vec2, glm::vec3 and glm::vec4) but implementation specific objects that can be automatically interpreted by other swizzle operators and
This version returns implementation-specific objects that _implicitly convert_ to their respective vector types. As a consequence of this design, these extra types **can't be directly used** by GLM functions; they must be converted through constructors or `operator()`.
vector constructors. Unfortunately, those can’t be interpreted by GLM functions so that the programmer must convert a swizzle operators to a vector type or call the () operator on a swizzle objects to pass it to another C++ functions.
```cpp
```cpp
#define GLM_FORCE_SWIZZLE
#define GLM_SWIZZLE
#include<glm/glm.hpp>
#include<glm/glm.hpp>
using glm::vec4;
void foo()
void foo()
{
{
glm::vec4 Color(1.0f, 0.5f, 0.0f, 1.0f);
vec4 Color(1.0f, 0.5f, 0.0f, 1.0f);
...
// Generates compiler errors. Color.rgba is not a vector type.
// Generates compiler errors. Color.rgba is not a vector type.