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