- Cal3D 0.11 API Reference -

loader.h

00001 //****************************************************************************//
00002 // loader.h                                                                   //
00003 // Copyright (C) 2001, 2002 Bruno 'Beosil' Heidelberger                       //
00004 //****************************************************************************//
00005 // This library is free software; you can redistribute it and/or modify it    //
00006 // under the terms of the GNU Lesser General Public License as published by   //
00007 // the Free Software Foundation; either version 2.1 of the License, or (at    //
00008 // your option) any later version.                                            //
00009 //****************************************************************************//
00010 
00011 #ifndef CAL_LOADER_H
00012 #define CAL_LOADER_H
00013 
00014 //****************************************************************************//
00015 // Includes                                                                   //
00016 //****************************************************************************//
00017 
00018 
00019 #include <string>
00020 #include <istream>
00021 #include "cal3d/global.h"
00022 #include "cal3d/datasource.h"
00023 #include "cal3d/coreanimation.h"
00024 #include "cal3d/corematerial.h"
00025 #include "cal3d/coremesh.h"
00026 #include "cal3d/coreskeleton.h"
00027 #include "cal3d/tinyxml.h"
00028 
00029 //****************************************************************************//
00030 // Forward declarations                                                       //
00031 //****************************************************************************//
00032 
00033 class CalCoreModel;
00034 class CalCoreBone;
00035 class CalCoreAnimation;
00036 class CalCoreAnimatedMorph;
00037 class CalCoreTrack;
00038 class CalCoreKeyframe;
00039 class CalCoreMorphTrack;
00040 class CalCoreMorphKeyframe;
00041 class CalCoreMesh;
00042 class CalCoreSubmesh;
00043 class CalCoreMaterial;
00044 class CalVector;
00045 class CalQuaternion;
00046 
00047 namespace cal3d
00048 {
00049     class TiXmlDocument;
00050 }
00051 
00052 enum
00053 {
00054     LOADER_ROTATE_X_AXIS = 1,
00055     LOADER_INVERT_V_COORD = 2,
00056     LOADER_FLIP_WINDING = 4
00057 };
00058 
00059 
00060 bool CAL3D_API CalVectorFromDataSrc( CalDataSource & dataSrc, CalVector * calVec );
00061 
00062 
00063  /*****************************************************************************/
00067 class CAL3D_API CalLoader
00068 {
00069 // member functions
00070 public:
00071   static unsigned int const keyframeBitsPerOriComponent;
00072   static unsigned int const keyframeBitsPerTime;
00073 
00074   static unsigned int const keyframeBitsPerUnsignedPosComponent;
00075   static unsigned int const keyframeBitsPerPosPadding;
00076   static float const keyframePosRange;
00077   static unsigned int const keyframePosBytes;
00078 
00079   static unsigned int const keyframeBitsPerUnsignedPosComponentSmall;
00080   static unsigned int const keyframeBitsPerPosPaddingSmall;
00081   static float const keyframePosRangeSmall;
00082   static unsigned int const keyframePosBytesSmall;
00083   static CalCoreAnimationPtr loadCoreAnimation(const std::string& strFilename, CalCoreSkeleton *skel=NULL);
00084   static CalCoreMaterialPtr  loadCoreMaterial(const std::string& strFilename);
00085   static CalCoreMeshPtr      loadCoreMesh(const std::string& strFilename);
00086   static CalCoreSkeletonPtr  loadCoreSkeleton(const std::string& strFilename);
00087 
00088   static CalCoreAnimatedMorph *loadCoreAnimatedMorph(const std::string& strFilename);
00089 
00090   static CalCoreAnimationPtr loadCoreAnimation(std::istream& inputStream, CalCoreSkeleton *skel=NULL);
00091   static CalCoreAnimatedMorph *loadCoreAnimatedMorph(std::istream& inputStream);
00092   static CalCoreMaterialPtr  loadCoreMaterial(std::istream& inputStream);
00093   static CalCoreMeshPtr      loadCoreMesh(std::istream& inputStream);
00094   static CalCoreSkeletonPtr  loadCoreSkeleton(std::istream& inputStream);
00095 
00096   static CalCoreAnimationPtr loadCoreAnimation(void* inputBuffer, CalCoreSkeleton *skel=NULL);
00097   static CalCoreAnimatedMorph *loadCoreAnimatedMorphFromBuffer(void* inputBuffer, unsigned int len);
00098   static CalCoreMaterialPtr  loadCoreMaterial(void* inputBuffer);
00099   static CalCoreMeshPtr      loadCoreMesh(void* inputBuffer);
00100   static CalCoreSkeletonPtr  loadCoreSkeleton(void* inputBuffer);
00101 
00102   static CalCoreAnimationPtr loadCoreAnimation(CalDataSource& inputSrc, CalCoreSkeleton *skel=NULL);
00103   static CalCoreAnimatedMorph *loadCoreAnimatedMorph(CalDataSource& inputSrc);
00104   static CalCoreMaterialPtr  loadCoreMaterial(CalDataSource& inputSrc);
00105   static CalCoreMeshPtr      loadCoreMesh(CalDataSource& inputSrc);
00106   static CalCoreSkeletonPtr  loadCoreSkeleton(CalDataSource& inputSrc);
00107 
00108   static void setLoadingMode(int flags);
00109   static void setAnimationCollapseSequencesOn( bool p );
00110   static void setAnimationLoadingCompressionOn( bool p );
00111   static void setAnimationTranslationTolerance( double p );
00112   static void setAnimationRotationToleranceDegrees( double p );
00113 
00114   static bool getAnimationLoadingCompressionOn() { return loadingCompressionOn; }
00115   static CalCoreKeyframe *loadCoreKeyframe(CalDataSource& dataSrc);
00116   static CalCoreKeyframe *loadCompressedCoreKeyframe(CalDataSource& dataSrc, const CalVector &trackMinPt, const CalVector &trackScale, float trackDuration);
00117   static double getAnimationTranslationTolerance() { return translationTolerance; }
00118   static double getAnimationRotationToleranceDegrees() { return rotationToleranceDegrees; }
00119   static int getAnimationNumEliminatedKeyframes() { return numEliminatedKeyframes; }
00120   static int getAnimationNumKeptKeyframes() { return numKeptKeyframes; }
00121   static int getAnimationNumRoundedKeyframes() { return numRoundedKeyframes; }
00122   static int getAnimationNumCompressedAnimations() { return numCompressedAnimations; }
00123   static void addAnimationCompressionStatistic( int totalKeyframes, int eliminatedKeyframes, int numRounded ) {
00124     numEliminatedKeyframes += eliminatedKeyframes;
00125     numKeptKeyframes += totalKeyframes - eliminatedKeyframes;
00126     numRoundedKeyframes += numRounded;
00127     numCompressedAnimations++;
00128   }
00129   static void resetCompressionStatistics() {
00130     numEliminatedKeyframes = 0;
00131     numKeptKeyframes = 0;
00132     numCompressedAnimations = 0;
00133   }
00134   static bool usesAnimationCompression( int version );
00135   static unsigned int compressedKeyframeRequiredBytes( CalCoreKeyframe * lastCoreKeyframe, bool translationRequired, bool highRangeRequired, bool translationIsDynamic );
00136   static unsigned int readCompressedKeyframe( unsigned char * buf, unsigned int bytes, CalCoreBone * coreboneOrNull,
00137     CalVector * vecResult, CalQuaternion * quatResult, float * timeResult,
00138     CalCoreKeyframe * lastCoreKeyframe,
00139     bool translationRequired, bool highRangeRequired, bool translationIsDynamic,
00140     bool useAnimationCompression);
00141   static unsigned int writeCompressedKeyframe( unsigned char * buf, unsigned int bufLen, const std::string& strFilename, 
00142     CalVector const & translation, CalQuaternion const & rotation, float caltime,
00143     int version, 
00144     bool needTranslation, bool highRangeRequired );
00145   static void compressCoreAnimation( CalCoreAnimation * anim, CalCoreSkeleton *skel );
00146 
00147   // xmlformat.cpp
00148   static CalCoreAnimationPtr loadXmlCoreAnimation(const std::string& strFilename, CalCoreSkeleton *skel=NULL);
00149   static CalCoreAnimationPtr loadXmlCoreAnimation(const char*, CalCoreSkeleton* skel);
00150   static CalCoreAnimationPtr loadXmlCoreAnimation(cal3d::TiXmlDocument & doc, CalCoreSkeleton* skel);
00151   static CalCoreSkeletonPtr loadXmlCoreSkeleton(const std::string& strFilename);
00152   static CalCoreMeshPtr loadXmlCoreMesh(const std::string& strFilename);
00153   static CalCoreMaterialPtr loadXmlCoreMaterial(const std::string& strFilename);
00154   static CalCoreAnimatedMorph *loadXmlCoreAnimatedMorph(const std::string& strFilename);
00155   static CalCoreAnimatedMorph *loadXmlCoreAnimatedMorph(const void *);
00156   static CalCoreAnimatedMorph *loadXmlCoreAnimatedMorph(cal3d::TiXmlDocument & doc);
00157 
00158   static CalCoreSkeletonPtr loadXmlCoreSkeletonFromFile(const std::string& strFilename);
00159   static CalCoreSkeletonPtr loadXmlCoreSkeleton(const void *);
00160 
00161   static CalCoreMeshPtr loadXmlCoreMesh(const void *);
00162 
00163   static CalCoreMaterialPtr loadXmlCoreMaterial(const void *);
00164 
00165 private:
00166   static CalCoreBone *loadCoreBones(CalDataSource& dataSrc, int version);
00167   static CalCoreKeyframe *loadCoreKeyframe(CalDataSource& dataSrc, CalCoreBone * coreboneOrNull, 
00168                                              int version, CalCoreKeyframe * lastCoreKeyframe, 
00169                                              bool translationRequired, bool highRangeRequired, bool translationIsDynamic,
00170                                              bool useAnimationCompression);
00171   static CalCoreMorphKeyframe *loadCoreMorphKeyframe(CalDataSource& dataSrc);
00172   static CalCoreSubmesh *loadCoreSubmesh(CalDataSource& dataSrc, int version);
00173   static CalCoreTrack *loadCoreTrack(CalDataSource & dataSrc, CalCoreSkeleton * skel, int version, bool useAnimationCompresssion);
00174   static CalCoreMorphTrack *loadCoreMorphTrack(CalDataSource& dataSrc);
00175 
00176   static CalCoreSkeletonPtr loadXmlCoreSkeleton(cal3d::TiXmlDocument& doc);
00177   static CalCoreMeshPtr loadXmlCoreMesh(cal3d::TiXmlDocument& doc);
00178   static CalCoreMaterialPtr loadXmlCoreMaterial(cal3d::TiXmlDocument& doc);
00179 
00180   static int loadingMode;
00181   static double translationTolerance;
00182   static double rotationToleranceDegrees;
00183   static bool loadingCompressionOn;
00184   static bool collapseSequencesOn;
00185 
00186   static int numEliminatedKeyframes;
00187   static int numKeptKeyframes;
00188   static int numCompressedAnimations;
00189   static int numRoundedKeyframes;
00190 };
00191 
00192 
00193 class BitWriter {
00194 public:
00195   BitWriter( unsigned char * dest ) {
00196     dest_ = dest;
00197     buf_ = 0;
00198     bitsInBuf_ = 0;
00199     bytesWritten_ = 0;
00200   }
00201   void write( unsigned int data, unsigned int numBits );
00202   void flush();
00203   inline unsigned int bytesWritten() { return bytesWritten_; }
00204 private:
00205   unsigned int buf_;
00206   unsigned int bitsInBuf_;
00207   unsigned int bytesWritten_;
00208   unsigned char * dest_;
00209 };
00210 
00211 
00212 class BitReader {
00213 public:
00214   BitReader( unsigned char const * source ) {
00215     source_ = source;
00216     bitsInBuf_ = 0;
00217     buf_ = 0;
00218     bytesRead_ = 0;
00219   }
00220   inline void read( unsigned int * data, unsigned int numBits );
00221   inline unsigned int bytesRead() { return bytesRead_; }
00222 private:
00223   unsigned int buf_;
00224   unsigned int bitsInBuf_;
00225   unsigned int bytesRead_;
00226   unsigned char const * source_;
00227 };
00228 
00229 inline void
00230 BitReader::read( unsigned int * data, unsigned int numBits )
00231 {
00232 
00233   // Fill up the buffer with enough bits.  I load 8 at a time, only when
00234   // needed, so I can never have more than 7 left over.
00235   assert( bitsInBuf_ < 8 );
00236   while( bitsInBuf_ < numBits ) {
00237     buf_ |= ( source_[ bytesRead_ ] << bitsInBuf_ );
00238     bitsInBuf_ += 8;
00239     bytesRead_++;
00240   }
00241 
00242   // Read the data.
00243   * data = buf_ & ( ( 1 << numBits ) - 1 );
00244   buf_ >>= numBits;
00245   bitsInBuf_ -= numBits;
00246 }
00247 
00248 inline unsigned int
00249 FloatZeroToOneToFixedPoint( float zeroToOne, unsigned int numBits )
00250 {
00251 
00252   // Consider the case of numBits = 2.
00253   // Then maxVal = 3.
00254   // The fractional values convert to fixed point at these thresholds:
00255   //
00256   //    0/1 --> 0
00257   //    1/6 --> 1
00258   //    3/6 --> 2
00259   //    5/6 --> 3
00260   //
00261   // Then they convert back by:
00262   //
00263   //    0 --> 0/1
00264   //    1 --> 1/3
00265   //    2 --> 2/3
00266   //    3 --> 1/1
00267   //
00268   // Note that we can represent the endpoints inclusively.  However, the midway value
00269   // is right on a threshold, and will get rounded up:
00270   //
00271   //    1/2 --> 2 --> 2/3.
00272   //
00273   // Thus while endpoints are represented accurately in fixed point, the midpoint will not be.
00274   unsigned int maxVal = ( 1 << numBits ) - 1;
00275   return ( unsigned int ) ( maxVal * zeroToOne + 0.5f );
00276 }
00277 
00278 inline float
00279 FixedPointToFloatZeroToOne( unsigned int fixedPoint, unsigned int numBits )
00280 {
00281   unsigned int maxVal = ( 1 << numBits ) - 1;
00282   return ( float ) fixedPoint / maxVal;
00283 }
00284 
00285 
00286 
00287 // Return the number of bytes written.
00288 inline unsigned int
00289 WriteQuatAndExtra( unsigned char * dest, float const * vals, unsigned int extra,
00290                  unsigned int bitsPerComponent, unsigned int bitsPerExtra )
00291 {
00292   float absVals[] = { fabsf( vals[ 0 ] ), fabsf( vals[ 1 ] ), fabsf( vals[ 2 ] ), fabsf( vals[ 3 ] ) };
00293 
00294   // Calculate largest magnitude component.
00295   unsigned int i;
00296   unsigned int bigi = 0;
00297   float biggest = absVals[ bigi ];
00298   for( i = 1; i < 4; i++ ) {
00299     if( absVals[ i ] > biggest ) {
00300       biggest = absVals[ i ];
00301       bigi = i;
00302     }
00303   }
00304 
00305   // If largest component is negative, reverse sign of all components including largest, so
00306   // that I can assume largest in result is non-negative.
00307   unsigned int signOne = ( vals[ bigi ] < 0 ) ? 0 : 1;
00308   unsigned int signZero = 1 - signOne;
00309 
00310   // Format: 
00311   // selection (2), 
00312   // asign (1), afixed (n), 
00313   // bsign (1), bfixed (n), 
00314   // csign (1), cfixed (n), 
00315   // extra
00316   BitWriter bw( dest );
00317   bw.write( bigi, 2 );
00318   for( i = 0; i < 4; i++ ) {
00319     if( i != bigi ) {
00320 
00321       // Add the sign bit for the component.
00322       if( vals[ i ] < 0 ) {
00323         bw.write( signOne, 1 );
00324       } else {
00325         bw.write( signZero, 1 );
00326       }
00327 
00328       // Add the fixed point bits.
00329       bw.write( FloatZeroToOneToFixedPoint( absVals[ i ], bitsPerComponent ), bitsPerComponent );
00330     }
00331   }
00332   bw.write( extra, bitsPerExtra );
00333   bw.flush();
00334   return bw.bytesWritten();
00335 }
00336 
00337 
00338 
00339 // Return the number of bytes read.
00340 inline unsigned int
00341 ReadQuatAndExtra( unsigned char const * data, float * valsResult, unsigned int * extraResult,
00342                  unsigned int bitsPerComponent, unsigned int bitsPerExtra )
00343 {
00344   BitReader br( data );
00345   unsigned int bigi;
00346   br.read( & bigi, 2 ); 
00347   unsigned int i;
00348   float sum = 0.0f;
00349   for( i = 0; i < 4; i++ ) {
00350     if( i != bigi ) {
00351       unsigned int sign;
00352       br.read( & sign, 1 );
00353       unsigned int val;
00354       br.read( & val, bitsPerComponent );
00355       float fval = FixedPointToFloatZeroToOne( val, bitsPerComponent );
00356       valsResult[ i ] = sign ? - fval : fval;
00357       sum += valsResult[ i ] * valsResult[ i ];
00358     }
00359   }
00360   if( sum > 1.0f ) sum = 1.0f; // Safety for sqrt.
00361   valsResult[ bigi ] = sqrtf( 1.0f - sum );
00362   br.read( extraResult, bitsPerExtra );
00363   return br.bytesRead();
00364 }
00365 
00366 void
00367 SetTranslationInvalid( float * xResult, float * yResult, float * zResult );
00368 void
00369 SetTranslationInvalid( CalVector * result );
00370 bool
00371 TranslationInvalid( CalVector const & result );
00372 
00373 
00374 
00375 #endif
00376 
00377 //****************************************************************************//

Generated by The Cal3D Team with Doxygen 1.7.3