Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix node invalid pointer #238

Merged
merged 2 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 19 additions & 21 deletions private/Producers/FbxProducer/FbxProducerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ void FbxProducerImpl::Execute(cd::SceneDatabase* pSceneDatabase)
uint32_t oldNodeCount = pSceneDatabase->GetNodeCount();
m_nodeIDGenerator.SetCurrentID(oldNodeCount);
pSceneDatabase->SetNodeCount(oldNodeCount + sceneNodeCount);
TraverseNodeRecursively(pSDKScene->GetRootNode(), nullptr, pSceneDatabase);
TraverseNodeRecursively(pSDKScene->GetRootNode(), cd::NodeID::InvalidID, pSceneDatabase);
}

// Convert fbx bone/joint to cd scene bone/joint.
Expand Down Expand Up @@ -247,16 +247,14 @@ int FbxProducerImpl::GetSceneNodeCount(const fbxsdk::FbxNode* pSceneNode)
return totalCount;
}

void FbxProducerImpl::TraverseNodeRecursively(fbxsdk::FbxNode* pSDKNode, cd::Node* pParentNode, cd::SceneDatabase* pSceneDatabase)
void FbxProducerImpl::TraverseNodeRecursively(fbxsdk::FbxNode* pSDKNode, cd::NodeID parentNodeID, cd::SceneDatabase* pSceneDatabase)
{
fbxsdk::FbxNodeAttribute* pNodeAttribute = pSDKNode->GetNodeAttribute();

cd::Node* pNode = nullptr;
if (nullptr == pNodeAttribute ||
fbxsdk::FbxNodeAttribute::eNull == pNodeAttribute->GetAttributeType())
{
cd::NodeID nodeID = AddNode(pSDKNode, pParentNode, pSceneDatabase);
pNode = &pSceneDatabase->GetNodes()[nodeID.Data()];
AddNode(pSDKNode, parentNodeID, pSceneDatabase);
}
else if (fbxsdk::FbxNodeAttribute::eLight == pNodeAttribute->GetAttributeType() && m_importLight)
{
Expand Down Expand Up @@ -300,44 +298,44 @@ void FbxProducerImpl::TraverseNodeRecursively(fbxsdk::FbxNode* pSDKNode, cd::Nod
{
assert(1U == materialCount && "Material is AllSame mapping mode but has multiple materials.");

AddMesh(pFbxMesh, pSDKNode->GetName(), 0, pParentNode, pSceneDatabase);
AddMesh(pFbxMesh, pSDKNode->GetName(), 0, parentNodeID, pSceneDatabase);
}
else if (fbxsdk::FbxLayerElement::eByPolygon == materialMappingMode)
{
assert(materialCount > 1U && "Material is ByPolygon mapping mode but only one material.");

// It will generate multiple meshes to assign one material.
// To manage them conveniently, they are placed under a new Node.
cd::NodeID meshesNodeID = AddNode(pSDKNode, pParentNode, pSceneDatabase);
cd::NodeID meshesNodeID = AddNode(pSDKNode, parentNodeID, pSceneDatabase);
for (uint32_t materialIndex = 0U; materialIndex < materialCount; ++materialIndex)
{
std::string splitMeshName(std::format("{}_{}", pSDKNode->GetName(), materialIndex));
AddMesh(pFbxMesh, splitMeshName.c_str(), materialIndex, &pSceneDatabase->GetNodes()[meshesNodeID.Data()], pSceneDatabase);
AddMesh(pFbxMesh, splitMeshName.c_str(), materialIndex, meshesNodeID, pSceneDatabase);
}
}
}
else
{
AddMesh(pFbxMesh, pSDKNode->GetName(), std::nullopt, pParentNode, pSceneDatabase);
AddMesh(pFbxMesh, pSDKNode->GetName(), std::nullopt, parentNodeID, pSceneDatabase);
}
}
}

for (int childIndex = 0; childIndex < pSDKNode->GetChildCount(); ++childIndex)
{
TraverseNodeRecursively(pSDKNode->GetChild(childIndex), pNode, pSceneDatabase);
TraverseNodeRecursively(pSDKNode->GetChild(childIndex), parentNodeID, pSceneDatabase);
}
}

