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 = {CF_CLIENT | CF_ARCHIVE, "r_mipsprites", "1", "mipmaps sprites so they render faster in the distance and do not display noise artifacts"};
29 cvar_t r_labelsprites_scale = {CF_CLIENT | CF_ARCHIVE, "r_labelsprites_scale", "1", "global scale to apply to label sprites before conversion to HUD coordinates"};
30 cvar_t r_labelsprites_roundtopixels = {CF_CLIENT | CF_ARCHIVE, "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"};
31 cvar_t r_overheadsprites_perspective = {CF_CLIENT | CF_ARCHIVE, "r_overheadsprites_perspective", "5", "fake perspective effect for SPR_OVERHEAD sprites"};
32 cvar_t r_overheadsprites_pushback = {CF_CLIENT | CF_ARCHIVE, "r_overheadsprites_pushback", "15", "how far to pull the SPR_OVERHEAD sprites toward the eye (used to avoid intersections with 3D models)"};
33 cvar_t r_overheadsprites_scalex = {CF_CLIENT | CF_ARCHIVE, "r_overheadsprites_scalex", "1", "additional scale for overhead sprites for x axis"};
34 cvar_t r_overheadsprites_scaley = {CF_CLIENT | CF_ARCHIVE, "r_overheadsprites_scaley", "1", "additional scale for overhead sprites for y axis"};
35 cvar_t r_track_sprites = {CF_CLIENT | CF_ARCHIVE, "r_track_sprites", "1", "track SPR_LABEL* sprites by putting them as indicator at the screen border to rotate to"};
36 cvar_t r_track_sprites_flags = {CF_CLIENT | CF_ARCHIVE, "r_track_sprites_flags", "1", "1: Rotate sprites accordingly, 2: Make it a continuous rotation"};
37 cvar_t r_track_sprites_scalew = {CF_CLIENT | CF_ARCHIVE, "r_track_sprites_scalew", "1", "width scaling of tracked sprites"};
38 cvar_t r_track_sprites_scaleh = {CF_CLIENT | CF_ARCHIVE, "r_track_sprites_scaleh", "1", "height scaling of tracked sprites"};
45 void Mod_SpriteInit (void)
47 Cvar_RegisterVariable(&r_mipsprites);
48 Cvar_RegisterVariable(&r_labelsprites_scale);
49 Cvar_RegisterVariable(&r_labelsprites_roundtopixels);
50 Cvar_RegisterVariable(&r_overheadsprites_perspective);
51 Cvar_RegisterVariable(&r_overheadsprites_pushback);
52 Cvar_RegisterVariable(&r_overheadsprites_scalex);
53 Cvar_RegisterVariable(&r_overheadsprites_scaley);
54 Cvar_RegisterVariable(&r_track_sprites);
55 Cvar_RegisterVariable(&r_track_sprites_flags);
56 Cvar_RegisterVariable(&r_track_sprites_scalew);
57 Cvar_RegisterVariable(&r_track_sprites_scaleh);
60 static void Mod_SpriteSetupTexture(texture_t *texture, skinframe_t *skinframe, qbool fullbright, qbool additive)
63 skinframe = R_SkinFrame_LoadMissing();
64 texture->offsetmapping = OFFSETMAPPING_OFF;
65 texture->offsetscale = 1;
66 texture->offsetbias = 0;
67 texture->specularscalemod = 1;
68 texture->specularpowermod = 1;
69 texture->basematerialflags = MATERIALFLAG_WALL;
70 texture->basealpha = 1.0f;
72 texture->basematerialflags |= MATERIALFLAG_FULLBRIGHT;
74 texture->basematerialflags |= MATERIALFLAG_ADD | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW;
75 else if (skinframe->hasalpha)
76 texture->basematerialflags |= MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW;
77 texture->currentmaterialflags = texture->basematerialflags;
78 texture->materialshaderpass = texture->shaderpasses[0] = Mod_CreateShaderPass(loadmodel->mempool, skinframe);
79 texture->currentskinframe = skinframe;
80 texture->surfaceflags = 0;
81 texture->supercontents = SUPERCONTENTS_SOLID;
82 if (!(texture->basematerialflags & MATERIALFLAG_BLENDED))
83 texture->supercontents |= SUPERCONTENTS_OPAQUE;
84 texture->transparentsort = TRANSPARENTSORT_DISTANCE;
85 // WHEN ADDING DEFAULTS HERE, REMEMBER TO PUT DEFAULTS IN ALL LOADERS
86 // JUST GREP FOR "specularscalemod = 1".
89 extern cvar_t gl_texturecompression_sprites;
91 static void Mod_Sprite_SharedSetup(const unsigned char *datapointer, int version, const unsigned int *palette, qbool additive)
93 int i, j, groupframes, realframes, x, y, origin[2], width, height;
95 dspriteframetype_t *pinframetype;
96 dspriteframe_t *pinframe;
97 dspritegroup_t *pingroup;
98 dspriteinterval_t *pinintervals;
99 skinframe_t *skinframe;
100 float modelradius, interval;
101 char name[MAX_QPATH], fogname[MAX_QPATH];
102 const void *startframes;
103 int texflags = (r_mipsprites.integer ? TEXF_MIPMAP : 0) | ((gl_texturecompression.integer && gl_texturecompression_sprites.integer) ? TEXF_COMPRESS : 0) | TEXF_ISSPRITE | TEXF_PICMIP | TEXF_ALPHA | TEXF_CLAMP;
106 if (loadmodel->numframes < 1)
107 Host_Error ("Mod_Sprite_SharedSetup: Invalid # of frames: %d", loadmodel->numframes);
109 // LadyHavoc: hack to allow sprites to be non-fullbright
111 for (i = 0;i < MAX_QPATH && loadmodel->name[i];i++)
112 if (loadmodel->name[i] == '!')
118 startframes = datapointer;
120 for (i = 0;i < loadmodel->numframes;i++)
122 pinframetype = (dspriteframetype_t *)datapointer;
123 datapointer += sizeof(dspriteframetype_t);
125 if (LittleLong (pinframetype->type) == SPR_SINGLE)
129 pingroup = (dspritegroup_t *)datapointer;
130 datapointer += sizeof(dspritegroup_t);
132 groupframes = LittleLong(pingroup->numframes);
134 datapointer += sizeof(dspriteinterval_t) * groupframes;
137 for (j = 0;j < groupframes;j++)
139 pinframe = (dspriteframe_t *)datapointer;
140 if (version == SPRITE32_VERSION)
141 datapointer += sizeof(dspriteframe_t) + LittleLong(pinframe->width) * LittleLong(pinframe->height) * 4;
142 else //if (version == SPRITE_VERSION || version == SPRITEHL_VERSION)
143 datapointer += sizeof(dspriteframe_t) + LittleLong(pinframe->width) * LittleLong(pinframe->height);
145 realframes += groupframes;
148 loadmodel->animscenes = (animscene_t *)Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
149 loadmodel->sprite.sprdata_frames = (mspriteframe_t *)Mem_Alloc(loadmodel->mempool, sizeof(mspriteframe_t) * realframes);
150 loadmodel->num_textures = realframes;
151 loadmodel->num_texturesperskin = 1;
152 loadmodel->data_textures = (texture_t *)Mem_Alloc(loadmodel->mempool, sizeof(texture_t) * loadmodel->num_textures);
154 datapointer = (unsigned char *)startframes;
156 for (i = 0;i < loadmodel->numframes;i++)
158 pinframetype = (dspriteframetype_t *)datapointer;
159 datapointer += sizeof(dspriteframetype_t);
161 if (LittleLong (pinframetype->type) == SPR_SINGLE)
168 pingroup = (dspritegroup_t *)datapointer;
169 datapointer += sizeof(dspritegroup_t);
171 groupframes = LittleLong(pingroup->numframes);
173 pinintervals = (dspriteinterval_t *)datapointer;
174 datapointer += sizeof(dspriteinterval_t) * groupframes;
176 interval = LittleFloat(pinintervals[0].interval);
177 if (interval < 0.01f)
178 Host_Error("Mod_Sprite_SharedSetup: invalid interval");
181 dpsnprintf(loadmodel->animscenes[i].name, sizeof(loadmodel->animscenes[i].name), "frame %i", i);
182 loadmodel->animscenes[i].firstframe = realframes;
183 loadmodel->animscenes[i].framecount = groupframes;
184 loadmodel->animscenes[i].framerate = 1.0f / interval;
185 loadmodel->animscenes[i].loop = true;
187 for (j = 0;j < groupframes;j++)
189 pinframe = (dspriteframe_t *)datapointer;
190 datapointer += sizeof(dspriteframe_t);
192 origin[0] = LittleLong (pinframe->origin[0]);
193 origin[1] = LittleLong (pinframe->origin[1]);
194 width = LittleLong (pinframe->width);
195 height = LittleLong (pinframe->height);
197 loadmodel->sprite.sprdata_frames[realframes].left = origin[0];
198 loadmodel->sprite.sprdata_frames[realframes].right = origin[0] + width;
199 loadmodel->sprite.sprdata_frames[realframes].up = origin[1];
200 loadmodel->sprite.sprdata_frames[realframes].down = origin[1] - height;
202 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);
203 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);
204 if (modelradius < x + y)
207 if (cls.state != ca_dedicated)
210 // note: Nehahra's null.spr has width == 0 and height == 0
211 if (width > 0 && height > 0)
215 dpsnprintf (name, sizeof(name), "%s_%i_%i", loadmodel->name, i, j);
216 dpsnprintf (fogname, sizeof(fogname), "%s_%i_%ifog", loadmodel->name, i, j);
220 dpsnprintf (name, sizeof(name), "%s_%i", loadmodel->name, i);
221 dpsnprintf (fogname, sizeof(fogname), "%s_%ifog", loadmodel->name, i);
223 if (!(skinframe = R_SkinFrame_LoadExternal(name, texflags | TEXF_COMPRESS, false, false)))
225 unsigned char *pixels = (unsigned char *) Mem_Alloc(loadmodel->mempool, width*height*4);
226 if (version == SPRITE32_VERSION)
228 for (x = 0;x < width*height;x++)
230 pixels[x*4+2] = datapointer[x*4+0];
231 pixels[x*4+1] = datapointer[x*4+1];
232 pixels[x*4+0] = datapointer[x*4+2];
233 pixels[x*4+3] = datapointer[x*4+3];
236 else //if (version == SPRITEHL_VERSION || version == SPRITE_VERSION)
237 Image_Copy8bitBGRA(datapointer, pixels, width*height, palette ? palette : palette_bgra_transparent);
238 skinframe = R_SkinFrame_LoadInternalBGRA(name, texflags, pixels, width, height, 0, 0, 0, false);
239 // texflags |= TEXF_COMPRESS;
243 if (skinframe == NULL)
244 skinframe = R_SkinFrame_LoadMissing();
245 Mod_SpriteSetupTexture(&loadmodel->data_textures[realframes], skinframe, fullbright, additive);
248 if (version == SPRITE32_VERSION)
249 datapointer += width * height * 4;
250 else //if (version == SPRITE_VERSION || version == SPRITEHL_VERSION)
251 datapointer += width * height;
256 modelradius = sqrt(modelradius);
257 for (i = 0;i < 3;i++)
259 loadmodel->normalmins[i] = loadmodel->yawmins[i] = loadmodel->rotatedmins[i] = -modelradius;
260 loadmodel->normalmaxs[i] = loadmodel->yawmaxs[i] = loadmodel->rotatedmaxs[i] = modelradius;
262 loadmodel->radius = modelradius;
263 loadmodel->radius2 = modelradius * modelradius;
266 void Mod_IDSP_Load(model_t *mod, void *buffer, void *bufferend)
269 const unsigned char *datapointer;
271 datapointer = (unsigned char *)buffer;
273 loadmodel->modeldatatypestring = "SPR1";
275 loadmodel->type = mod_sprite;
277 loadmodel->DrawSky = NULL;
278 loadmodel->Draw = R_Model_Sprite_Draw;
279 loadmodel->DrawDepth = NULL;
280 loadmodel->DrawLight = NULL;
281 loadmodel->DrawAddWaterPlanes = NULL;
283 version = LittleLong(((dsprite_t *)buffer)->version);
284 if (version == SPRITE_VERSION || version == SPRITE32_VERSION)
286 dsprite_t *pinqsprite;
288 pinqsprite = (dsprite_t *)datapointer;
289 datapointer += sizeof(dsprite_t);
291 loadmodel->numframes = LittleLong (pinqsprite->numframes);
292 loadmodel->sprite.sprnum_type = LittleLong (pinqsprite->type);
293 loadmodel->synctype = (synctype_t)LittleLong (pinqsprite->synctype);
295 Mod_Sprite_SharedSetup(datapointer, LittleLong (pinqsprite->version), NULL, false);
297 else if (version == SPRITEHL_VERSION)
300 unsigned char palette[256][4];
301 const unsigned char *in;
302 dspritehl_t *pinhlsprite;
304 pinhlsprite = (dspritehl_t *)datapointer;
305 datapointer += sizeof(dspritehl_t);
307 loadmodel->numframes = LittleLong (pinhlsprite->numframes);
308 loadmodel->sprite.sprnum_type = LittleLong (pinhlsprite->type);
309 loadmodel->synctype = (synctype_t)LittleLong (pinhlsprite->synctype);
310 rendermode = pinhlsprite->rendermode;
314 i = in[0] + in[1] * 256;
316 Host_Error ("Mod_IDSP_Load: unexpected number of palette colors %i (should be 256)", i);
322 for (i = 0;i < 256;i++)
324 palette[i][2] = in[i*3+0];
325 palette[i][1] = in[i*3+1];
326 palette[i][0] = in[i*3+2];
331 for (i = 0;i < 256;i++)
333 palette[i][2] = in[i*3+0];
334 palette[i][1] = in[i*3+1];
335 palette[i][0] = in[i*3+2];
338 // also passes additive == true to Mod_Sprite_SharedSetup
340 case SPRHL_INDEXALPHA:
341 for (i = 0;i < 256;i++)
343 palette[i][2] = in[765];
344 palette[i][1] = in[766];
345 palette[i][0] = in[767];
350 case SPRHL_ALPHATEST:
351 for (i = 0;i < 256;i++)
353 palette[i][2] = in[i*3+0];
354 palette[i][1] = in[i*3+1];
355 palette[i][0] = in[i*3+2];
358 palette[255][0] = palette[255][1] = palette[255][2] = palette[255][3] = 0;
359 // should this use alpha test or alpha blend? (currently blend)
362 Host_Error("Mod_IDSP_Load: unknown texFormat (%i, should be 0, 1, 2, or 3)", i);
366 Mod_Sprite_SharedSetup(datapointer, LittleLong (pinhlsprite->version), (unsigned int *)(&palette[0][0]), rendermode == SPRHL_ADDITIVE);
369 Host_Error("Mod_IDSP_Load: %s has wrong version number (%i). Only %i (quake), %i (HalfLife), and %i (sprite32) supported",
370 loadmodel->name, version, SPRITE_VERSION, SPRITEHL_VERSION, SPRITE32_VERSION);
372 // TODO: Note that isanimated only means whether vertices change due to
373 // the animation. This may happen due to sprframe parameters changing.
374 // Mere texture chanegs OTOH shouldn't require isanimated to be 1.
375 loadmodel->surfmesh.isanimated = loadmodel->numframes > 1 || (loadmodel->animscenes && loadmodel->animscenes[0].framecount > 1);
379 void Mod_IDS2_Load(model_t *mod, void *buffer, void *bufferend)
383 const dsprite2_t *pinqsprite;
384 skinframe_t *skinframe;
386 int texflags = (r_mipsprites.integer ? TEXF_MIPMAP : 0) | TEXF_ISSPRITE | TEXF_PICMIP | TEXF_COMPRESS | TEXF_ALPHA | TEXF_CLAMP;
388 loadmodel->modeldatatypestring = "SPR2";
390 loadmodel->type = mod_sprite;
392 loadmodel->DrawSky = NULL;
393 loadmodel->Draw = R_Model_Sprite_Draw;
394 loadmodel->DrawDepth = NULL;
395 loadmodel->DrawLight = NULL;
396 loadmodel->DrawAddWaterPlanes = NULL;
398 pinqsprite = (dsprite2_t *)buffer;
400 version = LittleLong(pinqsprite->version);
401 if (version != SPRITE2_VERSION)
402 Host_Error("Mod_IDS2_Load: %s has wrong version number (%i should be 2 (quake 2)", loadmodel->name, version);
404 loadmodel->numframes = LittleLong (pinqsprite->numframes);
405 if (loadmodel->numframes < 1)
406 Host_Error ("Mod_IDS2_Load: Invalid # of frames: %d", loadmodel->numframes);
407 loadmodel->sprite.sprnum_type = SPR_VP_PARALLEL;
408 loadmodel->synctype = ST_SYNC;
410 // LadyHavoc: hack to allow sprites to be non-fullbright
412 for (i = 0;i < MAX_QPATH && loadmodel->name[i];i++)
413 if (loadmodel->name[i] == '!')
416 loadmodel->animscenes = (animscene_t *)Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
417 loadmodel->sprite.sprdata_frames = (mspriteframe_t *)Mem_Alloc(loadmodel->mempool, sizeof(mspriteframe_t) * loadmodel->numframes);
418 loadmodel->num_textures = loadmodel->numframes;
419 loadmodel->num_texturesperskin = 1;
420 loadmodel->data_textures = (texture_t *)Mem_Alloc(loadmodel->mempool, sizeof(texture_t) * loadmodel->num_textures);
423 for (i = 0;i < loadmodel->numframes;i++)
425 int origin[2], x, y, width, height;
426 const dsprite2frame_t *pinframe;
427 mspriteframe_t *sprframe;
429 dpsnprintf(loadmodel->animscenes[i].name, sizeof(loadmodel->animscenes[i].name), "frame %i", i);
430 loadmodel->animscenes[i].firstframe = i;
431 loadmodel->animscenes[i].framecount = 1;
432 loadmodel->animscenes[i].framerate = 10;
433 loadmodel->animscenes[i].loop = true;
435 pinframe = &pinqsprite->frames[i];
437 origin[0] = LittleLong (pinframe->origin_x);
438 origin[1] = LittleLong (pinframe->origin_y);
439 width = LittleLong (pinframe->width);
440 height = LittleLong (pinframe->height);
442 sprframe = &loadmodel->sprite.sprdata_frames[i];
444 // note that sp2 origin[0] is positive, where as it is negative in
446 sprframe->left = -origin[0];
447 sprframe->right = -origin[0] + width;
448 sprframe->up = origin[1];
449 sprframe->down = origin[1] - height;
451 x = (int)max(sprframe->left * sprframe->left, sprframe->right * sprframe->right);
452 y = (int)max(sprframe->up * sprframe->up, sprframe->down * sprframe->down);
453 if (modelradius < x + y)
457 if (cls.state != ca_dedicated)
459 for (i = 0;i < loadmodel->numframes;i++)
461 const dsprite2frame_t *pinframe;
462 pinframe = &pinqsprite->frames[i];
463 if (!(skinframe = R_SkinFrame_LoadExternal(pinframe->name, texflags, false, false)))
465 Con_Printf(CON_ERROR "Mod_IDS2_Load: failed to load %s", pinframe->name);
466 skinframe = R_SkinFrame_LoadMissing();
468 Mod_SpriteSetupTexture(&loadmodel->data_textures[i], skinframe, fullbright, false);
472 modelradius = sqrt(modelradius);
473 for (i = 0;i < 3;i++)
475 loadmodel->normalmins[i] = loadmodel->yawmins[i] = loadmodel->rotatedmins[i] = -modelradius;
476 loadmodel->normalmaxs[i] = loadmodel->yawmaxs[i] = loadmodel->rotatedmaxs[i] = modelradius;
478 loadmodel->radius = modelradius;
479 loadmodel->radius2 = modelradius * modelradius;
481 // TODO: Note that isanimated only means whether vertices change due to
482 // the animation. This may happen due to sprframe parameters changing.
483 // Mere texture chanegs OTOH shouldn't require isanimated to be 1.
484 loadmodel->surfmesh.isanimated = loadmodel->numframes > 1 || (loadmodel->animscenes && loadmodel->animscenes[0].framecount > 1);