-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.cpp
405 lines (303 loc) · 9.31 KB
/
model.cpp
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
#include "model.h"
#include <string.h>//for strcat
#include <math.h>
vec3_t anorms[162] = {
#include "anorms.h"
};
#include "const.h"
void model::Progress()
{
unsigned long now = GetTickCount();
double t = double(now - time) / 1000 * c_models_frames_per_second;
frameNr += t;
frameNr = fmod(frameNr, header.num_frames - 1);
time = now;
}
int model::Openmodel(const char *filename)
{
FILE *fp;
int i;
fp = fopen (filename, "rb");
if (!fp)
{
fprintf (stderr, "Error: couldn't open \"%s\"!\n", filename);
return 1;
}
//Read header
fread (&header, 1, sizeof (struct md2_header_t), fp);
if ((header.ident != 844121161) ||
(header.version != 8))
{
//Error!
fprintf (stderr, "Error: bad version or identifier\n");
fclose (fp);
return 1;
}
//Memory allocations
skins = (struct md2_skin_t *)
malloc (sizeof (struct md2_skin_t) * header.num_skins);
texcoords = (struct md2_texCoord_t *)
malloc (sizeof (struct md2_texCoord_t) * header.num_st);
triangles = (struct md2_triangle_t *)
malloc (sizeof (struct md2_triangle_t) * header.num_tris);
frames = (struct md2_frame_t *)
malloc (sizeof (struct md2_frame_t) * header.num_frames);
glcmds = (int *)malloc (sizeof (int) * header.num_glcmds);
//Read model data
fseek (fp, header.offset_skins, SEEK_SET);
fread (skins, sizeof (struct md2_skin_t),
header.num_skins, fp);
fseek (fp, header.offset_st, SEEK_SET);
fread (texcoords, sizeof (struct md2_texCoord_t),
header.num_st, fp);
fseek (fp, header.offset_tris, SEEK_SET);
fread (triangles, sizeof (struct md2_triangle_t),
header.num_tris, fp);
fseek (fp, header.offset_glcmds, SEEK_SET);
fread (glcmds, sizeof (int), header.num_glcmds, fp);
//Read frames
fseek (fp, header.offset_frames, SEEK_SET);
for (i = 0; i < header.num_frames; ++i)
{
//Memory allocation for vertices of this frame
frames[i].verts = (struct md2_vertex_t *)
malloc (sizeof (struct md2_vertex_t) * header.num_vertices);
//Read frame data
fread (frames[i].scale, sizeof (vec3_t), 1, fp);
fread (frames[i].translate, sizeof (vec3_t), 1, fp);
fread (frames[i].name, sizeof (char), 16, fp);
fread (frames[i].verts, sizeof (struct md2_vertex_t),
header.num_vertices, fp);
}
fclose (fp);
frameNr = 0;
return 0;//sekmingai
}
int model::OpenTexture(const char *filename)
{
tex_id = LoadTexture(filename);
return 0;
}
int model::Open(const char *modelFilename, const char *TexFilename)
{
if (Openmodel(modelFilename) || OpenTexture(TexFilename))
return 1;//klaida atidarant
if (header.num_frames == 1)
{
list = glGenLists(1);
glNewList(list, GL_COMPILE);
glRotated(180, 0, -1, 0);
glRotated(90, -1, 0, 0);
RenderFrame(0);
glRotated(90, 1, 0, 0);
glRotated(180, 0, 1, 0);
glEndList();
}
return 0;
}
int model::Open(const char *path)
{
char main_path[] = c_dir_model_main_path;
char end_model[] = c_file_model_model_end;
char end_tex[] = c_file_model_tex_end;
char m[128], t[128];
strcpy(m, main_path);
strcat(m, path);
strcat(m, end_model);
strcpy(t, main_path);
strcat(t, path);
strcat(t, end_tex);
return Open(m, t);
}
void model::RenderFrame(int n)
{
int i, j;
GLfloat s, t;
vec3_t v;
md2_frame_t *pframe;
md2_vertex_t *pvert;
/* check if n is in a valid range */
if ((n < 0) || (n > header.num_frames - 1))
return;
/* enable model's texture */
glBindTexture (GL_TEXTURE_2D, tex_id);
/* draw the model */
glBegin (GL_TRIANGLES);
/* draw each triangle */
for (i = 0; i < header.num_tris; ++i)
/* draw each vertex */
for (j = 0; j < 3; ++j)
{
pframe = &frames[n];
pvert = &pframe->verts[triangles[i].vertex[j]];
/* compute texture coordinates */
s = (GLfloat)texcoords[triangles[i].st[j]].s / header.skinwidth;
t = (GLfloat)texcoords[triangles[i].st[j]].t / header.skinheight;
/* pass texture coordinates to OpenGL */
glTexCoord2f (s, 1 - t);
/* normal vector */
glNormal3fv (anorms[pvert->normalIndex]);
/* calculate vertex real position */
//2 su 1 sukeista
v[0] = (pframe->scale[0] * pvert->v[0]) + pframe->translate[0];
v[1] = (pframe->scale[1] * pvert->v[1]) + pframe->translate[1];
v[2] = (pframe->scale[2] * pvert->v[2]) + pframe->translate[2];
//v[0] *= -1;//bug - reversed horizontal rendering. This fixes it
glVertex3fv (v);
}
glEnd ();
}
void model::Render()
{
int n = int(frameNr);
float interp = frameNr - n;
int i, j;
GLfloat s, t;
vec3_t v_curr, v_next, v, norm;
float *n_curr, *n_next;
md2_frame_t *pframe1, *pframe2;
md2_vertex_t *pvert1, *pvert2;
/* check if n is in a valid range */
if ((n < 0) || (n > header.num_frames))
return;
/* enable model's texture */
glBindTexture (GL_TEXTURE_2D, tex_id);
/* draw the model */
glBegin (GL_TRIANGLES);
/* draw each triangle */
for (i = 0; i < header.num_tris; ++i)
/* draw each vertex */
for (j = 0; j < 3; ++j)
{
pframe1 = &frames[n];
pframe2 = &frames[n + 1];
pvert1 = &pframe1->verts[triangles[i].vertex[j]];
pvert2 = &pframe2->verts[triangles[i].vertex[j]];
/* compute texture coordinates */
s = (GLfloat)texcoords[triangles[i].st[j]].s / header.skinwidth;
t = (GLfloat)texcoords[triangles[i].st[j]].t / header.skinheight;
/* pass texture coordinates to OpenGL */
glTexCoord2f (s, 1 - t);
/* interpolate normals */
n_curr = anorms[pvert1->normalIndex];
n_next = anorms[pvert2->normalIndex];
norm[0] = n_curr[0] + interp * (n_next[0] - n_curr[0]);
norm[2] = n_curr[1] + interp * (n_next[1] - n_curr[1]);
norm[1] = n_curr[2] + interp * (n_next[2] - n_curr[2]);
glNormal3fv (norm);
/* interpolate vertices */
v_curr[0] = pframe1->scale[0] * pvert1->v[0] + pframe1->translate[0];
v_curr[1] = pframe1->scale[1] * pvert1->v[1] + pframe1->translate[1];
v_curr[2] = pframe1->scale[2] * pvert1->v[2] + pframe1->translate[2];
v_next[0] = pframe2->scale[0] * pvert2->v[0] + pframe2->translate[0];
v_next[1] = pframe2->scale[1] * pvert2->v[1] + pframe2->translate[1];
v_next[2] = pframe2->scale[2] * pvert2->v[2] + pframe2->translate[2];
v[0] = v_curr[0] + interp * (v_next[0] - v_curr[0]);
v[2] = v_curr[1] + interp * (v_next[1] - v_curr[1]);
v[1] = v_curr[2] + interp * (v_next[2] - v_curr[2]);
glVertex3fv (v);
}
glEnd ();
}
void model::RenderFrameItpWithGLCmds ()
{
int n = 0;
float interp = 0;
int i, *pglcmds;
vec3_t v_curr, v_next, v, norm;
float *n_curr, *n_next;
struct md2_frame_t *pframe1, *pframe2;
struct md2_vertex_t *pvert1, *pvert2;
struct md2_glcmd_t *packet;
// Check if n is in a valid range
if ((n < 0) || (n > header.num_frames - 1))
return;
// Enable model's texture
glBindTexture (GL_TEXTURE_2D, tex_id);
// pglcmds points at the start of the command list
pglcmds = glcmds;
// Draw the model
while ((i = *(pglcmds++)) != 0)
{
if (i < 0)
{
glBegin (GL_TRIANGLE_FAN);
i = -i;
}
else
{
glBegin (GL_TRIANGLE_STRIP);
}
// Draw each vertex of this group
for (; i > 0; --i, pglcmds += 3)
{
packet = (struct md2_glcmd_t *)pglcmds;
pframe1 = &frames[n];
pframe2 = &frames[n + 1];
pvert1 = &pframe1->verts[packet->index];
pvert2 = &pframe2->verts[packet->index];
// Pass texture coordinates to OpenGL
glTexCoord2f (packet->s, packet->t);
//Interpolate normals
n_curr = anorms[pvert1->normalIndex];
n_next = anorms[pvert2->normalIndex];
norm[0] = n_curr[0] + interp * (n_next[0] - n_curr[0]);
norm[1] = n_curr[1] + interp * (n_next[1] - n_curr[1]);
norm[2] = n_curr[2] + interp * (n_next[2] - n_curr[2]);
glNormal3fv (norm);
// Interpolate vertices
v_curr[0] = pframe1->scale[0] * pvert1->v[0] + pframe1->translate[0];
v_curr[1] = pframe1->scale[1] * pvert1->v[1] + pframe1->translate[1];
v_curr[2] = pframe1->scale[2] * pvert1->v[2] + pframe1->translate[2];
v_next[0] = pframe2->scale[0] * pvert2->v[0] + pframe2->translate[0];
v_next[1] = pframe2->scale[1] * pvert2->v[1] + pframe2->translate[1];
v_next[2] = pframe2->scale[2] * pvert2->v[2] + pframe2->translate[2];
v[0] = v_curr[0] + interp * (v_next[0] - v_curr[0]);
v[1] = v_curr[1] + interp * (v_next[1] - v_curr[1]);
v[2] = v_curr[2] + interp * (v_next[2] - v_curr[2]);
glVertex3fv (v);
}
glEnd ();
}
}
/*
void model::Strech(double k)
{
int i, j;
for (i = 0; i < header.num_tris; i++)
{
for (j = 0; j < 3; j++)
frames[0].verts[i].v[j] = unsigned char(double(frames[0].verts[i].v[j])*k);
//frames[0].verts[i].v[0] *= k;
//frames[0].verts[i].v[1] *= k;
//frames[0].verts[i].v[2] *= k;
}
}
*/
void model::Draw()
{
glBindTexture(GL_TEXTURE_2D, tex_id);
if (header.num_frames == 1)
glCallList(list);
else
{
Progress();
Render();
}
}
void model::Draw(double x, double y, double z, double k)
{
glTranslated(x, y, z);
glRotated(k, 0, -1, 0);
Draw();
glRotated(k, 0, 1, 0);
glTranslated(-x, -y, -z);
}
void model::Draw(point p, double k)
{
Draw(p.x, 0, p.y, k);
}
void model::Draw(point3 p, double k)
{
Draw(p.x, p.y, p.z, k);
}