2014-04-17 89 views
0

所以我想用四元數和角度來控制我的相機,用我的鼠標。窗口座標到攝像機角度?

我積累的垂直/水平角度是這樣的:

void Camera::RotateCamera(const float offsetHorizontalAngle, const float offsetVerticalAngle) 
{ 
    mHorizontalAngle += offsetHorizontalAngle; 
    mHorizontalAngle = std::fmod(mHorizontalAngle, 360.0f); 

    mVerticalAngle += offsetVerticalAngle; 
    mVerticalAngle = std::fmod(mVerticalAngle, 360.0f); 
} 

,並計算我的方向是這樣的:

Mat4 Camera::Orientation() const 
{ 
    Quaternion rotation; 
    rotation = glm::angleAxis(mVerticalAngle, Vec3(1.0f, 0.0f, 0.0f)); 
    rotation = rotation * glm::angleAxis(mHorizontalAngle, Vec3(0.0f, 1.0f, 0.0f)); 

    return glm::toMat4(rotation); 
} 

和前向量,這是我需要的GLM ::的lookAt,像這個:

Vec3 Camera::Forward() const 
{ 
    return Vec3(glm::inverse(Orientation()) * Vec4(0.0f, 0.0f, -1.0f, 0.0f)); 
} 

我認爲應該這樣做,但我不知道如何在我的示例遊戲中獲得實際角度?我所擁有的是當前和以前的鼠標在窗口座標中的位置..我怎樣才能從中獲得正確的角度?

編輯:第二個想法..我的「旋轉照相機()」不能正確;由於角度在達到360°之後重置,我正在經歷橡皮帶效應......所以我如何正確累積角度?我可以總結起來沒完沒了

回答

3

以視錐的橫截面(藍圈是你的鼠標位置):

enter image description here

  • 西塔是你的FOV
  • P的一半是你的投影平面距離(不用擔心 - 它會抵消掉)

從簡單的比例可知:

enter image description here

enter image description here

enter image description here

但是從簡單的trignometry

enter image description here

所以......

enter image description here

只需計算角度psi爲每個鼠標位置和減去得到的差異。

類似的公式可以爲垂直角中找到:

enter image description here

其中A是您的縱橫比(寬度/高度)

+0

這樣的精細的答案 –

+0

的1 @MichaelIV阻止它男人,你讓我變紅......不,這只是證明(反過來)透視投影 - 它有助於圖形程序員理解背後的數學。 –

+0

確實,沒有多少人試圖以詳細的方式解釋SO中的東西:) –