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