Vectors, Matrices & Swizzling
Column-major means the constructor fills columns first, and M[i] is a column.
7.1 Component access & swizzling
vec4 v = vec4(1.0, 2.0, 3.0, 4.0);
v.x; v.y; v.z; v.w; // position names
v.r; v.g; v.b; v.a; // color names (same components)
v.s; v.t; v.p; v.q; // texture-coordinate names
v[2]; // array-style index → 3.0
vec2 a = v.xy; // (1, 2)
vec3 b = v.zyx; // (3, 2, 1) — reorder
vec4 c = v.xxyy; // (1, 1, 2, 2) — repeat
vec3 rgb = v.rgb;
v.xy = vec2(9.0); // write swizzle (no repeated components on the left side)
v.wz = v.zw; // swap
// v.xg is an error: you can't mix naming sets7.2 Vector arithmetic (component-wise)
vec3 a = vec3(1.0, 2.0, 3.0), b = vec3(4.0, 5.0, 6.0);
a + b; // (5, 7, 9)
a * b; // (4, 10, 18) component-wise (NOT dot product)
a * 2.0; // scalar broadcast
dot(a, b); // 32.0
cross(a, b); // (-3, 6, -3)
length(a); distance(a, b); normalize(a);7.3 Matrices
Column-major means the constructor fills columns first, and M[i] is a column. When uploading from C/C++ with row-major data, pass GL_TRUE as the transpose argument of glUniformMatrix4fv (or use GLM, which is column-major).
mat4 M = mat4(1.0); // identity
vec4 col1 = M[1]; // second COLUMN (column-major indexing)
float e = M[3][1]; // column 3, row 1
mat2 m = mat2(1.0, 2.0, // first column (1, 2)
3.0, 4.0); // second column (3, 4)
vec4 p = M * vec4(pos, 1.0); // matrix × column vector (transform)
vec4 q = vec4(pos, 1.0) * M; // row vector × matrix = transpose(M) * v
mat4 MVP = P * V * M; // composition: applied right to left
matrixCompMult(A, B); // component-wise product
transpose(M); inverse(M); determinant(M);
outerProduct(u, v); // u * vᵀ