2 Copyright (C) 1996-1997 Id Software, Inc.
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.
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.
13 See the GNU General Public License for more details.
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.
20 // models.c -- model loading and caching
22 // models are the only shared resource between a client and server running
23 // on the same machine.
29 // LordHavoc: increased from 512 to 2048
30 #define MAX_MOD_KNOWN 2048
31 static model_t mod_known[MAX_MOD_KNOWN];
33 rtexture_t *r_notexture;
34 rtexturepool_t *r_notexturepool;
36 texture_t r_surf_notexture;
38 void Mod_SetupNoTexture(void)
43 // this makes a light grey/dark grey checkerboard texture
44 for (y = 0;y < 16;y++)
46 for (x = 0;x < 16;x++)
48 if ((y < 8) ^ (x < 8))
65 r_notexturepool = R_AllocTexturePool();
66 r_notexture = R_LoadTexture2D(r_notexturepool, "notexture", 16, 16, &pix[0][0][0], TEXTYPE_RGBA, TEXF_MIPMAP, NULL);
69 extern void Mod_BrushStartup (void);
70 extern void Mod_BrushShutdown (void);
72 static void mod_start(void)
75 for (i = 0;i < MAX_MOD_KNOWN;i++)
76 if (mod_known[i].name[0])
77 Mod_UnloadModel(&mod_known[i]);
84 static void mod_shutdown(void)
87 for (i = 0;i < MAX_MOD_KNOWN;i++)
88 if (mod_known[i].name[0])
89 Mod_UnloadModel(&mod_known[i]);
91 R_FreeTexturePool(&r_notexturepool);
95 static void mod_newmap(void)
104 static void Mod_Print (void);
111 Cmd_AddCommand ("modellist", Mod_Print);
114 void Mod_RenderInit(void)
116 R_RegisterModule("Models", mod_start, mod_shutdown, mod_newmap);
119 void Mod_FreeModel (model_t *mod)
121 R_FreeTexturePool(&mod->texturepool);
122 Mem_FreePool(&mod->mempool);
124 // clear the struct to make it available
125 memset(mod, 0, sizeof(model_t));
128 void Mod_UnloadModel (model_t *mod)
130 char name[MAX_QPATH];
131 qboolean isworldmodel;
132 strcpy(name, mod->name);
133 isworldmodel = mod->isworldmodel;
135 strcpy(mod->name, name);
136 mod->isworldmodel = isworldmodel;
137 mod->needload = true;
147 static model_t *Mod_LoadModel (model_t *mod, qboolean crash, qboolean checkdisk, qboolean isworldmodel)
154 if (mod->name[0] == '*') // submodel
163 buf = COM_LoadFile (mod->name, false);
167 Host_Error ("Mod_LoadModel: %s not found", mod->name); // LordHavoc: Sys_Error was *ANNOYING*
171 crc = CRC_Block(buf, com_filesize);
176 if (mod->crc == crc && mod->isworldmodel == isworldmodel)
180 return mod; // already loaded
184 Con_DPrintf("loading model %s\n", mod->name);
188 buf = COM_LoadFile (mod->name, false);
192 Host_Error ("Mod_LoadModel: %s not found", mod->name);
195 crc = CRC_Block(buf, com_filesize);
198 // allocate a new model
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;
207 // errors can prevent the corresponding mod->error = false;
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();
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);
225 // no errors occurred
230 void Mod_CheckLoaded (model_t *mod)
235 Mod_LoadModel(mod, true, true, mod->isworldmodel);
238 if (mod->type == mod_invalid)
239 Host_Error("Mod_CheckLoaded: invalid model\n");
251 void Mod_ClearAll (void)
255 void Mod_ClearErrorModels (void)
260 for (i = 0, mod = mod_known;i < MAX_MOD_KNOWN;i++, mod++)
265 void Mod_ClearUsed(void)
270 for (i = 0, mod = mod_known;i < MAX_MOD_KNOWN;i++, mod++)
275 void Mod_PurgeUnused(void)
280 for (i = 0, mod = mod_known;i < MAX_MOD_KNOWN;i++, mod++)
286 void Mod_LoadModels(void)
291 for (i = 0, mod = mod_known;i < MAX_MOD_KNOWN;i++, mod++)
294 Mod_CheckLoaded(mod);
303 model_t *Mod_FindName (const char *name)
306 model_t *mod, *freemod;
309 Host_Error ("Mod_ForName: NULL name");
311 // search the currently loaded models
313 for (i = 0, mod = mod_known;i < MAX_MOD_KNOWN;i++, mod++)
317 if (!strcmp (mod->name, name))
323 else if (freemod == NULL)
330 strcpy (mod->name, name);
331 mod->needload = true;
336 Host_Error ("Mod_FindName: ran out of models\n");
346 void Mod_TouchModel (const char *name)
350 mod = Mod_FindName (name);
358 Loads in a model for the given name
361 model_t *Mod_ForName (const char *name, qboolean crash, qboolean checkdisk, qboolean isworldmodel)
363 return Mod_LoadModel (Mod_FindName (name), crash, checkdisk, isworldmodel);
369 //=============================================================================
376 static void Mod_Print (void)
381 Con_Printf ("Loaded models:\n");
382 for (i = 0, mod = mod_known;i < MAX_MOD_KNOWN;i++, mod++)
384 Con_Printf ("%4iK %s\n", mod->mempool ? (mod->mempool->totalsize + 1023) / 1024 : 0, mod->name);
387 int Mod_FindTriangleWithEdge(const int *elements, int numtriangles, int start, int end)
390 for (i = 0;i < numtriangles;i++, elements += 3)
392 if (elements[0] == start && elements[1] == end)
394 if (elements[1] == start && elements[2] == end)
396 if (elements[2] == start && elements[0] == end)
402 void Mod_BuildTriangleNeighbors(int *neighbors, const int *elements, int numtriangles)
406 for (i = 0, e = elements, n = neighbors;i < numtriangles;i++, e += 3, n += 3)
408 n[0] = Mod_FindTriangleWithEdge(elements, numtriangles, e[1], e[0]);
409 n[1] = Mod_FindTriangleWithEdge(elements, numtriangles, e[2], e[1]);
410 n[2] = Mod_FindTriangleWithEdge(elements, numtriangles, e[0], e[2]);
414 void Mod_ValidateElements(const int *elements, int numtriangles, int numverts, const char *filename, int fileline)
417 for (i = 0;i < numtriangles * 3;i++)
418 if ((unsigned int)elements[i] >= (unsigned int)numverts)
419 Con_Printf("Mod_ValidateElements: out of bounds element detected at %s:%d\n", filename, fileline);
423 a note on the cost of executing this function:
424 per triangle: 188 (83 42 13 45 4 1)
425 assignments: 83 (20 3 3 3 1 4 4 1 3 4 3 4 30)
426 adds: 42 (2 2 2 2 3 2 2 27)
427 subtracts: 13 (3 3 3 1 3)
428 multiplies: 45 (6 3 6 6 3 3 6 6 6)
431 per vertex: 39 (12 6 18 3)
432 assignments: 12 (4 4 4)
434 multiplies: 18 (6 6 6)
438 void Mod_BuildTextureVectorsAndNormals(int numverts, int numtriangles, const float *vertex, const float *texcoord, const int *elements, float *svectors, float *tvectors, float *normals)
440 int i, tnum, voffset;
441 float vert[3][4], vec[3][4], sdir[3], tdir[3], normal[3], f, *v;
444 memset(svectors, 0, numverts * sizeof(float[4]));
445 memset(tvectors, 0, numverts * sizeof(float[4]));
446 memset(normals, 0, numverts * sizeof(float[4]));
447 // process each vertex of each triangle and accumulate the results
448 for (tnum = 0, e = elements;tnum < numtriangles;tnum++, e += 3)
450 // calculate texture matrix for triangle
453 vert[0][0] = vertex[voffset+0];
454 vert[0][1] = vertex[voffset+1];
455 vert[0][2] = vertex[voffset+2];
456 vert[0][3] = texcoord[voffset];
458 vert[1][0] = vertex[voffset+0];
459 vert[1][1] = vertex[voffset+1];
460 vert[1][2] = vertex[voffset+2];
461 vert[1][3] = texcoord[voffset];
463 vert[2][0] = vertex[voffset+0];
464 vert[2][1] = vertex[voffset+1];
465 vert[2][2] = vertex[voffset+2];
466 vert[2][3] = texcoord[voffset];
467 // 3 assignments, 3 subtracts
468 VectorSubtract(vert[1], vert[0], vec[0]);
469 // 3 assignments, 3 subtracts
470 VectorSubtract(vert[2], vert[0], vec[1]);
471 // 3 assignments, 3 subtracts, 6 multiplies
472 CrossProduct(vec[0], vec[1], normal);
473 // 1 assignment, 2 adds, 3 multiplies, 1 compare
474 if (DotProduct(normal, normal) >= 0.001)
476 // 4 assignments, 1 rsqrt, 2 adds, 6 multiplies
477 VectorNormalize(normal);
478 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]);
479 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]);
480 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]);
481 // 4 assignments, 1 rsqrt, 2 adds, 6 multiplies
482 VectorNormalize(sdir);
483 // 1 assignments, 1 negates, 2 adds, 3 multiplies
484 f = -DotProduct(sdir, normal);
485 // 3 assignments, 3 adds, 3 multiplies
486 VectorMA(sdir, f, normal, sdir);
487 // 4 assignments, 1 rsqrt, 2 adds, 6 multiplies
488 VectorNormalize(sdir);
489 // 3 assignments, 3 subtracts, 6 multiplies
490 CrossProduct(sdir, normal, tdir);
491 // this is probably not necessary
492 // 4 assignments, 1 rsqrt, 2 adds, 6 multiplies
493 VectorNormalize(tdir);
494 // accumulate matrix onto verts used by triangle
495 // 30 assignments, 27 adds
496 for (i = 0;i < 3;i++)
499 svectors[voffset ] += sdir[0];
500 svectors[voffset + 1] += sdir[1];
501 svectors[voffset + 2] += sdir[2];
502 tvectors[voffset ] += tdir[0];
503 tvectors[voffset + 1] += tdir[1];
504 tvectors[voffset + 2] += tdir[2];
505 normals[voffset ] += normal[0];
506 normals[voffset + 1] += normal[1];
507 normals[voffset + 2] += normal[2];
511 // now we could divide the vectors by the number of averaged values on
512 // each vertex... but instead normalize them
513 for (i = 0, v = svectors;i < numverts;i++, v += 4)
514 // 4 assignments, 1 rsqrt, 2 adds, 6 multiplies
516 for (i = 0, v = tvectors;i < numverts;i++, v += 4)
517 // 4 assignments, 1 rsqrt, 2 adds, 6 multiplies
519 for (i = 0, v = normals;i < numverts;i++, v += 4)
520 // 4 assignments, 1 rsqrt, 2 adds, 6 multiplies
524 shadowmesh_t *Mod_ShadowMesh_Alloc(mempool_t *mempool, int maxverts)
527 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));
528 mesh->maxverts = maxverts;
529 mesh->maxtriangles = maxverts;
531 mesh->numtriangles = 0;
532 mesh->verts = (float *)(mesh + 1);
533 mesh->elements = (int *)(mesh->verts + mesh->maxverts * 4);
534 mesh->neighbors = (int *)(mesh->elements + mesh->maxtriangles * 3);
535 mesh->vertexhashtable = (shadowmeshvertexhash_t **)(mesh->neighbors + mesh->maxtriangles * 3);
536 mesh->vertexhashentries = (shadowmeshvertexhash_t *)(mesh->vertexhashtable + SHADOWMESHVERTEXHASH);
540 shadowmesh_t *Mod_ShadowMesh_ReAlloc(mempool_t *mempool, shadowmesh_t *oldmesh)
542 shadowmesh_t *newmesh;
543 newmesh = Mem_Alloc(mempool, sizeof(shadowmesh_t) + oldmesh->numverts * sizeof(float[4]) + oldmesh->numtriangles * sizeof(int[3]) + oldmesh->numtriangles * sizeof(int[3]));
544 newmesh->maxverts = newmesh->numverts = oldmesh->numverts;
545 newmesh->maxtriangles = newmesh->numtriangles = oldmesh->numtriangles;
546 newmesh->verts = (float *)(newmesh + 1);
547 newmesh->elements = (int *)(newmesh->verts + newmesh->maxverts * 4);
548 newmesh->neighbors = (int *)(newmesh->elements + newmesh->maxtriangles * 3);
549 memcpy(newmesh->verts, oldmesh->verts, newmesh->numverts * sizeof(float[4]));
550 memcpy(newmesh->elements, oldmesh->elements, newmesh->numtriangles * sizeof(int[3]));
551 memcpy(newmesh->neighbors, oldmesh->neighbors, newmesh->numtriangles * sizeof(int[3]));
555 int Mod_ShadowMesh_AddVertex(shadowmesh_t *mesh, float *v)
559 shadowmeshvertexhash_t *hash;
560 // this uses prime numbers intentionally
561 hashindex = (int) (v[0] * 3 + v[1] * 5 + v[2] * 7) % SHADOWMESHVERTEXHASH;
562 for (hash = mesh->vertexhashtable[hashindex];hash;hash = hash->next)
564 m = mesh->verts + (hash - mesh->vertexhashentries) * 4;
565 if (m[0] == v[0] && m[1] == v[1] && m[2] == v[2])
566 return hash - mesh->vertexhashentries;
568 hash = mesh->vertexhashentries + mesh->numverts;
569 hash->next = mesh->vertexhashtable[hashindex];
570 mesh->vertexhashtable[hashindex] = hash;
571 m = mesh->verts + (hash - mesh->vertexhashentries) * 4;
574 return mesh->numverts - 1;
577 void Mod_ShadowMesh_AddTriangle(mempool_t *mempool, shadowmesh_t *mesh, float *vert0, float *vert1, float *vert2)
579 while (mesh->numverts + 3 > mesh->maxverts || mesh->numtriangles + 1 > mesh->maxtriangles)
581 if (mesh->next == NULL)
582 mesh->next = Mod_ShadowMesh_Alloc(mempool, max(mesh->maxtriangles, 1));
585 mesh->elements[mesh->numtriangles * 3 + 0] = Mod_ShadowMesh_AddVertex(mesh, vert0);
586 mesh->elements[mesh->numtriangles * 3 + 1] = Mod_ShadowMesh_AddVertex(mesh, vert1);
587 mesh->elements[mesh->numtriangles * 3 + 2] = Mod_ShadowMesh_AddVertex(mesh, vert2);
588 mesh->numtriangles++;
591 void Mod_ShadowMesh_AddPolygon(mempool_t *mempool, shadowmesh_t *mesh, int numverts, float *verts)
595 for (i = 0, v = verts + 3;i < numverts - 2;i++, v += 3)
596 Mod_ShadowMesh_AddTriangle(mempool, mesh, verts, v, v + 3);
600 while (mesh->numverts + numverts > mesh->maxverts || mesh->numtriangles + (numverts - 2) > mesh->maxtriangles)
602 if (mesh->next == NULL)
603 mesh->next = Mod_ShadowMesh_Alloc(mempool, max(mesh->maxtriangles, numverts));
606 i1 = Mod_ShadowMesh_AddVertex(mesh, verts);
608 i3 = Mod_ShadowMesh_AddVertex(mesh, verts + 3);
609 for (i = 0, v = verts + 6;i < numverts - 2;i++, v += 3)
612 i3 = Mod_ShadowMesh_AddVertex(mesh, v);
613 mesh->elements[mesh->numtriangles * 3 + 0] = i1;
614 mesh->elements[mesh->numtriangles * 3 + 1] = i2;
615 mesh->elements[mesh->numtriangles * 3 + 2] = i3;
616 mesh->numtriangles++;
621 void Mod_ShadowMesh_AddMesh(mempool_t *mempool, shadowmesh_t *mesh, int numverts, float *verts, int numtris, int *elements)
624 for (i = 0;i < numtris;i++, elements += 3)
625 Mod_ShadowMesh_AddTriangle(mempool, mesh, verts + elements[0] * 4, verts + elements[1] * 4, verts + elements[2] * 4);
628 shadowmesh_t *Mod_ShadowMesh_Begin(mempool_t *mempool, int initialnumtriangles)
630 return Mod_ShadowMesh_Alloc(mempool, initialnumtriangles);
633 shadowmesh_t *Mod_ShadowMesh_Finish(mempool_t *mempool, shadowmesh_t *firstmesh)
637 shadowmesh_t *mesh, *newmesh, *nextmesh;
638 // reallocate meshs to conserve space
639 for (mesh = firstmesh, firstmesh = NULL;mesh;mesh = nextmesh)
641 nextmesh = mesh->next;
642 if (mesh->numverts >= 3 && mesh->numtriangles >= 1)
644 newmesh = Mod_ShadowMesh_ReAlloc(mempool, mesh);
645 newmesh->next = firstmesh;
647 //Con_Printf("mesh\n");
648 //for (i = 0;i < newmesh->numtriangles;i++)
649 // Con_Printf("tri %d %d %d\n", newmesh->elements[i * 3 + 0], newmesh->elements[i * 3 + 1], newmesh->elements[i * 3 + 2]);
650 Mod_ValidateElements(newmesh->elements, newmesh->numtriangles, newmesh->numverts, __FILE__, __LINE__);
651 Mod_BuildTriangleNeighbors(newmesh->neighbors, newmesh->elements, newmesh->numtriangles);
657 for (mesh = firstmesh;mesh;mesh = mesh->next)
659 Mod_ValidateElements(mesh->elements, mesh->numtriangles, mesh->numverts, __FILE__, __LINE__);
660 Mod_BuildTriangleNeighbors(mesh->neighbors, mesh->elements, mesh->numtriangles);
666 void Mod_ShadowMesh_CalcBBox(shadowmesh_t *firstmesh, vec3_t mins, vec3_t maxs, vec3_t center, float *radius)
670 vec3_t nmins, nmaxs, ncenter, temp;
671 float nradius2, dist2, *v;
673 for (mesh = firstmesh;mesh;mesh = mesh->next)
675 if (mesh == firstmesh)
677 VectorCopy(mesh->verts, nmins);
678 VectorCopy(mesh->verts, nmaxs);
680 for (i = 0, v = mesh->verts;i < mesh->numverts;i++, v += 4)
682 if (nmins[0] > v[0]) nmins[0] = v[0];if (nmaxs[0] < v[0]) nmaxs[0] = v[0];
683 if (nmins[1] > v[1]) nmins[1] = v[1];if (nmaxs[1] < v[1]) nmaxs[1] = v[1];
684 if (nmins[2] > v[2]) nmins[2] = v[2];if (nmaxs[2] < v[2]) nmaxs[2] = v[2];
687 // calculate center and radius
688 ncenter[0] = (nmins[0] + nmaxs[0]) * 0.5f;
689 ncenter[1] = (nmins[1] + nmaxs[1]) * 0.5f;
690 ncenter[2] = (nmins[2] + nmaxs[2]) * 0.5f;
692 for (mesh = firstmesh;mesh;mesh = mesh->next)
694 for (i = 0, v = mesh->verts;i < mesh->numverts;i++, v += 4)
696 VectorSubtract(v, ncenter, temp);
697 dist2 = DotProduct(temp, temp);
698 if (nradius2 < dist2)
704 VectorCopy(nmins, mins);
706 VectorCopy(nmaxs, maxs);
708 VectorCopy(ncenter, center);
710 *radius = sqrt(nradius2);
713 void Mod_ShadowMesh_Free(shadowmesh_t *mesh)
715 shadowmesh_t *nextmesh;
716 for (;mesh;mesh = nextmesh)
718 nextmesh = mesh->next;