在OpenGL中围绕特定点旋转对象

Rotating object in OpenGL around specific point

本文关键字:旋转 对象 OpenGL      更新时间:2023-10-16

我正在建模牛顿摇篮,我有电线/弦旋转的麻烦,因为它们目前围绕其中心旋转,而不是它们附着在框架上的地方。除了"翻译到旋转点,旋转,再翻译回来"这句话似乎行不通之外,网上的信息似乎很少。

std::stack<glm::mat4>model; 
model.push(glm::mat4(1.0f));
model.top() = glm::translate(model.top(), glm::vec3(x, y, z));
model.top() = glm::rotate(model.top(), vx, glm::vec3(1, 0, 0)); //rotating in clockwise direction around x-axis
model.top() = glm::rotate(model.top(), vy, glm::vec3(0, 1, 0)); //rotating in clockwise direction around y-axis
model.top() = glm::rotate(model.top(), vz, glm::vec3(0, 0, 1)); //rotating in clockwise direction around z-axis
model.top() = glm::scale(model.top(), glm::vec3(scale/40, scale*5, scale/40));//scale equally in all axis
model.top() = glm::translate(model.top(), glm::vec3(-x, -y, -z));
glUniformMatrix4fv(modelID, 1, GL_FALSE, &model.top()[0][0]);
glm::mat3 normalmatrix = glm::transpose(glm::inverse(glm::mat3(View * model.top())));
glUniformMatrix3fv(normalmatrixID, 1, GL_FALSE, &normalmatrix[0][0])

这是转换和旋转的位,或者它的当前迭代,堆栈实际上什么都不做。如果我的问题是不清楚想象模拟时钟臂,它将围绕时钟中心在其末端旋转,而不是由手臂本身的中心。我知道在OpenGl

中没有原生实现来围绕特定点旋转对象

作为旁注,在OpenGL (GLSL之外)中不再有任何矩阵内容的"本地"实现。

你这里的问题是,你只设置顶部()矩阵为一个值,而不是通过将它们相乘来累积转换。

稍后您可能还会注意到一个问题,即它不会移回相同的位置(在将矩阵相乘之后)。这是因为在转换回原始位置之前,您将对象缩小了很多。转换的顺序很重要。

像这样的教程提供了对所有这些东西的深入解释,可能对您非常有用:

http://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/