00001 #ifndef MODEL_H
00002 #define MODEL_H
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
00037
00038 struct SuperVertex
00039 {
00040 float x,y,z,a,b,c,u,v;
00041 float *weights;
00042 std::vector<unsigned int> nonzeros;
00043
00044 SuperVertex();
00045 ~SuperVertex();
00046 };
00047
00048 struct Face
00049 {
00050 unsigned int numIndices;
00051 unsigned int *indices;
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
00064 {
00065 unsigned int nameLength;
00066 char *texturename;
00067 unsigned int numVerts;
00068 SuperVertex *superVerts;
00069 unsigned int numFaces;
00070 void *userData;
00071 Face *faces;
00072
00073 Surface();
00074 void scaleto(const Surface &cp);
00075 ~Surface();
00076 };
00077
00078 struct Mesh
00079 {
00080 unsigned int numInfluence;
00081 unsigned int *influences;
00082 unsigned int numSurfaces;
00083 Surface *surfaces;
00084
00085 Mesh(std::string filename, UserFunction *loader = 0);
00086 Mesh();
00087 ~Mesh();
00088 void scaleto(const Mesh &cp);
00089 };
00090
00091 class MeshCacher
00092 {
00093 MeshCacher();
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);
00102 };
00103
00104
00105
00106
00107 struct Matrix
00108 {
00109 float matrix[4][4];
00110 };
00111
00112 struct Frame
00113 {
00114 Matrix *bones;
00115 Vertex keyframeVelocity;
00116
00117 Frame();
00118 ~Frame();
00119 };
00120
00121 struct Anim
00122 {
00123 unsigned int numFrames;
00124 Frame *frames;
00125
00126 Anim(std::string filename);
00127 ~Anim();
00128 };
00129
00130 class AnimCacher
00131 {
00132 AnimCacher();
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);
00142 };
00143
00144 class AnimPlayer
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
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
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);
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