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", "mipmaps sprites so they render faster in the distance and do not display noise artifacts"};
29 cvar_t r_labelsprites_scale = {CVAR_SAVE, "r_labelsprites_scale", "1", "global scale to apply to label sprites before conversion to HUD coordinates"};
30 cvar_t r_labelsprites_roundtopixels = {CVAR_SAVE, "r_labelsprites_roundtopixels", "1", "try to make label sprites sharper by rounding their size to 0.5x or 1x and by rounding their position to whole pixels if possible"};
37 void Mod_SpriteInit (void)
39 Cvar_RegisterVariable(&r_mipsprites);
40 Cvar_RegisterVariable(&r_labelsprites_scale);
41 Cvar_RegisterVariable(&r_labelsprites_roundtopixels);
44 static void Mod_SpriteSetupTexture(texture_t *texture, skinframe_t *skinframe, qboolean fullbright, qboolean additive)
47 skinframe = R_SkinFrame_LoadMissing();
48 texture->offsetmapping = OFFSETMAPPING_OFF;
49 texture->offsetscale = 1;
50 texture->specularscalemod = 1;
51 texture->specularpowermod = 1;
52 texture->basematerialflags = MATERIALFLAG_WALL;
54 texture->basematerialflags |= MATERIALFLAG_FULLBRIGHT;
56 texture->basematerialflags |= MATERIALFLAG_ADD | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW;
57 else if (skinframe->hasalpha)
58 texture->basematerialflags |= MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW;
59 texture->currentmaterialflags = texture->basematerialflags;
60 texture->numskinframes = 1;
61 texture->currentskinframe = texture->skinframes[0] = skinframe;
62 texture->surfaceflags = 0;
63 texture->supercontents = SUPERCONTENTS_SOLID;
64 if (!(texture->basematerialflags & MATERIALFLAG_BLENDED))
65 texture->supercontents |= SUPERCONTENTS_OPAQUE;
68 static void Mod_Sprite_SharedSetup(const unsigned char *datapointer, int version, const unsigned int *palette, qboolean additive)
70 int i, j, groupframes, realframes, x, y, origin[2], width, height;
72 dspriteframetype_t *pinframetype;
73 dspriteframe_t *pinframe;
74 dspritegroup_t *pingroup;
75 dspriteinterval_t *pinintervals;
76 skinframe_t *skinframe;
77 float modelradius, interval;
78 char name[MAX_QPATH], fogname[MAX_QPATH];
79 const void *startframes;
80 int texflags = (r_mipsprites.integer ? TEXF_MIPMAP : 0) | TEXF_ISSPRITE | TEXF_PICMIP | TEXF_ALPHA | TEXF_CLAMP;
83 if (loadmodel->numframes < 1)
84 Host_Error ("Mod_Sprite_SharedSetup: Invalid # of frames: %d", loadmodel->numframes);
86 // LordHavoc: hack to allow sprites to be non-fullbright
88 for (i = 0;i < MAX_QPATH && loadmodel->name[i];i++)
89 if (loadmodel->name[i] == '!')
95 startframes = datapointer;
97 for (i = 0;i < loadmodel->numframes;i++)
99 pinframetype = (dspriteframetype_t *)datapointer;
100 datapointer += sizeof(dspriteframetype_t);
102 if (LittleLong (pinframetype->type) == SPR_SINGLE)
106 pingroup = (dspritegroup_t *)datapointer;
107 datapointer += sizeof(dspritegroup_t);
109 groupframes = LittleLong(pingroup->numframes);
111 datapointer += sizeof(dspriteinterval_t) * groupframes;
114 for (j = 0;j < groupframes;j++)
116 pinframe = (dspriteframe_t *)datapointer;
117 if (version == SPRITE32_VERSION)
118 datapointer += sizeof(dspriteframe_t) + LittleLong(pinframe->width) * LittleLong(pinframe->height) * 4;
119 else //if (version == SPRITE_VERSION || version == SPRITEHL_VERSION)
120 datapointer += sizeof(dspriteframe_t) + LittleLong(pinframe->width) * LittleLong(pinframe->height);
122 realframes += groupframes;
125 loadmodel->animscenes = (animscene_t *)Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
126 loadmodel->sprite.sprdata_frames = (mspriteframe_t *)Mem_Alloc(loadmodel->mempool, sizeof(mspriteframe_t) * realframes);
127 loadmodel->num_textures = realframes;
128 loadmodel->num_texturesperskin = 1;
129 loadmodel->data_textures = (texture_t *)Mem_Alloc(loadmodel->mempool, sizeof(texture_t) * loadmodel->num_textures);
131 datapointer = (unsigned char *)startframes;
133 for (i = 0;i < loadmodel->numframes;i++)
135 pinframetype = (dspriteframetype_t *)datapointer;
136 datapointer += sizeof(dspriteframetype_t);
138 if (LittleLong (pinframetype->type) == SPR_SINGLE)
145 pingroup = (dspritegroup_t *)datapointer;
146 datapointer += sizeof(dspritegroup_t);
148 groupframes = LittleLong(pingroup->numframes);
150 pinintervals = (dspriteinterval_t *)datapointer;
151 datapointer += sizeof(dspriteinterval_t) * groupframes;
153 interval = LittleFloat(pinintervals[0].interval);
154 if (interval < 0.01f)
155 Host_Error("Mod_Sprite_SharedSetup: invalid interval");
158 dpsnprintf(loadmodel->animscenes[i].name, sizeof(loadmodel->animscenes[i].name), "frame %i", i);
159 loadmodel->animscenes[i].firstframe = realframes;
160 loadmodel->animscenes[i].framecount = groupframes;
161 loadmodel->animscenes[i].framerate = 1.0f / interval;
162 loadmodel->animscenes[i].loop = true;
164 for (j = 0;j < groupframes;j++)
166 pinframe = (dspriteframe_t *)datapointer;
167 datapointer += sizeof(dspriteframe_t);
169 origin[0] = LittleLong (pinframe->origin[0]);
170 origin[1] = LittleLong (pinframe->origin[1]);
171 width = LittleLong (pinframe->width);
172 height = LittleLong (pinframe->height);
174 loadmodel->sprite.sprdata_frames[realframes].left = origin[0];
175 loadmodel->sprite.sprdata_frames[realframes].right = origin[0] + width;
176 loadmodel->sprite.sprdata_frames[realframes].up = origin[1];
177 loadmodel->sprite.sprdata_frames[realframes].down = origin[1] - height;
179 x = (int)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);
180 y = (int)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);
181 if (modelradius < x + y)
184 if (cls.state != ca_dedicated)
187 // note: Nehahra's null.spr has width == 0 and height == 0
188 if (width > 0 && height > 0)
192 dpsnprintf (name, sizeof(name), "%s_%i_%i", loadmodel->name, i, j);
193 dpsnprintf (fogname, sizeof(fogname), "%s_%i_%ifog", loadmodel->name, i, j);
197 dpsnprintf (name, sizeof(name), "%s_%i", loadmodel->name, i);
198 dpsnprintf (fogname, sizeof(fogname), "%s_%ifog", loadmodel->name, i);
200 if (!(skinframe = R_SkinFrame_LoadExternal(name, texflags | TEXF_COMPRESS, false)))
202 unsigned char *pixels = (unsigned char *) Mem_Alloc(loadmodel->mempool, width*height*4);
203 if (version == SPRITE32_VERSION)
205 for (x = 0;x < width*height;x++)
207 pixels[x*4+2] = datapointer[x*4+0];
208 pixels[x*4+1] = datapointer[x*4+1];
209 pixels[x*4+0] = datapointer[x*4+2];
210 pixels[x*4+3] = datapointer[x*4+3];
213 else //if (version == SPRITEHL_VERSION || version == SPRITE_VERSION)
214 Image_Copy8bitBGRA(datapointer, pixels, width*height, palette ? palette : palette_bgra_transparent);
215 skinframe = R_SkinFrame_LoadInternalBGRA(name, texflags, pixels, width, height);
219 if (skinframe == NULL)
220 skinframe = R_SkinFrame_LoadMissing();
221 Mod_SpriteSetupTexture(&loadmodel->data_textures[realframes], skinframe, fullbright, additive);
224 if (version == SPRITE32_VERSION)
225 datapointer += width * height * 4;
226 else //if (version == SPRITE_VERSION || version == SPRITEHL_VERSION)
227 datapointer += width * height;
232 modelradius = sqrt(modelradius);
233 for (i = 0;i < 3;i++)
235 loadmodel->normalmins[i] = loadmodel->yawmins[i] = loadmodel->rotatedmins[i] = -modelradius;
236 loadmodel->normalmaxs[i] = loadmodel->yawmaxs[i] = loadmodel->rotatedmaxs[i] = modelradius;
238 loadmodel->radius = modelradius;
239 loadmodel->radius2 = modelradius * modelradius;
242 extern void R_Model_Sprite_Draw(entity_render_t *ent);
243 void Mod_IDSP_Load(dp_model_t *mod, void *buffer, void *bufferend)
246 const unsigned char *datapointer;
248 datapointer = (unsigned char *)buffer;
250 loadmodel->modeldatatypestring = "SPR1";
252 loadmodel->type = mod_sprite;
254 loadmodel->DrawSky = NULL;
255 loadmodel->Draw = R_Model_Sprite_Draw;
256 loadmodel->DrawDepth = NULL;
257 loadmodel->CompileShadowVolume = NULL;
258 loadmodel->DrawShadowVolume = NULL;
259 loadmodel->DrawLight = NULL;
260 loadmodel->DrawAddWaterPlanes = NULL;
262 version = LittleLong(((dsprite_t *)buffer)->version);
263 if (version == SPRITE_VERSION || version == SPRITE32_VERSION)
265 dsprite_t *pinqsprite;
267 pinqsprite = (dsprite_t *)datapointer;
268 datapointer += sizeof(dsprite_t);
270 loadmodel->numframes = LittleLong (pinqsprite->numframes);
271 loadmodel->sprite.sprnum_type = LittleLong (pinqsprite->type);
272 loadmodel->synctype = (synctype_t)LittleLong (pinqsprite->synctype);
274 Mod_Sprite_SharedSetup(datapointer, LittleLong (pinqsprite->version), NULL, false);
276 else if (version == SPRITEHL_VERSION)
279 unsigned char palette[256][4];
280 const unsigned char *in;
281 dspritehl_t *pinhlsprite;
283 pinhlsprite = (dspritehl_t *)datapointer;
284 datapointer += sizeof(dspritehl_t);
286 loadmodel->numframes = LittleLong (pinhlsprite->numframes);
287 loadmodel->sprite.sprnum_type = LittleLong (pinhlsprite->type);
288 loadmodel->synctype = (synctype_t)LittleLong (pinhlsprite->synctype);
289 rendermode = pinhlsprite->rendermode;
293 i = in[0] + in[1] * 256;
295 Host_Error ("Mod_IDSP_Load: unexpected number of palette colors %i (should be 256)", i);
301 for (i = 0;i < 256;i++)
303 palette[i][2] = in[i*3+0];
304 palette[i][1] = in[i*3+1];
305 palette[i][0] = in[i*3+2];
310 for (i = 0;i < 256;i++)
312 palette[i][2] = in[i*3+0];
313 palette[i][1] = in[i*3+1];
314 palette[i][0] = in[i*3+2];
317 // also passes additive == true to Mod_Sprite_SharedSetup
319 case SPRHL_INDEXALPHA:
320 for (i = 0;i < 256;i++)
322 palette[i][2] = in[765];
323 palette[i][1] = in[766];
324 palette[i][0] = in[767];
329 case SPRHL_ALPHATEST:
330 for (i = 0;i < 256;i++)
332 palette[i][2] = in[i*3+0];
333 palette[i][1] = in[i*3+1];
334 palette[i][0] = in[i*3+2];
337 palette[255][0] = palette[255][1] = palette[255][2] = palette[255][3] = 0;
338 // should this use alpha test or alpha blend? (currently blend)
341 Host_Error("Mod_IDSP_Load: unknown texFormat (%i, should be 0, 1, 2, or 3)", i);
345 Mod_Sprite_SharedSetup(datapointer, LittleLong (pinhlsprite->version), (unsigned int *)(&palette[0][0]), rendermode == SPRHL_ADDITIVE);
348 Host_Error("Mod_IDSP_Load: %s has wrong version number (%i). Only %i (quake), %i (HalfLife), and %i (sprite32) supported",
349 loadmodel->name, version, SPRITE_VERSION, SPRITEHL_VERSION, SPRITE32_VERSION);
351 loadmodel->surfmesh.isanimated = loadmodel->numframes > 1 || loadmodel->animscenes[0].framecount > 1;
355 void Mod_IDS2_Load(dp_model_t *mod, void *buffer, void *bufferend)
359 const dsprite2_t *pinqsprite;
360 skinframe_t *skinframe;
362 int texflags = (r_mipsprites.integer ? TEXF_MIPMAP : 0) | TEXF_ISSPRITE | TEXF_PICMIP | TEXF_COMPRESS | TEXF_ALPHA | TEXF_CLAMP;
364 loadmodel->modeldatatypestring = "SPR2";
366 loadmodel->type = mod_sprite;
368 loadmodel->DrawSky = NULL;
369 loadmodel->Draw = R_Model_Sprite_Draw;
370 loadmodel->DrawDepth = NULL;
371 loadmodel->CompileShadowVolume = NULL;
372 loadmodel->DrawShadowVolume = NULL;
373 loadmodel->DrawLight = NULL;
374 loadmodel->DrawAddWaterPlanes = NULL;
376 pinqsprite = (dsprite2_t *)buffer;
378 version = LittleLong(pinqsprite->version);
379 if (version != SPRITE2_VERSION)
380 Host_Error("Mod_IDS2_Load: %s has wrong version number (%i should be 2 (quake 2)", loadmodel->name, version);
382 loadmodel->numframes = LittleLong (pinqsprite->numframes);
383 if (loadmodel->numframes < 1)
384 Host_Error ("Mod_IDS2_Load: Invalid # of frames: %d", loadmodel->numframes);
385 loadmodel->sprite.sprnum_type = SPR_VP_PARALLEL;
386 loadmodel->synctype = ST_SYNC;
388 // LordHavoc: hack to allow sprites to be non-fullbright
390 for (i = 0;i < MAX_QPATH && loadmodel->name[i];i++)
391 if (loadmodel->name[i] == '!')
394 loadmodel->animscenes = (animscene_t *)Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
395 loadmodel->sprite.sprdata_frames = (mspriteframe_t *)Mem_Alloc(loadmodel->mempool, sizeof(mspriteframe_t) * loadmodel->numframes);
396 loadmodel->num_textures = loadmodel->numframes;
397 loadmodel->num_texturesperskin = 1;
398 loadmodel->data_textures = (texture_t *)Mem_Alloc(loadmodel->mempool, sizeof(texture_t) * loadmodel->num_textures);
401 for (i = 0;i < loadmodel->numframes;i++)
403 int origin[2], x, y, width, height;
404 const dsprite2frame_t *pinframe;
405 mspriteframe_t *sprframe;
407 dpsnprintf(loadmodel->animscenes[i].name, sizeof(loadmodel->animscenes[i].name), "frame %i", i);
408 loadmodel->animscenes[i].firstframe = i;
409 loadmodel->animscenes[i].framecount = 1;
410 loadmodel->animscenes[i].framerate = 10;
411 loadmodel->animscenes[i].loop = true;
413 pinframe = &pinqsprite->frames[i];
415 origin[0] = LittleLong (pinframe->origin_x);
416 origin[1] = LittleLong (pinframe->origin_y);
417 width = LittleLong (pinframe->width);
418 height = LittleLong (pinframe->height);
420 sprframe = &loadmodel->sprite.sprdata_frames[i];
422 // note that sp2 origin[0] is positive, where as it is negative in
424 sprframe->left = -origin[0];
425 sprframe->right = -origin[0] + width;
426 sprframe->up = origin[1];
427 sprframe->down = origin[1] - height;
429 x = (int)max(sprframe->left * sprframe->left, sprframe->right * sprframe->right);
430 y = (int)max(sprframe->up * sprframe->up, sprframe->down * sprframe->down);
431 if (modelradius < x + y)
435 if (cls.state != ca_dedicated)
437 for (i = 0;i < loadmodel->numframes;i++)
439 const dsprite2frame_t *pinframe;
440 pinframe = &pinqsprite->frames[i];
441 if (!(skinframe = R_SkinFrame_LoadExternal(pinframe->name, texflags, false)))
443 Con_Printf("Mod_IDS2_Load: failed to load %s", pinframe->name);
444 skinframe = R_SkinFrame_LoadMissing();
446 Mod_SpriteSetupTexture(&loadmodel->data_textures[i], skinframe, fullbright, false);
450 modelradius = sqrt(modelradius);
451 for (i = 0;i < 3;i++)
453 loadmodel->normalmins[i] = loadmodel->yawmins[i] = loadmodel->rotatedmins[i] = -modelradius;
454 loadmodel->normalmaxs[i] = loadmodel->yawmaxs[i] = loadmodel->rotatedmaxs[i] = modelradius;
456 loadmodel->radius = modelradius;
457 loadmodel->radius2 = modelradius * modelradius;
459 loadmodel->surfmesh.isanimated = loadmodel->numframes > 1 || loadmodel->animscenes[0].framecount > 1;