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

@@ -41,7 +41,7 @@ enum : z3d::size
};
}
enum class flags : z3d::u8
enum class flags : z3d::u16
{
none = 0,
ambient_filter = 1 << indices::ambient_filter,

View File

@@ -45,6 +45,8 @@ enum class flags : z3d::u8
using all = z3d::structure<
position,
normal,
tangent,
bi_tangent,
tex_coord,
color,
reflectance

View File

@@ -17,6 +17,10 @@ public:
C component_flags;
z3d::structure<z3d::vector<Ts>...> component_arrays{};
void add() {
}
protected:
void clear_component_arrays()
{

View File

@@ -15,7 +15,7 @@ public:
component_set() = default;
C component_flags{};
z3d::structure<z3d::optional<Ts...>> components{};
z3d::structure<z3d::optional<Ts>...> components{};
protected:
void clear_components()

View File

@@ -82,7 +82,7 @@ public:
using id_type = ID;
using container_type = std::vector<T>;
using iterator = generic_basic_store_iterator<ID, T>;
using const_iterator = generic_basic_store_iterator<std::add_const_t<T>...>;
using const_iterator = generic_basic_store_iterator<ID, std::add_const_t<T>>;
using size_type = std::size_t;
using data_type = T;

View File

@@ -17,15 +17,14 @@ class generic_material_store_iterator
{
public:
using size_type = std::size_t;
using count_type = ztu::u32;
using count_type = z3d::u32;
using component_flag_type = material_components::flags;
using id_type = material_id;
using value_type = std::pair<id_type, material_view>;
using flag_count_type = std::tuple<component_flag_type>;
using id_iterator_type = id_type*;
using component_iterator_type = std::tuple<std::add_pointer_t<Ts>...>;
using flag_count_iterator_type = const flag_count_type*;
using flag_iterator_type = const component_flag_type*;
using offsets_type = std::array<size_type, 1 + sizeof...(Ts)>;
using difference_type = std::ptrdiff_t;
@@ -39,7 +38,7 @@ private:
generic_material_store_iterator(
id_iterator_type ids,
const component_iterator_type& components,
flag_count_iterator_type flag_counts,
flag_iterator_type flags,
std::size_t index,
const offsets_type& offsets
);
@@ -88,7 +87,7 @@ protected:
private:
id_iterator_type m_ids{};
component_iterator_type m_components{};
flag_count_iterator_type m_flag_counts{};
flag_iterator_type m_flags{};
size_type m_index{};
offsets_type m_offsets{};
};
@@ -99,7 +98,7 @@ class generic_material_store<z3d::structure<Ts...>>
{
public:
using size_type = std::size_t;
using count_type = ztu::u32;
using count_type = z3d::u32;
using component_flag_type = material_components::flags;
using data_type = material_data;

View File

@@ -19,7 +19,7 @@ class generic_mesh_store_iterator
{
public:
using size_type = std::size_t;
using count_type = ztu::u32;
using count_type = z3d::u32;
using triangle_type = z3d::index_triangle;
using material_id_type = material_store::id_type;
using component_flag_type = mesh_vertex_components::flags;
@@ -109,7 +109,7 @@ class generic_mesh_store<z3d::structure<Ts...>>
{
public:
using size_type = std::size_t;
using count_type = ztu::u32;
using count_type = z3d::u32;
using triangle_type = z3d::index_triangle;
using component_flag_type = mesh_vertex_components::flags;
using material_id_type = material_id;

View File

@@ -12,7 +12,7 @@ namespace assets
class pose_list_store;
template<typename Char>
template<typename Pose>
class pose_list_store_iterator
{
public:
@@ -22,7 +22,7 @@ public:
using value_type = std::pair<id_type, pose_list_view>;
using id_iterator_type = id_type const*;
using pose_iterator_type = Char*;
using pose_iterator_type = Pose*;
using length_iterator_type = const length_type*;
using offset_type = size_type;
@@ -94,8 +94,8 @@ public:
using size_type = std::size_t;
using count_type = ztu::u32;
using iterator = pose_list_store_iterator<char>;
using const_iterator = pose_list_store_iterator<const char>;
using iterator = pose_list_store_iterator<pose_data>;
using const_iterator = pose_list_store_iterator<const pose_data>;
using data_type = pose_list_data;
using id_type = pose_list_id;
@@ -136,7 +136,7 @@ protected:
private:
std::vector<id_type> m_ids;
std::vector<char> m_poses;
std::vector<pose_data> m_poses;
std::vector<count_type> m_lengths;
mutable std::shared_mutex m_mutex;
};

View File

@@ -111,7 +111,6 @@ public:
[[nodiscard]] std::pair<const_iterator, bool> find(id_type id) const;
inline void remove(const iterator& it);
inline void clear();

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;
};
}

View File

@@ -0,0 +1,18 @@
#pragma once
#include <array>
#include <memory>
template<class T, size_t N>
struct std::hash<std::array<T, N>>
{
auto operator() (const std::array<T, N>& key) const {
auto hasher = std::hash<T>{};
auto result = std::size_t{};
for (const auto& element : key)
{
result = result * 31 + hasher(element);
}
return result;
}
};

View File

@@ -36,7 +36,7 @@
} \
\
constexpr ENUM_TYPE operator<<( \
ENUM_TYPE& lhs, \
ENUM_TYPE lhs, \
int shift \
) { \
return static_cast<ENUM_TYPE>( \
@@ -44,8 +44,17 @@ constexpr ENUM_TYPE operator<<( \
); \
} \
\
constexpr ENUM_TYPE operator<<( \
ENUM_TYPE lhs, \
std::size_t shift \
) { \
return static_cast<ENUM_TYPE>( \
static_cast<std::underlying_type_t<ENUM_TYPE>>(lhs) << shift \
); \
} \
\
constexpr ENUM_TYPE operator>>( \
ENUM_TYPE& lhs, \
ENUM_TYPE lhs, \
int shift \
) { \
return static_cast<ENUM_TYPE>( \

View File

@@ -146,5 +146,5 @@ private:
}
container_type m_container;
std::shared_mutex m_mutex;
mutable std::shared_mutex m_mutex;
};

View File

@@ -8,18 +8,24 @@ namespace ztu
template<typename Index, int ID>
class id_type
{
private:
explicit constexpr id_type(Index index) : index{ index } {}
Index index{};
public:
static constexpr id_type next()
{
static std::atomic<Index> next_index{ 1 };
return id_type{ next_index.fetch_add(1, std::memory_order_seq_cst) };
}
Index index{};
public:
constexpr id_type() = default;
constexpr id_type(const id_type& other) : index{ other.index } {}
constexpr id_type(id_type&& other) noexcept : index{ other.index } {}
constexpr id_type& operator=(const id_type& other) { index = other.index; return *this; }
constexpr id_type& operator=(id_type&& other) noexcept { index = other.index; return *this; }
constexpr auto operator<=>(const id_type&) const = default;
constexpr operator bool() const

View File

@@ -0,0 +1,24 @@
#pragma once
#include "glm/glm.hpp"
#include <memory>
// Hash function for Eigen matrix and vector.
// The code is from `hash_combine` function of the Boost library. See
// http://www.boost.org/doc/libs/1_55_0/doc/html/hash/reference.html#boost.hash_combine .
template<glm::length_t L, typename T, glm::qualifier Q>
struct std::hash<glm::vec<L, T, Q>>
{
auto operator() (const glm::vec<L, T, Q>& key) const
{
auto seed = std::size_t{};
for (glm::length_t i{}; i != L; ++i)
{
seed ^= std::hash<T>{}(key[i]) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
return seed;
}
};