Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

animadead.h

Go to the documentation of this file.
00001 #ifndef MODEL_H
00002 #define MODEL_H
00003 /*
00004         Animadead: A Skeletal Animation Library
00005         Copyright (C) 2004 John C. Butterfield
00006 
00007         This library is free software; you can redistribute it and/or
00008         modify it under the terms of the GNU Lesser General Public
00009         License as published by the Free Software Foundation; either
00010         version 2.1 of the License, or (at your option) any later version.
00011 
00012         This library is distributed in the hope that it will be useful,
00013         but WITHOUT ANY WARRANTY; without even the implied warranty of
00014         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00015         Lesser General Public License for more details.
00016 
00017         You should have received a copy of the GNU Lesser General Public
00018         License along with this library; if not, write to the Free Software
00019         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00020 
00021         John C. Butterfield
00022         johnb003@hotmail.com
00023 */
00024 
00025 #include "vertex.h"
00026 
00027 #include <string>
00028 #include <vector>
00029 #include <map>
00030 #include <cstdlib>
00031 #include <cstdio>
00032 
00033 namespace jb
00034 {
00035 /**************************************************************************************
00036         Mesh Stuff
00037 **************************************************************************************/
00038         struct SuperVertex                                                      // this is is all the information about each vertex
00039         {
00040                 float x,y,z,a,b,c,u,v;                                  // x,y,z is the point, a,b,c is the normal, and u,v are the texture coordinates
00041                 float *weights;                                                 // the size is Mesh::numInfluencestores, the index is the bone, and the value is the weight
00042                 std::vector<unsigned int> nonzeros;             // nonzeros is a list of all of the indicies of bones that have some influence on this vertex
00043 
00044                 SuperVertex();
00045                 ~SuperVertex();
00046         };
00047 
00048         struct Face
00049         {
00050                 unsigned int numIndices;
00051                 unsigned int *indices;                                  // refers to the index of the vertex
00052 
00053                 Face();
00054                 ~Face();
00055         };
00056 
00057         class UserFunction
00058         {
00059         public:
00060                 virtual void operator()(std::string filename, void *&userdata) {}
00061         };
00062 
00063         struct Surface                                                          // a surface is a mesh that contains a uv map, a texture and the mesh data.
00064         {                                                                                       // it's abstracted from a 'Mesh' to allow different textures within the same model for more detail
00065                 unsigned int nameLength;
00066                 char *texturename;                                              // every time a texture filename is read from the file, it passes it and the user data to the user function
00067                 unsigned int numVerts;
00068                 SuperVertex *superVerts;
00069                 unsigned int numFaces;
00070                 void *userData;                                                 // this can be used to store information about the surface. It is called when the user function is called
00071                 Face *faces;
00072 
00073                 Surface();
00074                 void scaleto(const Surface &cp);                // used to create a bare structure with just enough space to accomidate data that changes between keyframes.
00075                 ~Surface();
00076         };
00077 
00078         struct Mesh
00079         {
00080                 unsigned int numInfluence;                              // number of bones that affect this mesh (if 0, mesh is static)
00081                 unsigned int *influences;                               // the list of influences (if any) are the indicies of the bones relivant to this model
00082                 unsigned int numSurfaces;
00083                 Surface *surfaces;
00084 
00085                 Mesh(std::string filename, UserFunction *loader = 0); // Loads a mesh from a .jbm file
00086                 Mesh();
00087                 ~Mesh();
00088                 void scaleto(const Mesh &cp);                   // same as scaleto for Surface
00089         };
00090 
00091         class MeshCacher                                                        // loads a mesh and keeps track of the instances of previously loaded meshes.
00092         {
00093                 MeshCacher();                                                   // singleton, use MeshCacher::instance() to use it.
00094                 static MeshCacher *inst;
00095                 std::map<std::string, Mesh *> cache;
00096 
00097         public:
00098                 ~MeshCacher();
00099                 static MeshCacher *instance();
00100                 void release();
00101                 Mesh *GetMesh(std::string filename, UserFunction *fp = 0);      // returns instance of loaded .jbm file, or loads it if it isn't already.
00102         };
00103 
00104 /**************************************************************************************
00105         Anim Stuff
00106 **************************************************************************************/
00107         struct Matrix                                                           // used to represent the orientation of each bone
00108         {
00109                 float matrix[4][4];
00110         };
00111 
00112         struct Frame                                                            // a single frame of an animation
00113         {
00114                 Matrix *bones;
00115                 Vertex keyframeVelocity;                                // this vector represents the offset of the root bone from the previous frame to the current
00116 
00117                 Frame();
00118                 ~Frame();
00119         };
00120 
00121         struct Anim                                                                     // a collection of sampled frames making up an animation
00122         {
00123                 unsigned int numFrames;
00124                 Frame *frames;
00125 
00126                 Anim(std::string filename);                             // loads a .jba file
00127                 ~Anim();
00128         };
00129 
00130         class AnimCacher                                                        // loads an animation and keeps track of the instances of previously loaded animations.
00131         {
00132                 AnimCacher();                                                   // singleton, use MeshCacher::instance() to use it.
00133                 static AnimCacher *inst;
00134                 std::map<std::string, Anim *> cache;
00135 
00136         public:
00137                 ~AnimCacher();
00138 
00139                 static AnimCacher *instance();
00140                 void release();
00141                 Anim *GetAnim(std::string filename);    // returns instance of loaded .jba file, or loads it if it isn't already.
00142         };
00143 
00144         class AnimPlayer                                                        // needed to play a single instance of an animation at different frames.
00145         {
00146                 float timeweight;
00147                 Vertex *currentPosition;
00148                 Vertex *lastPosition;
00149                 bool _animFinished;
00150 
00151         public:
00152                 unsigned int previousFrame;
00153                 Anim *anim;
00154 
00155                 AnimPlayer(Anim *anim, unsigned int startFrame = 0);
00156                 ~AnimPlayer();
00157                 void GetKeyframeVelocity(Vertex *tempVel);
00158                 void GetVelocity(Vertex *tempVel);
00159                 void Update(float time);
00160                 void Update(unsigned int framesPassed);
00161                 bool getAnimFinished() { return _animFinished; }
00162                 void reset() { previousFrame = 0; }
00163         };
00164 
00165 /**************************************************************************************
00166         Model Base
00167 **************************************************************************************/
00168         enum ModelType {
00169                 STATIC,
00170                 DEFORMABLE
00171         };
00172 
00173         class Model
00174         {
00175         protected:
00176                 bool originSubtraction;
00177                 float timeweight;
00178                 AnimPlayer *velocityControl;
00179                 AnimPlayer *_currentAnim;
00180                         // a model can be made up of several meshes
00181                 std::vector<Mesh *> bindMesh;
00182                 std::vector<Mesh *> *previousMesh;
00183                 std::vector<Mesh *> *nextMesh;
00184                 std::vector<AnimPlayer *> anims;
00185                 std::vector<unsigned int> animFromBone;
00186                 UserFunction *fp;
00187                 ModelType type;
00188         public:
00189                 Model(ModelType type, UserFunction *texture_loader = 0);
00190                 virtual ~Model();
00191                 unsigned int AddMesh(std::string filename);
00192                 void SwapMesh(unsigned int index, std::string filename);
00193                 unsigned int AddAnim(std::string filename, unsigned int startFrame = 0);
00194                 void SwapAnim(unsigned int index, std::string filename, unsigned int startFrame = 0);
00195                 void GetTransform(unsigned int bone, Matrix &matrix);
00196                 void Bind(unsigned int bone, unsigned int anim);
00197                 void MovingOrigin(bool);
00198                 void OriginAnim(unsigned int index);
00199                 void GetKeyframeVelocity(Vertex *velocity);
00200                 void GetVelocity(Vertex *velocity);
00201                 void Update(float timestep);            // time in seconds.
00202                 void UpdateMeshes();
00203                 void Reset() { _currentAnim->reset(); }
00204                 bool getAnimFinished() { if (_currentAnim) return _currentAnim->getAnimFinished(); else return true; }
00205         };
00206 }
00207 
00208 #endif // MODEL_H
00209 

Generated on Wed Apr 21 11:10:53 2004 for naturecalls by doxygen1.2.18