fixed loading of replacement frame images (it was stripping the .spr extension, weird...
[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"};
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_Sprite_StripExtension(char *in, char *out)
41 {
42         char *end;
43         end = in + strlen(in);
44         if ((end - in) >= 6)
45                 if (strcmp(end - 6, ".spr32") == 0)
46                         end -= 6;
47         if ((end - in) >= 4)
48                 if (strcmp(end - 4, ".spr") == 0)
49                         end -= 4;
50         while (in < end)
51                 *out++ = *in++;
52         *out++ = 0;
53 }
54
55 static int alphaonlytable[4] = {255 | 0x80000000, 255 | 0x80000000, 255 | 0x80000000, 3};
56 static void Mod_Sprite_SharedSetup(qbyte *datapointer, int version, int *palette, int *alphapalette)
57 {
58         int                                     i, j, groupframes, realframes, x, y, origin[2], width, height;
59         dspriteframetype_t      *pinframetype;
60         dspriteframe_t          *pinframe;
61         dspritegroup_t          *pingroup;
62         dspriteinterval_t       *pinintervals;
63         float                           modelradius, interval;
64         char                            name[MAX_QPATH], fogname[MAX_QPATH];
65         qbyte                           *pixbuf;
66         void                            *startframes;
67         modelradius = 0;
68
69         if (loadmodel->numframes < 1)
70                 Host_Error ("Mod_Sprite_SharedSetup: Invalid # of frames: %d\n", loadmodel->numframes);
71
72         loadmodel->type = mod_sprite;
73         loadmodel->flags = EF_FULLBRIGHT;
74
75         // LordHavoc: hack to allow sprites to be non-fullbright
76         for (i = 0;i < MAX_QPATH && loadmodel->name[i];i++)
77         {
78                 if (loadmodel->name[i] == '!')
79                 {
80                         loadmodel->flags &= ~EF_FULLBRIGHT;
81                         break;
82                 }
83         }
84
85         // LordHavoc: 32bit textures
86         if (version != SPRITE_VERSION && version != SPRITE32_VERSION && version != SPRITEHL_VERSION)
87                 Host_Error("Mod_Sprite_SharedSetup: unsupported version %i, only %i (quake), %i (HalfLife), and %i (sprite32) supported", version, SPRITE_VERSION, SPRITEHL_VERSION, SPRITE32_VERSION);
88
89 //
90 // load the frames
91 //
92         startframes = datapointer;
93         realframes = 0;
94         for (i = 0;i < loadmodel->numframes;i++)
95         {
96                 pinframetype = (dspriteframetype_t *)datapointer;
97                 datapointer += sizeof(dspriteframetype_t);
98
99                 if (LittleLong (pinframetype->type) == SPR_SINGLE)
100                         groupframes = 1;
101                 else
102                 {
103                         pingroup = (dspritegroup_t *)datapointer;
104                         datapointer += sizeof(dspritegroup_t);
105
106                         groupframes = LittleLong(pingroup->numframes);
107
108                         datapointer += sizeof(dspriteinterval_t) * groupframes;
109                 }
110
111                 for (j = 0;j < groupframes;j++)
112                 {
113                         pinframe = (dspriteframe_t *)datapointer;
114                         if (version == SPRITE32_VERSION)
115                                 datapointer += sizeof(dspriteframe_t) + LittleLong(pinframe->width) * LittleLong(pinframe->height) * 4;
116                         else //if (version == SPRITE_VERSION || version == SPRITEHL_VERSION)
117                                 datapointer += sizeof(dspriteframe_t) + LittleLong(pinframe->width) * LittleLong(pinframe->height);
118                 }
119                 realframes += groupframes;
120         }
121
122         loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
123         loadmodel->sprite.sprdata_frames = Mem_Alloc(loadmodel->mempool, sizeof(mspriteframe_t) * realframes);
124
125         datapointer = startframes;
126         realframes = 0;
127         for (i = 0;i < loadmodel->numframes;i++)
128         {
129                 pinframetype = (dspriteframetype_t *)datapointer;
130                 datapointer += sizeof(dspriteframetype_t);
131
132                 if (LittleLong (pinframetype->type) == SPR_SINGLE)
133                 {
134                         groupframes = 1;
135                         interval = 0.1f;
136                 }
137                 else
138                 {
139                         pingroup = (dspritegroup_t *)datapointer;
140                         datapointer += sizeof(dspritegroup_t);
141
142                         groupframes = LittleLong(pingroup->numframes);
143
144                         pinintervals = (dspriteinterval_t *)datapointer;
145                         datapointer += sizeof(dspriteinterval_t) * groupframes;
146
147                         interval = LittleFloat(pinintervals[0].interval);
148                         if (interval < 0.01f)
149                                 Host_Error("Mod_Sprite_SharedSetup: invalid interval");
150                 }
151
152                 sprintf(loadmodel->animscenes[i].name, "frame %i", i);
153                 loadmodel->animscenes[i].firstframe = realframes;
154                 loadmodel->animscenes[i].framecount = groupframes;
155                 loadmodel->animscenes[i].framerate = 1.0f / interval;
156                 loadmodel->animscenes[i].loop = true;
157
158                 for (j = 0;j < groupframes;j++)
159                 {
160                         pinframe = (dspriteframe_t *)datapointer;
161                         datapointer += sizeof(dspriteframe_t);
162
163                         origin[0] = LittleLong (pinframe->origin[0]);
164                         origin[1] = LittleLong (pinframe->origin[1]);
165                         width = LittleLong (pinframe->width);
166                         height = LittleLong (pinframe->height);
167
168                         loadmodel->sprite.sprdata_frames[realframes].left = origin[0];
169                         loadmodel->sprite.sprdata_frames[realframes].right = origin[0] + width;
170                         loadmodel->sprite.sprdata_frames[realframes].up = origin[1];
171                         loadmodel->sprite.sprdata_frames[realframes].down = origin[1] - height;
172
173                         x = 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);
174                         y = 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);
175                         if (modelradius < x + y)
176                                 modelradius = x + y;
177
178                         if (width > 0 && height > 0)
179                         {
180                                 if (groupframes > 1)
181                                         sprintf (name, "%s_%i_%i", loadmodel->name, i, j);
182                                 else
183                                         sprintf (name, "%s_%i", loadmodel->name, i);
184                                 loadmodel->sprite.sprdata_frames[realframes].texture = loadtextureimagewithmask(loadmodel->texturepool, name, 0, 0, false, (r_mipsprites.integer ? TEXF_MIPMAP : 0) | TEXF_ALPHA | TEXF_CLAMP | TEXF_PRECACHE);
185                                 loadmodel->sprite.sprdata_frames[realframes].fogtexture = image_masktex;
186
187                                 if (!loadmodel->sprite.sprdata_frames[realframes].texture)
188                                 {
189                                         if (groupframes > 1)
190                                                 sprintf (fogname, "%s_%i_%ifog", loadmodel->name, i, j);
191                                         else
192                                                 sprintf (fogname, "%s_%ifog", loadmodel->name, i);
193                                         if (version == SPRITE32_VERSION)
194                                         {
195                                                 loadmodel->sprite.sprdata_frames[realframes].texture = R_LoadTexture2D(loadmodel->texturepool, name, width, height, datapointer, TEXTYPE_RGBA, TEXF_ALPHA | (r_mipsprites.integer ? TEXF_MIPMAP : 0) | TEXF_CLAMP | TEXF_PRECACHE, NULL);
196                                                 // make fog version (just alpha)
197                                                 pixbuf = Mem_Alloc(tempmempool, width*height*4);
198                                                 Image_CopyMux(pixbuf, datapointer, width, height, false, false, false, 4, 4, alphaonlytable);
199                                                 loadmodel->sprite.sprdata_frames[realframes].fogtexture = R_LoadTexture2D(loadmodel->texturepool, fogname, width, height, pixbuf, TEXTYPE_RGBA, TEXF_ALPHA | (r_mipsprites.integer ? TEXF_MIPMAP : 0) | TEXF_CLAMP | TEXF_PRECACHE, NULL);
200                                                 Mem_Free(pixbuf);
201                                         }
202                                         else //if (version == SPRITE_VERSION || version == SPRITEHL_VERSION)
203                                         {
204                                                 loadmodel->sprite.sprdata_frames[realframes].texture = R_LoadTexture2D(loadmodel->texturepool, name, width, height, datapointer, TEXTYPE_PALETTE, TEXF_ALPHA | (r_mipsprites.integer ? TEXF_MIPMAP : 0) | TEXF_CLAMP | TEXF_PRECACHE, palette);
205                                                 loadmodel->sprite.sprdata_frames[realframes].fogtexture = R_LoadTexture2D(loadmodel->texturepool, fogname, width, height, datapointer, TEXTYPE_PALETTE, TEXF_ALPHA | (r_mipsprites.integer ? TEXF_MIPMAP : 0) | TEXF_CLAMP | TEXF_PRECACHE, alphapalette);
206                                         }
207                                 }
208                         }
209
210                         if (version == SPRITE32_VERSION)
211                                 datapointer += width * height * 4;
212                         else //if (version == SPRITE_VERSION || version == SPRITEHL_VERSION)
213                                 datapointer += width * height;
214                         realframes++;
215                 }
216         }
217
218         modelradius = sqrt(modelradius);
219         for (i = 0;i < 3;i++)
220         {
221                 loadmodel->normalmins[i] = loadmodel->yawmins[i] = loadmodel->rotatedmins[i] = -modelradius;
222                 loadmodel->normalmaxs[i] = loadmodel->yawmaxs[i] = loadmodel->rotatedmaxs[i] = modelradius;
223         }
224         loadmodel->radius = modelradius;
225         loadmodel->radius2 = modelradius * modelradius;
226 }
227
228 extern void R_Model_Sprite_Draw(entity_render_t *ent);
229 void Mod_IDSP_Load(model_t *mod, void *buffer)
230 {
231         int version, i, rendermode;
232         qbyte palette[256][4], alphapalette[256][4], *in;
233         dsprite_t *pinqsprite;
234         dspritehl_t *pinhlsprite;
235         qbyte *datapointer;
236
237         datapointer = buffer;
238
239         loadmodel->DrawSky = NULL;
240         loadmodel->Draw = R_Model_Sprite_Draw;
241         loadmodel->DrawShadowVolume = NULL;
242         loadmodel->DrawLight = NULL;
243
244         version = LittleLong(((dsprite_t *)buffer)->version);
245         if (version == SPRITE_VERSION || SPRITE32_VERSION)
246         {
247                 pinqsprite = (dsprite_t *)datapointer;
248                 datapointer += sizeof(dsprite_t);
249
250                 loadmodel->numframes = LittleLong (pinqsprite->numframes);
251                 loadmodel->sprite.sprnum_type = LittleLong (pinqsprite->type);
252                 loadmodel->synctype = LittleLong (pinqsprite->synctype);
253
254                 Mod_Sprite_SharedSetup(datapointer, LittleLong (pinqsprite->version), palette_complete, palette_alpha);
255         }
256         else if (version == SPRITEHL_VERSION)
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 = 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_NORMAL:
276                         for (i = 0;i < 255;i++)
277                         {
278                                 palette[i][0] = *in++;
279                                 palette[i][1] = *in++;
280                                 palette[i][2] = *in++;
281                                 palette[i][3] = 255;
282                         }
283                         palette[255][0] = palette[255][1] = palette[255][2] = palette[255][3] = 0;
284                         break;
285                 case SPRHL_ADDITIVE:
286                         for (i = 0;i < 256;i++)
287                         {
288                                 palette[i][0] = *in++;
289                                 palette[i][1] = *in++;
290                                 palette[i][2] = *in++;
291                                 palette[i][3] = 255;
292                         }
293                         loadmodel->flags |= EF_ADDITIVE;
294                         break;
295                 case SPRHL_INDEXALPHA:
296                         for (i = 0;i < 256;i++)
297                         {
298                                 palette[i][0] = 255;
299                                 palette[i][1] = 255;
300                                 palette[i][2] = 255;
301                                 palette[i][3] = i;
302                                 in += 3;
303                         }
304                         break;
305                 case SPRHL_ALPHATEST:
306                         for (i = 0;i < 255;i++)
307                         {
308                                 palette[i][0] = *in++;
309                                 palette[i][1] = *in++;
310                                 palette[i][2] = *in++;
311                                 palette[i][3] = 255;
312                         }
313                         palette[0][0] = palette[0][1] = palette[0][2] = palette[0][3] = 0;
314                         break;
315                 default:
316                         Host_Error("Mod_IDSP_Load: unknown texFormat (%i, should be 0, 1, 2, or 3)\n", 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), (int *)(&palette[0][0]), (int *)(&alphapalette[0][0]));
329         }
330         else
331                 Host_Error("Mod_IDSP_Load: %s has wrong version number (%i should be 1 (quake) or 32 (sprite32) or 2 (halflife)", loadmodel->name, version);
332 }
333