Files
Z3D/include/assets/file_parsers/obj_loader.hpp
2025-03-31 21:41:24 +02:00

116 lines
2.5 KiB
C++
Executable File

#pragma once
#include <filesystem>
#include <system_error>
#include <string_view>
#include "assets/data/mesh_data.hpp"
#include "assets/data_stores.hpp"
#include "assets/path_id_lookups.hpp"
#include <set>
namespace assets::obj_loader_error
{
enum class codes {
ok = 0,
cannot_open_file,
malformed_vertex,
malformed_texture_coordinate,
malformed_normal,
malformed_face,
face_index_out_of_range,
unknown_line_begin,
use_material_without_material_library,
unknown_material_name
};
} // namespace obj_loader_error
namespace assets
{
struct obj_loader
{
static constexpr auto name = std::string_view("obj");
using data_type = mesh_data;
using store_type = mesh_store;
using lookup_type = mesh_id_lookup;
// TODO port this mess to the new interface
[[nodiscard]] std::error_code prefetch(
path_id_lookups& lookups
);
[[nodiscard]] std::error_code load(
path_id_lookups& lookups,
data_stores& stores,
bool pedantic = false
);
protected:
struct indexed_vertex_type
{
z3d::index_triangle vertex;
z3d::vertex_index buffer_index;
friend auto operator<=>(const indexed_vertex_type& a, const indexed_vertex_type& b) {
return a.vertex <=> b.vertex;
}
bool operator==(const indexed_vertex_type& other) const noexcept {
return other.vertex == vertex;
}
};
class parser_context
{
public:
parser_context(
const texture_id_lookup& texture_id_lookup,
material_id_lookup& material_id_lookup,
material_store& material_store,
store_type& store
);
void operator()(lookup_type::const_pointer entry) noexcept;
protected:
void reset();
[[nodiscard]] std::optional<texture_id> fetch_texture_id(
const std::filesystem::path& mtl_dir,
std::string_view filename,
std::string_view texture_type_name
);
private:
const path_id_lookups* m_id_lookups;
data_stores* m_stores;
data_type m_buffer{};
data_type m_read_buffer{};
std::set<indexed_vertex_type> vertex_ids{};
};
static void find_materials(
std::span<char> buffer,
std::filesystem::path& path_buffer,
const std::filesystem::path& base_directory,
std::ifstream& in,
ztu::string_list& material_filenames
);
[[nodiscard]] static std::error_code obj_loader::parse_file(
dynamic_mesh_buffer& read_buffer,
dynamic_mesh_buffer& mesh_buffer,
std::filesystem::path& path_buffer,
const std::filesystem::path& base_directory,
std::set<indexed_vertex_type>& vertex_ids,
std::ifstream& in,
prefetch_lookup& id_lookup,
dynamic_shader_source_store& store,
bool pedantic
);
};
}