Skylicht Engine
Loading...
Searching...
No Matches
CColladaLoader.h
1/*
2!@
3MIT License
4
5Copyright (c) 2019 Skylicht Technology CO., LTD
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
8(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
9merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
10subject to the following conditions:
11
12The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19
20This file is part of the "Skylicht Engine".
21https://github.com/skylicht-lab/skylicht-engine
22!#
23*/
24
25#pragma once
26
27#include "pch.h"
28#include "RenderMesh/CMesh.h"
29#include "RenderMesh/CSkinnedMesh.h"
30
31#include "Importer/CBaseMeshImporter.h"
32#include "RenderMesh/CJointData.h"
33
34namespace Skylicht
35{
36 class CEntity;
37}
38
39namespace Collada
40{
41 // const for buffer
42 const int k_positionBuffer = 0;
43 const int k_normalBuffer = 1;
44 const int k_texCoordBuffer = 2;
45
46 // const for mesh
47 const int k_mesh = 0;
48 const int k_skinMesh = 1;
49
51 {
52 std::wstring id;
53 std::wstring name;
54 std::wstring fileName;
55 };
56 typedef std::vector<SColladaImage> ArrayImages;
57
59 {
60 std::wstring Name;
61 std::wstring Source;
62 std::wstring InitFromTexture;
63 };
64 typedef std::vector<SEffectParam> ArrayEffectParams;
65
66 struct SEffect
67 {
69 std::wstring Id;
70 f32 Transparency;
71 bool HasAlpha;
72 bool TransparentAddColor;
73
74 bool HasDiffuseMapping;
75 bool HasBumpMapping;
76 bool HasSpecularMapping;
77
78 std::wstring DiffuseRef;
79 std::wstring SpecularRef;
80 std::wstring BumpRef;
81
82 ArrayEffectParams EffectParams;
83
84 inline bool operator< (const SEffect& other) const
85 {
86 return Id < other.Id;
87 }
88 };
89 typedef std::vector<SEffect> ArrayEffects;
90
92 {
93 std::wstring ID;
94 std::wstring Name;
95 std::wstring InstanceEffect;
96 SEffect Effect;
97 };
98 typedef std::vector<SColladaMaterial> ArrayMaterials;
99
100 struct SBufferParam
101 {
102 std::wstring Name;
103 float* FloatArray;
104 int ArrayCount;
105 int Type;
106 int Strike;
107
108 SBufferParam()
109 {
110 FloatArray = NULL;
111 }
112 };
113
114 struct SVerticesParam
115 {
116 std::wstring Name;
117 int PositionIndex;
118 int NormalIndex;
119 int TexCoord1Index;
120 int TexCoord2Index;
121
122 SVerticesParam()
123 {
124 PositionIndex = -1;
125 NormalIndex = -1;
126 TexCoord1Index = -1;
127 TexCoord2Index = -1;
128 }
129 };
130
131 struct STrianglesParam
132 {
133 int VerticesIndex;
134 int EffectIndex;
135 std::wstring Material;
136 int NumPolygon;
137 s32* VCount;
138 s32* IndexBuffer;
139 int NumElementPerVertex;
140 int OffsetVertex;
141 int OffsetNormal;
142 int OffsetTexcoord1;
143 int OffsetTexcoord2;
144
145 STrianglesParam()
146 {
147 VerticesIndex = -1;
148 EffectIndex = -1;
149 NumPolygon = 0;
150 IndexBuffer = NULL;
151 VCount = NULL;
152 NumElementPerVertex = 1;
153 OffsetVertex = 0;
154 OffsetNormal = 0;
155 OffsetTexcoord1 = 0;
156 OffsetTexcoord2 = 0;
157 }
158 };
159
160
162 {
163 u32 VertexID;
164 f32 Strength;
165 };
166
168 {
169 std::wstring Name;
170 core::matrix4 InvMatrix;
171 std::vector<SWeightParam> Weights;
172 };
173
175 {
176 std::wstring Name;
177 std::wstring ControllerName;
178 std::wstring ListJointName;
179
180 std::vector<SBufferParam> Buffers;
181 std::vector<SVerticesParam> Vertices;
182 std::vector<STrianglesParam> Triangles;
183 std::vector<SJointParam> Joints;
184
185 std::vector<s32> JointVertexIndex;
186 std::vector<s32> JointIndex;
187
188 int Type;
189 core::matrix4 BindShapeMatrix;
190 };
191 typedef std::vector<SMeshParam> ArrayMeshParams;
192
193 struct SNodeParam
194 {
195 std::wstring Name;
196 std::wstring Type;
197 std::wstring SID;
198 std::wstring Instance;
199 std::map<std::wstring, std::wstring> BindMaterial;
200
201 core::matrix4 Transform;
202
203 std::vector<SNodeParam*> Childs;
204 SNodeParam* Parent;
205
206 Skylicht::CEntity* Entity;
207 int ChildLevel;
208
209 SNodeParam()
210 {
211 Entity = NULL;
212 ChildLevel = 0;
213 }
214 };
215 typedef std::vector<SNodeParam*> ArrayNodeParams;
216
218 {
219 SMeshParam* meshId;
220 s32 bufferId;
221 s32 vertexId;
222
223 bool operator<(const SColladaMeshVertexMap& another) const
224 {
225 if (meshId == another.meshId)
226 {
227 if (bufferId == another.bufferId)
228 {
229 if (vertexId == another.vertexId)
230 return false;
231 else
232 return vertexId < another.vertexId;
233 }
234 else
235 return bufferId < another.bufferId;
236 }
237 else
238 {
239 return meshId < another.meshId;
240 }
241 }
242 };
243
245 {
246 s32 vertexId;
247 s32 normalId;
248 s32 texcoordId1;
249 s32 texcoordId2;
250
251 bool operator<(const SColladaVertexIndex& another) const
252 {
253 if (vertexId == another.vertexId)
254 {
255 if (normalId == another.normalId)
256 {
257 if (texcoordId1 == another.texcoordId1)
258 {
259 if (texcoordId2 == another.texcoordId2)
260 return false;
261 else
262 return texcoordId2 < another.texcoordId2;
263 }
264 else
265 return texcoordId1 < another.texcoordId1;
266 }
267 else
268 return normalId < another.normalId;
269 }
270 else
271 {
272 return vertexId < another.vertexId;
273 }
274 }
275 };
276}
277
278using namespace Collada;
279
280namespace Skylicht
281{
282 class SKYLICHT_API CColladaLoader : public CBaseMeshImporter
283 {
284 public:
285 static bool s_fixUV;
286
287 protected:
288
289 std::string m_meshFile;
290 std::string m_meshName;
291
292 std::string m_unit;
293 float m_unitScale;
294 core::matrix4 m_unitScaleMatrix;
295
296 protected:
297
298 bool m_zUp;
299 bool m_flipOx;
300
301 bool m_loadTexcoord2;
302 bool m_createTangent;
303 bool m_flipNormalMap;
304 bool m_createBatchMesh;
305
306 bool m_loadNormalMap;
307
308 float m_maxUVTile;
309
310 std::map<std::string, CJointData*> m_nameToJointData;
311 std::map<std::string, CJointData*> m_sidToJointData;
312
313 protected:
314
315 ArrayImages m_listImages;
316 ArrayEffects m_listEffects;
317 ArrayMaterials m_listMaterial;
318 ArrayMeshParams m_listMesh;
319 ArrayNodeParams m_listNode;
320
321 SNodeParam* m_colladaRoot;
322
323 // Add to support read DAE from FBX Converter
324 typedef std::vector<s32> ArrayShort;
325 std::map<SColladaMeshVertexMap, ArrayShort> m_meshVertexIndex;
326
327 public:
328 CColladaLoader();
329
330 virtual ~CColladaLoader();
331
332 void setLoadTexCoord2(bool b)
333 {
334 m_loadTexcoord2 = b;
335 }
336
337 void setCreateBatchMesh(bool b)
338 {
339 m_createBatchMesh = b;
340 }
341
342 void setLoadNormalMap(bool b, bool flipNormalMap)
343 {
344 m_loadNormalMap = b;
345 m_createTangent = b;
346 m_flipNormalMap = flipNormalMap;
347 }
348
349 void setTextureFolder(std::vector<std::string>& folder)
350 {
351 m_textureFolder = folder;
352 }
353
354 virtual bool loadModel(const char* resource, CEntityPrefab* output, bool normalMap = true, bool flipNormalMap = true, bool texcoord2 = true, bool batching = false);
355
356 protected:
357
358 bool loadDae(const char* fileName, CEntityPrefab* output);
359
360 void parseImageNode(io::IXMLReader* xmlRead, SColladaImage* image = NULL);
361
362 void parseMaterialNode(io::IXMLReader* xmlRead, SColladaMaterial* material = NULL);
363
364 void parseEffectNode(io::IXMLReader* xmlRead, SEffect* effect = NULL);
365
366 void parseGeometryNode(io::IXMLReader* xmlRead);
367
368 void parseControllerNode(io::IXMLReader* xmlRead);
369
370 void parseSceneNode(io::IXMLReader* xmlRead);
371
372 void parseUnit(io::IXMLReader* xmlRead);
373
374 protected:
375
376 SMeshParam* parseSkinNode(io::IXMLReader* xmlRead);
377
378 SNodeParam* parseNode(io::IXMLReader* xmlRead, SNodeParam* parent);
379
380 void updateEffectMaterial(SEffect* effect);
381
382 void updateJointToMesh(SMeshParam* mesh, int numJoints, float* arrayWeight, float* arrayTransform, std::vector<s32>& vCountArray, std::vector<s32>& vArray);
383
384 protected:
385
386 CJointData* findJointData(const char* name);
387
388 void loadEffectTexture();
389
390 ITexture* getTextureResource(std::wstring& refName, ArrayEffectParams& params);
391
392 void constructEntityPrefab(CEntityPrefab* output);
393
394 CMesh* constructMesh(SMeshParam* mesh, SNodeParam* node);
395
396 void constructSkinMesh(std::list<SNodeParam*>& nodes);
397
398 void constructSkinMesh(SMeshParam* meshParam, CSkinnedMesh* mesh);
399
400 IMeshBuffer* constructMeshBuffer(SMeshParam* mesh, STrianglesParam* tri, int bufferID, bool& needFixUVTile);
401
402 void convertToLightMapVertices(IMeshBuffer* buffer, SMeshParam* mesh, STrianglesParam* tri);
403
404 void cleanData();
405 };
406
407}
This is the object class that describes an entity.
Definition CEntity.h:58
This object class is created to store data in an array of multiple CEntities.
Definition CEntityPrefab.h:38
Definition CJointData.h:32
Definition CMesh.h:75
Definition CSkinnedMesh.h:36
Struct for holding a mesh with a single material.
Definition IMeshBuffer.h:42
Interface of a Video Driver dependent Texture.
Definition ITexture.h:119
Struct for holding parameters for a material renderer.
Definition SMaterial.h:255
Everything in the Skylicht Engine. You can start by looking at the topics.
Definition AudioDebugLog.h:29
CMatrix4< f32 > matrix4
Typedef for f32 matrix.
Definition matrix4.h:2241
IIrrXMLReader< wchar_t, IReferenceCounted > IXMLReader
An xml reader for wide characters, derived from IReferenceCounted.
Definition IXMLReader.h:19
float f32
32 bit floating point variable.
Definition irrTypes.h:104
unsigned int u32
32 bit unsigned variable.
Definition irrTypes.h:58
signed int s32
32 bit signed variable.
Definition irrTypes.h:66
Definition CColladaLoader.h:51
Definition CColladaLoader.h:92
Definition CColladaLoader.h:218
Definition CColladaLoader.h:245
Definition CColladaLoader.h:67
Definition CColladaLoader.h:59
Definition CColladaLoader.h:168
Definition CColladaLoader.h:175
Definition CColladaLoader.h:194
Definition CColladaLoader.h:132
Definition CColladaLoader.h:162