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