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