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_MAX_TRIANGLES 4096 |
---|
28 | #define MD2_MAX_VERTICES 2048 |
---|
29 | #define MD2_MAX_TEXCOORDS 2048 |
---|
30 | #define MD2_MAX_FRAMES 512 |
---|
31 | #define MD2_MAX_SKINS 32 |
---|
32 | #define MD2_MAX_FRAMESIZE (MD2_MAX_VERTICES * 4 + 128) |
---|
33 | |
---|
34 | //! This stores the speed of the animation between each key frame - currently conflicting with the animation framework |
---|
35 | #define kAnimationSpeed 5.0f |
---|
36 | |
---|
37 | //! This holds the header information that is read in at the beginning of the file: id software definition |
---|
38 | struct tMd2Header |
---|
39 | { |
---|
40 | int ident; //!< This is used to identify the file |
---|
41 | int version; //!< The version number of the file (Must be 8) |
---|
42 | |
---|
43 | int skinWidth; //!< The skin width in pixels |
---|
44 | int skinHeight; //!< The skin height in pixels |
---|
45 | int frameSize; //!< The size in bytes the frames are |
---|
46 | |
---|
47 | int numSkins; //!< The number of skins associated with the model |
---|
48 | int numVertices; //!< The number of vertices (constant for each frame) |
---|
49 | int numTexCoords; //!< The number of texture coordinates |
---|
50 | int numTriangles; //!< The number of faces (polygons) |
---|
51 | int numGlCommands; //!< The number of gl commands |
---|
52 | int numFrames; //!< The number of animation frames |
---|
53 | |
---|
54 | int offsetSkins; //!< The offset in the file for the skin data |
---|
55 | int offsetTexCoords; //!< The offset in the file for the texture data |
---|
56 | int offsetTriangles; //!< The offset in the file for the face data |
---|
57 | int offsetFrames; //!< The offset in the file for the frames data |
---|
58 | int offsetGlCommands; //!< The offset in the file for the gl commands data |
---|
59 | int offsetEnd; //!< The end of the file offset |
---|
60 | }; |
---|
61 | |
---|
62 | |
---|
63 | //! This is used to store the vertices that are read in for the current frame directly from the file (compressed) |
---|
64 | struct tMd2AliasTriangle |
---|
65 | { |
---|
66 | byte vertex[3]; |
---|
67 | byte lightNormalIndex; |
---|
68 | }; |
---|
69 | |
---|
70 | //! This stores the normals and vertices for the frames (uncompressed) |
---|
71 | struct tMd2Triangle |
---|
72 | { |
---|
73 | float vertex[3]; |
---|
74 | float normal[3]; |
---|
75 | }; |
---|
76 | |
---|
77 | //! This stores the indices into the vertex and texture coordinate arrays |
---|
78 | struct tMd2Face |
---|
79 | { |
---|
80 | short vertexIndices[3]; |
---|
81 | short textureIndices[3]; |
---|
82 | }; |
---|
83 | |
---|
84 | //! This stores UV coordinates |
---|
85 | struct tMd2TexCoord |
---|
86 | { |
---|
87 | short u, v; |
---|
88 | }; |
---|
89 | |
---|
90 | //! This stores the animation scale, translation and name information for a frame, plus verts |
---|
91 | struct tMd2AliasFrame |
---|
92 | { |
---|
93 | float scale[3]; |
---|
94 | float translate[3]; |
---|
95 | char name[16]; |
---|
96 | tMd2AliasTriangle aliasVertices[1]; |
---|
97 | }; |
---|
98 | |
---|
99 | //! This stores the frames vertices after they have been transformed |
---|
100 | struct tMd2Frame |
---|
101 | { |
---|
102 | char strName[16]; |
---|
103 | tMd2Triangle *pVertices; |
---|
104 | }; |
---|
105 | |
---|
106 | //! This stores a skin name |
---|
107 | typedef char tMd2Skin[64]; |
---|
108 | |
---|
109 | //! This is a MD2 Model class |
---|
110 | class MD2Model : public AbstractModel { |
---|
111 | |
---|
112 | public: |
---|
113 | MD2Model(); |
---|
114 | virtual ~MD2Model(); |
---|
115 | |
---|
116 | bool loadModel(const char* filename); |
---|
117 | bool loadSkin(const char* filename); |
---|
118 | |
---|
119 | void drawModel(float time); |
---|
120 | void drawFrame(int frame); |
---|
121 | |
---|
122 | void draw(t3DModel *pModel); |
---|
123 | |
---|
124 | void setAnim(int type); |
---|
125 | void scaleModel(float s); |
---|
126 | |
---|
127 | private: |
---|
128 | void animate(float time); |
---|
129 | void processLightning(); |
---|
130 | void interpolate(CVector3* vertlist); |
---|
131 | void renderFrame(); |
---|
132 | }; |
---|
133 | |
---|
134 | //! A class that handles all of the loading code |
---|
135 | class MD2Loader : public BaseObject { |
---|
136 | |
---|
137 | public: |
---|
138 | MD2Loader(); |
---|
139 | virtual ~MD2Loader(); |
---|
140 | |
---|
141 | bool importMD2(t3DModel *pModel, char *fileName, char *texture); |
---|
142 | |
---|
143 | private: |
---|
144 | void readMD2Data(); |
---|
145 | void parseAnimations(t3DModel *pModel); |
---|
146 | void convertDataStructures(t3DModel *pModel); |
---|
147 | void computeNormals(t3DModel *pModel); |
---|
148 | void cleanUp(); |
---|
149 | |
---|
150 | FILE *pFile; |
---|
151 | tMd2Header header; //!< The header data |
---|
152 | tMd2Skin *pSkins; //!< The skin data |
---|
153 | tMd2TexCoord *pTexCoords; //!< The texture coordinates |
---|
154 | tMd2Face *pTriangles; //!< Face index information |
---|
155 | tMd2Frame *pFrames; //!< The frames of animation (vertices) |
---|
156 | }; |
---|
157 | |
---|
158 | |
---|
159 | #endif /* _MD2MODEL_H */ |
---|