- Cal3D 0.11 API Reference -

loader.h
1//****************************************************************************//
2// loader.h //
3// Copyright (C) 2001, 2002 Bruno 'Beosil' Heidelberger //
4//****************************************************************************//
5// This library is free software; you can redistribute it and/or modify it //
6// under the terms of the GNU Lesser General Public License as published by //
7// the Free Software Foundation; either version 2.1 of the License, or (at //
8// your option) any later version. //
9//****************************************************************************//
10
11#ifndef CAL_LOADER_H
12#define CAL_LOADER_H
13
14//****************************************************************************//
15// Includes //
16//****************************************************************************//
17
18
19#include <string>
20#include <istream>
21#include "cal3d/global.h"
22#include "cal3d/datasource.h"
23#include "cal3d/coreanimation.h"
24#include "cal3d/corematerial.h"
25#include "cal3d/coremesh.h"
26#include "cal3d/coreskeleton.h"
27#include "cal3d/tinyxml.h"
28
29//****************************************************************************//
30// Forward declarations //
31//****************************************************************************//
32
33class CalCoreModel;
34class CalCoreBone;
37class CalCoreTrack;
38class CalCoreKeyframe;
41class CalCoreMesh;
42class CalCoreSubmesh;
43class CalCoreMaterial;
44class CalVector;
45class CalQuaternion;
46
47namespace cal3d
48{
49 class TiXmlDocument;
50}
51
52enum
53{
54 LOADER_ROTATE_X_AXIS = 1,
55 LOADER_INVERT_V_COORD = 2,
56 LOADER_FLIP_WINDING = 4
57};
58
59
60bool CAL3D_API CalVectorFromDataSrc( CalDataSource & dataSrc, CalVector * calVec );
61
62
63 /*****************************************************************************/
67class CAL3D_API CalLoader
68{
69// member functions
70public:
71 static unsigned int const keyframeBitsPerOriComponent;
72 static unsigned int const keyframeBitsPerTime;
73
74 static unsigned int const keyframeBitsPerUnsignedPosComponent;
75 static unsigned int const keyframeBitsPerPosPadding;
76 static float const keyframePosRange;
77 static unsigned int const keyframePosBytes;
78
79 static unsigned int const keyframeBitsPerUnsignedPosComponentSmall;
80 static unsigned int const keyframeBitsPerPosPaddingSmall;
81 static float const keyframePosRangeSmall;
82 static unsigned int const keyframePosBytesSmall;
83 static CalCoreAnimationPtr loadCoreAnimation(const std::string& strFilename, CalCoreSkeleton *skel=NULL);
84 static CalCoreMaterialPtr loadCoreMaterial(const std::string& strFilename);
85 static CalCoreMeshPtr loadCoreMesh(const std::string& strFilename);
86 static CalCoreSkeletonPtr loadCoreSkeleton(const std::string& strFilename);
87
88 static CalCoreAnimatedMorph *loadCoreAnimatedMorph(const std::string& strFilename);
89
90 static CalCoreAnimationPtr loadCoreAnimation(std::istream& inputStream, CalCoreSkeleton *skel=NULL);
91 static CalCoreAnimatedMorph *loadCoreAnimatedMorph(std::istream& inputStream);
92 static CalCoreMaterialPtr loadCoreMaterial(std::istream& inputStream);
93 static CalCoreMeshPtr loadCoreMesh(std::istream& inputStream);
94 static CalCoreSkeletonPtr loadCoreSkeleton(std::istream& inputStream);
95
96 static CalCoreAnimationPtr loadCoreAnimation(void* inputBuffer, CalCoreSkeleton *skel=NULL);
97 static CalCoreAnimatedMorph *loadCoreAnimatedMorphFromBuffer(void* inputBuffer, unsigned int len);
98 static CalCoreMaterialPtr loadCoreMaterial(void* inputBuffer);
99 static CalCoreMeshPtr loadCoreMesh(void* inputBuffer);
100 static CalCoreSkeletonPtr loadCoreSkeleton(void* inputBuffer);
101
102 static CalCoreAnimationPtr loadCoreAnimation(CalDataSource& inputSrc, CalCoreSkeleton *skel=NULL);
103 static CalCoreAnimatedMorph *loadCoreAnimatedMorph(CalDataSource& inputSrc);
104 static CalCoreMaterialPtr loadCoreMaterial(CalDataSource& inputSrc);
105 static CalCoreMeshPtr loadCoreMesh(CalDataSource& inputSrc);
106 static CalCoreSkeletonPtr loadCoreSkeleton(CalDataSource& inputSrc);
107
108 static void setLoadingMode(int flags);
109 static void setAnimationCollapseSequencesOn( bool p );
110 static void setAnimationLoadingCompressionOn( bool p );
111 static void setAnimationTranslationTolerance( double p );
112 static void setAnimationRotationToleranceDegrees( double p );
113
114 static bool getAnimationLoadingCompressionOn() { return loadingCompressionOn; }
115 static CalCoreKeyframe *loadCoreKeyframe(CalDataSource& dataSrc);
116 static CalCoreKeyframe *loadCompressedCoreKeyframe(CalDataSource& dataSrc, const CalVector &trackMinPt, const CalVector &trackScale, float trackDuration);
117 static double getAnimationTranslationTolerance() { return translationTolerance; }
118 static double getAnimationRotationToleranceDegrees() { return rotationToleranceDegrees; }
119 static int getAnimationNumEliminatedKeyframes() { return numEliminatedKeyframes; }
120 static int getAnimationNumKeptKeyframes() { return numKeptKeyframes; }
121 static int getAnimationNumRoundedKeyframes() { return numRoundedKeyframes; }
122 static int getAnimationNumCompressedAnimations() { return numCompressedAnimations; }
123 static void addAnimationCompressionStatistic( int totalKeyframes, int eliminatedKeyframes, int numRounded ) {
124 numEliminatedKeyframes += eliminatedKeyframes;
125 numKeptKeyframes += totalKeyframes - eliminatedKeyframes;
126 numRoundedKeyframes += numRounded;
127 numCompressedAnimations++;
128 }
129 static void resetCompressionStatistics() {
130 numEliminatedKeyframes = 0;
131 numKeptKeyframes = 0;
132 numCompressedAnimations = 0;
133 }
134 static bool usesAnimationCompression( int version );
135 static unsigned int compressedKeyframeRequiredBytes( CalCoreKeyframe * lastCoreKeyframe, bool translationRequired, bool highRangeRequired, bool translationIsDynamic );
136 static unsigned int readCompressedKeyframe( unsigned char * buf, unsigned int bytes, CalCoreBone * coreboneOrNull,
137 CalVector * vecResult, CalQuaternion * quatResult, float * timeResult,
138 CalCoreKeyframe * lastCoreKeyframe,
139 bool translationRequired, bool highRangeRequired, bool translationIsDynamic,
140 bool useAnimationCompression);
141 static unsigned int writeCompressedKeyframe( unsigned char * buf, unsigned int bufLen, const std::string& strFilename,
142 CalVector const & translation, CalQuaternion const & rotation, float caltime,
143 int version,
144 bool needTranslation, bool highRangeRequired );
145 static void compressCoreAnimation( CalCoreAnimation * anim, CalCoreSkeleton *skel );
146
147 // xmlformat.cpp
148 static CalCoreAnimationPtr loadXmlCoreAnimation(const std::string& strFilename, CalCoreSkeleton *skel=NULL);
149 static CalCoreAnimationPtr loadXmlCoreAnimation(const char*, CalCoreSkeleton* skel);
150 static CalCoreAnimationPtr loadXmlCoreAnimation(cal3d::TiXmlDocument & doc, CalCoreSkeleton* skel);
151 static CalCoreSkeletonPtr loadXmlCoreSkeleton(const std::string& strFilename);
152 static CalCoreMeshPtr loadXmlCoreMesh(const std::string& strFilename);
153 static CalCoreMaterialPtr loadXmlCoreMaterial(const std::string& strFilename);
154 static CalCoreAnimatedMorph *loadXmlCoreAnimatedMorph(const std::string& strFilename);
155 static CalCoreAnimatedMorph *loadXmlCoreAnimatedMorph(const void *);
156 static CalCoreAnimatedMorph *loadXmlCoreAnimatedMorph(cal3d::TiXmlDocument & doc);
157
158 static CalCoreSkeletonPtr loadXmlCoreSkeletonFromFile(const std::string& strFilename);
159 static CalCoreSkeletonPtr loadXmlCoreSkeleton(const void *);
160
161 static CalCoreMeshPtr loadXmlCoreMesh(const void *);
162
163 static CalCoreMaterialPtr loadXmlCoreMaterial(const void *);
164
165 static CalCoreBone *loadCoreBones(CalDataSource& dataSrc, int version);
166 static CalCoreKeyframe *loadCoreKeyframe(CalDataSource& dataSrc, CalCoreBone * coreboneOrNull,
167 int version, CalCoreKeyframe * lastCoreKeyframe,
168 bool translationRequired, bool highRangeRequired, bool translationIsDynamic,
169 bool useAnimationCompression);
170 static CalCoreMorphKeyframe *loadCoreMorphKeyframe(CalDataSource& dataSrc);
171 static CalCoreSubmesh *loadCoreSubmesh(CalDataSource& dataSrc, int version);
172 static CalCoreTrack *loadCoreTrack(CalDataSource & dataSrc, CalCoreSkeleton * skel, int version, bool useAnimationCompresssion);
173 static CalCoreMorphTrack *loadCoreMorphTrack(CalDataSource& dataSrc);
174
175 static CalCoreSkeletonPtr loadXmlCoreSkeleton(cal3d::TiXmlDocument& doc);
176 static CalCoreMeshPtr loadXmlCoreMesh(cal3d::TiXmlDocument& doc);
177 static CalCoreMaterialPtr loadXmlCoreMaterial(cal3d::TiXmlDocument& doc);
178
179private:
180 static int loadingMode;
181 static double translationTolerance;
182 static double rotationToleranceDegrees;
183 static bool loadingCompressionOn;
184 static bool collapseSequencesOn;
185
186 static int numEliminatedKeyframes;
187 static int numKeptKeyframes;
188 static int numCompressedAnimations;
189 static int numRoundedKeyframes;
190};
191
192
194public:
195 BitWriter( unsigned char * dest ) {
196 dest_ = dest;
197 buf_ = 0;
198 bitsInBuf_ = 0;
199 bytesWritten_ = 0;
200 }
201 void write( unsigned int data, unsigned int numBits );
202 void flush();
203 inline unsigned int bytesWritten() { return bytesWritten_; }
204private:
205 unsigned int buf_;
206 unsigned int bitsInBuf_;
207 unsigned int bytesWritten_;
208 unsigned char * dest_;
209};
210
211
213public:
214 BitReader( unsigned char const * source ) {
215 source_ = source;
216 bitsInBuf_ = 0;
217 buf_ = 0;
218 bytesRead_ = 0;
219 }
220 inline void read( unsigned int * data, unsigned int numBits );
221 inline unsigned int bytesRead() { return bytesRead_; }
222private:
223 unsigned int buf_;
224 unsigned int bitsInBuf_;
225 unsigned int bytesRead_;
226 unsigned char const * source_;
227};
228
229inline void
230BitReader::read( unsigned int * data, unsigned int numBits )
231{
232
233 // Fill up the buffer with enough bits. I load 8 at a time, only when
234 // needed, so I can never have more than 7 left over.
235 assert( bitsInBuf_ < 8 );
236 while( bitsInBuf_ < numBits ) {
237 buf_ |= ( source_[ bytesRead_ ] << bitsInBuf_ );
238 bitsInBuf_ += 8;
239 bytesRead_++;
240 }
241
242 // Read the data.
243 * data = buf_ & ( ( 1 << numBits ) - 1 );
244 buf_ >>= numBits;
245 bitsInBuf_ -= numBits;
246}
247
248inline unsigned int
249FloatZeroToOneToFixedPoint( float zeroToOne, unsigned int numBits )
250{
251
252 // Consider the case of numBits = 2.
253 // Then maxVal = 3.
254 // The fractional values convert to fixed point at these thresholds:
255 //
256 // 0/1 --> 0
257 // 1/6 --> 1
258 // 3/6 --> 2
259 // 5/6 --> 3
260 //
261 // Then they convert back by:
262 //
263 // 0 --> 0/1
264 // 1 --> 1/3
265 // 2 --> 2/3
266 // 3 --> 1/1
267 //
268 // Note that we can represent the endpoints inclusively. However, the midway value
269 // is right on a threshold, and will get rounded up:
270 //
271 // 1/2 --> 2 --> 2/3.
272 //
273 // Thus while endpoints are represented accurately in fixed point, the midpoint will not be.
274 unsigned int maxVal = ( 1 << numBits ) - 1;
275 return ( unsigned int ) ( maxVal * zeroToOne + 0.5f );
276}
277
278inline float
279FixedPointToFloatZeroToOne( unsigned int fixedPoint, unsigned int numBits )
280{
281 unsigned int maxVal = ( 1 << numBits ) - 1;
282 return ( float ) fixedPoint / maxVal;
283}
284
285
286
287// Return the number of bytes written.
288inline unsigned int
289WriteQuatAndExtra( unsigned char * dest, float const * vals, unsigned int extra,
290 unsigned int bitsPerComponent, unsigned int bitsPerExtra )
291{
292 float absVals[] = { fabsf( vals[ 0 ] ), fabsf( vals[ 1 ] ), fabsf( vals[ 2 ] ), fabsf( vals[ 3 ] ) };
293
294 // Calculate largest magnitude component.
295 unsigned int i;
296 unsigned int bigi = 0;
297 float biggest = absVals[ bigi ];
298 for( i = 1; i < 4; i++ ) {
299 if( absVals[ i ] > biggest ) {
300 biggest = absVals[ i ];
301 bigi = i;
302 }
303 }
304
305 // If largest component is negative, reverse sign of all components including largest, so
306 // that I can assume largest in result is non-negative.
307 unsigned int signOne = ( vals[ bigi ] < 0 ) ? 0 : 1;
308 unsigned int signZero = 1 - signOne;
309
310 // Format:
311 // selection (2),
312 // asign (1), afixed (n),
313 // bsign (1), bfixed (n),
314 // csign (1), cfixed (n),
315 // extra
316 BitWriter bw( dest );
317 bw.write( bigi, 2 );
318 for( i = 0; i < 4; i++ ) {
319 if( i != bigi ) {
320
321 // Add the sign bit for the component.
322 if( vals[ i ] < 0 ) {
323 bw.write( signOne, 1 );
324 } else {
325 bw.write( signZero, 1 );
326 }
327
328 // Add the fixed point bits.
329 bw.write( FloatZeroToOneToFixedPoint( absVals[ i ], bitsPerComponent ), bitsPerComponent );
330 }
331 }
332 bw.write( extra, bitsPerExtra );
333 bw.flush();
334 return bw.bytesWritten();
335}
336
337
338
339// Return the number of bytes read.
340inline unsigned int
341ReadQuatAndExtra( unsigned char const * data, float * valsResult, unsigned int * extraResult,
342 unsigned int bitsPerComponent, unsigned int bitsPerExtra )
343{
344 BitReader br( data );
345 unsigned int bigi;
346 br.read( & bigi, 2 );
347 unsigned int i;
348 float sum = 0.0f;
349 for( i = 0; i < 4; i++ ) {
350 if( i != bigi ) {
351 unsigned int sign;
352 br.read( & sign, 1 );
353 unsigned int val;
354 br.read( & val, bitsPerComponent );
355 float fval = FixedPointToFloatZeroToOne( val, bitsPerComponent );
356 valsResult[ i ] = sign ? - fval : fval;
357 sum += valsResult[ i ] * valsResult[ i ];
358 }
359 }
360 if( sum > 1.0f ) sum = 1.0f; // Safety for sqrt.
361 valsResult[ bigi ] = sqrtf( 1.0f - sum );
362 br.read( extraResult, bitsPerExtra );
363 return br.bytesRead();
364}
365
366void
367SetTranslationInvalid( float * xResult, float * yResult, float * zResult );
368void
369SetTranslationInvalid( CalVector * result );
370bool
371TranslationInvalid( CalVector const & result );
372
373
374
375#endif
376
377//****************************************************************************//
Definition loader.h:212
Definition loader.h:193
The core animatedMorph class.
Definition coreanimatedmorph.h:37
Definition coreanimation.h:24
Definition corebone.h:35
The core keyframe class.
Definition corekeyframe.h:32
Definition corematerial.h:23
Definition coremesh.h:24
Definition coremodel.h:26
The core keyframe class.
Definition coremorphkeyframe.h:32
Definition coremorphtrack.h:39
Definition coreskeleton.h:25
Definition coresubmesh.h:32
Definition coretrack.h:27
CalDataSource abstract interface class.
Definition datasource.h:31
The loader class.
Definition loader.h:68
The quaternion class.
Definition quaternion.h:36
The vector class.
Definition vector.h:37
A container-safe smart pointer used for refcounted classes.
Definition refptr.h:11
Always the top level node.
Definition tinyxml.h:991

Generated by The Cal3D Team with Doxygen 1.10.0