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