opengl用glm单独旋转一个对象

时间:2023-01-30 20:05:19

I have an object that I need to rotate. For some reason known only to the computer my entire scene gets rotated, that is all the objects get rotated as a group. Please note that all the objects are from the same class. I wanted to rotate these objects individually in the same time. I can't post all the code but here are the relevant parts. I'll add more code if asked to. This is where I periodically update the rotation angle:

我有一个需要旋转的物体。由于某些原因,只有计算机知道我的整个场景都被旋转,即所有对象都作为一组旋转。请注意,所有对象都来自同一个类。我想在同一时间单独旋转这些对象。我无法发布所有代码,但这里是相关部分。如果被要求,我会添加更多代码。这是我定期更新旋转角度的地方:

void Model::Update( float dt ) {    
    mRotationAngleInDegrees += dt;
}

This is where i calculate the transform matrix:

这是我计算变换矩阵的地方:

mat4 Model::GetWorldMatrix() const {
    mat4 worldMatrix( 1.0f );
    worldMatrix = glm::translate( worldMatrix, position );
    worldMatrix = glm::rotate( worldMatrix, mRotationAngleInDegrees, vec3( 0, 0, 1 ) );
    return worldMatrix;
}

This is where I paint the model:

这是我绘制模型的地方:

void Model::Draw() {

    GLuint WorldMatrixLocation = glGetUniformLocation(Renderer::getShaderID(), "WorldTransform"); 
    glUniformMatrix4fv(WorldMatrixLocation, 1, GL_FALSE, &GetWorldMatrix()[0][0]);

    //vertex buffer code here

    glDrawArrays(GL_TRIANGLE_STRIP, 0, 9);

    //some more cleanup code here
}

And here is the relevant code in main:

以下是主要的相关代码:

for( vector<Model>::iterator it = grass.begin(); it != grass.end(); ++it ) {
    it->Update(dt);
    it->Draw();
}

Can anyone see what's the problem?

任何人都可以看到问题是什么?

1 个解决方案

#1


1  

The problem was that in function GetWorldMatrix() after rotating I was supposed to translate the model matrix back to the original location. That is necessary because the local rotation must follow these steps (in this strict order):

问题是在旋转后的函数GetWorldMatrix()中我应该将模型矩阵转换回原始位置。这是必要的,因为本地轮换必须遵循这些步骤(按照严格的顺序):

  1. translate from the original point to the anchor of rotation
  2. 从原始点转换为旋转锚点
  3. rotate
  4. 回转
  5. translate back to the original point
  6. 转回原点

So I added this line:

所以我添加了这一行:

worldMatrix = glm::translate( worldMatrix, -position );

The function now looks like this:

该功能现在看起来像这样:

mat4 Model::GetWorldMatrix() const {
    mat4 worldMatrix( 1.0f );
    worldMatrix = glm::translate( worldMatrix, position );
    worldMatrix = glm::rotate( worldMatrix, mRotationAngleInDegrees, vec3( 0, 0, 1 ) );
    worldMatrix = glm::translate( worldMatrix, -position );
    return worldMatrix;
}

Thank you, 40two, for the great hint.

谢谢,40two,这是一个很好的提示。

#1


1  

The problem was that in function GetWorldMatrix() after rotating I was supposed to translate the model matrix back to the original location. That is necessary because the local rotation must follow these steps (in this strict order):

问题是在旋转后的函数GetWorldMatrix()中我应该将模型矩阵转换回原始位置。这是必要的,因为本地轮换必须遵循这些步骤(按照严格的顺序):

  1. translate from the original point to the anchor of rotation
  2. 从原始点转换为旋转锚点
  3. rotate
  4. 回转
  5. translate back to the original point
  6. 转回原点

So I added this line:

所以我添加了这一行:

worldMatrix = glm::translate( worldMatrix, -position );

The function now looks like this:

该功能现在看起来像这样:

mat4 Model::GetWorldMatrix() const {
    mat4 worldMatrix( 1.0f );
    worldMatrix = glm::translate( worldMatrix, position );
    worldMatrix = glm::rotate( worldMatrix, mRotationAngleInDegrees, vec3( 0, 0, 1 ) );
    worldMatrix = glm::translate( worldMatrix, -position );
    return worldMatrix;
}

Thank you, 40two, for the great hint.

谢谢,40two,这是一个很好的提示。