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