diff --git a/misc/MeshGenerteProducer.hpp b/misc/MeshGenerteProducer.hpp index 71c36fa..9f44d33 100644 --- a/misc/MeshGenerteProducer.hpp +++ b/misc/MeshGenerteProducer.hpp @@ -31,11 +31,11 @@ class MeshGenerteProducer final : public IProducer cd::Sphere sphere(cd::Point(0.0f, 0.0f, 0.0f), 2.0f); //cd::Box box(cd::Vec3f(-1.0f, -1.0f, -1.0f), cd::Vec3f(1.0f, 1.0f, 1.0f)); cd::VertexFormat vertexFormat; - vertexFormat.AddAttributeLayout(cd::VertexAttributeType::Position, cd::GetAttributeValueType(), 3); - vertexFormat.AddAttributeLayout(cd::VertexAttributeType::Normal, cd::GetAttributeValueType(), 3); - vertexFormat.AddAttributeLayout(cd::VertexAttributeType::UV, cd::GetAttributeValueType(), 3); - vertexFormat.AddAttributeLayout(cd::VertexAttributeType::Tangent, cd::GetAttributeValueType(), 3); - vertexFormat.AddAttributeLayout(cd::VertexAttributeType::Bitangent, cd::GetAttributeValueType(), 3); + vertexFormat.AddVertexAttributeLayout(cd::VertexAttributeType::Position, cd::GetAttributeValueType(), 3); + vertexFormat.AddVertexAttributeLayout(cd::VertexAttributeType::Normal, cd::GetAttributeValueType(), 3); + vertexFormat.AddVertexAttributeLayout(cd::VertexAttributeType::UV, cd::GetAttributeValueType(), 3); + vertexFormat.AddVertexAttributeLayout(cd::VertexAttributeType::Tangent, cd::GetAttributeValueType(), 3); + vertexFormat.AddVertexAttributeLayout(cd::VertexAttributeType::Bitangent, cd::GetAttributeValueType(), 3); std::optional optMesh = cd::MeshGenerator::Generate(sphere, 20, 20, vertexFormat); if (!optMesh.has_value()) { diff --git a/private/Math/MeshGenerator.cpp b/private/Math/MeshGenerator.cpp index 218f652..85aefa9 100644 --- a/private/Math/MeshGenerator.cpp +++ b/private/Math/MeshGenerator.cpp @@ -104,12 +104,12 @@ std::optional MeshGenerator::Generate(const Box& box, const VertexFormat& mesh.AddPolygonGroup(cd::MoveTemp(polygonGroup)); cd::VertexFormat meshVertexFormat; - meshVertexFormat.AddAttributeLayout(cd::VertexAttributeType::Position, cd::GetAttributeValueType(), cd::Point::Size); + meshVertexFormat.AddVertexAttributeLayout(cd::VertexAttributeType::Position, cd::GetAttributeValueType(), cd::Point::Size); if (vertexFormat.Contains(VertexAttributeType::Normal)) { mesh.ComputeVertexNormals(); - meshVertexFormat.AddAttributeLayout(cd::VertexAttributeType::Normal, cd::GetAttributeValueType(), cd::Direction::Size); + meshVertexFormat.AddVertexAttributeLayout(cd::VertexAttributeType::Normal, cd::GetAttributeValueType(), cd::Direction::Size); } if (vertexFormat.Contains(VertexAttributeType::UV)) @@ -121,15 +121,15 @@ std::optional MeshGenerator::Generate(const Box& box, const VertexFormat& mesh.SetVertexUV(0U, vertexIndex, cd::UV(position.x(), position.z())); } - meshVertexFormat.AddAttributeLayout(cd::VertexAttributeType::UV, cd::GetAttributeValueType(), cd::UV::Size); + meshVertexFormat.AddVertexAttributeLayout(cd::VertexAttributeType::UV, cd::GetAttributeValueType(), cd::UV::Size); } if (vertexFormat.Contains(VertexAttributeType::Tangent) || vertexFormat.Contains(VertexAttributeType::Bitangent)) { mesh.ComputeVertexTangents(); - meshVertexFormat.AddAttributeLayout(cd::VertexAttributeType::Tangent, cd::GetAttributeValueType(), cd::Direction::Size); - meshVertexFormat.AddAttributeLayout(cd::VertexAttributeType::Bitangent, cd::GetAttributeValueType(), cd::Direction::Size); + meshVertexFormat.AddVertexAttributeLayout(cd::VertexAttributeType::Tangent, cd::GetAttributeValueType(), cd::Direction::Size); + meshVertexFormat.AddVertexAttributeLayout(cd::VertexAttributeType::Bitangent, cd::GetAttributeValueType(), cd::Direction::Size); } // Use VertexColor0 to present braycentric coordinates. @@ -160,7 +160,7 @@ std::optional MeshGenerator::Generate(const Box& box, const VertexFormat& mesh.SetVertexColor(0U, 21U, cd::Vec4f(1.0f, 0.0f, 0.0f, 1.0f)); mesh.SetVertexColor(0U, 22U, cd::Vec4f(0.0f, 1.0f, 0.0f, 1.0f)); mesh.SetVertexColor(0U, 23U, cd::Vec4f(0.0f, 0.0f, 1.0f, 1.0f)); - meshVertexFormat.AddAttributeLayout(cd::VertexAttributeType::Color, cd::GetAttributeValueType(), cd::Vec4f::Size); + meshVertexFormat.AddVertexAttributeLayout(cd::VertexAttributeType::Color, cd::GetAttributeValueType(), cd::Vec4f::Size); } mesh.SetVertexFormat(MoveTemp(meshVertexFormat)); @@ -179,13 +179,13 @@ std::optional MeshGenerator::Generate(const Sphere& sphere, uint32_t numSt mesh.Init(vertexCount); cd::VertexFormat meshVertexFormat; - meshVertexFormat.AddAttributeLayout(cd::VertexAttributeType::Position, cd::GetAttributeValueType(), cd::Point::Size); + meshVertexFormat.AddVertexAttributeLayout(cd::VertexAttributeType::Position, cd::GetAttributeValueType(), cd::Point::Size); bool generateUV = vertexFormat.Contains(VertexAttributeType::UV); if (generateUV) { mesh.SetVertexUVSetCount(1); - meshVertexFormat.AddAttributeLayout(cd::VertexAttributeType::UV, cd::GetAttributeValueType(), cd::UV::Size); + meshVertexFormat.AddVertexAttributeLayout(cd::VertexAttributeType::UV, cd::GetAttributeValueType(), cd::UV::Size); } // Generate vertices @@ -284,20 +284,20 @@ std::optional MeshGenerator::Generate(const Sphere& sphere, uint32_t numSt } } - meshVertexFormat.AddAttributeLayout(cd::VertexAttributeType::Normal, cd::GetAttributeValueType(), cd::Direction::Size); + meshVertexFormat.AddVertexAttributeLayout(cd::VertexAttributeType::Normal, cd::GetAttributeValueType(), cd::Direction::Size); } if (vertexFormat.Contains(VertexAttributeType::Tangent) || vertexFormat.Contains(VertexAttributeType::Bitangent)) { mesh.ComputeVertexTangents(); - meshVertexFormat.AddAttributeLayout(cd::VertexAttributeType::Tangent, cd::GetAttributeValueType(), cd::Direction::Size); - meshVertexFormat.AddAttributeLayout(cd::VertexAttributeType::Bitangent, cd::GetAttributeValueType(), cd::Direction::Size); + meshVertexFormat.AddVertexAttributeLayout(cd::VertexAttributeType::Tangent, cd::GetAttributeValueType(), cd::Direction::Size); + meshVertexFormat.AddVertexAttributeLayout(cd::VertexAttributeType::Bitangent, cd::GetAttributeValueType(), cd::Direction::Size); } if (vertexFormat.Contains(VertexAttributeType::Color)) { mesh.SetVertexColorSetCount(1U); - meshVertexFormat.AddAttributeLayout(cd::VertexAttributeType::Color, cd::GetAttributeValueType(), cd::Vec4f::Size); + meshVertexFormat.AddVertexAttributeLayout(cd::VertexAttributeType::Color, cd::GetAttributeValueType(), cd::Vec4f::Size); } mesh.SetVertexFormat(MoveTemp(meshVertexFormat)); diff --git a/private/Producers/FbxProducer/FbxProducerImpl.cpp b/private/Producers/FbxProducer/FbxProducerImpl.cpp index 3d389e8..c233cac 100644 --- a/private/Producers/FbxProducer/FbxProducerImpl.cpp +++ b/private/Producers/FbxProducer/FbxProducerImpl.cpp @@ -953,28 +953,28 @@ cd::MeshID FbxProducerImpl::ImportMesh(const fbxsdk::FbxMesh* pFbxMesh, cd::Scen cd::VertexFormat meshVertexFormat; if (controlPointCount > 0U) { - meshVertexFormat.AddAttributeLayout(cd::VertexAttributeType::Position, cd::GetAttributeValueType(), cd::Point::Size); + meshVertexFormat.AddVertexAttributeLayout(cd::VertexAttributeType::Position, cd::GetAttributeValueType(), cd::Point::Size); } const fbxsdk::FbxLayerElementNormal* pLayerElementNormalData = pMeshBaseLayer->GetNormals(); if (pLayerElementNormalData) { mesh.SetVertexNormalCount(vertexInstanceCount); - meshVertexFormat.AddAttributeLayout(cd::VertexAttributeType::Normal, cd::GetAttributeValueType(), cd::Direction::Size); + meshVertexFormat.AddVertexAttributeLayout(cd::VertexAttributeType::Normal, cd::GetAttributeValueType(), cd::Direction::Size); } const fbxsdk::FbxLayerElementTangent* pLayerElementTangentData = pMeshBaseLayer->GetTangents(); if (pLayerElementTangentData) { mesh.SetVertexTangentCount(vertexInstanceCount); - meshVertexFormat.AddAttributeLayout(cd::VertexAttributeType::Tangent, cd::GetAttributeValueType(), cd::Direction::Size); + meshVertexFormat.AddVertexAttributeLayout(cd::VertexAttributeType::Tangent, cd::GetAttributeValueType(), cd::Direction::Size); } const fbxsdk::FbxLayerElementBinormal* pLayerElementBinormalData = pMeshBaseLayer->GetBinormals(); if (pLayerElementBinormalData) { mesh.SetVertexBiTangentCount(vertexInstanceCount); - meshVertexFormat.AddAttributeLayout(cd::VertexAttributeType::Bitangent, cd::GetAttributeValueType(), cd::Direction::Size); + meshVertexFormat.AddVertexAttributeLayout(cd::VertexAttributeType::Bitangent, cd::GetAttributeValueType(), cd::Direction::Size); } const fbxsdk::FbxLayerElementVertexColor* pLayerElementColorData = pMeshBaseLayer->GetVertexColors(); @@ -983,7 +983,7 @@ cd::MeshID FbxProducerImpl::ImportMesh(const fbxsdk::FbxMesh* pFbxMesh, cd::Scen // TODO : Multiple vertex color sets if necessary. mesh.SetVertexColorSetCount(1U); mesh.GetVertexColors(0U).resize(vertexInstanceCount); - meshVertexFormat.AddAttributeLayout(cd::VertexAttributeType::Color, cd::GetAttributeValueType(), cd::Vec4f::Size); + meshVertexFormat.AddVertexAttributeLayout(cd::VertexAttributeType::Color, cd::GetAttributeValueType(), cd::Vec4f::Size); } std::vector layerElementUVDatas; @@ -1004,7 +1004,7 @@ cd::MeshID FbxProducerImpl::ImportMesh(const fbxsdk::FbxMesh* pFbxMesh, cd::Scen { mesh.GetVertexUVs(uvSetIndex).resize(vertexInstanceCount); } - meshVertexFormat.AddAttributeLayout(cd::VertexAttributeType::UV, cd::GetAttributeValueType(), cd::UV::Size); + meshVertexFormat.AddVertexAttributeLayout(cd::VertexAttributeType::UV, cd::GetAttributeValueType(), cd::UV::Size); } mesh.SetVertexFormat(cd::MoveTemp(meshVertexFormat)); diff --git a/private/Producers/GenericProducer/GenericProducerImpl.cpp b/private/Producers/GenericProducer/GenericProducerImpl.cpp index ec81b46..d6b9032 100644 --- a/private/Producers/GenericProducer/GenericProducerImpl.cpp +++ b/private/Producers/GenericProducer/GenericProducerImpl.cpp @@ -223,7 +223,7 @@ cd::MeshID GenericProducerImpl::AddMesh(cd::SceneDatabase* pSceneDatabase, const const aiVector3D& position = pSourceMesh->mVertices[vertexDataIndex]; mesh.SetVertexPosition(vertexIndex, cd::Point(position.x, position.y, position.z)); } - meshVertexFormat.AddAttributeLayout(cd::VertexAttributeType::Position, cd::GetAttributeValueType(), cd::Point::Size); + meshVertexFormat.AddVertexAttributeLayout(cd::VertexAttributeType::Position, cd::GetAttributeValueType(), cd::Point::Size); if (pSourceMesh->HasNormals()) { @@ -233,7 +233,7 @@ cd::MeshID GenericProducerImpl::AddMesh(cd::SceneDatabase* pSceneDatabase, const const aiVector3D& normal = pSourceMesh->mNormals[vertexDataIndex]; mesh.SetVertexNormal(vertexIndex, cd::Direction(normal.x, normal.y, normal.z)); } - meshVertexFormat.AddAttributeLayout(cd::VertexAttributeType::Normal, cd::GetAttributeValueType(), cd::Direction::Size); + meshVertexFormat.AddVertexAttributeLayout(cd::VertexAttributeType::Normal, cd::GetAttributeValueType(), cd::Direction::Size); if (pSourceMesh->HasTangentsAndBitangents()) { @@ -243,7 +243,7 @@ cd::MeshID GenericProducerImpl::AddMesh(cd::SceneDatabase* pSceneDatabase, const const aiVector3D& tangent = pSourceMesh->mTangents[vertexDataIndex]; mesh.SetVertexTangent(vertexIndex, cd::Direction(tangent.x, tangent.y, tangent.z)); } - meshVertexFormat.AddAttributeLayout(cd::VertexAttributeType::Tangent, cd::GetAttributeValueType(), cd::Direction::Size); + meshVertexFormat.AddVertexAttributeLayout(cd::VertexAttributeType::Tangent, cd::GetAttributeValueType(), cd::Direction::Size); for (uint32_t vertexIndex = 0; vertexIndex < numVertices; ++vertexIndex) @@ -252,7 +252,7 @@ cd::MeshID GenericProducerImpl::AddMesh(cd::SceneDatabase* pSceneDatabase, const const aiVector3D& biTangent = pSourceMesh->mBitangents[vertexDataIndex]; mesh.SetVertexBiTangent(vertexIndex, cd::Direction(biTangent.x, biTangent.y, biTangent.z)); } - meshVertexFormat.AddAttributeLayout(cd::VertexAttributeType::Bitangent, cd::GetAttributeValueType(), cd::Direction::Size); + meshVertexFormat.AddVertexAttributeLayout(cd::VertexAttributeType::Bitangent, cd::GetAttributeValueType(), cd::Direction::Size); } } @@ -276,7 +276,7 @@ cd::MeshID GenericProducerImpl::AddMesh(cd::SceneDatabase* pSceneDatabase, const const aiVector3D& uv = vertexUVArray[vertexDataIndex]; mesh.SetVertexUV(uvSetIndex, vertexIndex, cd::UV(uv.x, uv.y)); } - meshVertexFormat.AddAttributeLayout(cd::VertexAttributeType::UV, cd::GetAttributeValueType(), cd::UV::Size); + meshVertexFormat.AddVertexAttributeLayout(cd::VertexAttributeType::UV, cd::GetAttributeValueType(), cd::UV::Size); } uint32_t colorSetCount = pSourceMesh->GetNumColorChannels(); @@ -291,7 +291,7 @@ cd::MeshID GenericProducerImpl::AddMesh(cd::SceneDatabase* pSceneDatabase, const const aiColor4D& color = vertexColorArray[vertexDataIndex]; mesh.SetVertexColor(colorSetIndex, vertexIndex, cd::Color(color.r, color.g, color.b, color.a)); } - meshVertexFormat.AddAttributeLayout(cd::VertexAttributeType::Color, cd::GetAttributeValueType(), cd::Color::Size); + meshVertexFormat.AddVertexAttributeLayout(cd::VertexAttributeType::Color, cd::GetAttributeValueType(), cd::Color::Size); } mesh.SetVertexFormat(cd::MoveTemp(meshVertexFormat)); diff --git a/private/Producers/TerrainProducer/TerrainProducerImpl.cpp b/private/Producers/TerrainProducer/TerrainProducerImpl.cpp index 371b0cf..902f4dd 100644 --- a/private/Producers/TerrainProducer/TerrainProducerImpl.cpp +++ b/private/Producers/TerrainProducer/TerrainProducerImpl.cpp @@ -395,8 +395,8 @@ Mesh TerrainProducerImpl::GenerateSectorAt(uint32_t sector_x, uint32_t sector_z, } // Set vertex attribute VertexFormat meshVertexFormat; - meshVertexFormat.AddAttributeLayout(VertexAttributeType::Position, GetAttributeValueType(), Point::Size); - meshVertexFormat.AddAttributeLayout(VertexAttributeType::UV, GetAttributeValueType(), UV::Size); + meshVertexFormat.AddVertexAttributeLayout(VertexAttributeType::Position, GetAttributeValueType(), Point::Size); + meshVertexFormat.AddVertexAttributeLayout(VertexAttributeType::UV, GetAttributeValueType(), UV::Size); terrain.SetVertexFormat(cd::MoveTemp(meshVertexFormat)); // Set aabb diff --git a/private/ProgressiveMesh/ProgressiveMeshImpl.cpp b/private/ProgressiveMesh/ProgressiveMeshImpl.cpp index 5d5a2e6..5efd956 100644 --- a/private/ProgressiveMesh/ProgressiveMeshImpl.cpp +++ b/private/ProgressiveMesh/ProgressiveMeshImpl.cpp @@ -423,7 +423,7 @@ cd::Mesh ProgressiveMeshImpl::GenerateLodMesh(uint32_t targetFaceCount, const cd } else { - mesh.GetVertexFormat().AddAttributeLayout(cd::VertexAttributeType::Position, cd::AttributeValueType::Float, 3); + mesh.GetVertexFormat().AddVertexAttributeLayout(cd::VertexAttributeType::Position, cd::AttributeValueType::Float, 3); } const auto& vertexFormat = mesh.GetVertexFormat(); diff --git a/private/Scene/VertexFormat.cpp b/private/Scene/VertexFormat.cpp index 2925556..2c6fb26 100644 --- a/private/Scene/VertexFormat.cpp +++ b/private/Scene/VertexFormat.cpp @@ -41,14 +41,24 @@ VertexFormat::~VertexFormat() } } -void VertexFormat::AddAttributeLayout(VertexAttributeType attributeType, AttributeValueType valueType, uint8_t count) +void VertexFormat::AddVertexAttributeLayout(VertexAttributeType attributeType, AttributeValueType valueType, uint8_t count) { - m_pVertexFormatImpl->AddAttributeLayout(attributeType, valueType, count); + m_pVertexFormatImpl->AddVertexAttributeLayout(attributeType, valueType, count); } -const std::vector& VertexFormat::GetVertexLayout() const +void VertexFormat::AddVertexAttributeLayout(VertexAttributeLayout vertexLayout) { - return m_pVertexFormatImpl->GetVertexLayout(); + m_pVertexFormatImpl->AddVertexAttributeLayout(cd::MoveTemp(vertexLayout)); +} + +const VertexAttributeLayout* VertexFormat::GetVertexAttributeLayout(VertexAttributeType attributeType) const +{ + return m_pVertexFormatImpl->GetVertexAttributeLayout(attributeType); +} + +const std::vector& VertexFormat::GetVertexAttributeLayouts() const +{ + return m_pVertexFormatImpl->GetVertexAttributeLayouts(); } bool VertexFormat::Contains(VertexAttributeType attributeType) const diff --git a/private/Scene/VertexFormatImpl.cpp b/private/Scene/VertexFormatImpl.cpp index 8ea65bd..d2e21be 100644 --- a/private/Scene/VertexFormatImpl.cpp +++ b/private/Scene/VertexFormatImpl.cpp @@ -14,28 +14,43 @@ VertexFormatImpl& VertexFormatImpl::operator=(const VertexFormatImpl& rhs) return *this; } -void VertexFormatImpl::AddAttributeLayout(VertexAttributeType attributeType, AttributeValueType valueType, uint8_t count) +void VertexFormatImpl::AddVertexAttributeLayout(VertexAttributeType attributeType, AttributeValueType valueType, uint8_t count) { m_vertexLayouts.push_back(VertexAttributeLayout{ .vertexAttributeType = attributeType, .attributeValueType = valueType, .attributeCount = count }); } -bool VertexFormatImpl::Contains(VertexAttributeType attributeType) const +void VertexFormatImpl::AddVertexAttributeLayout(VertexAttributeLayout vertexLayout) +{ + m_vertexLayouts.emplace_back(cd::MoveTemp(vertexLayout)); +} + +const VertexAttributeLayout* VertexFormatImpl::GetVertexAttributeLayout(VertexAttributeType attributeType) const { for (const auto& vertexLayout : m_vertexLayouts) { if (attributeType == vertexLayout.vertexAttributeType) { - return true; + return &vertexLayout; } } - return false; + return nullptr; +} + +bool VertexFormatImpl::Contains(VertexAttributeType attributeType) const +{ + return GetVertexAttributeLayout(attributeType) != nullptr; } bool VertexFormatImpl::IsCompatiableTo(const VertexFormatImpl& other) const { + if (other.m_vertexLayouts.empty()) + { + return m_vertexLayouts.empty(); + } + for (const auto& vertexLayout : other.m_vertexLayouts) { if (!Contains(vertexLayout.vertexAttributeType)) diff --git a/private/Scene/VertexFormatImpl.h b/private/Scene/VertexFormatImpl.h index 2cfb4f2..a6aafca 100644 --- a/private/Scene/VertexFormatImpl.h +++ b/private/Scene/VertexFormatImpl.h @@ -19,8 +19,10 @@ class VertexFormatImpl VertexFormatImpl& operator=(VertexFormatImpl&&) = default; ~VertexFormatImpl() = default; - void AddAttributeLayout(VertexAttributeType attributeType, AttributeValueType valueType, uint8_t count); - const std::vector& GetVertexLayout() const { return m_vertexLayouts; } + void AddVertexAttributeLayout(VertexAttributeType attributeType, AttributeValueType valueType, uint8_t count); + void AddVertexAttributeLayout(VertexAttributeLayout vertexLayout); + const VertexAttributeLayout* GetVertexAttributeLayout(VertexAttributeType attributeType) const; + const std::vector& GetVertexAttributeLayouts() const { return m_vertexLayouts; } // Returns if vertex format contains vertex attribute type. bool Contains(VertexAttributeType attributeType) const; diff --git a/public/Scene/VertexFormat.h b/public/Scene/VertexFormat.h index c4a47ad..6d22cb6 100644 --- a/public/Scene/VertexFormat.h +++ b/public/Scene/VertexFormat.h @@ -23,8 +23,10 @@ class CORE_API VertexFormat final VertexFormat& operator=(VertexFormat&&); ~VertexFormat(); - void AddAttributeLayout(VertexAttributeType attributeType, AttributeValueType valueType, uint8_t count); - const std::vector& GetVertexLayout() const; + void AddVertexAttributeLayout(VertexAttributeType attributeType, AttributeValueType valueType, uint8_t count); + void AddVertexAttributeLayout(VertexAttributeLayout vertexLayout); + const VertexAttributeLayout* GetVertexAttributeLayout(VertexAttributeType attributeType) const; + const std::vector& GetVertexAttributeLayouts() const; // Returns if vertex format contains vertex attribute type. bool Contains(VertexAttributeType attributeType) const;