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