]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - model_shared.c
added Electro's smoke trail tweaks
[xonotic/darkplaces.git] / model_shared.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // models.c -- model loading and caching
21
22 // models are the only shared resource between a client and server running
23 // on the same machine.
24
25 #include "quakedef.h"
26
27 model_t *loadmodel;
28
29 // LordHavoc: increased from 512 to 2048
30 #define MAX_MOD_KNOWN   2048
31 static model_t mod_known[MAX_MOD_KNOWN];
32
33 rtexture_t *r_notexture;
34 rtexturepool_t *r_notexturepool;
35
36 texture_t r_surf_notexture;
37
38 void Mod_SetupNoTexture(void)
39 {
40         int x, y;
41         qbyte pix[16][16][4];
42
43         // this makes a light grey/dark grey checkerboard texture
44         for (y = 0;y < 16;y++)
45         {
46                 for (x = 0;x < 16;x++)
47                 {
48                         if ((y < 8) ^ (x < 8))
49                         {
50                                 pix[y][x][0] = 128;
51                                 pix[y][x][1] = 128;
52                                 pix[y][x][2] = 128;
53                                 pix[y][x][3] = 255;
54                         }
55                         else
56                         {
57                                 pix[y][x][0] = 64;
58                                 pix[y][x][1] = 64;
59                                 pix[y][x][2] = 64;
60                                 pix[y][x][3] = 255;
61                         }
62                 }
63         }
64
65         r_notexturepool = R_AllocTexturePool();
66         r_notexture = R_LoadTexture2D(r_notexturepool, "notexture", 16, 16, &pix[0][0][0], TEXTYPE_RGBA, TEXF_MIPMAP, NULL);
67 }
68
69 extern void Mod_BrushStartup (void);
70 extern void Mod_BrushShutdown (void);
71
72 static void mod_start(void)
73 {
74         int i;
75         for (i = 0;i < MAX_MOD_KNOWN;i++)
76                 if (mod_known[i].name[0])
77                         Mod_UnloadModel(&mod_known[i]);
78         Mod_LoadModels();
79
80         Mod_SetupNoTexture();
81         Mod_BrushStartup();
82 }
83
84 static void mod_shutdown(void)
85 {
86         int i;
87         for (i = 0;i < MAX_MOD_KNOWN;i++)
88                 if (mod_known[i].name[0])
89                         Mod_UnloadModel(&mod_known[i]);
90
91         R_FreeTexturePool(&r_notexturepool);
92         Mod_BrushShutdown();
93 }
94
95 static void mod_newmap(void)
96 {
97 }
98
99 /*
100 ===============
101 Mod_Init
102 ===============
103 */
104 static void Mod_Print (void);
105 void Mod_Init (void)
106 {
107         Mod_BrushInit();
108         Mod_AliasInit();
109         Mod_SpriteInit();
110
111         Cmd_AddCommand ("modellist", Mod_Print);
112 }
113
114 void Mod_RenderInit(void)
115 {
116         R_RegisterModule("Models", mod_start, mod_shutdown, mod_newmap);
117 }
118
119 void Mod_FreeModel (model_t *mod)
120 {
121         R_FreeTexturePool(&mod->texturepool);
122         Mem_FreePool(&mod->mempool);
123
124         // clear the struct to make it available
125         memset(mod, 0, sizeof(model_t));
126 }
127
128 void Mod_UnloadModel (model_t *mod)
129 {
130         char name[MAX_QPATH];
131         qboolean isworldmodel;
132         strcpy(name, mod->name);
133         isworldmodel = mod->isworldmodel;
134         Mod_FreeModel(mod);
135         strcpy(mod->name, name);
136         mod->isworldmodel = isworldmodel;
137         mod->needload = true;
138 }
139
140 /*
141 ==================
142 Mod_LoadModel
143
144 Loads a model
145 ==================
146 */
147 static model_t *Mod_LoadModel (model_t *mod, qboolean crash, qboolean checkdisk, qboolean isworldmodel)
148 {
149         unsigned int crc;
150         void *buf;
151
152         mod->used = true;
153
154         if (mod->name[0] == '*') // submodel
155                 return mod;
156
157         crc = 0;
158         buf = NULL;
159         if (!mod->needload)
160         {
161                 if (checkdisk)
162                 {
163                         buf = COM_LoadFile (mod->name, false);
164                         if (!buf)
165                         {
166                                 if (crash)
167                                         Host_Error ("Mod_LoadModel: %s not found", mod->name); // LordHavoc: Sys_Error was *ANNOYING*
168                                 return NULL;
169                         }
170
171                         crc = CRC_Block(buf, com_filesize);
172                 }
173                 else
174                         crc = mod->crc;
175
176                 if (mod->crc == crc && mod->isworldmodel == isworldmodel)
177                 {
178                         if (buf)
179                                 Mem_Free(buf);
180                         return mod; // already loaded
181                 }
182         }
183
184         Con_DPrintf("loading model %s\n", mod->name);
185
186         if (!buf)
187         {
188                 buf = COM_LoadFile (mod->name, false);
189                 if (!buf)
190                 {
191                         if (crash)
192                                 Host_Error ("Mod_LoadModel: %s not found", mod->name);
193                         return NULL;
194                 }
195                 crc = CRC_Block(buf, com_filesize);
196         }
197
198         // allocate a new model
199         loadmodel = mod;
200
201         // LordHavoc: unload the existing model in this slot (if there is one)
202         Mod_UnloadModel(mod);
203         mod->isworldmodel = isworldmodel;
204         mod->needload = false;
205         mod->used = true;
206         mod->crc = crc;
207         // errors can prevent the corresponding mod->error = false;
208         mod->error = true;
209
210         // all models use memory, so allocate a memory pool
211         mod->mempool = Mem_AllocPool(mod->name);
212         // all models load textures, so allocate a texture pool
213         if (cls.state != ca_dedicated)
214                 mod->texturepool = R_AllocTexturePool();
215
216         // call the apropriate loader
217              if (!memcmp(buf, "IDPO"    , 4)) Mod_LoadQ1AliasModel(mod, buf);
218         else if (!memcmp(buf, "IDP2"    , 4)) Mod_LoadQ2AliasModel(mod, buf);
219         else if (!memcmp(buf, "IDP3"    , 4)) Mod_LoadQ3AliasModel(mod, buf);
220         else if (!memcmp(buf, "ZYMOTIC" , 7)) Mod_LoadZymoticModel(mod, buf);
221         else if (!memcmp(buf, "IDSP"    , 4)) Mod_LoadSpriteModel (mod, buf);
222         else                                  Mod_LoadBrushModel  (mod, buf);
223
224         Mem_Free(buf);
225
226         // no errors occurred
227         mod->error = false;
228         return mod;
229 }
230
231 void Mod_CheckLoaded (model_t *mod)
232 {
233         if (mod)
234         {
235                 if (mod->needload)
236                         Mod_LoadModel(mod, true, true, mod->isworldmodel);
237                 else
238                 {
239                         if (mod->type == mod_invalid)
240                                 Host_Error("Mod_CheckLoaded: invalid model\n");
241                         mod->used = true;
242                         return;
243                 }
244         }
245 }
246
247 /*
248 ===================
249 Mod_ClearAll
250 ===================
251 */
252 void Mod_ClearAll (void)
253 {
254 }
255
256 void Mod_ClearErrorModels (void)
257 {
258         int i;
259         model_t *mod;
260
261         for (i = 0, mod = mod_known;i < MAX_MOD_KNOWN;i++, mod++)
262                 if (mod->error)
263                         Mod_FreeModel(mod);
264 }
265
266 void Mod_ClearUsed(void)
267 {
268         int i;
269         model_t *mod;
270
271         for (i = 0, mod = mod_known;i < MAX_MOD_KNOWN;i++, mod++)
272                 if (mod->name[0])
273                         mod->used = false;
274 }
275
276 void Mod_PurgeUnused(void)
277 {
278         int i;
279         model_t *mod;
280
281         for (i = 0, mod = mod_known;i < MAX_MOD_KNOWN;i++, mod++)
282                 if (mod->name[0])
283                         if (!mod->used)
284                                 Mod_FreeModel(mod);
285 }
286
287 void Mod_LoadModels(void)
288 {
289         int i;
290         model_t *mod;
291
292         for (i = 0, mod = mod_known;i < MAX_MOD_KNOWN;i++, mod++)
293                 if (mod->name[0])
294                         if (mod->used)
295                                 Mod_CheckLoaded(mod);
296 }
297
298 /*
299 ==================
300 Mod_FindName
301
302 ==================
303 */
304 model_t *Mod_FindName (const char *name)
305 {
306         int i;
307         model_t *mod, *freemod;
308
309         if (!name[0])
310                 Host_Error ("Mod_ForName: NULL name");
311
312 // search the currently loaded models
313         freemod = NULL;
314         for (i = 0, mod = mod_known;i < MAX_MOD_KNOWN;i++, mod++)
315         {
316                 if (mod->name[0])
317                 {
318                         if (!strcmp (mod->name, name))
319                         {
320                                 mod->used = true;
321                                 return mod;
322                         }
323                 }
324                 else if (freemod == NULL)
325                         freemod = mod;
326         }
327
328         if (freemod)
329         {
330                 mod = freemod;
331                 strcpy (mod->name, name);
332                 mod->needload = true;
333                 mod->used = true;
334                 return mod;
335         }
336
337         Host_Error ("Mod_FindName: ran out of models\n");
338         return NULL;
339 }
340
341 /*
342 ==================
343 Mod_TouchModel
344
345 ==================
346 */
347 void Mod_TouchModel (const char *name)
348 {
349         model_t *mod;
350
351         mod = Mod_FindName (name);
352         mod->used = true;
353 }
354
355 /*
356 ==================
357 Mod_ForName
358
359 Loads in a model for the given name
360 ==================
361 */
362 model_t *Mod_ForName (const char *name, qboolean crash, qboolean checkdisk, qboolean isworldmodel)
363 {
364         return Mod_LoadModel (Mod_FindName (name), crash, checkdisk, isworldmodel);
365 }
366
367 qbyte *mod_base;
368
369
370 //=============================================================================
371
372 /*
373 ================
374 Mod_Print
375 ================
376 */
377 static void Mod_Print (void)
378 {
379         int             i;
380         model_t *mod;
381
382         Con_Printf ("Loaded models:\n");
383         for (i = 0, mod = mod_known;i < MAX_MOD_KNOWN;i++, mod++)
384                 if (mod->name[0])
385                         Con_Printf ("%4iK %s\n", mod->mempool ? (mod->mempool->totalsize + 1023) / 1024 : 0, mod->name);
386 }
387
388 int Mod_FindTriangleWithEdge(const int *elements, int numtriangles, int start, int end, int ignore)
389 {
390         int i, match, count;
391         count = 0;
392         match = -1;
393         for (i = 0;i < numtriangles;i++, elements += 3)
394         {
395                      if ((elements[0] == start && elements[1] == end)
396                       || (elements[1] == start && elements[2] == end)
397                       || (elements[2] == start && elements[0] == end))
398                 {
399                         if (i != ignore)
400                                 match = i;
401                         count++;
402                 }
403                 else if ((elements[1] == start && elements[0] == end)
404                       || (elements[2] == start && elements[1] == end)
405                       || (elements[0] == start && elements[2] == end))
406                         count++;
407         }
408         // detect edges shared by three triangles and make them seams
409         if (count > 2)
410                 match = -1;
411         return match;
412 }
413
414 void Mod_BuildTriangleNeighbors(int *neighbors, const int *elements, int numtriangles)
415 {
416         int i, *n;
417         const int *e;
418         for (i = 0, e = elements, n = neighbors;i < numtriangles;i++, e += 3, n += 3)
419         {
420                 n[0] = Mod_FindTriangleWithEdge(elements, numtriangles, e[1], e[0], i);
421                 n[1] = Mod_FindTriangleWithEdge(elements, numtriangles, e[2], e[1], i);
422                 n[2] = Mod_FindTriangleWithEdge(elements, numtriangles, e[0], e[2], i);
423         }
424 }
425
426 void Mod_ValidateElements(const int *elements, int numtriangles, int numverts, const char *filename, int fileline)
427 {
428         int i;
429         for (i = 0;i < numtriangles * 3;i++)
430                 if ((unsigned int)elements[i] >= (unsigned int)numverts)
431                         Con_Printf("Mod_ValidateElements: out of bounds element detected at %s:%d\n", filename, fileline);
432 }
433
434 /*
435 a note on the cost of executing this function:
436 per triangle: 188 (83 42 13 45 4 1)
437 assignments: 83 (20 3 3 3 1 4 4 1 3 4 3 4 30)
438 adds: 42 (2 2 2 2 3 2 2 27)
439 subtracts: 13 (3 3 3 1 3)
440 multiplies: 45 (6 3 6 6 3 3 6 6 6)
441 rsqrts: 4 (1 1 1 1)
442 compares: 1 (1)
443 per vertex: 39 (12 6 18 3)
444 assignments: 12 (4 4 4)
445 adds: 6 (2 2 2)
446 multiplies: 18 (6 6 6)
447 rsqrts: 3 (1 1 1)
448 */
449
450 void Mod_BuildTextureVectorsAndNormals(int numverts, int numtriangles, const float *vertex, const float *texcoord, const int *elements, float *svectors, float *tvectors, float *normals)
451 {
452         int i, tnum, voffset;
453         float vert[3][4], vec[3][4], sdir[3], tdir[3], normal[3], f, *v;
454         const int *e;
455         // clear the vectors
456         memset(svectors, 0, numverts * sizeof(float[4]));
457         memset(tvectors, 0, numverts * sizeof(float[4]));
458         memset(normals, 0, numverts * sizeof(float[4]));
459         // process each vertex of each triangle and accumulate the results
460         for (tnum = 0, e = elements;tnum < numtriangles;tnum++, e += 3)
461         {
462                 // calculate texture matrix for triangle
463                 // 20 assignments
464                 voffset = e[0] * 4;
465                 vert[0][0] = vertex[voffset+0];
466                 vert[0][1] = vertex[voffset+1];
467                 vert[0][2] = vertex[voffset+2];
468                 vert[0][3] = texcoord[voffset];
469                 voffset = e[1] * 4;
470                 vert[1][0] = vertex[voffset+0];
471                 vert[1][1] = vertex[voffset+1];
472                 vert[1][2] = vertex[voffset+2];
473                 vert[1][3] = texcoord[voffset];
474                 voffset = e[2] * 4;
475                 vert[2][0] = vertex[voffset+0];
476                 vert[2][1] = vertex[voffset+1];
477                 vert[2][2] = vertex[voffset+2];
478                 vert[2][3] = texcoord[voffset];
479                 // 3 assignments, 3 subtracts
480                 VectorSubtract(vert[1], vert[0], vec[0]);
481                 // 3 assignments, 3 subtracts
482                 VectorSubtract(vert[2], vert[0], vec[1]);
483                 // 3 assignments, 3 subtracts, 6 multiplies
484                 CrossProduct(vec[0], vec[1], normal);
485                 // 1 assignment, 2 adds, 3 multiplies, 1 compare
486                 if (DotProduct(normal, normal) >= 0.001)
487                 {
488                         // 4 assignments, 1 rsqrt, 2 adds, 6 multiplies
489                         VectorNormalize(normal);
490                         sdir[0] = (vert[1][3] - vert[0][3]) * (vert[2][0] - vert[0][0]) - (vert[2][3] - vert[0][3]) * (vert[1][0] - vert[0][0]);
491                         sdir[1] = (vert[1][3] - vert[0][3]) * (vert[2][1] - vert[0][1]) - (vert[2][3] - vert[0][3]) * (vert[1][1] - vert[0][1]);
492                         sdir[2] = (vert[1][3] - vert[0][3]) * (vert[2][2] - vert[0][2]) - (vert[2][3] - vert[0][3]) * (vert[1][2] - vert[0][2]);
493                         // 4 assignments, 1 rsqrt, 2 adds, 6 multiplies
494                         VectorNormalize(sdir);
495                         // 1 assignments, 1 negates, 2 adds, 3 multiplies
496                         f = -DotProduct(sdir, normal);
497                         // 3 assignments, 3 adds, 3 multiplies
498                         VectorMA(sdir, f, normal, sdir);
499                         // 4 assignments, 1 rsqrt, 2 adds, 6 multiplies
500                         VectorNormalize(sdir);
501                         // 3 assignments, 3 subtracts, 6 multiplies
502                         CrossProduct(sdir, normal, tdir);
503                         // this is probably not necessary
504                         // 4 assignments, 1 rsqrt, 2 adds, 6 multiplies
505                         VectorNormalize(tdir);
506                         // accumulate matrix onto verts used by triangle
507                         // 30 assignments, 27 adds
508                         for (i = 0;i < 3;i++)
509                         {
510                                 voffset = e[i] * 4;
511                                 svectors[voffset    ] += sdir[0];
512                                 svectors[voffset + 1] += sdir[1];
513                                 svectors[voffset + 2] += sdir[2];
514                                 tvectors[voffset    ] += tdir[0];
515                                 tvectors[voffset + 1] += tdir[1];
516                                 tvectors[voffset + 2] += tdir[2];
517                                 normals[voffset    ] += normal[0];
518                                 normals[voffset + 1] += normal[1];
519                                 normals[voffset + 2] += normal[2];
520                         }
521                 }
522         }
523         // now we could divide the vectors by the number of averaged values on
524         // each vertex...  but instead normalize them
525         for (i = 0, v = svectors;i < numverts;i++, v += 4)
526                 // 4 assignments, 1 rsqrt, 2 adds, 6 multiplies
527                 VectorNormalize(v);
528         for (i = 0, v = tvectors;i < numverts;i++, v += 4)
529                 // 4 assignments, 1 rsqrt, 2 adds, 6 multiplies
530                 VectorNormalize(v);
531         for (i = 0, v = normals;i < numverts;i++, v += 4)
532                 // 4 assignments, 1 rsqrt, 2 adds, 6 multiplies
533                 VectorNormalize(v);
534 }
535
536 shadowmesh_t *Mod_ShadowMesh_Alloc(mempool_t *mempool, int maxverts)
537 {
538         shadowmesh_t *mesh;
539         mesh = Mem_Alloc(mempool, sizeof(shadowmesh_t) + maxverts * sizeof(float[4]) + maxverts * sizeof(int[3]) + maxverts * sizeof(int[3]) + SHADOWMESHVERTEXHASH * sizeof(shadowmeshvertexhash_t *) + maxverts * sizeof(shadowmeshvertexhash_t));
540         mesh->maxverts = maxverts;
541         mesh->maxtriangles = maxverts;
542         mesh->numverts = 0;
543         mesh->numtriangles = 0;
544         mesh->verts = (float *)(mesh + 1);
545         mesh->elements = (int *)(mesh->verts + mesh->maxverts * 4);
546         mesh->neighbors = (int *)(mesh->elements + mesh->maxtriangles * 3);
547         mesh->vertexhashtable = (shadowmeshvertexhash_t **)(mesh->neighbors + mesh->maxtriangles * 3);
548         mesh->vertexhashentries = (shadowmeshvertexhash_t *)(mesh->vertexhashtable + SHADOWMESHVERTEXHASH);
549         return mesh;
550 }
551
552 shadowmesh_t *Mod_ShadowMesh_ReAlloc(mempool_t *mempool, shadowmesh_t *oldmesh)
553 {
554         shadowmesh_t *newmesh;
555         newmesh = Mem_Alloc(mempool, sizeof(shadowmesh_t) + oldmesh->numverts * sizeof(float[4]) + oldmesh->numtriangles * sizeof(int[3]) + oldmesh->numtriangles * sizeof(int[3]));
556         newmesh->maxverts = newmesh->numverts = oldmesh->numverts;
557         newmesh->maxtriangles = newmesh->numtriangles = oldmesh->numtriangles;
558         newmesh->verts = (float *)(newmesh + 1);
559         newmesh->elements = (int *)(newmesh->verts + newmesh->maxverts * 4);
560         newmesh->neighbors = (int *)(newmesh->elements + newmesh->maxtriangles * 3);
561         memcpy(newmesh->verts, oldmesh->verts, newmesh->numverts * sizeof(float[4]));
562         memcpy(newmesh->elements, oldmesh->elements, newmesh->numtriangles * sizeof(int[3]));
563         memcpy(newmesh->neighbors, oldmesh->neighbors, newmesh->numtriangles * sizeof(int[3]));
564         return newmesh;
565 }
566
567 int Mod_ShadowMesh_AddVertex(shadowmesh_t *mesh, float *v)
568 {
569         int hashindex;
570         float *m;
571         shadowmeshvertexhash_t *hash;
572         // this uses prime numbers intentionally
573         hashindex = (int) (v[0] * 3 + v[1] * 5 + v[2] * 7) % SHADOWMESHVERTEXHASH;
574         for (hash = mesh->vertexhashtable[hashindex];hash;hash = hash->next)
575         {
576                 m = mesh->verts + (hash - mesh->vertexhashentries) * 4;
577                 if (m[0] == v[0] && m[1] == v[1] &&  m[2] == v[2])
578                         return hash - mesh->vertexhashentries;
579         }
580         hash = mesh->vertexhashentries + mesh->numverts;
581         hash->next = mesh->vertexhashtable[hashindex];
582         mesh->vertexhashtable[hashindex] = hash;
583         m = mesh->verts + (hash - mesh->vertexhashentries) * 4;
584         VectorCopy(v, m);
585         mesh->numverts++;
586         return mesh->numverts - 1;
587 }
588
589 void Mod_ShadowMesh_AddTriangle(mempool_t *mempool, shadowmesh_t *mesh, float *vert0, float *vert1, float *vert2)
590 {
591         while (mesh->numverts + 3 > mesh->maxverts || mesh->numtriangles + 1 > mesh->maxtriangles)
592         {
593                 if (mesh->next == NULL)
594                         mesh->next = Mod_ShadowMesh_Alloc(mempool, max(mesh->maxtriangles, 1));
595                 mesh = mesh->next;
596         }
597         mesh->elements[mesh->numtriangles * 3 + 0] = Mod_ShadowMesh_AddVertex(mesh, vert0);
598         mesh->elements[mesh->numtriangles * 3 + 1] = Mod_ShadowMesh_AddVertex(mesh, vert1);
599         mesh->elements[mesh->numtriangles * 3 + 2] = Mod_ShadowMesh_AddVertex(mesh, vert2);
600         mesh->numtriangles++;
601 }
602
603 void Mod_ShadowMesh_AddPolygon(mempool_t *mempool, shadowmesh_t *mesh, int numverts, float *verts)
604 {
605         int i;
606         float *v;
607         for (i = 0, v = verts + 3;i < numverts - 2;i++, v += 3)
608                 Mod_ShadowMesh_AddTriangle(mempool, mesh, verts, v, v + 3);
609         /*
610         int i, i1, i2, i3;
611         float *v;
612         while (mesh->numverts + numverts > mesh->maxverts || mesh->numtriangles + (numverts - 2) > mesh->maxtriangles)
613         {
614                 if (mesh->next == NULL)
615                         mesh->next = Mod_ShadowMesh_Alloc(mempool, max(mesh->maxtriangles, numverts));
616                 mesh = mesh->next;
617         }
618         i1 = Mod_ShadowMesh_AddVertex(mesh, verts);
619         i2 = 0;
620         i3 = Mod_ShadowMesh_AddVertex(mesh, verts + 3);
621         for (i = 0, v = verts + 6;i < numverts - 2;i++, v += 3)
622         {
623                 i2 = i3;
624                 i3 = Mod_ShadowMesh_AddVertex(mesh, v);
625                 mesh->elements[mesh->numtriangles * 3 + 0] = i1;
626                 mesh->elements[mesh->numtriangles * 3 + 1] = i2;
627                 mesh->elements[mesh->numtriangles * 3 + 2] = i3;
628                 mesh->numtriangles++;
629         }
630         */
631 }
632
633 void Mod_ShadowMesh_AddMesh(mempool_t *mempool, shadowmesh_t *mesh, int numverts, float *verts, int numtris, int *elements)
634 {
635         int i;
636         for (i = 0;i < numtris;i++, elements += 3)
637                 Mod_ShadowMesh_AddTriangle(mempool, mesh, verts + elements[0] * 4, verts + elements[1] * 4, verts + elements[2] * 4);
638 }
639
640 shadowmesh_t *Mod_ShadowMesh_Begin(mempool_t *mempool, int initialnumtriangles)
641 {
642         return Mod_ShadowMesh_Alloc(mempool, initialnumtriangles);
643 }
644
645 shadowmesh_t *Mod_ShadowMesh_Finish(mempool_t *mempool, shadowmesh_t *firstmesh)
646 {
647 #if 1
648         //int i;
649         shadowmesh_t *mesh, *newmesh, *nextmesh;
650         // reallocate meshs to conserve space
651         for (mesh = firstmesh, firstmesh = NULL;mesh;mesh = nextmesh)
652         {
653                 nextmesh = mesh->next;
654                 if (mesh->numverts >= 3 && mesh->numtriangles >= 1)
655                 {
656                         newmesh = Mod_ShadowMesh_ReAlloc(mempool, mesh);
657                         newmesh->next = firstmesh;
658                         firstmesh = newmesh;
659                         //Con_Printf("mesh\n");
660                         //for (i = 0;i < newmesh->numtriangles;i++)
661                         //      Con_Printf("tri %d %d %d\n", newmesh->elements[i * 3 + 0], newmesh->elements[i * 3 + 1], newmesh->elements[i * 3 + 2]);
662                         Mod_ValidateElements(newmesh->elements, newmesh->numtriangles, newmesh->numverts, __FILE__, __LINE__);
663                         Mod_BuildTriangleNeighbors(newmesh->neighbors, newmesh->elements, newmesh->numtriangles);
664                 }
665                 Mem_Free(mesh);
666         }
667 #else
668         shadowmesh_t *mesh;
669         for (mesh = firstmesh;mesh;mesh = mesh->next)
670         {
671                 Mod_ValidateElements(mesh->elements, mesh->numtriangles, mesh->numverts, __FILE__, __LINE__);
672                 Mod_BuildTriangleNeighbors(mesh->neighbors, mesh->elements, mesh->numtriangles);
673         }
674 #endif
675         return firstmesh;
676 }
677
678 void Mod_ShadowMesh_CalcBBox(shadowmesh_t *firstmesh, vec3_t mins, vec3_t maxs, vec3_t center, float *radius)
679 {
680         int i;
681         shadowmesh_t *mesh;
682         vec3_t nmins, nmaxs, ncenter, temp;
683         float nradius2, dist2, *v;
684         // calculate bbox
685         for (mesh = firstmesh;mesh;mesh = mesh->next)
686         {
687                 if (mesh == firstmesh)
688                 {
689                         VectorCopy(mesh->verts, nmins);
690                         VectorCopy(mesh->verts, nmaxs);
691                 }
692                 for (i = 0, v = mesh->verts;i < mesh->numverts;i++, v += 4)
693                 {
694                         if (nmins[0] > v[0]) nmins[0] = v[0];if (nmaxs[0] < v[0]) nmaxs[0] = v[0];
695                         if (nmins[1] > v[1]) nmins[1] = v[1];if (nmaxs[1] < v[1]) nmaxs[1] = v[1];
696                         if (nmins[2] > v[2]) nmins[2] = v[2];if (nmaxs[2] < v[2]) nmaxs[2] = v[2];
697                 }
698         }
699         // calculate center and radius
700         ncenter[0] = (nmins[0] + nmaxs[0]) * 0.5f;
701         ncenter[1] = (nmins[1] + nmaxs[1]) * 0.5f;
702         ncenter[2] = (nmins[2] + nmaxs[2]) * 0.5f;
703         nradius2 = 0;
704         for (mesh = firstmesh;mesh;mesh = mesh->next)
705         {
706                 for (i = 0, v = mesh->verts;i < mesh->numverts;i++, v += 4)
707                 {
708                         VectorSubtract(v, ncenter, temp);
709                         dist2 = DotProduct(temp, temp);
710                         if (nradius2 < dist2)
711                                 nradius2 = dist2;
712                 }
713         }
714         // return data
715         if (mins)
716                 VectorCopy(nmins, mins);
717         if (maxs)
718                 VectorCopy(nmaxs, maxs);
719         if (center)
720                 VectorCopy(ncenter, center);
721         if (radius)
722                 *radius = sqrt(nradius2);
723 }
724
725 void Mod_ShadowMesh_Free(shadowmesh_t *mesh)
726 {
727         shadowmesh_t *nextmesh;
728         for (;mesh;mesh = nextmesh)
729         {
730                 nextmesh = mesh->next;
731                 Mem_Free(mesh);
732         }
733 }