我正在开发一个场景,其中涉及一些锥形网格,这些网格将用作延迟渲染器中的聚光灯。我需要缩放,旋转和平移这些锥形网格,使它们指向正确的方向。根据我的一位讲师的说法,我可以旋转锥体以与方向矢量对齐,并通过将其模型矩阵与由此返回的矩阵相乘来将它们移动到正确的位置,
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 :(得分: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)