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"};
31 cvar_t r_overheadsprites_perspective = {CVAR_SAVE, "r_overheadsprites_perspective", "5", "fake perspective effect for SPR_OVERHEAD sprites"};
32 cvar_t r_overheadsprites_pushback = {CVAR_SAVE, "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 = {CVAR_SAVE, "r_overheadsprites_scalex", "1", "additional scale for overhead sprites for x axis"};
34 cvar_t r_overheadsprites_scaley = {CVAR_SAVE, "r_overheadsprites_scaley", "1", "additional scale for overhead sprites for y axis"};
35 cvar_t r_track_sprites = {CVAR_SAVE, "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 = {CVAR_SAVE, "r_track_sprites_flags", "1", "1: Rotate sprites accordingly, 2: Make it a continuous rotation"};
37 cvar_t r_track_sprites_scalew = {CVAR_SAVE, "r_track_sprites_scalew", "1", "width scaling of tracked sprites"};
38 cvar_t r_track_sprites_scaleh = {CVAR_SAVE, "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, qboolean fullbright, qboolean 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;
71 texture->basematerialflags |= MATERIALFLAG_FULLBRIGHT;
73 texture->basematerialflags |= MATERIALFLAG_ADD | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW;
74 else if (skinframe->hasalpha)
75 texture->basematerialflags |= MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW;
76 texture->currentmaterialflags = texture->basematerialflags;
77 texture->numskinframes = 1;
78 texture->currentskinframe = texture->skinframes[0] = skinframe;
79 texture->surfaceflags = 0;
80 texture->supercontents = SUPERCONTENTS_SOLID;
81 if (!(texture->basematerialflags & MATERIALFLAG_BLENDED))
82 texture->supercontents |= SUPERCONTENTS_OPAQUE;
85 extern cvar_t gl_texturecompression_sprites;
87 static void Mod_Sprite_SharedSetup(const unsigned char *datapointer, int version, const unsigned int *palette, qboolean additive)
89 int i, j, groupframes, realframes, x, y, origin[2], width, height;
91 dspriteframetype_t *pinframetype;
92 dspriteframe_t *pinframe;
93 dspritegroup_t *pingroup;
94 dspriteinterval_t *pinintervals;
95 skinframe_t *skinframe;
96 float modelradius, interval;
97 char name[MAX_QPATH], fogname[MAX_QPATH];
98 const void *startframes;
99 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;
102 if (loadmodel->numframes < 1)
103 Host_Error ("Mod_Sprite_SharedSetup: Invalid # of frames: %d", loadmodel->numframes);
105 // LordHavoc: hack to allow sprites to be non-fullbright
107 for (i = 0;i < MAX_QPATH && loadmodel->name[i];i++)
108 if (loadmodel->name[i] == '!')
114 startframes = datapointer;
116 for (i = 0;i < loadmodel->numframes;i++)
118 pinframetype = (dspriteframetype_t *)datapointer;
119 datapointer += sizeof(dspriteframetype_t);
121 if (LittleLong (pinframetype->type) == SPR_SINGLE)
125 pingroup = (dspritegroup_t *)datapointer;
126 datapointer += sizeof(dspritegroup_t);
128 groupframes = LittleLong(pingroup->numframes);
130 datapointer += sizeof(dspriteinterval_t) * groupframes;
133 for (j = 0;j < groupframes;j++)
135 pinframe = (dspriteframe_t *)datapointer;
136 if (version == SPRITE32_VERSION)
137 datapointer += sizeof(dspriteframe_t) + LittleLong(pinframe->width) * LittleLong(pinframe->height) * 4;
138 else //if (version == SPRITE_VERSION || version == SPRITEHL_VERSION)
139 datapointer += sizeof(dspriteframe_t) + LittleLong(pinframe->width) * LittleLong(pinframe->height);
141 realframes += groupframes;
144 loadmodel->animscenes = (animscene_t *)Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
145 loadmodel->sprite.sprdata_frames = (mspriteframe_t *)Mem_Alloc(loadmodel->mempool, sizeof(mspriteframe_t) * realframes);
146 loadmodel->num_textures = realframes;
147 loadmodel->num_texturesperskin = 1;
148 loadmodel->data_textures = (texture_t *)Mem_Alloc(loadmodel->mempool, sizeof(texture_t) * loadmodel->num_textures);
150 datapointer = (unsigned char *)startframes;
152 for (i = 0;i < loadmodel->numframes;i++)
154 pinframetype = (dspriteframetype_t *)datapointer;
155 datapointer += sizeof(dspriteframetype_t);
157 if (LittleLong (pinframetype->type) == SPR_SINGLE)
164 pingroup = (dspritegroup_t *)datapointer;
165 datapointer += sizeof(dspritegroup_t);
167 groupframes = LittleLong(pingroup->numframes);
169 pinintervals = (dspriteinterval_t *)datapointer;
170 datapointer += sizeof(dspriteinterval_t) * groupframes;
172 interval = LittleFloat(pinintervals[0].interval);
173 if (interval < 0.01f)
174 Host_Error("Mod_Sprite_SharedSetup: invalid interval");
177 dpsnprintf(loadmodel->animscenes[i].name, sizeof(loadmodel->animscenes[i].name), "frame %i", i);
178 loadmodel->animscenes[i].firstframe = realframes;
179 loadmodel->animscenes[i].framecount = groupframes;
180 loadmodel->animscenes[i].framerate = 1.0f / interval;
181 loadmodel->animscenes[i].loop = true;
183 for (j = 0;j < groupframes;j++)
185 pinframe = (dspriteframe_t *)datapointer;
186 datapointer += sizeof(dspriteframe_t);
188 origin[0] = LittleLong (pinframe->origin[0]);
189 origin[1] = LittleLong (pinframe->origin[1]);
190 width = LittleLong (pinframe->width);
191 height = LittleLong (pinframe->height);
193 loadmodel->sprite.sprdata_frames[realframes].left = origin[0];
194 loadmodel->sprite.sprdata_frames[realframes].right = origin[0] + width;
195 loadmodel->sprite.sprdata_frames[realframes].up = origin[1];
196 loadmodel->sprite.sprdata_frames[realframes].down = origin[1] - height;
198 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);
199 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);
200 if (modelradius < x + y)
203 if (cls.state != ca_dedicated)
206 // note: Nehahra's null.spr has width == 0 and height == 0
207 if (width > 0 && height > 0)
211 dpsnprintf (name, sizeof(name), "%s_%i_%i", loadmodel->name, i, j);
212 dpsnprintf (fogname, sizeof(fogname), "%s_%i_%ifog", loadmodel->name, i, j);
216 dpsnprintf (name, sizeof(name), "%s_%i", loadmodel->name, i);
217 dpsnprintf (fogname, sizeof(fogname), "%s_%ifog", loadmodel->name, i);
219 if (!(skinframe = R_SkinFrame_LoadExternal(name, texflags | TEXF_COMPRESS, false)))
221 unsigned char *pixels = (unsigned char *) Mem_Alloc(loadmodel->mempool, width*height*4);
222 if (version == SPRITE32_VERSION)
224 for (x = 0;x < width*height;x++)
226 pixels[x*4+2] = datapointer[x*4+0];
227 pixels[x*4+1] = datapointer[x*4+1];
228 pixels[x*4+0] = datapointer[x*4+2];
229 pixels[x*4+3] = datapointer[x*4+3];
232 else //if (version == SPRITEHL_VERSION || version == SPRITE_VERSION)
233 Image_Copy8bitBGRA(datapointer, pixels, width*height, palette ? palette : palette_bgra_transparent);
234 skinframe = R_SkinFrame_LoadInternalBGRA(name, texflags, pixels, width, height, false);
235 // texflags |= TEXF_COMPRESS;
239 if (skinframe == NULL)
240 skinframe = R_SkinFrame_LoadMissing();
241 Mod_SpriteSetupTexture(&loadmodel->data_textures[realframes], skinframe, fullbright, additive);
244 if (version == SPRITE32_VERSION)
245 datapointer += width * height * 4;
246 else //if (version == SPRITE_VERSION || version == SPRITEHL_VERSION)
247 datapointer += width * height;
252 modelradius = sqrt(modelradius);
253 for (i = 0;i < 3;i++)
255 loadmodel->normalmins[i] = loadmodel->yawmins[i] = loadmodel->rotatedmins[i] = -modelradius;
256 loadmodel->normalmaxs[i] = loadmodel->yawmaxs[i] = loadmodel->rotatedmaxs[i] = modelradius;
258 loadmodel->radius = modelradius;
259 loadmodel->radius2 = modelradius * modelradius;
262 extern void R_Model_Sprite_Draw(entity_render_t *ent);
263 void Mod_IDSP_Load(dp_model_t *mod, void *buffer, void *bufferend)
266 const unsigned char *datapointer;
268 datapointer = (unsigned char *)buffer;
270 loadmodel->modeldatatypestring = "SPR1";
272 loadmodel->type = mod_sprite;
274 loadmodel->DrawSky = NULL;
275 loadmodel->Draw = R_Model_Sprite_Draw;
276 loadmodel->DrawDepth = NULL;
277 loadmodel->CompileShadowVolume = NULL;
278 loadmodel->DrawShadowVolume = NULL;
279 loadmodel->DrawLight = NULL;
280 loadmodel->DrawAddWaterPlanes = NULL;
282 version = LittleLong(((dsprite_t *)buffer)->version);
283 if (version == SPRITE_VERSION || version == SPRITE32_VERSION)
285 dsprite_t *pinqsprite;
287 pinqsprite = (dsprite_t *)datapointer;
288 datapointer += sizeof(dsprite_t);
290 loadmodel->numframes = LittleLong (pinqsprite->numframes);
291 loadmodel->sprite.sprnum_type = LittleLong (pinqsprite->type);
292 loadmodel->synctype = (synctype_t)LittleLong (pinqsprite->synctype);
294 Mod_Sprite_SharedSetup(datapointer, LittleLong (pinqsprite->version), NULL, false);
296 else if (version == SPRITEHL_VERSION)
299 unsigned char palette[256][4];
300 const unsigned char *in;
301 dspritehl_t *pinhlsprite;
303 pinhlsprite = (dspritehl_t *)datapointer;
304 datapointer += sizeof(dspritehl_t);
306 loadmodel->numframes = LittleLong (pinhlsprite->numframes);
307 loadmodel->sprite.sprnum_type = LittleLong (pinhlsprite->type);
308 loadmodel->synctype = (synctype_t)LittleLong (pinhlsprite->synctype);
309 rendermode = pinhlsprite->rendermode;
313 i = in[0] + in[1] * 256;
315 Host_Error ("Mod_IDSP_Load: unexpected number of palette colors %i (should be 256)", i);
321 for (i = 0;i < 256;i++)
323 palette[i][2] = in[i*3+0];
324 palette[i][1] = in[i*3+1];
325 palette[i][0] = in[i*3+2];
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 // also passes additive == true to Mod_Sprite_SharedSetup
339 case SPRHL_INDEXALPHA:
340 for (i = 0;i < 256;i++)
342 palette[i][2] = in[765];
343 palette[i][1] = in[766];
344 palette[i][0] = in[767];
349 case SPRHL_ALPHATEST:
350 for (i = 0;i < 256;i++)
352 palette[i][2] = in[i*3+0];
353 palette[i][1] = in[i*3+1];
354 palette[i][0] = in[i*3+2];
357 palette[255][0] = palette[255][1] = palette[255][2] = palette[255][3] = 0;
358 // should this use alpha test or alpha blend? (currently blend)
361 Host_Error("Mod_IDSP_Load: unknown texFormat (%i, should be 0, 1, 2, or 3)", i);
365 Mod_Sprite_SharedSetup(datapointer, LittleLong (pinhlsprite->version), (unsigned int *)(&palette[0][0]), rendermode == SPRHL_ADDITIVE);
368 Host_Error("Mod_IDSP_Load: %s has wrong version number (%i). Only %i (quake), %i (HalfLife), and %i (sprite32) supported",
369 loadmodel->name, version, SPRITE_VERSION, SPRITEHL_VERSION, SPRITE32_VERSION);
371 loadmodel->surfmesh.isanimated = loadmodel->numframes > 1 || loadmodel->animscenes[0].framecount > 1;
375 void Mod_IDS2_Load(dp_model_t *mod, void *buffer, void *bufferend)
379 const dsprite2_t *pinqsprite;
380 skinframe_t *skinframe;
382 int texflags = (r_mipsprites.integer ? TEXF_MIPMAP : 0) | TEXF_ISSPRITE | TEXF_PICMIP | TEXF_COMPRESS | TEXF_ALPHA | TEXF_CLAMP;
384 loadmodel->modeldatatypestring = "SPR2";
386 loadmodel->type = mod_sprite;
388 loadmodel->DrawSky = NULL;
389 loadmodel->Draw = R_Model_Sprite_Draw;
390 loadmodel->DrawDepth = NULL;
391 loadmodel->CompileShadowVolume = NULL;
392 loadmodel->DrawShadowVolume = NULL;
393 loadmodel->DrawLight = NULL;
394 loadmodel->DrawAddWaterPlanes = NULL;
396 pinqsprite = (dsprite2_t *)buffer;
398 version = LittleLong(pinqsprite->version);
399 if (version != SPRITE2_VERSION)
400 Host_Error("Mod_IDS2_Load: %s has wrong version number (%i should be 2 (quake 2)", loadmodel->name, version);
402 loadmodel->numframes = LittleLong (pinqsprite->numframes);
403 if (loadmodel->numframes < 1)
404 Host_Error ("Mod_IDS2_Load: Invalid # of frames: %d", loadmodel->numframes);
405 loadmodel->sprite.sprnum_type = SPR_VP_PARALLEL;
406 loadmodel->synctype = ST_SYNC;
408 // LordHavoc: hack to allow sprites to be non-fullbright
410 for (i = 0;i < MAX_QPATH && loadmodel->name[i];i++)
411 if (loadmodel->name[i] == '!')
414 loadmodel->animscenes = (animscene_t *)Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
415 loadmodel->sprite.sprdata_frames = (mspriteframe_t *)Mem_Alloc(loadmodel->mempool, sizeof(mspriteframe_t) * loadmodel->numframes);
416 loadmodel->num_textures = loadmodel->numframes;
417 loadmodel->num_texturesperskin = 1;
418 loadmodel->data_textures = (texture_t *)Mem_Alloc(loadmodel->mempool, sizeof(texture_t) * loadmodel->num_textures);
421 for (i = 0;i < loadmodel->numframes;i++)
423 int origin[2], x, y, width, height;
424 const dsprite2frame_t *pinframe;
425 mspriteframe_t *sprframe;
427 dpsnprintf(loadmodel->animscenes[i].name, sizeof(loadmodel->animscenes[i].name), "frame %i", i);
428 loadmodel->animscenes[i].firstframe = i;
429 loadmodel->animscenes[i].framecount = 1;
430 loadmodel->animscenes[i].framerate = 10;
431 loadmodel->animscenes[i].loop = true;
433 pinframe = &pinqsprite->frames[i];
435 origin[0] = LittleLong (pinframe->origin_x);
436 origin[1] = LittleLong (pinframe->origin_y);
437 width = LittleLong (pinframe->width);
438 height = LittleLong (pinframe->height);
440 sprframe = &loadmodel->sprite.sprdata_frames[i];
442 // note that sp2 origin[0] is positive, where as it is negative in
444 sprframe->left = -origin[0];
445 sprframe->right = -origin[0] + width;
446 sprframe->up = origin[1];
447 sprframe->down = origin[1] - height;
449 x = (int)max(sprframe->left * sprframe->left, sprframe->right * sprframe->right);
450 y = (int)max(sprframe->up * sprframe->up, sprframe->down * sprframe->down);
451 if (modelradius < x + y)
455 if (cls.state != ca_dedicated)
457 for (i = 0;i < loadmodel->numframes;i++)
459 const dsprite2frame_t *pinframe;
460 pinframe = &pinqsprite->frames[i];
461 if (!(skinframe = R_SkinFrame_LoadExternal(pinframe->name, texflags, false)))
463 Con_Printf("Mod_IDS2_Load: failed to load %s", pinframe->name);
464 skinframe = R_SkinFrame_LoadMissing();
466 Mod_SpriteSetupTexture(&loadmodel->data_textures[i], skinframe, fullbright, false);
470 modelradius = sqrt(modelradius);
471 for (i = 0;i < 3;i++)
473 loadmodel->normalmins[i] = loadmodel->yawmins[i] = loadmodel->rotatedmins[i] = -modelradius;
474 loadmodel->normalmaxs[i] = loadmodel->yawmaxs[i] = loadmodel->rotatedmaxs[i] = modelradius;
476 loadmodel->radius = modelradius;
477 loadmodel->radius2 = modelradius * modelradius;
479 loadmodel->surfmesh.isanimated = loadmodel->numframes > 1 || loadmodel->animscenes[0].framecount > 1;