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