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 int *palette, const 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, const void *buffer)
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->DrawShadowVolume = NULL;
220 loadmodel->DrawLight = NULL;
222 version = LittleLong(((dsprite_t *)buffer)->version);
223 if (version == SPRITE_VERSION || version == SPRITE32_VERSION)
225 dsprite_t *pinqsprite;
227 pinqsprite = (dsprite_t *)datapointer;
228 datapointer += sizeof(dsprite_t);
230 loadmodel->numframes = LittleLong (pinqsprite->numframes);
231 loadmodel->sprite.sprnum_type = LittleLong (pinqsprite->type);
232 loadmodel->synctype = LittleLong (pinqsprite->synctype);
234 Mod_Sprite_SharedSetup(datapointer, LittleLong (pinqsprite->version), palette_complete, palette_alpha);
236 else if (version == SPRITEHL_VERSION)
239 qbyte palette[256][4], alphapalette[256][4];
241 dspritehl_t *pinhlsprite;
243 pinhlsprite = (dspritehl_t *)datapointer;
244 datapointer += sizeof(dspritehl_t);
246 loadmodel->numframes = LittleLong (pinhlsprite->numframes);
247 loadmodel->sprite.sprnum_type = LittleLong (pinhlsprite->type);
248 loadmodel->synctype = LittleLong (pinhlsprite->synctype);
249 rendermode = pinhlsprite->rendermode;
253 i = in[0] + in[1] * 256;
255 Host_Error ("Mod_IDSP_Load: unexpected number of palette colors %i (should be 256)", i);
261 for (i = 0;i < 256;i++)
263 palette[i][0] = *in++;
264 palette[i][1] = *in++;
265 palette[i][2] = *in++;
270 for (i = 0;i < 256;i++)
272 palette[i][0] = *in++;
273 palette[i][1] = *in++;
274 palette[i][2] = *in++;
277 loadmodel->flags2 |= EF_ADDITIVE;
279 case SPRHL_INDEXALPHA:
280 for (i = 0;i < 256;i++)
282 palette[i][0] = in[765];
283 palette[i][1] = in[766];
284 palette[i][2] = in[767];
289 case SPRHL_ALPHATEST:
290 for (i = 0;i < 256;i++)
292 palette[i][0] = *in++;
293 palette[i][1] = *in++;
294 palette[i][2] = *in++;
297 palette[255][0] = palette[255][1] = palette[255][2] = palette[255][3] = 0;
300 Host_Error("Mod_IDSP_Load: unknown texFormat (%i, should be 0, 1, 2, or 3)\n", i);
304 for (i = 0;i < 256;i++)
306 alphapalette[i][0] = 255;
307 alphapalette[i][1] = 255;
308 alphapalette[i][2] = 255;
309 alphapalette[i][3] = palette[i][3];
312 Mod_Sprite_SharedSetup(datapointer, LittleLong (pinhlsprite->version), (int *)(&palette[0][0]), (int *)(&alphapalette[0][0]));
315 Host_Error("Mod_IDSP_Load: %s has wrong version number (%i). Only %i (quake), %i (HalfLife), and %i (sprite32) supported",
316 loadmodel->name, version, SPRITE_VERSION, SPRITEHL_VERSION, SPRITE32_VERSION);
320 void Mod_IDS2_Load(model_t *mod, const void *buffer)
323 const dsprite2_t *pinqsprite;
326 loadmodel->type = mod_sprite;
327 loadmodel->flags2 = EF_FULLBRIGHT;
329 loadmodel->DrawSky = NULL;
330 loadmodel->Draw = R_Model_Sprite_Draw;
331 loadmodel->DrawShadowVolume = NULL;
332 loadmodel->DrawLight = NULL;
334 pinqsprite = (dsprite2_t *)buffer;
336 version = LittleLong(pinqsprite->version);
337 if (version != SPRITE2_VERSION)
338 Host_Error("Mod_IDS2_Load: %s has wrong version number (%i should be 2 (quake 2)", loadmodel->name, version);
340 loadmodel->numframes = LittleLong (pinqsprite->numframes);
341 if (loadmodel->numframes < 1)
342 Host_Error ("Mod_IDS2_Load: Invalid # of frames: %d\n", loadmodel->numframes);
343 loadmodel->sprite.sprnum_type = SPR_VP_PARALLEL;
344 loadmodel->synctype = ST_SYNC;
346 // Hack to allow sprites to be non-fullbright
347 for (i = 0;i < MAX_QPATH && loadmodel->name[i];i++)
349 if (loadmodel->name[i] == '!')
351 loadmodel->flags2 &= ~EF_FULLBRIGHT;
356 loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
357 loadmodel->sprite.sprdata_frames = Mem_Alloc(loadmodel->mempool, sizeof(mspriteframe_t) * loadmodel->numframes);
360 for (i = 0;i < loadmodel->numframes;i++)
362 int origin[2], x, y, width, height;
363 const dsprite2frame_t *pinframe;
364 mspriteframe_t *sprframe;
366 dpsnprintf(loadmodel->animscenes[i].name, sizeof(loadmodel->animscenes[i].name), "frame %i", i);
367 loadmodel->animscenes[i].firstframe = i;
368 loadmodel->animscenes[i].framecount = 1;
369 loadmodel->animscenes[i].framerate = 10;
370 loadmodel->animscenes[i].loop = true;
372 pinframe = &pinqsprite->frames[i];
374 origin[0] = LittleLong (pinframe->origin_x);
375 origin[1] = LittleLong (pinframe->origin_y);
376 width = LittleLong (pinframe->width);
377 height = LittleLong (pinframe->height);
379 sprframe = &loadmodel->sprite.sprdata_frames[i];
381 sprframe->left = origin[0];
382 sprframe->right = origin[0] + width;
383 sprframe->up = origin[1];
384 sprframe->down = origin[1] - height;
386 x = max(sprframe->left * sprframe->left, sprframe->right * sprframe->right);
387 y = max(sprframe->up * sprframe->up, sprframe->down * sprframe->down);
388 if (modelradius < x + y)
391 if (width > 0 && height > 0)
393 sprframe->texture = loadtextureimagewithmask(loadmodel->texturepool, pinframe->name, 0, 0, false, (r_mipsprites.integer ? TEXF_MIPMAP : 0) | TEXF_ALPHA | TEXF_CLAMP | TEXF_PRECACHE | TEXF_PICMIP);
394 sprframe->fogtexture = image_masktex;
396 // TODO: use a default texture if we can't load it?
397 if (sprframe->texture == NULL)
398 Host_Error("Mod_IDS2_Load: failed to load %s", pinframe->name);
402 modelradius = sqrt(modelradius);
403 for (i = 0;i < 3;i++)
405 loadmodel->normalmins[i] = loadmodel->yawmins[i] = loadmodel->rotatedmins[i] = -modelradius;
406 loadmodel->normalmaxs[i] = loadmodel->yawmaxs[i] = loadmodel->rotatedmaxs[i] = modelradius;
408 loadmodel->radius = modelradius;
409 loadmodel->radius2 = modelradius * modelradius;