Graphics/GraphicalMesh: Add AABB

This commit is contained in:
SirLynix 2023-03-09 17:50:38 +01:00
parent a96722d4ec
commit 55f2937678
10 changed files with 52 additions and 13 deletions

View File

@ -226,11 +226,11 @@ int main()
std::shared_ptr<Nz::MaterialInstance> planeMat = deferredMaterial->Instantiate(); std::shared_ptr<Nz::MaterialInstance> planeMat = deferredMaterial->Instantiate();
planeMat->SetTextureProperty("BaseColorMap", Nz::Texture::LoadFromFile(resourceDir / "dev_grey.png", texParams), planeSampler); planeMat->SetTextureProperty("BaseColorMap", Nz::Texture::LoadFromFile(resourceDir / "dev_grey.png", texParams), planeSampler);
Nz::Model spaceshipModel(std::move(gfxMesh), spaceship->GetAABB()); Nz::Model spaceshipModel(std::move(gfxMesh));
for (std::size_t i = 0; i < spaceshipModel.GetSubMeshCount(); ++i) for (std::size_t i = 0; i < spaceshipModel.GetSubMeshCount(); ++i)
spaceshipModel.SetMaterial(i, spaceshipMat); spaceshipModel.SetMaterial(i, spaceshipMat);
Nz::Model planeModel(std::move(planeMeshGfx), planeMesh->GetAABB()); Nz::Model planeModel(std::move(planeMeshGfx));
for (std::size_t i = 0; i < planeModel.GetSubMeshCount(); ++i) for (std::size_t i = 0; i < planeModel.GetSubMeshCount(); ++i)
planeModel.SetMaterial(i, planeMat); planeModel.SetMaterial(i, planeMat);

View File

@ -57,7 +57,7 @@ int main()
std::size_t normalMapProperty = materialInstance->FindTextureProperty("NormalMap"); std::size_t normalMapProperty = materialInstance->FindTextureProperty("NormalMap");
materialInstance->SetTextureProperty(normalMapProperty, normalMap); materialInstance->SetTextureProperty(normalMapProperty, normalMap);
Nz::Model model(std::move(gfxMesh), sphereMesh->GetAABB()); Nz::Model model(std::move(gfxMesh));
for (std::size_t i = 0; i < model.GetSubMeshCount(); ++i) for (std::size_t i = 0; i < model.GetSubMeshCount(); ++i)
model.SetMaterial(i, materialInstance); model.SetMaterial(i, materialInstance);

View File

@ -72,7 +72,7 @@ int main()
material->SetTextureProperty("AlphaMap", Nz::Texture::LoadFromFile(resourceDir / "alphatile.png", texParams)); material->SetTextureProperty("AlphaMap", Nz::Texture::LoadFromFile(resourceDir / "alphatile.png", texParams));
material->SetTextureProperty("BaseColorMap", Nz::Texture::LoadFromFile(resourceDir / "Spaceship/Texture/diffuse.png", texParams)); material->SetTextureProperty("BaseColorMap", Nz::Texture::LoadFromFile(resourceDir / "Spaceship/Texture/diffuse.png", texParams));
std::shared_ptr<Nz::Model> model = std::make_shared<Nz::Model>(std::move(gfxMesh), spaceshipAABB); std::shared_ptr<Nz::Model> model = std::make_shared<Nz::Model>(std::move(gfxMesh));
for (std::size_t i = 0; i < model->GetSubMeshCount(); ++i) for (std::size_t i = 0; i < model->GetSubMeshCount(); ++i)
model->SetMaterial(i, material); model->SetMaterial(i, material);
@ -118,7 +118,7 @@ int main()
std::shared_ptr<Nz::Mesh> colliderMesh = Nz::Mesh::Build(shipCollider->GenerateMesh()); std::shared_ptr<Nz::Mesh> colliderMesh = Nz::Mesh::Build(shipCollider->GenerateMesh());
std::shared_ptr<Nz::GraphicalMesh> colliderGraphicalMesh = Nz::GraphicalMesh::BuildFromMesh(*colliderMesh); std::shared_ptr<Nz::GraphicalMesh> colliderGraphicalMesh = Nz::GraphicalMesh::BuildFromMesh(*colliderMesh);
colliderModel = std::make_shared<Nz::Model>(colliderGraphicalMesh, spaceshipAABB); colliderModel = std::make_shared<Nz::Model>(colliderGraphicalMesh);
for (std::size_t i = 0; i < colliderModel->GetSubMeshCount(); ++i) for (std::size_t i = 0; i < colliderModel->GetSubMeshCount(); ++i)
colliderModel->SetMaterial(i, colliderMat); colliderModel->SetMaterial(i, colliderMat);
} }

