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