6498e4feb63925ad48c2cf3fd045d5524086465d
[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->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         loadmodel->flags2 = EF_FULLBRIGHT;
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->data_textures = (texture_t *)Mem_Alloc(loadmodel->mempool, sizeof(texture_t) * loadmodel->num_textures);
365
366         modelradius = 0;
367         for (i = 0;i < loadmodel->numframes;i++)
368         {
369                 int origin[2], x, y, width, height;
370                 const dsprite2frame_t *pinframe;
371                 mspriteframe_t *sprframe;
372
373                 dpsnprintf(loadmodel->animscenes[i].name, sizeof(loadmodel->animscenes[i].name), "frame %i", i);
374                 loadmodel->animscenes[i].firstframe = i;
375                 loadmodel->animscenes[i].framecount = 1;
376                 loadmodel->animscenes[i].framerate = 10;
377                 loadmodel->animscenes[i].loop = true;
378
379                 pinframe = &pinqsprite->frames[i];
380
381                 origin[0] = LittleLong (pinframe->origin_x);
382                 origin[1] = LittleLong (pinframe->origin_y);
383                 width = LittleLong (pinframe->width);
384                 height = LittleLong (pinframe->height);
385
386                 sprframe = &loadmodel->sprite.sprdata_frames[i];
387
388                 // note that sp2 origin[0] is positive, where as it is negative in
389                 // spr/spr32/hlspr
390                 sprframe->left = -origin[0];
391                 sprframe->right = -origin[0] + width;
392                 sprframe->up = origin[1];
393                 sprframe->down = origin[1] - height;
394
395                 x = (int)max(sprframe->left * sprframe->left, sprframe->right * sprframe->right);
396                 y = (int)max(sprframe->up * sprframe->up, sprframe->down * sprframe->down);
397                 if (modelradius < x + y)
398                         modelradius = x + y;
399
400                 skinframe = NULL;
401                 if (width > 0 && height > 0 && cls.state != ca_dedicated)
402                 {
403                         if (!(skinframe = R_SkinFrame_LoadExternal(pinframe->name, texflags)))
404                         {
405                                 Con_Printf("Mod_IDS2_Load: failed to load %s", pinframe->name);
406                                 skinframe = R_SkinFrame_LoadMissing();
407                         }
408                 }
409                 Mod_SpriteSetupTexture(&loadmodel->data_textures[i], skinframe, fullbright, false);
410         }
411
412         modelradius = sqrt(modelradius);
413         for (i = 0;i < 3;i++)
414         {
415                 loadmodel->normalmins[i] = loadmodel->yawmins[i] = loadmodel->rotatedmins[i] = -modelradius;
416                 loadmodel->normalmaxs[i] = loadmodel->yawmaxs[i] = loadmodel->rotatedmaxs[i] = modelradius;
417         }
418         loadmodel->radius = modelradius;
419         loadmodel->radius2 = modelradius * modelradius;
420 }