好吧,這是一個非常具體的問題,必須在OpenGL中進行相機轉換。當視圖打開時,旋轉點位於原點周圍 - 並且在翻譯相機時,首先進行平移,然後進行旋轉以保持原點周圍的旋轉點。在原點以外的旋轉點周圍翻譯相機
嗯,我想旋轉點從原點更改爲您點擊的六邊形瓷磚,而不會出現相機位置改變。這看起來很模糊,所以我列入了一些圖片來說明。
我不能包括照片,因爲我代表的心不是在這裏足夠高,但這裏是照片的鏈接的flickr
現在,當我點擊一個新的瓷磚改變旋轉點相機會移動以將該瓷磚保持在屏幕中央。我會張貼一個鏈接來顯示,但只有足夠的代表兩個鏈接 - 所以相反,我會發佈一個鏈接到我想如何改變旋轉點的發生(當旋轉點改變時相機不移動) 。
現在沒事了一些代碼 - 這裏是讓相機變換矩陣代碼
NSMatrix4Df rotateTransform = NSMatrix4Df::getRotationMatrix(-orientation.x,-orientation.y,-orientation.z);
NSMatrix4Df rotationTranslationTransform = NSMatrix4Df::getTranslationMatrix(rotationPoint.x,rotationPoint.y,rotationPoint.z);
NSMatrix4Df cameraTranslateTransform = NSMatrix4Df::getTranslationMatrix(position.x,position.y,position.z);
return cameraTranslateTransform * rotateTransform * rotateTransform;
哪裏方向,位置和rotationPoint都在世界空間。這是實際矩陣變換的代碼。
NSMatrix4Df NSMatrix4Df::getRotationMatrix(float xAxisAng, float yAxisAng, float zAxisAng)
{
float xRad = DegreesToRadians(xAxisAng);
float yRad = DegreesToRadians(yAxisAng);
float zRad = DegreesToRadians(zAxisAng);
NSMatrix4Df xRotMat;
NSMatrix4Df yRotMat;
NSMatrix4Df zRotMat;
xRotMat.value(1,1) = cosf(xRad); xRotMat.value(1,2) = -sinf(xRad);
xRotMat.value(2,1) = sinf(xRad); xRotMat.value(2,2) = cosf(xRad);
yRotMat.value(0,0) = cosf(yRad); yRotMat.value(0,2) = -sinf(yRad);
yRotMat.value(2,0) = sinf(yRad); yRotMat.value(2,2) = cosf(yRad);
zRotMat.value(0,0) = cosf(zRad); zRotMat.value(0,1) = -sinf(zRad);
zRotMat.value(1,0) = sinf(zRad); zRotMat.value(1,1) = cosf(zRad);
return zRotMat * yRotMat * xRotMat;
}
NSMatrix4Df NSMatrix4Df::getTranslationMatrix(float xMove, float yMove, float zMove)
{
NSMatrix4Df identity;
// set the fourth column of the matrix to the translation amount
// the rest of the matrix is the identity matrix
identity.setColumn(NSVec4Df(xMove,yMove,zMove,1),3);
return identity;
}
的NSMatrix4Df創建表示一個4×4矩陣2維陣列([4] [4])填充有單位矩陣。代碼就在這裏。
NSMatrix4Df::NSMatrix4Df():rows(MATDIM4D),
columns(MATDIM4D)
{
data[0][0] = 1; data[0][1] = 0; data[0][2] = 0; data[0][3] = 0;
data[1][0] = 0; data[1][1] = 1; data[1][2] = 0; data[1][3] = 0;
data[2][0] = 0; data[2][1] = 0; data[2][2] = 1; data[2][3] = 0;
data[3][0] = 0; data[3][1] = 0; data[3][2] = 0; data[3][3] = 1;
}
所以基本上我的問題是這樣的 - 沒有任何人知道如何使之出現在相機保持在同一地點時,旋轉點移動相機的位置移動旋轉點後平移。我試着只是增加平移旋轉點平移,像這樣......之後
NSMatrix4Df rotateTransform = NSMatrix4Df::getRotationMatrix(-orientation.x,-orientation.y,-orientation.z);
NSMatrix4Df rotationTranslationTransform = NSMatrix4Df::getTranslationMatrix(rotationPoint.x,rotationPoint.y,rotationPoint.z);
NSMatrix4Df cameraTranslateTransform = NSMatrix4Df::getTranslationMatrix(**position.x - rotationPoint.x,position.y - rotationPoint.y,position.z - rotationPoint.z**);
return cameraTranslateTransform * rotateTransform * rotateTransform;
這個工程除了當攝像機在旋轉,所有改變旋轉點之前,我想不通爲什麼。在這種情況下(當改變旋轉點之前旋轉相機時),相機將以受旋轉量影響的方式進行平移。例如,如果圍繞x軸有45度旋轉,則會導致相機在x方向上轉換額外的量。我無法弄清楚爲什麼。
我知道要旋轉相機的原點以外的其他位置,您可以平移*旋轉* - 旋轉這是我在最後一種情況下的情況,但如果旋轉相機,最終平移不正確。我認爲我的翻譯或輪換代碼一定有問題,但我不知道是什麼。
我知道這是一個複雜的問題,如果任何人有任何見解,我會感謝幫助。謝謝你的時間。