Matrix mult order in Direct3D

久未见 提交于 2019-12-03 16:48:24

The problem is the order you are multiplying the matrices to get the composite transform matrix is reversed from what it should be. You are doing: wm = rotatem * translatem, which follows the order of operations you are doing for OpenGL, but for DirectX the matrix should have been wm = translatem * rotatem

The fundamental difference between OpenGL and DirectX arises from the fact that OpenGL treats matrices in column major order, while DirectX treats matrics in row major order.

To go from column major to row major you need to find the transpose ( swap the rows and the columns ) of the OpenGL matrix.

So, if you write wm = rotatem * translatem in OpenGL, then you want the transpose of that for DirectX, which is:

wmT = (rotatem*translatem)T = translatemT * rotatemT

which explains why the order of the matrix multiply has to be reversed in DirectX.

HelloGoodbye

See this answer. In OpenGL, each subsequent operation is a pre-multiplication of all the operations before it, not a post-multiplication. You can see a matrix multiplication of a vector as a function evaluation.

If what you want is to first rotate a vector and then translate your rotated vector, which you in OpenGL would have solved by first calling glRotatef and then calling glTranslatef, you could express that using function calls as

myNewVector = translate(rotate(myOldVector))

The rotate function does this

rotate(anyVector) = rotationMatrix * anyVector

and the translate function does this

translate(anyOtherVector) = translationMatrix * anyOtherVector

so your equivalent expression using matrix multiplications would look like

myNewVector = translationMatrix * rotationMatrix * myOldVector

That is, your combined transformation matrix would look be translationMatrix * rotationMatrix.

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