dl
dl_model_object.h
浏览该文件的文档.
1
13#pragma once
14
15#ifndef DL_DISABLE_RENDER
16#include <string>
17#include <unordered_map>
18
19#include "io/dl_file.h"
20#include "vk/dl_vertex.h"
25
26namespace dl
27{
28
29//模型对象 材质
31{
32 //最后转为Model时,变为可以用的着色器资源
34 std::string _materialName;
35 //路径为空,则表示没有(VK加载时会返回0)
36 std::string _pathTexDiffuse;
37 std::string _pathTexNormal;
38
40 {
41 s.Read(&_material);
45 }
46
48 {
49 s.Write(&_material);
53 }
54};
55//子模型对象 范围
57{
59 size_t _indexMaterial;//材质下标,-1代表没有
60};
61
62//一个文件会有多个ModelObject,一个ModelObject根据材质分为多个ModelSub
63//注意ModelSub为一个材质,需要读取时合并网格
65{
66 friend class VK;
67 friend class ModelLoader;
68public:
69 //从源文件加载模型
70 static std::vector<ModelObject*> Create(std::string_view path_name, std::string_view res_name);
71
72 void Load(std::string_view path_name)
73 {
75 if (!s.LoadFromFile(path_name, true))
76 {
77 log_warn("文件加载失败:{}", path_name );
78 return;
79 }
80
81 s.Read(&_allVertex);
82 s.Read(&_allVertexBone);
83 s.Read(&_allIndex);
84 s.Read(&_allSub);
85
86 size_t size;
87 s.Read64(&size);
88 _allMaterial.resize(size);
89 for (size_t i = 0; i < size; ++i)
90 {
91 _allMaterial[i].Load(s);
92 }
93
94 bool has_bone;
95 s.Read(&has_bone);
96 if (has_bone)
97 {
98 _boneData = new BoneData;
99 _boneData->Load(s);
100 }
101 else
102 _boneData = nullptr;
103 }
104
105 //保存到文件
106 void SaveToFile(std::string_view path_name)
107 {
108 StreamOutput s;
109
110 s.Write(&_allVertex);
111 s.Write(&_allVertexBone);
112 s.Write(&_allIndex);
113 s.Write(&_allSub);
114
115 size_t size = _allMaterial.size();
116 s.Write64(&size);
117 for (size_t i = 0; i < size; ++i)
118 {
119 _allMaterial[i].Save(s);
120 }
121
122 bool has_bone = _boneData;
123 s.Write(&has_bone);
124 if (has_bone)
125 {
126 _boneData->Save(s);
127 }
128
129 s.Save(path_name, true);
130 }
131
133 {
134 for (auto& iter : _allMaterial)
135 {
136 if (!iter._pathTexDiffuse.empty()
137 || !iter._pathTexNormal.empty())
138 return true;
139 }
140 return false;
141 }
142
143 //转换贴图路径
144 void ConvertTexturePath(std::string_view path)
145 {
146 //避免重复导出同一张图像
147 std::unordered_map<std::string, std::string> map_out;
148
149 auto fn_output = [&map_out, path](std::string& path_name, size_t i, std::string_view tag)
150 {
151 if (path_name.empty())
152 return;
153 auto iter = map_out.find(path_name);
154 if (iter == map_out.end())
155 {
156 std::string fmt = File::GetExtension(path_name);
157 std::string name = fmt::format("{}{}{}.{}", path, tag, i, fmt);
158 File::Copy(path_name, name);
159 map_out[path_name] = name;
160 //返回path_name前,先记录到表
161 path_name = name;
162 }
163 else
164 path_name = iter->second;
165 };
166
167 size_t i = 0;
168 for (auto& iter : _allMaterial)
169 {
170 fn_output(iter._pathTexDiffuse, i, "diffuse#");
171 fn_output(iter._pathTexNormal, i, "normal#");
172 ++i;
173 }
174 }
176 _boneData{ nullptr }
177 {}
178private:
179 std::vector<ModelObjectSub> _allSub;
180
181 std::vector<Vertex> _allVertex;//顶点缓存
182 std::vector<VertexBone> _allVertexBone;//顶点缓存(有骨骼动画时使用)
183 std::vector<uint32_t> _allIndex;//索引缓存
184
185 //所有材质(对于assimp来说,似乎总会至少有一个DefaultMaterial,但我们不使用它)
186 std::vector<ModelObjectMaterial> _allMaterial;
187
188 //所有骨骼(转移给Model,不用删除)
189 BoneData* _boneData;//骨骼数据
190};
191
192}
193
194#endif
骨骼数据(多个骨骼)
void Save(StreamOutput &s)
void Load(StreamInput &s)
void Load(std::string_view path_name)
void ConvertTexturePath(std::string_view path)
static std::vector< ModelObject * > Create(std::string_view path_name, std::string_view res_name)
void SaveToFile(std::string_view path_name)
注释参考StreamOutput
void Read(T *data)
void Read64(T *data)
写入一个类型,写为64位,用于在64位平台的大小不一致,所以特殊处理
bool LoadFromFile(std::string_view path_name, bool decompress=false, size_t length=0)
从文件读到内存,可选长度
bool Save(std::string_view path_name, bool compress=false)
保存到文件
void Write64(const T *data)
写入一个类型以64位,对于在64位平台的大小不一致,所以特殊处理
void Write(const T *data)
写入一个对象
Definition: dl_vk.h:137
文件系统
#define log_warn(...)
Definition: dl_log.h:242
模型之骨骼动画
bool Copy(std::string_view source, std::string_view target, CopyMode mode=CopyMode::NONE, bool recursion=false)
复制文件或文件夹
std::string GetExtension(std::string_view path_name, bool has_dot=false)
返回文件后缀
Definition: dl_array2.h:18
材质
Definition: dl_vertex.h:111
void Save(StreamOutput &s)
void Load(StreamInput &s)
ModelTriangleRange _range