Graphics: Add GraphicalMesh and Model classes

This commit is contained in:
Jérôme Leclercq
2021-01-27 16:45:00 +01:00
parent a1e0ae3f38
commit a9e9ef2524
7 changed files with 300 additions and 66 deletions

View File

@@ -0,0 +1,56 @@
// Copyright (C) 2017 Jérôme Leclercq
// 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/GraphicalMesh.hpp>
#include <Nazara/Graphics/Graphics.hpp>
#include <Nazara/Utility/SoftwareBuffer.hpp>
#include <Nazara/Utility/StaticMesh.hpp>
#include <cassert>
#include <Nazara/Graphics/Debug.hpp>
namespace Nz
{
GraphicalMesh::GraphicalMesh(const Mesh* mesh)
{
assert(mesh->GetAnimationType() == AnimationType_Static);
RenderDevice& renderDevice = Graphics::Instance()->GetRenderDevice();
m_subMeshes.reserve(mesh->GetSubMeshCount());
for (std::size_t i = 0; i < mesh->GetSubMeshCount(); ++i)
{
const SubMesh* subMesh = mesh->GetSubMesh(i);
const StaticMesh* staticMesh = static_cast<const StaticMesh*>(subMesh);
const IndexBuffer* indexBuffer = staticMesh->GetIndexBuffer();
const VertexBuffer* vertexBuffer = staticMesh->GetVertexBuffer();
assert(indexBuffer->GetBuffer()->GetStorage() == DataStorage_Software);
const SoftwareBuffer* indexBufferContent = static_cast<const SoftwareBuffer*>(indexBuffer->GetBuffer()->GetImpl());
assert(vertexBuffer->GetBuffer()->GetStorage() == DataStorage_Software);
const SoftwareBuffer* vertexBufferContent = static_cast<const SoftwareBuffer*>(vertexBuffer->GetBuffer()->GetImpl());
auto& submeshData = m_subMeshes.emplace_back();
submeshData.indexBuffer = renderDevice.InstantiateBuffer(BufferType_Index);
if (!submeshData.indexBuffer->Initialize(indexBuffer->GetStride() * indexBuffer->GetIndexCount(), BufferUsage_DeviceLocal))
throw std::runtime_error("failed to create index buffer");
if (!submeshData.indexBuffer->Fill(indexBufferContent->GetData() + indexBuffer->GetStartOffset(), 0, indexBuffer->GetEndOffset() - indexBuffer->GetStartOffset()))
throw std::runtime_error("failed to fill index buffer");
submeshData.indexCount = indexBuffer->GetIndexCount();
submeshData.vertexBuffer = renderDevice.InstantiateBuffer(BufferType_Vertex);
if (!submeshData.vertexBuffer->Initialize(vertexBuffer->GetStride() * vertexBuffer->GetVertexCount(), BufferUsage_DeviceLocal))
throw std::runtime_error("failed to create vertex buffer");
if (!submeshData.vertexBuffer->Fill(vertexBufferContent->GetData() + vertexBuffer->GetStartOffset(), 0, vertexBuffer->GetEndOffset() - vertexBuffer->GetStartOffset()))
throw std::runtime_error("failed to fill vertex buffer");
submeshData.vertexDeclaration = vertexBuffer->GetVertexDeclaration();
}
}
}

View File

@@ -0,0 +1,51 @@
// Copyright (C) 2017 Jérôme Leclercq
// 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/Material.hpp>
#include <Nazara/Graphics/Debug.hpp>
namespace Nz
{
inline Model::Model(std::shared_ptr<GraphicalMesh> graphicalMesh) :
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)
}
};
}
}
const std::shared_ptr<AbstractBuffer>& 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<RenderPipeline>& Model::GetRenderPipeline(std::size_t subMeshIndex) const
{
assert(subMeshIndex < m_subMeshes.size());
const auto& subMeshData = m_subMeshes[subMeshIndex];
return subMeshData.material->GetPipeline()->GetRenderPipeline(subMeshData.vertexBufferData);
}
const std::shared_ptr<AbstractBuffer>& Model::GetVertexBuffer(std::size_t subMeshIndex) const
{
return m_graphicalMesh->GetVertexBuffer(subMeshIndex);
}
}