vrshoot

diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/assimp/MD5Parser.h	Sat Feb 01 19:58:19 2014 +0200
     1.3 @@ -0,0 +1,460 @@
     1.4 +/*
     1.5 +Open Asset Import Library (assimp)
     1.6 +----------------------------------------------------------------------
     1.7 +
     1.8 +Copyright (c) 2006-2012, assimp team
     1.9 +All rights reserved.
    1.10 +
    1.11 +Redistribution and use of this software in source and binary forms, 
    1.12 +with or without modification, are permitted provided that the 
    1.13 +following conditions are met:
    1.14 +
    1.15 +* Redistributions of source code must retain the above
    1.16 +  copyright notice, this list of conditions and the
    1.17 +  following disclaimer.
    1.18 +
    1.19 +* Redistributions in binary form must reproduce the above
    1.20 +  copyright notice, this list of conditions and the
    1.21 +  following disclaimer in the documentation and/or other
    1.22 +  materials provided with the distribution.
    1.23 +
    1.24 +* Neither the name of the assimp team, nor the names of its
    1.25 +  contributors may be used to endorse or promote products
    1.26 +  derived from this software without specific prior
    1.27 +  written permission of the assimp team.
    1.28 +
    1.29 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
    1.30 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
    1.31 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.32 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
    1.33 +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.34 +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
    1.35 +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.36 +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
    1.37 +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
    1.38 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
    1.39 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.40 +
    1.41 +----------------------------------------------------------------------
    1.42 +*/
    1.43 +
    1.44 +
    1.45 +/** @file  MD5Parser.h
    1.46 + *  @brief Definition of the .MD5 parser class.
    1.47 + *  http://www.modwiki.net/wiki/MD5_(file_format)
    1.48 + */
    1.49 +#ifndef AI_MD5PARSER_H_INCLUDED
    1.50 +#define AI_MD5PARSER_H_INCLUDED
    1.51 +
    1.52 +#include "assimp/types.h"
    1.53 +#include "ParsingUtils.h"
    1.54 +
    1.55 +struct aiFace;
    1.56 +
    1.57 +namespace Assimp	{
    1.58 +namespace MD5			{
    1.59 +
    1.60 +// ---------------------------------------------------------------------------
    1.61 +/** Represents a single element in a MD5 file
    1.62 + *  
    1.63 + *  Elements are always contained in sections.
    1.64 +*/
    1.65 +struct Element
    1.66 +{
    1.67 +	//! Points to the starting point of the element
    1.68 +	//! Whitespace at the beginning and at the end have been removed,
    1.69 +	//! Elements are terminated with \0
    1.70 +	char* szStart;
    1.71 +
    1.72 +	//! Original line number (can be used in error messages
    1.73 +	//! if a parsing error occurs)
    1.74 +	unsigned int iLineNumber;
    1.75 +};
    1.76 +
    1.77 +typedef std::vector< Element > ElementList;
    1.78 +
    1.79 +// ---------------------------------------------------------------------------
    1.80 +/** Represents a section of a MD5 file (such as the mesh or the joints section)
    1.81 + *  
    1.82 + *  A section is always enclosed in { and } brackets.
    1.83 +*/
    1.84 +struct Section
    1.85 +{
    1.86 +	//! Original line number (can be used in error messages
    1.87 +	//! if a parsing error occurs)
    1.88 +	unsigned int iLineNumber;
    1.89 +
    1.90 +	//! List of all elements which have been parsed in this section.
    1.91 +	ElementList mElements;
    1.92 +
    1.93 +	//! Name of the section
    1.94 +	std::string mName;
    1.95 +
    1.96 +	//! For global elements: the value of the element as string
    1.97 +	//! Iif !length() the section is not a global element
    1.98 +	std::string mGlobalValue;
    1.99 +};
   1.100 +
   1.101 +typedef std::vector< Section> SectionList;
   1.102 +
   1.103 +// ---------------------------------------------------------------------------
   1.104 +/** Basic information about a joint
   1.105 +*/
   1.106 +struct BaseJointDescription
   1.107 +{
   1.108 +	//! Name of the bone
   1.109 +	aiString mName;
   1.110 +
   1.111 +	//! Parent index of the bone
   1.112 +	int mParentIndex;
   1.113 +};
   1.114 +
   1.115 +// ---------------------------------------------------------------------------
   1.116 +/** Represents a bone (joint) descriptor in a MD5Mesh file
   1.117 +*/
   1.118 +struct BoneDesc : BaseJointDescription
   1.119 +{
   1.120 +	//! Absolute position of the bone
   1.121 +	aiVector3D mPositionXYZ;
   1.122 +
   1.123 +	//! Absolute rotation of the bone
   1.124 +	aiVector3D mRotationQuat;
   1.125 +	aiQuaternion mRotationQuatConverted;
   1.126 +
   1.127 +	//! Absolute transformation of the bone
   1.128 +	//! (temporary)
   1.129 +	aiMatrix4x4 mTransform;
   1.130 +
   1.131 +	//! Inverse transformation of the bone
   1.132 +	//! (temporary)
   1.133 +	aiMatrix4x4 mInvTransform;
   1.134 +
   1.135 +	//! Internal
   1.136 +	unsigned int mMap;
   1.137 +};
   1.138 +
   1.139 +typedef std::vector< BoneDesc > BoneList;
   1.140 +
   1.141 +// ---------------------------------------------------------------------------
   1.142 +/** Represents a bone (joint) descriptor in a MD5Anim file
   1.143 +*/
   1.144 +struct AnimBoneDesc : BaseJointDescription
   1.145 +{
   1.146 +	//! Flags (AI_MD5_ANIMATION_FLAG_xxx)
   1.147 +	unsigned int iFlags;
   1.148 +
   1.149 +	//! Index of the first key that corresponds to this anim bone
   1.150 +	unsigned int iFirstKeyIndex;
   1.151 +};
   1.152 +
   1.153 +typedef std::vector< AnimBoneDesc > AnimBoneList;
   1.154 +
   1.155 +
   1.156 +// ---------------------------------------------------------------------------
   1.157 +/** Represents a base frame descriptor in a MD5Anim file
   1.158 +*/
   1.159 +struct BaseFrameDesc
   1.160 +{
   1.161 +	aiVector3D vPositionXYZ;
   1.162 +	aiVector3D vRotationQuat;
   1.163 +};
   1.164 +
   1.165 +typedef std::vector< BaseFrameDesc > BaseFrameList;
   1.166 +
   1.167 +// ---------------------------------------------------------------------------
   1.168 +/** Represents a camera animation frame in a MDCamera file
   1.169 +*/
   1.170 +struct CameraAnimFrameDesc : BaseFrameDesc
   1.171 +{
   1.172 +	float fFOV;
   1.173 +};
   1.174 +
   1.175 +typedef std::vector< CameraAnimFrameDesc > CameraFrameList;
   1.176 +
   1.177 +// ---------------------------------------------------------------------------
   1.178 +/** Represents a frame descriptor in a MD5Anim file
   1.179 +*/
   1.180 +struct FrameDesc
   1.181 +{
   1.182 +	//! Index of the frame
   1.183 +	unsigned int iIndex;
   1.184 +
   1.185 +	//! Animation keyframes - a large blob of data at first
   1.186 +	std::vector< float > mValues;
   1.187 +};
   1.188 +
   1.189 +typedef std::vector< FrameDesc > FrameList;
   1.190 +
   1.191 +// ---------------------------------------------------------------------------
   1.192 +/** Represents a vertex  descriptor in a MD5 file
   1.193 +*/
   1.194 +struct VertexDesc
   1.195 +{
   1.196 +	VertexDesc()
   1.197 +		: mFirstWeight	(0)
   1.198 +		, mNumWeights	(0)
   1.199 +	{}
   1.200 +
   1.201 +	//! UV cordinate of the vertex
   1.202 +	aiVector2D mUV;
   1.203 +
   1.204 +	//! Index of the first weight of the vertex in
   1.205 +	//! the vertex weight list
   1.206 +	unsigned int mFirstWeight;
   1.207 +
   1.208 +	//! Number of weights assigned to this vertex
   1.209 +	unsigned int mNumWeights;
   1.210 +};
   1.211 +
   1.212 +typedef std::vector< VertexDesc > VertexList;
   1.213 +
   1.214 +// ---------------------------------------------------------------------------
   1.215 +/** Represents a vertex weight descriptor in a MD5 file
   1.216 +*/
   1.217 +struct WeightDesc
   1.218 +{
   1.219 +	//! Index of the bone to which this weight refers
   1.220 +	unsigned int mBone;
   1.221 +
   1.222 +	//! The weight value
   1.223 +	float mWeight;
   1.224 +
   1.225 +	//! The offset position of this weight
   1.226 +	// ! (in the coordinate system defined by the parent bone)
   1.227 +	aiVector3D vOffsetPosition;
   1.228 +};
   1.229 +
   1.230 +typedef std::vector< WeightDesc > WeightList;
   1.231 +typedef std::vector< aiFace > FaceList;
   1.232 +
   1.233 +// ---------------------------------------------------------------------------
   1.234 +/** Represents a mesh in a MD5 file
   1.235 +*/
   1.236 +struct MeshDesc
   1.237 +{
   1.238 +	//! Weights of the mesh
   1.239 +	WeightList mWeights;
   1.240 +
   1.241 +	//! Vertices of the mesh
   1.242 +	VertexList mVertices;
   1.243 +
   1.244 +	//! Faces of the mesh
   1.245 +	FaceList mFaces;
   1.246 +
   1.247 +	//! Name of the shader (=texture) to be assigned to the mesh
   1.248 +	aiString mShader;
   1.249 +};
   1.250 +
   1.251 +typedef std::vector< MeshDesc > MeshList;
   1.252 +
   1.253 +// ---------------------------------------------------------------------------
   1.254 +// Convert a quaternion to its usual representation
   1.255 +inline void ConvertQuaternion (const aiVector3D& in, aiQuaternion& out) {
   1.256 +
   1.257 +	out.x = in.x;
   1.258 +	out.y = in.y;
   1.259 +	out.z = in.z;
   1.260 +
   1.261 +	const float t = 1.0f - (in.x*in.x) - (in.y*in.y) - (in.z*in.z);
   1.262 +
   1.263 +	if (t < 0.0f)
   1.264 +		out.w = 0.0f;
   1.265 +	else out.w = sqrt (t);
   1.266 +}
   1.267 +
   1.268 +// ---------------------------------------------------------------------------
   1.269 +/** Parses the data sections of a MD5 mesh file
   1.270 +*/
   1.271 +class MD5MeshParser
   1.272 +{
   1.273 +public:
   1.274 +
   1.275 +	// -------------------------------------------------------------------
   1.276 +	/** Constructs a new MD5MeshParser instance from an existing
   1.277 +	 *  preparsed list of file sections.
   1.278 +	 *
   1.279 +	 *  @param mSections List of file sections (output of MD5Parser)
   1.280 +	 */
   1.281 +	MD5MeshParser(SectionList& mSections);
   1.282 +
   1.283 +	//! List of all meshes
   1.284 +	MeshList mMeshes;
   1.285 +
   1.286 +	//! List of all joints
   1.287 +	BoneList mJoints;
   1.288 +};
   1.289 +
   1.290 +// remove this flag if you need to the bounding box data
   1.291 +#define AI_MD5_PARSE_NO_BOUNDS
   1.292 +
   1.293 +// ---------------------------------------------------------------------------
   1.294 +/** Parses the data sections of a MD5 animation file
   1.295 +*/
   1.296 +class MD5AnimParser
   1.297 +{
   1.298 +public:
   1.299 +
   1.300 +	// -------------------------------------------------------------------
   1.301 +	/** Constructs a new MD5AnimParser instance from an existing
   1.302 +	 *  preparsed list of file sections.
   1.303 +	 *
   1.304 +	 *  @param mSections List of file sections (output of MD5Parser)
   1.305 +	 */
   1.306 +	MD5AnimParser(SectionList& mSections);
   1.307 +
   1.308 +	
   1.309 +	//! Output frame rate
   1.310 +	float fFrameRate;
   1.311 +
   1.312 +	//! List of animation bones
   1.313 +	AnimBoneList mAnimatedBones;
   1.314 +
   1.315 +	//! List of base frames
   1.316 +	BaseFrameList mBaseFrames;
   1.317 +
   1.318 +	//! List of animation frames
   1.319 +	FrameList mFrames;
   1.320 +
   1.321 +	//! Number of animated components
   1.322 +	unsigned int mNumAnimatedComponents;
   1.323 +};
   1.324 +
   1.325 +// ---------------------------------------------------------------------------
   1.326 +/** Parses the data sections of a MD5 camera animation file
   1.327 +*/
   1.328 +class MD5CameraParser
   1.329 +{
   1.330 +public:
   1.331 +
   1.332 +	// -------------------------------------------------------------------
   1.333 +	/** Constructs a new MD5CameraParser instance from an existing
   1.334 +	 *  preparsed list of file sections.
   1.335 +	 *
   1.336 +	 *  @param mSections List of file sections (output of MD5Parser)
   1.337 +	 */
   1.338 +	MD5CameraParser(SectionList& mSections);
   1.339 +
   1.340 +	
   1.341 +	//! Output frame rate
   1.342 +	float fFrameRate;
   1.343 +
   1.344 +	//! List of cuts
   1.345 +	std::vector<unsigned int> cuts;
   1.346 +
   1.347 +	//! Frames
   1.348 +	CameraFrameList frames;
   1.349 +};
   1.350 +
   1.351 +// ---------------------------------------------------------------------------
   1.352 +/** Parses the block structure of MD5MESH and MD5ANIM files (but does no
   1.353 + *  further processing)
   1.354 +*/
   1.355 +class MD5Parser
   1.356 +{
   1.357 +public:
   1.358 +
   1.359 +	// -------------------------------------------------------------------
   1.360 +	/** Constructs a new MD5Parser instance from an existing buffer.
   1.361 +	 *
   1.362 +	 *  @param buffer File buffer
   1.363 +	 *  @param fileSize Length of the file in bytes (excluding a terminal 0)
   1.364 +	 */
   1.365 +	MD5Parser(char* buffer, unsigned int fileSize);
   1.366 +
   1.367 +	
   1.368 +	// -------------------------------------------------------------------
   1.369 +	/** Report a specific error message and throw an exception
   1.370 +	 *  @param error Error message to be reported
   1.371 +	 *  @param line Index of the line where the error occured
   1.372 +	 */
   1.373 +	static void ReportError (const char* error, unsigned int line);
   1.374 +
   1.375 +	// -------------------------------------------------------------------
   1.376 +	/** Report a specific warning
   1.377 +	 *  @param warn Warn message to be reported
   1.378 +	 *  @param line Index of the line where the error occured
   1.379 +	 */
   1.380 +	static void ReportWarning (const char* warn, unsigned int line);
   1.381 +
   1.382 +
   1.383 +	void ReportError (const char* error) {
   1.384 +		return ReportError(error, lineNumber);
   1.385 +	}
   1.386 +
   1.387 +	void ReportWarning (const char* warn) {
   1.388 +		return ReportWarning(warn, lineNumber);
   1.389 +	}
   1.390 +
   1.391 +public:
   1.392 +
   1.393 +	//! List of all sections which have been read
   1.394 +	SectionList mSections;
   1.395 +
   1.396 +private:
   1.397 +
   1.398 +	// -------------------------------------------------------------------
   1.399 +	/** Parses a file section. The current file pointer must be outside
   1.400 +	 *  of a section.
   1.401 +	 *  @param out Receives the section data
   1.402 +	 *  @return true if the end of the file has been reached
   1.403 +	 *  @throws ImportErrorException if an error occurs
   1.404 +	 */
   1.405 +	bool ParseSection(Section& out);
   1.406 +
   1.407 +	// -------------------------------------------------------------------
   1.408 +	/** Parses the file header
   1.409 +	 *  @throws ImportErrorException if an error occurs
   1.410 +	 */
   1.411 +	void ParseHeader();
   1.412 +
   1.413 +
   1.414 +	// override these functions to make sure the line counter gets incremented
   1.415 +	// -------------------------------------------------------------------
   1.416 +	bool SkipLine( const char* in, const char** out)
   1.417 +	{
   1.418 +		++lineNumber;
   1.419 +		return Assimp::SkipLine(in,out);
   1.420 +	}
   1.421 +	// -------------------------------------------------------------------
   1.422 +	bool SkipLine( )
   1.423 +	{
   1.424 +		return SkipLine(buffer,(const char**)&buffer);
   1.425 +	}
   1.426 +	// -------------------------------------------------------------------
   1.427 +	bool SkipSpacesAndLineEnd( const char* in, const char** out)
   1.428 +	{
   1.429 +		bool bHad = false;
   1.430 +		bool running = true;
   1.431 +		while (running)	{
   1.432 +			if( *in == '\r' || *in == '\n')	{
   1.433 +				 // we open files in binary mode, so there could be \r\n sequences ...
   1.434 +				if (!bHad)	{
   1.435 +					bHad = true;
   1.436 +					++lineNumber;
   1.437 +				}
   1.438 +			}
   1.439 +			else if (*in == '\t' || *in == ' ')bHad = false;
   1.440 +			else break;
   1.441 +			in++;
   1.442 +		}
   1.443 +		*out = in;
   1.444 +		return *in != '\0';
   1.445 +	}
   1.446 +	// -------------------------------------------------------------------
   1.447 +	bool SkipSpacesAndLineEnd( )
   1.448 +	{
   1.449 +		return SkipSpacesAndLineEnd(buffer,(const char**)&buffer);
   1.450 +	}
   1.451 +	// -------------------------------------------------------------------
   1.452 +	bool SkipSpaces( )
   1.453 +	{
   1.454 +		return Assimp::SkipSpaces((const char**)&buffer);
   1.455 +	}
   1.456 +
   1.457 +	char* buffer;
   1.458 +	unsigned int fileSize;
   1.459 +	unsigned int lineNumber;
   1.460 +};
   1.461 +}}
   1.462 +
   1.463 +#endif // AI_MD5PARSER_H_INCLUDED