-
Notifications
You must be signed in to change notification settings - Fork 1
/
Md2Loader.h
385 lines (317 loc) · 9.72 KB
/
Md2Loader.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
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
#include <stdio.h>
#include <math.h>
#include <stdint.h>
#include <float.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
namespace Md2
{
typedef unsigned char u8;
typedef char s8;
typedef unsigned short u16;
typedef short s16;
typedef unsigned int u32;
typedef int s32;
typedef float f32;
typedef double f64;
typedef struct
{
f32 P[3];
} vec3;
typedef struct
{
f32 S;
f32 T;
} TexCoord;
typedef struct
{
s16 S;
s16 T;
} STIndex;
typedef struct
{
u8 V[3];
u8 NormalIndex;
} FramePoint;
typedef struct
{
f32 Scale[3];
f32 Translate[3];
s8 Name[16];
FramePoint Fp[1];
} Frame;
typedef struct
{
u16 MeshIndex[3];
u16 STIndex[3];
} Mesh;
typedef struct
{
u16 MeshIndex[3];
u16 STIndex[3];
u16 NormalIndex[3];
} MeshExt;
typedef struct
{
s32 Ident;
s32 Version;
s32 SkinWidth;
s32 SkinHeight;
s32 FrameSize;
s32 NumSkins;
s32 NumXYZ;
s32 NumST;
s32 NumTris;
s32 NumGLCmds;
s32 NumFrames;
s32 OffsetSkins;
s32 OffsetST;
s32 OffsetTris;
s32 OffsetFrames;
s32 OffsetGLCmds;
s32 OffsetEnd;
} ModelHeader;
typedef struct
{
s32 NumFrames;
s32 NumPoints;
s32 NumTriangles;
s32 NumST;
s32 FrameSize;
s32 TexWidth;
s32 TexHeight;
s32 CurrentFrame;
s32 NextFrame;
f32 Interpol;
MeshExt* TriIndex;
TexCoord* ST;
vec3* PointList;
vec3* NormalList;
} ModelData;
float FastInverseSqrt(float number)
{
long i;
float x2, y;
const float threehalfs = 1.5f;
x2 = number * 0.5f;
y = number;
i = *(long*)&y; // evil floating point bit level hacking
i = 0x5f3759df - (i >> 1); // what the fuck?
y = *(float*)&i;
y = y * (threehalfs - (x2 * y * y)); // 1st iteration
y = y * (threehalfs - (x2 * y * y)); // 2nd iteration, this can be removed - it's for more accuracy
return y;
}
void Normalize(vec3* v)
{
register float Length = FastInverseSqrt(v->P[0] * v->P[0] + v->P[1] * v->P[1] + v->P[2] * v->P[2]);
v->P[0] *= Length;
v->P[1] *= Length;
v->P[2] *= Length;
}
vec3 Cross(vec3* a, vec3* b)
{
vec3 r;
r.P[0] = a->P[1] * b->P[2] - a->P[2] * b->P[1];
r.P[1] = a->P[2] * b->P[0] - a->P[0] * b->P[2];
r.P[2] = a->P[0] * b->P[1] - a->P[1] * b->P[0];
return r;
}
vec3 Sub(vec3* a, vec3* b)
{
vec3 r;
r.P[0] = a->P[0] - b->P[0];
r.P[1] = a->P[1] - b->P[1];
r.P[2] = a->P[2] - b->P[2];
return r;
}
void GenerateNormals(u32 VertexCount, vec3* Vertices, u32 IndexCount, MeshExt* Indices, vec3* Normals, u32 j)
{
vec3* PointList = &Vertices[VertexCount * j];
vec3* NormalList = &Normals[VertexCount * j];
for (u32 i = 0; i < VertexCount; i++)
{
NormalList[i].P[0] = 0.0;
NormalList[i].P[1] = 0.0;
NormalList[i].P[2] = 0.0;
}
for (u32 i = 0; i < IndexCount; i++)
{
vec3* V0 = &PointList[Indices[i].MeshIndex[0]];
vec3* V1 = &PointList[Indices[i].MeshIndex[2]];
vec3* V2 = &PointList[Indices[i].MeshIndex[1]];
vec3 A = Sub(V1, V0);
vec3 B = Sub(V2, V0);
vec3 Normal = Cross(&A, &B);
Normalize(&Normal);
NormalList[Indices[i].MeshIndex[0]].P[0] += Normal.P[0];
NormalList[Indices[i].MeshIndex[0]].P[1] += Normal.P[1];
NormalList[Indices[i].MeshIndex[0]].P[2] += Normal.P[2];
NormalList[Indices[i].MeshIndex[2]].P[0] += Normal.P[0];
NormalList[Indices[i].MeshIndex[2]].P[1] += Normal.P[1];
NormalList[Indices[i].MeshIndex[2]].P[2] += Normal.P[2];
NormalList[Indices[i].MeshIndex[1]].P[0] += Normal.P[0];
NormalList[Indices[i].MeshIndex[1]].P[1] += Normal.P[1];
NormalList[Indices[i].MeshIndex[1]].P[2] += Normal.P[2];
}
for (u32 i = 0; i < VertexCount; i++)
Normalize(&NormalList[i]);
}
s8 Load(const s8* FileName, s32 TexWidth, s32 TexHeight, ModelData* Model)
{
ModelHeader* Header;
STIndex* STIndices;
Frame* FrameData;
vec3* PointList;
MeshExt* TriIndex;
Mesh* BufIndex;
s32 i;
s32 j;
FILE* File = fopen(FileName, "rb");
if (!File)
return 0;
fseek(File, 0, SEEK_END);
s32 FileLength = ftell(File);
fseek(File, 0, SEEK_SET);
s8* Buffer = (s8*)malloc(FileLength + 1);
fread(Buffer, 1, FileLength, File);
fclose(File);
Header = (ModelHeader*)Buffer;
printf("d: %d\n", Header->NumXYZ);
Model->PointList = (vec3*)malloc(Header->NumXYZ * Header->NumFrames * sizeof(vec3));
//-----//
Model->NormalList = (vec3*)malloc(Header->NumXYZ * Header->NumFrames * sizeof(vec3));
//-----//
Model->NumPoints = Header->NumXYZ;
Model->NumFrames = Header->NumFrames;
Model->FrameSize = Header->FrameSize;
for (j = 0; j < Header->NumFrames; j++)
{
FrameData = (Frame*)&Buffer[Header->OffsetFrames + Header->FrameSize * j];
PointList = (vec3*)&Model->PointList[Header->NumXYZ * j];
for (i = 0; i < Header->NumXYZ; i++)
{
PointList[i].P[0] = FrameData->Scale[0] * FrameData->Fp[i].V[0] + FrameData->Translate[0];
PointList[i].P[1] = FrameData->Scale[1] * FrameData->Fp[i].V[1] + FrameData->Translate[1];
PointList[i].P[2] = FrameData->Scale[2] * FrameData->Fp[i].V[2] + FrameData->Translate[2];
}
}
Model->ST = (TexCoord*)malloc(Header->NumST * sizeof(TexCoord));
Model->NumST = Header->NumST;
STIndices = (STIndex*)&Buffer[Header->OffsetST];
for (i = 0; i < Header->NumST; i++)
{
Model->ST[i].S = (f32)STIndices[i].S / (f32)TexWidth;
Model->ST[i].T = (f32)STIndices[i].T / (f32)TexHeight;
}
TriIndex = (MeshExt*)malloc(Header->NumTris * sizeof(MeshExt));
Model->NumTriangles = Header->NumTris;
Model->TriIndex = TriIndex;
BufIndex = (Mesh*)&Buffer[Header->OffsetTris];
//why this loop it doesnt do anything
for (j = 0; j < Model->NumFrames; j++)
{
for (i = 0; i < Header->NumTris; i++)
{
TriIndex[i].MeshIndex[0] = BufIndex[i].MeshIndex[0];
TriIndex[i].MeshIndex[1] = BufIndex[i].MeshIndex[1];
TriIndex[i].MeshIndex[2] = BufIndex[i].MeshIndex[2];
TriIndex[i].STIndex[0] = BufIndex[i].STIndex[0];
TriIndex[i].STIndex[1] = BufIndex[i].STIndex[1];
TriIndex[i].STIndex[2] = BufIndex[i].STIndex[2];
TriIndex[i].NormalIndex[0] = BufIndex[i].MeshIndex[0];
TriIndex[i].NormalIndex[1] = BufIndex[i].MeshIndex[1];
TriIndex[i].NormalIndex[2] = BufIndex[i].MeshIndex[2];
}
GenerateNormals(Header->NumXYZ, Model->PointList, Header->NumTris, TriIndex, Model->NormalList, j);
}
free(Buffer);
Model->CurrentFrame = 0;
Model->NextFrame = 1;
Model->Interpol = 0.0;
return 1;
}
void Free(ModelData* Model)
{
free(Model->TriIndex);
free(Model->ST);
free(Model->PointList);
}
class Proccess
{
public:
ModelData* Model;
vec3 Vertex[3];
TexCoord TexCoord[3];
vec3 Normal[3];
private:
f32 X0, Y0, Z0;
f32 X1, Y1, Z1;
vec3* PointList;
vec3* NextPointList;
vec3* NormalList;
public:
void Start(s32 StartFrame, s32 EndFrame)
{
if (StartFrame > Model->CurrentFrame)
Model->CurrentFrame = StartFrame;
// if ((StartFrame > Model->CurrentFrame))
if (Model->Interpol >= 1.0)
{
Model->Interpol = 0.0;
Model->CurrentFrame++;
if (Model->CurrentFrame >= EndFrame)
Model->CurrentFrame = StartFrame;
Model->NextFrame = Model->CurrentFrame + 1;
if (Model->NextFrame >= EndFrame)
Model->NextFrame = StartFrame;
}
PointList = &Model->PointList[Model->NumPoints * Model->CurrentFrame];
NextPointList = &Model->PointList[Model->NumPoints * Model->NextFrame];
NormalList = &Model->NormalList[Model->NumPoints * Model->CurrentFrame];
}
void End(f32 Percent)
{
Model->Interpol += Percent;
}
void Update(s32 i)
{
X0 = PointList[Model->TriIndex[i].MeshIndex[0]].P[0];
Y0 = PointList[Model->TriIndex[i].MeshIndex[0]].P[1];
Z0 = PointList[Model->TriIndex[i].MeshIndex[0]].P[2];
X1 = NextPointList[Model->TriIndex[i].MeshIndex[0]].P[0];
Y1 = NextPointList[Model->TriIndex[i].MeshIndex[0]].P[1];
Z1 = NextPointList[Model->TriIndex[i].MeshIndex[0]].P[2];
Vertex[0].P[0] = X0 + Model->Interpol * (X1 - X0);
Vertex[0].P[1] = Y0 + Model->Interpol * (Y1 - Y0);
Vertex[0].P[2] = Z0 + Model->Interpol * (Z1 - Z0);
//---------//
X0 = PointList[Model->TriIndex[i].MeshIndex[2]].P[0];
Y0 = PointList[Model->TriIndex[i].MeshIndex[2]].P[1];
Z0 = PointList[Model->TriIndex[i].MeshIndex[2]].P[2];
X1 = NextPointList[Model->TriIndex[i].MeshIndex[2]].P[0];
Y1 = NextPointList[Model->TriIndex[i].MeshIndex[2]].P[1];
Z1 = NextPointList[Model->TriIndex[i].MeshIndex[2]].P[2];
Vertex[2].P[0] = X0 + Model->Interpol * (X1 - X0);
Vertex[2].P[1] = Y0 + Model->Interpol * (Y1 - Y0);
Vertex[2].P[2] = Z0 + Model->Interpol * (Z1 - Z0);
//---------//
X0 = PointList[Model->TriIndex[i].MeshIndex[1]].P[0];
Y0 = PointList[Model->TriIndex[i].MeshIndex[1]].P[1];
Z0 = PointList[Model->TriIndex[i].MeshIndex[1]].P[2];
X1 = NextPointList[Model->TriIndex[i].MeshIndex[1]].P[0];
Y1 = NextPointList[Model->TriIndex[i].MeshIndex[1]].P[1];
Z1 = NextPointList[Model->TriIndex[i].MeshIndex[1]].P[2];
Vertex[1].P[0] = X0 + Model->Interpol * (X1 - X0);
Vertex[1].P[1] = Y0 + Model->Interpol * (Y1 - Y0);
Vertex[1].P[2] = Z0 + Model->Interpol * (Z1 - Z0);
TexCoord[0] = Model->ST[Model->TriIndex[i].STIndex[0]];
TexCoord[2] = Model->ST[Model->TriIndex[i].STIndex[2]];
TexCoord[1] = Model->ST[Model->TriIndex[i].STIndex[1]];
Normal[0] = NormalList[Model->TriIndex[i].NormalIndex[0]];
Normal[2] = NormalList[Model->TriIndex[i].NormalIndex[2]];
Normal[1] = NormalList[Model->TriIndex[i].NormalIndex[1]];
}
};
}