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