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
62 _indexMaterial{(size_t)-1}
63 {
64 }
65};
66
67//一个文件会有多个ModelObject,一个ModelObject根据材质分为多个ModelSub
68//注意ModelSub为一个材质,需要读取时合并网格
70{
71 friend class VK;
72 friend class ModelLoader;
73public:
74 //从源文件加载模型
75 static std::vector<ModelObject*> Create(std::string_view path_name, std::string_view res_name);
76
77 void Load(std::string_view path_name)
78 {
79 /*StreamInput s;
80 if (!s.LoadFromFile(path_name, true))
81 {
82 log_warn("文件加载失败:{}", path_name );
83 return;
84 }
85
86 s.Read(&_allVertex);
87 s.Read(&_allVertexBone);
88 s.Read(&_allIndex);
89 s.Read(&_allSub);
90
91 size_t size;
92 s.Read64(&size);
93 _allMaterial.resize(size);
94 for (size_t i = 0; i < size; ++i)
95 {
96 _allMaterial[i].Load(s);
97 }
98
99 bool has_bone;
100 s.Read(&has_bone);
101 if (has_bone)
102 {
103 _boneData = new BoneData;
104 _boneData->Load(s);
105 }
106 else
107 _boneData = nullptr;*/
108 }
109
110 //保存到文件
111 void SaveToFile(std::string_view path_name)
112 {
113 /*StreamOutput s;
114
115 s.Write(&_allVertex);
116 s.Write(&_allVertexBone);
117 s.Write(&_allIndex);
118 s.Write(&_allSub);
119
120 size_t size = _allMaterial.size();
121 s.Write64(&size);
122 for (size_t i = 0; i < size; ++i)
123 {
124 _allMaterial[i].Save(s);
125 }
126
127 bool has_bone = _boneData;
128 s.Write(&has_bone);
129 if (has_bone)
130 {
131 _boneData->Save(s);
132 }
133
134 s.Save(path_name, true);*/
135 }
136
138 {
139 for (auto& iter : _allMaterial)
140 {
141 if (!iter._pathTexDiffuse.empty()
142 || !iter._pathTexNormal.empty())
143 return true;
144 }
145 return false;
146 }
147
148 //转换贴图路径
149 void ConvertTexturePath(std::string_view path)
150 {
151 //避免重复导出同一张图像
152 std::unordered_map<std::string, std::string> map_out;
153
154 auto fn_output = [&map_out, path](std::string& path_name, size_t i, std::string_view tag)
155 {
156 if (path_name.empty())
157 return;
158 auto iter = map_out.find(path_name);
159 if (iter == map_out.end())
160 {
161 std::string fmt = File::GetExtension(path_name);
162 std::string name = fmt::format("{}{}{}.{}", path, tag, i, fmt);
163 File::Copy(path_name, name, File::CopyMode::OVERRIDE);
164 map_out[path_name] = name;
165 //返回path_name前,先记录到表
166 path_name = name;
167 }
168 else
169 path_name = iter->second;
170 };
171
172 size_t i = 0;
173 for (auto& iter : _allMaterial)
174 {
175 fn_output(iter._pathTexDiffuse, i, "diffuse#");
176 fn_output(iter._pathTexNormal, i, "normal#");
177 ++i;
178 }
179 }
181 _modelData{ nullptr }
182 {}
183private:
184 std::vector<ModelObjectSub> _allSub;
185
186 std::vector<Vertex> _allVertex;//顶点缓存
187 std::vector<VertexBone> _allVertexBone;//顶点缓存(有骨骼动画时使用)
188 std::vector<uint32_t> _allIndex;//索引缓存
189
190 // 所有材质
191 std::vector<ModelObjectMaterial> _allMaterial;
192
193 ModelData* _modelData;
194};
195
196}
197
198#endif
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)
二进制输入流
二进制输出流
void Write(const T *data)
写入一个对象
文件系统
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)
返回文件后缀
void Save(StreamOutput &s)
ModelTriangleRange _range