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