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