126 lines
3.0 KiB
C++
126 lines
3.0 KiB
C++
/**
|
|
* @file dl_model_loader.ixx
|
|
* @brief 模型加载器
|
|
*
|
|
*
|
|
* @version 1.0
|
|
* @author lveyou
|
|
* @date 22-09-02
|
|
*
|
|
* @note 支持obj、gltf(未完)
|
|
*/
|
|
#pragma once
|
|
|
|
#ifndef DL_DISABLE_RENDER
|
|
#include <tiny_obj_loader.h>
|
|
#include <tiny_gltf.h>
|
|
|
|
#include "io/dl_json.h"
|
|
#include "render/dl_model_object.h"
|
|
|
|
namespace dl
|
|
{
|
|
struct equal_idx
|
|
{
|
|
bool operator()(const tinyobj::index_t& a, const tinyobj::index_t& b) const
|
|
{
|
|
return a.vertex_index == b.vertex_index
|
|
&& a.texcoord_index == b.texcoord_index
|
|
&& a.normal_index == b.normal_index;
|
|
}
|
|
};
|
|
|
|
struct hash_idx
|
|
{
|
|
size_t operator()(const tinyobj::index_t& a) const
|
|
{
|
|
return ((a.vertex_index
|
|
^ a.texcoord_index << 1) >> 1)
|
|
^ (a.normal_index << 1);
|
|
}
|
|
};
|
|
|
|
//inline float ShininessToRoughness(float Ypoint)
|
|
//{
|
|
// float a = -1;
|
|
// float b = 2;
|
|
|
|
// float c;
|
|
// c = (Ypoint / 100) - 1;
|
|
|
|
// float D;
|
|
// D = b * b - (4 * a * c);
|
|
|
|
// float x1;
|
|
// x1 = (-b + sqrt(D)) / (2 * a);
|
|
|
|
// return x1;
|
|
//}
|
|
|
|
class ModelLoader
|
|
{
|
|
public:
|
|
std::vector<ModelObject*> Load(std::string_view path_name, std::string_view res_name)
|
|
{
|
|
std::string ext = File::GetExtension(path_name);
|
|
if (ext == "obj")
|
|
return _load_obj(path_name);
|
|
else if (ext == "glb")
|
|
return _load_gltf(path_name, res_name, true);
|
|
else if (ext == "gltf")
|
|
return _load_gltf(path_name, res_name, false);
|
|
log_err("不支持的模型格式:{}", path_name);
|
|
return {};
|
|
}
|
|
private:
|
|
std::vector<ModelObject*> _load_obj(std::string_view path_name);
|
|
std::vector<ModelObject*> _load_gltf(
|
|
std::string_view path_name, std::string_view res_name, bool is_glb);
|
|
|
|
ModelObject* _create_model_object(ModelData& data);
|
|
|
|
struct LoaderInfo
|
|
{
|
|
uint32_t* _indexBuffer;
|
|
Vertex* _vertexBuffer;
|
|
VertexBone* _vertexBufferSkin;
|
|
size_t _indexPos = 0;
|
|
size_t _vertexPos = 0;
|
|
};
|
|
// 读取材质
|
|
void _gltf_load_material(const tinygltf::Model& model, std::string_view res_name,
|
|
std::vector<ModelMaterial>& vec_material);
|
|
// 读取场景
|
|
void _gltf_load_scene(const tinygltf::Model& model, ModelData& data);
|
|
|
|
// 统计顶点和索引
|
|
void _gltf_get_node_prop(const tinygltf::Model& model, const tinygltf::Node& node,
|
|
size_t& vertex_count, size_t& index_count);
|
|
|
|
// 读取节点
|
|
void _gltf_load_node(const tinygltf::Model& model, int node,
|
|
ModelNode* model_node_parent, ModelData& data);
|
|
// 读取蒙皮
|
|
void _gltf_load_skin(const tinygltf::Model& model, ModelData& data);
|
|
// 读取动画
|
|
void _gltf_load_animation(const tinygltf::Model& model, ModelData& data);
|
|
|
|
ModelNode* _find_node(ModelData& data, uint32_t index);
|
|
Matrix4 _get_node_mat_world(const ModelNode* node);
|
|
|
|
Position3 _vec3_cast(const std::vector<double>& vec3);
|
|
Position4 _vec4_cast(const std::vector<double>& vec4);
|
|
Position4 _quat_cast(const std::vector<double>& vec4);
|
|
Position2 _vec2_cast(const float* p);
|
|
Position3 _vec3_cast(const float* p);
|
|
Position4 _vec4_cast(const float* p);
|
|
|
|
Matrix4 _mat4_cast(const std::vector<double>& matrix);
|
|
Matrix4 _mat4_cast(float* p);
|
|
|
|
};
|
|
|
|
extern ModelLoader* g_modelLoader;
|
|
|
|
}
|
|
#endif |