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