問(wèn)題描述
我正在嘗試實(shí)現(xiàn)一些功能,在這些功能中我可以使用 glm 以局部或全局方向旋轉(zhuǎn)/平移對(duì)象,例如在 3D 建模軟件中.像這樣:
i am trying to implement functions, where i can rotate/ translate an object in local or global orientation, like in 3D modeling software, using glm. Something like this:
void Rotate(float x, float y, float z, bool localOrientation);
但我不知道如何讓它工作.局部旋轉(zhuǎn)旋轉(zhuǎn)應(yīng)該是這樣的(?):
but I dont know how to get it working. Local rotation rotation should just be something like this(?):
m_Orientation *= glm::rotate(x, glm::vec3(1,0,0);
m_Orientation *= glm::rotate(y, glm::vec3(0,1,0);
m_Orientation *= glm::rotate(z, glm::vec3(0,0,1);
// (m_Orientation is glm::mat4)
但是如何將其與本地定位結(jié)合起來(lái)呢?實(shí)際上我需要在世界方向旋轉(zhuǎn)旋轉(zhuǎn)矩陣,對(duì)嗎?我希望你知道我對(duì)面向局部和全局的旋轉(zhuǎn)/平移的意思,就像在 3D 建模程序中一樣.在大多數(shù)情況下,您有一個(gè)按鈕可以在本地和全局之間切換.
But how to combine this with local orientation? Actually i need to rotate the rotation matrix in world orientation, right? I hope you know what i mean with local and global oriented rotation/translation, like it is in 3D modeling programs. In most of them you have a button to switch between local and global.
然后我將如何計(jì)算向前/向右/向上矢量?通常應(yīng)該是這樣的,對(duì)吧?:
And how would i calculating the forward/right/up vector then? normally it should be something like this, right?:
forward = m_Orientation * glm::vec4(0,0,-1,0);
我用這個(gè)嘗試了全局旋轉(zhuǎn):
I tried global rotation with this:
m_GlobalOrientation = glm::rotate(m_GlobalRotation.x, glm::vec(1,0,0);
m_GlobalOrientation *= glm::rotate(m_GlobalRotation.y, glm::vec(0,1,0);
m_GlobalOrientation *= glm::rotate(m_GlobalRotation.z, glm::vec(0,0,1);
但是只有 x 旋轉(zhuǎn)處于全局方向,y 和 z 旋轉(zhuǎn)處于局部方向,因?yàn)樗呀?jīng)圍繞 x 軸旋轉(zhuǎn).所以我需要一次旋轉(zhuǎn)所有 3 個(gè)角度(?)
but then only x rotation is in global orientation, y and z rotation is in local orientation, since it is already rotated around x axis. So I need to rotate all 3 angles at once(?)
翻譯本地應(yīng)該只是將翻譯值添加到當(dāng)前翻譯中,而本地翻譯應(yīng)該是 glm::inverse(m_Orientation) * translationVector 對(duì)嗎?
Translating local should just be adding translation values to current translation, and local translation should be glm::inverse(m_Orientation) * translationVector right?
推薦答案
在回答你的問(wèn)題之前,讓我解釋一下矩陣的一些核心概念.
Before I come to your question, let me explain some core concepts of matrices.
假設(shè)我們有以下矩陣:
其中 T
是平移,R
是旋轉(zhuǎn)矩陣.
wher T
is a translation and R
is a rotation matrix.
當(dāng)我們使用這個(gè)矩陣來(lái)變換一個(gè)頂點(diǎn)(甚至網(wǎng)格)時(shí),會(huì)有一個(gè)獨(dú)特的結(jié)果.但是,我們可以通過(guò)兩種解釋得出這個(gè)結(jié)果:
When we use this matrix to transform a vertex (or even mesh), there is one unique result. However, we can get to this result with the help of two interpretations:
解讀1:從右到左評(píng)估
如果我們從右到左評(píng)估矩陣,則所有變換都在全局坐標(biāo)系中執(zhí)行.所以如果我們變換一個(gè)位于原點(diǎn)的三角形,我們會(huì)得到以下結(jié)果:
If we evaluate the matrix from right to left, all transformations are performed in the global coordinate system. So if we transform a triangle that sits at the origin, we get the following result:
解讀2:從左到右評(píng)價(jià)
在另一種情況下,所有變換都在局部坐標(biāo)系中進(jìn)行:
In the other case, all transformations are performed in the local coordinate system:
當(dāng)然,我們得到了相同的結(jié)果.
Of course, we get the same result.
所以回到你的問(wèn)題.如果將對(duì)象的位置和方向存儲(chǔ)為矩陣 T
.您可以通過(guò)將旋轉(zhuǎn)矩陣乘以當(dāng)前矩陣的右側(cè),在其局部坐標(biāo)系中旋轉(zhuǎn)此對(duì)象.在全局系統(tǒng)中通過(guò)在左側(cè)乘以它.這同樣適用于翻譯:
So coming back to your question. If you store the position and orientation of the object as a matrix T
. You can rotate this object in its local coordinate system by multiplying a rotation matrix to the right side of the current matrix. And in the global system by multiplying it at the left side. The same applies for translation:
void Rotate(float x, float y, float z, bool localOrientation)
{
auto rotationMatrix = glm::rotate(x, glm::vec3(1,0,0));
rotationMatrix *= glm::rotate(y, glm::vec3(0,1,0));
rotationMatrix *= glm::rotate(z, glm::vec3(0,0,1));
if(localOrientation)
this->T = this->T * rotationMatrix;
else
this->T = rotationMatrix * this->T;
}
右/前/上向量是矩陣T
的列向量.您可以直接讀取它們,也可以通過(guò)將矩陣與 (1, 0, 0, 0)
(right), (0, 1, 0, 0)
相乘得到它們(up), (0, 0, 1, 0)
(for/backward) or (0, 0, 0, 1)
(position).
The right / forward / up vectors are the column vectors of the matrix T
. You can either read them directly or get them by multiplying the matrix with (1, 0, 0, 0)
(right), (0, 1, 0, 0)
(up), (0, 0, 1, 0)
(for/backward) or (0, 0, 0, 1)
(position).
如果您想了解更多相關(guān)信息,請(qǐng)查看我的 有關(guān) DirectX 中矩陣的博客文章.但它適用于使用轉(zhuǎn)置矩陣的 DirectX.因此矩陣順序是相反的.閱讀文章時(shí)請(qǐng)注意這一點(diǎn).
If you want to read more about this, take a look at my blog article about matrices in DirectX. But it's for DirectX, which uses transposed matrices. Therefore the matrix order is reversed. Watch out for that when reading the article.
這篇關(guān)于使用 glm 以局部和全局方向旋轉(zhuǎn)和平移對(duì)象的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!