Ported the obj parser.

This commit is contained in:
zy4n
2025-04-01 21:51:56 +02:00
parent bc065bc657
commit 27977c1738
34 changed files with 1676 additions and 1554 deletions

View File

@@ -0,0 +1,100 @@
#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 <unordered_map>
#include "util/array_hash.hpp"
namespace assets::obj_parser_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_parser
{
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:
using component_indices = std::array<z3d::vertex_index, 3>;
class prefetcher_context
{
public:
prefetcher_context(
path_id_lookups& id_lookups
);
void operator()(lookup_type::const_pointer entry) noexcept;
private:
path_id_lookups* m_id_lookups;
std::vector<char> m_buffer{};
};
class parser_context
{
public:
parser_context(
path_id_lookups& m_id_lookups,
data_stores& m_stores
);
void operator()(lookup_type::const_pointer entry) noexcept;
protected:
void reset();
z3d::vertex_index find_or_push_vertex(const component_indices& vertex_comp_indices);
[[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:
path_id_lookups* m_id_lookups;
data_stores* m_stores;
data_type m_mesh{};
std::vector<mesh_vertex_components::position> m_position_buffer{};
std::vector<mesh_vertex_components::normal> m_normal_buffer{};
std::vector<mesh_vertex_components::tex_coord> m_tex_coord_buffer{};
std::unordered_map<component_indices, z3d::vertex_index> m_vertex_comp_indices_to_vertex_index{};
};
private:
std::vector<lookup_type::const_pointer> m_path_buffer;
};
}