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

@@ -7,31 +7,63 @@
#include "glm/mat4x4.hpp"
#include "util/result.hpp"
#include "util/string_list.hpp"
#include "assets/prefetch_queue.hpp"
#include "assets/data_stores.hpp"
#include "assets/path_id_lookups.hpp"
#include "assets/data/point_cloud_data.hpp"
#include "assets/data_stores/point_cloud_store.hpp"
#include "assets/prefetch_lookup.hpp"
namespace assets
{
template<bool Normal, bool Color, bool Reflectance>
struct generic_3dtk_loader
{
[[nodiscard]] static std::error_code prefetch(
const ztu::string_list& filenames,
prefetch_queue& queue
using data_type = point_cloud_data;
using store_type = point_cloud_data;
using lookup_type = point_cloud_id_lookup;
[[nodiscard]] std::error_code prefetch(
path_id_lookups& lookups
);
[[nodiscard]] static std::error_code load(
dynamic_point_cloud_buffer& buffer,
const file_dir_list& paths,
dynamic_point_cloud_store& store,
prefetch_lookup& id_lookup,
[[nodiscard]] std::error_code load(
path_id_lookups& lookups,
data_stores& stores,
bool pedantic = false
);
protected:
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{};
};
[[nodiscard]] static ztu::result<pose_prefetch_lookup::index_type> parse_index(
std::string_view filename
@@ -51,8 +83,13 @@ private:
const std::filesystem::path& filename,
dynamic_point_cloud_buffer& point_cloud
);
private:
std::vector<lookup_type::const_pointer> m_path_buffer;
};
}
#define INCLUDE_GENERIC_3DTK_LOADER_IMPLEMENTATION
#include "../../data_parsers"
#include "assets/file_parsers/generic/generic_3dtk_loader.ipp"
#undef INCLUDE_GENERIC_3DTK_LOADER_IMPLEMENTATION

View File

@@ -5,8 +5,6 @@
#include "assets/data_stores.hpp"
#include "assets/path_id_lookups.hpp"
#include "assets/prefetch_lookup.hpp"
#include "assets/prefetch_queue.hpp"
#include "assets/data/material_data.hpp"
#include "assets/data/material_library_data.hpp"
#include "assets/data_stores/material_store.hpp"
@@ -50,14 +48,35 @@ struct mtl_parser
);
protected:
class prefetcher_context
{
public:
prefetcher_context(
path_id_lookups& lookups
);
void operator()(lookup_type::const_pointer entry) noexcept;
protected:
[[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_lookups;
std::vector<char> m_buffer{};
};
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
path_id_lookups& lookups,
data_stores& stores
);
void operator()(lookup_type::const_pointer entry) noexcept;
@@ -72,21 +91,11 @@ protected:
);
private:
const texture_id_lookup* m_texture_id_lookup;
material_id_lookup* m_material_id_lookup;
material_store* m_material_store;
store_type* m_store;
path_id_lookups* m_lookups;
data_stores* m_stores;
data_type m_buffer{};
};
static void find_textures(
std::span<char> buffer,
std::filesystem::path& path_buffer,
const std::filesystem::path& base_directory,
std::ifstream& in,
ztu::string_list& texture_filenames
);
private:
std::vector<lookup_type::const_pointer> m_path_buffer;
};

View File

@@ -6,9 +6,10 @@
#include "assets/data/mesh_data.hpp"
#include "assets/data_stores.hpp"
#include "assets/path_id_lookups.hpp"
#include <set>
#include <unordered_map>
#include "util/array_hash.hpp"
namespace assets::obj_loader_error
namespace assets::obj_parser_error
{
enum class codes {
ok = 0,
@@ -27,7 +28,7 @@ enum class codes {
namespace assets
{
struct obj_loader
struct obj_parser
{
static constexpr auto name = std::string_view("obj");
using data_type = mesh_data;
@@ -46,28 +47,28 @@ struct obj_loader
);
protected:
struct indexed_vertex_type
using component_indices = std::array<z3d::vertex_index, 3>;
class prefetcher_context
{
z3d::index_triangle vertex;
z3d::vertex_index buffer_index;
public:
prefetcher_context(
path_id_lookups& id_lookups
);
friend auto operator<=>(const indexed_vertex_type& a, const indexed_vertex_type& b) {
return a.vertex <=> b.vertex;
}
void operator()(lookup_type::const_pointer entry) noexcept;
bool operator==(const indexed_vertex_type& other) const noexcept {
return other.vertex == vertex;
}
private:
path_id_lookups* m_id_lookups;
std::vector<char> m_buffer{};
};
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
path_id_lookups& m_id_lookups,
data_stores& m_stores
);
void operator()(lookup_type::const_pointer entry) noexcept;
@@ -75,6 +76,8 @@ protected:
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,
@@ -82,34 +85,16 @@ protected:
);
private:
const path_id_lookups* m_id_lookups;
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{};
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{};
};
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
);
private:
std::vector<lookup_type::const_pointer> m_path_buffer;
};
}

View File

@@ -3,28 +3,65 @@
#include <filesystem>
#include <system_error>
#include <string_view>
#include <unordered_map>
#include "assets/data_stores.hpp"
#include "assets/data"
#include "../data_stores"
#include "assets/prefetch_lookup.hpp"
#include "assets/prefetch_queue.hpp"
#include "assets/data/mesh_data.hpp"
#include "assets/data_stores.hpp"
#include "assets/path_id_lookups.hpp"
#include "util/vector_hash.hpp"
struct stl_loader {
namespace assets
{
struct stl_loader
{
static constexpr auto name = std::string_view("stl");
using data_type = mesh_data;
using store_type = mesh_store;
using lookup_type = mesh_id_lookup;
[[nodiscard]] static std::error_code prefetch(
const file_dir_list& paths,
prefetch_queue& queue
[[nodiscard]] std::error_code prefetch(
path_id_lookups& lookups
);
[[nodiscard]] static std::error_code load(
// space stuff that has to persist
dynamic_mesh_buffer& buffer,
const file_dir_list& paths,
prefetch_lookup& id_lookup,
shader_source_store& store,
[[nodiscard]] std::error_code load(
path_id_lookups& lookups,
data_stores& stores,
bool pedantic = false
);
protected:
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::unordered_map<mesh_vertex_components::position, z3d::vertex_index> m_vertex_index_lookup{};
};
private:
std::vector<lookup_type::const_pointer> m_path_buffer;
};
}