Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/md2_loader/src/lib/graphics/importer/md2Model.h @ 4172

Last change on this file since 4172 was 4172, checked in by patrick, 19 years ago

orxonox/branches/md2_loader: reimplemente the whole md2_loader to make it more performant

File size: 6.0 KB
Line 
1/*!
2    \file md2Model.h
3    \brief Definition of an MD2 Model, a model format invented by ID Software.
4       
5        We are deeply thankfull for all the wunderfull things id software made for us gamers!
6
7        The md2 file format is structured in a very simple way: it contains animations which are made out of
8        frames (so called keyframes). Each frame is a complete draweable model in a specific position.
9        A frame is a collection of vertex and its compagnions (normals, texture coordinates).
10       
11        A typical model has about 200 frames, the maximum frame count is fixed by MD2_MAX_FRAMES to currently
12        512 frames. The maximal vetices count is set to 2048 verteces, not enough?
13        You just have to change the MD2_MAX_* values if it doesn't fit your purposes...
14
15        Surface Culling is fully implemented in md2 models: quake2 uses front face culling.
16*/
17
18#ifndef _MD2MODEL_H
19#define _MD2MODEL_H
20
21#include "abstract_model.h"
22#include "base_object.h"
23#include "stdincl.h"
24
25
26//! These are the needed defines for the max values when loading .MD2 files
27#define MD2_IDENT                       (('2'<<24) + ('P'<<16) + ('D'<<8) + 'I')
28#define MD2_VERSION                     8
29#define MD2_MAX_TRIANGLES               4096
30#define MD2_MAX_VERTICES                2048
31#define MD2_MAX_TEXCOORDS               2048
32#define MD2_MAX_FRAMES                  512
33#define MD2_MAX_SKINS                   32
34#define MD2_MAX_FRAMESIZE               (MD2_MAX_VERTICES * 4 + 128)
35
36#define NUM_VERTEX_NORMALS              162
37#define SHADEDOT_QUANT                  16
38
39//! This stores the speed of the animation between each key frame - currently conflicting with the animation framework
40#define kAnimationSpeed                 12.0f
41
42//! This holds the header information that is read in at the beginning of the file: id software definition
43struct tMd2Header
44{ 
45   int ident;                           //!< This is used to identify the file
46   int version;                         //!< The version number of the file (Must be 8)
47   
48   int skinWidth;                       //!< The skin width in pixels
49   int skinHeight;                      //!< The skin height in pixels
50   int frameSize;                       //!< The size in bytes the frames are
51   
52   int numSkins;                        //!< The number of skins associated with the model
53   int numVertices;                     //!< The number of vertices (constant for each frame)
54   int numTexCoords;                    //!< The number of texture coordinates
55   int numTriangles;                    //!< The number of faces (polygons)
56   int numGlCommands;                   //!< The number of gl commands
57   int numFrames;                       //!< The number of animation frames
58   
59   int offsetSkins;                     //!< The offset in the file for the skin data
60   int offsetTexCoords;                 //!< The offset in the file for the texture data
61   int offsetTriangles;                 //!< The offset in the file for the face data
62   int offsetFrames;                    //!< The offset in the file for the frames data
63   int offsetGlCommands;                //!< The offset in the file for the gl commands data
64   int offsetEnd;                       //!< The end of the file offset
65};
66
67
68//! This is used to store the vertices that are read in for the current frame directly from the file (compressed)
69struct tMd2AliasTriangle
70{
71   byte vertex[3];
72   byte lightNormalIndex;
73};
74
75//! This stores the normals and vertices for the frames (uncompressed)
76struct tMd2Triangle
77{
78   float vertex[3];
79   float normal[3];
80};
81
82//! This stores the indices into the vertex and texture coordinate arrays
83struct tMd2Face
84{
85   short vertexIndices[3];
86   short textureIndices[3];
87};
88
89//! This stores UV coordinates
90struct tMd2TexCoord
91{
92   short u, v;
93};
94
95//! This stores the animation scale, translation and name information for a frame, plus verts
96struct tMd2AliasFrame
97{
98   float scale[3];
99   float translate[3];
100   char name[16];
101   tMd2AliasTriangle aliasVertices[1];
102};
103
104//! This stores the frames vertices after they have been transformed
105struct tMd2Frame
106{
107   char strName[16];
108   tMd2Triangle *pVertices;
109};
110
111//! This stores a skin name
112typedef char tMd2Skin[64];
113
114
115//! This is a MD2 Model class
116class MD2Model : public AbstractModel {
117
118public:
119  MD2Model();
120  virtual ~MD2Model();
121 
122  bool loadModel(const char* filename);
123  bool loadSkin(const char* filename);
124 
125  void drawModel(float time);
126  void drawFrame(int frame);
127  void draw();
128 
129  void setAnim(int type);
130  void scaleModel(float s);
131
132  void tick(float dtS);
133
134  void animate(/*float time*/);
135private:
136  void processLightning();
137  void interpolate(CVector3* vertlist);
138  void renderFrame();
139  float getCurrentTime(t3DModel *pModel, int nextFrame);
140
141  t3DModel* model;
142  float localTime;
143};
144
145/* forward definitions */
146class Material;
147
148//! This is a MD2 Model class
149class MD2Model2 : public AbstractModel {
150
151public:
152  MD2Model2();
153  virtual ~MD2Model2();
154 
155  bool loadModel(const char* filename);
156  bool loadSkin(const char* filename);
157 
158  void drawFrame(int frame);
159  void draw();
160 
161  void setAnim(int type);
162  void scaleModel(float scaleFactor) { this->scaleFactor = scaleFactor;}
163
164  void tick(float dtS);
165
166private:
167  void animate(float time); 
168  void processLighting();
169  void interpolate(sVec3D* verticesList);
170  void renderFrame();
171
172 public:
173  /* these variables are static, because they are all the same for every model */
174  static sVec3D anorms[NUM_VERTEX_NORMALS];
175  static float anormsDots[SHADEDOT_QUANT][256];
176  static sAnim animationList[21];
177
178 private:
179  int numFrames;
180  int numVertices;
181  int numGLCommands;
182
183  sVec3D* pVertices;
184  int* pGLCommands;
185  int* pLightNormals;
186
187  unsigned int textureID;
188  Material* material;
189  sAnimState animationState;
190  float scaleFactor;
191};
192
193
194//! A class that handles all of the loading code
195class MD2Loader : public BaseObject {
196
197public:
198  MD2Loader();
199  virtual ~MD2Loader();
200 
201  bool importMD2(t3DModel *pModel, char *fileName, char *texture = NULL);
202
203private:
204  void readMD2Data();
205  void parseAnimations(t3DModel *pModel);
206  void convertDataStructures(t3DModel *pModel);
207  void computeNormals(t3DModel *pModel);
208  void cleanUp();
209 
210  FILE *pFile; 
211  tMd2Header header;                          //!< The header data
212  tMd2Skin *pSkins;                           //!< The skin data
213  tMd2TexCoord *pTexCoords;                   //!< The texture coordinates
214  tMd2Face *pTriangles;                       //!< Face index information
215  tMd2Frame *pFrames;                         //!< The frames of animation (vertices)
216};
217
218
219#endif /* _MD2MODEL_H */
Note: See TracBrowser for help on using the repository browser.