Skip to content

Commit

Permalink
fix mesh log and render data build
Browse files Browse the repository at this point in the history
  • Loading branch information
T-rvw committed Feb 5, 2024
1 parent f615750 commit 1a2e95b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 28 deletions.
11 changes: 7 additions & 4 deletions private/Scene/SceneDatabaseImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,14 @@ void SceneDatabaseImpl::Dump() const
auto& polygonGroup = polygonGroups[polygonGroupIndex];
printf("\t[PolygonGroup %u] PolygonCount = %u\n", polygonGroupIndex, static_cast<uint32_t>(polygonGroup.size()));

auto materialID = mesh.GetMaterialID(polygonGroupIndex);
printf("\t\t[Associated Material %u] Name = %s\n", materialID.Data(), materialID.IsValid() ? GetMaterial(materialID.Data()).GetName() : "");
if (materialID.IsValid())
if (mesh.GetMaterialIDCount() > 0U)
{
materialDrawMeshPolygonGroupIDs[materialID][mesh.GetID()].push_back(polygonGroupIndex);
auto materialID = mesh.GetMaterialID(polygonGroupIndex);
printf("\t\t[Associated Material %u] Name = %s\n", materialID.Data(), materialID.IsValid() ? GetMaterial(materialID.Data()).GetName() : "");
if (materialID.IsValid())
{
materialDrawMeshPolygonGroupIDs[materialID][mesh.GetID()].push_back(polygonGroupIndex);
}
}
}

Expand Down
33 changes: 9 additions & 24 deletions public/Utilities/MeshUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace cd
using VertexBuffer = std::vector<std::byte>;
using IndexBuffer = std::vector<std::byte>;

std::optional<VertexBuffer> BuildVertexBufferForStaticMesh(const cd::Mesh& mesh, const cd::VertexFormat& requiredVertexFormat)
static std::optional<VertexBuffer> BuildVertexBufferForStaticMesh(const cd::Mesh& mesh, const cd::VertexFormat& requiredVertexFormat)
{
const bool containsPosition = requiredVertexFormat.Contains(cd::VertexAttributeType::Position);
const bool containsNormal = requiredVertexFormat.Contains(cd::VertexAttributeType::Normal);
Expand Down Expand Up @@ -80,31 +80,17 @@ std::optional<VertexBuffer> BuildVertexBufferForStaticMesh(const cd::Mesh& mesh,
return vertexBuffer;
}

std::optional<VertexBuffer> BuildVertexBufferForSkeletalMesh(const cd::Mesh& mesh, const cd::VertexFormat& requiredVertexFormat, const cd::SceneDatabase* pSceneDatabase)
static std::optional<VertexBuffer> BuildVertexBufferForSkeletalMesh(const cd::Mesh& mesh, const cd::VertexFormat& requiredVertexFormat, const cd::Skin& skin, const std::vector<const cd::Bone*>& skeletonBones)
{
assert(skin.GetVertexBoneNameArrayCount() == skin.GetVertexBoneWeightArrayCount());

const bool containsBoneIndex = requiredVertexFormat.Contains(cd::VertexAttributeType::BoneIndex);
const bool containsBoneWeight = requiredVertexFormat.Contains(cd::VertexAttributeType::BoneWeight);
if (!containsBoneIndex || !containsBoneWeight)
{
return std::nullopt;
}

// One Skin is associated with one Skeleton and one Mesh.
// When multiple Skins happened, it means that one Mesh may have multiple Skeletons.
// Check if has multiple root bones? Currently, we don't support this case.
assert(mesh.GetSkinIDCount() == 1U);
if (!mesh.GetSkinID(0U).IsValid())
{
return std::nullopt;
}

const cd::Skin& skin = pSceneDatabase->GetSkin(mesh.GetSkinID(0U).Data());
if (!skin.GetSkeletonID().IsValid())
{
return std::nullopt;
}
assert(skin.GetVertexBoneNameArrayCount() == skin.GetVertexBoneWeightArrayCount());

// TODO : refine hardcoded 4 bones.
uint32_t vertexMaxInfluenceCount = 4U;
assert(skin.GetMaxVertexInfluenceCount() <= vertexMaxInfluenceCount);
Expand All @@ -118,12 +104,11 @@ std::optional<VertexBuffer> BuildVertexBufferForSkeletalMesh(const cd::Mesh& mes
float defaultVertexBoneWeight = 0.0f;

// Building a mapping table from skeleton bone name to bone index in the skeleton bone tree.
const cd::Skeleton& skeleton = pSceneDatabase->GetSkeleton(skin.GetSkeletonID().Data());
std::map<std::string, uint16_t> skeletonBoneNameToIndex;
for (uint32_t boneIndex = 0U, boneCount = skeleton.GetBoneIDCount(); boneIndex < boneCount; ++boneIndex)
for (size_t boneIndex = 0U; boneIndex < skeletonBones.size(); ++boneIndex)
{
const auto& bone = pSceneDatabase->GetBone(boneIndex);
skeletonBoneNameToIndex[bone.GetName()] = static_cast<uint16_t>(boneIndex);
const cd::Bone* pBone = skeletonBones[boneIndex];
skeletonBoneNameToIndex[pBone->GetName()] = static_cast<uint16_t>(boneIndex);
}

// Build vertex buffer.
Expand Down Expand Up @@ -221,7 +206,7 @@ std::optional<VertexBuffer> BuildVertexBufferForSkeletalMesh(const cd::Mesh& mes
return vertexBuffer;
}

std::optional<IndexBuffer> BuildIndexBufferesForPolygonGroup(const cd::Mesh& mesh, uint32_t polygonGroupIndex, bool forceIndex32 = false)
static std::optional<IndexBuffer> BuildIndexBufferesForPolygonGroup(const cd::Mesh& mesh, uint32_t polygonGroupIndex, bool forceIndex32 = false)
{
if (polygonGroupIndex >= mesh.GetPolygonGroupCount())
{
Expand Down Expand Up @@ -281,7 +266,7 @@ std::optional<IndexBuffer> BuildIndexBufferesForPolygonGroup(const cd::Mesh& mes
return indexBuffer;
}

std::vector<std::optional<IndexBuffer>> BuildIndexBufferesForMesh(const cd::Mesh& mesh, bool forceIndex32 = false)
static std::vector<std::optional<IndexBuffer>> BuildIndexBufferesForMesh(const cd::Mesh& mesh, bool forceIndex32 = false)
{
std::vector<std::optional<IndexBuffer>> indexBufferes;

Expand Down

0 comments on commit 1a2e95b

Please sign in to comment.