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 int alphaonlytable[4] = {255 | 0x80000000, 255 | 0x80000000, 255 | 0x80000000, 3};
41 static void Mod_Sprite_SharedSetup(const qbyte *datapointer, int version, const unsigned int *palette, const unsigned int *alphapalette)
43 int i, j, groupframes, realframes, x, y, origin[2], width, height;
44 dspriteframetype_t *pinframetype;
45 dspriteframe_t *pinframe;
46 dspritegroup_t *pingroup;
47 dspriteinterval_t *pinintervals;
48 float modelradius, interval;
49 char name[MAX_QPATH], fogname[MAX_QPATH];
51 const void *startframes;
54 if (loadmodel->numframes < 1)
55 Host_Error ("Mod_Sprite_SharedSetup: Invalid # of frames: %d\n", loadmodel->numframes);
57 // LordHavoc: hack to allow sprites to be non-fullbright
58 for (i = 0;i < MAX_QPATH && loadmodel->name[i];i++)
60 if (loadmodel->name[i] == '!')
62 loadmodel->flags2 &= ~EF_FULLBRIGHT;
70 startframes = datapointer;
72 for (i = 0;i < loadmodel->numframes;i++)
74 pinframetype = (dspriteframetype_t *)datapointer;
75 datapointer += sizeof(dspriteframetype_t);
77 if (LittleLong (pinframetype->type) == SPR_SINGLE)
81 pingroup = (dspritegroup_t *)datapointer;
82 datapointer += sizeof(dspritegroup_t);
84 groupframes = LittleLong(pingroup->numframes);
86 datapointer += sizeof(dspriteinterval_t) * groupframes;
89 for (j = 0;j < groupframes;j++)
91 pinframe = (dspriteframe_t *)datapointer;
92 if (version == SPRITE32_VERSION)
93 datapointer += sizeof(dspriteframe_t) + LittleLong(pinframe->width) * LittleLong(pinframe->height) * 4;
94 else //if (version == SPRITE_VERSION || version == SPRITEHL_VERSION)
95 datapointer += sizeof(dspriteframe_t) + LittleLong(pinframe->width) * LittleLong(pinframe->height);
97 realframes += groupframes;
100 loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
101 loadmodel->sprite.sprdata_frames = Mem_Alloc(loadmodel->mempool, sizeof(mspriteframe_t) * realframes);
103 datapointer = startframes;
105 for (i = 0;i < loadmodel->numframes;i++)
107 pinframetype = (dspriteframetype_t *)datapointer;
108 datapointer += sizeof(dspriteframetype_t);
110 if (LittleLong (pinframetype->type) == SPR_SINGLE)
117 pingroup = (dspritegroup_t *)datapointer;
118 datapointer += sizeof(dspritegroup_t);
120 groupframes = LittleLong(pingroup->numframes);
122 pinintervals = (dspriteinterval_t *)datapointer;
123 datapointer += sizeof(dspriteinterval_t) * groupframes;
125 interval = LittleFloat(pinintervals[0].interval);
126 if (interval < 0.01f)
127 Host_Error("Mod_Sprite_SharedSetup: invalid interval");
130 sprintf(loadmodel->animscenes[i].name, "frame %i", i);
131 loadmodel->animscenes[i].firstframe = realframes;
132 loadmodel->animscenes[i].framecount = groupframes;
133 loadmodel->animscenes[i].framerate = 1.0f / interval;
134 loadmodel->animscenes[i].loop = true;
136 for (j = 0;j < groupframes;j++)
138 pinframe = (dspriteframe_t *)datapointer;
139 datapointer += sizeof(dspriteframe_t);
141 origin[0] = LittleLong (pinframe->origin[0]);
142 origin[1] = LittleLong (pinframe->origin[1]);
143 width = LittleLong (pinframe->width);
144 height = LittleLong (pinframe->height);
146 loadmodel->sprite.sprdata_frames[realframes].left = origin[0];
147 loadmodel->sprite.sprdata_frames[realframes].right = origin[0] + width;
148 loadmodel->sprite.sprdata_frames[realframes].up = origin[1];
149 loadmodel->sprite.sprdata_frames[realframes].down = origin[1] - height;
151 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);
152 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);
153 if (modelradius < x + y)
156 if (width > 0 && height > 0)
159 sprintf (name, "%s_%i_%i", loadmodel->name, i, j);
161 sprintf (name, "%s_%i", loadmodel->name, i);
162 loadmodel->sprite.sprdata_frames[realframes].texture = loadtextureimagewithmask(loadmodel->texturepool, name, 0, 0, false, (r_mipsprites.integer ? TEXF_MIPMAP : 0) | TEXF_ALPHA | TEXF_CLAMP | TEXF_PRECACHE | TEXF_PICMIP);
163 loadmodel->sprite.sprdata_frames[realframes].fogtexture = image_masktex;
165 if (!loadmodel->sprite.sprdata_frames[realframes].texture)
168 sprintf (fogname, "%s_%i_%ifog", loadmodel->name, i, j);
170 sprintf (fogname, "%s_%ifog", loadmodel->name, i);
171 if (version == SPRITE32_VERSION)
173 loadmodel->sprite.sprdata_frames[realframes].texture = R_LoadTexture2D(loadmodel->texturepool, name, width, height, datapointer, TEXTYPE_RGBA, TEXF_ALPHA | (r_mipsprites.integer ? TEXF_MIPMAP : 0) | TEXF_CLAMP | TEXF_PRECACHE | TEXF_PICMIP, NULL);
174 // make fog version (just alpha)
175 pixbuf = Mem_Alloc(tempmempool, width*height*4);
176 Image_CopyMux(pixbuf, datapointer, width, height, false, false, false, 4, 4, alphaonlytable);
177 loadmodel->sprite.sprdata_frames[realframes].fogtexture = R_LoadTexture2D(loadmodel->texturepool, fogname, width, height, pixbuf, TEXTYPE_RGBA, TEXF_ALPHA | (r_mipsprites.integer ? TEXF_MIPMAP : 0) | TEXF_CLAMP | TEXF_PRECACHE | TEXF_PICMIP, NULL);
180 else //if (version == SPRITE_VERSION || version == SPRITEHL_VERSION)
182 loadmodel->sprite.sprdata_frames[realframes].texture = R_LoadTexture2D(loadmodel->texturepool, name, width, height, datapointer, TEXTYPE_PALETTE, TEXF_ALPHA | (r_mipsprites.integer ? TEXF_MIPMAP : 0) | TEXF_CLAMP | TEXF_PRECACHE | TEXF_PICMIP, palette);
183 loadmodel->sprite.sprdata_frames[realframes].fogtexture = R_LoadTexture2D(loadmodel->texturepool, fogname, width, height, datapointer, TEXTYPE_PALETTE, TEXF_ALPHA | (r_mipsprites.integer ? TEXF_MIPMAP : 0) | TEXF_CLAMP | TEXF_PRECACHE | TEXF_PICMIP, alphapalette);
188 if (version == SPRITE32_VERSION)
189 datapointer += width * height * 4;
190 else //if (version == SPRITE_VERSION || version == SPRITEHL_VERSION)
191 datapointer += width * height;
196 modelradius = sqrt(modelradius);
197 for (i = 0;i < 3;i++)
199 loadmodel->normalmins[i] = loadmodel->yawmins[i] = loadmodel->rotatedmins[i] = -modelradius;
200 loadmodel->normalmaxs[i] = loadmodel->yawmaxs[i] = loadmodel->rotatedmaxs[i] = modelradius;
202 loadmodel->radius = modelradius;
203 loadmodel->radius2 = modelradius * modelradius;
206 extern void R_Model_Sprite_Draw(entity_render_t *ent);
207 void Mod_IDSP_Load(model_t *mod, void *buffer, void *bufferend)
210 const qbyte *datapointer;
212 datapointer = buffer;
214 loadmodel->type = mod_sprite;
215 loadmodel->flags2 = EF_FULLBRIGHT;
217 loadmodel->DrawSky = NULL;
218 loadmodel->Draw = R_Model_Sprite_Draw;
219 loadmodel->CompileShadowVolume = NULL;
220 loadmodel->DrawShadowVolume = NULL;
221 loadmodel->DrawLight = NULL;
223 version = LittleLong(((dsprite_t *)buffer)->version);
224 if (version == SPRITE_VERSION || version == SPRITE32_VERSION)
226 dsprite_t *pinqsprite;
228 pinqsprite = (dsprite_t *)datapointer;
229 datapointer += sizeof(dsprite_t);
231 loadmodel->numframes = LittleLong (pinqsprite->numframes);
232 loadmodel->sprite.sprnum_type = LittleLong (pinqsprite->type);
233 loadmodel->synctype = LittleLong (pinqsprite->synctype);
235 Mod_Sprite_SharedSetup(datapointer, LittleLong (pinqsprite->version), palette_complete, palette_alpha);
237 else if (version == SPRITEHL_VERSION)
240 qbyte palette[256][4], alphapalette[256][4];
242 dspritehl_t *pinhlsprite;
244 pinhlsprite = (dspritehl_t *)datapointer;
245 datapointer += sizeof(dspritehl_t);
247 loadmodel->numframes = LittleLong (pinhlsprite->numframes);
248 loadmodel->sprite.sprnum_type = LittleLong (pinhlsprite->type);
249 loadmodel->synctype = LittleLong (pinhlsprite->synctype);
250 rendermode = pinhlsprite->rendermode;
254 i = in[0] + in[1] * 256;
256 Host_Error ("Mod_IDSP_Load: unexpected number of palette colors %i (should be 256)", i);
262 for (i = 0;i < 256;i++)
264 palette[i][0] = *in++;
265 palette[i][1] = *in++;
266 palette[i][2] = *in++;
271 for (i = 0;i < 256;i++)
273 palette[i][0] = *in++;
274 palette[i][1] = *in++;
275 palette[i][2] = *in++;
278 loadmodel->flags2 |= EF_ADDITIVE;
280 case SPRHL_INDEXALPHA:
281 for (i = 0;i < 256;i++)
283 palette[i][0] = in[765];
284 palette[i][1] = in[766];
285 palette[i][2] = in[767];
290 case SPRHL_ALPHATEST:
291 for (i = 0;i < 256;i++)
293 palette[i][0] = *in++;
294 palette[i][1] = *in++;
295 palette[i][2] = *in++;
298 palette[255][0] = palette[255][1] = palette[255][2] = palette[255][3] = 0;
301 Host_Error("Mod_IDSP_Load: unknown texFormat (%i, should be 0, 1, 2, or 3)\n", i);
305 for (i = 0;i < 256;i++)
307 alphapalette[i][0] = 255;
308 alphapalette[i][1] = 255;
309 alphapalette[i][2] = 255;
310 alphapalette[i][3] = palette[i][3];
313 Mod_Sprite_SharedSetup(datapointer, LittleLong (pinhlsprite->version), (unsigned int *)(&palette[0][0]), (unsigned int *)(&alphapalette[0][0]));
316 Host_Error("Mod_IDSP_Load: %s has wrong version number (%i). Only %i (quake), %i (HalfLife), and %i (sprite32) supported",
317 loadmodel->name, version, SPRITE_VERSION, SPRITEHL_VERSION, SPRITE32_VERSION);
321 void Mod_IDS2_Load(model_t *mod, void *buffer, void *bufferend)
324 const dsprite2_t *pinqsprite;
327 loadmodel->type = mod_sprite;
328 loadmodel->flags2 = EF_FULLBRIGHT;
330 loadmodel->DrawSky = NULL;
331 loadmodel->Draw = R_Model_Sprite_Draw;
332 loadmodel->CompileShadowVolume = NULL;
333 loadmodel->DrawShadowVolume = NULL;
334 loadmodel->DrawLight = NULL;
336 pinqsprite = (dsprite2_t *)buffer;
338 version = LittleLong(pinqsprite->version);
339 if (version != SPRITE2_VERSION)
340 Host_Error("Mod_IDS2_Load: %s has wrong version number (%i should be 2 (quake 2)", loadmodel->name, version);
342 loadmodel->numframes = LittleLong (pinqsprite->numframes);
343 if (loadmodel->numframes < 1)
344 Host_Error ("Mod_IDS2_Load: Invalid # of frames: %d\n", loadmodel->numframes);
345 loadmodel->sprite.sprnum_type = SPR_VP_PARALLEL;
346 loadmodel->synctype = ST_SYNC;
348 // Hack to allow sprites to be non-fullbright
349 for (i = 0;i < MAX_QPATH && loadmodel->name[i];i++)
351 if (loadmodel->name[i] == '!')
353 loadmodel->flags2 &= ~EF_FULLBRIGHT;
358 loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
359 loadmodel->sprite.sprdata_frames = Mem_Alloc(loadmodel->mempool, sizeof(mspriteframe_t) * loadmodel->numframes);
362 for (i = 0;i < loadmodel->numframes;i++)
364 int origin[2], x, y, width, height;
365 const dsprite2frame_t *pinframe;
366 mspriteframe_t *sprframe;
368 dpsnprintf(loadmodel->animscenes[i].name, sizeof(loadmodel->animscenes[i].name), "frame %i", i);
369 loadmodel->animscenes[i].firstframe = i;
370 loadmodel->animscenes[i].framecount = 1;
371 loadmodel->animscenes[i].framerate = 10;
372 loadmodel->animscenes[i].loop = true;
374 pinframe = &pinqsprite->frames[i];
376 origin[0] = LittleLong (pinframe->origin_x);
377 origin[1] = LittleLong (pinframe->origin_y);
378 width = LittleLong (pinframe->width);
379 height = LittleLong (pinframe->height);
381 sprframe = &loadmodel->sprite.sprdata_frames[i];
383 sprframe->left = origin[0];
384 sprframe->right = origin[0] + width;
385 sprframe->up = origin[1];
386 sprframe->down = origin[1] - height;
388 x = max(sprframe->left * sprframe->left, sprframe->right * sprframe->right);
389 y = max(sprframe->up * sprframe->up, sprframe->down * sprframe->down);
390 if (modelradius < x + y)
393 if (width > 0 && height > 0)
395 sprframe->texture = loadtextureimagewithmask(loadmodel->texturepool, pinframe->name, 0, 0, false, (r_mipsprites.integer ? TEXF_MIPMAP : 0) | TEXF_ALPHA | TEXF_CLAMP | TEXF_PRECACHE | TEXF_PICMIP);
396 sprframe->fogtexture = image_masktex;
398 // TODO: use a default texture if we can't load it?
399 if (sprframe->texture == NULL)
400 Host_Error("Mod_IDS2_Load: failed to load %s", pinframe->name);
404 modelradius = sqrt(modelradius);
405 for (i = 0;i < 3;i++)
407 loadmodel->normalmins[i] = loadmodel->yawmins[i] = loadmodel->rotatedmins[i] = -modelradius;
408 loadmodel->normalmaxs[i] = loadmodel->yawmaxs[i] = loadmodel->rotatedmaxs[i] = modelradius;
410 loadmodel->radius = modelradius;
411 loadmodel->radius2 = modelradius * modelradius;