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