You can do basically the same thing as the sequence you posted without doing full matrix multiplies at runtime. Since the translation matrices are very simple, you can to the multiplication manually. With R
being a 3x3 rotation matrix, and p
being the position you want to rotate around, you can build your 4x4 matrix by:
- Store
R
in the rotation part of the 4x4 matrix. - Calculate
p - R * p
, and store the result in the translation part of the 4x4 matrix.
This would reduce it to one matrix * vector multiply, and a vector subtraction. You may want to double check my math, since I just derived it on paper.
Another approach is that you keep rotations and translations separate in your CPU code, update them as needed, and only combine them into a 4x4 matrix when updating the uniform.
Since you have control over the shader code, you don't even have to use a 4x4 matrix to contain the entire transformation. For example, if it's more convenient for you to apply translations before rotations, you can feed a mat3 rotation matrix and a vec3 translation vector into your shader, and add the translation vector to the position before multiplying with the rotation matrix. Keeping the two separate can also be beneficial if you update one of them much more frequently than the other.