2016-12-12 23 views
1

我正在處理涉及一些錐形網格的場景,這些錐形網格將用作延遲渲染器中的聚光燈。我需要縮放,旋轉和平移這些錐形網格,以便它們指向正確的方向。據我的老師之一,我可以旋轉錐體與方向向量對齊,並通過與該返回的矩陣其模型矩陣相乘,將它們移動到正確的位置,如何使用glm :: lookAt()來旋轉對象?

glm::inverse(glm::lookAt(spot_light_direction, spot_light_position, up));

然而,這並不似乎工作,這樣做會導致所有錐體被放置在世界原點。如果我然後用另一個矩陣手動翻譯錐體,看起來錐體甚至沒有面向正確的方向。

有沒有更好的方法來旋轉對象,以便它們面對特定的方向?

這裏是獲取每個錐執行我當前的代碼,

//Move the cone to the correct place 
glm::mat4 model = glm::mat4(1, 0, 0, 0, 
          0, 1, 0, 0, 
          0, 0, 1, 0, 
          spot_light_position.x, spot_light_position.y, spot_light_position.z, 1); 

// Calculate rotation matrix 
model *= glm::inverse(glm::lookAt(spot_light_direction, spot_light_position, up)); 

float missing_angle = 180 - (spot_light_angle/2 + 90); 

float scale = (spot_light_range * sin(missing_angle))/sin(spot_light_angle/2); 

// Scale the cone to the correct dimensions 
model *= glm::mat4(scale, 0,  0,     0, 
        0,  scale, 0,     0, 
        0,  0,  spot_light_range, 0, 
        0,  0,  0,     1); 

// The origin of the cones is at the flat end, offset their position so that they rotate around the point. 
model *= glm::mat4(1, 0, 0, 0, 
        0, 1, 0, 0, 
        0, 0, 1, 0, 
        0, 0, -1, 1); 

我已經注意到這個的意見,但我會在的平端的中心再次,該錐體的起源是提錐,我不知道這是否有所作爲,我只是想我會提出來。

+0

'lookAt'已經包含了翻譯。你不需要再次這樣做。即(model = glm :: inverse(glm :: lookAt(...'(不要乘)) –

+0

@NicoSchertler正如我在帖子中所說,如果我不手動包含翻譯,那麼模型會被放置在世界的起源我做了其他的東西嗎? –

+0

你混淆了'lookAt'的參數,參見[documentation](https://glm.g-truc.net/0.9.2/api/a00245.html) 。我還沒有確信剩餘矩陣的順序。 –

回答

2

你矩陣的順序似乎是正確的,但的lookAt功能預期:

glm::mat4 lookAt (glm::vec3 eye, glm::vec3 center, glm::vec3 up) 

這裏映入眼簾的是攝像頭的位置,中心是您正在尋找的對象的位置(在你的情況下,如果你沒有那個位置,你可以使用 spot_light_direction + spot_light_position)。

所以才改變

glm::lookAt(spot_light_direction, spot_light_position, up) 

glm::lookAt(spot_light_position, spot_light_direction + spot_light_position, up) 
+0

非常感謝!這是問題所在,我認爲這個函數在用於此目的時使用方式不同,但我可能應該考慮嘗試。 –