2016-12-12 20 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

非常感谢!这是问题所在,我认为这个函数在用于此目的时使用方式不同,但我可能应该考虑尝试。 –