-
Notifications
You must be signed in to change notification settings - Fork 0
/
md2.h
218 lines (176 loc) · 4.53 KB
/
md2.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#ifndef __MD2_H
#define __MD2_H
#include <windows.h> // standard Windows app include
#include <gl/gl.h> // standard OpenGL include
#include <gl/glu.h> // OpenGL utilties
#include "TargaImage.h"
const float KEYFRAMES_PER_S = 8;
// a single vertex
typedef struct
{
union
{
struct
{
GLfloat x, y, z;
};
GLfloat v[3];
};
} xyz_t;
struct sphere_t
{
xyz_t center;
GLfloat radius;
};
// texture coordinate
typedef struct
{
float s;
float t;
} texCoord_t;
// texture coordinate index
typedef struct
{
short s;
short t;
} stIndex_t;
// info for a single frame point
typedef struct
{
unsigned char v[3];
unsigned char normalIndex; // not used
} framePoint_t;
// information for a single frame
typedef struct
{
float scale[3];
float translate[3];
char name[16];
framePoint_t fp[1];
} frame_t;
// data for a single triangle
typedef struct
{
unsigned short meshIndex[3]; // vertex indices
unsigned short stIndex[3]; // texture coordinate indices
} mesh_t;
typedef struct
{
int ident; // identifies as MD2 file "IDP2"
int version; // mine is 8
int skinwidth; // width of texture
int skinheight; // height of texture
int framesize; // number of bytes per frame
int numSkins; // number of textures
int numXYZ; // number of points
int numST; // number of texture
int numTris; // number of triangles
int numGLcmds;
int numFrames; // total number of frames
int offsetSkins; // offset to skin names (64 bytes each)
int offsetST; // offset of texture s-t values
int offsetTris; // offset of triangle mesh
int offsetFrames; // offset of frame data (points)
int offsetGLcmds; // type of OpenGL commands to use
int offsetEnd; // end of file
} modelHeader_t;
class CMD2Data;
class CMD2Instance
{
friend CMD2Data;
public:
~CMD2Instance(); // destructor
// render model with interpolation to get animation
void SetAnimationCustom(int startFrame, int endFrame);
void SetAnimation(int state, int nextState = _REPEAT);
void Animate(float seconds);
void Render();
// free memory of model
void Unload();
// retrieve animation state of model
int GetAnimation() { return m_state; }
const sphere_t &GetBoundingSphere() { return m_boundingSphere; }
void Move(GLfloat x, GLfloat y, GLfloat z);
void Rotate(GLfloat angle) { m_rotate = angle; }
// model animation states
enum
{
IDLE,
RUN,
ATTACK,
PAIN1,
PAIN2,
PAIN3,
JUMP,
FLIPOFF,
SAULTE,
TAUNT,
WAVE,
POINT,
CROUCH_IDLE,
CROUCH_WALK,
CROUCH_ATTACK,
CROUCH_PAIN,
CROUCH_DEATH,
DEATH1,
DEATH2,
DEATH3,
_REPEAT,
_CUSTOM,
_STATIC
};
protected:
CMD2Instance(); // constructor
private:
int m_state; // current model animation state
int m_nextState; // state to automatically transition to after the current state is finished
int m_currentFrame; // current frame # in animation
int m_nextFrame; // next frame # in animation
int m_startFrame; // first frame in current animation
int m_endFrame; // last frame in current animation
sphere_t m_boundingSphere;
float m_interpol; // percent through current frame
int m_numVertices;
int m_numWeaponVertices;
int m_numFrames;
xyz_t *m_currentVerts; // working vertex list
xyz_t *m_weaponVerts;
CMD2Data *m_pData;
GLfloat m_miny;
xyz_t m_pos;
GLfloat m_rotate;
};
class CMD2Data
{
friend CMD2Instance;
public:
CMD2Data(); // constructor
~CMD2Data(); // destructor
// load model and skin/texture at the same time
bool Load(char *modelFile, char *skinFile, char *weaponFile = NULL, char *weaponSkin = NULL, float scale = 1.0f);
// free memory of model
void Unload();
CMD2Instance *GetInstance();
protected:
void Animate(xyz_t *pVerts, xyz_t *pWeaponVerts, int curFrame, int nextFrame, float interpol);
const sphere_t &GetBoundingSphere() { return m_boundingSphere; }
private:
sphere_t m_boundingSphere;
GLfloat m_miny;
// main mesh data
mesh_t *m_tris; // triangle list
texCoord_t *m_texCoords; // texture coordinate list
xyz_t *m_verts; // vertex list
int m_numVerts;
int m_numTris;
GLuint m_texID; // texture ID
int m_numFrames;
// weapon mesh data
mesh_t *m_weaponTris; // triangle list
texCoord_t *m_weaponTexCoords; // texture coordinate list
xyz_t *m_weaponVerts; // vertex list
int m_numWeaponVerts;
int m_numWeaponTris;
GLuint m_weaponTexID; // texture ID
};
#endif