]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - model_shared.c
1aaac8e46b4b42988b2f36cbb739c815c12f1d1d
[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 char    loadname[32];   // for hunk tags
29
30 extern void Mod_LoadSpriteModel (model_t *mod, void *buffer);
31 extern void Mod_LoadBrushModel (model_t *mod, void *buffer);
32 extern void Mod_LoadAliasModel (model_t *mod, void *buffer);
33 extern void Mod_LoadQ2AliasModel (model_t *mod, void *buffer);
34 extern void Mod_LoadZymoticModel (model_t *mod, void *buffer);
35 model_t *Mod_LoadModel (model_t *mod, qboolean crash);
36
37 // LordHavoc: increased from 512 to 2048
38 #define MAX_MOD_KNOWN   2048
39 model_t mod_known[MAX_MOD_KNOWN];
40 int             mod_numknown;
41
42 extern void Mod_BrushInit();
43 extern void Mod_AliasInit();
44 extern void Mod_SpriteInit();
45
46 /*
47 ===============
48 Mod_Init
49 ===============
50 */
51 void Mod_Init (void)
52 {
53         Mod_BrushInit();
54         Mod_AliasInit();
55         Mod_SpriteInit();
56 }
57
58 /*
59 ===============
60 Mod_Init
61
62 Caches the data if needed
63 ===============
64 */
65 void *Mod_Extradata (model_t *mod)
66 {
67         void    *r;
68         
69         r = Cache_Check (&mod->cache);
70         if (r)
71                 return r;
72
73         Mod_LoadModel (mod, true);
74         
75         if (!mod->cache.data)
76                 Host_Error ("Mod_Extradata: caching failed");
77         return mod->cache.data;
78 }
79
80 /*
81 ===================
82 Mod_ClearAll
83 ===================
84 */
85 void Mod_ClearAll (void)
86 {
87         int             i;
88         model_t *mod;
89         
90         for (i=0 , mod=mod_known ; i<mod_numknown ; i++, mod++)
91                 if (!mod->cachesize)
92                         mod->needload = true;
93 }
94
95 /*
96 ==================
97 Mod_FindName
98
99 ==================
100 */
101 model_t *Mod_FindName (char *name)
102 {
103         int             i;
104         model_t *mod;
105         
106         if (!name[0])
107                 Host_Error ("Mod_ForName: NULL name");
108                 
109 //
110 // search the currently loaded models
111 //
112         for (i=0 , mod=mod_known ; i<mod_numknown ; i++, mod++)
113                 if (!strcmp (mod->name, name) )
114                         break;
115                         
116         if (i == mod_numknown)
117         {
118                 if (mod_numknown == MAX_MOD_KNOWN)
119                         Host_Error ("mod_numknown == MAX_MOD_KNOWN");
120                 strcpy (mod->name, name);
121                 mod->needload = true;
122                 mod_numknown++;
123         }
124
125         return mod;
126 }
127
128 /*
129 ==================
130 Mod_TouchModel
131
132 ==================
133 */
134 void Mod_TouchModel (char *name)
135 {
136         model_t *mod;
137         
138         mod = Mod_FindName (name);
139         
140         if (!mod->needload)
141                 if (mod->cachesize)
142                         Cache_Check (&mod->cache);
143 }
144
145 /*
146 ==================
147 Mod_LoadModel
148
149 Loads a model into the cache
150 ==================
151 */
152 model_t *Mod_LoadModel (model_t *mod, qboolean crash)
153 {
154         void    *d;
155         unsigned *buf;
156
157         if (!mod->needload)
158         {
159                 if (mod->cachesize)
160                 {
161                         d = Cache_Check (&mod->cache);
162                         if (d)
163                                 return mod;
164                 }
165                 else
166                         return mod;             // not cached at all
167         }
168
169         Con_DPrintf("loading model %s\n", mod->name);
170
171 // load the file
172         buf = (unsigned *)COM_LoadMallocFile (mod->name, false);
173         if (!buf)
174         {
175                 if (crash)
176                         Host_Error ("Mod_NumForName: %s not found", mod->name); // LordHavoc: Sys_Error was *ANNOYING*
177                 return NULL;
178         }
179         
180 // allocate a new model
181 //      COM_FileBase (mod->name, loadname);
182         strcpy(loadname, mod->name);
183         
184         loadmodel = mod;
185
186 // call the apropriate loader
187         mod->needload = false;
188         
189         switch (LittleLong(*(unsigned *)buf))
190         {
191         case IDPOLYHEADER:
192                 Mod_LoadAliasModel (mod, buf);
193                 break;
194
195         case MD2IDALIASHEADER: // LordHavoc: added Quake2 model support
196                 Mod_LoadQ2AliasModel (mod, buf);
197                 break;
198
199         case (('O'<<24)+('M'<<16)+('Y'<<8)+'Z'):
200                 Mod_LoadZymoticModel(mod, buf);
201                 break;
202                 
203         case IDSPRITEHEADER:
204                 Mod_LoadSpriteModel (mod, buf);
205                 break;
206         
207         default:
208                 Mod_LoadBrushModel (mod, buf);
209                 break;
210         }
211         qfree(buf);
212
213         return mod;
214 }
215
216 /*
217 ==================
218 Mod_ForName
219
220 Loads in a model for the given name
221 ==================
222 */
223 model_t *Mod_ForName (char *name, qboolean crash)
224 {
225         model_t *mod;
226         
227         mod = Mod_FindName (name);
228         
229         return Mod_LoadModel (mod, crash);
230 }
231
232 byte    *mod_base;
233
234 /*
235 =================
236 RadiusFromBounds
237 =================
238 */
239 float RadiusFromBounds (vec3_t mins, vec3_t maxs)
240 {
241         int             i;
242         vec3_t  corner;
243
244         for (i=0 ; i<3 ; i++)
245                 corner[i] = fabs(mins[i]) > fabs(maxs[i]) ? fabs(mins[i]) : fabs(maxs[i]);
246
247         return Length (corner);
248 }
249
250 //=============================================================================
251
252 /*
253 ================
254 Mod_Print
255 ================
256 */
257 void Mod_Print (void)
258 {
259         int             i;
260         model_t *mod;
261
262         Con_Printf ("Cached models:\n");
263         for (i=0, mod=mod_known ; i < mod_numknown ; i++, mod++)
264         {
265                 Con_Printf ("%4iK : %8p : %s\n", (mod->cachesize + 1023) / 1024, mod->cache.data, mod->name);
266         }
267 }
268
269