Files
NazaraEngine/src/Nazara/Graphics/Model.cpp

87 lines
2.8 KiB
C++

// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Graphics module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Graphics/Model.hpp>
#include <Nazara/Graphics/GraphicalMesh.hpp>
#include <Nazara/Graphics/Graphics.hpp>
#include <Nazara/Graphics/Material.hpp>
#include <Nazara/Graphics/RenderSubmesh.hpp>
#include <Nazara/Graphics/WorldInstance.hpp>
#include <Nazara/Renderer/CommandBufferBuilder.hpp>
#include <Nazara/Graphics/Debug.hpp>
namespace Nz
{
Model::Model(std::shared_ptr<GraphicalMesh> graphicalMesh, const Boxf& aabb) :
m_graphicalMesh(std::move(graphicalMesh))
{
m_submeshes.reserve(m_graphicalMesh->GetSubMeshCount());
for (std::size_t i = 0; i < m_graphicalMesh->GetSubMeshCount(); ++i)
{
auto& subMeshData = m_submeshes.emplace_back();
//subMeshData.material = DefaultMaterial; //< TODO
subMeshData.vertexBufferData = {
{
0,
m_graphicalMesh->GetVertexDeclaration(i)
}
};
}
UpdateAABB(aabb);
}
void Model::BuildElement(std::size_t passIndex, const WorldInstance& worldInstance, std::vector<std::unique_ptr<RenderElement>>& elements, const Recti& scissorBox) const
{
for (std::size_t i = 0; i < m_submeshes.size(); ++i)
{
const auto& submeshData = m_submeshes[i];
const auto& materialPass = submeshData.material->GetPass(passIndex);
if (!materialPass)
continue;
const auto& indexBuffer = m_graphicalMesh->GetIndexBuffer(i);
const auto& vertexBuffer = m_graphicalMesh->GetVertexBuffer(i);
const auto& renderPipeline = materialPass->GetPipeline()->GetRenderPipeline(submeshData.vertexBufferData);
elements.emplace_back(std::make_unique<RenderSubmesh>(GetRenderLayer(), materialPass, renderPipeline, worldInstance, m_graphicalMesh->GetIndexCount(i), indexBuffer, vertexBuffer, scissorBox));
}
}
const std::shared_ptr<RenderBuffer>& Model::GetIndexBuffer(std::size_t subMeshIndex) const
{
return m_graphicalMesh->GetIndexBuffer(subMeshIndex);
}
std::size_t Model::GetIndexCount(std::size_t subMeshIndex) const
{
return m_graphicalMesh->GetIndexCount(subMeshIndex);
}
const std::shared_ptr<Material>& Model::GetMaterial(std::size_t subMeshIndex) const
{
assert(subMeshIndex < m_submeshes.size());
const auto& subMeshData = m_submeshes[subMeshIndex];
return subMeshData.material;
}
std::size_t Model::GetMaterialCount() const
{
return m_submeshes.size();
}
const std::vector<RenderPipelineInfo::VertexBufferData>& Model::GetVertexBufferData(std::size_t subMeshIndex) const
{
assert(subMeshIndex < m_submeshes.size());
const auto& subMeshData = m_submeshes[subMeshIndex];
return subMeshData.vertexBufferData;
}
const std::shared_ptr<RenderBuffer>& Model::GetVertexBuffer(std::size_t subMeshIndex) const
{
return m_graphicalMesh->GetVertexBuffer(subMeshIndex);
}
}