FCT
载入中...
搜索中...
未找到
ModelLoader.h
浏览该文件的文档.
1#ifndef MODELLOADER_H
2#define MODELLOADER_H
3#include "../ThirdParty.h"
4#include "../Context/Vertex.h"
5namespace FCT
6{
7 namespace ModelInfo
8 {
38
39
40 struct MeshInfo
41 {
42 std::string name;
43 uint32_t vertexCount;
44 uint32_t indexCount;
45 uint32_t triangleCount;
47
52
53 std::array<bool, 8> hasTexCoords;
54 std::array<bool, 8> hasVertexColors;
55
59
61 isIndexed(false), hasPositions(false), hasNormals(false),
62 hasTangents(false), hasBitangents(false),
64 {
65 hasTexCoords.fill(false);
66 hasVertexColors.fill(false);
67 }
68
69 private:
71 template<class Archive>
72 void serialize(Archive & ar, const unsigned int version)
73 {
74 ar & name;
75 ar & vertexCount;
76 ar & indexCount;
77 ar & triangleCount;
78 ar & isIndexed;
79 ar & hasPositions;
80 ar & hasNormals;
81 ar & hasTangents;
82 ar & hasBitangents;
83 ar & hasTexCoords;
84 ar & hasVertexColors;
85 ar & boundingBoxMin;
86 ar & boundingBoxMax;
88 }
89 };
90
92 {
93 private:
95 template<class Archive>
96 void serialize(Archive & ar, const unsigned int version)
97 {
98 // 当MaterialInfo有成员时在这里添加序列化
99 }
100 };
101
103 {
104 std::string path;
106 std::set<TextureType> types;
107 TextureInfo() : isInner(false) {}
108 private:
110 template<class Archive>
111 void serialize(Archive & ar, const unsigned int version)
112 {
113 ar & path;
114 ar & isInner;
115 if (version >= 3) {
116 ar & types;
117 }
118 }
119 };
120
122 {
123 std::string name;
124 std::vector<MeshInfo> meshInfos;
125 std::vector<MaterialInfo> materialInfos;
126 std::vector<TextureInfo> textureInfos;
127
128 private:
130 template<class Archive>
131 void serialize(Archive & ar, const unsigned int version)
132 {
133 ar & name;
134 ar & meshInfos;
135 ar & materialInfos;
136 ar & textureInfos;
137 }
138 };
139 }
140}
141
146
147namespace boost {
148namespace serialization {
149 template<class Archive>
150 void serialize(Archive & ar, FCT::ModelInfo::TextureType & t, const unsigned int version)
151 {
152 ar & static_cast<int&>(t);
153 }
154}
155}
156namespace FCT
157{
159 {
160 ModelVertex() : position(0.0f, 0.0f, 0.0f),
161 normal(0.0f, 0.0f, 1.0f),
162 tangent(1.0f, 0.0f, 0.0f),
163 bitangent(0.0f, 1.0f, 0.0f)
164 {
165 for (int i = 0; i < 8; i++) {
166 texCoords[i] = Vec2(0.0f, 0.0f);
167 }
168
169 for (int i = 0; i < 8; i++) {
170 colors[i] = Vec4(1.0f, 1.0f, 1.0f, 1.0f);
171 }
172 }
179 };
181 {
182
183 };
185 {
187 std::vector<ModelVertex> vertices;
188 std::vector<uint32_t> indices;
189 std::string name;
190 };
192 {
193 std::vector<ModelObject*> children;
195 };
197 {
198 std::vector<ModelMaterial*> materials;
199 std::vector<ModelMesh*> meshes;
200 std::vector<ModelObject*> objects;
201 ModelMesh* findMesh(const std::string &name)
202 {
203 auto it = std::find_if(meshes.begin(), meshes.end(), [&name](const ModelMesh* mesh)
204 {
205 return mesh->name == name;
206 });
207
208 return (it != meshes.end()) ? *it : nullptr;
209 }
210 };
211
212 inline void setVertexAttributeFromModel(Vertex& vertex, size_t elementIndex,
213 const VertexElement& element,
214 const ModelVertex& modelVertex) {
215 ModelVertexAttribute modelAttr = element.getModelAttribute();
216 Format format = element.getFormat();
217
218 if (modelAttr == ModelVertexAttribute::Position) {
219 switch (format) {
221 vertex.setAttribute(elementIndex, Vec2(modelVertex.position.x, modelVertex.position.y));
222 break;
224 vertex.setAttribute(elementIndex, Vec3(modelVertex.position.x, modelVertex.position.y, modelVertex.position.z));
225 break;
227 vertex.setAttribute(elementIndex, Vec4(modelVertex.position.x, modelVertex.position.y, modelVertex.position.z, 1.0f));
228 break;
229 default:
230 break;
231 }
232 return;
233 }
234
235 if (modelAttr == ModelVertexAttribute::Normal) {
236 switch (format) {
238 vertex.setAttribute(elementIndex, Vec3(modelVertex.normal.x, modelVertex.normal.y, modelVertex.normal.z));
239 break;
240 default:
241 break;
242 }
243 return;
244 }
245
246 if (modelAttr == ModelVertexAttribute::Tangent) {
247 switch (format) {
249 vertex.setAttribute(elementIndex, Vec3(modelVertex.tangent.x, modelVertex.tangent.y, modelVertex.tangent.z));
250 break;
251 default:
252 break;
253 }
254 return;
255 }
256
257 if (modelAttr == ModelVertexAttribute::Bitangent) {
258 switch (format) {
260 vertex.setAttribute(elementIndex, Vec3(modelVertex.bitangent.x, modelVertex.bitangent.y, modelVertex.bitangent.z));
261 break;
262 default:
263 break;
264 }
265 return;
266 }
267
268 if (modelAttr >= ModelVertexAttribute::TexCoord0 && modelAttr <= ModelVertexAttribute::TexCoord7) {
269 size_t texCoordIndex = static_cast<size_t>(modelAttr) - static_cast<size_t>(ModelVertexAttribute::TexCoord0);
270 if (texCoordIndex < 8) {
271 switch (format) {
273 vertex.setAttribute(elementIndex, Vec2(modelVertex.texCoords[texCoordIndex].x, modelVertex.texCoords[texCoordIndex].y));
274 break;
275 default:
276 break;
277 }
278 }
279 return;
280 }
281
282 if (modelAttr >= ModelVertexAttribute::Color0 && modelAttr <= ModelVertexAttribute::Color7) {
283 size_t colorIndex = static_cast<size_t>(modelAttr) - static_cast<size_t>(ModelVertexAttribute::Color0);
284 if (colorIndex < 8) {
285 switch (format) {
287 vertex.setAttribute(elementIndex, Vec3(modelVertex.colors[colorIndex].x, modelVertex.colors[colorIndex].y, modelVertex.colors[colorIndex].z));
288 break;
290 vertex.setAttribute(elementIndex, Vec4(modelVertex.colors[colorIndex].x, modelVertex.colors[colorIndex].y, modelVertex.colors[colorIndex].z, modelVertex.colors[colorIndex].w));
291 break;
293 {
294 Vector4<uint8_t> colorData(
295 static_cast<uint8_t>(modelVertex.colors[colorIndex].x * 255.0f),
296 static_cast<uint8_t>(modelVertex.colors[colorIndex].y * 255.0f),
297 static_cast<uint8_t>(modelVertex.colors[colorIndex].z * 255.0f),
298 static_cast<uint8_t>(modelVertex.colors[colorIndex].w * 255.0f)
299 );
300 vertex.setAttribute(elementIndex, colorData);
301 }
302 break;
304 {
305 Vector4<uint8_t> colorData(
306 static_cast<uint8_t>(modelVertex.colors[colorIndex].z * 255.0f),
307 static_cast<uint8_t>(modelVertex.colors[colorIndex].y * 255.0f),
308 static_cast<uint8_t>(modelVertex.colors[colorIndex].x * 255.0f),
309 static_cast<uint8_t>(modelVertex.colors[colorIndex].w * 255.0f)
310 );
311 vertex.setAttribute(elementIndex, colorData);
312 }
313 break;
314 default:
315 break;
316 }
317 }
318 return;
319 }
320 }
321
322 // 转换时,用一个map存ModelMaterial
323 // 转换时,用一个map存ModelMesh
324
326 public:
327 virtual std::unique_ptr<ModelData> loadModel(const std::string &path) = 0;
328 virtual ModelInfo::SceneInfo loadModelInfo(const std::string &path) = 0;
336 virtual std::set<std::string> resolveTexturePaths(const std::string& modelPath) const = 0;
337
346 virtual bool getEmbeddedTextureData(const std::string& modelPath, int textureIndex,
347 std::vector<unsigned char>& outData) const = 0;
348
356 std::set<std::string> resolveModePaths(const std::string& modelPath);
357 std::set<std::string> getSupportedExtensions() const;
358 protected:
359 std::set<std::string> getModelSpecificDependencies(const std::string& modelPath) const;
360 virtual std::set<std::string> getPlatformSupportedExtensions() const = 0;;
361 };
362
363}
364#endif //MODELLOADER_H
BOOST_CLASS_VERSION(FCT::ModelInfo::MeshInfo, 1)
virtual bool getEmbeddedTextureData(const std::string &modelPath, int textureIndex, std::vector< unsigned char > &outData) const =0
获取内嵌纹理数据
virtual std::set< std::string > resolveTexturePaths(const std::string &modelPath) const =0
解析模型依赖的纹理位置
std::set< std::string > getModelSpecificDependencies(const std::string &modelPath) const
virtual std::unique_ptr< ModelData > loadModel(const std::string &path)=0
std::set< std::string > getSupportedExtensions() const
virtual std::set< std::string > getPlatformSupportedExtensions() const =0
virtual ModelInfo::SceneInfo loadModelInfo(const std::string &path)=0
std::set< std::string > resolveModePaths(const std::string &modelPath)
解析模型路径,获取模型文件的所有依赖路径
constexpr Format getFormat() const noexcept
constexpr ModelVertexAttribute getModelAttribute() const noexcept
void setAttribute(VtxType type, const T &value) noexcept
TextureType
纹理类型枚举
ModelVertexAttribute
void setVertexAttributeFromModel(Vertex &vertex, size_t elementIndex, const VertexElement &element, const ModelVertex &modelVertex)
void serialize(Archive &ar, FCT::ModelInfo::TextureType &t, const unsigned int version)
ModelMesh * findMesh(const std::string &name)
std::vector< ModelObject * > objects
std::vector< ModelMaterial * > materials
std::vector< ModelMesh * > meshes
void serialize(Archive &ar, const unsigned int version)
friend class boost::serialization::access
void serialize(Archive &ar, const unsigned int version)
std::array< bool, 8 > hasTexCoords
friend class boost::serialization::access
std::array< bool, 8 > hasVertexColors
void serialize(Archive &ar, const unsigned int version)
std::vector< TextureInfo > textureInfos
std::vector< MaterialInfo > materialInfos
std::vector< MeshInfo > meshInfos
friend class boost::serialization::access
void serialize(Archive &ar, const unsigned int version)
friend class boost::serialization::access
std::set< TextureType > types
std::vector< uint32_t > indices
ModelMaterial * material
std::vector< ModelVertex > vertices
std::vector< ModelObject * > children
float x
定义 Vec.h:11
float y
定义 Vec.h:11
float x
定义 Vec.h:89
float z
定义 Vec.h:89
float y
定义 Vec.h:89
float z
定义 Vec.h:212
float y
定义 Vec.h:212
float w
定义 Vec.h:212
float x
定义 Vec.h:212