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