Skip to content
Snippets Groups Projects
Commit 3bd63464 authored by sdegrande's avatar sdegrande
Browse files

Fix computation of matrix transformations of hierarchical 3D models.

parent b99354a1
Branches
No related tags found
No related merge requests found
......@@ -47,9 +47,8 @@ const aiScene* Model3DImport::import(const std::string& modelName, const std::st
/* Remove lines and points - TODO: handle those primitives */
importer.SetPropertyInteger(AI_CONFIG_PP_SBP_REMOVE, aiPrimitiveType_LINE | aiPrimitiveType_POINT);
readFlags = (importProperties.pretransformVertices) ? aiProcess_PreTransformVertices
: (aiProcess_OptimizeGraph | aiProcess_OptimizeMeshes);
// Nota: do not use aiProcess_OptimizeGraph, it produces a somehow buggy graph !
readFlags = (importProperties.pretransformVertices) ? aiProcess_PreTransformVertices : aiProcess_OptimizeMeshes;
readFlags |= aiProcess_Triangulate | aiProcess_SortByPType | aiProcess_RemoveComponent;
if (importProperties.meshesSize > 0) readFlags |= aiProcess_SplitLargeMeshes;
......
......@@ -15,6 +15,7 @@
#include "Model3D.hpp"
#include <glm/glm.hpp>
#include <glm/ext.hpp>
#include <glm/gtx/transform.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
......@@ -45,7 +46,7 @@ static inline glm::mat4 aiToGlmMatrix(const aiMatrix4x4& mat)
// Hierarchy functions
//=====================================================================
void HierarchyContainer::loadNodeHierarchy(aiNode* importedNode, std::string tab)
void HierarchyContainer::loadNodeHierarchy(aiNode* importedNode)
{
setId(importedNode->mName.C_Str());
setMatrix(aiToGlmMatrix(importedNode->mTransformation));
......@@ -57,7 +58,8 @@ void HierarchyContainer::loadNodeHierarchy(aiNode* importedNode, std::string tab
if (importedNode->mNumChildren != 0) {
for (size_t i = 0; i < importedNode->mNumChildren; ++i) {
HierarchyContainer* child = new HierarchyContainer;
child->loadNodeHierarchy(importedNode->mChildren[i], tab.append(" "));
child->loadNodeHierarchy(importedNode->mChildren[i]);
addNode(child);
}
}
}
......@@ -72,13 +74,18 @@ glm::mat4 HierarchyContainer::getCumulativeMeshTransform(uint32_t searchedMeshId
HierarchyContainer* node) -> std::pair<bool, glm::mat4> {
for (const auto& meshIdx : node->meshIndices) {
if (meshIdx == searchedMeshIdx) {
return { true, node->getMatrix() };
return { true, glm::mat4(1.0f) };
}
}
for (const auto& child : node->nodes) {
auto [found, mat] = descend(descend, dynamic_cast<HierarchyContainer*>(child));
if (found) {
return { true, node->getMatrix() * child->getMatrix() };
if (node->getParent() == nullptr) {
// do not accumulate with the root transform, it is applied to the Content
return { true, mat };
} else {
return { true, node->getMatrix() * mat };
}
}
}
return { false, { 1.0f } };
......@@ -176,7 +183,7 @@ void Model3DNode::render(Renderer* renderer, const glm::mat4& local)
{
if (!canRender(renderer)) return;
glm::mat4 localMat = local * getMatrix();
glm::mat4 localMat = local * getMatrix() * content.getMatrix();
for (int i = 0; i < content.nbChildren(); ++i) {
static_cast<Model3DMeshNode*>(content[i])->render(renderer, localMat);
}
......@@ -233,7 +240,7 @@ bool Model3DNode::importModel(const std::string& modelName, const std::string& m
// Will be used to computed the meshes transform matrices, if nedded.
HierarchyContainer* rootNode = new HierarchyContainer();
rootNode->loadNodeHierarchy(scene->mRootNode, std::string { "" });
rootNode->loadNodeHierarchy(scene->mRootNode);
// Create and add a mesh for each object's imported mesh.
// It could be a Mesh<MVertex> or a Mesh<SMVertex> if the mesh has bones.
......@@ -363,6 +370,7 @@ bool Model3DNode::importModel(const std::string& modelName, const std::string& m
content.addNode(meshNode);
}
content.setMatrix(aiToGlmMatrix(scene->mRootNode->mTransformation));
setTransparency(modelTransparency);
modelLoaded = true;
......
......@@ -65,7 +65,7 @@ public:
return -1;
}
void loadNodeHierarchy(aiNode* importedNode, std::string tab);
void loadNodeHierarchy(aiNode* importedNode);
glm::mat4 getCumulativeMeshTransform(uint32_t meshIdx);
private:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment