Direct3D中的矩阵多阶 [英] Matrix mult order in Direct3D

查看:23
本文介绍了Direct3D中的矩阵多阶的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于在 Direct3D 中乘以矩阵以获得结果,我收到了两个相互矛盾的答案.教程确实说明从左到右相乘,这很好,但这不是我想象的方式.

I've received two conflicting answers in terms of multiplying matrices in Direct3D to achieve results. Tutorials do state to multiply from left to right and that's fine but it's not how I would visualize it.

这是一个例子:

OpenGL(从上到下阅读):

OpenGL (reading from top to bottom):

GLRotatef(90.0f);
GLTranslatef(20.0f,0,0);

所以你想象世界轴旋转了 30 度.然后你在现在旋转的 x 轴上平移 20.0,这样看起来你是在世界 y 轴上上升.

So you visualize the world axis rotating 30 degrees. Then you translate 20.0 on the now rotated x-axis so it looks like you are going up on the world y-axis.

在 Direct3D 中,执行:

In Direct3D, doing:

wm = rotatem * translatem;

不一样.看起来对象只是在原点旋转并在世界的 x 轴上平移,因此它向右而不是向上.只有在我颠倒顺序并从右向左阅读时才有效.

is different. It looks like the object was just rotated at the origin and translated on the world's x-axis so it goes to the right and not up. It only works once I reverse the order and read from right to left.

此外,在 frank luna 关于 DX10 的书中,他解释了如何进行镜面反射.我得到了所有这些,但是当他这样做时:

Also for example, in frank luna's book on DX10, he goes into explaining how to do mirror reflections. I get all of that but when he does for example:

reflection_matrix = world_m * reflection_m;

围绕 xy 平面,我是否将此解释为首先进行世界定位然后进行反射或相反?

around the xy plane, do I interpret this as first doing a the world positioning then a reflection or the opposite?

推荐答案

问题是您将矩阵相乘以获得复合变换矩阵的顺序与应有的顺序相反.您正在执行:wm = rotatem * translatem,它遵循您为 OpenGL 执行的操作顺序,但对于 DirectX,矩阵应该是 wm = translatem * rotatem

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

OpenGL 和 DirectX 之间的根本区别在于,OpenGL 按列主顺序处理矩阵,而 DirectX 按行主顺序处理矩阵.

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.

要从主要列转到主要行,您需要找到 OpenGL 矩阵的转置(交换行和列).

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

因此,如果您在 OpenGL 中编写 wm = rotatem * translatem,那么您需要为 DirectX 进行转置,即:

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

这就解释了为什么必须在 DirectX 中反转矩阵乘法的顺序.

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

这篇关于Direct3D中的矩阵多阶的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