18#define TINYOBJLOADER_IMPLEMENTATION
19#include <tiny_obj_loader.h>
20#define TINYGLTF_IMPLEMENTATION
29 bool operator()(
const tinyobj::index_t& a,
const tinyobj::index_t& b)
const
31 return a.vertex_index == b.vertex_index
32 && a.texcoord_index == b.texcoord_index
33 && a.normal_index == b.normal_index;
41 return ((a.vertex_index
42 ^ a.texcoord_index << 1) >> 1)
43 ^ (a.normal_index << 1);
53 c = (Ypoint / 100) - 1;
56 D = b * b - (4 * a * c);
59 x1 = (-b + sqrt(
D)) / (2 * a);
67 std::vector<ModelObject*>
Load(std::string_view path_name, std::string_view res_name)
71 return _load_obj(path_name);
72 else if (ext ==
"glb")
73 return _load_gltf(path_name,
true);
74 else if (ext ==
"gltf")
75 return _load_gltf(path_name,
false);
76 log_err(
"不支持的模型格式:{}", path_name);
80 std::vector<ModelObject*> _load_obj(std::string_view path_name)
82 std::string str_path = std::string{ path_name };
83 std::string str_base = String::EraseFilename(path_name);
85 const char* filename = str_path.c_str();
86 const char* basepath = str_base.c_str();
87 bool triangulate =
true;
89 log_info(
"开始加载obj文件:{},{}", filename, basepath);
91 tinyobj::attrib_t attrib;
92 std::vector<tinyobj::shape_t> shapes;
93 std::vector<tinyobj::material_t> materials;
98 bool b_read = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err, filename,
99 basepath, triangulate);
110 log_err(
"读取obj文件失败:{}", path_name);
113 log_info(
"顶点数:{}", attrib.vertices.size() / 3);
114 log_info(
"法线数:{}", attrib.normals.size() / 3);
115 log_info(
"UV数:{}", attrib.texcoords.size() / 2);
117 log_info(
"材质数:{}", materials.size());
120 bool is_axis_up_z =
false;
136 std::vector<ModelObject*> ret;
137 ModelObject* model_object =
new ModelObject;
138 std::vector<Vertex>& mo_vertices = model_object->_allVertex;
139 std::vector<uint32_t>& mo_indices = model_object->_allIndex;
140 std::vector<ModelObjectMaterial>& mo_material = model_object->_allMaterial;
141 ret.push_back(model_object);
144 mo_material.resize(materials.size());
145 for (
size_t i = 0; i < materials.size(); ++i)
147 tinyobj::material_t m = materials[i];
150 ModelObjectMaterial& material = mo_material[i];
151 material._materialName = m.name;
153 material._material._diffuseAlbedo = { m.diffuse[0], m.diffuse[1], m.diffuse[2], 1.0f };
154 material._material._fresnelR0 = { m.specular[0], m.specular[1], m.specular[2] };
157 if (!m.diffuse_texname.empty())
158 material._pathTexDiffuse = str_base + m.diffuse_texname;
159 if (!m.normal_texname.empty())
160 material._pathTexNormal = str_base + m.normal_texname;
165 std::vector<std::vector<tinyobj::index_t>> all_sub;
166 all_sub.resize(1 + materials.size());
168 for (
size_t i = 0; i < shapes.size(); i++)
170 tinyobj::shape_t& shape = shapes[i];
171 size_t num_index = shape.mesh.indices.size();
172 size_t num_face = shape.mesh.num_face_vertices.size();
173 log_info(
"读取子模型:{},{}", i, shape.name);
174 log_info(
"索引数:{};面数:{}", num_index, num_face);
177 size_t index_offset = 0;
179 for (
size_t j = 0; j < num_face; ++j)
181 int index_mat = shape.mesh.material_ids[j];
182 std::vector<tinyobj::index_t>& sub_idx = all_sub[1 + index_mat];
183 sub_idx.push_back(shape.mesh.indices[index_offset + 0]);
186 sub_idx.push_back(shape.mesh.indices[index_offset + 1]);
187 sub_idx.push_back(shape.mesh.indices[index_offset + 2]);
191 sub_idx.push_back(shape.mesh.indices[index_offset + 2]);
192 sub_idx.push_back(shape.mesh.indices[index_offset + 1]);
198 std::unordered_map<tinyobj::index_t, size_t, hash_idx, equal_idx>
202 for (std::vector<tinyobj::index_t>& sub_idx : all_sub)
205 sub._range._offsetIndex = i;
206 sub._range._countTriangle = sub_idx.size() / 3;
207 sub._indexMaterial = j - 1;
209 model_object->_allSub.push_back(sub);
211 for (tinyobj::index_t& idx : sub_idx)
213 auto iter = uniqueVertices.find(idx);
214 if (iter == uniqueVertices.end())
218 v._pos[0] = attrib.vertices[idx.vertex_index * 3 + 0];
221 v._pos[1] = attrib.vertices[idx.vertex_index * 3 + 1];
222 v._pos[2] = attrib.vertices[idx.vertex_index * 3 + 2];
226 v._pos[1] = attrib.vertices[idx.vertex_index * 3 + 2];
227 v._pos[2] = attrib.vertices[idx.vertex_index * 3 + 1];
231 v._uv[0] = attrib.texcoords[idx.texcoord_index * 2 + 0];
232 v._uv[1] = attrib.texcoords[idx.texcoord_index * 2 + 1];
233 v._uv[1] = 1.0f - v._uv[1];
235 uniqueVertices[idx] = mo_vertices.size();
236 mo_indices.push_back((uint32_t)mo_vertices.size());
237 mo_vertices.push_back(v);
241 mo_indices.push_back((uint32_t)iter->second);
246 log_msg(
"解析obj模型完成:v{},i{}", mo_vertices.size(), mo_indices.size());
249 std::vector<ModelObject*> _load_gltf(std::string_view path_name,
bool is_glb)
251 tinygltf::Model model;
252 tinygltf::TinyGLTF loader;
258 ret = loader.LoadBinaryFromFile(&model, &err, &warn, std::string{ path_name });
260 ret = loader.LoadASCIIFromFile(&model, &err, &warn, std::string{ path_name });
std::vector< ModelObject * > Load(std::string_view path_name, std::string_view res_name)
std::string GetExtension(std::string_view path_name, bool has_dot=false)
返回文件后缀
json Parse(std::string_view path_name)
从 文件 构建json对象
bool Get(const json &j, const std::string &name, T &t)
从json返回值,不存在则返回false,打印日志
ModelLoader * g_modelLoader
float ShininessToRoughness(float Ypoint)
bool operator()(const tinyobj::index_t &a, const tinyobj::index_t &b) const
size_t operator()(const tinyobj::index_t &a) const