View File

@ -108,7 +108,7 @@ int main()
//std::shared_ptr<Nz::Material> material = Nz::Graphics::Instance()->GetDefaultMaterials().basicTransparent; //std::shared_ptr<Nz::Material> material = Nz::Graphics::Instance()->GetDefaultMaterials().basicTransparent;
std::shared_ptr<Nz::Model> bobModel = std::make_shared<Nz::Model>(std::move(bobGfxMesh), bobAABB); std::shared_ptr<Nz::Model> bobModel = std::make_shared<Nz::Model>(std::move(bobGfxMesh));
std::vector<std::shared_ptr<Nz::MaterialInstance>> materials(bobMesh->GetMaterialCount()); std::vector<std::shared_ptr<Nz::MaterialInstance>> materials(bobMesh->GetMaterialCount());
std::bitset<5> alphaMaterials("01010"); std::bitset<5> alphaMaterials("01010");
@ -212,7 +212,7 @@ int main()
std::shared_ptr<Nz::MaterialInstance> sphereMat = Nz::Graphics::Instance()->GetDefaultMaterials().phongMaterial->Instantiate(); std::shared_ptr<Nz::MaterialInstance> sphereMat = Nz::Graphics::Instance()->GetDefaultMaterials().phongMaterial->Instantiate();
sphereMat->SetTextureProperty("BaseColorMap", Nz::Texture::LoadFromFile(resourceDir / "Rusty/rustediron2_basecolor.png", srgbTexParams)); sphereMat->SetTextureProperty("BaseColorMap", Nz::Texture::LoadFromFile(resourceDir / "Rusty/rustediron2_basecolor.png", srgbTexParams));
std::shared_ptr<Nz::Model> sphereModel = std::make_shared<Nz::Model>(std::move(gfxMesh), sphereMesh->GetAABB()); std::shared_ptr<Nz::Model> sphereModel = std::make_shared<Nz::Model>(std::move(gfxMesh));
for (std::size_t i = 0; i < sphereModel->GetSubMeshCount(); ++i) for (std::size_t i = 0; i < sphereModel->GetSubMeshCount(); ++i)
sphereModel->SetMaterial(i, sphereMat); sphereModel->SetMaterial(i, sphereMat);
@ -318,7 +318,7 @@ int main()
floorBox = planeMesh.GetAABB(); floorBox = planeMesh.GetAABB();
std::shared_ptr<Nz::Model> planeModel = std::make_shared<Nz::Model>(std::move(planeMeshGfx), planeMesh.GetAABB()); std::shared_ptr<Nz::Model> planeModel = std::make_shared<Nz::Model>(std::move(planeMeshGfx));
planeModel->SetMaterial(0, planeMat); planeModel->SetMaterial(0, planeMat);
auto& planeGfx = planeEntity.emplace<Nz::GraphicsComponent>(); auto& planeGfx = planeEntity.emplace<Nz::GraphicsComponent>();
@ -336,7 +336,7 @@ int main()
std::shared_ptr<Nz::GraphicalMesh> boxMeshGfx = Nz::GraphicalMesh::BuildFromMesh(boxMesh); std::shared_ptr<Nz::GraphicalMesh> boxMeshGfx = Nz::GraphicalMesh::BuildFromMesh(boxMesh);
std::shared_ptr<Nz::Model> boxModel = std::make_shared<Nz::Model>(std::move(boxMeshGfx), boxMesh.GetAABB()); std::shared_ptr<Nz::Model> boxModel = std::make_shared<Nz::Model>(std::move(boxMeshGfx));
boxModel->SetMaterial(0, planeMat); boxModel->SetMaterial(0, planeMat);
entt::handle boxEntity = world.CreateEntity(); entt::handle boxEntity = world.CreateEntity();

