-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxmd2.c
449 lines (416 loc) · 16.3 KB
/
xmd2.c
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/**
* This file belongs to the 'xlab' game engine.
* Copyright 2009 xfacter
* Copyright 2016 wickles
* This work is licensed under the LGPLv3
* subject to all terms as reproduced in the included LICENSE file.
*/
#include <pspgu.h>
#include <pspgum.h>
#include "xgraphics.h"
#include "xmath.h"
#include "xmem.h"
#include "xmd2.h"
#define MD2_MAX_TRIS (4096)
#define MD2_MAX_VERTS (2048)
#define MD2_MAX_TEX_COORDS (2048)
#define MD2_MAX_FRAMES (512)
#define MD2_MAX_SKINS (32)
#define MD2_MAX_NORMALS (162)
#define MD2_IDENT ('I'<<0|'D'<<8|'P'<<16|'2'<<24) /* "IDP2" (0x32504449) */
#define MD2_VERSION (8)
#define MD2_NORMAL_TABLE_SIZE (162)
static ScePspFVector3 md2_normal_lookup[MD2_NORMAL_TABLE_SIZE] =
{
{ -0.525731f, 0.000000f, 0.850651f },
{ -0.442863f, 0.238856f, 0.864188f },
{ -0.295242f, 0.000000f, 0.955423f },
{ -0.309017f, 0.500000f, 0.809017f },
{ -0.162460f, 0.262866f, 0.951056f },
{ 0.000000f, 0.000000f, 1.000000f },
{ 0.000000f, 0.850651f, 0.525731f },
{ -0.147621f, 0.716567f, 0.681718f },
{ 0.147621f, 0.716567f, 0.681718f },
{ 0.000000f, 0.525731f, 0.850651f },
{ 0.309017f, 0.500000f, 0.809017f },
{ 0.525731f, 0.000000f, 0.850651f },
{ 0.295242f, 0.000000f, 0.955423f },
{ 0.442863f, 0.238856f, 0.864188f },
{ 0.162460f, 0.262866f, 0.951056f },
{ -0.681718f, 0.147621f, 0.716567f },
{ -0.809017f, 0.309017f, 0.500000f },
{ -0.587785f, 0.425325f, 0.688191f },
{ -0.850651f, 0.525731f, 0.000000f },
{ -0.864188f, 0.442863f, 0.238856f },
{ -0.716567f, 0.681718f, 0.147621f },
{ -0.688191f, 0.587785f, 0.425325f },
{ -0.500000f, 0.809017f, 0.309017f },
{ -0.238856f, 0.864188f, 0.442863f },
{ -0.425325f, 0.688191f, 0.587785f },
{ -0.716567f, 0.681718f, -0.147621f },
{ -0.500000f, 0.809017f, -0.309017f },
{ -0.525731f, 0.850651f, 0.000000f },
{ 0.000000f, 0.850651f, -0.525731f },
{ -0.238856f, 0.864188f, -0.442863f },
{ 0.000000f, 0.955423f, -0.295242f },
{ -0.262866f, 0.951056f, -0.162460f },
{ 0.000000f, 1.000000f, 0.000000f },
{ 0.000000f, 0.955423f, 0.295242f },
{ -0.262866f, 0.951056f, 0.162460f },
{ 0.238856f, 0.864188f, 0.442863f },
{ 0.262866f, 0.951056f, 0.162460f },
{ 0.500000f, 0.809017f, 0.309017f },
{ 0.238856f, 0.864188f, -0.442863f },
{ 0.262866f, 0.951056f, -0.162460f },
{ 0.500000f, 0.809017f, -0.309017f },
{ 0.850651f, 0.525731f, 0.000000f },
{ 0.716567f, 0.681718f, 0.147621f },
{ 0.716567f, 0.681718f, -0.147621f },
{ 0.525731f, 0.850651f, 0.000000f },
{ 0.425325f, 0.688191f, 0.587785f },
{ 0.864188f, 0.442863f, 0.238856f },
{ 0.688191f, 0.587785f, 0.425325f },
{ 0.809017f, 0.309017f, 0.500000f },
{ 0.681718f, 0.147621f, 0.716567f },
{ 0.587785f, 0.425325f, 0.688191f },
{ 0.955423f, 0.295242f, 0.000000f },
{ 1.000000f, 0.000000f, 0.000000f },
{ 0.951056f, 0.162460f, 0.262866f },
{ 0.850651f, -0.525731f, 0.000000f },
{ 0.955423f, -0.295242f, 0.000000f },
{ 0.864188f, -0.442863f, 0.238856f },
{ 0.951056f, -0.162460f, 0.262866f },
{ 0.809017f, -0.309017f, 0.500000f },
{ 0.681718f, -0.147621f, 0.716567f },
{ 0.850651f, 0.000000f, 0.525731f },
{ 0.864188f, 0.442863f, -0.238856f },
{ 0.809017f, 0.309017f, -0.500000f },
{ 0.951056f, 0.162460f, -0.262866f },
{ 0.525731f, 0.000000f, -0.850651f },
{ 0.681718f, 0.147621f, -0.716567f },
{ 0.681718f, -0.147621f, -0.716567f },
{ 0.850651f, 0.000000f, -0.525731f },
{ 0.809017f, -0.309017f, -0.500000f },
{ 0.864188f, -0.442863f, -0.238856f },
{ 0.951056f, -0.162460f, -0.262866f },
{ 0.147621f, 0.716567f, -0.681718f },
{ 0.309017f, 0.500000f, -0.809017f },
{ 0.425325f, 0.688191f, -0.587785f },
{ 0.442863f, 0.238856f, -0.864188f },
{ 0.587785f, 0.425325f, -0.688191f },
{ 0.688191f, 0.587785f, -0.425325f },
{ -0.147621f, 0.716567f, -0.681718f },
{ -0.309017f, 0.500000f, -0.809017f },
{ 0.000000f, 0.525731f, -0.850651f },
{ -0.525731f, 0.000000f, -0.850651f },
{ -0.442863f, 0.238856f, -0.864188f },
{ -0.295242f, 0.000000f, -0.955423f },
{ -0.162460f, 0.262866f, -0.951056f },
{ 0.000000f, 0.000000f, -1.000000f },
{ 0.295242f, 0.000000f, -0.955423f },
{ 0.162460f, 0.262866f, -0.951056f },
{ -0.442863f, -0.238856f, -0.864188f },
{ -0.309017f, -0.500000f, -0.809017f },
{ -0.162460f, -0.262866f, -0.951056f },
{ 0.000000f, -0.850651f, -0.525731f },
{ -0.147621f, -0.716567f, -0.681718f },
{ 0.147621f, -0.716567f, -0.681718f },
{ 0.000000f, -0.525731f, -0.850651f },
{ 0.309017f, -0.500000f, -0.809017f },
{ 0.442863f, -0.238856f, -0.864188f },
{ 0.162460f, -0.262866f, -0.951056f },
{ 0.238856f, -0.864188f, -0.442863f },
{ 0.500000f, -0.809017f, -0.309017f },
{ 0.425325f, -0.688191f, -0.587785f },
{ 0.716567f, -0.681718f, -0.147621f },
{ 0.688191f, -0.587785f, -0.425325f },
{ 0.587785f, -0.425325f, -0.688191f },
{ 0.000000f, -0.955423f, -0.295242f },
{ 0.000000f, -1.000000f, 0.000000f },
{ 0.262866f, -0.951056f, -0.162460f },
{ 0.000000f, -0.850651f, 0.525731f },
{ 0.000000f, -0.955423f, 0.295242f },
{ 0.238856f, -0.864188f, 0.442863f },
{ 0.262866f, -0.951056f, 0.162460f },
{ 0.500000f, -0.809017f, 0.309017f },
{ 0.716567f, -0.681718f, 0.147621f },
{ 0.525731f, -0.850651f, 0.000000f },
{ -0.238856f, -0.864188f, -0.442863f },
{ -0.500000f, -0.809017f, -0.309017f },
{ -0.262866f, -0.951056f, -0.162460f },
{ -0.850651f, -0.525731f, 0.000000f },
{ -0.716567f, -0.681718f, -0.147621f },
{ -0.716567f, -0.681718f, 0.147621f },
{ -0.525731f, -0.850651f, 0.000000f },
{ -0.500000f, -0.809017f, 0.309017f },
{ -0.238856f, -0.864188f, 0.442863f },
{ -0.262866f, -0.951056f, 0.162460f },
{ -0.864188f, -0.442863f, 0.238856f },
{ -0.809017f, -0.309017f, 0.500000f },
{ -0.688191f, -0.587785f, 0.425325f },
{ -0.681718f, -0.147621f, 0.716567f },
{ -0.442863f, -0.238856f, 0.864188f },
{ -0.587785f, -0.425325f, 0.688191f },
{ -0.309017f, -0.500000f, 0.809017f },
{ -0.147621f, -0.716567f, 0.681718f },
{ -0.425325f, -0.688191f, 0.587785f },
{ -0.162460f, -0.262866f, 0.951056f },
{ 0.442863f, -0.238856f, 0.864188f },
{ 0.162460f, -0.262866f, 0.951056f },
{ 0.309017f, -0.500000f, 0.809017f },
{ 0.147621f, -0.716567f, 0.681718f },
{ 0.000000f, -0.525731f, 0.850651f },
{ 0.425325f, -0.688191f, 0.587785f },
{ 0.587785f, -0.425325f, 0.688191f },
{ 0.688191f, -0.587785f, 0.425325f },
{ -0.955423f, 0.295242f, 0.000000f },
{ -0.951056f, 0.162460f, 0.262866f },
{ -1.000000f, 0.000000f, 0.000000f },
{ -0.850651f, 0.000000f, 0.525731f },
{ -0.955423f, -0.295242f, 0.000000f },
{ -0.951056f, -0.162460f, 0.262866f },
{ -0.864188f, 0.442863f, -0.238856f },
{ -0.951056f, 0.162460f, -0.262866f },
{ -0.809017f, 0.309017f, -0.500000f },
{ -0.864188f, -0.442863f, -0.238856f },
{ -0.951056f, -0.162460f, -0.262866f },
{ -0.809017f, -0.309017f, -0.500000f },
{ -0.681718f, 0.147621f, -0.716567f },
{ -0.681718f, -0.147621f, -0.716567f },
{ -0.850651f, 0.000000f, -0.525731f },
{ -0.688191f, 0.587785f, -0.425325f },
{ -0.587785f, 0.425325f, -0.688191f },
{ -0.425325f, 0.688191f, -0.587785f },
{ -0.425325f, -0.688191f, -0.587785f },
{ -0.587785f, -0.425325f, -0.688191f },
{ -0.688191f, -0.587785f, -0.425325f }
};
#define MD2_DEFAULT_NUM_ANIMS (21)
static x_anim default_anims[MD2_DEFAULT_NUM_ANIMS] =
{
/* first, size, fps */
{ 0, 40, 1.0f/9 }, /* STAND */
{ 40, 6, 1.0f/10 }, /* RUN */
{ 46, 8, 1.0f/10 }, /* ATTACK */
{ 54, 4, 1.0f/7 }, /* PAIN_A */
{ 58, 4, 1.0f/7 }, /* PAIN_B */
{ 62, 4, 1.0f/7 }, /* PAIN_C */
{ 66, 6, 1.0f/7 }, /* JUMP */
{ 72, 12, 1.0f/7 }, /* FLIP */
{ 84, 11, 1.0f/7 }, /* SALUTE */
{ 95, 17, 1.0f/10 }, /* FALLBACK */
{ 112, 11, 1.0f/7 }, /* WAVE */
{ 123, 12, 1.0f/6 }, /* POINT */
{ 135, 19, 1.0f/10 }, /* CROUCH_STAND */
{ 154, 6, 1.0f/7 }, /* CROUCH_WALK */
{ 160, 6, 1.0f/10 }, /* CROUCH_ATTACK */
{ 166, 6, 1.0f/7 }, /* CROUCH_PAIN */
{ 173, 5, 1.0f/5 }, /* CROUCH_DEATH */
{ 178, 6, 1.0f/7 }, /* DEATH_FALLBACK */
{ 184, 6, 1.0f/7 }, /* DEATH_FALLFORWARD */
{ 190, 8, 1.0f/7 }, /* DEATH_FALLBACKSLOW */
{ 198, 1, 1.0f/5 }, /* BOOM */
};
typedef struct
{
int ident; // magic number. must be equal to "IPD2"
int version; // md2 version. must be equal to 8
int skinwidth; // width of the texture
int skinheight; // height of the texture
int framesize; // size of one frame in bytes
int num_skins; // number of textures
int num_xyz; // number of vertices
int num_st; // number of texture coordinates
int num_tris; // number of triangles
int num_glcmds; // number of opengl commands
int num_frames; // total number of frames
int ofs_skins; // offset to skin names (64 bytes each)
int ofs_st; // offset to s-t texture coordinates
int ofs_tris; // offset to triangles
int ofs_frames; // offset to frame data
int ofs_glcmds; // offset to opengl commands
int ofs_end; // offset to the end of file
} md2_header;
typedef struct
{
u8 x, y, z; // compressed vertex' (x, y, z) coordinates
u8 lightnormalindex; // index to a normal vector for the lighting
} md2_small_vertex;
typedef struct
{
ScePspFVector3 scale; //scale values
ScePspFVector3 translate; //translation vector
char name[16]; // frame name
md2_small_vertex verts[1]; // first vertex of this frame
} md2_frame;
int xMd2Load(xMd2* my_md2, char* filename)
{
X_LOG("Loading MD2.", 0);
md2_header header;
void* frame_data;
md2_frame* frame;
int i, j;
FILE* file = fopen(filename, "rb");
if (!file) return 0;
fread(&header, 1, sizeof(md2_header), file);
X_LOG("framesize: %i, num_frames: %i, num_verts: %i, num_glcmds: %i", header.framesize, header.num_frames, header.num_xyz, header.num_glcmds);
if (header.ident != MD2_IDENT || header.version != MD2_VERSION)
{
fclose(file);
return 0;
}
my_md2->num_frames = header.num_frames;
my_md2->num_verts = header.num_xyz;
my_md2->num_glcmds = header.num_glcmds;
my_md2->vertices = (ScePspFVector3*)x_malloc(my_md2->num_verts * my_md2->num_frames * sizeof(ScePspFVector3));
my_md2->normal_indices = (u8*)x_malloc(my_md2->num_verts * my_md2->num_frames * sizeof(u8));
my_md2->gl_commands = (int*)x_malloc(my_md2->num_glcmds * sizeof(int));
frame_data = x_malloc(my_md2->num_frames * header.framesize);
fseek(file, header.ofs_frames, SEEK_SET);
fread(frame_data, my_md2->num_frames * header.framesize, 1, file);
fseek(file, header.ofs_glcmds, SEEK_SET);
fread(my_md2->gl_commands, my_md2->num_glcmds * sizeof(int), 1, file);
fclose(file);
for (i = 0; i < my_md2->num_frames; i++)
{
frame = (md2_frame*)((u32)frame_data + header.framesize * i);
for (j = 0; j < my_md2->num_verts; j++)
{
my_md2->vertices[i*my_md2->num_verts + j].x = frame->verts[j].x * frame->scale.x + frame->translate.x;
my_md2->vertices[i*my_md2->num_verts + j].y = frame->verts[j].y * frame->scale.y + frame->translate.y;
my_md2->vertices[i*my_md2->num_verts + j].z = frame->verts[j].z * frame->scale.z + frame->translate.z;
my_md2->normal_indices[i*my_md2->num_verts + j] = frame->verts[j].lightnormalindex;
}
}
x_free(frame_data);
X_LOG("Successfully loaded MD2.", 0);
return 1;
}
void xMd2Free(xMd2* my_md2)
{
X_DELETE(my_md2->vertices);
X_DELETE(my_md2->gl_commands);
}
void xMd2AnimBind(xMd2Object* object, xMd2* my_md2, x_anim* my_anims, int num_anim, int default_anim)
{
if (!object || !my_md2) return;
object->md2_use = my_md2;
if (my_anims)
{
object->anims_use = my_anims;
object->num_anims = num_anim;
}
else
{
object->anims_use = default_anims;
object->num_anims = MD2_DEFAULT_NUM_ANIMS;
}
xMd2AnimSet(object, default_anim, 1, 0);
object->frame_cur = object->anims_use[object->anim].start;
xMd2AnimUpdate(object, 0.0f);
}
void xMd2AnimSet(xMd2Object* object, int anim, int loop, int ifnot)
{
if (!object || !object->anims_use) return;
if (anim < 0 || anim >= object->num_anims) anim = 0;
if (ifnot && anim == object->anim) return;
object->anim = anim;
if (loop) object->def_anim = object->anim;
object->frame_next = object->anims_use[object->anim].start;
object->time = 0.0f;
}
void xMd2AnimUpdate(xMd2Object* object, float dt)
{
if (!object || !object->anims_use) return;
object->time += dt;
while (object->time > object->anims_use[object->anim].delay)
{
object->time -= object->anims_use[object->anim].delay;
object->frame_cur = object->frame_next;
object->frame_next += 1;
if (object->frame_next >= object->anims_use[object->anim].start + object->anims_use[object->anim].size)
{
xMd2AnimSet(object, object->def_anim, 0, 0);
}
}
object->interp = object->time / object->anims_use[object->anim].delay;
}
typedef struct
{
float u, v;
ScePspFVector3 normal;
ScePspFVector3 vertex;
} md2_draw_vert;
#define md2_draw_vert_vtype (GU_TEXTURE_32BITF|GU_NORMAL_32BITF|GU_VERTEX_32BITF)
#ifdef X_MD2_HARDWARE
static md2_draw_vert md2_vert_buffer[2*MD2_MAX_VERTS];
#else
static md2_draw_vert md2_vert_buffer[MD2_MAX_VERTS];
static ScePspFVector3 md2_interp_buffer[MD2_MAX_VERTS];
static inline void interpolate_frames(xMd2Object* object)
{
int i;
int num = object->md2_use->num_verts;
ScePspFVector3* cur_ptr = &object->md2_use->vertices[object->md2_use->num_verts*object->frame_cur];
ScePspFVector3* next_ptr = &object->md2_use->vertices[object->md2_use->num_verts*object->frame_next];
for (i = 0; i < num; i++)
{
x_lerp(&md2_interp_buffer[i], cur_ptr, next_ptr, object->interp);
cur_ptr += 1;
next_ptr += 1;
}
}
#endif
void xMd2AnimDraw(xMd2Object* object)
{
if (!object || !object->md2_use || !object->md2_use->gl_commands || !object->md2_use->vertices || !object->anims_use) return;
md2_draw_vert* vertices = md2_vert_buffer;
md2_draw_vert* vert_ptr = X_UNCACHED(vertices);
int* glcmds = object->md2_use->gl_commands;
int prim;
int i, j;
#ifndef X_MD2_HARDWARE
interpolate_frames(object);
#endif
/* a zero marks the end of the commands */
while ( (i = *(glcmds++)) )
{
if (i < 0)
{
prim = GU_TRIANGLE_FAN;
i = -i;
}
else
{
prim = GU_TRIANGLE_STRIP;
}
for (j = 0; j < i; j++)
{
#ifdef X_MD2_HARDWARE
vert_ptr[0].u = ((float*)glcmds)[0];
vert_ptr[0].v = 1.0f - ((float*)glcmds)[1];
vert_ptr[0].normal = md2_normal_lookup[object->md2_use->normal_indices[glcmds[2]]];
vert_ptr[0].vertex = object->md2_use->vertices[object->md2_use->num_verts*object->frame_cur + glcmds[2]];
vert_ptr[1] = vert_ptr[0];
vert_ptr[1].vertex = object->md2_use->vertices[object->md2_use->num_verts*object->frame_next + glcmds[2]];
vert_ptr += 2;
#else
vert_ptr->u = ((float*)glcmds)[0];
vert_ptr->v = 1.0f - ((float*)glcmds)[1];
vert_ptr->normal = md2_normal_lookup[object->md2_use->normal_indices[glcmds[2]]];
vert_ptr->vertex = md2_interp_buffer[glcmds[2]];
vert_ptr += 1;
#endif
glcmds += 3;
}
//sceKernelDcacheWritebackInvalidateAll();
#ifdef X_MD2_HARDWARE
sceGuMorphWeight(0, 1.0f - object->interp);
sceGuMorphWeight(1, object->interp);
sceGumDrawArray(prim, md2_draw_vert_vtype|GU_VERTICES(2)|GU_TRANSFORM_3D, i, 0, vertices);
#else
sceGumDrawArray(prim, md2_draw_vert_vtype|GU_TRANSFORM_3D, i, 0, vertices);
#endif
vertices = vert_ptr;
}
}