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 struct LoaderInfo
81 {
82 uint32_t* _indexBuffer;
83 VertexBone* _vertexBuffer;
84 size_t _indexPos = 0;
85 size_t _vertexPos = 0;
86 };
87 // 读取材质
88 void _gltf_load_material(const tinygltf::Model& model, std::string_view res_name,
89 std::vector<ModelObjectMaterial>& mo_material);
90 // 读取场景
91 void _gltf_load_scene(const tinygltf::Model& model, ModelData* data);
92
93 // 统计顶点和索引
94 void _gltf_get_node_prop(const tinygltf::Model& model, const tinygltf::Node& node,
95 size_t& vertex_count, size_t& index_count);
96
97 // 读取节点
98 void _gltf_load_node(const tinygltf::Model& model, int node,
99 ModelNode* model_node_parent, LoaderInfo& loader_info, ModelData* data);
100 // 读取蒙皮
101 void _gltf_load_skin(const tinygltf::Model& model, ModelData* data);
102 // 读取动画
103 void _gltf_load_animation(const tinygltf::Model& model, ModelData* data);
104
105 ModelNode* findNode(ModelData* data, ModelNode* parent, uint32_t index);
106
107 ModelNode* nodeFromIndex(ModelData* data, uint32_t index);
108
109 Position3 _vec3_cast(const std::vector<double>& vec3);
110 Position4 _vec4_cast(const std::vector<double>& vec4);
111 Position2 _vec2_cast(const float* p);
112 Position3 _vec3_cast(const float* p);
113 Position4 _vec4_cast(const float* p);
114
115 Matrix4 _mat4_cast(const std::vector<double>& matrix);
116 Matrix4 _mat4_cast(float* p);
117
118};
119
121
122}
123#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
float ShininessToRoughness(float Ypoint)
3d顶点(带骨骼)
bool operator()(const tinyobj::index_t &a, const tinyobj::index_t &b) const
size_t operator()(const tinyobj::index_t &a) const