View File

@ -31,6 +31,7 @@ namespace Nz
inline void Clear(); inline void Clear();
inline const Boxf& GetAABB() const;
inline const std::shared_ptr<RenderBuffer>& GetIndexBuffer(std::size_t subMesh) const; inline const std::shared_ptr<RenderBuffer>& GetIndexBuffer(std::size_t subMesh) const;
inline UInt32 GetIndexCount(std::size_t subMesh) const; inline UInt32 GetIndexCount(std::size_t subMesh) const;
inline IndexType GetIndexType(std::size_t subMesh) const; inline IndexType GetIndexType(std::size_t subMesh) const;
@ -38,6 +39,7 @@ namespace Nz
inline const std::shared_ptr<const VertexDeclaration>& GetVertexDeclaration(std::size_t subMesh) const; inline const std::shared_ptr<const VertexDeclaration>& GetVertexDeclaration(std::size_t subMesh) const;
inline std::size_t GetSubMeshCount() const; inline std::size_t GetSubMeshCount() const;
inline void UpdateAABB(const Boxf& aabb);
inline void UpdateSubMeshIndexCount(std::size_t subMeshIndex, UInt32 indexCount); inline void UpdateSubMeshIndexCount(std::size_t subMeshIndex, UInt32 indexCount);
GraphicalMesh& operator=(const GraphicalMesh&) = delete; GraphicalMesh& operator=(const GraphicalMesh&) = delete;
@ -52,12 +54,15 @@ namespace Nz
UInt32 indexCount; UInt32 indexCount;
}; };
static inline std::shared_ptr<GraphicalMesh> Build(const Primitive& primitive, const MeshParams& params = MeshParams());
static inline std::shared_ptr<GraphicalMesh> Build(const PrimitiveList& primitiveList, const MeshParams& params = MeshParams());
static std::shared_ptr<GraphicalMesh> BuildFromMesh(const Mesh& mesh); static std::shared_ptr<GraphicalMesh> BuildFromMesh(const Mesh& mesh);
NazaraSignal(OnInvalidated, GraphicalMesh* /*gfxMesh*/); NazaraSignal(OnInvalidated, GraphicalMesh* /*gfxMesh*/);
private: private:
std::vector<SubMesh> m_subMeshes; std::vector<SubMesh> m_subMeshes;
Boxf m_aabb;
}; };
} }

View File

@ -25,6 +25,11 @@ namespace Nz
OnInvalidated(this); OnInvalidated(this);
} }
inline const Boxf& GraphicalMesh::GetAABB() const
{
return m_aabb;
}
inline const std::shared_ptr<RenderBuffer>& GraphicalMesh::GetIndexBuffer(std::size_t subMesh) const inline const std::shared_ptr<RenderBuffer>& GraphicalMesh::GetIndexBuffer(std::size_t subMesh) const
{ {
assert(subMesh < m_subMeshes.size()); assert(subMesh < m_subMeshes.size());
@ -60,6 +65,13 @@ namespace Nz
return m_subMeshes.size(); return m_subMeshes.size();
} }
inline void GraphicalMesh::UpdateAABB(const Boxf& aabb)
{
m_aabb = aabb;
OnInvalidated(this);
}
inline void GraphicalMesh::UpdateSubMeshIndexCount(std::size_t subMeshIndex, UInt32 indexCount) inline void GraphicalMesh::UpdateSubMeshIndexCount(std::size_t subMeshIndex, UInt32 indexCount)
{ {
NazaraAssert(subMeshIndex < m_subMeshes.size(), "invalid submesh index"); NazaraAssert(subMeshIndex < m_subMeshes.size(), "invalid submesh index");
@ -67,6 +79,25 @@ namespace Nz
OnInvalidated(this); OnInvalidated(this);
} }
inline std::shared_ptr<GraphicalMesh> GraphicalMesh::Build(const Primitive& primitive, const MeshParams& params)
{
Mesh mesh;
mesh.CreateStatic();
mesh.BuildSubMesh(primitive, params);
return BuildFromMesh(mesh);
}
inline std::shared_ptr<GraphicalMesh> GraphicalMesh::Build(const PrimitiveList& primitiveList, const MeshParams& params)
{
Mesh mesh;
mesh.CreateStatic();
mesh.BuildSubMeshes(primitiveList, params);
return BuildFromMesh(mesh);
}
} }
#include <Nazara/Graphics/DebugOff.hpp> #include <Nazara/Graphics/DebugOff.hpp>

