Friday, 26 July 2013

OpenGL ES: How to rotate around a specific point instead of origin?

If you want to rotate around a point (x0, y0, z0), assuming

matrix-vector multiplication order is vector * matrix,

your current modelview matrix is M1,

the rotation matrix is MR,

translation matrix from (0, 0, 0) to (x0, y0, z0) is T(x0, y0, z0),

After rotation, the modelview matrix should be

M1*T(-x0, -y0, -z0) * MR * T(x0, y0, z0)

Note if (x0, y0, z0) is in world coordinates, it needs to be first transformed to eye coordinates by

(x0, y0, z0) * M1


OpenGL ES: Is modelview matrix column major or row major?

Modelview matrix is column major. In memory, it is like

M00, M10, M20, M30, M01, M11, M21, M31, M02, M12, M22, M32, M03, M13, M23, M33

A translation of (x, y, z) is

1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1

In GLSL, matrix vector multiplication is defined as matrix * vector.

In host program, depending on how you define your matrix vector multiplication operator, it can be either matrix * vector or vector * matrix.

For a series of transformations M1, M2, M3,

if you define matrix-vector multiplication as matrix * vector,  the total transformation is

M3 * M2 * M1

If you define it as vector * matrix, the total transformation is

M1 * M2 * M3