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_LoadTexture(r_notexturepool, "notexture", 16, 16, &pix[0][0][0], TEXTYPE_RGBA, TEXF_MIPMAP);
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);
105 static void Mod_Flush (void);
112 Cmd_AddCommand ("modellist", Mod_Print);
113 Cmd_AddCommand ("modelflush", Mod_Flush);
116 void Mod_RenderInit(void)
118 R_RegisterModule("Models", mod_start, mod_shutdown, mod_newmap);
121 void Mod_FreeModel (model_t *mod)
123 R_FreeTexturePool(&mod->texturepool);
124 Mem_FreePool(&mod->mempool);
126 // clear the struct to make it available
127 memset(mod, 0, sizeof(model_t));
130 void Mod_UnloadModel (model_t *mod)
132 char name[MAX_QPATH];
133 qboolean isworldmodel;
134 strcpy(name, mod->name);
135 isworldmodel = mod->isworldmodel;
137 strcpy(mod->name, name);
138 mod->isworldmodel = isworldmodel;
139 mod->needload = true;
149 static model_t *Mod_LoadModel (model_t *mod, qboolean crash, qboolean checkdisk, qboolean isworldmodel)
156 if (mod->name[0] == '*') // submodel
165 buf = COM_LoadFile (mod->name, false);
169 Host_Error ("Mod_LoadModel: %s not found", mod->name); // LordHavoc: Sys_Error was *ANNOYING*
173 crc = CRC_Block(buf, com_filesize);
178 if (mod->crc == crc && mod->isworldmodel == isworldmodel)
182 return mod; // already loaded
186 Con_DPrintf("loading model %s\n", mod->name);
190 buf = COM_LoadFile (mod->name, false);
194 Host_Error ("Mod_LoadModel: %s not found", mod->name);
197 crc = CRC_Block(buf, com_filesize);
200 // allocate a new model
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;
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);
228 void Mod_CheckLoaded (model_t *mod)
233 Mod_LoadModel(mod, true, true, mod->isworldmodel);
236 if (mod->type == mod_invalid)
237 Host_Error("Mod_CheckLoaded: invalid model\n");
249 void Mod_ClearAll (void)
253 void Mod_ClearUsed(void)
258 for (i = 0, mod = mod_known;i < MAX_MOD_KNOWN;i++, mod++)
263 void Mod_PurgeUnused(void)
268 for (i = 0, mod = mod_known;i < MAX_MOD_KNOWN;i++, mod++)
274 void Mod_LoadModels(void)
279 for (i = 0, mod = mod_known;i < MAX_MOD_KNOWN;i++, mod++)
282 Mod_CheckLoaded(mod);
291 model_t *Mod_FindName (const char *name)
294 model_t *mod, *freemod;
297 Host_Error ("Mod_ForName: NULL name");
299 // search the currently loaded models
301 for (i = 0, mod = mod_known;i < MAX_MOD_KNOWN;i++, mod++)
305 if (!strcmp (mod->name, name))
311 else if (freemod == NULL)
318 strcpy (mod->name, name);
319 mod->needload = true;
324 Host_Error ("Mod_FindName: ran out of models\n");
334 void Mod_TouchModel (const char *name)
338 mod = Mod_FindName (name);
346 Loads in a model for the given name
349 model_t *Mod_ForName (const char *name, qboolean crash, qboolean checkdisk, qboolean isworldmodel)
351 return Mod_LoadModel (Mod_FindName (name), crash, checkdisk, isworldmodel);
357 //=============================================================================
364 static void Mod_Print (void)
369 Con_Printf ("Loaded models:\n");
370 for (i = 0, mod = mod_known;i < MAX_MOD_KNOWN;i++, mod++)
372 Con_Printf ("%4iK %s\n", mod->mempool ? (mod->mempool->totalsize + 1023) / 1024 : 0, mod->name);
375 static void Mod_Flush (void)
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]);
386 int Mod_FindTriangleWithEdge(int *elements, int numtriangles, int start, int end)
389 for (i = 0;i < numtriangles;i++, elements += 3)
391 if (elements[0] == start && elements[1] == end)
393 if (elements[1] == start && elements[2] == end)
395 if (elements[2] == start && elements[0] == end)
401 void Mod_BuildTriangleNeighbors(int *neighbors, int *elements, int numtriangles)
404 for (i = 0, e = elements, n = neighbors;i < numtriangles;i++, e += 3, n += 3)
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]);