]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - model_shared.c
R_ShadowMesh_Finish now discards empty meshs
[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_LoadTexture(r_notexturepool, "notexture", 16, 16, &pix[0][0][0], TEXTYPE_RGBA, TEXF_MIPMAP);
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 static void Mod_Flush (void);
106 void Mod_Init (void)
107 {
108         Mod_BrushInit();
109         Mod_AliasInit();
110         Mod_SpriteInit();
111
112         Cmd_AddCommand ("modellist", Mod_Print);
113         Cmd_AddCommand ("modelflush", Mod_Flush);
114 }
115
116 void Mod_RenderInit(void)
117 {
118         R_RegisterModule("Models", mod_start, mod_shutdown, mod_newmap);
119 }
120
121 void Mod_FreeModel (model_t *mod)
122 {
123         R_FreeTexturePool(&mod->texturepool);
124         Mem_FreePool(&mod->mempool);
125
126         // clear the struct to make it available
127         memset(mod, 0, sizeof(model_t));
128 }
129
130 void Mod_UnloadModel (model_t *mod)
131 {
132         char name[MAX_QPATH];
133         qboolean isworldmodel;
134         strcpy(name, mod->name);
135         isworldmodel = mod->isworldmodel;
136         Mod_FreeModel(mod);
137         strcpy(mod->name, name);
138         mod->isworldmodel = isworldmodel;
139         mod->needload = true;
140 }
141
142 /*
143 ==================
144 Mod_LoadModel
145
146 Loads a model
147 ==================
148 */
149 static model_t *Mod_LoadModel (model_t *mod, qboolean crash, qboolean checkdisk, qboolean isworldmodel)
150 {
151         int crc;
152         void *buf;
153
154         mod->used = true;
155
156         if (mod->name[0] == '*') // submodel
157                 return mod;
158
159         crc = 0;
160         buf = NULL;
161         if (!mod->needload)
162         {
163                 if (checkdisk)
164                 {
165                         buf = COM_LoadFile (mod->name, false);
166                         if (!buf)
167                         {
168                                 if (crash)
169                                         Host_Error ("Mod_LoadModel: %s not found", mod->name); // LordHavoc: Sys_Error was *ANNOYING*
170                                 return NULL;
171                         }
172
173                         crc = CRC_Block(buf, com_filesize);
174                 }
175                 else
176                         crc = mod->crc;
177
178                 if (mod->crc == crc && mod->isworldmodel == isworldmodel)
179                 {
180                         if (buf)
181                                 Mem_Free(buf);
182                         return mod; // already loaded
183                 }
184         }
185
186         Con_DPrintf("loading model %s\n", mod->name);
187
188         if (!buf)
189         {
190                 buf = COM_LoadFile (mod->name, false);
191                 if (!buf)
192                 {
193                         if (crash)
194                                 Host_Error ("Mod_LoadModel: %s not found", mod->name);
195                         return NULL;
196                 }
197                 crc = CRC_Block(buf, com_filesize);
198         }
199
200         // allocate a new model
201         loadmodel = mod;
202
203         // LordHavoc: unload the existing model in this slot (if there is one)
204         Mod_UnloadModel(mod);
205         mod->isworldmodel = isworldmodel;
206         mod->needload = false;
207         mod->used = true;
208         mod->crc = crc;
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_LoadAliasModel  (mod, buf);
218         else if (!memcmp(buf, "IDP2"    , 4)) Mod_LoadQ2AliasModel(mod, buf);
219         else if (!memcmp(buf, "ZYMOTIC" , 7)) Mod_LoadZymoticModel(mod, buf);
220         else if (!memcmp(buf, "IDSP"    , 4)) Mod_LoadSpriteModel (mod, buf);
221         else                                  Mod_LoadBrushModel  (mod, buf);
222
223         Mem_Free(buf);
224
225         return mod;
226 }
227
228 void Mod_CheckLoaded (model_t *mod)
229 {
230         if (mod)
231         {
232                 if (mod->needload)
233                         Mod_LoadModel(mod, true, true, mod->isworldmodel);
234                 else
235                 {
236                         if (mod->type == mod_invalid)
237                                 Host_Error("Mod_CheckLoaded: invalid model\n");
238                         mod->used = true;
239                         return;
240                 }
241         }
242 }
243
244 /*
245 ===================
246 Mod_ClearAll
247 ===================
248 */
249 void Mod_ClearAll (void)
250 {
251 }
252
253 void Mod_ClearUsed(void)
254 {
255         int i;
256         model_t *mod;
257
258         for (i = 0, mod = mod_known;i < MAX_MOD_KNOWN;i++, mod++)
259                 if (mod->name[0])
260                         mod->used = false;
261 }
262
263 void Mod_PurgeUnused(void)
264 {
265         int i;
266         model_t *mod;
267
268         for (i = 0, mod = mod_known;i < MAX_MOD_KNOWN;i++, mod++)
269                 if (mod->name[0])
270                         if (!mod->used)
271                                 Mod_FreeModel(mod);
272 }
273
274 void Mod_LoadModels(void)
275 {
276         int i;
277         model_t *mod;
278
279         for (i = 0, mod = mod_known;i < MAX_MOD_KNOWN;i++, mod++)
280                 if (mod->name[0])
281                         if (mod->used)
282                                 Mod_CheckLoaded(mod);
283 }
284
285 /*
286 ==================
287 Mod_FindName
288
289 ==================
290 */
291 model_t *Mod_FindName (const char *name)
292 {
293         int i;
294         model_t *mod, *freemod;
295
296         if (!name[0])
297                 Host_Error ("Mod_ForName: NULL name");
298
299 // search the currently loaded models
300         freemod = NULL;
301         for (i = 0, mod = mod_known;i < MAX_MOD_KNOWN;i++, mod++)
302         {
303                 if (mod->name[0])
304                 {
305                         if (!strcmp (mod->name, name))
306                         {
307                                 mod->used = true;
308                                 return mod;
309                         }
310                 }
311                 else if (freemod == NULL)
312                         freemod = mod;
313         }
314
315         if (freemod)
316         {
317                 mod = freemod;
318                 strcpy (mod->name, name);
319                 mod->needload = true;
320                 mod->used = true;
321                 return mod;
322         }
323
324         Host_Error ("Mod_FindName: ran out of models\n");
325         return NULL;
326 }
327
328 /*
329 ==================
330 Mod_TouchModel
331
332 ==================
333 */
334 void Mod_TouchModel (const char *name)
335 {
336         model_t *mod;
337
338         mod = Mod_FindName (name);
339         mod->used = true;
340 }
341
342 /*
343 ==================
344 Mod_ForName
345
346 Loads in a model for the given name
347 ==================
348 */
349 model_t *Mod_ForName (const char *name, qboolean crash, qboolean checkdisk, qboolean isworldmodel)
350 {
351         return Mod_LoadModel (Mod_FindName (name), crash, checkdisk, isworldmodel);
352 }
353
354 qbyte *mod_base;
355
356
357 //=============================================================================
358
359 /*
360 ================
361 Mod_Print
362 ================
363 */
364 static void Mod_Print (void)
365 {
366         int             i;
367         model_t *mod;
368
369         Con_Printf ("Loaded models:\n");
370         for (i = 0, mod = mod_known;i < MAX_MOD_KNOWN;i++, mod++)
371                 if (mod->name[0])
372                         Con_Printf ("%4iK %s\n", mod->mempool ? (mod->mempool->totalsize + 1023) / 1024 : 0, mod->name);
373 }
374
375 static void Mod_Flush (void)
376 {
377         int             i;
378
379         Con_Printf ("Unloading models\n");
380         for (i = 0;i < MAX_MOD_KNOWN;i++)
381                 if (mod_known[i].name[0])
382                         Mod_UnloadModel(&mod_known[i]);
383         Mod_LoadModels();
384 }
385
386 int Mod_FindTriangleWithEdge(int *elements, int numtriangles, int start, int end)
387 {
388         int i;
389         for (i = 0;i < numtriangles;i++, elements += 3)
390         {
391                 if (elements[0] == start && elements[1] == end)
392                         return i;
393                 if (elements[1] == start && elements[2] == end)
394                         return i;
395                 if (elements[2] == start && elements[0] == end)
396                         return i;
397         }
398         return -1;
399 }
400
401 void Mod_BuildTriangleNeighbors(int *neighbors, int *elements, int numtriangles)
402 {
403         int i, *e, *n;
404         for (i = 0, e = elements, n = neighbors;i < numtriangles;i++, e += 3, n += 3)
405         {
406                 n[0] = Mod_FindTriangleWithEdge(elements, numtriangles, e[1], e[0]);
407                 n[1] = Mod_FindTriangleWithEdge(elements, numtriangles, e[2], e[1]);
408                 n[2] = Mod_FindTriangleWithEdge(elements, numtriangles, e[0], e[2]);
409         }
410 }
411
412 shadowmesh_t *Mod_ShadowMesh_Alloc(mempool_t *mempool, int maxverts)
413 {
414         shadowmesh_t *mesh;
415         mesh = Mem_Alloc(mempool, sizeof(shadowmesh_t) + maxverts * sizeof(float[4]) + maxverts * sizeof(int[3]) + maxverts * sizeof(int[3]));
416         mesh->maxverts = maxverts;
417         mesh->maxtriangles = maxverts;
418         mesh->numverts = 0;
419         mesh->numtriangles = 0;
420         mesh->verts = (float *)(mesh + 1);
421         mesh->elements = (int *)(mesh->verts + mesh->maxverts * 4);
422         mesh->neighbors = (int *)(mesh->elements + mesh->maxtriangles * 3);
423         return mesh;
424 }
425
426 shadowmesh_t *Mod_ShadowMesh_ReAlloc(mempool_t *mempool, shadowmesh_t *oldmesh)
427 {
428         shadowmesh_t *newmesh;
429         newmesh = Mem_Alloc(mempool, sizeof(shadowmesh_t) + oldmesh->numverts * sizeof(float[4]) + oldmesh->numtriangles * sizeof(int[3]) + oldmesh->numtriangles * sizeof(int[3]));
430         newmesh->maxverts = newmesh->numverts = oldmesh->numverts;
431         newmesh->maxtriangles = newmesh->numtriangles = oldmesh->numtriangles;
432         newmesh->verts = (float *)(newmesh + 1);
433         newmesh->elements = (int *)(newmesh->verts + newmesh->maxverts * 4);
434         newmesh->neighbors = (int *)(newmesh->elements + newmesh->maxtriangles * 3);
435         memcpy(newmesh->verts, oldmesh->verts, newmesh->numverts * sizeof(float[4]));
436         memcpy(newmesh->elements, oldmesh->elements, newmesh->numtriangles * sizeof(int[3]));
437         memcpy(newmesh->neighbors, oldmesh->neighbors, newmesh->numtriangles * sizeof(int[3]));
438         return newmesh;
439 }
440
441 int Mod_ShadowMesh_AddVertex(shadowmesh_t *mesh, float *v)
442 {
443         int j;
444         float *m, temp[3];
445         for (j = 0, m = mesh->verts;j < mesh->numverts;j++, m += 4)
446         {
447                 VectorSubtract(v, m, temp);
448                 if (DotProduct(temp, temp) < 0.1)
449                         return j;
450         }
451         mesh->numverts++;
452         VectorCopy(v, m);
453         return j;
454 }
455
456 void Mod_ShadowMesh_AddPolygon(mempool_t *mempool, shadowmesh_t *mesh, int numverts, float *verts)
457 {
458         int i, i1, i2, i3;
459         float *v;
460         while (numverts + mesh->numverts > mesh->maxverts || (numverts - 2) + mesh->numtriangles > mesh->maxtriangles)
461         {
462                 if (mesh->next == NULL)
463                         mesh->next = Mod_ShadowMesh_Alloc(mempool, max(1000, numverts));
464                 mesh = mesh->next;
465         }
466         i1 = Mod_ShadowMesh_AddVertex(mesh, verts);
467         i2 = 0;
468         i3 = Mod_ShadowMesh_AddVertex(mesh, verts + 3);
469         for (i = 0, v = verts + 6;i < numverts - 2;i++, v += 3)
470         {
471                 i2 = i3;
472                 i3 = Mod_ShadowMesh_AddVertex(mesh, v);
473                 mesh->elements[mesh->numtriangles * 3 + 0] = i1;
474                 mesh->elements[mesh->numtriangles * 3 + 1] = i2;
475                 mesh->elements[mesh->numtriangles * 3 + 2] = i3;
476                 mesh->numtriangles++;
477         }
478 }
479
480 shadowmesh_t *Mod_ShadowMesh_Begin(mempool_t *mempool)
481 {
482         return Mod_ShadowMesh_Alloc(mempool, 1000);
483 }
484
485 shadowmesh_t *Mod_ShadowMesh_Finish(mempool_t *mempool, shadowmesh_t *firstmesh)
486 {
487         //int i;
488         shadowmesh_t *mesh, *newmesh, *nextmesh;
489         // reallocate meshs to conserve space
490         for (mesh = firstmesh, firstmesh = NULL;mesh;mesh = nextmesh)
491         {
492                 nextmesh = mesh->next;
493                 if (mesh->numverts >= 3 && mesh->numtriangles >= 1)
494                 {
495                         newmesh = Mod_ShadowMesh_ReAlloc(mempool, mesh);
496                         newmesh->next = firstmesh;
497                         firstmesh = newmesh;
498                         //Con_Printf("mesh\n");
499                         //for (i = 0;i < newmesh->numtriangles;i++)
500                         //      Con_Printf("tri %d %d %d\n", newmesh->elements[i * 3 + 0], newmesh->elements[i * 3 + 1], newmesh->elements[i * 3 + 2]);
501                         Mod_BuildTriangleNeighbors(newmesh->neighbors, newmesh->elements, newmesh->numtriangles);
502                 }
503                 Mem_Free(mesh);
504         }
505         return firstmesh;
506 }
507
508 void Mod_ShadowMesh_CalcBBox(shadowmesh_t *firstmesh, vec3_t mins, vec3_t maxs, vec3_t center, float *radius)
509 {
510         int i;
511         shadowmesh_t *mesh;
512         vec3_t nmins, nmaxs, ncenter, temp;
513         float nradius2, dist2, *v;
514         // calculate bbox
515         for (mesh = firstmesh;mesh;mesh = mesh->next)
516         {
517                 if (mesh == firstmesh)
518                 {
519                         VectorCopy(mesh->verts, nmins);
520                         VectorCopy(mesh->verts, nmaxs);
521                 }
522                 for (i = 0, v = mesh->verts;i < mesh->numverts;i++, v += 4)
523                 {
524                         if (nmins[0] > v[0]) nmins[0] = v[0];if (nmaxs[0] < v[0]) nmaxs[0] = v[0];
525                         if (nmins[1] > v[1]) nmins[1] = v[1];if (nmaxs[1] < v[1]) nmaxs[1] = v[1];
526                         if (nmins[2] > v[2]) nmins[2] = v[2];if (nmaxs[2] < v[2]) nmaxs[2] = v[2];
527                 }
528         }
529         // calculate center and radius
530         ncenter[0] = (nmins[0] + nmaxs[0]) * 0.5f;
531         ncenter[1] = (nmins[1] + nmaxs[1]) * 0.5f;
532         ncenter[2] = (nmins[2] + nmaxs[2]) * 0.5f;
533         nradius2 = 0;
534         for (mesh = firstmesh;mesh;mesh = mesh->next)
535         {
536                 for (i = 0, v = mesh->verts;i < mesh->numverts;i++, v += 4)
537                 {
538                         VectorSubtract(v, ncenter, temp);
539                         dist2 = DotProduct(temp, temp);
540                         if (nradius2 < dist2)
541                                 nradius2 = dist2;
542                 }
543         }
544         // return data
545         if (mins)
546                 VectorCopy(nmins, mins);
547         if (maxs)
548                 VectorCopy(nmaxs, maxs);
549         if (center)
550                 VectorCopy(ncenter, center);
551         if (radius)
552                 *radius = sqrt(nradius2);
553 }
554
555 void Mod_ShadowMesh_Free(shadowmesh_t *mesh)
556 {
557         shadowmesh_t *nextmesh;
558         for (;mesh;mesh = nextmesh)
559         {
560                 nextmesh = mesh->next;
561                 Mem_Free(mesh);
562         }
563 }