1 #ifndef _MD2_H
2 #define _MD2_H
3 #pragma once
4 
5 #include "render.h"
6 
7 /**
8  * Credit for this code is mainly due to:
9  * DigiBen     digiben@gametutorials.com
10  * Look up his other great tutorials at:
11  * http://www.gametutorials.com
12  *
13  * glCommands (and thus simplification of this file) is implemented
14  * thanks to David Henry tutorial :
15  *   http://tfc.duke.free.fr/us/tutorials/models/md2.htm
16  */
17 
18 /**
19   *@author Gabor Torok
20   */
21 // These are the needed defines for the max values when loading .MD2 files
22 #define MD2_MAX_TRIANGLES       4096
23 #define MD2_MAX_VERTICES        2048
24 #define MD2_MAX_TEXCOORDS       2048
25 #define MD2_MAX_FRAMES          512
26 #define MD2_MAX_SKINS           32
27 #define MD2_MAX_FRAMESIZE       (MD2_MAX_VERTICES * 4 + 128)
28 
29 // TODO :  Load different skins
30 
31 /// Md2 header information.
32 struct tMd2Header {
33 	int magic;                   // This is used to identify the file
34 	int version;                 // The version number of the file (Must be 8)
35 	int skinWidth;               // The skin width in pixels
36 	int skinHeight;              // The skin height in pixels
37 	int frameSize;               // The size in bytes of a frame (constant for each)
38 	int numSkins;                // The number of skins associated with the model
39 	int numVertices;             // The number of vertices (constant for each frame)
40 	int numTexCoords;            // The number of texture coordinates
41 	int numTriangles;            // The number of faces (polygons)
42 	int numGlCommands;           // The number of gl commands
43 	int numFrames;               // The number of animation frames
44 	int offsetSkins;             // The offset in the file for the skin data
45 	int offsetTexCoords;         // The offset in the file for the texture data
46 	int offsetTriangles;         // The offset in the file for the face data
47 	int offsetFrames;            // The offset in the file for the frames data
48 	int offsetGlCommands;        // The offset in the file for the gl commands data
49 	int offsetEnd;               // The end of the file offset
50 };
51 
52 
53 /// This is used to store the vertices that are read in for the current frame.
54 struct tMd2AliasTriangle {
55 	byte vertex[3];
56 	byte lightNormalIndex;
57 };
58 
59 /// This stores the animation scale, translation and name information for a frame, plus verts.
60 struct tMd2AliasFrame {
61 	float scale[3];
62 	float translate[3];
63 	char name[16];
64 	tMd2AliasTriangle aliasVertices[1];
65 };
66 
67 
68 // This stores a skin or a frame name
69 typedef char tMd2String[64];
70 
71 // different actions possible in a md2 file
72 enum md2_action {
73 	MD2_STAND,
74 	MD2_RUN,
75 	MD2_ATTACK,
76 	MD2_PAIN1,
77 	MD2_PAIN2,
78 	MD2_PAIN3,
79 	MD2_JUMP,
80 	MD2_FLIP,
81 	MD2_SALUTE,
82 	MD2_TAUNT,
83 	MD2_WAVE,
84 	MD2_POINT,
85 	MD2_CRSTAND,
86 	MD2_CRWALK,
87 	MD2_CRATTACK,
88 	MD2_CRPAIN,
89 	MD2_CRDEATH,
90 	MD2_DEATH1,
91 	MD2_DEATH2,
92 	MD2_DEATH3,
93 
94 	// Must be last
95 	MD2_CREATURE_ACTION_COUNT
96 };
97 
98 /// Loader for .md2 files.
99 class CLoadMD2 {
100 
101 public:
102 	CLoadMD2();
103 	bool ImportMD2( t3DModel *pModel, std::string& strFileName );
104 	void DeleteMD2( t3DModel *pModel );
105 	static void findBounds( t3DModel *pModel, vect3d min, vect3d max );
106 	static void normalize( t3DModel *pModel, vect3d min, vect3d max );
107 
108 private:
109 
110 	void ReadMD2Data( t3DModel *pModel );
111 	void ParseAnimations( t3DModel *pModel );
112 	void ComputeMinMaxValues( t3DModel *pModel );
113 	void CleanUp();
114 
115 	FILE *m_FilePointer;
116 
117 	tMd2Header              m_Header;           // The header data
118 	tMd2String              *m_pSkins;          // The skin data
119 	tMd2String              *m_pFrames;         // The name of the frames
120 };
121 
122 
123 #endif
124 
125 
126 /////////////////////////////////////////////////////////////////////////////////
127 //
128 // Ben Humphrey (DigiBen)
129 // Game Programmer
130 // DigiBen@GameTutorials.com
131 // Co-Web Host of www.GameTutorials.com
132 //
133 // The Quake2 .Md2 file format is owned by ID Software.  This tutorial is being used
134 // as a teaching tool to help understand model loading and animation.  This should
135 // not be sold or used under any way for commercial use with out written conset
136 // from ID Software.
137 //
138 // Quake and Quake2 are trademarks of id Software.
139 // All trademarks used are properties of their respective owners.
140 //
141 //
142 
143