vrshoot

view libs/assimp/ASEParser.h @ 2:334d17aed7de

visual studio project files
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 02 Feb 2014 18:36:38 +0200
parents
children
line source
1 /*
2 Open Asset Import Library (assimp)
3 ----------------------------------------------------------------------
5 Copyright (c) 2006-2012, assimp team
6 All rights reserved.
8 Redistribution and use of this software in source and binary forms,
9 with or without modification, are permitted provided that the
10 following conditions are met:
12 * Redistributions of source code must retain the above
13 copyright notice, this list of conditions and the
14 following disclaimer.
16 * Redistributions in binary form must reproduce the above
17 copyright notice, this list of conditions and the
18 following disclaimer in the documentation and/or other
19 materials provided with the distribution.
21 * Neither the name of the assimp team, nor the names of its
22 contributors may be used to endorse or promote products
23 derived from this software without specific prior
24 written permission of the assimp team.
26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 ----------------------------------------------------------------------
39 */
42 /** @file Defines the helper data structures for importing ASE files */
43 #ifndef AI_ASEFILEHELPER_H_INC
44 #define AI_ASEFILEHELPER_H_INC
46 // STL/CRT headers
47 #include <string>
48 #include <vector>
49 #include <list>
51 // public ASSIMP headers
52 #include "assimp/types.h"
53 #include "assimp/mesh.h"
54 #include "assimp/anim.h"
56 // for some helper routines like IsSpace()
57 #include "ParsingUtils.h"
58 #include "qnan.h"
60 // ASE is quite similar to 3ds. We can reuse some structures
61 #include "3DSLoader.h"
63 namespace Assimp {
64 namespace ASE {
66 using namespace D3DS;
68 // ---------------------------------------------------------------------------
69 /** Helper structure representing an ASE material */
70 struct Material : public D3DS::Material
71 {
72 //! Default constructor
73 Material() : pcInstance(NULL), bNeed (false)
74 {}
76 //! Contains all sub materials of this material
77 std::vector<Material> avSubMaterials;
79 //! aiMaterial object
80 aiMaterial* pcInstance;
82 //! Can we remove this material?
83 bool bNeed;
84 };
86 // ---------------------------------------------------------------------------
87 /** Helper structure to represent an ASE file face */
88 struct Face : public FaceWithSmoothingGroup
89 {
90 //! Default constructor. Initializes everything with 0
91 Face()
92 {
93 mColorIndices[0] = mColorIndices[1] = mColorIndices[2] = 0;
94 for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS;++i)
95 {
96 amUVIndices[i][0] = amUVIndices[i][1] = amUVIndices[i][2] = 0;
97 }
99 iMaterial = DEFAULT_MATINDEX;
100 iFace = 0;
101 }
103 //! special value to indicate that no material index has
104 //! been assigned to a face. The default material index
105 //! will replace this value later.
106 static const unsigned int DEFAULT_MATINDEX = 0xFFFFFFFF;
110 //! Indices into each list of texture coordinates
111 unsigned int amUVIndices[AI_MAX_NUMBER_OF_TEXTURECOORDS][3];
113 //! Index into the list of vertex colors
114 unsigned int mColorIndices[3];
116 //! (Sub)Material index to be assigned to this face
117 unsigned int iMaterial;
119 //! Index of the face. It is not specified whether it is
120 //! a requirement of the file format that all faces are
121 //! written in sequential order, so we have to expect this case
122 unsigned int iFace;
123 };
125 // ---------------------------------------------------------------------------
126 /** Helper structure to represent an ASE file bone */
127 struct Bone
128 {
129 //! Constructor
130 Bone()
131 {
132 static int iCnt = 0;
134 // Generate a default name for the bone
135 char szTemp[128];
136 ::sprintf(szTemp,"UNNAMED_%i",iCnt++);
137 mName = szTemp;
138 }
140 //! Construction from an existing name
141 Bone( const std::string& name)
142 : mName (name)
143 {}
145 //! Name of the bone
146 std::string mName;
147 };
149 // ---------------------------------------------------------------------------
150 /** Helper structure to represent an ASE file bone vertex */
151 struct BoneVertex
152 {
153 //! Bone and corresponding vertex weight.
154 //! -1 for unrequired bones ....
155 std::vector<std::pair<int,float> > mBoneWeights;
157 //! Position of the bone vertex.
158 //! MUST be identical to the vertex position
159 //aiVector3D mPosition;
160 };
162 // ---------------------------------------------------------------------------
163 /** Helper structure to represent an ASE file animation */
164 struct Animation
165 {
166 enum Type
167 {
168 TRACK = 0x0,
169 BEZIER = 0x1,
170 TCB = 0x2
171 } mRotationType, mScalingType, mPositionType;
173 Animation()
174 : mRotationType (TRACK)
175 , mScalingType (TRACK)
176 , mPositionType (TRACK)
177 {}
179 //! List of track rotation keyframes
180 std::vector< aiQuatKey > akeyRotations;
182 //! List of track position keyframes
183 std::vector< aiVectorKey > akeyPositions;
185 //! List of track scaling keyframes
186 std::vector< aiVectorKey > akeyScaling;
188 };
190 // ---------------------------------------------------------------------------
191 /** Helper structure to represent the inheritance information of an ASE node */
192 struct InheritanceInfo
193 {
194 //! Default constructor
195 InheritanceInfo()
196 {
197 // set the inheritance flag for all axes by default to true
198 for (unsigned int i = 0; i < 3;++i)
199 abInheritPosition[i] = abInheritRotation[i] = abInheritScaling[i] = true;
200 }
202 //! Inherit the parent's position?, axis order is x,y,z
203 bool abInheritPosition[3];
205 //! Inherit the parent's rotation?, axis order is x,y,z
206 bool abInheritRotation[3];
208 //! Inherit the parent's scaling?, axis order is x,y,z
209 bool abInheritScaling[3];
210 };
212 // ---------------------------------------------------------------------------
213 /** Represents an ASE file node. Base class for mesh, light and cameras */
214 struct BaseNode
215 {
216 enum Type {Light, Camera, Mesh, Dummy} mType;
218 //! Constructor. Creates a default name for the node
219 BaseNode(Type _mType)
220 : mType (_mType)
221 , mProcessed (false)
222 {
223 // generate a default name for the node
224 static int iCnt = 0;
225 char szTemp[128]; // should be sufficiently large
226 ::sprintf(szTemp,"UNNAMED_%i",iCnt++);
227 mName = szTemp;
229 // Set mTargetPosition to qnan
230 const float qnan = get_qnan();
231 mTargetPosition.x = qnan;
232 }
234 //! Name of the mesh
235 std::string mName;
237 //! Name of the parent of the node
238 //! "" if there is no parent ...
239 std::string mParent;
241 //! Transformation matrix of the node
242 aiMatrix4x4 mTransform;
244 //! Target position (target lights and cameras)
245 aiVector3D mTargetPosition;
247 //! Specifies which axes transformations a node inherits
248 //! from its parent ...
249 InheritanceInfo inherit;
251 //! Animation channels for the node
252 Animation mAnim;
254 //! Needed for lights and cameras: target animation channel
255 //! Should contain position keys only.
256 Animation mTargetAnim;
258 bool mProcessed;
259 };
261 // ---------------------------------------------------------------------------
262 /** Helper structure to represent an ASE file mesh */
263 struct Mesh : public MeshWithSmoothingGroups<ASE::Face>, public BaseNode
264 {
265 //! Constructor.
266 Mesh()
267 : BaseNode (BaseNode::Mesh)
268 , bSkip (false)
269 {
270 // use 2 texture vertex components by default
271 for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_TEXTURECOORDS;++c)
272 this->mNumUVComponents[c] = 2;
274 // setup the default material index by default
275 iMaterialIndex = Face::DEFAULT_MATINDEX;
276 }
278 //! List of all texture coordinate sets
279 std::vector<aiVector3D> amTexCoords[AI_MAX_NUMBER_OF_TEXTURECOORDS];
281 //! List of all vertex color sets.
282 std::vector<aiColor4D> mVertexColors;
284 //! List of all bone vertices
285 std::vector<BoneVertex> mBoneVertices;
287 //! List of all bones
288 std::vector<Bone> mBones;
290 //! Material index of the mesh
291 unsigned int iMaterialIndex;
293 //! Number of vertex components for each UVW set
294 unsigned int mNumUVComponents[AI_MAX_NUMBER_OF_TEXTURECOORDS];
296 //! used internally
297 bool bSkip;
298 };
300 // ---------------------------------------------------------------------------
301 /** Helper structure to represent an ASE light source */
302 struct Light : public BaseNode
303 {
304 enum LightType
305 {
306 OMNI,
307 TARGET,
308 FREE,
309 DIRECTIONAL
310 };
312 //! Constructor.
313 Light()
314 : BaseNode (BaseNode::Light)
315 , mLightType (OMNI)
316 , mColor (1.f,1.f,1.f)
317 , mIntensity (1.f) // light is white by default
318 , mAngle (45.f)
319 , mFalloff (0.f)
320 {
321 }
323 LightType mLightType;
324 aiColor3D mColor;
325 float mIntensity;
326 float mAngle; // in degrees
327 float mFalloff;
328 };
330 // ---------------------------------------------------------------------------
331 /** Helper structure to represent an ASE camera */
332 struct Camera : public BaseNode
333 {
334 enum CameraType
335 {
336 FREE,
337 TARGET
338 };
340 //! Constructor
341 Camera()
342 : BaseNode (BaseNode::Camera)
343 , mFOV (0.75f) // in radians
344 , mNear (0.1f)
345 , mFar (1000.f) // could be zero
346 , mCameraType (FREE)
347 {
348 }
350 float mFOV, mNear, mFar;
351 CameraType mCameraType;
352 };
354 // ---------------------------------------------------------------------------
355 /** Helper structure to represent an ASE helper object (dummy) */
356 struct Dummy : public BaseNode
357 {
358 //! Constructor
359 Dummy()
360 : BaseNode (BaseNode::Dummy)
361 {
362 }
363 };
365 // Parameters to Parser::Parse()
366 #define AI_ASE_NEW_FILE_FORMAT 200
367 #define AI_ASE_OLD_FILE_FORMAT 110
369 // Internally we're a little bit more tolerant
370 #define AI_ASE_IS_NEW_FILE_FORMAT() (iFileFormat >= 200)
371 #define AI_ASE_IS_OLD_FILE_FORMAT() (iFileFormat < 200)
373 // -------------------------------------------------------------------------------
374 /** \brief Class to parse ASE files
375 */
376 class Parser
377 {
379 private:
381 Parser() {}
383 public:
385 // -------------------------------------------------------------------
386 //! Construct a parser from a given input file which is
387 //! guaranted to be terminated with zero.
388 //! @param szFile Input file
389 //! @param fileFormatDefault Assumed file format version. If the
390 //! file format is specified in the file the new value replaces
391 //! the default value.
392 Parser (const char* szFile, unsigned int fileFormatDefault);
394 // -------------------------------------------------------------------
395 //! Parses the file into the parsers internal representation
396 void Parse();
399 private:
401 // -------------------------------------------------------------------
402 //! Parse the *SCENE block in a file
403 void ParseLV1SceneBlock();
405 // -------------------------------------------------------------------
406 //! Parse the *MESH_SOFTSKINVERTS block in a file
407 void ParseLV1SoftSkinBlock();
409 // -------------------------------------------------------------------
410 //! Parse the *MATERIAL_LIST block in a file
411 void ParseLV1MaterialListBlock();
413 // -------------------------------------------------------------------
414 //! Parse a *<xxx>OBJECT block in a file
415 //! \param mesh Node to be filled
416 void ParseLV1ObjectBlock(BaseNode& mesh);
418 // -------------------------------------------------------------------
419 //! Parse a *MATERIAL blocks in a material list
420 //! \param mat Material structure to be filled
421 void ParseLV2MaterialBlock(Material& mat);
423 // -------------------------------------------------------------------
424 //! Parse a *NODE_TM block in a file
425 //! \param mesh Node (!) object to be filled
426 void ParseLV2NodeTransformBlock(BaseNode& mesh);
428 // -------------------------------------------------------------------
429 //! Parse a *TM_ANIMATION block in a file
430 //! \param mesh Mesh object to be filled
431 void ParseLV2AnimationBlock(BaseNode& mesh);
432 void ParseLV3PosAnimationBlock(ASE::Animation& anim);
433 void ParseLV3ScaleAnimationBlock(ASE::Animation& anim);
434 void ParseLV3RotAnimationBlock(ASE::Animation& anim);
436 // -------------------------------------------------------------------
437 //! Parse a *MESH block in a file
438 //! \param mesh Mesh object to be filled
439 void ParseLV2MeshBlock(Mesh& mesh);
441 // -------------------------------------------------------------------
442 //! Parse a *LIGHT_SETTINGS block in a file
443 //! \param light Light object to be filled
444 void ParseLV2LightSettingsBlock(Light& light);
446 // -------------------------------------------------------------------
447 //! Parse a *CAMERA_SETTINGS block in a file
448 //! \param cam Camera object to be filled
449 void ParseLV2CameraSettingsBlock(Camera& cam);
451 // -------------------------------------------------------------------
452 //! Parse the *MAP_XXXXXX blocks in a material
453 //! \param map Texture structure to be filled
454 void ParseLV3MapBlock(Texture& map);
456 // -------------------------------------------------------------------
457 //! Parse a *MESH_VERTEX_LIST block in a file
458 //! \param iNumVertices Value of *MESH_NUMVERTEX, if present.
459 //! Otherwise zero. This is used to check the consistency of the file.
460 //! A warning is sent to the logger if the validations fails.
461 //! \param mesh Mesh object to be filled
462 void ParseLV3MeshVertexListBlock(
463 unsigned int iNumVertices,Mesh& mesh);
465 // -------------------------------------------------------------------
466 //! Parse a *MESH_FACE_LIST block in a file
467 //! \param iNumFaces Value of *MESH_NUMFACES, if present.
468 //! Otherwise zero. This is used to check the consistency of the file.
469 //! A warning is sent to the logger if the validations fails.
470 //! \param mesh Mesh object to be filled
471 void ParseLV3MeshFaceListBlock(
472 unsigned int iNumFaces,Mesh& mesh);
474 // -------------------------------------------------------------------
475 //! Parse a *MESH_TVERT_LIST block in a file
476 //! \param iNumVertices Value of *MESH_NUMTVERTEX, if present.
477 //! Otherwise zero. This is used to check the consistency of the file.
478 //! A warning is sent to the logger if the validations fails.
479 //! \param mesh Mesh object to be filled
480 //! \param iChannel Output UVW channel
481 void ParseLV3MeshTListBlock(
482 unsigned int iNumVertices,Mesh& mesh, unsigned int iChannel = 0);
484 // -------------------------------------------------------------------
485 //! Parse a *MESH_TFACELIST block in a file
486 //! \param iNumFaces Value of *MESH_NUMTVFACES, if present.
487 //! Otherwise zero. This is used to check the consistency of the file.
488 //! A warning is sent to the logger if the validations fails.
489 //! \param mesh Mesh object to be filled
490 //! \param iChannel Output UVW channel
491 void ParseLV3MeshTFaceListBlock(
492 unsigned int iNumFaces,Mesh& mesh, unsigned int iChannel = 0);
494 // -------------------------------------------------------------------
495 //! Parse an additional mapping channel
496 //! (specified via *MESH_MAPPINGCHANNEL)
497 //! \param iChannel Channel index to be filled
498 //! \param mesh Mesh object to be filled
499 void ParseLV3MappingChannel(
500 unsigned int iChannel, Mesh& mesh);
502 // -------------------------------------------------------------------
503 //! Parse a *MESH_CVERTLIST block in a file
504 //! \param iNumVertices Value of *MESH_NUMCVERTEX, if present.
505 //! Otherwise zero. This is used to check the consistency of the file.
506 //! A warning is sent to the logger if the validations fails.
507 //! \param mesh Mesh object to be filled
508 void ParseLV3MeshCListBlock(
509 unsigned int iNumVertices, Mesh& mesh);
511 // -------------------------------------------------------------------
512 //! Parse a *MESH_CFACELIST block in a file
513 //! \param iNumFaces Value of *MESH_NUMCVFACES, if present.
514 //! Otherwise zero. This is used to check the consistency of the file.
515 //! A warning is sent to the logger if the validations fails.
516 //! \param mesh Mesh object to be filled
517 void ParseLV3MeshCFaceListBlock(
518 unsigned int iNumFaces, Mesh& mesh);
520 // -------------------------------------------------------------------
521 //! Parse a *MESH_NORMALS block in a file
522 //! \param mesh Mesh object to be filled
523 void ParseLV3MeshNormalListBlock(Mesh& mesh);
525 // -------------------------------------------------------------------
526 //! Parse a *MESH_WEIGHTSblock in a file
527 //! \param mesh Mesh object to be filled
528 void ParseLV3MeshWeightsBlock(Mesh& mesh);
530 // -------------------------------------------------------------------
531 //! Parse the bone list of a file
532 //! \param mesh Mesh object to be filled
533 //! \param iNumBones Number of bones in the mesh
534 void ParseLV4MeshBones(unsigned int iNumBones,Mesh& mesh);
536 // -------------------------------------------------------------------
537 //! Parse the bone vertices list of a file
538 //! \param mesh Mesh object to be filled
539 //! \param iNumVertices Number of vertices to be parsed
540 void ParseLV4MeshBonesVertices(unsigned int iNumVertices,Mesh& mesh);
542 // -------------------------------------------------------------------
543 //! Parse a *MESH_FACE block in a file
544 //! \param out receive the face data
545 void ParseLV4MeshFace(ASE::Face& out);
547 // -------------------------------------------------------------------
548 //! Parse a *MESH_VERT block in a file
549 //! (also works for MESH_TVERT, MESH_CFACE, MESH_VERTCOL ...)
550 //! \param apOut Output buffer (3 floats)
551 //! \param rIndexOut Output index
552 void ParseLV4MeshFloatTriple(float* apOut, unsigned int& rIndexOut);
554 // -------------------------------------------------------------------
555 //! Parse a *MESH_VERT block in a file
556 //! (also works for MESH_TVERT, MESH_CFACE, MESH_VERTCOL ...)
557 //! \param apOut Output buffer (3 floats)
558 void ParseLV4MeshFloatTriple(float* apOut);
560 // -------------------------------------------------------------------
561 //! Parse a *MESH_TFACE block in a file
562 //! (also works for MESH_CFACE)
563 //! \param apOut Output buffer (3 ints)
564 //! \param rIndexOut Output index
565 void ParseLV4MeshLongTriple(unsigned int* apOut, unsigned int& rIndexOut);
567 // -------------------------------------------------------------------
568 //! Parse a *MESH_TFACE block in a file
569 //! (also works for MESH_CFACE)
570 //! \param apOut Output buffer (3 ints)
571 void ParseLV4MeshLongTriple(unsigned int* apOut);
573 // -------------------------------------------------------------------
574 //! Parse a single float element
575 //! \param fOut Output float
576 void ParseLV4MeshFloat(float& fOut);
578 // -------------------------------------------------------------------
579 //! Parse a single int element
580 //! \param iOut Output integer
581 void ParseLV4MeshLong(unsigned int& iOut);
583 // -------------------------------------------------------------------
584 //! Skip everything to the next: '*' or '\0'
585 bool SkipToNextToken();
587 // -------------------------------------------------------------------
588 //! Skip the current section until the token after the closing }.
589 //! This function handles embedded subsections correctly
590 bool SkipSection();
592 // -------------------------------------------------------------------
593 //! Output a warning to the logger
594 //! \param szWarn Warn message
595 void LogWarning(const char* szWarn);
597 // -------------------------------------------------------------------
598 //! Output a message to the logger
599 //! \param szWarn Message
600 void LogInfo(const char* szWarn);
602 // -------------------------------------------------------------------
603 //! Output an error to the logger
604 //! \param szWarn Error message
605 void LogError(const char* szWarn);
607 // -------------------------------------------------------------------
608 //! Parse a string, enclosed in double quotation marks
609 //! \param out Output string
610 //! \param szName Name of the enclosing element -> used in error
611 //! messages.
612 //! \return false if an error occured
613 bool ParseString(std::string& out,const char* szName);
615 public:
617 //! Pointer to current data
618 const char* filePtr;
620 //! background color to be passed to the viewer
621 //! QNAN if none was found
622 aiColor3D m_clrBackground;
624 //! Base ambient color to be passed to all materials
625 //! QNAN if none was found
626 aiColor3D m_clrAmbient;
628 //! List of all materials found in the file
629 std::vector<Material> m_vMaterials;
631 //! List of all meshes found in the file
632 std::vector<Mesh> m_vMeshes;
634 //! List of all dummies found in the file
635 std::vector<Dummy> m_vDummies;
637 //! List of all lights found in the file
638 std::vector<Light> m_vLights;
640 //! List of all cameras found in the file
641 std::vector<Camera> m_vCameras;
643 //! Current line in the file
644 unsigned int iLineNumber;
646 //! First frame
647 unsigned int iFirstFrame;
649 //! Last frame
650 unsigned int iLastFrame;
652 //! Frame speed - frames per second
653 unsigned int iFrameSpeed;
655 //! Ticks per frame
656 unsigned int iTicksPerFrame;
658 //! true if the last character read was an end-line character
659 bool bLastWasEndLine;
661 //! File format version
662 unsigned int iFileFormat;
663 };
666 } // Namespace ASE
667 } // Namespace ASSIMP
669 #endif // !! include guard