View File

@ -23,7 +23,7 @@ namespace Nz
class NAZARA_GRAPHICS_API Model : public InstancedRenderable class NAZARA_GRAPHICS_API Model : public InstancedRenderable
{ {
public: public:
Model(std::shared_ptr<GraphicalMesh> graphicalMesh, const Boxf& aabb); Model(std::shared_ptr<GraphicalMesh> graphicalMesh);
Model(const Model&) = delete; Model(const Model&) = delete;
Model(Model&&) noexcept = default; Model(Model&&) noexcept = default;
~Model() = default; ~Model() = default;

View File

@ -54,6 +54,8 @@ namespace Nz
gfxMesh->AddSubMesh(std::move(submeshData)); gfxMesh->AddSubMesh(std::move(submeshData));
} }
gfxMesh->UpdateAABB(mesh.GetAABB());
return gfxMesh; return gfxMesh;
} }
} }

View File

@ -14,7 +14,7 @@
namespace Nz namespace Nz
{ {
Model::Model(std::shared_ptr<GraphicalMesh> graphicalMesh, const Boxf& aabb) : Model::Model(std::shared_ptr<GraphicalMesh> graphicalMesh) :
m_graphicalMesh(std::move(graphicalMesh)) m_graphicalMesh(std::move(graphicalMesh))
{ {
Graphics* graphics = Graphics::Instance(); Graphics* graphics = Graphics::Instance();
@ -34,10 +34,11 @@ namespace Nz
m_onInvalidated.Connect(m_graphicalMesh->OnInvalidated, [this](GraphicalMesh*) m_onInvalidated.Connect(m_graphicalMesh->OnInvalidated, [this](GraphicalMesh*)
{ {
UpdateAABB(m_graphicalMesh->GetAABB());
OnElementInvalidated(this); OnElementInvalidated(this);
}); });
UpdateAABB(aabb); UpdateAABB(m_graphicalMesh->GetAABB());
} }
void Model::BuildElement(ElementRendererRegistry& registry, const ElementData& elementData, std::size_t passIndex, std::vector<RenderElementOwner>& elements) const void Model::BuildElement(ElementRendererRegistry& registry, const ElementData& elementData, std::size_t passIndex, std::vector<RenderElementOwner>& elements) const

View File

@ -72,7 +72,7 @@ int main()
std::shared_ptr<Nz::MaterialInstance> materialInstance2 = std::make_shared<Nz::MaterialInstance>(material); std::shared_ptr<Nz::MaterialInstance> materialInstance2 = std::make_shared<Nz::MaterialInstance>(material);
materialInstance2->SetValueProperty(0, Nz::Color::Green()); materialInstance2->SetValueProperty(0, Nz::Color::Green());
Nz::Model model(std::move(gfxMesh), spaceshipMesh->GetAABB()); Nz::Model model(std::move(gfxMesh));
for (std::size_t i = 0; i < model.GetSubMeshCount(); ++i) for (std::size_t i = 0; i < model.GetSubMeshCount(); ++i)
model.SetMaterial(i, materialInstance); model.SetMaterial(i, materialInstance);