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