对于多个网格的FBX模型,网格不会显示在正确的位置

in4*_*001 -9 c opengl transformation mesh fbx

我导入了一个由多个网格组成的FBX模型.不幸的是,我无法将每个网格显示在正确的位置.对于每个网格,我将网格的几何变换与网格的局部变换相乘,然后将其传递给着色器.我该如何解决这个问题?

OpenGL着色器

gl_Position = modelViewProjectionMatrix *TransformationMatrix*vertexPositionsOfMesh;
Run Code Online (Sandbox Code Playgroud)

创建转换矩阵

GLKMatrix4 LcLTransformation = createTransformationMatrix(
   Mesh->LclRotation,
   Mesh->LclScaling,
   Mesh->LclTranslation);
GLKMatrix4 GeoTransformation = createTransformationMatrix(
   Mesh->GeometricRotation,
   Mesh->GeometricScaling,
   Mesh->GeometricTranslation);
TransformationMatrix=GLKMatrix4Transpose(GLKMatrix4Multiply(LcLTransformation,
                                                            GeoTransformation));
Run Code Online (Sandbox Code Playgroud)

createTransformationMatrix

GLKMatrix4 createTransformationMatrix(float* _rotation, float* _scaling, float* _translation)
{
  GLKMatrix4 Rx = GLKMatrix4Make(1, 0,                 0,                  0,
                                 0, cos(_rotation[0]), -sin(_rotation[0]), 0,
                                 0, sin(_rotation[0]), cos(_rotation[0]),  0,
                                 0, 0,                 0,                  1
                                 );

  GLKMatrix4 Ry = GLKMatrix4Make(cos(_rotation[1]),  0, sin(_rotation[1]), 0,
                                 0,                  1, 0,                 0,
                                 -sin(_rotation[1]), 0, cos(_rotation[1]), 0,
                                 0,                  0, 0,                 1
                                 );

  GLKMatrix4 Rz = GLKMatrix4Make(cos(_rotation[2]), -sin(_rotation[2]), 0, 0,
                                 sin(_rotation[2]), cos(_rotation[2]),  0, 0,
                                 0,                 0,                  1, 0,
                                 0,                 0,                  0, 1
                                 );

  GLKMatrix4 Translation = GLKMatrix4Make(1, 0, 0, _translation[0],
                                          0, 1, 0, _translation[1],
                                          0, 0, 1, _translation[2],
                                          0, 0, 0, 1
                                          );
  GLKMatrix4 Scaling = GLKMatrix4Identity;

  Scaling.m00 = _scaling[0];
  Scaling.m11 = _scaling[1];
  Scaling.m22 = _scaling[2];

  GLKMatrix4 Rotation = GLKMatrix4Multiply(GLKMatrix4Multiply(Rx, Ry), Rz);
  Transformation = GLKMatrix4Multiply(Scaling, GLKMatrix4Multiply(Rotation, Translation));
  return Transformation;
}
Run Code Online (Sandbox Code Playgroud)

小智 144

我在引擎中正确地从MAX导入了fbx.

你必须:

WorldMatrix= [ParentWorldMatrix * ModelMatrix] * GeometricMatrix
Run Code Online (Sandbox Code Playgroud)

在获得层次结构的世界之后,您必须仅增加几何矩阵.该"ParentMatrix"不包含GEOMs.

所以模型应该是:

World = GrandGrandParentModel*[...]*GrandParentModel*ParentModel*Model*CurrentModelGeometric.

请记住,旋转是ZYX.

码:

void GRPNODE::UpdateWorldMatrix(bool * mustUpdate)
{
    if (!parent)
        return;

    parent->UpdateWorldMatrix(mustUpdate);

    if (worldmatrix_is_pending)
        *mustUpdate = true;

    if (*mustUpdate)
        this->worldmatrix.GetMulplicationMatrix(parent->GetWorldMatrixPointer(), &modelmatrix);
}
Run Code Online (Sandbox Code Playgroud)

然后我得到节点的世界矩阵,当我转换顶点时,我做:

void GRPELEMENT::ComputeMatrices(GRPMATRIX* viewmatrix, GRPMATRIX* viewprojection, GRPMATRIX* projection)
{
    modelmatrix=node->GetWorldMatrix();
    if (node->UsesGeometric)
        modelmatrix.GetMulplicationMatrix(modelmatrix, (*node->GetGeometricMatrix()));

    modelviewmatrix.GetMulplicationMatrix((*viewmatrix), modelmatrix);
    modelviewprojectionmatrix.GetMulplicationMatrix(projection, &modelviewmatrix);
}

void GRPNODE::MaxUpdate()
{
    // 1.0 Create Scale matrix
    scalematrix.BuildScaleMatrix(scale.vector[0], scale.vector[1], scale.vector[2]);

    // 1.1 Create current Rotation Translation Matrix
    Rx.BuildRotationMatrixX    (this->rotation.vector[0]);
    Ry.BuildRotationMatrixY    (this->rotation.vector[1]);
    Rz.BuildRotationMatrixZ    (this->rotation.vector[2]);

    if (UsesPreRotation)
    {
        Rpre.GetMulplicationMatrix(&prerotationmatrix, &Rz);
        Rt.GetMulplicationMatrix(&Rpre, &Ry);
        rotationmatrix.GetMulplicationMatrix(&Rt, &Rx);
    }
    else
    {
        Rt.GetMulplicationMatrix(&Rz, &Ry);
        rotationmatrix.GetMulplicationMatrix(&Rt, &Rx);
    }

    if (UsesPostRotation)
    {
        Rpost.GetMulplicationMatrix(&rotationmatrix, &postrotationmatrix);
        rotationmatrix = Rpost;
    }

    translationmatrix.BuildTranslationMatrix(position);

    //1.2. Create current model matrix (from stored matrix with rotation/translation)
    m.GetMulplicationMatrix(translationmatrix, rotationmatrix);
    modelmatrix.GetMulplicationMatrix(m, scalematrix);
}
Run Code Online (Sandbox Code Playgroud)

  • 我对代码不够熟悉; 但是,`GetMulplicationMatrix`似乎是一个错误(可能意味着`GetMultiplicationMatrix`?) (4认同)