vrshoot

annotate 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
rev   line source
nuclear@0 1 /*
nuclear@0 2 Open Asset Import Library (assimp)
nuclear@0 3 ----------------------------------------------------------------------
nuclear@0 4
nuclear@0 5 Copyright (c) 2006-2012, assimp team
nuclear@0 6 All rights reserved.
nuclear@0 7
nuclear@0 8 Redistribution and use of this software in source and binary forms,
nuclear@0 9 with or without modification, are permitted provided that the
nuclear@0 10 following conditions are met:
nuclear@0 11
nuclear@0 12 * Redistributions of source code must retain the above
nuclear@0 13 copyright notice, this list of conditions and the
nuclear@0 14 following disclaimer.
nuclear@0 15
nuclear@0 16 * Redistributions in binary form must reproduce the above
nuclear@0 17 copyright notice, this list of conditions and the
nuclear@0 18 following disclaimer in the documentation and/or other
nuclear@0 19 materials provided with the distribution.
nuclear@0 20
nuclear@0 21 * Neither the name of the assimp team, nor the names of its
nuclear@0 22 contributors may be used to endorse or promote products
nuclear@0 23 derived from this software without specific prior
nuclear@0 24 written permission of the assimp team.
nuclear@0 25
nuclear@0 26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
nuclear@0 27 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
nuclear@0 28 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
nuclear@0 29 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
nuclear@0 30 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
nuclear@0 31 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
nuclear@0 32 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
nuclear@0 33 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
nuclear@0 34 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
nuclear@0 35 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
nuclear@0 36 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
nuclear@0 37
nuclear@0 38 ----------------------------------------------------------------------
nuclear@0 39 */
nuclear@0 40
nuclear@0 41
nuclear@0 42 /** @file MD5Parser.h
nuclear@0 43 * @brief Definition of the .MD5 parser class.
nuclear@0 44 * http://www.modwiki.net/wiki/MD5_(file_format)
nuclear@0 45 */
nuclear@0 46 #ifndef AI_MD5PARSER_H_INCLUDED
nuclear@0 47 #define AI_MD5PARSER_H_INCLUDED
nuclear@0 48
nuclear@0 49 #include "assimp/types.h"
nuclear@0 50 #include "ParsingUtils.h"
nuclear@0 51
nuclear@0 52 struct aiFace;
nuclear@0 53
nuclear@0 54 namespace Assimp {
nuclear@0 55 namespace MD5 {
nuclear@0 56
nuclear@0 57 // ---------------------------------------------------------------------------
nuclear@0 58 /** Represents a single element in a MD5 file
nuclear@0 59 *
nuclear@0 60 * Elements are always contained in sections.
nuclear@0 61 */
nuclear@0 62 struct Element
nuclear@0 63 {
nuclear@0 64 //! Points to the starting point of the element
nuclear@0 65 //! Whitespace at the beginning and at the end have been removed,
nuclear@0 66 //! Elements are terminated with \0
nuclear@0 67 char* szStart;
nuclear@0 68
nuclear@0 69 //! Original line number (can be used in error messages
nuclear@0 70 //! if a parsing error occurs)
nuclear@0 71 unsigned int iLineNumber;
nuclear@0 72 };
nuclear@0 73
nuclear@0 74 typedef std::vector< Element > ElementList;
nuclear@0 75
nuclear@0 76 // ---------------------------------------------------------------------------
nuclear@0 77 /** Represents a section of a MD5 file (such as the mesh or the joints section)
nuclear@0 78 *
nuclear@0 79 * A section is always enclosed in { and } brackets.
nuclear@0 80 */
nuclear@0 81 struct Section
nuclear@0 82 {
nuclear@0 83 //! Original line number (can be used in error messages
nuclear@0 84 //! if a parsing error occurs)
nuclear@0 85 unsigned int iLineNumber;
nuclear@0 86
nuclear@0 87 //! List of all elements which have been parsed in this section.
nuclear@0 88 ElementList mElements;
nuclear@0 89
nuclear@0 90 //! Name of the section
nuclear@0 91 std::string mName;
nuclear@0 92
nuclear@0 93 //! For global elements: the value of the element as string
nuclear@0 94 //! Iif !length() the section is not a global element
nuclear@0 95 std::string mGlobalValue;
nuclear@0 96 };
nuclear@0 97
nuclear@0 98 typedef std::vector< Section> SectionList;
nuclear@0 99
nuclear@0 100 // ---------------------------------------------------------------------------
nuclear@0 101 /** Basic information about a joint
nuclear@0 102 */
nuclear@0 103 struct BaseJointDescription
nuclear@0 104 {
nuclear@0 105 //! Name of the bone
nuclear@0 106 aiString mName;
nuclear@0 107
nuclear@0 108 //! Parent index of the bone
nuclear@0 109 int mParentIndex;
nuclear@0 110 };
nuclear@0 111
nuclear@0 112 // ---------------------------------------------------------------------------
nuclear@0 113 /** Represents a bone (joint) descriptor in a MD5Mesh file
nuclear@0 114 */
nuclear@0 115 struct BoneDesc : BaseJointDescription
nuclear@0 116 {
nuclear@0 117 //! Absolute position of the bone
nuclear@0 118 aiVector3D mPositionXYZ;
nuclear@0 119
nuclear@0 120 //! Absolute rotation of the bone
nuclear@0 121 aiVector3D mRotationQuat;
nuclear@0 122 aiQuaternion mRotationQuatConverted;
nuclear@0 123
nuclear@0 124 //! Absolute transformation of the bone
nuclear@0 125 //! (temporary)
nuclear@0 126 aiMatrix4x4 mTransform;
nuclear@0 127
nuclear@0 128 //! Inverse transformation of the bone
nuclear@0 129 //! (temporary)
nuclear@0 130 aiMatrix4x4 mInvTransform;
nuclear@0 131
nuclear@0 132 //! Internal
nuclear@0 133 unsigned int mMap;
nuclear@0 134 };
nuclear@0 135
nuclear@0 136 typedef std::vector< BoneDesc > BoneList;
nuclear@0 137
nuclear@0 138 // ---------------------------------------------------------------------------
nuclear@0 139 /** Represents a bone (joint) descriptor in a MD5Anim file
nuclear@0 140 */
nuclear@0 141 struct AnimBoneDesc : BaseJointDescription
nuclear@0 142 {
nuclear@0 143 //! Flags (AI_MD5_ANIMATION_FLAG_xxx)
nuclear@0 144 unsigned int iFlags;
nuclear@0 145
nuclear@0 146 //! Index of the first key that corresponds to this anim bone
nuclear@0 147 unsigned int iFirstKeyIndex;
nuclear@0 148 };
nuclear@0 149
nuclear@0 150 typedef std::vector< AnimBoneDesc > AnimBoneList;
nuclear@0 151
nuclear@0 152
nuclear@0 153 // ---------------------------------------------------------------------------
nuclear@0 154 /** Represents a base frame descriptor in a MD5Anim file
nuclear@0 155 */
nuclear@0 156 struct BaseFrameDesc
nuclear@0 157 {
nuclear@0 158 aiVector3D vPositionXYZ;
nuclear@0 159 aiVector3D vRotationQuat;
nuclear@0 160 };
nuclear@0 161
nuclear@0 162 typedef std::vector< BaseFrameDesc > BaseFrameList;
nuclear@0 163
nuclear@0 164 // ---------------------------------------------------------------------------
nuclear@0 165 /** Represents a camera animation frame in a MDCamera file
nuclear@0 166 */
nuclear@0 167 struct CameraAnimFrameDesc : BaseFrameDesc
nuclear@0 168 {
nuclear@0 169 float fFOV;
nuclear@0 170 };
nuclear@0 171
nuclear@0 172 typedef std::vector< CameraAnimFrameDesc > CameraFrameList;
nuclear@0 173
nuclear@0 174 // ---------------------------------------------------------------------------
nuclear@0 175 /** Represents a frame descriptor in a MD5Anim file
nuclear@0 176 */
nuclear@0 177 struct FrameDesc
nuclear@0 178 {
nuclear@0 179 //! Index of the frame
nuclear@0 180 unsigned int iIndex;
nuclear@0 181
nuclear@0 182 //! Animation keyframes - a large blob of data at first
nuclear@0 183 std::vector< float > mValues;
nuclear@0 184 };
nuclear@0 185
nuclear@0 186 typedef std::vector< FrameDesc > FrameList;
nuclear@0 187
nuclear@0 188 // ---------------------------------------------------------------------------
nuclear@0 189 /** Represents a vertex descriptor in a MD5 file
nuclear@0 190 */
nuclear@0 191 struct VertexDesc
nuclear@0 192 {
nuclear@0 193 VertexDesc()
nuclear@0 194 : mFirstWeight (0)
nuclear@0 195 , mNumWeights (0)
nuclear@0 196 {}
nuclear@0 197
nuclear@0 198 //! UV cordinate of the vertex
nuclear@0 199 aiVector2D mUV;
nuclear@0 200
nuclear@0 201 //! Index of the first weight of the vertex in
nuclear@0 202 //! the vertex weight list
nuclear@0 203 unsigned int mFirstWeight;
nuclear@0 204
nuclear@0 205 //! Number of weights assigned to this vertex
nuclear@0 206 unsigned int mNumWeights;
nuclear@0 207 };
nuclear@0 208
nuclear@0 209 typedef std::vector< VertexDesc > VertexList;
nuclear@0 210
nuclear@0 211 // ---------------------------------------------------------------------------
nuclear@0 212 /** Represents a vertex weight descriptor in a MD5 file
nuclear@0 213 */
nuclear@0 214 struct WeightDesc
nuclear@0 215 {
nuclear@0 216 //! Index of the bone to which this weight refers
nuclear@0 217 unsigned int mBone;
nuclear@0 218
nuclear@0 219 //! The weight value
nuclear@0 220 float mWeight;
nuclear@0 221
nuclear@0 222 //! The offset position of this weight
nuclear@0 223 // ! (in the coordinate system defined by the parent bone)
nuclear@0 224 aiVector3D vOffsetPosition;
nuclear@0 225 };
nuclear@0 226
nuclear@0 227 typedef std::vector< WeightDesc > WeightList;
nuclear@0 228 typedef std::vector< aiFace > FaceList;
nuclear@0 229
nuclear@0 230 // ---------------------------------------------------------------------------
nuclear@0 231 /** Represents a mesh in a MD5 file
nuclear@0 232 */
nuclear@0 233 struct MeshDesc
nuclear@0 234 {
nuclear@0 235 //! Weights of the mesh
nuclear@0 236 WeightList mWeights;
nuclear@0 237
nuclear@0 238 //! Vertices of the mesh
nuclear@0 239 VertexList mVertices;
nuclear@0 240
nuclear@0 241 //! Faces of the mesh
nuclear@0 242 FaceList mFaces;
nuclear@0 243
nuclear@0 244 //! Name of the shader (=texture) to be assigned to the mesh
nuclear@0 245 aiString mShader;
nuclear@0 246 };
nuclear@0 247
nuclear@0 248 typedef std::vector< MeshDesc > MeshList;
nuclear@0 249
nuclear@0 250 // ---------------------------------------------------------------------------
nuclear@0 251 // Convert a quaternion to its usual representation
nuclear@0 252 inline void ConvertQuaternion (const aiVector3D& in, aiQuaternion& out) {
nuclear@0 253
nuclear@0 254 out.x = in.x;
nuclear@0 255 out.y = in.y;
nuclear@0 256 out.z = in.z;
nuclear@0 257
nuclear@0 258 const float t = 1.0f - (in.x*in.x) - (in.y*in.y) - (in.z*in.z);
nuclear@0 259
nuclear@0 260 if (t < 0.0f)
nuclear@0 261 out.w = 0.0f;
nuclear@0 262 else out.w = sqrt (t);
nuclear@0 263 }
nuclear@0 264
nuclear@0 265 // ---------------------------------------------------------------------------
nuclear@0 266 /** Parses the data sections of a MD5 mesh file
nuclear@0 267 */
nuclear@0 268 class MD5MeshParser
nuclear@0 269 {
nuclear@0 270 public:
nuclear@0 271
nuclear@0 272 // -------------------------------------------------------------------
nuclear@0 273 /** Constructs a new MD5MeshParser instance from an existing
nuclear@0 274 * preparsed list of file sections.
nuclear@0 275 *
nuclear@0 276 * @param mSections List of file sections (output of MD5Parser)
nuclear@0 277 */
nuclear@0 278 MD5MeshParser(SectionList& mSections);
nuclear@0 279
nuclear@0 280 //! List of all meshes
nuclear@0 281 MeshList mMeshes;
nuclear@0 282
nuclear@0 283 //! List of all joints
nuclear@0 284 BoneList mJoints;
nuclear@0 285 };
nuclear@0 286
nuclear@0 287 // remove this flag if you need to the bounding box data
nuclear@0 288 #define AI_MD5_PARSE_NO_BOUNDS
nuclear@0 289
nuclear@0 290 // ---------------------------------------------------------------------------
nuclear@0 291 /** Parses the data sections of a MD5 animation file
nuclear@0 292 */
nuclear@0 293 class MD5AnimParser
nuclear@0 294 {
nuclear@0 295 public:
nuclear@0 296
nuclear@0 297 // -------------------------------------------------------------------
nuclear@0 298 /** Constructs a new MD5AnimParser instance from an existing
nuclear@0 299 * preparsed list of file sections.
nuclear@0 300 *
nuclear@0 301 * @param mSections List of file sections (output of MD5Parser)
nuclear@0 302 */
nuclear@0 303 MD5AnimParser(SectionList& mSections);
nuclear@0 304
nuclear@0 305
nuclear@0 306 //! Output frame rate
nuclear@0 307 float fFrameRate;
nuclear@0 308
nuclear@0 309 //! List of animation bones
nuclear@0 310 AnimBoneList mAnimatedBones;
nuclear@0 311
nuclear@0 312 //! List of base frames
nuclear@0 313 BaseFrameList mBaseFrames;
nuclear@0 314
nuclear@0 315 //! List of animation frames
nuclear@0 316 FrameList mFrames;
nuclear@0 317
nuclear@0 318 //! Number of animated components
nuclear@0 319 unsigned int mNumAnimatedComponents;
nuclear@0 320 };
nuclear@0 321
nuclear@0 322 // ---------------------------------------------------------------------------
nuclear@0 323 /** Parses the data sections of a MD5 camera animation file
nuclear@0 324 */
nuclear@0 325 class MD5CameraParser
nuclear@0 326 {
nuclear@0 327 public:
nuclear@0 328
nuclear@0 329 // -------------------------------------------------------------------
nuclear@0 330 /** Constructs a new MD5CameraParser instance from an existing
nuclear@0 331 * preparsed list of file sections.
nuclear@0 332 *
nuclear@0 333 * @param mSections List of file sections (output of MD5Parser)
nuclear@0 334 */
nuclear@0 335 MD5CameraParser(SectionList& mSections);
nuclear@0 336
nuclear@0 337
nuclear@0 338 //! Output frame rate
nuclear@0 339 float fFrameRate;
nuclear@0 340
nuclear@0 341 //! List of cuts
nuclear@0 342 std::vector<unsigned int> cuts;
nuclear@0 343
nuclear@0 344 //! Frames
nuclear@0 345 CameraFrameList frames;
nuclear@0 346 };
nuclear@0 347
nuclear@0 348 // ---------------------------------------------------------------------------
nuclear@0 349 /** Parses the block structure of MD5MESH and MD5ANIM files (but does no
nuclear@0 350 * further processing)
nuclear@0 351 */
nuclear@0 352 class MD5Parser
nuclear@0 353 {
nuclear@0 354 public:
nuclear@0 355
nuclear@0 356 // -------------------------------------------------------------------
nuclear@0 357 /** Constructs a new MD5Parser instance from an existing buffer.
nuclear@0 358 *
nuclear@0 359 * @param buffer File buffer
nuclear@0 360 * @param fileSize Length of the file in bytes (excluding a terminal 0)
nuclear@0 361 */
nuclear@0 362 MD5Parser(char* buffer, unsigned int fileSize);
nuclear@0 363
nuclear@0 364
nuclear@0 365 // -------------------------------------------------------------------
nuclear@0 366 /** Report a specific error message and throw an exception
nuclear@0 367 * @param error Error message to be reported
nuclear@0 368 * @param line Index of the line where the error occured
nuclear@0 369 */
nuclear@0 370 static void ReportError (const char* error, unsigned int line);
nuclear@0 371
nuclear@0 372 // -------------------------------------------------------------------
nuclear@0 373 /** Report a specific warning
nuclear@0 374 * @param warn Warn message to be reported
nuclear@0 375 * @param line Index of the line where the error occured
nuclear@0 376 */
nuclear@0 377 static void ReportWarning (const char* warn, unsigned int line);
nuclear@0 378
nuclear@0 379
nuclear@0 380 void ReportError (const char* error) {
nuclear@0 381 return ReportError(error, lineNumber);
nuclear@0 382 }
nuclear@0 383
nuclear@0 384 void ReportWarning (const char* warn) {
nuclear@0 385 return ReportWarning(warn, lineNumber);
nuclear@0 386 }
nuclear@0 387
nuclear@0 388 public:
nuclear@0 389
nuclear@0 390 //! List of all sections which have been read
nuclear@0 391 SectionList mSections;
nuclear@0 392
nuclear@0 393 private:
nuclear@0 394
nuclear@0 395 // -------------------------------------------------------------------
nuclear@0 396 /** Parses a file section. The current file pointer must be outside
nuclear@0 397 * of a section.
nuclear@0 398 * @param out Receives the section data
nuclear@0 399 * @return true if the end of the file has been reached
nuclear@0 400 * @throws ImportErrorException if an error occurs
nuclear@0 401 */
nuclear@0 402 bool ParseSection(Section& out);
nuclear@0 403
nuclear@0 404 // -------------------------------------------------------------------
nuclear@0 405 /** Parses the file header
nuclear@0 406 * @throws ImportErrorException if an error occurs
nuclear@0 407 */
nuclear@0 408 void ParseHeader();
nuclear@0 409
nuclear@0 410
nuclear@0 411 // override these functions to make sure the line counter gets incremented
nuclear@0 412 // -------------------------------------------------------------------
nuclear@0 413 bool SkipLine( const char* in, const char** out)
nuclear@0 414 {
nuclear@0 415 ++lineNumber;
nuclear@0 416 return Assimp::SkipLine(in,out);
nuclear@0 417 }
nuclear@0 418 // -------------------------------------------------------------------
nuclear@0 419 bool SkipLine( )
nuclear@0 420 {
nuclear@0 421 return SkipLine(buffer,(const char**)&buffer);
nuclear@0 422 }
nuclear@0 423 // -------------------------------------------------------------------
nuclear@0 424 bool SkipSpacesAndLineEnd( const char* in, const char** out)
nuclear@0 425 {
nuclear@0 426 bool bHad = false;
nuclear@0 427 bool running = true;
nuclear@0 428 while (running) {
nuclear@0 429 if( *in == '\r' || *in == '\n') {
nuclear@0 430 // we open files in binary mode, so there could be \r\n sequences ...
nuclear@0 431 if (!bHad) {
nuclear@0 432 bHad = true;
nuclear@0 433 ++lineNumber;
nuclear@0 434 }
nuclear@0 435 }
nuclear@0 436 else if (*in == '\t' || *in == ' ')bHad = false;
nuclear@0 437 else break;
nuclear@0 438 in++;
nuclear@0 439 }
nuclear@0 440 *out = in;
nuclear@0 441 return *in != '\0';
nuclear@0 442 }
nuclear@0 443 // -------------------------------------------------------------------
nuclear@0 444 bool SkipSpacesAndLineEnd( )
nuclear@0 445 {
nuclear@0 446 return SkipSpacesAndLineEnd(buffer,(const char**)&buffer);
nuclear@0 447 }
nuclear@0 448 // -------------------------------------------------------------------
nuclear@0 449 bool SkipSpaces( )
nuclear@0 450 {
nuclear@0 451 return Assimp::SkipSpaces((const char**)&buffer);
nuclear@0 452 }
nuclear@0 453
nuclear@0 454 char* buffer;
nuclear@0 455 unsigned int fileSize;
nuclear@0 456 unsigned int lineNumber;
nuclear@0 457 };
nuclear@0 458 }}
nuclear@0 459
nuclear@0 460 #endif // AI_MD5PARSER_H_INCLUDED