cd::NodeID FbxProducerImpl::AddNode(const fbxsdk::FbxNode* pSDKNode, cd::Node* pParentNode, cd::SceneDatabase* pSceneDatabase)
cd::NodeID FbxProducerImpl::AddNode(const fbxsdk::FbxNode* pSDKNode, cd::NodeID parentNodeID, cd::SceneDatabase* pSceneDatabase)
{
cd::NodeID nodeID = m_nodeIDGenerator.AllocateID();
cd::Node node(nodeID, pSDKNode->GetName());
node.SetTransform(details::ConvertFbxNodeTransform(const_cast<fbxsdk::FbxNode*>(pSDKNode)));
if (pParentNode)
if (parentNodeID.IsValid())
{
pParentNode->AddChildID(nodeID.Data());
node.SetParentID(pParentNode->GetID().Data());
pSceneDatabase->GetNode(parentNodeID.Data()).AddChildID(nodeID.Data());
node.SetParentID(parentNodeID.Data());
}
pSceneDatabase->AddNode(cd::MoveTemp(node));

Expand Down Expand Up @@ -409,7 +407,7 @@ cd::LightID FbxProducerImpl::AddLight(const fbxsdk::FbxLight* pFbxLight, const c
return lightID;
}

cd::MeshID FbxProducerImpl::AddMesh(const fbxsdk::FbxMesh* pFbxMesh, const char* pMeshName, std::optional<int32_t> optMaterialIndex, cd::Node* pParentNode, cd::SceneDatabase* pSceneDatabase)
cd::MeshID FbxProducerImpl::AddMesh(const fbxsdk::FbxMesh* pFbxMesh, const char* pMeshName, std::optional<int32_t> optMaterialIndex, cd::NodeID parentNodeID, cd::SceneDatabase* pSceneDatabase)
{
// For geometry data, we only query base layer which means index 0.
const fbxsdk::FbxLayer* pMeshBaseLayer = pFbxMesh->GetLayer(0);
Expand Down Expand Up @@ -500,9 +498,9 @@ cd::MeshID FbxProducerImpl::AddMesh(const fbxsdk::FbxMesh* pFbxMesh, const char*
mesh.SetVertexFormat(cd::MoveTemp(meshVertexFormat));

// Associate mesh id to its parent transform node.
if (pParentNode)
if (parentNodeID.IsValid())
{
pParentNode->AddMeshID(meshID.Data());
pSceneDatabase->GetNode(parentNodeID.Data()).AddMeshID(meshID.Data());
}

if (optMaterialIndex.has_value())
Expand Down Expand Up @@ -843,15 +841,15 @@ int FbxProducerImpl::GetSceneBoneCount(const fbxsdk::FbxNode* pSceneNode)
// }
//}

cd::BoneID FbxProducerImpl::AddBone(const fbxsdk::FbxNode* pSDKNode, cd::Bone* pParentBone, cd::SceneDatabase* pSceneDatabase)
cd::BoneID FbxProducerImpl::AddBone(const fbxsdk::FbxNode* pSDKNode, cd::BoneID parentBoneID, cd::SceneDatabase* pSceneDatabase)
{
cd::BoneID boneID = m_boneIDGenerator.AllocateID();
cd::Bone bone(boneID, pSDKNode->GetName());
bone.SetTransform(details::ConvertFbxNodeTransform(const_cast<fbxsdk::FbxNode*>(pSDKNode)));
if (pParentBone)
if (parentBoneID.IsValid())
{
pParentBone->AddChildID(boneID.Data());
bone.SetParentID(pParentBone->GetID().Data());
pSceneDatabase->GetBone(parentBoneID.Data()).AddChildID(boneID.Data());
bone.SetParentID(parentBoneID.Data());
}
pSceneDatabase->AddBone(cd::MoveTemp(bone));

Expand Down
8 changes: 4 additions & 4 deletions private/Producers/FbxProducer/FbxProducerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class FbxProducerImpl final

private:
int GetSceneNodeCount(const fbxsdk::FbxNode* pSceneNode);
void TraverseNodeRecursively(fbxsdk::FbxNode* pSDKNode, cd::Node* pParentNode, cd::SceneDatabase* pSceneDatabase);
void TraverseNodeRecursively(fbxsdk::FbxNode* pSDKNode, cd::NodeID parentNodeID, cd::SceneDatabase* pSceneDatabase);

int GetSceneBoneCount(const fbxsdk::FbxNode* pSceneNode);
//void TraverseBoneRecursively(fbxsdk::FbxNode* pSDKNode, fbxsdk::FbxScene* pSDKScene, cd::Bone* pParentNode, cd::SceneDatabase* pSceneDatabase);
Expand All @@ -81,12 +81,12 @@ class FbxProducerImpl final
void AddMaterialTexture(const fbxsdk::FbxProperty& sdkProperty, cd::MaterialTextureType textureType, cd::Material& material, cd::SceneDatabase* pSceneDatabase);
cd::MaterialID AddMaterial(const fbxsdk::FbxSurfaceMaterial* pSDKMaterial, cd::SceneDatabase* pSceneDatabase);

cd::NodeID AddNode(const fbxsdk::FbxNode* pSDKNode, cd::Node* pParentNode, cd::SceneDatabase* pSceneDatabase);
cd::NodeID AddNode(const fbxsdk::FbxNode* pSDKNode, cd::NodeID parentNodeID, cd::SceneDatabase* pSceneDatabase);
cd::LightID AddLight(const fbxsdk::FbxLight* pFbxLight, const char* pLightName, cd::Transform transform, cd::SceneDatabase* pSceneDatabase);
cd::MeshID AddMesh(const fbxsdk::FbxMesh* pFbxMesh, const char* pMeshName, std::optional<int32_t> optMaterialIndex, cd::Node* pParentNode, cd::SceneDatabase* pSceneDatabase);
cd::MeshID AddMesh(const fbxsdk::FbxMesh* pFbxMesh, const char* pMeshName, std::optional<int32_t> optMaterialIndex, cd::NodeID parentNodeID, cd::SceneDatabase* pSceneDatabase);
std::vector<cd::MorphID> AddMorphs(const fbxsdk::FbxBlendShape* pBlendShape, const cd::Mesh& sourceMesh, const std::map<uint32_t, uint32_t>& mapVertexIDToControlPointIndex, cd::SceneDatabase* pSceneDatabase);

cd::BoneID AddBone(const fbxsdk::FbxNode* pSDKNode, cd::Bone* pParentNode, cd::SceneDatabase* pSceneDatabase);
cd::BoneID AddBone(const fbxsdk::FbxNode* pSDKNode, cd::BoneID parentBoneID, cd::SceneDatabase* pSceneDatabase);
//cd::TrackID AddTrack(const fbxsdk::FbxNode* pSDKNode, cd::Node* pParentNode, cd::SceneDatabase* pSceneDatabase);
cd::AnimationID AddAnimation(fbxsdk::FbxNode* pSDKNode, fbxsdk::FbxScene* pSDKScene, cd::SceneDatabase* pSceneDatabase);

Expand Down
45 changes: 45 additions & 0 deletions private/Scene/SceneDatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ void SceneDatabase::SetNodeCount(uint32_t nodeCount)
return m_pSceneDatabaseImpl->SetNodeCount(nodeCount);
}

Node& SceneDatabase::GetNode(uint32_t index)
{
return m_pSceneDatabaseImpl->GetNode(index);
}

const Node& SceneDatabase::GetNode(uint32_t index) const
{
return m_pSceneDatabaseImpl->GetNode(index);
Expand Down Expand Up @@ -147,6 +152,11 @@ void SceneDatabase::SetMeshCount(uint32_t meshCount)
return m_pSceneDatabaseImpl->SetMeshCount(meshCount);
}

Mesh& SceneDatabase::GetMesh(uint32_t index)
{
return m_pSceneDatabaseImpl->GetMesh(index);
}

const Mesh& SceneDatabase::GetMesh(uint32_t index) const
{
return m_pSceneDatabaseImpl->GetMesh(index);
Expand Down Expand Up @@ -180,6 +190,11 @@ void SceneDatabase::SetMorphCount(uint32_t morphCount)
return m_pSceneDatabaseImpl->SetMorphCount(morphCount);
}

Morph& SceneDatabase::GetMorph(uint32_t index)
{
return m_pSceneDatabaseImpl->GetMorph(index);
}

const Morph& SceneDatabase::GetMorph(uint32_t index) const
{
return m_pSceneDatabaseImpl->GetMorph(index);
Expand Down Expand Up @@ -213,6 +228,11 @@ void SceneDatabase::SetMaterialCount(uint32_t materialCount)
return m_pSceneDatabaseImpl->SetMaterialCount(materialCount);
}

Material& SceneDatabase::GetMaterial(uint32_t index)
{
return m_pSceneDatabaseImpl->GetMaterial(index);
}

const Material& SceneDatabase::GetMaterial(uint32_t index) const
{
return m_pSceneDatabaseImpl->GetMaterial(index);
Expand Down Expand Up @@ -284,6 +304,11 @@ void SceneDatabase::SetCameraCount(uint32_t cameraCount)
return m_pSceneDatabaseImpl->SetCameraCount(cameraCount);
}

Camera& SceneDatabase::GetCamera(uint32_t index)
{
return m_pSceneDatabaseImpl->GetCamera(index);
}

const Camera& SceneDatabase::GetCamera(uint32_t index) const
{
return m_pSceneDatabaseImpl->GetCamera(index);
Expand Down Expand Up @@ -317,6 +342,11 @@ void SceneDatabase::SetLightCount(uint32_t lightCount)
return m_pSceneDatabaseImpl->SetLightCount(lightCount);
}

Light& SceneDatabase::GetLight(uint32_t index)
{
return m_pSceneDatabaseImpl->GetLight(index);
}

const Light& SceneDatabase::GetLight(uint32_t index) const
{
return m_pSceneDatabaseImpl->GetLight(index);
Expand Down Expand Up @@ -350,6 +380,11 @@ void SceneDatabase::SetBoneCount(uint32_t boneCount)
return m_pSceneDatabaseImpl->SetBoneCount(boneCount);
}

Bone& SceneDatabase::GetBone(uint32_t index)
{
return m_pSceneDatabaseImpl->GetBone(index);
}

const Bone& SceneDatabase::GetBone(uint32_t index) const
{
return m_pSceneDatabaseImpl->GetBone(index);
Expand Down Expand Up @@ -388,6 +423,11 @@ void SceneDatabase::SetAnimationCount(uint32_t animationCount)
return m_pSceneDatabaseImpl->SetAnimationCount(animationCount);
}

Animation& SceneDatabase::GetAnimation(uint32_t index)
{
return m_pSceneDatabaseImpl->GetAnimation(index);
}

const Animation& SceneDatabase::GetAnimation(uint32_t index) const
{
return m_pSceneDatabaseImpl->GetAnimation(index);
Expand Down Expand Up @@ -421,6 +461,11 @@ void SceneDatabase::SetTrackCount(uint32_t animationCount)
return m_pSceneDatabaseImpl->SetTrackCount(animationCount);
}

Track& SceneDatabase::GetTrack(uint32_t index)
{
return m_pSceneDatabaseImpl->GetTrack(index);
}

const Track& SceneDatabase::GetTrack(uint32_t index) const
{
return m_pSceneDatabaseImpl->GetTrack(index);
Expand Down
9 changes: 9 additions & 0 deletions private/Scene/SceneDatabaseImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class SceneDatabaseImpl
std::vector<Node>& GetNodes() { return m_nodes; }
const std::vector<Node>& GetNodes() const { return m_nodes; }
void SetNodeCount(uint32_t count) { m_nodes.reserve(count); }
Node& GetNode(uint32_t index) { return m_nodes[index]; }
const Node& GetNode(uint32_t index) const { return m_nodes[index]; }
const Node* GetNodeByName(const char* pName) const;
uint32_t GetNodeCount() const { return static_cast<uint32_t>(m_nodes.size()); }
Expand All @@ -66,6 +67,7 @@ class SceneDatabaseImpl
std::vector<Mesh>& GetMeshes() { return m_meshes; }
const std::vector<Mesh>& GetMeshes() const { return m_meshes; }
void SetMeshCount(uint32_t count) { m_meshes.reserve(count); }
Mesh& GetMesh(uint32_t index) { return m_meshes[index]; }
const Mesh& GetMesh(uint32_t index) const { return m_meshes[index]; }
uint32_t GetMeshCount() const { return static_cast<uint32_t>(m_meshes.size()); }

Expand All @@ -74,6 +76,7 @@ class SceneDatabaseImpl
std::vector<Morph>& GetMorphs() { return m_morphs; }
const std::vector<Morph>& GetMorphs() const { return m_morphs; }
void SetMorphCount(uint32_t count) { m_morphs.reserve(count); }
Morph& GetMorph(uint32_t index) { return m_morphs[index]; }
const Morph& GetMorph(uint32_t index) const { return m_morphs[index]; }
uint32_t GetMorphCount() const { return static_cast<uint32_t>(m_morphs.size()); }

Expand All @@ -91,6 +94,7 @@ class SceneDatabaseImpl
std::vector<Material>& GetMaterials() { return m_materials; }
const std::vector<Material>& GetMaterials() const { return m_materials; }
void SetMaterialCount(uint32_t count) { m_materials.reserve(count); }
Material& GetMaterial(uint32_t index) { return m_materials[index]; }
const Material& GetMaterial(uint32_t index) const { return m_materials[index]; }
uint32_t GetMaterialCount() const { return static_cast<uint32_t>(m_materials.size()); }

Expand All @@ -99,6 +103,7 @@ class SceneDatabaseImpl
std::vector<Camera>& GetCameras() { return m_cameras; }
const std::vector<Camera>& GetCameras() const { return m_cameras; }
void SetCameraCount(uint32_t count) { m_cameras.reserve(count); }
Camera& GetCamera(uint32_t index) { return m_cameras[index]; }
const Camera& GetCamera(uint32_t index) const { return m_cameras[index]; }
uint32_t GetCameraCount() const { return static_cast<uint32_t>(m_cameras.size()); }

Expand All @@ -107,6 +112,7 @@ class SceneDatabaseImpl
std::vector<Light>& GetLights() { return m_lights; }
const std::vector<Light>& GetLights() const { return m_lights; }
void SetLightCount(uint32_t count) { m_lights.reserve(count); }
Light& GetLight(uint32_t index) { return m_lights[index]; }
const Light& GetLight(uint32_t index) const { return m_lights[index]; }
uint32_t GetLightCount() const { return static_cast<uint32_t>(m_lights.size()); }

Expand All @@ -115,6 +121,7 @@ class SceneDatabaseImpl
std::vector<Bone>& GetBones() { return m_bones; }
const std::vector<Bone>& GetBones() const { return m_bones; }
void SetBoneCount(uint32_t count) { m_bones.reserve(count); }
Bone& GetBone(uint32_t index) { return m_bones[index]; }
const Bone& GetBone(uint32_t index) const { return m_bones[index]; }
const Bone* GetBoneByName(const char* pName) const;
uint32_t GetBoneCount() const { return static_cast<uint32_t>(m_bones.size()); }
Expand All @@ -124,6 +131,7 @@ class SceneDatabaseImpl
std::vector<Animation>& GetAnimations() { return m_animations; }
const std::vector<Animation>& GetAnimations() const { return m_animations; }
void SetAnimationCount(uint32_t count) { m_animations.reserve(count); }
Animation& GetAnimation(uint32_t index) { return m_animations[index]; }
const Animation& GetAnimation(uint32_t index) const { return m_animations[index]; }
uint32_t GetAnimationCount() const { return static_cast<uint32_t>(m_animations.size()); }

Expand All @@ -132,6 +140,7 @@ class SceneDatabaseImpl
std::vector<Track>& GetTracks() { return m_tracks; }
const std::vector<Track>& GetTracks() const { return m_tracks; }
void SetTrackCount(uint32_t count) { m_tracks.reserve(count); }
Track& GetTrack(uint32_t index) { return m_tracks[index]; }
const Track& GetTrack(uint32_t index) const { return m_tracks[index]; }
const Track* GetTrackByName(const char* pName) const;
uint32_t GetTrackCount() const { return static_cast<uint32_t>(m_tracks.size()); }
Expand Down
9 changes: 9 additions & 0 deletions public/Scene/SceneDatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class CORE_API SceneDatabase final
std::vector<Node>& GetNodes();
const std::vector<Node>& GetNodes() const;
void SetNodeCount(uint32_t nodeCount);
Node& GetNode(uint32_t index);
const Node& GetNode(uint32_t index) const;
const Node* GetNodeByName(const char* pName) const;
uint32_t GetNodeCount() const;
Expand All @@ -69,6 +70,7 @@ class CORE_API SceneDatabase final
std::vector<Mesh>& GetMeshes();
const std::vector<Mesh>& GetMeshes() const;
void SetMeshCount(uint32_t meshCount);
Mesh& GetMesh(uint32_t index);
const Mesh& GetMesh(uint32_t index) const;
uint32_t GetMeshCount() const;

Expand All @@ -77,6 +79,7 @@ class CORE_API SceneDatabase final
std::vector<Morph>& GetMorphs();
const std::vector<Morph>& GetMorphs() const;
void SetMorphCount(uint32_t morphCount);
Morph& GetMorph(uint32_t index);
const Morph& GetMorph(uint32_t index) const;
uint32_t GetMorphCount() const;

Expand All @@ -85,6 +88,7 @@ class CORE_API SceneDatabase final
std::vector<Material>& GetMaterials();
const std::vector<Material>& GetMaterials() const;
void SetMaterialCount(uint32_t materialCount);
Material& GetMaterial(uint32_t index);
const Material& GetMaterial(uint32_t index) const;
uint32_t GetMaterialCount() const;

Expand All @@ -102,6 +106,7 @@ class CORE_API SceneDatabase final
std::vector<Camera>& GetCameras();
const std::vector<Camera>& GetCameras() const;
void SetCameraCount(uint32_t cameraCount);
Camera& GetCamera(uint32_t index);
const Camera& GetCamera(uint32_t index) const;
uint32_t GetCameraCount() const;

Expand All @@ -110,6 +115,7 @@ class CORE_API SceneDatabase final
std::vector<Light>& GetLights();
const std::vector<Light>& GetLights() const;
void SetLightCount(uint32_t lightCount);
Light& GetLight(uint32_t index);
const Light& GetLight(uint32_t index) const;
uint32_t GetLightCount() const;

Expand All @@ -118,6 +124,7 @@ class CORE_API SceneDatabase final
std::vector<Bone>& GetBones();
const std::vector<Bone>& GetBones() const;
void SetBoneCount(uint32_t boneCount);
Bone& GetBone(uint32_t index);
const Bone& GetBone(uint32_t index) const;
const Bone* GetBoneByName(const char* pName) const;
uint32_t GetBoneCount() const;
Expand All @@ -127,6 +134,7 @@ class CORE_API SceneDatabase final
std::vector<Animation>& GetAnimations();
const std::vector<Animation>& GetAnimations() const;
void SetAnimationCount(uint32_t animationCount);
Animation& GetAnimation(uint32_t index);
const Animation& GetAnimation(uint32_t index) const;
uint32_t GetAnimationCount() const;

Expand All @@ -135,6 +143,7 @@ class CORE_API SceneDatabase final
std::vector<Track>& GetTracks();
const std::vector<Track>& GetTracks() const;
void SetTrackCount(uint32_t TrackCount);
Track& GetTrack(uint32_t index);
const Track& GetTrack(uint32_t index) const;
const Track* GetTrackByName(const char* pName) const;
uint32_t GetTrackCount() const;
Expand Down