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 <boost/shared_ptr.hpp>
00022 #include <fstream>
00023 #include <stdexcept>
00024 #include <string>
00025 #include <vector>
00026 #include <map>
00027 
00028 #include "Mathlib.h"
00029 #include "Texture.h"
00030 
00031 using std::string;
00032 using std::vector;
00033 using std::map;
00034 
00035 using boost::shared_ptr;
00036 
00037 
00038 // Constants definitions
00039 const int kMd3MaxFrames = 1024;
00040 const int kMd3MaxTags = 16;
00041 const int kMd3MaxMeshes = 32;
00042 const int kMd3MaxShaders = 256;
00043 const int kMd3MaxTriangles = 8192;
00044 const int kMd3MaxVertices = 4096;
00045 const float kMd3XYZScale = 1.0f / 64.0f;
00046 
00047 
00048 // OpenGL vector types
00049 typedef GLfloat vec2_t[2];
00050 typedef GLfloat vec3_t[3];
00051 
00052 
00053 // MDL Header
00054 struct Md3Header_t
00055 {
00056   int ident;            // Magic number, "IDP3"
00057   int version;          // Md3 format version, should be 15
00058 
00059   char name[64];        // Path name
00060   int flags;            // ?
00061 
00062   int num_frames;       // Number of frames
00063   int num_tags;         // Number of tags
00064   int num_meshes;       // Number of meshes
00065   int num_skins;        // Number of skins
00066 
00067   int offset_frames;    // Offset to frame data
00068   int offset_tag;       // Offset to tag data
00069   int offset_meshes;    // Offset to meshes
00070   int offset_eof;       // Offset end of file
00071 };
00072 
00073 
00074 // Frame data
00075 struct Md3Frame_t
00076 {
00077   vec3_t min_bounds;    // First corner of the bbox
00078   vec3_t max_bounds;    // Second corner of the bbox
00079   vec3_t local_origin;
00080 
00081   float radius;         // Radius of bounding sphere
00082   char creator[16];
00083 };
00084 
00085 
00086 // Tag information
00087 struct Md3Tag_t
00088 {
00089   char name[64];
00090   vec3_t origin;        // Position vector
00091   float axis[3][3];     // Orientation matrix
00092 };
00093 
00094 
00095 // Mesh header
00096 struct Md3MeshHeader_t
00097 {
00098   int ident;            // Magic number, "IDP3"
00099   char name[64];        // Mesh's name
00100   int flags;            // ?
00101 
00102   int num_frames;       // Number of frames
00103   int num_shaders;      // Number of textures
00104   int num_verts;        // Number of vertices per frame
00105   int num_triangles;    // Number of triangles
00106 
00107   int offset_triangles; // Offset to triangle data
00108   int offset_shaders;   // Offset to skin data
00109   int offset_st;        // Offset to texture coords.
00110   int offset_xyznormal; // Offset to vertex data
00111   int offset_end;       // Offset to the end of the mesh
00112 };
00113 
00114 
00115 // Mesh texture
00116 struct Md3Shader_t
00117 {
00118   char name[64];
00119   int shader_index;
00120 };
00121 
00122 
00123 // Triangle data
00124 struct Md3Triangle_t
00125 {
00126   int index[3];         // Vertex indices
00127 };
00128 
00129 
00130 // Texture coordinates
00131 struct Md3TexCoord_t
00132 {
00133   float s;
00134   float t;
00135 };
00136 
00137 
00138 // Compressed vertex data
00139 struct Md3Vertex_t
00140 {
00141   short v[3];
00142   unsigned char normal[2];
00143 };
00144 
00145 
00146 /////////////////////////////////////////////////////////////////////////////
00147 //
00148 // class Md3Exception - Exception class for MD3 loader classes.
00149 // This acts like a standard runtime_error exception but
00150 // know which file or mesh has failed to be loaded.
00151 //
00152 /////////////////////////////////////////////////////////////////////////////
00153 
00154 class Md3Exception : public std::runtime_error
00155 {
00156 public:
00157   // Constructors
00158   Md3Exception (const string &error)
00159     : std::runtime_error (error) { }
00160   Md3Exception (const string &error, const string &name)
00161     : std::runtime_error (error), _which (name) { }
00162   virtual ~Md3Exception () throw () { }
00163 
00164 public:
00165   // Public interface
00166   virtual const char *which () const throw () {
00167     return _which.c_str ();
00168   }
00169 
00170 private:
00171   // Member variables
00172   string _which;
00173 };
00174 
00175 
00176 /////////////////////////////////////////////////////////////////////////////
00177 //
00178 // class NormalLookupTable -- A Normal Lookup Table Class for MD3 Models.
00179 //
00180 /////////////////////////////////////////////////////////////////////////////
00181 
00182 class NormalLookupTable
00183 {
00184 public:
00185   // Constructor
00186   NormalLookupTable ();
00187 
00188 public:
00189   // Accessors
00190   const vec3_t *operator[] (int i) const {
00191     return _normal[i];
00192   }
00193 
00194 private:
00195   // Member variables
00196   vec3_t _normal[256][256];
00197 };
00198 
00199 
00200 /////////////////////////////////////////////////////////////////////////////
00201 //
00202 // class Md3Mesh -- MD3 Mesh Data Class.
00203 //
00204 /////////////////////////////////////////////////////////////////////////////
00205 
00206 class Md3Mesh
00207 {
00208 public:
00209   // Constructor/destructor
00210   Md3Mesh (std::ifstream &ifs)
00211     throw (Md3Exception);
00212   ~Md3Mesh ();
00213 
00214 private:
00215   // Internal types
00216   typedef shared_ptr<Md3Shader_t> Md3ShaderPtr;
00217   typedef shared_ptr<Md3Triangle_t> Md3TrianglePtr;
00218   typedef shared_ptr<Md3Vertex_t> Md3VertexPtr;
00219 
00220 public:
00221   // Public interface
00222   void loadShader (int index);
00223   void bindTexture () const;
00224   void setupVertexArrays (int frameA, int frameB, float interp, float scale);
00225 
00226   void renderFrameImmediate (int frame, float scale) const;
00227   void renderWithVertexArrays () const;
00228 
00229   void setTexture (const Texture2D *tex) { _tex = tex; }
00230 
00231   // Accessors
00232   const char *name () const { return _header.name; }
00233 
00234 private:
00235   // Member variables
00236 
00237   // Constants
00238   static const NormalLookupTable _kAnorms;
00239   static const int _kMd3Ident;
00240 
00241   // Vertex arrays (shared by all meshes)
00242   static vec3_t _kVertexArray[];
00243   static vec3_t _kNormalArray[];
00244 
00245   // Mesh data
00246   Md3MeshHeader_t _header;
00247 
00248   vector<Md3ShaderPtr> _shaders;
00249   vector<Md3TrianglePtr> _triangles;
00250   vector<Md3TexCoord_t> _texCoords;
00251   vector<Md3VertexPtr> _vertices;
00252 
00253   // Indices for vertex arrays
00254   vector<GLuint> _indices;
00255 
00256   // Texture
00257   const Texture2D *_tex;
00258 };
00259 
00260 
00261 /////////////////////////////////////////////////////////////////////////////
00262 //
00263 // class Md3Model -- MD3 Model Data Class.  A model is contituted of
00264 // multiple meshes.
00265 //
00266 /////////////////////////////////////////////////////////////////////////////
00267 
00268 class Md3Model
00269 {
00270 public:
00271   // Constructor/destructor
00272   Md3Model (const string &filename)
00273     throw (Md3Exception);
00274   ~Md3Model ();
00275 
00276 private:
00277   // Internal types
00278 
00279   // Tag information using Quaternion instead
00280   // of a 3x3 rotation matrix
00281   struct Md3QuaternionTag
00282   {
00283     // Constructor
00284     Md3QuaternionTag (const Md3Tag_t &tag);
00285 
00286     string name;
00287     Vector3f origin;
00288     Quaternionf orient;
00289   };
00290 
00291   typedef shared_ptr<Md3Frame_t> Md3FramePtr;
00292   typedef shared_ptr<Md3Mesh> Md3MeshPtr;
00293   typedef shared_ptr<Md3QuaternionTag> Md3TagPtr;
00294 
00295 public:
00296   // Public interface
00297   void loadShaders () const;
00298   void draw () const;
00299   void renderFrameImmediate (int frame) const;
00300   void renderFrameItpWithVertexArrays (int frameA, int frameB,
00301                                        float interp) const;
00302 
00303   bool link (const string &name, const Md3Model *model);
00304   bool unlink (const string &name);
00305   void setScale (float scale) { _scale = scale; }
00306   void setTexture (const string &mesh, const Texture2D *tex);
00307   void setupAnimation (int currFrame, int nextFrame, float interp);
00308 
00309   // Accessors
00310   int numMeshes () const { return _header.num_meshes; }
00311   int numFrames () const { return _header.num_frames; }
00312   int numTags () const { return _header.num_tags; }
00313   float scale () const { return _scale; }
00314   const string &name () const { return _name; }
00315 
00316 private:
00317   // Member variables
00318 
00319   // Constants
00320   static const int _kMd3Ident;
00321   static const int _kMd3Version;
00322 
00323   // Model data
00324   Md3Header_t _header;
00325 
00326   vector<Md3FramePtr> _frames;
00327   vector<Md3MeshPtr> _meshes;
00328   vector<Md3TagPtr> _qtags;
00329   vector<const Md3Model*> _links;
00330 
00331   // Animation data used for the
00332   // next rendering
00333   int _currFrame;
00334   int _nextFrame;
00335   float _interp;
00336 
00337   GLfloat _scale;
00338   string _name;
00339 };
00340 
00341 #endif  // __MD3MODEL_H__