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 extern cvar_t gl_texturecompression_sprites;
70 static void Mod_Sprite_SharedSetup(const unsigned char *datapointer, int version, const unsigned int *palette, qboolean additive)
72 int i, j, groupframes, realframes, x, y, origin[2], width, height;
74 dspriteframetype_t *pinframetype;
75 dspriteframe_t *pinframe;
76 dspritegroup_t *pingroup;
77 dspriteinterval_t *pinintervals;
78 skinframe_t *skinframe;
79 float modelradius, interval;
80 char name[MAX_QPATH], fogname[MAX_QPATH];
81 const void *startframes;
82 int texflags = (r_mipsprites.integer ? TEXF_MIPMAP : 0) | (gl_texturecompression_sprites.integer ? TEXF_COMPRESS : 0) | TEXF_ISSPRITE | TEXF_PICMIP | TEXF_ALPHA | TEXF_CLAMP;
85 if (loadmodel->numframes < 1)
86 Host_Error ("Mod_Sprite_SharedSetup: Invalid # of frames: %d", loadmodel->numframes);
88 // LordHavoc: hack to allow sprites to be non-fullbright
90 for (i = 0;i < MAX_QPATH && loadmodel->name[i];i++)
91 if (loadmodel->name[i] == '!')
97 startframes = datapointer;
99 for (i = 0;i < loadmodel->numframes;i++)
101 pinframetype = (dspriteframetype_t *)datapointer;
102 datapointer += sizeof(dspriteframetype_t);
104 if (LittleLong (pinframetype->type) == SPR_SINGLE)
108 pingroup = (dspritegroup_t *)datapointer;
109 datapointer += sizeof(dspritegroup_t);
111 groupframes = LittleLong(pingroup->numframes);
113 datapointer += sizeof(dspriteinterval_t) * groupframes;
116 for (j = 0;j < groupframes;j++)
118 pinframe = (dspriteframe_t *)datapointer;
119 if (version == SPRITE32_VERSION)
120 datapointer += sizeof(dspriteframe_t) + LittleLong(pinframe->width) * LittleLong(pinframe->height) * 4;
121 else //if (version == SPRITE_VERSION || version == SPRITEHL_VERSION)
122 datapointer += sizeof(dspriteframe_t) + LittleLong(pinframe->width) * LittleLong(pinframe->height);
124 realframes += groupframes;
127 loadmodel->animscenes = (animscene_t *)Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
128 loadmodel->sprite.sprdata_frames = (mspriteframe_t *)Mem_Alloc(loadmodel->mempool, sizeof(mspriteframe_t) * realframes);
129 loadmodel->num_textures = realframes;
130 loadmodel->num_texturesperskin = 1;
131 loadmodel->data_textures = (texture_t *)Mem_Alloc(loadmodel->mempool, sizeof(texture_t) * loadmodel->num_textures);
133 datapointer = (unsigned char *)startframes;
135 for (i = 0;i < loadmodel->numframes;i++)
137 pinframetype = (dspriteframetype_t *)datapointer;
138 datapointer += sizeof(dspriteframetype_t);
140 if (LittleLong (pinframetype->type) == SPR_SINGLE)
147 pingroup = (dspritegroup_t *)datapointer;
148 datapointer += sizeof(dspritegroup_t);
150 groupframes = LittleLong(pingroup->numframes);
152 pinintervals = (dspriteinterval_t *)datapointer;
153 datapointer += sizeof(dspriteinterval_t) * groupframes;
155 interval = LittleFloat(pinintervals[0].interval);
156 if (interval < 0.01f)
157 Host_Error("Mod_Sprite_SharedSetup: invalid interval");
160 dpsnprintf(loadmodel->animscenes[i].name, sizeof(loadmodel->animscenes[i].name), "frame %i", i);
161 loadmodel->animscenes[i].firstframe = realframes;
162 loadmodel->animscenes[i].framecount = groupframes;
163 loadmodel->animscenes[i].framerate = 1.0f / interval;
164 loadmodel->animscenes[i].loop = true;
166 for (j = 0;j < groupframes;j++)
168 pinframe = (dspriteframe_t *)datapointer;
169 datapointer += sizeof(dspriteframe_t);
171 origin[0] = LittleLong (pinframe->origin[0]);
172 origin[1] = LittleLong (pinframe->origin[1]);
173 width = LittleLong (pinframe->width);
174 height = LittleLong (pinframe->height);
176 loadmodel->sprite.sprdata_frames[realframes].left = origin[0];
177 loadmodel->sprite.sprdata_frames[realframes].right = origin[0] + width;
178 loadmodel->sprite.sprdata_frames[realframes].up = origin[1];
179 loadmodel->sprite.sprdata_frames[realframes].down = origin[1] - height;
181 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);
182 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);
183 if (modelradius < x + y)
186 if (cls.state != ca_dedicated)
189 // note: Nehahra's null.spr has width == 0 and height == 0
190 if (width > 0 && height > 0)
194 dpsnprintf (name, sizeof(name), "%s_%i_%i", loadmodel->name, i, j);
195 dpsnprintf (fogname, sizeof(fogname), "%s_%i_%ifog", loadmodel->name, i, j);
199 dpsnprintf (name, sizeof(name), "%s_%i", loadmodel->name, i);
200 dpsnprintf (fogname, sizeof(fogname), "%s_%ifog", loadmodel->name, i);
202 if (!(skinframe = R_SkinFrame_LoadExternal(name, texflags | TEXF_COMPRESS, false)))
204 unsigned char *pixels = (unsigned char *) Mem_Alloc(loadmodel->mempool, width*height*4);
205 if (version == SPRITE32_VERSION)
207 for (x = 0;x < width*height;x++)
209 pixels[x*4+2] = datapointer[x*4+0];
210 pixels[x*4+1] = datapointer[x*4+1];
211 pixels[x*4+0] = datapointer[x*4+2];
212 pixels[x*4+3] = datapointer[x*4+3];
215 else //if (version == SPRITEHL_VERSION || version == SPRITE_VERSION)
216 Image_Copy8bitBGRA(datapointer, pixels, width*height, palette ? palette : palette_bgra_transparent);
217 skinframe = R_SkinFrame_LoadInternalBGRA(name, texflags, pixels, width, height, false);
218 // texflags |= TEXF_COMPRESS;
222 if (skinframe == NULL)
223 skinframe = R_SkinFrame_LoadMissing();
224 Mod_SpriteSetupTexture(&loadmodel->data_textures[realframes], skinframe, fullbright, additive);
227 if (version == SPRITE32_VERSION)
228 datapointer += width * height * 4;
229 else //if (version == SPRITE_VERSION || version == SPRITEHL_VERSION)
230 datapointer += width * height;
235 modelradius = sqrt(modelradius);
236 for (i = 0;i < 3;i++)
238 loadmodel->normalmins[i] = loadmodel->yawmins[i] = loadmodel->rotatedmins[i] = -modelradius;
239 loadmodel->normalmaxs[i] = loadmodel->yawmaxs[i] = loadmodel->rotatedmaxs[i] = modelradius;
241 loadmodel->radius = modelradius;
242 loadmodel->radius2 = modelradius * modelradius;
245 extern void R_Model_Sprite_Draw(entity_render_t *ent);
246 void Mod_IDSP_Load(dp_model_t *mod, void *buffer, void *bufferend)
249 const unsigned char *datapointer;
251 datapointer = (unsigned char *)buffer;
253 loadmodel->modeldatatypestring = "SPR1";
255 loadmodel->type = mod_sprite;
257 loadmodel->DrawSky = NULL;
258 loadmodel->Draw = R_Model_Sprite_Draw;
259 loadmodel->DrawDepth = NULL;
260 loadmodel->CompileShadowVolume = NULL;
261 loadmodel->DrawShadowVolume = NULL;
262 loadmodel->DrawLight = NULL;
263 loadmodel->DrawAddWaterPlanes = NULL;
265 version = LittleLong(((dsprite_t *)buffer)->version);
266 if (version == SPRITE_VERSION || version == SPRITE32_VERSION)
268 dsprite_t *pinqsprite;
270 pinqsprite = (dsprite_t *)datapointer;
271 datapointer += sizeof(dsprite_t);
273 loadmodel->numframes = LittleLong (pinqsprite->numframes);
274 loadmodel->sprite.sprnum_type = LittleLong (pinqsprite->type);
275 loadmodel->synctype = (synctype_t)LittleLong (pinqsprite->synctype);
277 Mod_Sprite_SharedSetup(datapointer, LittleLong (pinqsprite->version), NULL, false);
279 else if (version == SPRITEHL_VERSION)
282 unsigned char palette[256][4];
283 const unsigned char *in;
284 dspritehl_t *pinhlsprite;
286 pinhlsprite = (dspritehl_t *)datapointer;
287 datapointer += sizeof(dspritehl_t);
289 loadmodel->numframes = LittleLong (pinhlsprite->numframes);
290 loadmodel->sprite.sprnum_type = LittleLong (pinhlsprite->type);
291 loadmodel->synctype = (synctype_t)LittleLong (pinhlsprite->synctype);
292 rendermode = pinhlsprite->rendermode;
296 i = in[0] + in[1] * 256;
298 Host_Error ("Mod_IDSP_Load: unexpected number of palette colors %i (should be 256)", i);
304 for (i = 0;i < 256;i++)
306 palette[i][2] = in[i*3+0];
307 palette[i][1] = in[i*3+1];
308 palette[i][0] = in[i*3+2];
313 for (i = 0;i < 256;i++)
315 palette[i][2] = in[i*3+0];
316 palette[i][1] = in[i*3+1];
317 palette[i][0] = in[i*3+2];
320 // also passes additive == true to Mod_Sprite_SharedSetup
322 case SPRHL_INDEXALPHA:
323 for (i = 0;i < 256;i++)
325 palette[i][2] = in[765];
326 palette[i][1] = in[766];
327 palette[i][0] = in[767];
332 case SPRHL_ALPHATEST:
333 for (i = 0;i < 256;i++)
335 palette[i][2] = in[i*3+0];
336 palette[i][1] = in[i*3+1];
337 palette[i][0] = in[i*3+2];
340 palette[255][0] = palette[255][1] = palette[255][2] = palette[255][3] = 0;
341 // should this use alpha test or alpha blend? (currently blend)
344 Host_Error("Mod_IDSP_Load: unknown texFormat (%i, should be 0, 1, 2, or 3)", i);
348 Mod_Sprite_SharedSetup(datapointer, LittleLong (pinhlsprite->version), (unsigned int *)(&palette[0][0]), rendermode == SPRHL_ADDITIVE);
351 Host_Error("Mod_IDSP_Load: %s has wrong version number (%i). Only %i (quake), %i (HalfLife), and %i (sprite32) supported",
352 loadmodel->name, version, SPRITE_VERSION, SPRITEHL_VERSION, SPRITE32_VERSION);
354 loadmodel->surfmesh.isanimated = loadmodel->numframes > 1 || loadmodel->animscenes[0].framecount > 1;
358 void Mod_IDS2_Load(dp_model_t *mod, void *buffer, void *bufferend)
362 const dsprite2_t *pinqsprite;
363 skinframe_t *skinframe;
365 int texflags = (r_mipsprites.integer ? TEXF_MIPMAP : 0) | TEXF_ISSPRITE | TEXF_PICMIP | TEXF_COMPRESS | TEXF_ALPHA | TEXF_CLAMP;
367 loadmodel->modeldatatypestring = "SPR2";
369 loadmodel->type = mod_sprite;
371 loadmodel->DrawSky = NULL;
372 loadmodel->Draw = R_Model_Sprite_Draw;
373 loadmodel->DrawDepth = NULL;
374 loadmodel->CompileShadowVolume = NULL;
375 loadmodel->DrawShadowVolume = NULL;
376 loadmodel->DrawLight = NULL;
377 loadmodel->DrawAddWaterPlanes = NULL;
379 pinqsprite = (dsprite2_t *)buffer;
381 version = LittleLong(pinqsprite->version);
382 if (version != SPRITE2_VERSION)
383 Host_Error("Mod_IDS2_Load: %s has wrong version number (%i should be 2 (quake 2)", loadmodel->name, version);
385 loadmodel->numframes = LittleLong (pinqsprite->numframes);
386 if (loadmodel->numframes < 1)
387 Host_Error ("Mod_IDS2_Load: Invalid # of frames: %d", loadmodel->numframes);
388 loadmodel->sprite.sprnum_type = SPR_VP_PARALLEL;
389 loadmodel->synctype = ST_SYNC;
391 // LordHavoc: hack to allow sprites to be non-fullbright
393 for (i = 0;i < MAX_QPATH && loadmodel->name[i];i++)
394 if (loadmodel->name[i] == '!')
397 loadmodel->animscenes = (animscene_t *)Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
398 loadmodel->sprite.sprdata_frames = (mspriteframe_t *)Mem_Alloc(loadmodel->mempool, sizeof(mspriteframe_t) * loadmodel->numframes);
399 loadmodel->num_textures = loadmodel->numframes;
400 loadmodel->num_texturesperskin = 1;
401 loadmodel->data_textures = (texture_t *)Mem_Alloc(loadmodel->mempool, sizeof(texture_t) * loadmodel->num_textures);
404 for (i = 0;i < loadmodel->numframes;i++)
406 int origin[2], x, y, width, height;
407 const dsprite2frame_t *pinframe;
408 mspriteframe_t *sprframe;
410 dpsnprintf(loadmodel->animscenes[i].name, sizeof(loadmodel->animscenes[i].name), "frame %i", i);
411 loadmodel->animscenes[i].firstframe = i;
412 loadmodel->animscenes[i].framecount = 1;
413 loadmodel->animscenes[i].framerate = 10;
414 loadmodel->animscenes[i].loop = true;
416 pinframe = &pinqsprite->frames[i];
418 origin[0] = LittleLong (pinframe->origin_x);
419 origin[1] = LittleLong (pinframe->origin_y);
420 width = LittleLong (pinframe->width);
421 height = LittleLong (pinframe->height);
423 sprframe = &loadmodel->sprite.sprdata_frames[i];
425 // note that sp2 origin[0] is positive, where as it is negative in
427 sprframe->left = -origin[0];
428 sprframe->right = -origin[0] + width;
429 sprframe->up = origin[1];
430 sprframe->down = origin[1] - height;
432 x = (int)max(sprframe->left * sprframe->left, sprframe->right * sprframe->right);
433 y = (int)max(sprframe->up * sprframe->up, sprframe->down * sprframe->down);
434 if (modelradius < x + y)
438 if (cls.state != ca_dedicated)
440 for (i = 0;i < loadmodel->numframes;i++)
442 const dsprite2frame_t *pinframe;
443 pinframe = &pinqsprite->frames[i];
444 if (!(skinframe = R_SkinFrame_LoadExternal(pinframe->name, texflags, false)))
446 Con_Printf("Mod_IDS2_Load: failed to load %s", pinframe->name);
447 skinframe = R_SkinFrame_LoadMissing();
449 Mod_SpriteSetupTexture(&loadmodel->data_textures[i], skinframe, fullbright, false);
453 modelradius = sqrt(modelradius);
454 for (i = 0;i < 3;i++)
456 loadmodel->normalmins[i] = loadmodel->yawmins[i] = loadmodel->rotatedmins[i] = -modelradius;
457 loadmodel->normalmaxs[i] = loadmodel->yawmaxs[i] = loadmodel->rotatedmaxs[i] = modelradius;
459 loadmodel->radius = modelradius;
460 loadmodel->radius2 = modelradius * modelradius;
462 loadmodel->surfmesh.isanimated = loadmodel->numframes > 1 || loadmodel->animscenes[0].framecount > 1;