Md3Model.h

Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /////////////////////////////////////////////////////////////////////////////
00003 //
00004 // Md3Model.h -- Copyright (c) 2006-2007 David Henry
00005 // last modification: may. 7, 2007
00006 //
00007 // This code is licenced under the MIT license.
00008 //
00009 // This software is provided "as is" without express or implied
00010 // warranties. You may freely copy and compile this source into
00011 // applications you distribute provided that the copyright text
00012 // below is included in the resulting source code.
00013 //
00014 // Definition of MD3 Model Classes.
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"   // we replace boost shared_ptr with this
00031 
00032 using std::string;
00033 using std::vector;
00034 using std::map;
00035 
00036 
00037 // Constants definitions
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 // OpenGL vector types
00048 typedef GLfloat vec2_t[2];
00049 typedef GLfloat vec3_t[3];
00050 
00051 
00052 // MDL Header
00053 struct Md3Header_t
00054 {
00055   int ident;            // Magic number, "IDP3"
00056   int version;          // Md3 format version, should be 15
00057 
00058   char name[64];        // Path name
00059   int flags;            // ?
00060 
00061   int num_frames;       // Number of frames
00062   int num_tags;         // Number of tags
00063   int num_meshes;       // Number of meshes
00064   int num_skins;        // Number of skins
00065 
00066   int offset_frames;    // Offset to frame data
00067   int offset_tag;       // Offset to tag data
00068   int offset_meshes;    // Offset to meshes
00069   int offset_eof;       // Offset end of file
00070 };
00071 
00072 
00073 // Frame data
00074 struct Md3Frame_t
00075 {
00076   vec3_t min_bounds;    // First corner of the bbox
00077   vec3_t max_bounds;    // Second corner of the bbox
00078   vec3_t local_origin;
00079 
00080   float radius;         // Radius of bounding sphere
00081   char creator[16];
00082 };
00083 
00084 
00085 // Tag information
00086 struct Md3Tag_t
00087 {
00088   char name[64];
00089   vec3_t origin;        // Position vector
00090   float axis[3][3];     // Orientation matrix
00091 };
00092 
00093 
00094 // Mesh header
00095 struct Md3MeshHeader_t
00096 {
00097   int ident;            // Magic number, "IDP3"
00098   char name[64];        // Mesh's name
00099   int flags;            // ?
00100 
00101   int num_frames;       // Number of frames
00102   int num_shaders;      // Number of textures
00103   int num_verts;        // Number of vertices per frame
00104   int num_triangles;    // Number of triangles
00105 
00106   int offset_triangles; // Offset to triangle data
00107   int offset_shaders;   // Offset to skin data
00108   int offset_st;        // Offset to texture coords.
00109   int offset_xyznormal; // Offset to vertex data
00110   int offset_end;       // Offset to the end of the mesh
00111 };
00112 
00113 
00114 // Mesh texture
00115 struct Md3Shader_t
00116 {
00117   char name[64];
00118   int shader_index;
00119 };
00120 
00121 
00122 // Triangle data
00123 struct Md3Triangle_t
00124 {
00125   int index[3];         // Vertex indices
00126 };
00127 
00128 
00129 // Texture coordinates
00130 struct Md3TexCoord_t
00131 {
00132   float s;
00133   float t;
00134 };
00135 
00136 
00137 // Compressed vertex data
00138 struct Md3Vertex_t
00139 {
00140   short v[3];
00141   unsigned char normal[2];
00142 };
00143 
00144 
00145 /////////////////////////////////////////////////////////////////////////////
00146 //
00147 // class Md3Exception - Exception class for MD3 loader classes.
00148 // This acts like a standard runtime_error exception but
00149 // know which file or mesh has failed to be loaded.
00150 //
00151 /////////////////////////////////////////////////////////////////////////////
00152 
00153 class Md3Exception : public std::runtime_error
00154 {
00155 public:
00156   // Constructors
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   // Public interface
00165   virtual const char *which () const throw () {
00166     return _which.c_str ();
00167   }
00168 
00169 private:
00170   // Member variables
00171   string _which;
00172 };
00173 
00174 
00175 /////////////////////////////////////////////////////////////////////////////
00176 //
00177 // class NormalLookupTable -- A Normal Lookup Table Class for MD3 Models.
00178 //
00179 /////////////////////////////////////////////////////////////////////////////
00180 
00181 class NormalLookupTable
00182 {
00183 public:
00184   // Constructor
00185   NormalLookupTable ();
00186 
00187 public:
00188   // Accessors
00189   const vec3_t *operator[] (int i) const {
00190     return _normal[i];
00191   }
00192 
00193 private:
00194   // Member variables
00195   vec3_t _normal[256][256];
00196 };
00197 
00198 
00199 /////////////////////////////////////////////////////////////////////////////
00200 //
00201 // class Md3Mesh -- MD3 Mesh Data Class.
00202 //
00203 /////////////////////////////////////////////////////////////////////////////
00204 
00205 class Md3Mesh
00206 {
00207 public:
00208   // Constructor/destructor
00209   Md3Mesh (std::ifstream &ifs);
00210   ~Md3Mesh ();
00211 
00212 private:
00213   // Internal types
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   // Public interface
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   // Accessors
00230   const char *name () const { return _header.name; }
00231 
00232 private:
00233   // Member variables
00234 
00235   // Constants
00236   static const NormalLookupTable _kAnorms;
00237   static const int _kMd3Ident;
00238 
00239   // Vertex arrays (shared by all meshes)
00240   static vec3_t _kVertexArray[];
00241   static vec3_t _kNormalArray[];
00242 
00243   // Mesh data
00244   Md3MeshHeader_t _header;
00245 
00246   vector<Md3ShaderPtr> _shaders;
00247   vector<Md3TrianglePtr> _triangles;
00248   vector<Md3TexCoord_t> _texCoords;
00249   vector<Md3VertexPtr> _vertices;
00250 
00251   // Indices for vertex arrays
00252   vector<GLuint> _indices;
00253 
00254   // Texture
00255   const Texture2D *_tex;
00256 };
00257 
00258 
00259 /////////////////////////////////////////////////////////////////////////////
00260 //
00261 // class Md3Model -- MD3 Model Data Class.  A model is contituted of
00262 // multiple meshes.
00263 //
00264 /////////////////////////////////////////////////////////////////////////////
00265 
00266 class Md3Model
00267 {
00268 public:
00269   // Constructor/destructor
00270   Md3Model (const string &filename);
00271   ~Md3Model ();
00272 
00273 private:
00274   // Internal types
00275 
00276   // Tag information using Quaternion instead
00277   // of a 3x3 rotation matrix
00278   struct Md3QuaternionTag
00279   {
00280     // Constructor
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   // Public interface
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   // Accessors
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   // Member variables
00315 
00316   // Constants
00317   static const int _kMd3Ident;
00318   static const int _kMd3Version;
00319 
00320   // Model data
00321   Md3Header_t _header;
00322 
00323   vector<Md3FramePtr> _frames;
00324   vector<Md3MeshPtr> _meshes;
00325   vector<Md3TagPtr> _qtags;
00326   vector<const Md3Model*> _links;
00327 
00328   // Animation data used for the
00329   // next rendering
00330   int _currFrame;
00331   int _nextFrame;
00332   float _interp;
00333 
00334   GLfloat _scale;
00335   string _name;
00336 };
00337 
00338 #endif  // __MD3MODEL_H__