dl
dl_model_loader.h
浏览该文件的文档.
1
12#pragma once
13
14#include <tiny_obj_loader.h>
15#include <tiny_gltf.h>
16
17#include "io/dl_json.h"
19
20namespace dl
21{
22 struct equal_idx
23 {
24 bool operator()(const tinyobj::index_t& a, const tinyobj::index_t& b) const
25 {
26 return a.vertex_index == b.vertex_index
27 && a.texcoord_index == b.texcoord_index
28 && a.normal_index == b.normal_index;
29 }
30 };
31
32 struct hash_idx
33 {
34 size_t operator()(const tinyobj::index_t& a) const
35 {
36 return ((a.vertex_index
37 ^ a.texcoord_index << 1) >> 1)
38 ^ (a.normal_index << 1);
39 }
40 };
41
42 inline float ShininessToRoughness(float Ypoint)
43 {
44 float a = -1;
45 float b = 2;
46
47 float c;
48 c = (Ypoint / 100) - 1;
49
50 float D;
51 D = b * b - (4 * a * c);
52
53 float x1;
54 x1 = (-b + sqrt(D)) / (2 * a);
55
56 return x1;
57 }
58
60{
61public:
62 std::vector<ModelObject*> Load(std::string_view path_name, std::string_view res_name)
63 {
64 std::string ext = File::GetExtension(path_name);
65 if (ext == "obj")
66 return _load_obj(path_name);
67 else if (ext == "glb")
68 return _load_gltf(path_name, res_name, true);
69 else if (ext == "gltf")
70 return _load_gltf(path_name, res_name, false);
71 log_err("不支持的模型格式:{}", path_name);
72 return {};
73 }
74private:
75 std::vector<ModelObject*> _load_obj(std::string_view path_name);
76 std::vector<ModelObject*> _load_gltf(
77 std::string_view path_name, std::string_view res_name, bool is_glb);
78
79 struct LoaderInfo
80 {
81 uint32_t* _indexBuffer;
82 VertexBone* _vertexBuffer;
83 size_t _indexPos = 0;
84 size_t _vertexPos = 0;
85 };
86 // 读取材质
87 void _gltf_load_material(const tinygltf::Model& model, std::string_view res_name,
88 std::vector<ModelObjectMaterial>& mo_material);
89 // 读取场景
90 void _gltf_load_scene(const tinygltf::Model& model, ModelData* data);
91
92 // 统计顶点和索引
93 void _gltf_get_node_prop(const tinygltf::Model& model, const tinygltf::Node& node,
94 size_t& vertex_count, size_t& index_count);
95
96 // 读取节点
97 void _gltf_load_node(const tinygltf::Model& model, int node,
98 ModelNode* model_node_parent, LoaderInfo& loader_info, ModelData* data);
99 // 读取蒙皮
100 void _gltf_load_skin(const tinygltf::Model& model, ModelData* data);
101 // 读取动画
102 void _gltf_load_animation(const tinygltf::Model& model, ModelData* data);
103
104 ModelNode* findNode(ModelData* data, ModelNode* parent, uint32_t index);
105
106 ModelNode* nodeFromIndex(ModelData* data, uint32_t index);
107
108 Position3 _vec3_cast(const std::vector<double>& vec3);
109 Position4 _vec4_cast(const std::vector<double>& vec4);
110 Position2 _vec2_cast(const float* p);
111 Position3 _vec3_cast(const float* p);
112 Position4 _vec4_cast(const float* p);
113
114 Matrix4 _mat4_cast(const std::vector<double>& matrix);
115 Matrix4 _mat4_cast(float* p);
116
117};
118
120
121}
std::vector< ModelObject * > Load(std::string_view path_name, std::string_view res_name)
json格式相关
#define log_err(...)
中转模型数据
std::string GetExtension(std::string_view path_name, bool has_dot=false)
返回文件后缀
std::array< Float, 3 > Position3
std::array< Float, 4 > Position4
Matrix< Float, 4, 4 > Matrix4
ModelLoader * g_modelLoader
float ShininessToRoughness(float Ypoint)
std::array< Float, 2 > Position2
3d顶点(带骨骼)
bool operator()(const tinyobj::index_t &a, const tinyobj::index_t &b) const
size_t operator()(const tinyobj::index_t &a) const