]> git.xonotic.org Git - xonotic/darkplaces.git/blob - model_sprite.c
ec41b6e4dbe1acc9775c7d52e529c0af629a7313
[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 unsigned int *palette, const unsigned 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, void *buffer, void *bufferend)
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->CompileShadowVolume = NULL;
220         loadmodel->DrawShadowVolume = NULL;
221         loadmodel->DrawLight = NULL;
222
223         version = LittleLong(((dsprite_t *)buffer)->version);
224         if (version == SPRITE_VERSION || version == SPRITE32_VERSION)
225         {
226                 dsprite_t *pinqsprite;
227
228                 pinqsprite = (dsprite_t *)datapointer;
229                 datapointer += sizeof(dsprite_t);
230
231                 loadmodel->numframes = LittleLong (pinqsprite->numframes);
232                 loadmodel->sprite.sprnum_type = LittleLong (pinqsprite->type);
233                 loadmodel->synctype = LittleLong (pinqsprite->synctype);
234
235                 Mod_Sprite_SharedSetup(datapointer, LittleLong (pinqsprite->version), palette_complete, palette_alpha);
236         }
237         else if (version == SPRITEHL_VERSION)
238         {
239                 int i, rendermode;
240                 qbyte palette[256][4], alphapalette[256][4];
241                 const qbyte *in;
242                 dspritehl_t *pinhlsprite;
243
244                 pinhlsprite = (dspritehl_t *)datapointer;
245                 datapointer += sizeof(dspritehl_t);
246
247                 loadmodel->numframes = LittleLong (pinhlsprite->numframes);
248                 loadmodel->sprite.sprnum_type = LittleLong (pinhlsprite->type);
249                 loadmodel->synctype = LittleLong (pinhlsprite->synctype);
250                 rendermode = pinhlsprite->rendermode;
251
252                 in = datapointer;
253                 datapointer += 2;
254                 i = in[0] + in[1] * 256;
255                 if (i != 256)
256                         Host_Error ("Mod_IDSP_Load: unexpected number of palette colors %i (should be 256)", i);
257                 in = datapointer;
258                 datapointer += 768;
259                 switch(rendermode)
260                 {
261                 case SPRHL_OPAQUE:
262                         for (i = 0;i < 256;i++)
263                         {
264                                 palette[i][0] = *in++;
265                                 palette[i][1] = *in++;
266                                 palette[i][2] = *in++;
267                                 palette[i][3] = 255;
268                         }
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->flags2 |= EF_ADDITIVE;
279                         break;
280                 case SPRHL_INDEXALPHA:
281                         for (i = 0;i < 256;i++)
282                         {
283                                 palette[i][0] = in[765];
284                                 palette[i][1] = in[766];
285                                 palette[i][2] = in[767];
286                                 palette[i][3] = i;
287                                 in += 3;
288                         }
289                         break;
290                 case SPRHL_ALPHATEST:
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                         palette[255][0] = palette[255][1] = palette[255][2] = palette[255][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), (unsigned int *)(&palette[0][0]), (unsigned int *)(&alphapalette[0][0]));
314         }
315         else
316                 Host_Error("Mod_IDSP_Load: %s has wrong version number (%i). Only %i (quake), %i (HalfLife), and %i (sprite32) supported",
317                                         loadmodel->name, version, SPRITE_VERSION, SPRITEHL_VERSION, SPRITE32_VERSION);
318 }
319
320
321 void Mod_IDS2_Load(model_t *mod, void *buffer, void *bufferend)
322 {
323         int i, version;
324         const dsprite2_t *pinqsprite;
325         float modelradius;
326
327         loadmodel->type = mod_sprite;
328         loadmodel->flags2 = EF_FULLBRIGHT;
329
330         loadmodel->DrawSky = NULL;
331         loadmodel->Draw = R_Model_Sprite_Draw;
332         loadmodel->CompileShadowVolume = NULL;
333         loadmodel->DrawShadowVolume = NULL;
334         loadmodel->DrawLight = NULL;
335
336         pinqsprite = (dsprite2_t *)buffer;
337
338         version = LittleLong(pinqsprite->version);
339         if (version != SPRITE2_VERSION)
340                 Host_Error("Mod_IDS2_Load: %s has wrong version number (%i should be 2 (quake 2)", loadmodel->name, version);
341
342         loadmodel->numframes = LittleLong (pinqsprite->numframes);
343         if (loadmodel->numframes < 1)
344                 Host_Error ("Mod_IDS2_Load: Invalid # of frames: %d\n", loadmodel->numframes);
345         loadmodel->sprite.sprnum_type = SPR_VP_PARALLEL;
346         loadmodel->synctype = ST_SYNC;
347
348         // Hack to allow sprites to be non-fullbright
349         for (i = 0;i < MAX_QPATH && loadmodel->name[i];i++)
350         {
351                 if (loadmodel->name[i] == '!')
352                 {
353                         loadmodel->flags2 &= ~EF_FULLBRIGHT;
354                         break;
355                 }
356         }
357
358         loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
359         loadmodel->sprite.sprdata_frames = Mem_Alloc(loadmodel->mempool, sizeof(mspriteframe_t) * loadmodel->numframes);
360
361         modelradius = 0;
362         for (i = 0;i < loadmodel->numframes;i++)
363         {
364                 int origin[2], x, y, width, height;
365                 const dsprite2frame_t *pinframe;
366                 mspriteframe_t *sprframe;
367
368                 dpsnprintf(loadmodel->animscenes[i].name, sizeof(loadmodel->animscenes[i].name), "frame %i", i);
369                 loadmodel->animscenes[i].firstframe = i;
370                 loadmodel->animscenes[i].framecount = 1;
371                 loadmodel->animscenes[i].framerate = 10;
372                 loadmodel->animscenes[i].loop = true;
373
374                 pinframe = &pinqsprite->frames[i];
375
376                 origin[0] = LittleLong (pinframe->origin_x);
377                 origin[1] = LittleLong (pinframe->origin_y);
378                 width = LittleLong (pinframe->width);
379                 height = LittleLong (pinframe->height);
380
381                 sprframe = &loadmodel->sprite.sprdata_frames[i];
382
383                 sprframe->left = origin[0];
384                 sprframe->right = origin[0] + width;
385                 sprframe->up = origin[1];
386                 sprframe->down = origin[1] - height;
387
388                 x = max(sprframe->left * sprframe->left, sprframe->right * sprframe->right);
389                 y = max(sprframe->up * sprframe->up, sprframe->down * sprframe->down);
390                 if (modelradius < x + y)
391                         modelradius = x + y;
392
393                 if (width > 0 && height > 0)
394                 {
395                         sprframe->texture = loadtextureimagewithmask(loadmodel->texturepool, pinframe->name, 0, 0, false, (r_mipsprites.integer ? TEXF_MIPMAP : 0) | TEXF_ALPHA | TEXF_CLAMP | TEXF_PRECACHE | TEXF_PICMIP);
396                         sprframe->fogtexture = image_masktex;
397
398                         // TODO: use a default texture if we can't load it?
399                         if (sprframe->texture == NULL)
400                                 Host_Error("Mod_IDS2_Load: failed to load %s", pinframe->name);
401                 }
402         }
403
404         modelradius = sqrt(modelradius);
405         for (i = 0;i < 3;i++)
406         {
407                 loadmodel->normalmins[i] = loadmodel->yawmins[i] = loadmodel->rotatedmins[i] = -modelradius;
408                 loadmodel->normalmaxs[i] = loadmodel->yawmaxs[i] = loadmodel->rotatedmaxs[i] = modelradius;
409         }
410         loadmodel->radius = modelradius;
411         loadmodel->radius2 = modelradius * modelradius;
412 }