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.
28 cvar_t r_mipsprites = {CVAR_SAVE, "r_mipsprites", "1"};
35 void Mod_SpriteInit (void)
37 Cvar_RegisterVariable(&r_mipsprites);
40 static void Mod_Sprite_SharedSetup(const unsigned char *datapointer, int version, const unsigned int *palette, const unsigned int *alphapalette)
42 int i, j, groupframes, realframes, x, y, origin[2], width, height;
43 dspriteframetype_t *pinframetype;
44 dspriteframe_t *pinframe;
45 dspritegroup_t *pingroup;
46 dspriteinterval_t *pinintervals;
47 float modelradius, interval;
48 char name[MAX_QPATH], fogname[MAX_QPATH];
49 const void *startframes;
52 if (loadmodel->numframes < 1)
53 Host_Error ("Mod_Sprite_SharedSetup: Invalid # of frames: %d", loadmodel->numframes);
55 // LordHavoc: hack to allow sprites to be non-fullbright
56 for (i = 0;i < MAX_QPATH && loadmodel->name[i];i++)
58 if (loadmodel->name[i] == '!')
60 loadmodel->flags2 &= ~EF_FULLBRIGHT;
68 startframes = datapointer;
70 for (i = 0;i < loadmodel->numframes;i++)
72 pinframetype = (dspriteframetype_t *)datapointer;
73 datapointer += sizeof(dspriteframetype_t);
75 if (LittleLong (pinframetype->type) == SPR_SINGLE)
79 pingroup = (dspritegroup_t *)datapointer;
80 datapointer += sizeof(dspritegroup_t);
82 groupframes = LittleLong(pingroup->numframes);
84 datapointer += sizeof(dspriteinterval_t) * groupframes;
87 for (j = 0;j < groupframes;j++)
89 pinframe = (dspriteframe_t *)datapointer;
90 if (version == SPRITE32_VERSION)
91 datapointer += sizeof(dspriteframe_t) + LittleLong(pinframe->width) * LittleLong(pinframe->height) * 4;
92 else //if (version == SPRITE_VERSION || version == SPRITEHL_VERSION)
93 datapointer += sizeof(dspriteframe_t) + LittleLong(pinframe->width) * LittleLong(pinframe->height);
95 realframes += groupframes;
98 loadmodel->animscenes = (animscene_t *)Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
99 loadmodel->sprite.sprdata_frames = (mspriteframe_t *)Mem_Alloc(loadmodel->mempool, sizeof(mspriteframe_t) * realframes);
101 datapointer = (unsigned char *)startframes;
103 for (i = 0;i < loadmodel->numframes;i++)
105 pinframetype = (dspriteframetype_t *)datapointer;
106 datapointer += sizeof(dspriteframetype_t);
108 if (LittleLong (pinframetype->type) == SPR_SINGLE)
115 pingroup = (dspritegroup_t *)datapointer;
116 datapointer += sizeof(dspritegroup_t);
118 groupframes = LittleLong(pingroup->numframes);
120 pinintervals = (dspriteinterval_t *)datapointer;
121 datapointer += sizeof(dspriteinterval_t) * groupframes;
123 interval = LittleFloat(pinintervals[0].interval);
124 if (interval < 0.01f)
125 Host_Error("Mod_Sprite_SharedSetup: invalid interval");
128 sprintf(loadmodel->animscenes[i].name, "frame %i", i);
129 loadmodel->animscenes[i].firstframe = realframes;
130 loadmodel->animscenes[i].framecount = groupframes;
131 loadmodel->animscenes[i].framerate = 1.0f / interval;
132 loadmodel->animscenes[i].loop = true;
134 for (j = 0;j < groupframes;j++)
136 pinframe = (dspriteframe_t *)datapointer;
137 datapointer += sizeof(dspriteframe_t);
139 origin[0] = LittleLong (pinframe->origin[0]);
140 origin[1] = LittleLong (pinframe->origin[1]);
141 width = LittleLong (pinframe->width);
142 height = LittleLong (pinframe->height);
144 loadmodel->sprite.sprdata_frames[realframes].left = origin[0];
145 loadmodel->sprite.sprdata_frames[realframes].right = origin[0] + width;
146 loadmodel->sprite.sprdata_frames[realframes].up = origin[1];
147 loadmodel->sprite.sprdata_frames[realframes].down = origin[1] - height;
149 x = max(loadmodel->sprite.sprdata_frames[realframes].left * loadmodel->sprite.sprdata_frames[realframes].left, loadmodel->sprite.sprdata_frames[realframes].right * loadmodel->sprite.sprdata_frames[realframes].right);
150 y = max(loadmodel->sprite.sprdata_frames[realframes].up * loadmodel->sprite.sprdata_frames[realframes].up, loadmodel->sprite.sprdata_frames[realframes].down * loadmodel->sprite.sprdata_frames[realframes].down);
151 if (modelradius < x + y)
154 if (width > 0 && height > 0 && cls.state != ca_dedicated)
157 sprintf (name, "%s_%i_%i", loadmodel->name, i, j);
159 sprintf (name, "%s_%i", loadmodel->name, i);
160 Mod_LoadSkinFrame(&loadmodel->sprite.sprdata_frames[realframes].skin, name, (r_mipsprites.integer ? TEXF_MIPMAP : 0) | TEXF_ALPHA | TEXF_CLAMP | TEXF_PRECACHE | TEXF_PICMIP, false, false);
162 if (!loadmodel->sprite.sprdata_frames[realframes].skin.base)
165 sprintf (fogname, "%s_%i_%ifog", loadmodel->name, i, j);
167 sprintf (fogname, "%s_%ifog", loadmodel->name, i);
168 if (version == SPRITE32_VERSION)
169 Mod_LoadSkinFrame_Internal(&loadmodel->sprite.sprdata_frames[realframes].skin, name, (r_mipsprites.integer ? TEXF_MIPMAP : 0) | TEXF_ALPHA | TEXF_CLAMP | TEXF_PRECACHE | TEXF_PICMIP, false, false, datapointer, width, height, 32, NULL, NULL);
170 else //if (version == SPRITE_VERSION || version == SPRITEHL_VERSION)
171 Mod_LoadSkinFrame_Internal(&loadmodel->sprite.sprdata_frames[realframes].skin, name, (r_mipsprites.integer ? TEXF_MIPMAP : 0) | TEXF_ALPHA | TEXF_CLAMP | TEXF_PRECACHE | TEXF_PICMIP, false, false, datapointer, width, height, 8, palette, alphapalette);
175 if (version == SPRITE32_VERSION)
176 datapointer += width * height * 4;
177 else //if (version == SPRITE_VERSION || version == SPRITEHL_VERSION)
178 datapointer += width * height;
183 modelradius = sqrt(modelradius);
184 for (i = 0;i < 3;i++)
186 loadmodel->normalmins[i] = loadmodel->yawmins[i] = loadmodel->rotatedmins[i] = -modelradius;
187 loadmodel->normalmaxs[i] = loadmodel->yawmaxs[i] = loadmodel->rotatedmaxs[i] = modelradius;
189 loadmodel->radius = modelradius;
190 loadmodel->radius2 = modelradius * modelradius;
193 extern void R_Model_Sprite_Draw(entity_render_t *ent);
194 void Mod_IDSP_Load(model_t *mod, void *buffer, void *bufferend)
197 const unsigned char *datapointer;
199 datapointer = (unsigned char *)buffer;
201 loadmodel->type = mod_sprite;
202 loadmodel->flags2 = EF_FULLBRIGHT;
204 loadmodel->DrawSky = NULL;
205 loadmodel->Draw = R_Model_Sprite_Draw;
206 loadmodel->CompileShadowVolume = NULL;
207 loadmodel->DrawShadowVolume = NULL;
208 loadmodel->DrawLight = NULL;
210 version = LittleLong(((dsprite_t *)buffer)->version);
211 if (version == SPRITE_VERSION || version == SPRITE32_VERSION)
213 dsprite_t *pinqsprite;
215 pinqsprite = (dsprite_t *)datapointer;
216 datapointer += sizeof(dsprite_t);
218 loadmodel->numframes = LittleLong (pinqsprite->numframes);
219 loadmodel->sprite.sprnum_type = LittleLong (pinqsprite->type);
220 loadmodel->synctype = (synctype_t)LittleLong (pinqsprite->synctype);
222 Mod_Sprite_SharedSetup(datapointer, LittleLong (pinqsprite->version), palette_complete, palette_alpha);
224 else if (version == SPRITEHL_VERSION)
227 unsigned char palette[256][4], alphapalette[256][4];
228 const unsigned char *in;
229 dspritehl_t *pinhlsprite;
231 pinhlsprite = (dspritehl_t *)datapointer;
232 datapointer += sizeof(dspritehl_t);
234 loadmodel->numframes = LittleLong (pinhlsprite->numframes);
235 loadmodel->sprite.sprnum_type = LittleLong (pinhlsprite->type);
236 loadmodel->synctype = (synctype_t)LittleLong (pinhlsprite->synctype);
237 rendermode = pinhlsprite->rendermode;
241 i = in[0] + in[1] * 256;
243 Host_Error ("Mod_IDSP_Load: unexpected number of palette colors %i (should be 256)", i);
249 for (i = 0;i < 256;i++)
251 palette[i][0] = *in++;
252 palette[i][1] = *in++;
253 palette[i][2] = *in++;
258 for (i = 0;i < 256;i++)
260 palette[i][0] = *in++;
261 palette[i][1] = *in++;
262 palette[i][2] = *in++;
265 loadmodel->flags2 |= EF_ADDITIVE;
267 case SPRHL_INDEXALPHA:
268 for (i = 0;i < 256;i++)
270 palette[i][0] = in[765];
271 palette[i][1] = in[766];
272 palette[i][2] = in[767];
277 case SPRHL_ALPHATEST:
278 for (i = 0;i < 256;i++)
280 palette[i][0] = *in++;
281 palette[i][1] = *in++;
282 palette[i][2] = *in++;
285 palette[255][0] = palette[255][1] = palette[255][2] = palette[255][3] = 0;
288 Host_Error("Mod_IDSP_Load: unknown texFormat (%i, should be 0, 1, 2, or 3)", i);
292 for (i = 0;i < 256;i++)
294 alphapalette[i][0] = 255;
295 alphapalette[i][1] = 255;
296 alphapalette[i][2] = 255;
297 alphapalette[i][3] = palette[i][3];
300 Mod_Sprite_SharedSetup(datapointer, LittleLong (pinhlsprite->version), (unsigned int *)(&palette[0][0]), (unsigned int *)(&alphapalette[0][0]));
303 Host_Error("Mod_IDSP_Load: %s has wrong version number (%i). Only %i (quake), %i (HalfLife), and %i (sprite32) supported",
304 loadmodel->name, version, SPRITE_VERSION, SPRITEHL_VERSION, SPRITE32_VERSION);
308 void Mod_IDS2_Load(model_t *mod, void *buffer, void *bufferend)
311 const dsprite2_t *pinqsprite;
314 loadmodel->type = mod_sprite;
315 loadmodel->flags2 = EF_FULLBRIGHT;
317 loadmodel->DrawSky = NULL;
318 loadmodel->Draw = R_Model_Sprite_Draw;
319 loadmodel->CompileShadowVolume = NULL;
320 loadmodel->DrawShadowVolume = NULL;
321 loadmodel->DrawLight = NULL;
323 pinqsprite = (dsprite2_t *)buffer;
325 version = LittleLong(pinqsprite->version);
326 if (version != SPRITE2_VERSION)
327 Host_Error("Mod_IDS2_Load: %s has wrong version number (%i should be 2 (quake 2)", loadmodel->name, version);
329 loadmodel->numframes = LittleLong (pinqsprite->numframes);
330 if (loadmodel->numframes < 1)
331 Host_Error ("Mod_IDS2_Load: Invalid # of frames: %d", loadmodel->numframes);
332 loadmodel->sprite.sprnum_type = SPR_VP_PARALLEL;
333 loadmodel->synctype = ST_SYNC;
335 // Hack to allow sprites to be non-fullbright
336 for (i = 0;i < MAX_QPATH && loadmodel->name[i];i++)
338 if (loadmodel->name[i] == '!')
340 loadmodel->flags2 &= ~EF_FULLBRIGHT;
345 loadmodel->animscenes = (animscene_t *)Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
346 loadmodel->sprite.sprdata_frames = (mspriteframe_t *)Mem_Alloc(loadmodel->mempool, sizeof(mspriteframe_t) * loadmodel->numframes);
349 for (i = 0;i < loadmodel->numframes;i++)
351 int origin[2], x, y, width, height;
352 const dsprite2frame_t *pinframe;
353 mspriteframe_t *sprframe;
355 dpsnprintf(loadmodel->animscenes[i].name, sizeof(loadmodel->animscenes[i].name), "frame %i", i);
356 loadmodel->animscenes[i].firstframe = i;
357 loadmodel->animscenes[i].framecount = 1;
358 loadmodel->animscenes[i].framerate = 10;
359 loadmodel->animscenes[i].loop = true;
361 pinframe = &pinqsprite->frames[i];
363 origin[0] = LittleLong (pinframe->origin_x);
364 origin[1] = LittleLong (pinframe->origin_y);
365 width = LittleLong (pinframe->width);
366 height = LittleLong (pinframe->height);
368 sprframe = &loadmodel->sprite.sprdata_frames[i];
370 sprframe->left = origin[0];
371 sprframe->right = origin[0] + width;
372 sprframe->up = origin[1];
373 sprframe->down = origin[1] - height;
375 x = max(sprframe->left * sprframe->left, sprframe->right * sprframe->right);
376 y = max(sprframe->up * sprframe->up, sprframe->down * sprframe->down);
377 if (modelradius < x + y)
380 if (width > 0 && height > 0 && cls.state != ca_dedicated)
382 Mod_LoadSkinFrame(&sprframe->skin, pinframe->name, (r_mipsprites.integer ? TEXF_MIPMAP : 0) | TEXF_ALPHA | TEXF_CLAMP | TEXF_PRECACHE | TEXF_PICMIP, false, false);
383 // TODO: use a default texture if we can't load it?
384 if (sprframe->skin.base == NULL)
385 Host_Error("Mod_IDS2_Load: failed to load %s", pinframe->name);
389 modelradius = sqrt(modelradius);
390 for (i = 0;i < 3;i++)
392 loadmodel->normalmins[i] = loadmodel->yawmins[i] = loadmodel->rotatedmins[i] = -modelradius;
393 loadmodel->normalmaxs[i] = loadmodel->yawmaxs[i] = loadmodel->rotatedmaxs[i] = modelradius;
395 loadmodel->radius = modelradius;
396 loadmodel->radius2 = modelradius * modelradius;