-
Notifications
You must be signed in to change notification settings - Fork 2
/
gl_mesh.c
514 lines (418 loc) · 11.8 KB
/
gl_mesh.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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/*
Copyright (C) 1996-1997 Id Software, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
// gl_mesh.c: triangle model functions
#include "quakedef.h"
#ifndef RQM_SV_ONLY
/*
=================================================================
ALIAS MODEL DISPLAY LIST GENERATION
=================================================================
*/
#define MAX_CMDS 16384 // was 8192
/*****JDH*****/
//model_t *aliasmodel;
//aliashdr_t *paliashdr;
/*****JDH*****/
qboolean used[MAX_CMDS];
// the command list holds counts and s/t values that are valid for
// every frame
int commands[MAX_CMDS];
int numcommands;
// all frames will have their vertexes rearranged and expanded
// so they are in the order expected by the command list
int vertexorder[MAX_CMDS];
int numorder;
int allverts, alltris;
#define MAX_STRIPS 256 // was 128
int stripverts[MAX_STRIPS];
int striptris[MAX_STRIPS];
int stripcount;
#ifdef HEXEN2_SUPPORT
int stripstverts[MAX_STRIPS];
#endif
extern cvar_t gl_cache_ms2;
/*
================
StripLength
================
*/
int StripLength (int numtris, int starttri, int startv)
{
int m1, m2, j, k;
mtriangle_t *last, *check;
#ifdef HEXEN2_SUPPORT
int st1, st2;
#endif
used[starttri] = 2;
last = &triangles[starttri];
stripverts[0] = last->vertindex[(startv)%3];
stripverts[1] = last->vertindex[(startv+1)%3];
stripverts[2] = last->vertindex[(startv+2)%3];
#ifdef HEXEN2_SUPPORT
stripstverts[0] = last->stindex[(startv)%3];
stripstverts[1] = last->stindex[(startv+1)%3];
stripstverts[2] = last->stindex[(startv+2)%3];
st1 = last->stindex[(startv+2)%3];
st2 = last->stindex[(startv+1)%3];
#endif
striptris[0] = starttri;
stripcount = 1;
m1 = last->vertindex[(startv+2)%3];
m2 = last->vertindex[(startv+1)%3];
// look for a matching triangle
nexttri:
for (j=starttri+1, check=&triangles[starttri+1] ; j<numtris ; j++, check++)
{
if (check->facesfront != last->facesfront)
continue;
for (k=0 ; k<3 ; k++)
{
if (check->vertindex[k] != m1)
continue;
if (check->vertindex[ (k+1)%3 ] != m2)
continue;
#ifdef HEXEN2_SUPPORT
if (check->stindex[k] != st1)
continue;
if (check->stindex[ (k+1)%3 ] != st2)
continue;
#endif
// this is the next part of the fan
// if we can't use this triangle, this tristrip is done
if (used[j])
goto done;
// the new edge
if (stripcount & 1)
{
m2 = check->vertindex[ (k+2)%3 ];
#ifdef HEXEN2_SUPPORT
st2 = check->stindex[ (k+2)%3 ];
#endif
}
else
{
m1 = check->vertindex[ (k+2)%3 ];
#ifdef HEXEN2_SUPPORT
st1 = check->stindex[ (k+2)%3 ];
#endif
}
#ifdef HEXEN2_SUPPORT
stripstverts[stripcount+2] = check->stindex[ (k+2)%3 ];
#endif
stripverts[stripcount+2] = check->vertindex[ (k+2)%3 ];
striptris[stripcount] = j;
stripcount++;
used[j] = 2;
goto nexttri;
}
}
done:
// clear the temp used flags
for (j=starttri+1 ; j<numtris ; j++)
if (used[j] == 2)
used[j] = 0;
return stripcount;
}
/*
===========
FanLength
===========
*/
int FanLength (int numtris, int starttri, int startv)
{
int m1, m2, j, k;
mtriangle_t *last, *check;
#ifdef HEXEN2_SUPPORT
int st1, st2;
#endif
used[starttri] = 2;
last = &triangles[starttri];
stripverts[0] = last->vertindex[(startv)%3];
stripverts[1] = last->vertindex[(startv+1)%3];
stripverts[2] = last->vertindex[(startv+2)%3];
#ifdef HEXEN2_SUPPORT
stripstverts[0] = last->stindex[(startv)%3];
stripstverts[1] = last->stindex[(startv+1)%3];
stripstverts[2] = last->stindex[(startv+2)%3];
st1 = last->stindex[(startv+2)%3];
st2 = last->stindex[(startv+1)%3];
#endif
striptris[0] = starttri;
stripcount = 1;
m1 = last->vertindex[(startv+0)%3];
m2 = last->vertindex[(startv+2)%3];
// look for a matching triangle
nexttri:
for (j=starttri+1, check=&triangles[starttri+1] ; j<numtris ; j++, check++)
{
if (check->facesfront != last->facesfront)
continue;
for (k=0 ; k<3 ; k++)
{
if (check->vertindex[k] != m1)
continue;
if (check->vertindex[ (k+1)%3 ] != m2)
continue;
#ifdef HEXEN2_SUPPORT
if (check->stindex[k] != st1)
continue;
if (check->stindex[ (k+1)%3 ] != st2)
continue;
#endif
// this is the next part of the fan
// if we can't use this triangle, this tristrip is done
if (used[j])
goto done;
// the new edge
m2 = check->vertindex[ (k+2)%3 ];
#ifdef HEXEN2_SUPPORT
st2 = check->stindex[ (k+2)%3 ];
stripstverts[stripcount+2] = st2;
#endif
stripverts[stripcount+2] = m2;
striptris[stripcount] = j;
stripcount++;
used[j] = 2;
goto nexttri;
}
}
done:
// clear the temp used flags
for (j=starttri+1 ; j<numtris ; j++)
if (used[j] == 2)
used[j] = 0;
return stripcount;
}
/*
================
BuildTris
Generate a list of trifans or strips
for the model, which holds for all frames
================
*/
void BuildTris (aliashdr_t *pheader)
{
int i, j, k, startv, len, bestlen, besttype, type;
float s, t;
int bestverts[1024], besttris[1024];
#ifdef HEXEN2_SUPPORT
int beststverts[1024];
#endif
// build tristrips
numorder = 0;
numcommands = 0;
memset (used, 0, sizeof(used));
for (i=0 ; i<pheader->numtris ; i++)
{
// pick an unused triangle and start the trifan
if (used[i])
continue;
bestlen = 0;
for (type=0 ; type<2 ; type++)
// type = 1;
{
for (startv=0 ; startv < 3 ; startv++)
{
len = (type == 1) ? StripLength (pheader->numtris, i, startv) :
FanLength (pheader->numtris, i, startv);
if (len > bestlen)
{
besttype = type;
bestlen = len;
for (j=0 ; j<bestlen+2 ; j++)
{
bestverts[j] = stripverts[j];
#ifdef HEXEN2_SUPPORT
beststverts[j] = stripstverts[j];
#endif
}
for (j=0 ; j<bestlen ; j++)
besttris[j] = striptris[j];
}
}
}
// mark the tris on the best strip as used
for (j=0 ; j<bestlen ; j++)
used[besttris[j]] = 1;
commands[numcommands++] = (besttype == 1) ? (bestlen+2) : -(bestlen+2);
for (j=0 ; j<bestlen+2 ; j++)
{
// emit a vertex into the reorder buffer
k = bestverts[j];
vertexorder[numorder++] = k;
#ifdef HEXEN2_SUPPORT
k = beststverts[j];
#endif
// emit s/t coords into the commands stream
s = stverts[k].s;
t = stverts[k].t;
if (!triangles[besttris[0]].facesfront && stverts[k].onseam)
s += pheader->skinwidth / 2; // on back side
s = (s + 0.5) / pheader->skinwidth;
t = (t + 0.5) / pheader->skinheight;
*(float *)&commands[numcommands++] = s;
*(float *)&commands[numcommands++] = t;
}
}
commands[numcommands++] = 0; // end of list marker
if (developer.value > 1)
Con_Printf ("%3i tri %3i vert %3i cmd\n", pheader->numtris, numorder, numcommands);
allverts += numorder;
alltris += pheader->numtris;
}
/*
================
GL_MakeAliasModelDisplayLists
================
*/
void GL_MakeAliasModelDisplayLists (model_t *m, aliashdr_t *paliashdr)
{
int i, j, count, texflags, new_width, new_height, cmdcount;
int *cmds, *loadcmds;
trivertx_t *verts;
skingroup_t *skingroup;
skin_t *skin;
char cache[MAX_QPATH], fullpath[MAX_OSPATH];
FILE *f = NULL;
com_fileinfo_t fileinfo;
const searchpath_t *path1;
// JDH: added caching back in - faster than rebuilding on slow machines
if (gl_cache_ms2.value)
{
//
// look for a cached version
//
count = Q_strcpy (cache, "glquake/", sizeof(cache));
count += COM_StripExtension (COM_SkipPath(m->name), cache+count, sizeof(cache)-count);
Q_strcpy (cache+count, ".ms2", sizeof(cache)-count);
if (COM_FindFile (m->name, 0, &fileinfo)) // JDH: look for ms2 only in same folder as mdl file
{
path1 = fileinfo.searchpath;
if (COM_FindFile (cache, 0, &fileinfo))
{
if ((path1 == fileinfo.searchpath) || COM_FilenamesEqual (path1->filename, fileinfo.searchpath->filename))
f = COM_FOpenFile (cache, 0, NULL);
else
{
if (!fileinfo.searchpath->pack)
COM_DeleteFile (va("%s/%s", fileinfo.searchpath->filename, cache)); // so it's not found next time
}
}
}
}
if (f)
{
fread (&numcommands, 4, 1, f);
fread (&numorder, 4, 1, f);
fread (&commands, numcommands * sizeof(commands[0]), 1, f);
fread (&vertexorder, numorder * sizeof(vertexorder[0]), 1, f);
fclose (f);
}
else
{
//
// build it from scratch
//
if (developer.value > 1)
Con_Printf ("meshing %s...\n",m->name);
BuildTris (paliashdr); // trifans or lists
if (gl_cache_ms2.value)
{
//
// save out the cached version
//
Q_snprintfz (fullpath, sizeof(fullpath), "%s/%s", path1 ? path1->filename : com_gamedir, cache);
COM_CreatePath (fullpath);
f = fopen (fullpath, "wb");
if (f)
{
fwrite (&numcommands, 4, 1, f);
fwrite (&numorder, 4, 1, f);
fwrite (&commands, numcommands * sizeof(commands[0]), 1, f);
fwrite (&vertexorder, numorder * sizeof(vertexorder[0]), 1, f);
fclose (f);
}
}
}
// save the data out
paliashdr->poseverts = numorder;
//cmds = Hunk_Alloc (numcommands * 4);
cmds = Hunk_Alloc (numcommands * 6);
paliashdr->commands = (byte *)cmds - (byte *)paliashdr;
// memcpy (cmds, commands, numcommands * 4);
// because of external textures, each skin for a given model could be a different size.
// This means coordinate scaling has to occur when model is drawn (either that or
// maintain multiple command lists)
texflags = (gl_picmip_all.value ? TEX_MIPMAP : 0);
if (m->modhint == MOD_PLAYER)
texflags |= TEX_PLAYER;
for (i=0; i < paliashdr->numskins; i++)
{
skingroup = &paliashdr->skins[i];
for (j = 0; j < 4; j++)
{
skin = &skingroup->skins[j];
if (texflags & TEX_PLAYER)
{
// player skins get padded if enlarged, but not if shrunk -- FIXME?
GL_ScaleDimensions (skin->h_value, skin->v_value, &new_width, &new_height, texflags);
}
else
{
// other skins get padded _before_ reducing size (if reduction is necessary)
for (new_width = 1 ; new_width < skin->h_value ; new_width <<= 1);
for (new_height = 1 ; new_height < skin->v_value ; new_height <<= 1);
}
// Initially, h_value & v_value hold skin width & height, respectively.
// From here on, however, they will store texture coordinates.
if ((new_width >= skin->h_value) && (new_height >= skin->v_value))
{
// skin is padded only if it is scaled up
skin->h_value /= (float) new_width;
skin->v_value /= (float) new_height;
}
else
{
skin->h_value = 1.0;
skin->v_value = 1.0;
}
}
}
// JDH: for compatibility with MD2 models, another value is stored after each coordinate pair.
// (MD2 can store texture coords in a different order than model coords; this value ties the
// two pairs together. But here, the 2 indices are the same).
cmdcount = 0;
loadcmds = commands;
while(1)
{
*cmds++ = count = *loadcmds++;
if (!count)
break;
if (count < 0)
count = -count;
do
{
*(float *)cmds++ = *(float *)loadcmds++;
*(float *)cmds++ = *(float *)loadcmds++;
*cmds++ = cmdcount++;
} while (--count);
}
verts = Hunk_Alloc (paliashdr->numposes * paliashdr->poseverts * sizeof(trivertx_t));
paliashdr->posedata = (byte *)verts - (byte *)paliashdr;
for (i=0 ; i<paliashdr->numposes ; i++)
for (j=0 ; j<numorder ; j++)
*verts++ = poseverts[i][vertexorder[j]];
}
#endif //#ifndef RQM_SV_ONLY