vrshoot

view libs/assimp/MD5Parser.h @ 0:b2f14e535253

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 01 Feb 2014 19:58:19 +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 MD5Parser.h
43 * @brief Definition of the .MD5 parser class.
44 * http://www.modwiki.net/wiki/MD5_(file_format)
45 */
46 #ifndef AI_MD5PARSER_H_INCLUDED
47 #define AI_MD5PARSER_H_INCLUDED
49 #include "assimp/types.h"
50 #include "ParsingUtils.h"
52 struct aiFace;
54 namespace Assimp {
55 namespace MD5 {
57 // ---------------------------------------------------------------------------
58 /** Represents a single element in a MD5 file
59 *
60 * Elements are always contained in sections.
61 */
62 struct Element
63 {
64 //! Points to the starting point of the element
65 //! Whitespace at the beginning and at the end have been removed,
66 //! Elements are terminated with \0
67 char* szStart;
69 //! Original line number (can be used in error messages
70 //! if a parsing error occurs)
71 unsigned int iLineNumber;
72 };
74 typedef std::vector< Element > ElementList;
76 // ---------------------------------------------------------------------------
77 /** Represents a section of a MD5 file (such as the mesh or the joints section)
78 *
79 * A section is always enclosed in { and } brackets.
80 */
81 struct Section
82 {
83 //! Original line number (can be used in error messages
84 //! if a parsing error occurs)
85 unsigned int iLineNumber;
87 //! List of all elements which have been parsed in this section.
88 ElementList mElements;
90 //! Name of the section
91 std::string mName;
93 //! For global elements: the value of the element as string
94 //! Iif !length() the section is not a global element
95 std::string mGlobalValue;
96 };
98 typedef std::vector< Section> SectionList;
100 // ---------------------------------------------------------------------------
101 /** Basic information about a joint
102 */
103 struct BaseJointDescription
104 {
105 //! Name of the bone
106 aiString mName;
108 //! Parent index of the bone
109 int mParentIndex;
110 };
112 // ---------------------------------------------------------------------------
113 /** Represents a bone (joint) descriptor in a MD5Mesh file
114 */
115 struct BoneDesc : BaseJointDescription
116 {
117 //! Absolute position of the bone
118 aiVector3D mPositionXYZ;
120 //! Absolute rotation of the bone
121 aiVector3D mRotationQuat;
122 aiQuaternion mRotationQuatConverted;
124 //! Absolute transformation of the bone
125 //! (temporary)
126 aiMatrix4x4 mTransform;
128 //! Inverse transformation of the bone
129 //! (temporary)
130 aiMatrix4x4 mInvTransform;
132 //! Internal
133 unsigned int mMap;
134 };
136 typedef std::vector< BoneDesc > BoneList;
138 // ---------------------------------------------------------------------------
139 /** Represents a bone (joint) descriptor in a MD5Anim file
140 */
141 struct AnimBoneDesc : BaseJointDescription
142 {
143 //! Flags (AI_MD5_ANIMATION_FLAG_xxx)
144 unsigned int iFlags;
146 //! Index of the first key that corresponds to this anim bone
147 unsigned int iFirstKeyIndex;
148 };
150 typedef std::vector< AnimBoneDesc > AnimBoneList;
153 // ---------------------------------------------------------------------------
154 /** Represents a base frame descriptor in a MD5Anim file
155 */
156 struct BaseFrameDesc
157 {
158 aiVector3D vPositionXYZ;
159 aiVector3D vRotationQuat;
160 };
162 typedef std::vector< BaseFrameDesc > BaseFrameList;
164 // ---------------------------------------------------------------------------
165 /** Represents a camera animation frame in a MDCamera file
166 */
167 struct CameraAnimFrameDesc : BaseFrameDesc
168 {
169 float fFOV;
170 };
172 typedef std::vector< CameraAnimFrameDesc > CameraFrameList;
174 // ---------------------------------------------------------------------------
175 /** Represents a frame descriptor in a MD5Anim file
176 */
177 struct FrameDesc
178 {
179 //! Index of the frame
180 unsigned int iIndex;
182 //! Animation keyframes - a large blob of data at first
183 std::vector< float > mValues;
184 };
186 typedef std::vector< FrameDesc > FrameList;
188 // ---------------------------------------------------------------------------
189 /** Represents a vertex descriptor in a MD5 file
190 */
191 struct VertexDesc
192 {
193 VertexDesc()
194 : mFirstWeight (0)
195 , mNumWeights (0)
196 {}
198 //! UV cordinate of the vertex
199 aiVector2D mUV;
201 //! Index of the first weight of the vertex in
202 //! the vertex weight list
203 unsigned int mFirstWeight;
205 //! Number of weights assigned to this vertex
206 unsigned int mNumWeights;
207 };
209 typedef std::vector< VertexDesc > VertexList;
211 // ---------------------------------------------------------------------------
212 /** Represents a vertex weight descriptor in a MD5 file
213 */
214 struct WeightDesc
215 {
216 //! Index of the bone to which this weight refers
217 unsigned int mBone;
219 //! The weight value
220 float mWeight;
222 //! The offset position of this weight
223 // ! (in the coordinate system defined by the parent bone)
224 aiVector3D vOffsetPosition;
225 };
227 typedef std::vector< WeightDesc > WeightList;
228 typedef std::vector< aiFace > FaceList;
230 // ---------------------------------------------------------------------------
231 /** Represents a mesh in a MD5 file
232 */
233 struct MeshDesc
234 {
235 //! Weights of the mesh
236 WeightList mWeights;
238 //! Vertices of the mesh
239 VertexList mVertices;
241 //! Faces of the mesh
242 FaceList mFaces;
244 //! Name of the shader (=texture) to be assigned to the mesh
245 aiString mShader;
246 };
248 typedef std::vector< MeshDesc > MeshList;
250 // ---------------------------------------------------------------------------
251 // Convert a quaternion to its usual representation
252 inline void ConvertQuaternion (const aiVector3D& in, aiQuaternion& out) {
254 out.x = in.x;
255 out.y = in.y;
256 out.z = in.z;
258 const float t = 1.0f - (in.x*in.x) - (in.y*in.y) - (in.z*in.z);
260 if (t < 0.0f)
261 out.w = 0.0f;
262 else out.w = sqrt (t);
263 }
265 // ---------------------------------------------------------------------------
266 /** Parses the data sections of a MD5 mesh file
267 */
268 class MD5MeshParser
269 {
270 public:
272 // -------------------------------------------------------------------
273 /** Constructs a new MD5MeshParser instance from an existing
274 * preparsed list of file sections.
275 *
276 * @param mSections List of file sections (output of MD5Parser)
277 */
278 MD5MeshParser(SectionList& mSections);
280 //! List of all meshes
281 MeshList mMeshes;
283 //! List of all joints
284 BoneList mJoints;
285 };
287 // remove this flag if you need to the bounding box data
288 #define AI_MD5_PARSE_NO_BOUNDS
290 // ---------------------------------------------------------------------------
291 /** Parses the data sections of a MD5 animation file
292 */
293 class MD5AnimParser
294 {
295 public:
297 // -------------------------------------------------------------------
298 /** Constructs a new MD5AnimParser instance from an existing
299 * preparsed list of file sections.
300 *
301 * @param mSections List of file sections (output of MD5Parser)
302 */
303 MD5AnimParser(SectionList& mSections);
306 //! Output frame rate
307 float fFrameRate;
309 //! List of animation bones
310 AnimBoneList mAnimatedBones;
312 //! List of base frames
313 BaseFrameList mBaseFrames;
315 //! List of animation frames
316 FrameList mFrames;
318 //! Number of animated components
319 unsigned int mNumAnimatedComponents;
320 };
322 // ---------------------------------------------------------------------------
323 /** Parses the data sections of a MD5 camera animation file
324 */
325 class MD5CameraParser
326 {
327 public:
329 // -------------------------------------------------------------------
330 /** Constructs a new MD5CameraParser instance from an existing
331 * preparsed list of file sections.
332 *
333 * @param mSections List of file sections (output of MD5Parser)
334 */
335 MD5CameraParser(SectionList& mSections);
338 //! Output frame rate
339 float fFrameRate;
341 //! List of cuts
342 std::vector<unsigned int> cuts;
344 //! Frames
345 CameraFrameList frames;
346 };
348 // ---------------------------------------------------------------------------
349 /** Parses the block structure of MD5MESH and MD5ANIM files (but does no
350 * further processing)
351 */
352 class MD5Parser
353 {
354 public:
356 // -------------------------------------------------------------------
357 /** Constructs a new MD5Parser instance from an existing buffer.
358 *
359 * @param buffer File buffer
360 * @param fileSize Length of the file in bytes (excluding a terminal 0)
361 */
362 MD5Parser(char* buffer, unsigned int fileSize);
365 // -------------------------------------------------------------------
366 /** Report a specific error message and throw an exception
367 * @param error Error message to be reported
368 * @param line Index of the line where the error occured
369 */
370 static void ReportError (const char* error, unsigned int line);
372 // -------------------------------------------------------------------
373 /** Report a specific warning
374 * @param warn Warn message to be reported
375 * @param line Index of the line where the error occured
376 */
377 static void ReportWarning (const char* warn, unsigned int line);
380 void ReportError (const char* error) {
381 return ReportError(error, lineNumber);
382 }
384 void ReportWarning (const char* warn) {
385 return ReportWarning(warn, lineNumber);
386 }
388 public:
390 //! List of all sections which have been read
391 SectionList mSections;
393 private:
395 // -------------------------------------------------------------------
396 /** Parses a file section. The current file pointer must be outside
397 * of a section.
398 * @param out Receives the section data
399 * @return true if the end of the file has been reached
400 * @throws ImportErrorException if an error occurs
401 */
402 bool ParseSection(Section& out);
404 // -------------------------------------------------------------------
405 /** Parses the file header
406 * @throws ImportErrorException if an error occurs
407 */
408 void ParseHeader();
411 // override these functions to make sure the line counter gets incremented
412 // -------------------------------------------------------------------
413 bool SkipLine( const char* in, const char** out)
414 {
415 ++lineNumber;
416 return Assimp::SkipLine(in,out);
417 }
418 // -------------------------------------------------------------------
419 bool SkipLine( )
420 {
421 return SkipLine(buffer,(const char**)&buffer);
422 }
423 // -------------------------------------------------------------------
424 bool SkipSpacesAndLineEnd( const char* in, const char** out)
425 {
426 bool bHad = false;
427 bool running = true;
428 while (running) {
429 if( *in == '\r' || *in == '\n') {
430 // we open files in binary mode, so there could be \r\n sequences ...
431 if (!bHad) {
432 bHad = true;
433 ++lineNumber;
434 }
435 }
436 else if (*in == '\t' || *in == ' ')bHad = false;
437 else break;
438 in++;
439 }
440 *out = in;
441 return *in != '\0';
442 }
443 // -------------------------------------------------------------------
444 bool SkipSpacesAndLineEnd( )
445 {
446 return SkipSpacesAndLineEnd(buffer,(const char**)&buffer);
447 }
448 // -------------------------------------------------------------------
449 bool SkipSpaces( )
450 {
451 return Assimp::SkipSpaces((const char**)&buffer);
452 }
454 char* buffer;
455 unsigned int fileSize;
456 unsigned int lineNumber;
457 };
458 }}
460 #endif // AI_MD5PARSER_H_INCLUDED