Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 #ifndef __MD3MODEL_H__
00019 #define __MD3MODEL_H__
00020 
00021 #include <fstream>
00022 #include <stdexcept>
00023 #include <string>
00024 #include <vector>
00025 #include <map>
00026 
00027 #include "Mathlib.h"
00028 #include "Texture.h"
00029 
00030 #include "threadsafe/smart_ptr.h"   
00031 
00032 using std::string;
00033 using std::vector;
00034 using std::map;
00035 
00036 
00037 
00038 const int kMd3MaxFrames = 1024;
00039 const int kMd3MaxTags = 16;
00040 const int kMd3MaxMeshes = 32;
00041 const int kMd3MaxShaders = 256;
00042 const int kMd3MaxTriangles = 8192;
00043 const int kMd3MaxVertices = 4096;
00044 const float kMd3XYZScale = 1.0f / 64.0f;
00045 
00046 
00047 
00048 typedef GLfloat vec2_t[2];
00049 typedef GLfloat vec3_t[3];
00050 
00051 
00052 
00053 struct Md3Header_t
00054 {
00055   int ident;            
00056   int version;          
00057 
00058   char name[64];        
00059   int flags;            
00060 
00061   int num_frames;       
00062   int num_tags;         
00063   int num_meshes;       
00064   int num_skins;        
00065 
00066   int offset_frames;    
00067   int offset_tag;       
00068   int offset_meshes;    
00069   int offset_eof;       
00070 };
00071 
00072 
00073 
00074 struct Md3Frame_t
00075 {
00076   vec3_t min_bounds;    
00077   vec3_t max_bounds;    
00078   vec3_t local_origin;
00079 
00080   float radius;         
00081   char creator[16];
00082 };
00083 
00084 
00085 
00086 struct Md3Tag_t
00087 {
00088   char name[64];
00089   vec3_t origin;        
00090   float axis[3][3];     
00091 };
00092 
00093 
00094 
00095 struct Md3MeshHeader_t
00096 {
00097   int ident;            
00098   char name[64];        
00099   int flags;            
00100 
00101   int num_frames;       
00102   int num_shaders;      
00103   int num_verts;        
00104   int num_triangles;    
00105 
00106   int offset_triangles; 
00107   int offset_shaders;   
00108   int offset_st;        
00109   int offset_xyznormal; 
00110   int offset_end;       
00111 };
00112 
00113 
00114 
00115 struct Md3Shader_t
00116 {
00117   char name[64];
00118   int shader_index;
00119 };
00120 
00121 
00122 
00123 struct Md3Triangle_t
00124 {
00125   int index[3];         
00126 };
00127 
00128 
00129 
00130 struct Md3TexCoord_t
00131 {
00132   float s;
00133   float t;
00134 };
00135 
00136 
00137 
00138 struct Md3Vertex_t
00139 {
00140   short v[3];
00141   unsigned char normal[2];
00142 };
00143 
00144 
00145 
00146 
00147 
00148 
00149 
00150 
00151 
00152 
00153 class Md3Exception : public std::runtime_error
00154 {
00155 public:
00156   
00157   Md3Exception (const string &error)
00158     : std::runtime_error (error) { }
00159   Md3Exception (const string &error, const string &name)
00160     : std::runtime_error (error), _which (name) { }
00161   virtual ~Md3Exception () throw () { }
00162 
00163 public:
00164   
00165   virtual const char *which () const throw () {
00166     return _which.c_str ();
00167   }
00168 
00169 private:
00170   
00171   string _which;
00172 };
00173 
00174 
00175 
00176 
00177 
00178 
00179 
00180 
00181 class NormalLookupTable
00182 {
00183 public:
00184   
00185   NormalLookupTable ();
00186 
00187 public:
00188   
00189   const vec3_t *operator[] (int i) const {
00190     return _normal[i];
00191   }
00192 
00193 private:
00194   
00195   vec3_t _normal[256][256];
00196 };
00197 
00198 
00199 
00200 
00201 
00202 
00203 
00204 
00205 class Md3Mesh
00206 {
00207 public:
00208   
00209   Md3Mesh (std::ifstream &ifs);
00210   ~Md3Mesh ();
00211 
00212 private:
00213   
00214   typedef smart_ptr<Md3Shader_t> Md3ShaderPtr;
00215   typedef smart_ptr<Md3Triangle_t> Md3TrianglePtr;
00216   typedef smart_ptr<Md3Vertex_t> Md3VertexPtr;
00217 
00218 public:
00219   
00220   void loadShader (int index);
00221   void bindTexture () const;
00222   void setupVertexArrays (int frameA, int frameB, float interp, float scale);
00223 
00224   void renderFrameImmediate (int frame, float scale) const;
00225   void renderWithVertexArrays () const;
00226 
00227   void setTexture (const Texture2D *tex) { _tex = tex; }
00228 
00229   
00230   const char *name () const { return _header.name; }
00231 
00232 private:
00233   
00234 
00235   
00236   static const NormalLookupTable _kAnorms;
00237   static const int _kMd3Ident;
00238 
00239   
00240   static vec3_t _kVertexArray[];
00241   static vec3_t _kNormalArray[];
00242 
00243   
00244   Md3MeshHeader_t _header;
00245 
00246   vector<Md3ShaderPtr> _shaders;
00247   vector<Md3TrianglePtr> _triangles;
00248   vector<Md3TexCoord_t> _texCoords;
00249   vector<Md3VertexPtr> _vertices;
00250 
00251   
00252   vector<GLuint> _indices;
00253 
00254   
00255   const Texture2D *_tex;
00256 };
00257 
00258 
00259 
00260 
00261 
00262 
00263 
00264 
00265 
00266 class Md3Model
00267 {
00268 public:
00269   
00270   Md3Model (const string &filename);
00271   ~Md3Model ();
00272 
00273 private:
00274   
00275 
00276   
00277   
00278   struct Md3QuaternionTag
00279   {
00280     
00281     Md3QuaternionTag (const Md3Tag_t &tag);
00282 
00283     string name;
00284     Vector3f origin;
00285     Quaternionf orient;
00286   };
00287 
00288   typedef smart_ptr<Md3Frame_t> Md3FramePtr;
00289   typedef smart_ptr<Md3Mesh> Md3MeshPtr;
00290   typedef smart_ptr<Md3QuaternionTag> Md3TagPtr;
00291 
00292 public:
00293   
00294   void loadShaders ();
00295   void draw () const;
00296   void renderFrameImmediate (int frame) const;
00297   void renderFrameItpWithVertexArrays (int frameA, int frameB,
00298                                        float interp) const;
00299 
00300   bool link (const string &name, const Md3Model *model);
00301   bool unlink (const string &name);
00302   void setScale (float scale) { _scale = scale; }
00303   void setTexture (const string &mesh, const Texture2D *tex);
00304   void setupAnimation (int currFrame, int nextFrame, float interp);
00305 
00306   
00307   int numMeshes () const { return _header.num_meshes; }
00308   int numFrames () const { return _header.num_frames; }
00309   int numTags () const { return _header.num_tags; }
00310   float scale () const { return _scale; }
00311   const string &name () const { return _name; }
00312 
00313 private:
00314   
00315 
00316   
00317   static const int _kMd3Ident;
00318   static const int _kMd3Version;
00319 
00320   
00321   Md3Header_t _header;
00322 
00323   vector<Md3FramePtr> _frames;
00324   vector<Md3MeshPtr> _meshes;
00325   vector<Md3TagPtr> _qtags;
00326   vector<const Md3Model*> _links;
00327 
00328   
00329   
00330   int _currFrame;
00331   int _nextFrame;
00332   float _interp;
00333 
00334   GLfloat _scale;
00335   string _name;
00336 };
00337 
00338 #endif  // __MD3MODEL_H__