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_picmipsprites = {CVAR_SAVE, "r_picmipsprites", "1", "make gl_picmip affect sprites too (saves some graphics memory in sprite heavy games)"};
30 cvar_t r_labelsprites_scale = {CVAR_SAVE, "r_labelsprites_scale", "1", "global scale to apply to label sprites before conversion to HUD coordinates"};
31 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"};
38 void Mod_SpriteInit (void)
40 Cvar_RegisterVariable(&r_mipsprites);
41 Cvar_RegisterVariable(&r_picmipsprites);
42 Cvar_RegisterVariable(&r_labelsprites_scale);
43 Cvar_RegisterVariable(&r_labelsprites_roundtopixels);
46 static void Mod_SpriteSetupTexture(texture_t *texture, skinframe_t *skinframe, qboolean fullbright, qboolean additive)
49 skinframe = R_SkinFrame_LoadMissing();
50 texture->basematerialflags = MATERIALFLAG_WALL;
52 texture->basematerialflags |= MATERIALFLAG_FULLBRIGHT;
54 texture->basematerialflags |= MATERIALFLAG_ADD | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW;
55 else if (skinframe->fog)
56 texture->basematerialflags |= MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW;
57 texture->currentmaterialflags = texture->basematerialflags;
58 texture->numskinframes = 1;
59 texture->currentskinframe = texture->skinframes[0] = skinframe;
62 static void Mod_Sprite_SharedSetup(const unsigned char *datapointer, int version, const unsigned int *palette, qboolean additive)
64 int i, j, groupframes, realframes, x, y, origin[2], width, height, fullbright;
65 dspriteframetype_t *pinframetype;
66 dspriteframe_t *pinframe;
67 dspritegroup_t *pingroup;
68 dspriteinterval_t *pinintervals;
69 skinframe_t *skinframe;
70 float modelradius, interval;
71 char name[MAX_QPATH], fogname[MAX_QPATH];
72 const void *startframes;
73 int texflags = (r_mipsprites.integer ? TEXF_MIPMAP : 0) | (r_picmipsprites.integer ? TEXF_PICMIP : 0) | TEXF_ALPHA | TEXF_CLAMP | TEXF_PRECACHE;
76 if (loadmodel->numframes < 1)
77 Host_Error ("Mod_Sprite_SharedSetup: Invalid # of frames: %d", loadmodel->numframes);
79 // LordHavoc: hack to allow sprites to be non-fullbright
81 for (i = 0;i < MAX_QPATH && loadmodel->name[i];i++)
82 if (loadmodel->name[i] == '!')
88 startframes = datapointer;
90 for (i = 0;i < loadmodel->numframes;i++)
92 pinframetype = (dspriteframetype_t *)datapointer;
93 datapointer += sizeof(dspriteframetype_t);
95 if (LittleLong (pinframetype->type) == SPR_SINGLE)
99 pingroup = (dspritegroup_t *)datapointer;
100 datapointer += sizeof(dspritegroup_t);
102 groupframes = LittleLong(pingroup->numframes);
104 datapointer += sizeof(dspriteinterval_t) * groupframes;
107 for (j = 0;j < groupframes;j++)
109 pinframe = (dspriteframe_t *)datapointer;
110 if (version == SPRITE32_VERSION)
111 datapointer += sizeof(dspriteframe_t) + LittleLong(pinframe->width) * LittleLong(pinframe->height) * 4;
112 else //if (version == SPRITE_VERSION || version == SPRITEHL_VERSION)
113 datapointer += sizeof(dspriteframe_t) + LittleLong(pinframe->width) * LittleLong(pinframe->height);
115 realframes += groupframes;
118 loadmodel->animscenes = (animscene_t *)Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
119 loadmodel->sprite.sprdata_frames = (mspriteframe_t *)Mem_Alloc(loadmodel->mempool, sizeof(mspriteframe_t) * realframes);
120 loadmodel->num_textures = realframes;
121 loadmodel->num_texturesperskin = 1;
122 loadmodel->data_textures = (texture_t *)Mem_Alloc(loadmodel->mempool, sizeof(texture_t) * loadmodel->num_textures);
124 datapointer = (unsigned char *)startframes;
126 for (i = 0;i < loadmodel->numframes;i++)
128 pinframetype = (dspriteframetype_t *)datapointer;
129 datapointer += sizeof(dspriteframetype_t);
131 if (LittleLong (pinframetype->type) == SPR_SINGLE)
138 pingroup = (dspritegroup_t *)datapointer;
139 datapointer += sizeof(dspritegroup_t);
141 groupframes = LittleLong(pingroup->numframes);
143 pinintervals = (dspriteinterval_t *)datapointer;
144 datapointer += sizeof(dspriteinterval_t) * groupframes;
146 interval = LittleFloat(pinintervals[0].interval);
147 if (interval < 0.01f)
148 Host_Error("Mod_Sprite_SharedSetup: invalid interval");
151 sprintf(loadmodel->animscenes[i].name, "frame %i", i);
152 loadmodel->animscenes[i].firstframe = realframes;
153 loadmodel->animscenes[i].framecount = groupframes;
154 loadmodel->animscenes[i].framerate = 1.0f / interval;
155 loadmodel->animscenes[i].loop = true;
157 for (j = 0;j < groupframes;j++)
159 pinframe = (dspriteframe_t *)datapointer;
160 datapointer += sizeof(dspriteframe_t);
162 origin[0] = LittleLong (pinframe->origin[0]);
163 origin[1] = LittleLong (pinframe->origin[1]);
164 width = LittleLong (pinframe->width);
165 height = LittleLong (pinframe->height);
167 loadmodel->sprite.sprdata_frames[realframes].left = origin[0];
168 loadmodel->sprite.sprdata_frames[realframes].right = origin[0] + width;
169 loadmodel->sprite.sprdata_frames[realframes].up = origin[1];
170 loadmodel->sprite.sprdata_frames[realframes].down = origin[1] - height;
172 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);
173 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);
174 if (modelradius < x + y)
177 if (cls.state != ca_dedicated)
180 // note: Nehahra's null.spr has width == 0 and height == 0
181 if (width > 0 && height > 0)
185 sprintf (name, "%s_%i_%i", loadmodel->name, i, j);
186 sprintf (fogname, "%s_%i_%ifog", loadmodel->name, i, j);
190 sprintf (name, "%s_%i", loadmodel->name, i);
191 sprintf (fogname, "%s_%ifog", loadmodel->name, i);
193 if (!(skinframe = R_SkinFrame_LoadExternal(name, texflags | TEXF_COMPRESS, false)))
195 unsigned char *pixels = Mem_Alloc(loadmodel->mempool, width*height*4);
196 if (version == SPRITE32_VERSION)
198 for (x = 0;x < width*height;x++)
200 pixels[x*4+2] = datapointer[x*4+0];
201 pixels[x*4+1] = datapointer[x*4+1];
202 pixels[x*4+0] = datapointer[x*4+2];
203 pixels[x*4+3] = datapointer[x*4+3];
206 else //if (version == SPRITEHL_VERSION || version == SPRITE_VERSION)
207 Image_Copy8bitBGRA(datapointer, pixels, width*height, palette ? palette : palette_bgra_transparent);
208 skinframe = R_SkinFrame_LoadInternalBGRA(name, texflags, pixels, width, height);
212 if (skinframe == NULL)
213 skinframe = R_SkinFrame_LoadMissing();
214 Mod_SpriteSetupTexture(&loadmodel->data_textures[realframes], skinframe, fullbright, additive);
217 if (version == SPRITE32_VERSION)
218 datapointer += width * height * 4;
219 else //if (version == SPRITE_VERSION || version == SPRITEHL_VERSION)
220 datapointer += width * height;
225 modelradius = sqrt(modelradius);
226 for (i = 0;i < 3;i++)
228 loadmodel->normalmins[i] = loadmodel->yawmins[i] = loadmodel->rotatedmins[i] = -modelradius;
229 loadmodel->normalmaxs[i] = loadmodel->yawmaxs[i] = loadmodel->rotatedmaxs[i] = modelradius;
231 loadmodel->radius = modelradius;
232 loadmodel->radius2 = modelradius * modelradius;
235 extern void R_Model_Sprite_Draw(entity_render_t *ent);
236 void Mod_IDSP_Load(model_t *mod, void *buffer, void *bufferend)
239 const unsigned char *datapointer;
241 datapointer = (unsigned char *)buffer;
243 loadmodel->modeldatatypestring = "SPR1";
245 loadmodel->type = mod_sprite;
247 loadmodel->DrawSky = NULL;
248 loadmodel->Draw = R_Model_Sprite_Draw;
249 loadmodel->DrawDepth = NULL;
250 loadmodel->CompileShadowVolume = NULL;
251 loadmodel->DrawShadowVolume = NULL;
252 loadmodel->DrawLight = NULL;
253 loadmodel->DrawAddWaterPlanes = NULL;
255 version = LittleLong(((dsprite_t *)buffer)->version);
256 if (version == SPRITE_VERSION || version == SPRITE32_VERSION)
258 dsprite_t *pinqsprite;
260 pinqsprite = (dsprite_t *)datapointer;
261 datapointer += sizeof(dsprite_t);
263 loadmodel->numframes = LittleLong (pinqsprite->numframes);
264 loadmodel->sprite.sprnum_type = LittleLong (pinqsprite->type);
265 loadmodel->synctype = (synctype_t)LittleLong (pinqsprite->synctype);
267 Mod_Sprite_SharedSetup(datapointer, LittleLong (pinqsprite->version), NULL, false);
269 else if (version == SPRITEHL_VERSION)
272 unsigned char palette[256][4];
273 const unsigned char *in;
274 dspritehl_t *pinhlsprite;
276 pinhlsprite = (dspritehl_t *)datapointer;
277 datapointer += sizeof(dspritehl_t);
279 loadmodel->numframes = LittleLong (pinhlsprite->numframes);
280 loadmodel->sprite.sprnum_type = LittleLong (pinhlsprite->type);
281 loadmodel->synctype = (synctype_t)LittleLong (pinhlsprite->synctype);
282 rendermode = pinhlsprite->rendermode;
286 i = in[0] + in[1] * 256;
288 Host_Error ("Mod_IDSP_Load: unexpected number of palette colors %i (should be 256)", i);
294 for (i = 0;i < 256;i++)
296 palette[i][2] = in[i*3+0];
297 palette[i][1] = in[i*3+1];
298 palette[i][0] = in[i*3+2];
303 for (i = 0;i < 256;i++)
305 palette[i][2] = in[i*3+0];
306 palette[i][1] = in[i*3+1];
307 palette[i][0] = in[i*3+2];
310 // also passes additive == true to Mod_Sprite_SharedSetup
312 case SPRHL_INDEXALPHA:
313 for (i = 0;i < 256;i++)
315 palette[i][2] = in[765];
316 palette[i][1] = in[766];
317 palette[i][0] = in[767];
322 case SPRHL_ALPHATEST:
323 for (i = 0;i < 256;i++)
325 palette[i][2] = in[i*3+0];
326 palette[i][1] = in[i*3+1];
327 palette[i][0] = in[i*3+2];
330 palette[255][0] = palette[255][1] = palette[255][2] = palette[255][3] = 0;
331 // should this use alpha test or alpha blend? (currently blend)
334 Host_Error("Mod_IDSP_Load: unknown texFormat (%i, should be 0, 1, 2, or 3)", i);
338 Mod_Sprite_SharedSetup(datapointer, LittleLong (pinhlsprite->version), (unsigned int *)(&palette[0][0]), rendermode == SPRHL_ADDITIVE);
341 Host_Error("Mod_IDSP_Load: %s has wrong version number (%i). Only %i (quake), %i (HalfLife), and %i (sprite32) supported",
342 loadmodel->name, version, SPRITE_VERSION, SPRITEHL_VERSION, SPRITE32_VERSION);
346 void Mod_IDS2_Load(model_t *mod, void *buffer, void *bufferend)
348 int i, version, fullbright;
349 const dsprite2_t *pinqsprite;
350 skinframe_t *skinframe;
352 int texflags = (r_mipsprites.integer ? TEXF_MIPMAP : 0) | (r_picmipsprites.integer ? TEXF_PICMIP : 0) | TEXF_COMPRESS | TEXF_ALPHA | TEXF_CLAMP | TEXF_PRECACHE;
354 loadmodel->modeldatatypestring = "SPR2";
356 loadmodel->type = mod_sprite;
358 loadmodel->DrawSky = NULL;
359 loadmodel->Draw = R_Model_Sprite_Draw;
360 loadmodel->DrawDepth = NULL;
361 loadmodel->CompileShadowVolume = NULL;
362 loadmodel->DrawShadowVolume = NULL;
363 loadmodel->DrawLight = NULL;
364 loadmodel->DrawAddWaterPlanes = NULL;
366 pinqsprite = (dsprite2_t *)buffer;
368 version = LittleLong(pinqsprite->version);
369 if (version != SPRITE2_VERSION)
370 Host_Error("Mod_IDS2_Load: %s has wrong version number (%i should be 2 (quake 2)", loadmodel->name, version);
372 loadmodel->numframes = LittleLong (pinqsprite->numframes);
373 if (loadmodel->numframes < 1)
374 Host_Error ("Mod_IDS2_Load: Invalid # of frames: %d", loadmodel->numframes);
375 loadmodel->sprite.sprnum_type = SPR_VP_PARALLEL;
376 loadmodel->synctype = ST_SYNC;
378 // LordHavoc: hack to allow sprites to be non-fullbright
380 for (i = 0;i < MAX_QPATH && loadmodel->name[i];i++)
381 if (loadmodel->name[i] == '!')
384 loadmodel->animscenes = (animscene_t *)Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
385 loadmodel->sprite.sprdata_frames = (mspriteframe_t *)Mem_Alloc(loadmodel->mempool, sizeof(mspriteframe_t) * loadmodel->numframes);
386 loadmodel->num_textures = loadmodel->numframes;
387 loadmodel->num_texturesperskin = 1;
388 loadmodel->data_textures = (texture_t *)Mem_Alloc(loadmodel->mempool, sizeof(texture_t) * loadmodel->num_textures);
391 for (i = 0;i < loadmodel->numframes;i++)
393 int origin[2], x, y, width, height;
394 const dsprite2frame_t *pinframe;
395 mspriteframe_t *sprframe;
397 dpsnprintf(loadmodel->animscenes[i].name, sizeof(loadmodel->animscenes[i].name), "frame %i", i);
398 loadmodel->animscenes[i].firstframe = i;
399 loadmodel->animscenes[i].framecount = 1;
400 loadmodel->animscenes[i].framerate = 10;
401 loadmodel->animscenes[i].loop = true;
403 pinframe = &pinqsprite->frames[i];
405 origin[0] = LittleLong (pinframe->origin_x);
406 origin[1] = LittleLong (pinframe->origin_y);
407 width = LittleLong (pinframe->width);
408 height = LittleLong (pinframe->height);
410 sprframe = &loadmodel->sprite.sprdata_frames[i];
412 // note that sp2 origin[0] is positive, where as it is negative in
414 sprframe->left = -origin[0];
415 sprframe->right = -origin[0] + width;
416 sprframe->up = origin[1];
417 sprframe->down = origin[1] - height;
419 x = (int)max(sprframe->left * sprframe->left, sprframe->right * sprframe->right);
420 y = (int)max(sprframe->up * sprframe->up, sprframe->down * sprframe->down);
421 if (modelradius < x + y)
425 if (cls.state != ca_dedicated)
427 for (i = 0;i < loadmodel->numframes;i++)
429 const dsprite2frame_t *pinframe;
430 pinframe = &pinqsprite->frames[i];
431 if (!(skinframe = R_SkinFrame_LoadExternal(pinframe->name, texflags, false)))
433 Con_Printf("Mod_IDS2_Load: failed to load %s", pinframe->name);
434 skinframe = R_SkinFrame_LoadMissing();
436 Mod_SpriteSetupTexture(&loadmodel->data_textures[i], skinframe, fullbright, false);
440 modelradius = sqrt(modelradius);
441 for (i = 0;i < 3;i++)
443 loadmodel->normalmins[i] = loadmodel->yawmins[i] = loadmodel->rotatedmins[i] = -modelradius;
444 loadmodel->normalmaxs[i] = loadmodel->yawmaxs[i] = loadmodel->rotatedmaxs[i] = modelradius;
446 loadmodel->radius = modelradius;
447 loadmodel->radius2 = modelradius * modelradius;