【发布时间】:2021-08-25 12:49:48
【问题描述】:
我目前正在使用 GLM 在 OpenGL 中转换形状。但是,当形状的旋转在 1 到 2 弧度之间时,表示旋转的矩阵的值将设置回它们在单位矩阵中的值。我目前正在将模型矩阵输出到控制台,这有助于找出它转回 0 旋转弧度的确切时刻。就是这样:
对不起,如果很难理解,我已经尝试对其进行注释,因为否则看起来很烦人。如果需要,可以直接在图片中添加link。
控制台中每行的第一个值是以弧度为单位的单个值的旋转。然后输出形状的模型矩阵。
上传它的视频很烦人,但我相信你可以想象它 - 它只是一个正常旋转一段时间的形状,直到旋转达到一个弧度,此时它会重置为正常旋转。然后,当它达到两个弧度时,它会恢复正常,就好像从来没有问题一样。
以下是变形相关的代码:
// NOTE: the variables 'active_translation, active_rotation, and active_scaling' are the values the user gives to translate, rotate, and scale the shape
// In this case, 'active_rotation' is equal to ~0.02 (1 degree in radians) every frame
void defshape::apply_transformations() {
// Create an identity matrix to store the applied transformations
glm::mat4 applied_transformation = glm::mat4(1.0f);
// Add the active translation to the stored transformations variable
applied_transformation = glm::translate(applied_transformation, active_translation);
// Apply the rotation to the mat4. For the sake of this example, I've only showed the Z axis line to simplify it as it's the only one being affected.
applied_transformation = glm::rotate(applied_transformation, active_rotation.z, glm::vec3(0, 0, 1));
// Add the active scale to the stored transformations variable
applied_transformation = glm::scale(applied_transformation, active_scaling);
// Apply these changes to the renderer by setting its model matrix and then updating it
renderer_handle->model_matrix = applied_transformation;
renderer_handle->update_renderer();
// Reset the translation of the model matrix back to 0
// This step ensures that the object rotates around its local origin, rather than the world space origin
applied_transformation = glm::translate(applied_transformation, glm::vec3(0.0f));
// Then apply this change to the renderer's model matrix
renderer_handle->model_matrix = applied_transformation;
renderer_handle->update_renderer();
}
这可能与 GLM 有关,还是我的代码有问题?
【问题讨论】: