-
Notifications
You must be signed in to change notification settings - Fork 0
/
chess_md2.h
186 lines (148 loc) · 4.42 KB
/
chess_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
#ifndef __CHESS_MD2_H__
#define __CHESS_MD2_H__
#include <windows.h> // standard Windows app include
#include <gl/gl.h> // standard OpenGL include
#include <gl/glu.h> // OpenGL utilities
#include "chess_tga_image.h"
const float KEYFRAMES_PER_S = 8;
// a single vertex
typedef struct
{
union {
struct {
GLfloat x, y, z;
};
GLfloat v[3];
};
} Vertex;
typedef struct {
Vertex center;
GLfloat radius;
} Sphere;
// texture coordinate
typedef struct {
float s;
float t;
} TexCoord;
// texture coordinate index
typedef struct {
short s;
short t;
} TexCoordIdx;
// info for a single frame point
typedef struct {
unsigned char v[3];
unsigned char normalIndex; // not used
} FramePoint;
// information for a single frame
typedef struct {
float scale[3];
float translate[3];
char name[16];
FramePoint fp[1];
} Frame;
// data for a single triangle
typedef struct {
unsigned short meshIndex[3]; // vertex indices
unsigned short texIndex[3]; // texture coordinate indices
} Mesh;
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 numVerts; // number of points
int numTex; // 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 offsetTex; // 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;
class MD2Data;
class MD2Instance {
friend MD2Data;
public:
~MD2Instance(); // 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 aniState; }
const Sphere &getBoundingSphere() { return boundingSphere; }
void move(GLfloat x, GLfloat y, GLfloat z);
void rotate(GLfloat angle) { rotateAngle = 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:
MD2Instance(); // constructor
private:
int aniState; // current model animation state
int nextAniState; // state to automatically transition to after the current state is finished
int currentFrame; // current frame # in animation
int nextFrame; // next frame # in animation
int startFrame; // first frame in current animation
int endFrame; // last frame in current animation
Sphere boundingSphere;
float interpolation; // percent through current frame
int numVertices;
int numWeaponVertices;
int numFrames;
Vertex *currentVerts; // working vertex list
Vertex *weaponVerts;
MD2Data *data;
GLfloat miny;
Vertex pos;
GLfloat rotateAngle;
};
class MD2Data {
friend MD2Instance;
public:
MD2Data(); // constructor
~MD2Data(); // 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();
MD2Instance *getInstance();
protected:
void animate(Vertex *pVerts, Vertex *pWeaponVerts, int curFrame,
int nextFrame, float interpol);
const Sphere &getBoundingSphere() { return boundingSphere; }
private:
Sphere boundingSphere;
GLfloat miny;
// main mesh data
Mesh *tris; // triangle list
TexCoord *texCoords; // texture coordinate list
Vertex *verts; // vertex list
int numVerts;
int numTris;
GLuint texID; // texture ID
int numFrames;
// weapon mesh data
Mesh *weaponTris; // triangle list
TexCoord *weaponTexCoords; // texture coordinate list
Vertex *weaponVerts; // vertex list
int numWeaponVerts;
int numWeaponTris;
GLuint weaponTexID; // texture ID
};
#endif // __CHESS_MD2_H__