]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - model_sprite.c
server list: allow more AND and OR masks (except in DP_SMALLMEMORY profile)
[xonotic/darkplaces.git] / model_sprite.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
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.
8
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.
12
13 See the GNU General Public License for more details.
14
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.
18
19 */
20 // models.c -- model loading and caching
21
22 // models are the only shared resource between a client and server running
23 // on the same machine.
24
25 #include "quakedef.h"
26 #include "image.h"
27
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"};
39
40 /*
41 ===============
42 Mod_SpriteInit
43 ===============
44 */
45 void Mod_SpriteInit (void)
46 {
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);
58 }
59
60 static void Mod_SpriteSetupTexture(texture_t *texture, skinframe_t *skinframe, qboolean fullbright, qboolean additive)
61 {
62         if (!skinframe)
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         if (fullbright)
71                 texture->basematerialflags |= MATERIALFLAG_FULLBRIGHT;
72         if (additive)
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;
83 }
84
85 extern cvar_t gl_texturecompression_sprites;
86
87 static void Mod_Sprite_SharedSetup(const unsigned char *datapointer, int version, const unsigned int *palette, qboolean additive)
88 {
89         int                                     i, j, groupframes, realframes, x, y, origin[2], width, height;
90         qboolean                        fullbright;
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;
100         modelradius = 0;
101
102         if (loadmodel->numframes < 1)
103                 Host_Error ("Mod_Sprite_SharedSetup: Invalid # of frames: %d", loadmodel->numframes);
104
105         // LordHavoc: hack to allow sprites to be non-fullbright
106         fullbright = true;
107         for (i = 0;i < MAX_QPATH && loadmodel->name[i];i++)
108                 if (loadmodel->name[i] == '!')
109                         fullbright = false;
110
111 //
112 // load the frames
113 //
114         startframes = datapointer;
115         realframes = 0;
116         for (i = 0;i < loadmodel->numframes;i++)
117         {
118                 pinframetype = (dspriteframetype_t *)datapointer;
119                 datapointer += sizeof(dspriteframetype_t);
120
121                 if (LittleLong (pinframetype->type) == SPR_SINGLE)
122                         groupframes = 1;
123                 else
124                 {
125                         pingroup = (dspritegroup_t *)datapointer;
126                         datapointer += sizeof(dspritegroup_t);
127
128                         groupframes = LittleLong(pingroup->numframes);
129
130                         datapointer += sizeof(dspriteinterval_t) * groupframes;
131                 }
132
133                 for (j = 0;j < groupframes;j++)
134                 {
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);
140                 }
141                 realframes += groupframes;
142         }
143
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);
149
150         datapointer = (unsigned char *)startframes;
151         realframes = 0;
152         for (i = 0;i < loadmodel->numframes;i++)
153         {
154                 pinframetype = (dspriteframetype_t *)datapointer;
155                 datapointer += sizeof(dspriteframetype_t);
156
157                 if (LittleLong (pinframetype->type) == SPR_SINGLE)
158                 {
159                         groupframes = 1;
160                         interval = 0.1f;
161                 }
162                 else
163                 {
164                         pingroup = (dspritegroup_t *)datapointer;
165                         datapointer += sizeof(dspritegroup_t);
166
167                         groupframes = LittleLong(pingroup->numframes);
168
169                         pinintervals = (dspriteinterval_t *)datapointer;
170                         datapointer += sizeof(dspriteinterval_t) * groupframes;
171
172                         interval = LittleFloat(pinintervals[0].interval);
173                         if (interval < 0.01f)
174                                 Host_Error("Mod_Sprite_SharedSetup: invalid interval");
175                 }
176
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;
182
183                 for (j = 0;j < groupframes;j++)
184                 {
185                         pinframe = (dspriteframe_t *)datapointer;
186                         datapointer += sizeof(dspriteframe_t);
187
188                         origin[0] = LittleLong (pinframe->origin[0]);
189                         origin[1] = LittleLong (pinframe->origin[1]);
190                         width = LittleLong (pinframe->width);
191                         height = LittleLong (pinframe->height);
192
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;
197
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)
201                                 modelradius = x + y;
202
203                         if (cls.state != ca_dedicated)
204                         {
205                                 skinframe = NULL;
206                                 // note: Nehahra's null.spr has width == 0 and height == 0
207                                 if (width > 0 && height > 0)
208                                 {
209                                         if (groupframes > 1)
210                                         {
211                                                 dpsnprintf (name, sizeof(name), "%s_%i_%i", loadmodel->name, i, j);
212                                                 dpsnprintf (fogname, sizeof(fogname), "%s_%i_%ifog", loadmodel->name, i, j);
213                                         }
214                                         else
215                                         {
216                                                 dpsnprintf (name, sizeof(name), "%s_%i", loadmodel->name, i);
217                                                 dpsnprintf (fogname, sizeof(fogname), "%s_%ifog", loadmodel->name, i);
218                                         }
219                                         if (!(skinframe = R_SkinFrame_LoadExternal(name, texflags | TEXF_COMPRESS, false)))
220                                         {
221                                                 unsigned char *pixels = (unsigned char *) Mem_Alloc(loadmodel->mempool, width*height*4);
222                                                 if (version == SPRITE32_VERSION)
223                                                 {
224                                                         for (x = 0;x < width*height;x++)
225                                                         {
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];
230                                                         }
231                                                 }
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;
236                                                 Mem_Free(pixels);
237                                         }
238                                 }
239                                 if (skinframe == NULL)
240                                         skinframe = R_SkinFrame_LoadMissing();
241                                 Mod_SpriteSetupTexture(&loadmodel->data_textures[realframes], skinframe, fullbright, additive);
242                         }
243
244                         if (version == SPRITE32_VERSION)
245                                 datapointer += width * height * 4;
246                         else //if (version == SPRITE_VERSION || version == SPRITEHL_VERSION)
247                                 datapointer += width * height;
248                         realframes++;
249                 }
250         }
251
252         modelradius = sqrt(modelradius);
253         for (i = 0;i < 3;i++)
254         {
255                 loadmodel->normalmins[i] = loadmodel->yawmins[i] = loadmodel->rotatedmins[i] = -modelradius;
256                 loadmodel->normalmaxs[i] = loadmodel->yawmaxs[i] = loadmodel->rotatedmaxs[i] = modelradius;
257         }
258         loadmodel->radius = modelradius;
259         loadmodel->radius2 = modelradius * modelradius;
260 }
261
262 extern void R_Model_Sprite_Draw(entity_render_t *ent);
263 void Mod_IDSP_Load(dp_model_t *mod, void *buffer, void *bufferend)
264 {
265         int version;
266         const unsigned char *datapointer;
267
268         datapointer = (unsigned char *)buffer;
269
270         loadmodel->modeldatatypestring = "SPR1";
271
272         loadmodel->type = mod_sprite;
273
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;
281
282         version = LittleLong(((dsprite_t *)buffer)->version);
283         if (version == SPRITE_VERSION || version == SPRITE32_VERSION)
284         {
285                 dsprite_t *pinqsprite;
286
287                 pinqsprite = (dsprite_t *)datapointer;
288                 datapointer += sizeof(dsprite_t);
289
290                 loadmodel->numframes = LittleLong (pinqsprite->numframes);
291                 loadmodel->sprite.sprnum_type = LittleLong (pinqsprite->type);
292                 loadmodel->synctype = (synctype_t)LittleLong (pinqsprite->synctype);
293
294                 Mod_Sprite_SharedSetup(datapointer, LittleLong (pinqsprite->version), NULL, false);
295         }
296         else if (version == SPRITEHL_VERSION)
297         {
298                 int i, rendermode;
299                 unsigned char palette[256][4];
300                 const unsigned char *in;
301                 dspritehl_t *pinhlsprite;
302
303                 pinhlsprite = (dspritehl_t *)datapointer;
304                 datapointer += sizeof(dspritehl_t);
305
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;
310
311                 in = datapointer;
312                 datapointer += 2;
313                 i = in[0] + in[1] * 256;
314                 if (i != 256)
315                         Host_Error ("Mod_IDSP_Load: unexpected number of palette colors %i (should be 256)", i);
316                 in = datapointer;
317                 datapointer += 768;
318                 switch(rendermode)
319                 {
320                 case SPRHL_OPAQUE:
321                         for (i = 0;i < 256;i++)
322                         {
323                                 palette[i][2] = in[i*3+0];
324                                 palette[i][1] = in[i*3+1];
325                                 palette[i][0] = in[i*3+2];
326                                 palette[i][3] = 255;
327                         }
328                         break;
329                 case SPRHL_ADDITIVE:
330                         for (i = 0;i < 256;i++)
331                         {
332                                 palette[i][2] = in[i*3+0];
333                                 palette[i][1] = in[i*3+1];
334                                 palette[i][0] = in[i*3+2];
335                                 palette[i][3] = 255;
336                         }
337                         // also passes additive == true to Mod_Sprite_SharedSetup
338                         break;
339                 case SPRHL_INDEXALPHA:
340                         for (i = 0;i < 256;i++)
341                         {
342                                 palette[i][2] = in[765];
343                                 palette[i][1] = in[766];
344                                 palette[i][0] = in[767];
345                                 palette[i][3] = i;
346                                 in += 3;
347                         }
348                         break;
349                 case SPRHL_ALPHATEST:
350                         for (i = 0;i < 256;i++)
351                         {
352                                 palette[i][2] = in[i*3+0];
353                                 palette[i][1] = in[i*3+1];
354                                 palette[i][0] = in[i*3+2];
355                                 palette[i][3] = 255;
356                         }
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)
359                         break;
360                 default:
361                         Host_Error("Mod_IDSP_Load: unknown texFormat (%i, should be 0, 1, 2, or 3)", i);
362                         return;
363                 }
364
365                 Mod_Sprite_SharedSetup(datapointer, LittleLong (pinhlsprite->version), (unsigned int *)(&palette[0][0]), rendermode == SPRHL_ADDITIVE);
366         }
367         else
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);
370
371         loadmodel->surfmesh.isanimated = loadmodel->numframes > 1 || loadmodel->animscenes[0].framecount > 1;
372 }
373
374
375 void Mod_IDS2_Load(dp_model_t *mod, void *buffer, void *bufferend)
376 {
377         int i, version;
378         qboolean fullbright;
379         const dsprite2_t *pinqsprite;
380         skinframe_t *skinframe;
381         float modelradius;
382         int texflags = (r_mipsprites.integer ? TEXF_MIPMAP : 0) | TEXF_ISSPRITE | TEXF_PICMIP | TEXF_COMPRESS | TEXF_ALPHA | TEXF_CLAMP;
383
384         loadmodel->modeldatatypestring = "SPR2";
385
386         loadmodel->type = mod_sprite;
387
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;
395
396         pinqsprite = (dsprite2_t *)buffer;
397
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);
401
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;
407
408         // LordHavoc: hack to allow sprites to be non-fullbright
409         fullbright = true;
410         for (i = 0;i < MAX_QPATH && loadmodel->name[i];i++)
411                 if (loadmodel->name[i] == '!')
412                         fullbright = false;
413
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);
419
420         modelradius = 0;
421         for (i = 0;i < loadmodel->numframes;i++)
422         {
423                 int origin[2], x, y, width, height;
424                 const dsprite2frame_t *pinframe;
425                 mspriteframe_t *sprframe;
426
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;
432
433                 pinframe = &pinqsprite->frames[i];
434
435                 origin[0] = LittleLong (pinframe->origin_x);
436                 origin[1] = LittleLong (pinframe->origin_y);
437                 width = LittleLong (pinframe->width);
438                 height = LittleLong (pinframe->height);
439
440                 sprframe = &loadmodel->sprite.sprdata_frames[i];
441
442                 // note that sp2 origin[0] is positive, where as it is negative in
443                 // spr/spr32/hlspr
444                 sprframe->left = -origin[0];
445                 sprframe->right = -origin[0] + width;
446                 sprframe->up = origin[1];
447                 sprframe->down = origin[1] - height;
448
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)
452                         modelradius = x + y;
453         }
454
455         if (cls.state != ca_dedicated)
456         {
457                 for (i = 0;i < loadmodel->numframes;i++)
458                 {
459                         const dsprite2frame_t *pinframe;
460                         pinframe = &pinqsprite->frames[i];
461                         if (!(skinframe = R_SkinFrame_LoadExternal(pinframe->name, texflags, false)))
462                         {
463                                 Con_Printf("Mod_IDS2_Load: failed to load %s", pinframe->name);
464                                 skinframe = R_SkinFrame_LoadMissing();
465                         }
466                         Mod_SpriteSetupTexture(&loadmodel->data_textures[i], skinframe, fullbright, false);
467                 }
468         }
469
470         modelradius = sqrt(modelradius);
471         for (i = 0;i < 3;i++)
472         {
473                 loadmodel->normalmins[i] = loadmodel->yawmins[i] = loadmodel->rotatedmins[i] = -modelradius;
474                 loadmodel->normalmaxs[i] = loadmodel->yawmaxs[i] = loadmodel->rotatedmaxs[i] = modelradius;
475         }
476         loadmodel->radius = modelradius;
477         loadmodel->radius2 = modelradius * modelradius;
478
479         loadmodel->surfmesh.isanimated = loadmodel->numframes > 1 || loadmodel->animscenes[0].framecount > 1;
480 }