Merge branch 'master' into blub/cmd_unset
[divverent/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_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"};
32
33 /*
34 ===============
35 Mod_SpriteInit
36 ===============
37 */
38 void Mod_SpriteInit (void)
39 {
40         Cvar_RegisterVariable(&r_mipsprites);
41         Cvar_RegisterVariable(&r_picmipsprites);
42         Cvar_RegisterVariable(&r_labelsprites_scale);
43         Cvar_RegisterVariable(&r_labelsprites_roundtopixels);
44 }
45
46 static void Mod_SpriteSetupTexture(texture_t *texture, skinframe_t *skinframe, qboolean fullbright, qboolean additive)
47 {
48         if (!skinframe)
49                 skinframe = R_SkinFrame_LoadMissing();
50         texture->offsetmapping = OFFSETMAPPING_OFF;
51         texture->offsetscale = 1;
52         texture->specularscalemod = 1;
53         texture->specularpowermod = 1;
54         texture->basematerialflags = MATERIALFLAG_WALL;
55         if (fullbright)
56                 texture->basematerialflags |= MATERIALFLAG_FULLBRIGHT;
57         if (additive)
58                 texture->basematerialflags |= MATERIALFLAG_ADD | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW;
59         else if (skinframe->hasalpha)
60                 texture->basematerialflags |= MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW;
61         texture->currentmaterialflags = texture->basematerialflags;
62         texture->numskinframes = 1;
63         texture->currentskinframe = texture->skinframes[0] = skinframe;
64         texture->surfaceflags = 0;
65         texture->supercontents = SUPERCONTENTS_SOLID;
66         if (!(texture->basematerialflags & MATERIALFLAG_BLENDED))
67                 texture->supercontents |= SUPERCONTENTS_OPAQUE;
68 }
69
70 static void Mod_Sprite_SharedSetup(const unsigned char *datapointer, int version, const unsigned int *palette, qboolean additive)
71 {
72         int                                     i, j, groupframes, realframes, x, y, origin[2], width, height;
73         qboolean                        fullbright;
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) | (r_picmipsprites.integer ? TEXF_PICMIP : 0) | TEXF_ALPHA | TEXF_CLAMP;
83         modelradius = 0;
84
85         if (loadmodel->numframes < 1)
86                 Host_Error ("Mod_Sprite_SharedSetup: Invalid # of frames: %d", loadmodel->numframes);
87
88         // LordHavoc: hack to allow sprites to be non-fullbright
89         fullbright = true;
90         for (i = 0;i < MAX_QPATH && loadmodel->name[i];i++)
91                 if (loadmodel->name[i] == '!')
92                         fullbright = false;
93
94 //
95 // load the frames
96 //
97         startframes = datapointer;
98         realframes = 0;
99         for (i = 0;i < loadmodel->numframes;i++)
100         {
101                 pinframetype = (dspriteframetype_t *)datapointer;
102                 datapointer += sizeof(dspriteframetype_t);
103
104                 if (LittleLong (pinframetype->type) == SPR_SINGLE)
105                         groupframes = 1;
106                 else
107                 {
108                         pingroup = (dspritegroup_t *)datapointer;
109                         datapointer += sizeof(dspritegroup_t);
110
111                         groupframes = LittleLong(pingroup->numframes);
112
113                         datapointer += sizeof(dspriteinterval_t) * groupframes;
114                 }
115
116                 for (j = 0;j < groupframes;j++)
117                 {
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);
123                 }
124                 realframes += groupframes;
125         }
126
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);
132
133         datapointer = (unsigned char *)startframes;
134         realframes = 0;
135         for (i = 0;i < loadmodel->numframes;i++)
136         {
137                 pinframetype = (dspriteframetype_t *)datapointer;
138                 datapointer += sizeof(dspriteframetype_t);
139
140                 if (LittleLong (pinframetype->type) == SPR_SINGLE)
141                 {
142                         groupframes = 1;
143                         interval = 0.1f;
144                 }
145                 else
146                 {
147                         pingroup = (dspritegroup_t *)datapointer;
148                         datapointer += sizeof(dspritegroup_t);
149
150                         groupframes = LittleLong(pingroup->numframes);
151
152                         pinintervals = (dspriteinterval_t *)datapointer;
153                         datapointer += sizeof(dspriteinterval_t) * groupframes;
154
155                         interval = LittleFloat(pinintervals[0].interval);
156                         if (interval < 0.01f)
157                                 Host_Error("Mod_Sprite_SharedSetup: invalid interval");
158                 }
159
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;
165
166                 for (j = 0;j < groupframes;j++)
167                 {
168                         pinframe = (dspriteframe_t *)datapointer;
169                         datapointer += sizeof(dspriteframe_t);
170
171                         origin[0] = LittleLong (pinframe->origin[0]);
172                         origin[1] = LittleLong (pinframe->origin[1]);
173                         width = LittleLong (pinframe->width);
174                         height = LittleLong (pinframe->height);
175
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;
180
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)
184                                 modelradius = x + y;
185
186                         if (cls.state != ca_dedicated)
187                         {
188                                 skinframe = NULL;
189                                 // note: Nehahra's null.spr has width == 0 and height == 0
190                                 if (width > 0 && height > 0)
191                                 {
192                                         if (groupframes > 1)
193                                         {
194                                                 dpsnprintf (name, sizeof(name), "%s_%i_%i", loadmodel->name, i, j);
195                                                 dpsnprintf (fogname, sizeof(fogname), "%s_%i_%ifog", loadmodel->name, i, j);
196                                         }
197                                         else
198                                         {
199                                                 dpsnprintf (name, sizeof(name), "%s_%i", loadmodel->name, i);
200                                                 dpsnprintf (fogname, sizeof(fogname), "%s_%ifog", loadmodel->name, i);
201                                         }
202                                         if (!(skinframe = R_SkinFrame_LoadExternal(name, texflags | TEXF_COMPRESS, false)))
203                                         {
204                                                 unsigned char *pixels = (unsigned char *) Mem_Alloc(loadmodel->mempool, width*height*4);
205                                                 if (version == SPRITE32_VERSION)
206                                                 {
207                                                         for (x = 0;x < width*height;x++)
208                                                         {
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];
213                                                         }
214                                                 }
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);
218                                                 Mem_Free(pixels);
219                                         }
220                                 }
221                                 if (skinframe == NULL)
222                                         skinframe = R_SkinFrame_LoadMissing();
223                                 Mod_SpriteSetupTexture(&loadmodel->data_textures[realframes], skinframe, fullbright, additive);
224                         }
225
226                         if (version == SPRITE32_VERSION)
227                                 datapointer += width * height * 4;
228                         else //if (version == SPRITE_VERSION || version == SPRITEHL_VERSION)
229                                 datapointer += width * height;
230                         realframes++;
231                 }
232         }
233
234         modelradius = sqrt(modelradius);
235         for (i = 0;i < 3;i++)
236         {
237                 loadmodel->normalmins[i] = loadmodel->yawmins[i] = loadmodel->rotatedmins[i] = -modelradius;
238                 loadmodel->normalmaxs[i] = loadmodel->yawmaxs[i] = loadmodel->rotatedmaxs[i] = modelradius;
239         }
240         loadmodel->radius = modelradius;
241         loadmodel->radius2 = modelradius * modelradius;
242 }
243
244 extern void R_Model_Sprite_Draw(entity_render_t *ent);
245 void Mod_IDSP_Load(dp_model_t *mod, void *buffer, void *bufferend)
246 {
247         int version;
248         const unsigned char *datapointer;
249
250         datapointer = (unsigned char *)buffer;
251
252         loadmodel->modeldatatypestring = "SPR1";
253
254         loadmodel->type = mod_sprite;
255
256         loadmodel->DrawSky = NULL;
257         loadmodel->Draw = R_Model_Sprite_Draw;
258         loadmodel->DrawDepth = NULL;
259         loadmodel->CompileShadowVolume = NULL;
260         loadmodel->DrawShadowVolume = NULL;
261         loadmodel->DrawLight = NULL;
262         loadmodel->DrawAddWaterPlanes = NULL;
263
264         version = LittleLong(((dsprite_t *)buffer)->version);
265         if (version == SPRITE_VERSION || version == SPRITE32_VERSION)
266         {
267                 dsprite_t *pinqsprite;
268
269                 pinqsprite = (dsprite_t *)datapointer;
270                 datapointer += sizeof(dsprite_t);
271
272                 loadmodel->numframes = LittleLong (pinqsprite->numframes);
273                 loadmodel->sprite.sprnum_type = LittleLong (pinqsprite->type);
274                 loadmodel->synctype = (synctype_t)LittleLong (pinqsprite->synctype);
275
276                 Mod_Sprite_SharedSetup(datapointer, LittleLong (pinqsprite->version), NULL, false);
277         }
278         else if (version == SPRITEHL_VERSION)
279         {
280                 int i, rendermode;
281                 unsigned char palette[256][4];
282                 const unsigned char *in;
283                 dspritehl_t *pinhlsprite;
284
285                 pinhlsprite = (dspritehl_t *)datapointer;
286                 datapointer += sizeof(dspritehl_t);
287
288                 loadmodel->numframes = LittleLong (pinhlsprite->numframes);
289                 loadmodel->sprite.sprnum_type = LittleLong (pinhlsprite->type);
290                 loadmodel->synctype = (synctype_t)LittleLong (pinhlsprite->synctype);
291                 rendermode = pinhlsprite->rendermode;
292
293                 in = datapointer;
294                 datapointer += 2;
295                 i = in[0] + in[1] * 256;
296                 if (i != 256)
297                         Host_Error ("Mod_IDSP_Load: unexpected number of palette colors %i (should be 256)", i);
298                 in = datapointer;
299                 datapointer += 768;
300                 switch(rendermode)
301                 {
302                 case SPRHL_OPAQUE:
303                         for (i = 0;i < 256;i++)
304                         {
305                                 palette[i][2] = in[i*3+0];
306                                 palette[i][1] = in[i*3+1];
307                                 palette[i][0] = in[i*3+2];
308                                 palette[i][3] = 255;
309                         }
310                         break;
311                 case SPRHL_ADDITIVE:
312                         for (i = 0;i < 256;i++)
313                         {
314                                 palette[i][2] = in[i*3+0];
315                                 palette[i][1] = in[i*3+1];
316                                 palette[i][0] = in[i*3+2];
317                                 palette[i][3] = 255;
318                         }
319                         // also passes additive == true to Mod_Sprite_SharedSetup
320                         break;
321                 case SPRHL_INDEXALPHA:
322                         for (i = 0;i < 256;i++)
323                         {
324                                 palette[i][2] = in[765];
325                                 palette[i][1] = in[766];
326                                 palette[i][0] = in[767];
327                                 palette[i][3] = i;
328                                 in += 3;
329                         }
330                         break;
331                 case SPRHL_ALPHATEST:
332                         for (i = 0;i < 256;i++)
333                         {
334                                 palette[i][2] = in[i*3+0];
335                                 palette[i][1] = in[i*3+1];
336                                 palette[i][0] = in[i*3+2];
337                                 palette[i][3] = 255;
338                         }
339                         palette[255][0] = palette[255][1] = palette[255][2] = palette[255][3] = 0;
340                         // should this use alpha test or alpha blend?  (currently blend)
341                         break;
342                 default:
343                         Host_Error("Mod_IDSP_Load: unknown texFormat (%i, should be 0, 1, 2, or 3)", i);
344                         return;
345                 }
346
347                 Mod_Sprite_SharedSetup(datapointer, LittleLong (pinhlsprite->version), (unsigned int *)(&palette[0][0]), rendermode == SPRHL_ADDITIVE);
348         }
349         else
350                 Host_Error("Mod_IDSP_Load: %s has wrong version number (%i). Only %i (quake), %i (HalfLife), and %i (sprite32) supported",
351                                         loadmodel->name, version, SPRITE_VERSION, SPRITEHL_VERSION, SPRITE32_VERSION);
352
353         loadmodel->surfmesh.isanimated = loadmodel->numframes > 1 || loadmodel->animscenes[0].framecount > 1;
354 }
355
356
357 void Mod_IDS2_Load(dp_model_t *mod, void *buffer, void *bufferend)
358 {
359         int i, version;
360         qboolean fullbright;
361         const dsprite2_t *pinqsprite;
362         skinframe_t *skinframe;
363         float modelradius;
364         int texflags = (r_mipsprites.integer ? TEXF_MIPMAP : 0) | (r_picmipsprites.integer ? TEXF_PICMIP : 0) | TEXF_COMPRESS | TEXF_ALPHA | TEXF_CLAMP;
365
366         loadmodel->modeldatatypestring = "SPR2";
367
368         loadmodel->type = mod_sprite;
369
370         loadmodel->DrawSky = NULL;
371         loadmodel->Draw = R_Model_Sprite_Draw;
372         loadmodel->DrawDepth = NULL;
373         loadmodel->CompileShadowVolume = NULL;
374         loadmodel->DrawShadowVolume = NULL;
375         loadmodel->DrawLight = NULL;
376         loadmodel->DrawAddWaterPlanes = NULL;
377
378         pinqsprite = (dsprite2_t *)buffer;
379
380         version = LittleLong(pinqsprite->version);
381         if (version != SPRITE2_VERSION)
382                 Host_Error("Mod_IDS2_Load: %s has wrong version number (%i should be 2 (quake 2)", loadmodel->name, version);
383
384         loadmodel->numframes = LittleLong (pinqsprite->numframes);
385         if (loadmodel->numframes < 1)
386                 Host_Error ("Mod_IDS2_Load: Invalid # of frames: %d", loadmodel->numframes);
387         loadmodel->sprite.sprnum_type = SPR_VP_PARALLEL;
388         loadmodel->synctype = ST_SYNC;
389
390         // LordHavoc: hack to allow sprites to be non-fullbright
391         fullbright = true;
392         for (i = 0;i < MAX_QPATH && loadmodel->name[i];i++)
393                 if (loadmodel->name[i] == '!')
394                         fullbright = false;
395
396         loadmodel->animscenes = (animscene_t *)Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
397         loadmodel->sprite.sprdata_frames = (mspriteframe_t *)Mem_Alloc(loadmodel->mempool, sizeof(mspriteframe_t) * loadmodel->numframes);
398         loadmodel->num_textures = loadmodel->numframes;
399         loadmodel->num_texturesperskin = 1;
400         loadmodel->data_textures = (texture_t *)Mem_Alloc(loadmodel->mempool, sizeof(texture_t) * loadmodel->num_textures);
401
402         modelradius = 0;
403         for (i = 0;i < loadmodel->numframes;i++)
404         {
405                 int origin[2], x, y, width, height;
406                 const dsprite2frame_t *pinframe;
407                 mspriteframe_t *sprframe;
408
409                 dpsnprintf(loadmodel->animscenes[i].name, sizeof(loadmodel->animscenes[i].name), "frame %i", i);
410                 loadmodel->animscenes[i].firstframe = i;
411                 loadmodel->animscenes[i].framecount = 1;
412                 loadmodel->animscenes[i].framerate = 10;
413                 loadmodel->animscenes[i].loop = true;
414
415                 pinframe = &pinqsprite->frames[i];
416
417                 origin[0] = LittleLong (pinframe->origin_x);
418                 origin[1] = LittleLong (pinframe->origin_y);
419                 width = LittleLong (pinframe->width);
420                 height = LittleLong (pinframe->height);
421
422                 sprframe = &loadmodel->sprite.sprdata_frames[i];
423
424                 // note that sp2 origin[0] is positive, where as it is negative in
425                 // spr/spr32/hlspr
426                 sprframe->left = -origin[0];
427                 sprframe->right = -origin[0] + width;
428                 sprframe->up = origin[1];
429                 sprframe->down = origin[1] - height;
430
431                 x = (int)max(sprframe->left * sprframe->left, sprframe->right * sprframe->right);
432                 y = (int)max(sprframe->up * sprframe->up, sprframe->down * sprframe->down);
433                 if (modelradius < x + y)
434                         modelradius = x + y;
435         }
436
437         if (cls.state != ca_dedicated)
438         {
439                 for (i = 0;i < loadmodel->numframes;i++)
440                 {
441                         const dsprite2frame_t *pinframe;
442                         pinframe = &pinqsprite->frames[i];
443                         if (!(skinframe = R_SkinFrame_LoadExternal(pinframe->name, texflags, false)))
444                         {
445                                 Con_Printf("Mod_IDS2_Load: failed to load %s", pinframe->name);
446                                 skinframe = R_SkinFrame_LoadMissing();
447                         }
448                         Mod_SpriteSetupTexture(&loadmodel->data_textures[i], skinframe, fullbright, false);
449                 }
450         }
451
452         modelradius = sqrt(modelradius);
453         for (i = 0;i < 3;i++)
454         {
455                 loadmodel->normalmins[i] = loadmodel->yawmins[i] = loadmodel->rotatedmins[i] = -modelradius;
456                 loadmodel->normalmaxs[i] = loadmodel->yawmaxs[i] = loadmodel->rotatedmaxs[i] = modelradius;
457         }
458         loadmodel->radius = modelradius;
459         loadmodel->radius2 = modelradius * modelradius;
460
461         loadmodel->surfmesh.isanimated = loadmodel->numframes > 1 || loadmodel->animscenes[0].framecount > 1;
462 }