2 Copyright (C) 1996-1997 Id Software, Inc.
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.
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.
13 See the GNU General Public License for more details.
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.
20 // models.c -- model loading and caching
22 // models are the only shared resource between a client and server running
23 // on the same machine.
28 cvar_t r_mipsprites = {CVAR_SAVE, "r_mipsprites", "1"};
35 void Mod_SpriteInit (void)
37 Cvar_RegisterVariable(&r_mipsprites);
40 static int alphaonlytable[4] = {255 | 0x80000000, 255 | 0x80000000, 255 | 0x80000000, 3};
41 static void Mod_Sprite_SharedSetup(qbyte *datapointer, int version, int *palette, int *alphapalette)
43 int i, j, groupframes, realframes, x, y, origin[2], width, height;
44 dspriteframetype_t *pinframetype;
45 dspriteframe_t *pinframe;
46 dspritegroup_t *pingroup;
47 dspriteinterval_t *pinintervals;
48 float modelradius, interval;
49 char name[MAX_QPATH], fogname[MAX_QPATH];
54 if (loadmodel->numframes < 1)
55 Host_Error ("Mod_Sprite_SharedSetup: Invalid # of frames: %d\n", loadmodel->numframes);
57 loadmodel->type = mod_sprite;
58 loadmodel->flags2 = EF_FULLBRIGHT;
60 // LordHavoc: hack to allow sprites to be non-fullbright
61 for (i = 0;i < MAX_QPATH && loadmodel->name[i];i++)
63 if (loadmodel->name[i] == '!')
65 loadmodel->flags2 &= ~EF_FULLBRIGHT;
70 // LordHavoc: 32bit textures
71 if (version != SPRITE_VERSION && version != SPRITE32_VERSION && version != SPRITEHL_VERSION)
72 Host_Error("Mod_Sprite_SharedSetup: unsupported version %i, only %i (quake), %i (HalfLife), and %i (sprite32) supported", version, SPRITE_VERSION, SPRITEHL_VERSION, SPRITE32_VERSION);
77 startframes = datapointer;
79 for (i = 0;i < loadmodel->numframes;i++)
81 pinframetype = (dspriteframetype_t *)datapointer;
82 datapointer += sizeof(dspriteframetype_t);
84 if (LittleLong (pinframetype->type) == SPR_SINGLE)
88 pingroup = (dspritegroup_t *)datapointer;
89 datapointer += sizeof(dspritegroup_t);
91 groupframes = LittleLong(pingroup->numframes);
93 datapointer += sizeof(dspriteinterval_t) * groupframes;
96 for (j = 0;j < groupframes;j++)
98 pinframe = (dspriteframe_t *)datapointer;
99 if (version == SPRITE32_VERSION)
100 datapointer += sizeof(dspriteframe_t) + LittleLong(pinframe->width) * LittleLong(pinframe->height) * 4;
101 else //if (version == SPRITE_VERSION || version == SPRITEHL_VERSION)
102 datapointer += sizeof(dspriteframe_t) + LittleLong(pinframe->width) * LittleLong(pinframe->height);
104 realframes += groupframes;
107 loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
108 loadmodel->sprite.sprdata_frames = Mem_Alloc(loadmodel->mempool, sizeof(mspriteframe_t) * realframes);
110 datapointer = startframes;
112 for (i = 0;i < loadmodel->numframes;i++)
114 pinframetype = (dspriteframetype_t *)datapointer;
115 datapointer += sizeof(dspriteframetype_t);
117 if (LittleLong (pinframetype->type) == SPR_SINGLE)
124 pingroup = (dspritegroup_t *)datapointer;
125 datapointer += sizeof(dspritegroup_t);
127 groupframes = LittleLong(pingroup->numframes);
129 pinintervals = (dspriteinterval_t *)datapointer;
130 datapointer += sizeof(dspriteinterval_t) * groupframes;
132 interval = LittleFloat(pinintervals[0].interval);
133 if (interval < 0.01f)
134 Host_Error("Mod_Sprite_SharedSetup: invalid interval");
137 sprintf(loadmodel->animscenes[i].name, "frame %i", i);
138 loadmodel->animscenes[i].firstframe = realframes;
139 loadmodel->animscenes[i].framecount = groupframes;
140 loadmodel->animscenes[i].framerate = 1.0f / interval;
141 loadmodel->animscenes[i].loop = true;
143 for (j = 0;j < groupframes;j++)
145 pinframe = (dspriteframe_t *)datapointer;
146 datapointer += sizeof(dspriteframe_t);
148 origin[0] = LittleLong (pinframe->origin[0]);
149 origin[1] = LittleLong (pinframe->origin[1]);
150 width = LittleLong (pinframe->width);
151 height = LittleLong (pinframe->height);
153 loadmodel->sprite.sprdata_frames[realframes].left = origin[0];
154 loadmodel->sprite.sprdata_frames[realframes].right = origin[0] + width;
155 loadmodel->sprite.sprdata_frames[realframes].up = origin[1];
156 loadmodel->sprite.sprdata_frames[realframes].down = origin[1] - height;
158 x = 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);
159 y = 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);
160 if (modelradius < x + y)
163 if (width > 0 && height > 0)
166 sprintf (name, "%s_%i_%i", loadmodel->name, i, j);
168 sprintf (name, "%s_%i", loadmodel->name, i);
169 loadmodel->sprite.sprdata_frames[realframes].texture = loadtextureimagewithmask(loadmodel->texturepool, name, 0, 0, false, (r_mipsprites.integer ? TEXF_MIPMAP : 0) | TEXF_ALPHA | TEXF_CLAMP | TEXF_PRECACHE | TEXF_PICMIP);
170 loadmodel->sprite.sprdata_frames[realframes].fogtexture = image_masktex;
172 if (!loadmodel->sprite.sprdata_frames[realframes].texture)
175 sprintf (fogname, "%s_%i_%ifog", loadmodel->name, i, j);
177 sprintf (fogname, "%s_%ifog", loadmodel->name, i);
178 if (version == SPRITE32_VERSION)
180 loadmodel->sprite.sprdata_frames[realframes].texture = R_LoadTexture2D(loadmodel->texturepool, name, width, height, datapointer, TEXTYPE_RGBA, TEXF_ALPHA | (r_mipsprites.integer ? TEXF_MIPMAP : 0) | TEXF_CLAMP | TEXF_PRECACHE | TEXF_PICMIP, NULL);
181 // make fog version (just alpha)
182 pixbuf = Mem_Alloc(tempmempool, width*height*4);
183 Image_CopyMux(pixbuf, datapointer, width, height, false, false, false, 4, 4, alphaonlytable);
184 loadmodel->sprite.sprdata_frames[realframes].fogtexture = R_LoadTexture2D(loadmodel->texturepool, fogname, width, height, pixbuf, TEXTYPE_RGBA, TEXF_ALPHA | (r_mipsprites.integer ? TEXF_MIPMAP : 0) | TEXF_CLAMP | TEXF_PRECACHE | TEXF_PICMIP, NULL);
187 else //if (version == SPRITE_VERSION || version == SPRITEHL_VERSION)
189 loadmodel->sprite.sprdata_frames[realframes].texture = R_LoadTexture2D(loadmodel->texturepool, name, width, height, datapointer, TEXTYPE_PALETTE, TEXF_ALPHA | (r_mipsprites.integer ? TEXF_MIPMAP : 0) | TEXF_CLAMP | TEXF_PRECACHE | TEXF_PICMIP, palette);
190 loadmodel->sprite.sprdata_frames[realframes].fogtexture = R_LoadTexture2D(loadmodel->texturepool, fogname, width, height, datapointer, TEXTYPE_PALETTE, TEXF_ALPHA | (r_mipsprites.integer ? TEXF_MIPMAP : 0) | TEXF_CLAMP | TEXF_PRECACHE | TEXF_PICMIP, alphapalette);
195 if (version == SPRITE32_VERSION)
196 datapointer += width * height * 4;
197 else //if (version == SPRITE_VERSION || version == SPRITEHL_VERSION)
198 datapointer += width * height;
203 modelradius = sqrt(modelradius);
204 for (i = 0;i < 3;i++)
206 loadmodel->normalmins[i] = loadmodel->yawmins[i] = loadmodel->rotatedmins[i] = -modelradius;
207 loadmodel->normalmaxs[i] = loadmodel->yawmaxs[i] = loadmodel->rotatedmaxs[i] = modelradius;
209 loadmodel->radius = modelradius;
210 loadmodel->radius2 = modelradius * modelradius;
213 extern void R_Model_Sprite_Draw(entity_render_t *ent);
214 void Mod_IDSP_Load(model_t *mod, void *buffer)
216 int version, i, rendermode;
217 qbyte palette[256][4], alphapalette[256][4], *in;
218 dsprite_t *pinqsprite;
219 dspritehl_t *pinhlsprite;
222 datapointer = buffer;
224 loadmodel->DrawSky = NULL;
225 loadmodel->Draw = R_Model_Sprite_Draw;
226 loadmodel->DrawShadowVolume = NULL;
227 loadmodel->DrawLight = NULL;
229 version = LittleLong(((dsprite_t *)buffer)->version);
230 if (version == SPRITE_VERSION || SPRITE32_VERSION)
232 pinqsprite = (dsprite_t *)datapointer;
233 datapointer += sizeof(dsprite_t);
235 loadmodel->numframes = LittleLong (pinqsprite->numframes);
236 loadmodel->sprite.sprnum_type = LittleLong (pinqsprite->type);
237 loadmodel->synctype = LittleLong (pinqsprite->synctype);
239 Mod_Sprite_SharedSetup(datapointer, LittleLong (pinqsprite->version), palette_complete, palette_alpha);
241 else if (version == SPRITEHL_VERSION)
243 pinhlsprite = (dspritehl_t *)datapointer;
244 datapointer += sizeof(dspritehl_t);
246 loadmodel->numframes = LittleLong (pinhlsprite->numframes);
247 loadmodel->sprite.sprnum_type = LittleLong (pinhlsprite->type);
248 loadmodel->synctype = LittleLong (pinhlsprite->synctype);
249 rendermode = pinhlsprite->rendermode;
253 i = in[0] + in[1] * 256;
255 Host_Error ("Mod_IDSP_Load: unexpected number of palette colors %i (should be 256)", i);
261 for (i = 0;i < 255;i++)
263 palette[i][0] = *in++;
264 palette[i][1] = *in++;
265 palette[i][2] = *in++;
268 palette[255][0] = palette[255][1] = palette[255][2] = palette[255][3] = 0;
271 for (i = 0;i < 256;i++)
273 palette[i][0] = *in++;
274 palette[i][1] = *in++;
275 palette[i][2] = *in++;
278 loadmodel->flags2 |= EF_ADDITIVE;
280 case SPRHL_INDEXALPHA:
281 for (i = 0;i < 256;i++)
290 case SPRHL_ALPHATEST:
291 for (i = 0;i < 255;i++)
293 palette[i][0] = *in++;
294 palette[i][1] = *in++;
295 palette[i][2] = *in++;
298 palette[0][0] = palette[0][1] = palette[0][2] = palette[0][3] = 0;
301 Host_Error("Mod_IDSP_Load: unknown texFormat (%i, should be 0, 1, 2, or 3)\n", i);
305 for (i = 0;i < 256;i++)
307 alphapalette[i][0] = 255;
308 alphapalette[i][1] = 255;
309 alphapalette[i][2] = 255;
310 alphapalette[i][3] = palette[i][3];
313 Mod_Sprite_SharedSetup(datapointer, LittleLong (pinhlsprite->version), (int *)(&palette[0][0]), (int *)(&alphapalette[0][0]));
316 Host_Error("Mod_IDSP_Load: %s has wrong version number (%i should be 1 (quake) or 32 (sprite32) or 2 (halflife)", loadmodel->name, version);