Graphics/Model: Add model loader
This commit is contained in:
56
src/Nazara/Graphics/Formats/ModelMeshLoader.cpp
Normal file
56
src/Nazara/Graphics/Formats/ModelMeshLoader.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
// Copyright (C) 2024 Jérôme "SirLynix" 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/Formats/ModelMeshLoader.hpp>
|
||||
#include <Nazara/Graphics/GraphicalMesh.hpp>
|
||||
#include <Nazara/Graphics/MaterialInstance.hpp>
|
||||
#include <Nazara/Graphics/Model.hpp>
|
||||
#include <Nazara/Utility/Mesh.hpp>
|
||||
#include <Nazara/Utility/Utility.hpp>
|
||||
#include <Nazara/Graphics/Debug.hpp>
|
||||
|
||||
namespace Nz::Loaders
|
||||
{
|
||||
ModelLoader::Entry GetModelLoader_Mesh()
|
||||
{
|
||||
ModelLoader::Entry loaderEntry;
|
||||
loaderEntry.extensionSupport = [](std::string_view extension)
|
||||
{
|
||||
return Utility::Instance()->GetMeshLoader().IsExtensionSupported(extension);
|
||||
};
|
||||
|
||||
loaderEntry.streamLoader = [](Stream& stream, const ModelParams& parameters) -> Result<std::shared_ptr<Model>, ResourceLoadingError>
|
||||
{
|
||||
std::shared_ptr<Mesh> mesh = Mesh::LoadFromStream(stream, parameters.mesh);
|
||||
if (!mesh)
|
||||
return Err(ResourceLoadingError::Unrecognized);
|
||||
|
||||
std::shared_ptr<GraphicalMesh> gfxMesh = GraphicalMesh::BuildFromMesh(*mesh);
|
||||
if (!gfxMesh)
|
||||
return Err(ResourceLoadingError::Internal);
|
||||
|
||||
std::shared_ptr<Model> model = std::make_shared<Model>(std::move(gfxMesh));
|
||||
if (parameters.loadMaterials)
|
||||
{
|
||||
for (std::size_t matIndex = 0; matIndex < model->GetMaterialCount(); ++matIndex)
|
||||
{
|
||||
if (std::shared_ptr<MaterialInstance> matInstance = MaterialInstance::Build(mesh->GetMaterialData(matIndex)))
|
||||
model->SetMaterial(matIndex, std::move(matInstance));
|
||||
}
|
||||
}
|
||||
|
||||
return model;
|
||||
};
|
||||
|
||||
loaderEntry.parameterFilter = [](const ModelParams& parameters)
|
||||
{
|
||||
if (auto result = parameters.custom.GetBooleanParameter("SkipNativeMeshLoader"); result.GetValueOr(false))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
return loaderEntry;
|
||||
}
|
||||
}
|
||||
18
src/Nazara/Graphics/Formats/ModelMeshLoader.hpp
Normal file
18
src/Nazara/Graphics/Formats/ModelMeshLoader.hpp
Normal file
@@ -0,0 +1,18 @@
|
||||
// Copyright (C) 2024 Jérôme "SirLynix" 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
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NAZARA_GRAPHICS_FORMATS_MODELMESHLOADER_HPP
|
||||
#define NAZARA_GRAPHICS_FORMATS_MODELMESHLOADER_HPP
|
||||
|
||||
#include <NazaraUtils/Prerequisites.hpp>
|
||||
#include <Nazara/Graphics/Model.hpp>
|
||||
|
||||
namespace Nz::Loaders
|
||||
{
|
||||
ModelLoader::Entry GetModelLoader_Mesh();
|
||||
}
|
||||
|
||||
#endif // NAZARA_GRAPHICS_FORMATS_MODELMESHLOADER_HPP
|
||||
@@ -15,6 +15,7 @@
|
||||
#include <Nazara/Graphics/PipelinePassList.hpp>
|
||||
#include <Nazara/Graphics/PostProcessPipelinePass.hpp>
|
||||
#include <Nazara/Graphics/PredefinedMaterials.hpp>
|
||||
#include <Nazara/Graphics/Formats/ModelMeshLoader.hpp>
|
||||
#include <Nazara/Graphics/Formats/PipelinePassListLoader.hpp>
|
||||
#include <Nazara/Graphics/Formats/TextureLoader.hpp>
|
||||
#include <Nazara/Utility/Font.hpp>
|
||||
@@ -164,6 +165,7 @@ namespace Nz
|
||||
Font::SetDefaultAtlas(std::make_shared<GuillotineTextureAtlas>(*m_renderDevice));
|
||||
|
||||
m_materialInstanceLoader.RegisterLoader(Loaders::GetMaterialInstanceLoader_Texture()); // texture to material loader
|
||||
m_modelLoader.RegisterLoader(Loaders::GetModelLoader_Mesh());
|
||||
m_pipelinePassListLoader.RegisterLoader(Loaders::GetPipelinePassListLoader()); // texture to material loader
|
||||
}
|
||||
|
||||
|
||||
@@ -13,6 +13,11 @@
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
bool ModelParams::IsValid() const
|
||||
{
|
||||
return mesh.IsValid();
|
||||
}
|
||||
|
||||
Model::Model(std::shared_ptr<GraphicalMesh> graphicalMesh) :
|
||||
m_graphicalMesh(std::move(graphicalMesh))
|
||||
{
|
||||
@@ -94,4 +99,28 @@ namespace Nz
|
||||
{
|
||||
return m_graphicalMesh->GetVertexBuffer(subMeshIndex);
|
||||
}
|
||||
|
||||
std::shared_ptr<Model> Model::LoadFromFile(const std::filesystem::path& filePath, const ModelParams& params)
|
||||
{
|
||||
Graphics* graphics = Graphics::Instance();
|
||||
NazaraAssert(graphics, "Graphics module has not been initialized");
|
||||
|
||||
return graphics->GetModelLoader().LoadFromFile(filePath, params);
|
||||
}
|
||||
|
||||
std::shared_ptr<Model> Model::LoadFromMemory(const void* data, std::size_t size, const ModelParams& params)
|
||||
{
|
||||
Graphics* graphics = Graphics::Instance();
|
||||
NazaraAssert(graphics, "Graphics module has not been initialized");
|
||||
|
||||
return graphics->GetModelLoader().LoadFromMemory(data, size, params);
|
||||
}
|
||||
|
||||
std::shared_ptr<Model> Model::LoadFromStream(Stream& stream, const ModelParams& params)
|
||||
{
|
||||
Graphics* graphics = Graphics::Instance();
|
||||
NazaraAssert(graphics, "Graphics module has not been initialized");
|
||||
|
||||
return graphics->GetModelLoader().LoadFromStream(stream, params);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user