replaced Mod_LoadSkinFrame functions with R_SkinFrame system, this
[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", "mipmaps skins (so they become blurrier in the distance), unlike skins the sprites do not have strange border colors"};
29 cvar_t r_picmipsprites = {CVAR_SAVE, "r_picmipsprites", "1", "make gl_picmip affect sprites too (saves some graphics memory in sprite heavy games)"};
30
31 /*
32 ===============
33 Mod_SpriteInit
34 ===============
35 */
36 void Mod_SpriteInit (void)
37 {
38         Cvar_RegisterVariable(&r_mipsprites);
39         Cvar_RegisterVariable(&r_picmipsprites);
40 }
41
42 static void Mod_SpriteSetupTexture(mspriteframe_t *frame, qboolean fullbright, qboolean additive)
43 {
44         texture_t *texture = &frame->texture;
45         texture->basematerialflags = MATERIALFLAG_WALL;
46         if (fullbright)
47                 texture->basematerialflags |= MATERIALFLAG_FULLBRIGHT;
48         if (additive)
49                 texture->basematerialflags |= MATERIALFLAG_ADD | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW;
50         else if (texture->skinframes[0]->fog)
51                 texture->basematerialflags |= MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW;
52         texture->currentmaterialflags = texture->basematerialflags;
53         texture->currentskinframe = texture->skinframes[0];
54 }
55
56 static void Mod_Sprite_SharedSetup(const unsigned char *datapointer, int version, const unsigned int *palette, const unsigned int *alphapalette, qboolean additive)
57 {
58         int                                     i, j, groupframes, realframes, x, y, origin[2], width, height, fullbright;
59         dspriteframetype_t      *pinframetype;
60         dspriteframe_t          *pinframe;
61         dspritegroup_t          *pingroup;
62         dspriteinterval_t       *pinintervals;
63         float                           modelradius, interval;
64         char                            name[MAX_QPATH], fogname[MAX_QPATH];
65         const void                      *startframes;
66         int                 texflags = (r_mipsprites.integer ? TEXF_MIPMAP : 0) | (r_picmipsprites.integer ? TEXF_PICMIP : 0) | TEXF_ALPHA | TEXF_CLAMP | TEXF_PRECACHE;
67         modelradius = 0;
68
69         if (loadmodel->numframes < 1)
70                 Host_Error ("Mod_Sprite_SharedSetup: Invalid # of frames: %d", loadmodel->numframes);
71
72         // LordHavoc: hack to allow sprites to be non-fullbright
73         fullbright = true;
74         for (i = 0;i < MAX_QPATH && loadmodel->name[i];i++)
75                 if (loadmodel->name[i] == '!')
76                         fullbright = false;
77
78 //
79 // load the frames
80 //
81         startframes = datapointer;
82         realframes = 0;
83         for (i = 0;i < loadmodel->numframes;i++)
84         {
85                 pinframetype = (dspriteframetype_t *)datapointer;
86                 datapointer += sizeof(dspriteframetype_t);
87
88                 if (LittleLong (pinframetype->type) == SPR_SINGLE)
89                         groupframes = 1;
90                 else
91                 {
92                         pingroup = (dspritegroup_t *)datapointer;
93                         datapointer += sizeof(dspritegroup_t);
94
95                         groupframes = LittleLong(pingroup->numframes);
96
97                         datapointer += sizeof(dspriteinterval_t) * groupframes;
98                 }
99
100                 for (j = 0;j < groupframes;j++)
101                 {
102                         pinframe = (dspriteframe_t *)datapointer;
103                         if (version == SPRITE32_VERSION)
104                                 datapointer += sizeof(dspriteframe_t) + LittleLong(pinframe->width) * LittleLong(pinframe->height) * 4;
105                         else //if (version == SPRITE_VERSION || version == SPRITEHL_VERSION)
106                                 datapointer += sizeof(dspriteframe_t) + LittleLong(pinframe->width) * LittleLong(pinframe->height);
107                 }
108                 realframes += groupframes;
109         }
110
111         loadmodel->animscenes = (animscene_t *)Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
112         loadmodel->sprite.sprdata_frames = (mspriteframe_t *)Mem_Alloc(loadmodel->mempool, sizeof(mspriteframe_t) * realframes);
113
114         datapointer = (unsigned char *)startframes;
115         realframes = 0;
116         for (i = 0;i < loadmodel->numframes;i++)
117         {
118                 pinframetype = (dspriteframetype_t *)datapointer;
119                 datapointer += sizeof(dspriteframetype_t);
120
121                 if (LittleLong (pinframetype->type) == SPR_SINGLE)
122                 {
123                         groupframes = 1;
124                         interval = 0.1f;
125                 }
126                 else
127                 {
128                         pingroup = (dspritegroup_t *)datapointer;
129                         datapointer += sizeof(dspritegroup_t);
130
131                         groupframes = LittleLong(pingroup->numframes);
132
133                         pinintervals = (dspriteinterval_t *)datapointer;
134                         datapointer += sizeof(dspriteinterval_t) * groupframes;
135
136                         interval = LittleFloat(pinintervals[0].interval);
137                         if (interval < 0.01f)
138                                 Host_Error("Mod_Sprite_SharedSetup: invalid interval");
139                 }
140
141                 sprintf(loadmodel->animscenes[i].name, "frame %i", i);
142                 loadmodel->animscenes[i].firstframe = realframes;
143                 loadmodel->animscenes[i].framecount = groupframes;
144                 loadmodel->animscenes[i].framerate = 1.0f / interval;
145                 loadmodel->animscenes[i].loop = true;
146
147                 for (j = 0;j < groupframes;j++)
148                 {
149                         pinframe = (dspriteframe_t *)datapointer;
150                         datapointer += sizeof(dspriteframe_t);
151
152                         origin[0] = LittleLong (pinframe->origin[0]);
153                         origin[1] = LittleLong (pinframe->origin[1]);
154                         width = LittleLong (pinframe->width);
155                         height = LittleLong (pinframe->height);
156
157                         loadmodel->sprite.sprdata_frames[realframes].left = origin[0];
158                         loadmodel->sprite.sprdata_frames[realframes].right = origin[0] + width;
159                         loadmodel->sprite.sprdata_frames[realframes].up = origin[1];
160                         loadmodel->sprite.sprdata_frames[realframes].down = origin[1] - height;
161
162                         x = (int)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);
163                         y = (int)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);
164                         if (modelradius < x + y)
165                                 modelradius = x + y;
166
167                         if (width > 0 && height > 0 && cls.state != ca_dedicated)
168                         {
169                                 if (groupframes > 1)
170                                         sprintf (name, "%s_%i_%i", loadmodel->name, i, j);
171                                 else
172                                         sprintf (name, "%s_%i", loadmodel->name, i);
173                                 if (!(loadmodel->sprite.sprdata_frames[realframes].texture.skinframes[0] = R_SkinFrame_LoadExternal(name, texflags)))
174                                 {
175                                         if (groupframes > 1)
176                                                 sprintf (fogname, "%s_%i_%ifog", loadmodel->name, i, j);
177                                         else
178                                                 sprintf (fogname, "%s_%ifog", loadmodel->name, i);
179                                         if (version == SPRITE32_VERSION)
180                                                 loadmodel->sprite.sprdata_frames[realframes].texture.skinframes[0] = R_SkinFrame_LoadInternal(name, texflags, false, false, datapointer, width, height, 32, NULL, NULL);
181                                         else //if (version == SPRITE_VERSION || version == SPRITEHL_VERSION)
182                                                 loadmodel->sprite.sprdata_frames[realframes].texture.skinframes[0] = R_SkinFrame_LoadInternal(name, texflags, false, false, datapointer, width, height, 8, palette, alphapalette);
183                                 }
184                         }
185
186                         Mod_SpriteSetupTexture(&loadmodel->sprite.sprdata_frames[realframes], fullbright, additive);
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 unsigned char *datapointer;
211
212         datapointer = (unsigned char *)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 = (synctype_t)LittleLong (pinqsprite->synctype);
234
235                 Mod_Sprite_SharedSetup(datapointer, LittleLong (pinqsprite->version), NULL, NULL, false);
236         }
237         else if (version == SPRITEHL_VERSION)
238         {
239                 int i, rendermode;
240                 unsigned char palette[256][4], alphapalette[256][4];
241                 const unsigned char *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 = (synctype_t)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                         // also passes additive == true to Mod_Sprite_SharedSetup
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                         // should this use alpha test or alpha blend?  (currently blend)
300                         break;
301                 default:
302                         Host_Error("Mod_IDSP_Load: unknown texFormat (%i, should be 0, 1, 2, or 3)", i);
303                         return;
304                 }
305
306                 for (i = 0;i < 256;i++)
307                 {
308                         alphapalette[i][0] = 255;
309                         alphapalette[i][1] = 255;
310                         alphapalette[i][2] = 255;
311                         alphapalette[i][3] = palette[i][3];
312                 }
313
314                 Mod_Sprite_SharedSetup(datapointer, LittleLong (pinhlsprite->version), (unsigned int *)(&palette[0][0]), (unsigned int *)(&alphapalette[0][0]), rendermode == SPRHL_ADDITIVE);
315         }
316         else
317                 Host_Error("Mod_IDSP_Load: %s has wrong version number (%i). Only %i (quake), %i (HalfLife), and %i (sprite32) supported",
318                                         loadmodel->name, version, SPRITE_VERSION, SPRITEHL_VERSION, SPRITE32_VERSION);
319 }
320
321
322 void Mod_IDS2_Load(model_t *mod, void *buffer, void *bufferend)
323 {
324         int i, version, fullbright;
325         const dsprite2_t *pinqsprite;
326         float modelradius;
327         int texflags = (r_mipsprites.integer ? TEXF_MIPMAP : 0) | (r_picmipsprites.integer ? TEXF_PICMIP : 0) | TEXF_ALPHA | TEXF_CLAMP | TEXF_PRECACHE;
328
329         loadmodel->type = mod_sprite;
330
331         loadmodel->DrawSky = NULL;
332         loadmodel->Draw = R_Model_Sprite_Draw;
333         loadmodel->CompileShadowVolume = NULL;
334         loadmodel->DrawShadowVolume = NULL;
335         loadmodel->DrawLight = NULL;
336
337         pinqsprite = (dsprite2_t *)buffer;
338
339         version = LittleLong(pinqsprite->version);
340         if (version != SPRITE2_VERSION)
341                 Host_Error("Mod_IDS2_Load: %s has wrong version number (%i should be 2 (quake 2)", loadmodel->name, version);
342
343         loadmodel->numframes = LittleLong (pinqsprite->numframes);
344         if (loadmodel->numframes < 1)
345                 Host_Error ("Mod_IDS2_Load: Invalid # of frames: %d", loadmodel->numframes);
346         loadmodel->sprite.sprnum_type = SPR_VP_PARALLEL;
347         loadmodel->synctype = ST_SYNC;
348
349         // LordHavoc: hack to allow sprites to be non-fullbright
350         fullbright = true;
351         for (i = 0;i < MAX_QPATH && loadmodel->name[i];i++)
352                 if (loadmodel->name[i] == '!')
353                         fullbright = false;
354
355         loadmodel->animscenes = (animscene_t *)Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
356         loadmodel->sprite.sprdata_frames = (mspriteframe_t *)Mem_Alloc(loadmodel->mempool, sizeof(mspriteframe_t) * loadmodel->numframes);
357
358         modelradius = 0;
359         for (i = 0;i < loadmodel->numframes;i++)
360         {
361                 int origin[2], x, y, width, height;
362                 const dsprite2frame_t *pinframe;
363                 mspriteframe_t *sprframe;
364
365                 dpsnprintf(loadmodel->animscenes[i].name, sizeof(loadmodel->animscenes[i].name), "frame %i", i);
366                 loadmodel->animscenes[i].firstframe = i;
367                 loadmodel->animscenes[i].framecount = 1;
368                 loadmodel->animscenes[i].framerate = 10;
369                 loadmodel->animscenes[i].loop = true;
370
371                 pinframe = &pinqsprite->frames[i];
372
373                 origin[0] = LittleLong (pinframe->origin_x);
374                 origin[1] = LittleLong (pinframe->origin_y);
375                 width = LittleLong (pinframe->width);
376                 height = LittleLong (pinframe->height);
377
378                 sprframe = &loadmodel->sprite.sprdata_frames[i];
379
380                 // note that sp2 origin[0] is positive, where as it is negative in
381                 // spr/spr32/hlspr
382                 sprframe->left = -origin[0];
383                 sprframe->right = -origin[0] + width;
384                 sprframe->up = origin[1];
385                 sprframe->down = origin[1] - height;
386
387                 x = (int)max(sprframe->left * sprframe->left, sprframe->right * sprframe->right);
388                 y = (int)max(sprframe->up * sprframe->up, sprframe->down * sprframe->down);
389                 if (modelradius < x + y)
390                         modelradius = x + y;
391
392                 if (width > 0 && height > 0 && cls.state != ca_dedicated)
393                 {
394                         if (!(sprframe->texture.skinframes[0] = R_SkinFrame_LoadExternal(pinframe->name, texflags)))
395                         {
396                                 Con_Printf("Mod_IDS2_Load: failed to load %s", pinframe->name);
397                                 sprframe->texture.skinframes[0] = R_SkinFrame_LoadExternal("missing", TEXF_PRECACHE);
398                         }
399                 }
400
401                 Mod_SpriteSetupTexture(sprframe, fullbright, false);
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 }