Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
T-rvw committed Jan 5, 2024
1 parent 772184c commit 458f5c5
Show file tree
Hide file tree
Showing 11 changed files with 164 additions and 77 deletions.
10 changes: 7 additions & 3 deletions private/Math/MeshGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,11 @@ std::optional<Mesh> MeshGenerator::Generate(const Box& box, const VertexFormat&
}
}

cd::Mesh mesh(static_cast<uint32_t>(positions.size()));
cd::Mesh mesh;
uint32_t vertexCount = static_cast<uint32_t>(positions.size());
mesh.Init(vertexCount);

for (uint32_t i = 0U; i < positions.size(); ++i)
for (uint32_t i = 0U; i < vertexCount; ++i)
{
mesh.SetVertexPosition(i, positions[i]);
}
Expand Down Expand Up @@ -173,7 +175,9 @@ std::optional<Mesh> MeshGenerator::Generate(const Sphere& sphere, uint32_t numSt

uint32_t vertexCount = (numStacks + 1) * (numSlices + 1);

cd::Mesh mesh(vertexCount);
cd::Mesh mesh;
mesh.Init(vertexCount);

cd::VertexFormat meshVertexFormat;
meshVertexFormat.AddAttributeLayout(cd::VertexAttributeType::Position, cd::GetAttributeValueType<cd::Point::ValueType>(), cd::Point::Size);

Expand Down
9 changes: 4 additions & 5 deletions private/Producers/FbxProducer/FbxProducerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,11 +313,11 @@ void FbxProducerImpl::TraverseNodeRecursively(fbxsdk::FbxNode* pSDKNode, cd::Nod
if (IsOptionEnabled(FbxProducerOptions::ImportBlendShape))
{
int32_t blendShapeCount = pFbxMesh->GetDeformerCount(fbxsdk::FbxDeformer::eBlendShape);
assert(blendShapeCount <= 1 && "Why use two blendshape in a mesh?");
mesh.SetBlendShapeIDCount(blendShapeCount);
for (int32_t blendShapeIndex = 0; blendShapeIndex < blendShapeCount; ++blendShapeIndex)
{
const fbxsdk::FbxBlendShape* pBlendShape = static_cast<fbxsdk::FbxBlendShape*>(pFbxMesh->GetDeformer(blendShapeIndex, fbxsdk::FbxDeformer::eBlendShape));
mesh.SetBlendShapeID(ParseBlendShape(pBlendShape, mesh, pSceneDatabase));
mesh.SetBlendShapeID(blendShapeIndex, ParseBlendShape(pBlendShape, mesh, pSceneDatabase));
}
}

Expand Down Expand Up @@ -435,16 +435,15 @@ void FbxProducerImpl::ParseMesh(cd::Mesh& mesh, fbxsdk::FbxNode* pSDKNode, fbxsd

// Init vertex position.
uint32_t controlPointCount = pFbxMesh->GetControlPointsCount();
mesh.SetVertexPositionCount(controlPointCount);
mesh.SetVertexInstanceIDCount(controlPointCount); // Map control point index to vertex instance id.
uint32_t vertexInstanceCount = pFbxMesh->GetPolygonVertexCount();
mesh.Init(controlPointCount, vertexInstanceCount);
for (uint32_t controlPointIndex = 0U; controlPointIndex < controlPointCount; ++controlPointIndex)
{
fbxsdk::FbxVector4 position = pFbxMesh->GetControlPointAt(controlPointIndex);
mesh.SetVertexPosition(controlPointIndex, cd::Point(position[0], position[1], position[2]));
}

// Init vertex format.
uint32_t vertexInstanceCount = pFbxMesh->GetPolygonVertexCount();
cd::VertexFormat meshVertexFormat;
if (controlPointCount > 0U)
{
Expand Down
7 changes: 4 additions & 3 deletions private/Producers/GenericProducer/GenericProducerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,14 @@ cd::MeshID GenericProducerImpl::AddMesh(cd::SceneDatabase* pSceneDatabase, const
assert(pSourceMesh->mVertices && numVertices > 0 && "No vertex data.");

std::stringstream meshHashString;
// TODO : this hash is good to reuse mesh instance, but break the rule id same to index. Wait to have a explicit mesh instance support.
// meshHashString << pSourceMesh->mName.C_Str() << "_" << pSourceMesh->mVertices << "_" << pSourceMesh->mFaces;
meshHashString << pSourceMesh->mName.C_Str() << "_" << pSceneDatabase->GetMeshCount();

cd::MeshID::ValueType meshHash = cd::StringHash<cd::MeshID::ValueType>(meshHashString.str());
cd::MeshID meshID = m_meshIDGenerator.AllocateID(meshHash);
cd::Mesh mesh(meshID, pSourceMesh->mName.C_Str(), numVertices);
cd::Mesh mesh;
mesh.SetID(meshID);
mesh.SetName(pSourceMesh->mName.C_Str());
mesh.Init(numVertices);

// By default, aabb will be empty.
if (IsOptionEnabled(GenericProducerOptions::GenerateBoundingBox))
Expand Down
3 changes: 2 additions & 1 deletion private/ProgressiveMesh/ProgressiveMeshImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,8 @@ cd::Mesh ProgressiveMeshImpl::GenerateLodMesh(uint32_t targetFaceCount, const cd
std::vector<uint32_t> map = collapseInfoPair.second;

uint32_t targetVertexCount = targetFaceCount * 3U;
cd::Mesh mesh(targetVertexCount);
cd::Mesh mesh;
mesh.Init(targetVertexCount);

if (pSourceMesh)
{
Expand Down
43 changes: 18 additions & 25 deletions private/Scene/Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@ namespace cd

PIMPL_SCENE_CLASS(Mesh);

PIMPL_SIMPLE_TYPE_APIS(Mesh, ID);
PIMPL_SIMPLE_TYPE_APIS(Mesh, VertexAttributeCount);
PIMPL_STRING_TYPE_APIS(Mesh, Name);
PIMPL_COMPLEX_TYPE_APIS(Mesh, AABB);
PIMPL_COMPLEX_TYPE_APIS(Mesh, VertexFormat);
PIMPL_VECTOR_TYPE_APIS(Mesh, MaterialID);
PIMPL_VECTOR_TYPE_APIS(Mesh, BlendShapeID);
PIMPL_VECTOR_TYPE_APIS(Mesh, SkinID);
PIMPL_VECTOR_TYPE_APIS(Mesh, VertexInstanceID);
PIMPL_VECTOR_TYPE_APIS(Mesh, VertexPosition);
PIMPL_VECTOR_TYPE_APIS(Mesh, VertexNormal);
PIMPL_VECTOR_TYPE_APIS(Mesh, VertexTangent);
PIMPL_VECTOR_TYPE_APIS(Mesh, VertexBiTangent);
PIMPL_VECTOR_TYPE_APIS(Mesh, PolygonGroup);

Mesh Mesh::FromHalfEdgeMesh(const HalfEdgeMesh& halfEdgeMesh, ConvertStrategy strategy)
{
Mesh mesh;
Expand All @@ -16,38 +31,16 @@ Mesh Mesh::FromHalfEdgeMesh(const HalfEdgeMesh& halfEdgeMesh, ConvertStrategy st
return mesh;
}

Mesh::Mesh(uint32_t vertexCount)
void Mesh::Init(uint32_t vertexCount)
{
m_pMeshImpl = new MeshImpl();
m_pMeshImpl->Init(vertexCount);
}

Mesh::Mesh(MeshID id, const char* pName, uint32_t vertexCount) :
Mesh(vertexCount)
{
m_pMeshImpl->SetID(id);
m_pMeshImpl->SetName(pName);
}

void Mesh::Init(uint32_t vertexCount)
void Mesh::Init(uint32_t vertexPositionCount, uint32_t vertexAttributeCount)
{
m_pMeshImpl->Init(vertexCount);
m_pMeshImpl->Init(vertexPositionCount, vertexAttributeCount);
}

PIMPL_SIMPLE_TYPE_APIS(Mesh, ID);
PIMPL_SIMPLE_TYPE_APIS(Mesh, BlendShapeID);
PIMPL_STRING_TYPE_APIS(Mesh, Name);
PIMPL_COMPLEX_TYPE_APIS(Mesh, AABB);
PIMPL_COMPLEX_TYPE_APIS(Mesh, VertexFormat);
PIMPL_VECTOR_TYPE_APIS(Mesh, SkinID);
PIMPL_VECTOR_TYPE_APIS(Mesh, VertexInstanceID);
PIMPL_VECTOR_TYPE_APIS(Mesh, VertexPosition);
PIMPL_VECTOR_TYPE_APIS(Mesh, VertexNormal);
PIMPL_VECTOR_TYPE_APIS(Mesh, VertexTangent);
PIMPL_VECTOR_TYPE_APIS(Mesh, VertexBiTangent);
PIMPL_VECTOR_TYPE_APIS(Mesh, PolygonGroup);
PIMPL_VECTOR_TYPE_APIS(Mesh, MaterialID);

uint32_t Mesh::GetVertexCount() const
{
return m_pMeshImpl->GetVertexCount();
Expand Down
74 changes: 62 additions & 12 deletions private/Scene/MeshImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,22 +149,68 @@ void MeshImpl::FromHalfEdgeMesh(const HalfEdgeMesh& halfEdgeMesh, ConvertStrateg
assert("Unsupported convert strategy.");
}

GetPolygonGroups().emplace_back(cd::MoveTemp(polygonGroup));

// Make capcity same with actual size.
vertexPositions.shrink_to_fit();
vertexNormals.shrink_to_fit();
m_vertexUVSets[0].shrink_to_fit();
polygonGroup.shrink_to_fit();
GetPolygonGroups().emplace_back(cd::MoveTemp(polygonGroup)).shrink_to_fit();
ShrinkToFit();
}

void MeshImpl::Init(uint32_t vertexCount)
{
assert(vertexCount > 0 && "No need to create an empty mesh.");

SetVertexPositionCount(vertexCount);
SetVertexNormalCount(vertexCount);
SetVertexTangentCount(vertexCount);
SetVertexBiTangentCount(vertexCount);
SetVertexInstanceIDCount(0U);

InitVertexAttributes(vertexCount);
}

void MeshImpl::Init(uint32_t vertexPositionCount, uint32_t vertexAttributeCount)
{
SetVertexPositionCount(vertexPositionCount);
SetVertexInstanceIDCount(vertexPositionCount);

InitVertexAttributes(vertexAttributeCount);
}

void MeshImpl::InitVertexAttributes(uint32_t vertexAttributeCount)
{
SetVertexAttributeCount(vertexAttributeCount);
SetVertexNormalCount(GetVertexAttributeCount());
SetVertexTangentCount(GetVertexAttributeCount());
SetVertexBiTangentCount(GetVertexAttributeCount());

for (uint32_t setIndex = 0U; setIndex < GetVertexUVSetCount(); ++setIndex)
{
m_vertexUVSets[setIndex].resize(GetVertexAttributeCount());
}

for (uint32_t setIndex = 0U; setIndex < m_vertexColorSetCount; ++setIndex)
{
m_vertexColorSets[setIndex].resize(GetVertexAttributeCount());
}
}

void MeshImpl::ShrinkToFit()
{
GetVertexPositions().shrink_to_fit();
GetVertexInstanceIDs().shrink_to_fit();

GetVertexNormals().shrink_to_fit();
GetVertexTangents().shrink_to_fit();
GetVertexBiTangents().shrink_to_fit();
for (uint32_t setIndex = 0U; setIndex < GetVertexUVSetCount(); ++setIndex)
{
m_vertexUVSets[setIndex].shrink_to_fit();
}
for (uint32_t setIndex = 0U; setIndex < GetVertexColorSetCount(); ++setIndex)
{
m_vertexColorSets[setIndex].shrink_to_fit();
}

GetPolygonGroups().shrink_to_fit();
for (uint32_t polygonGroupIndex = 0U; polygonGroupIndex < GetPolygonGroupCount(); ++polygonGroupIndex)
{
GetPolygonGroup(polygonGroupIndex).shrink_to_fit();
}
}

uint32_t MeshImpl::GetPolygonCount() const
Expand Down Expand Up @@ -209,6 +255,8 @@ void MeshImpl::ComputeVertexNormals()
return;
}

SetVertexNormalCount(vertexCount);

// Create a vector of normals with the same size as the vertex positions vector,
// initialized to the zero vector.
std::vector<Direction> vertexNormals(vertexCount, Direction(0, 0, 0));
Expand Down Expand Up @@ -248,6 +296,8 @@ void MeshImpl::ComputeVertexNormals()

void MeshImpl::ComputeVertexTangents()
{
SetVertexTangentCount(GetVertexCount());

// Compute the tangents
for (const auto& polygonGroup : GetPolygonGroups())
{
Expand Down Expand Up @@ -298,7 +348,7 @@ void MeshImpl::SetVertexUVSetCount(uint32_t setCount)
m_vertexUVSetCount = setCount;
for(uint32_t setIndex = 0U; setIndex < m_vertexUVSetCount; ++setIndex)
{
m_vertexUVSets[setIndex].resize(GetVertexCount());
m_vertexUVSets[setIndex].resize(GetVertexAttributeCount());
}
}

Expand All @@ -312,7 +362,7 @@ void MeshImpl::SetVertexColorSetCount(uint32_t setCount)
m_vertexColorSetCount = setCount;
for (uint32_t setIndex = 0U; setIndex < m_vertexColorSetCount; ++setIndex)
{
m_vertexColorSets[setIndex].resize(GetVertexCount());
m_vertexColorSets[setIndex].resize(GetVertexAttributeCount());
}
}

Expand Down
37 changes: 27 additions & 10 deletions private/Scene/MeshImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,25 @@ class MeshImpl final
public:
DECLARE_SCENE_IMPL_CLASS(Mesh);

void Init(uint32_t vertexCount);

IMPLEMENT_SIMPLE_TYPE_APIS(Mesh, ID);
IMPLEMENT_SIMPLE_TYPE_APIS(Mesh, BlendShapeID);
IMPLEMENT_SIMPLE_TYPE_APIS(Mesh, VertexAttributeCount);
IMPLEMENT_STRING_TYPE_APIS(Mesh, Name);
IMPLEMENT_COMPLEX_TYPE_APIS(Mesh, AABB);
IMPLEMENT_COMPLEX_TYPE_APIS(Mesh, VertexFormat);
IMPLEMENT_VECTOR_TYPE_APIS(Mesh, MaterialID);
IMPLEMENT_VECTOR_TYPE_APIS(Mesh, BlendShapeID);
IMPLEMENT_VECTOR_TYPE_APIS(Mesh, SkinID);
IMPLEMENT_VECTOR_TYPE_APIS(Mesh, VertexInstanceID);
IMPLEMENT_VECTOR_TYPE_APIS(Mesh, VertexPosition);
IMPLEMENT_VECTOR_TYPE_APIS(Mesh, VertexNormal);
IMPLEMENT_VECTOR_TYPE_APIS(Mesh, VertexTangent);
IMPLEMENT_VECTOR_TYPE_APIS(Mesh, VertexBiTangent);
IMPLEMENT_VECTOR_TYPE_APIS(Mesh, PolygonGroup);
IMPLEMENT_VECTOR_TYPE_APIS(Mesh, MaterialID);

void Init(uint32_t vertexCount);
void Init(uint32_t vertexPositionCount, uint32_t vertexAttributeCount);
void InitVertexAttributes(uint32_t vertexAttributeCount);
void ShrinkToFit();

uint32_t GetVertexCount() const { return GetVertexPositionCount(); }
uint32_t GetPolygonCount() const;
Expand Down Expand Up @@ -79,27 +83,35 @@ class MeshImpl final
template<bool SwapBytesOrder>
MeshImpl& operator<<(TInputArchive<SwapBytesOrder>& inputArchive)
{
uint32_t skinCount;
uint32_t materialCount;
uint32_t blendShapeCount;
uint32_t skinCount;
uint32_t vertexCount;
uint32_t vertexUVSetCount;
uint32_t vertexColorSetCount;
uint32_t vertexInfluenceCount;
uint32_t polygonGroupCount;

inputArchive >> GetName() >> GetID().Data() >> GetBlendShapeID().Data() >> GetAABB() >> materialCount >> skinCount
>> vertexCount >> vertexUVSetCount >> vertexColorSetCount >> vertexInfluenceCount >> polygonGroupCount;
inputArchive >> GetName() >> GetID().Data() >> GetAABB()
>> materialCount >> blendShapeCount >> skinCount
>> vertexCount >> GetVertexAttributeCount()
>> vertexUVSetCount >> vertexColorSetCount >> vertexInfluenceCount
>> polygonGroupCount;

GetVertexFormat() << inputArchive;

SetMaterialIDCount(materialCount);
inputArchive.ImportBuffer(GetMaterialIDs().data());

SetBlendShapeIDCount(blendShapeCount);
inputArchive.ImportBuffer(GetBlendShapeIDs().data());

SetSkinIDCount(skinCount);
inputArchive.ImportBuffer(GetSkinIDs().data());

Init(vertexCount);
Init(vertexCount, GetVertexAttributeCount());
inputArchive.ImportBuffer(GetVertexPositions().data());
inputArchive.ImportBuffer(GetVertexInstanceIDs().data());
inputArchive.ImportBuffer(GetVertexNormals().data());
inputArchive.ImportBuffer(GetVertexTangents().data());
inputArchive.ImportBuffer(GetVertexBiTangents().data());
Expand Down Expand Up @@ -146,13 +158,18 @@ class MeshImpl final
template<bool SwapBytesOrder>
const MeshImpl& operator>>(TOutputArchive<SwapBytesOrder>& outputArchive) const
{
outputArchive << GetName() << GetID().Data() << GetBlendShapeID().Data() << GetAABB() << GetMaterialIDCount() << GetSkinIDCount()
<< GetVertexCount() << GetVertexUVSetCount() << GetVertexColorSetCount() << GetVertexInfluenceCount() << GetPolygonGroupCount();
outputArchive << GetName() << GetID().Data() << GetAABB()
<< GetMaterialIDCount() << GetBlendShapeIDCount() << GetSkinIDCount()
<< GetVertexPositionCount() << GetVertexAttributeCount()
<< GetVertexUVSetCount() << GetVertexColorSetCount() << GetVertexInfluenceCount()
<< GetPolygonGroupCount();

GetVertexFormat() >> outputArchive;
outputArchive.ExportBuffer(GetMaterialIDs().data(), GetMaterialIDs().size());
outputArchive.ExportBuffer(GetBlendShapeIDs().data(), GetBlendShapeIDs().size());
outputArchive.ExportBuffer(GetSkinIDs().data(), GetSkinIDs().size());
outputArchive.ExportBuffer(GetVertexPositions().data(), GetVertexPositions().size());
outputArchive.ExportBuffer(GetVertexInstanceIDs().data(), GetVertexInstanceIDs().size());
outputArchive.ExportBuffer(GetVertexNormals().data(), GetVertexNormals().size());
outputArchive.ExportBuffer(GetVertexTangents().data(), GetVertexTangents().size());
outputArchive.ExportBuffer(GetVertexBiTangents().data(), GetVertexBiTangents().size());
Expand Down
Loading

0 comments on commit 458f5c5

Please sign in to comment.