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.
27 cvar_t r_mipsprites = {CVAR_SAVE, "r_mipsprites", "1"};
34 void Mod_SpriteInit (void)
36 Cvar_RegisterVariable(&r_mipsprites);
39 void Mod_Sprite_StripExtension(char *in, char *out)
42 end = in + strlen(in);
44 if (strcmp(end - 6, ".spr32") == 0)
47 if (strcmp(end - 4, ".spr") == 0)
54 void Mod_Sprite_SharedSetup(qbyte *datapointer, int version, int *palette)
56 int i, j, k, groupframes, realframes, x, y, origin[2], width, height;
57 dspriteframetype_t *pinframetype;
58 dspriteframe_t *pinframe;
59 dspritegroup_t *pingroup;
60 dspriteinterval_t *pinintervals;
61 float modelradius, interval;
62 char tempname[MAX_QPATH], name[MAX_QPATH];
67 Mod_Sprite_StripExtension(loadmodel->name, tempname);
69 if (loadmodel->numframes < 1)
70 Host_Error ("Mod_Sprite_SharedSetup: Invalid # of frames: %d\n", loadmodel->numframes);
72 loadmodel->type = mod_sprite;
73 loadmodel->flags = EF_FULLBRIGHT;
75 // LordHavoc: hack to allow sprites to be non-fullbright
76 for (i = 0;i < MAX_QPATH && loadmodel->name[i];i++)
78 if (loadmodel->name[i] == '!')
80 loadmodel->flags &= ~EF_FULLBRIGHT;
85 // LordHavoc: 32bit textures
86 if (version != SPRITE_VERSION && version != SPRITE32_VERSION && version != SPRITEHL_VERSION)
87 Host_Error("Mod_Sprite_SharedSetup: unsupported version %i, only %i (quake), %i (HalfLife), and %i (sprite32) supported", version, SPRITE_VERSION, SPRITEHL_VERSION, SPRITE32_VERSION);
92 startframes = datapointer;
94 for (i = 0;i < loadmodel->numframes;i++)
96 pinframetype = (dspriteframetype_t *)datapointer;
97 datapointer += sizeof(dspriteframetype_t);
99 if (LittleLong (pinframetype->type) == SPR_SINGLE)
103 pingroup = (dspritegroup_t *)datapointer;
104 datapointer += sizeof(dspritegroup_t);
106 groupframes = LittleLong(pingroup->numframes);
108 datapointer += sizeof(dspriteinterval_t) * groupframes;
111 for (j = 0;j < groupframes;j++)
113 pinframe = (dspriteframe_t *)datapointer;
114 if (version == SPRITE32_VERSION)
115 datapointer += sizeof(dspriteframe_t) + LittleLong(pinframe->width) * LittleLong(pinframe->height) * 4;
116 else //if (version == SPRITE_VERSION || version == SPRITEHL_VERSION)
117 datapointer += sizeof(dspriteframe_t) + LittleLong(pinframe->width) * LittleLong(pinframe->height);
119 realframes += groupframes;
122 loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
123 loadmodel->sprdata_frames = Mem_Alloc(loadmodel->mempool, sizeof(mspriteframe_t) * realframes);
125 datapointer = startframes;
127 for (i = 0;i < loadmodel->numframes;i++)
129 pinframetype = (dspriteframetype_t *)datapointer;
130 datapointer += sizeof(dspriteframetype_t);
132 if (LittleLong (pinframetype->type) == SPR_SINGLE)
139 pingroup = (dspritegroup_t *)datapointer;
140 datapointer += sizeof(dspritegroup_t);
142 groupframes = LittleLong(pingroup->numframes);
144 pinintervals = (dspriteinterval_t *)datapointer;
145 datapointer += sizeof(dspriteinterval_t) * groupframes;
147 interval = LittleFloat(pinintervals[0].interval);
148 if (interval < 0.01f)
149 Host_Error("Mod_Sprite_SharedSetup: invalid interval");
152 sprintf(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;
158 for (j = 0;j < groupframes;j++)
160 pinframe = (dspriteframe_t *)datapointer;
161 datapointer += sizeof(dspriteframe_t);
163 origin[0] = LittleLong (pinframe->origin[0]);
164 origin[1] = LittleLong (pinframe->origin[1]);
165 width = LittleLong (pinframe->width);
166 height = LittleLong (pinframe->height);
168 loadmodel->sprdata_frames[realframes].left = origin[0];
169 loadmodel->sprdata_frames[realframes].right = origin[0] + width;
170 loadmodel->sprdata_frames[realframes].up = origin[1];
171 loadmodel->sprdata_frames[realframes].down = origin[1] - height;
173 x = max(loadmodel->sprdata_frames[realframes].left * loadmodel->sprdata_frames[realframes].left, loadmodel->sprdata_frames[realframes].right * loadmodel->sprdata_frames[realframes].right);
174 y = max(loadmodel->sprdata_frames[realframes].up * loadmodel->sprdata_frames[realframes].up, loadmodel->sprdata_frames[realframes].down * loadmodel->sprdata_frames[realframes].down);
175 if (modelradius < x + y)
178 if (width > 0 && height > 0)
181 sprintf (name, "%s_%i_%i", tempname, i, j);
183 sprintf (name, "%s_%i", tempname, i);
184 loadmodel->sprdata_frames[realframes].texture = loadtextureimagewithmask(loadmodel->texturepool, name, 0, 0, false, r_mipsprites.integer, true);
185 loadmodel->sprdata_frames[realframes].fogtexture = image_masktex;
187 if (!loadmodel->sprdata_frames[realframes].texture)
189 pixbuf = Mem_Alloc(tempmempool, width*height*4);
190 if (version == SPRITE32_VERSION)
191 memcpy(pixbuf, datapointer, width*height*4);
192 else //if (version == SPRITE_VERSION || version == SPRITEHL_VERSION)
193 Image_Copy8bitRGBA(datapointer, pixbuf, width*height, palette);
195 loadmodel->sprdata_frames[realframes].texture = R_LoadTexture (loadmodel->texturepool, name, width, height, pixbuf, TEXTYPE_RGBA, TEXF_ALPHA | (r_mipsprites.integer ? TEXF_MIPMAP : 0) | TEXF_PRECACHE);
197 // make fog version (just alpha)
198 for (k = 0;k < width*height;k++)
205 sprintf (name, "%s_%i_%ifog", tempname, i, j);
207 sprintf (name, "%s_%ifog", tempname, i);
208 loadmodel->sprdata_frames[realframes].fogtexture = R_LoadTexture (loadmodel->texturepool, name, width, height, pixbuf, TEXTYPE_RGBA, TEXF_ALPHA | (r_mipsprites.integer ? TEXF_MIPMAP : 0) | TEXF_PRECACHE);
214 if (version == SPRITE32_VERSION)
215 datapointer += width * height * 4;
216 else //if (version == SPRITE_VERSION || version == SPRITEHL_VERSION)
217 datapointer += width * height;
222 modelradius = sqrt(modelradius);
223 for (i = 0;i < 3;i++)
225 loadmodel->normalmins[i] = loadmodel->yawmins[i] = loadmodel->rotatedmins[i] = -modelradius;
226 loadmodel->normalmaxs[i] = loadmodel->yawmaxs[i] = loadmodel->rotatedmaxs[i] = modelradius;
230 void Mod_Sprite_SERAddEntity(void)
241 void Mod_LoadSpriteModel (model_t *mod, void *buffer)
243 int version, i, rendermode;
244 qbyte palette[256][4], *in;
245 dsprite_t *pinqsprite;
246 dspritehl_t *pinhlsprite;
249 datapointer = buffer;
251 loadmodel->SERAddEntity = Mod_Sprite_SERAddEntity;
252 loadmodel->Draw = R_DrawSpriteModel;
253 loadmodel->DrawSky = NULL;
254 loadmodel->DrawShadow = NULL;
256 version = LittleLong(((dsprite_t *)buffer)->version);
257 if (version == SPRITE_VERSION || SPRITE32_VERSION)
259 pinqsprite = (dsprite_t *)datapointer;
260 datapointer += sizeof(dsprite_t);
262 loadmodel->numframes = LittleLong (pinqsprite->numframes);
263 loadmodel->sprnum_type = LittleLong (pinqsprite->type);
264 loadmodel->synctype = LittleLong (pinqsprite->synctype);
266 Mod_Sprite_SharedSetup(datapointer, LittleLong (pinqsprite->version), d_8to24table);
268 else if (version == SPRITEHL_VERSION)
270 pinhlsprite = (dspritehl_t *)datapointer;
271 datapointer += sizeof(dspritehl_t);
273 loadmodel->numframes = LittleLong (pinhlsprite->numframes);
274 loadmodel->sprnum_type = LittleLong (pinhlsprite->type);
275 loadmodel->synctype = LittleLong (pinhlsprite->synctype);
276 rendermode = pinhlsprite->rendermode;
280 i = in[0] + in[1] * 256;
282 Host_Error ("Mod_LoadHLSprite: unexpected number of palette colors %i (should be 256)", i);
288 for (i = 0;i < 255;i++)
290 palette[i][0] = *in++;
291 palette[i][1] = *in++;
292 palette[i][2] = *in++;
295 palette[255][0] = palette[255][1] = palette[255][2] = palette[255][3] = 0;
298 for (i = 0;i < 256;i++)
300 palette[i][0] = *in++;
301 palette[i][1] = *in++;
302 palette[i][2] = *in++;
305 loadmodel->flags |= EF_ADDITIVE;
307 case SPRHL_INDEXALPHA:
308 for (i = 0;i < 256;i++)
317 case SPRHL_ALPHATEST:
318 for (i = 0;i < 255;i++)
320 palette[i][0] = *in++;
321 palette[i][1] = *in++;
322 palette[i][2] = *in++;
325 palette[0][0] = palette[0][1] = palette[0][2] = palette[0][3] = 0;
328 Host_Error("Mod_LoadHLSprite: unknown texFormat (%i, should be 0, 1, 2, or 3)\n", i);
332 Mod_Sprite_SharedSetup(datapointer, LittleLong (pinhlsprite->version), (int *)(&palette[0][0]));
335 Host_Error ("Mod_LoadSpriteModel: %s has wrong version number (%i should be 1 (quake) or 32 (sprite32) or 2 (halflife)", loadmodel->name, version);