Is there a standard way to rotate around local coordinates (that is, from a modelview matrix) in OpenGL 4.0?

泄露秘密 提交于 2019-12-02 11:54:21

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:

  1. Store R in the rotation part of the 4x4 matrix.
  2. 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.

I think you're over-complicating it by thinking of the old pipeline.

Form your new transformation matrix, T. Assume you have your existing camera matrix, C. You want C', the modified camera matrix.

There's really only two options. Either:

C' = CT, or
C' = TC

Because what other information have you got?

In practice what you're picking between is the new transform taking effect "before" the existing camera matrix, in which case it'll act in world coordinates, or "after" in which case it'll take place in camera coordinates.

You seem to want pre-multiplication. So use that. The transformation will then take effect in the global coordinate space.

The old OpenGL routines always post multiplied.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!