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.
23 static cvar_t r_mipskins = {CVAR_SAVE, "r_mipskins", "0"};
25 void Mod_AliasInit (void)
27 Cvar_RegisterVariable(&r_mipskins);
30 static void Mod_Alias_SERAddEntity(void)
32 R_Clip_AddBox(currentrenderentity->mins, currentrenderentity->maxs, R_Entity_Callback, currentrenderentity, NULL);
35 // LordHavoc: proper bounding box considerations
36 static float aliasbboxmin[3], aliasbboxmax[3], modelyawradius, modelradius;
38 static float vertst[MAXALIASVERTS][2];
39 static int vertusage[MAXALIASVERTS];
40 static int vertonseam[MAXALIASVERTS];
41 static int vertremap[MAXALIASVERTS];
42 static int temptris[MAXALIASTRIS][3];
44 static void Mod_ConvertAliasVerts (int inverts, vec3_t scale, vec3_t translate, trivertx_t *v, trivertx_t *out)
46 int i, j, invalidnormals = 0;
49 for (i = 0;i < inverts;i++)
51 if (vertremap[i] < 0 && vertremap[i+inverts] < 0) // only used vertices need apply...
54 temp[0] = v[i].v[0] * scale[0] + translate[0];
55 temp[1] = v[i].v[1] * scale[1] + translate[1];
56 temp[2] = v[i].v[2] * scale[2] + translate[2];
57 // update bounding box
58 if (temp[0] < aliasbboxmin[0]) aliasbboxmin[0] = temp[0];
59 if (temp[1] < aliasbboxmin[1]) aliasbboxmin[1] = temp[1];
60 if (temp[2] < aliasbboxmin[2]) aliasbboxmin[2] = temp[2];
61 if (temp[0] > aliasbboxmax[0]) aliasbboxmax[0] = temp[0];
62 if (temp[1] > aliasbboxmax[1]) aliasbboxmax[1] = temp[1];
63 if (temp[2] > aliasbboxmax[2]) aliasbboxmax[2] = temp[2];
64 dist = temp[0]*temp[0]+temp[1]*temp[1];
65 if (modelyawradius < dist)
66 modelyawradius = dist;
67 dist += temp[2]*temp[2];
68 if (modelradius < dist)
71 j = vertremap[i]; // not onseam
74 VectorCopy(v[i].v, out[j].v);
75 out[j].lightnormalindex = v[i].lightnormalindex;
76 if (out[j].lightnormalindex >= NUMVERTEXNORMALS)
79 out[j].lightnormalindex = 0;
82 j = vertremap[i+inverts]; // onseam
85 VectorCopy(v[i].v, out[j].v);
86 out[j].lightnormalindex = v[i].lightnormalindex;
87 if (out[j].lightnormalindex >= NUMVERTEXNORMALS)
90 out[j].lightnormalindex = 0;
95 Con_Printf("Mod_ConvertAliasVerts: \"%s\", %i invalid normal indices found\n", loadmodel->name, invalidnormals);
98 static void Mod_MDL_LoadFrames (long datapointer, int inverts, int outverts, vec3_t scale, vec3_t translate)
100 daliasframetype_t *pframetype;
101 daliasframe_t *pinframe;
102 daliasgroup_t *group;
103 daliasinterval_t *intervals;
104 int i, f, pose, groupframes;
108 scene = loadmodel->animscenes;
109 for (f = 0;f < loadmodel->numframes;f++)
111 pframetype = (daliasframetype_t *)datapointer;
112 datapointer += sizeof(daliasframetype_t);
113 if (LittleLong (pframetype->type) == ALIAS_SINGLE)
115 // a single frame is still treated as a group
122 group = (daliasgroup_t *)datapointer;
123 datapointer += sizeof(daliasgroup_t);
124 groupframes = LittleLong (group->numframes);
126 // intervals (time per frame)
127 intervals = (daliasinterval_t *)datapointer;
128 datapointer += sizeof(daliasinterval_t) * groupframes;
130 interval = LittleFloat (intervals->interval); // FIXME: support variable framerate groups
131 if (interval < 0.01f)
132 Host_Error("Mod_LoadAliasGroup: invalid interval");
135 // get scene name from first frame
136 pinframe = (daliasframe_t *)datapointer;
138 strcpy(scene->name, pinframe->name);
139 scene->firstframe = pose;
140 scene->framecount = groupframes;
141 scene->framerate = 1.0f / interval;
146 for (i = 0;i < groupframes;i++)
148 pinframe = (daliasframe_t *)datapointer;
149 datapointer += sizeof(daliasframe_t);
151 // convert to MD2 frame headers
152 strcpy(loadmodel->mdlmd2data_frames[pose].name, pinframe->name);
153 VectorCopy(scale, loadmodel->mdlmd2data_frames[pose].scale);
154 VectorCopy(translate, loadmodel->mdlmd2data_frames[pose].translate);
156 Mod_ConvertAliasVerts(inverts, scale, translate, (trivertx_t *)datapointer, loadmodel->mdlmd2data_pose + pose * outverts);
157 datapointer += sizeof(trivertx_t) * inverts;
163 static rtexture_t *GL_SkinSplitShirt(byte *in, byte *out, int width, int height, int bits, char *name, int precache)
165 int i, pixels, passed;
167 for (i = 0;i < 16;i++)
168 pixeltest[i] = (bits & (1 << i)) != 0;
169 pixels = width*height;
173 if (pixeltest[*in >> 4] && *in != 0 && *in != 255)
176 // turn to white while copying
177 if (*in >= 128 && *in < 224) // backwards ranges
178 *out = (*in & 15) ^ 15;
188 return R_LoadTexture (loadmodel->texturepool, name, width, height, out - width*height, TEXTYPE_QPALETTE, (r_mipskins.integer ? TEXF_MIPMAP : 0) | (precache ? TEXF_PRECACHE : 0));
193 static rtexture_t *GL_SkinSplit(byte *in, byte *out, int width, int height, int bits, char *name, int precache)
195 int i, pixels, passed;
197 for (i = 0;i < 16;i++)
198 pixeltest[i] = (bits & (1 << i)) != 0;
199 pixels = width*height;
203 if (pixeltest[*in >> 4] && *in != 0 && *in != 255)
214 return R_LoadTexture (loadmodel->texturepool, name, width, height, out - width*height, TEXTYPE_QPALETTE, (r_mipskins.integer ? TEXF_MIPMAP : 0) | (precache ? TEXF_PRECACHE : 0));
219 static void Mod_LoadSkin (char *basename, byte *skindata, byte *skintemp, int width, int height, skinframe_t *skinframe, int precache)
221 skinframe->base = loadtextureimagewithmask(loadmodel->texturepool, va("%s_normal", basename), 0, 0, false, r_mipskins.integer, precache);
222 skinframe->fog = image_masktex;
223 skinframe->pants = NULL;
224 skinframe->shirt = NULL;
225 skinframe->glow = loadtextureimage(loadmodel->texturepool, va("%s_glow" , basename), 0, 0, false, r_mipskins.integer, precache);
226 skinframe->merged = NULL;
229 skinframe->pants = loadtextureimage(loadmodel->texturepool, va("%s_pants" , basename), 0, 0, false, r_mipskins.integer, precache);
230 skinframe->shirt = loadtextureimage(loadmodel->texturepool, va("%s_shirt" , basename), 0, 0, false, r_mipskins.integer, precache);
234 skinframe->base = loadtextureimagewithmask(loadmodel->texturepool, basename, 0, 0, false, true, true);
235 skinframe->fog = image_masktex;
236 if (!skinframe->base)
238 skinframe->pants = GL_SkinSplitShirt(skindata, skintemp, width, height, 0x0040, va("%s_pants", basename), false); // pants
239 skinframe->shirt = GL_SkinSplitShirt(skindata, skintemp, width, height, 0x0002, va("%s_shirt", basename), false); // shirt
240 skinframe->glow = GL_SkinSplit (skindata, skintemp, width, height, 0xC000, va("%s_glow", basename), precache); // glow
241 if (skinframe->pants || skinframe->shirt)
243 skinframe->base = GL_SkinSplit (skindata, skintemp, width, height, 0x3FBD, va("%s_normal", basename), false); // normal (no special colors)
244 skinframe->merged = GL_SkinSplit (skindata, skintemp, width, height, 0x3FFF, va("%s_body", basename), precache); // body (normal + pants + shirt, but not glow)
247 skinframe->base = GL_SkinSplit (skindata, skintemp, width, height, 0x3FFF, va("%s_base", basename), precache); // no special colors
248 // quake model skins don't have alpha
249 skinframe->fog = NULL;
254 #define BOUNDI(VALUE,MIN,MAX) if (VALUE < MIN || VALUE >= MAX) Host_Error("model %s has an invalid ##VALUE (%d exceeds %d - %d)\n", loadmodel->name, VALUE, MIN, MAX);
255 #define BOUNDF(VALUE,MIN,MAX) if (VALUE < MIN || VALUE >= MAX) Host_Error("model %s has an invalid ##VALUE (%f exceeds %f - %f)\n", loadmodel->name, VALUE, MIN, MAX);
256 void Mod_LoadAliasModel (model_t *mod, void *buffer)
258 int i, j, version, numverts, totalposes, totalskins, skinwidth, skinheight, totalverts, groupframes, groupskins;
260 stvert_t *pinstverts;
261 dtriangle_t *pintriangles;
262 daliasskintype_t *pinskintype;
263 daliasskingroup_t *pinskingroup;
264 daliasskininterval_t *pinskinintervals;
265 daliasframetype_t *pinframetype;
266 daliasgroup_t *pinframegroup;
267 float scales, scalet, scale[3], translate[3], interval;
268 long datapointer, startframes, startskins;
269 char name[MAX_QPATH];
270 byte *skintemp = NULL;
274 datapointer = (long) buffer;
275 pinmodel = (mdl_t *)datapointer;
276 datapointer += sizeof(mdl_t);
278 version = LittleLong (pinmodel->version);
279 if (version != ALIAS_VERSION)
280 Host_Error ("%s has wrong version number (%i should be %i)",
281 loadmodel->name, version, ALIAS_VERSION);
283 loadmodel->type = mod_alias;
284 loadmodel->aliastype = ALIASTYPE_MDL;
286 loadmodel->numskins = LittleLong(pinmodel->numskins);
287 BOUNDI(loadmodel->numskins,0,256);
288 skinwidth = LittleLong (pinmodel->skinwidth);
289 BOUNDI(skinwidth,0,4096);
290 skinheight = LittleLong (pinmodel->skinheight);
291 BOUNDI(skinheight,0,4096);
292 loadmodel->numverts = numverts = LittleLong(pinmodel->numverts);
293 BOUNDI(loadmodel->numverts,0,MAXALIASVERTS);
294 loadmodel->numtris = LittleLong(pinmodel->numtris);
295 BOUNDI(loadmodel->numtris,0,MAXALIASTRIS);
296 loadmodel->numframes = LittleLong(pinmodel->numframes);
297 BOUNDI(loadmodel->numframes,0,65536);
298 loadmodel->synctype = LittleLong (pinmodel->synctype);
299 BOUNDI(loadmodel->synctype,0,2);
300 loadmodel->flags = LittleLong (pinmodel->flags);
302 for (i = 0;i < 3;i++)
304 scale[i] = LittleFloat (pinmodel->scale[i]);
305 translate[i] = LittleFloat (pinmodel->scale_origin[i]);
308 startskins = datapointer;
310 for (i = 0;i < loadmodel->numskins;i++)
312 pinskintype = (daliasskintype_t *)datapointer;
313 datapointer += sizeof(daliasskintype_t);
314 if (LittleLong(pinskintype->type) == ALIAS_SKIN_SINGLE)
318 pinskingroup = (daliasskingroup_t *)datapointer;
319 datapointer += sizeof(daliasskingroup_t);
320 groupskins = LittleLong(pinskingroup->numskins);
321 datapointer += sizeof(daliasskininterval_t) * groupskins;
324 for (j = 0;j < groupskins;j++)
326 datapointer += skinwidth * skinheight;
331 pinstverts = (stvert_t *)datapointer;
332 datapointer += sizeof(stvert_t) * numverts;
334 pintriangles = (dtriangle_t *)datapointer;
335 datapointer += sizeof(dtriangle_t) * loadmodel->numtris;
337 startframes = datapointer;
339 for (i = 0;i < loadmodel->numframes;i++)
341 pinframetype = (daliasframetype_t *)datapointer;
342 datapointer += sizeof(daliasframetype_t);
343 if (LittleLong (pinframetype->type) == ALIAS_SINGLE)
347 pinframegroup = (daliasgroup_t *)datapointer;
348 datapointer += sizeof(daliasgroup_t);
349 groupframes = LittleLong(pinframegroup->numframes);
350 datapointer += sizeof(daliasinterval_t) * groupframes;
353 for (j = 0;j < groupframes;j++)
355 datapointer += sizeof(daliasframe_t);
356 datapointer += sizeof(trivertx_t) * numverts;
362 skintemp = Mem_Alloc(tempmempool, skinwidth * skinheight);
363 loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, loadmodel->numskins * sizeof(animscene_t));
364 loadmodel->skinframes = Mem_Alloc(loadmodel->mempool, totalskins * sizeof(skinframe_t));
366 datapointer = startskins;
367 for (i = 0;i < loadmodel->numskins;i++)
369 pinskintype = (daliasskintype_t *)datapointer;
370 datapointer += sizeof(daliasskintype_t);
372 if (pinskintype->type == ALIAS_SKIN_SINGLE)
379 pinskingroup = (daliasskingroup_t *)datapointer;
380 datapointer += sizeof(daliasskingroup_t);
382 groupskins = LittleLong (pinskingroup->numskins);
384 pinskinintervals = (daliasskininterval_t *)datapointer;
385 datapointer += sizeof(daliasskininterval_t) * groupskins;
387 interval = LittleFloat(pinskinintervals[0].interval);
388 if (interval < 0.01f)
389 Host_Error("Mod_LoadAliasModel: invalid interval\n");
392 sprintf(loadmodel->skinscenes[i].name, "skin %i", i);
393 loadmodel->skinscenes[i].firstframe = totalskins;
394 loadmodel->skinscenes[i].framecount = groupskins;
395 loadmodel->skinscenes[i].framerate = 1.0f / interval;
396 loadmodel->skinscenes[i].loop = true;
398 for (j = 0;j < groupskins;j++)
401 sprintf (name, "%s_%i_%i", loadmodel->name, i, j);
403 sprintf (name, "%s_%i", loadmodel->name, i);
404 Mod_LoadSkin(name, (byte *)datapointer, skintemp, skinwidth, skinheight, loadmodel->skinframes + totalskins, i == 0);
405 datapointer += skinwidth * skinheight;
411 // store texture coordinates into temporary array, they will be stored after usage is determined (triangle data)
412 scales = 1.0 / skinwidth;
413 scalet = 1.0 / skinheight;
414 for (i = 0;i < numverts;i++)
416 vertonseam[i] = LittleLong(pinstverts[i].onseam);
417 vertst[i][0] = LittleLong(pinstverts[i].s) * scales;
418 vertst[i][1] = LittleLong(pinstverts[i].t) * scalet;
419 vertst[i+numverts][0] = vertst[i][0] + 0.5;
420 vertst[i+numverts][1] = vertst[i][1];
422 vertusage[i+numverts] = 0;
425 // load triangle data
426 loadmodel->mdldata_indices = Mem_Alloc(loadmodel->mempool, sizeof(int[3]) * loadmodel->numtris);
428 // count the vertices used
429 for (i = 0;i < numverts*2;i++)
431 for (i = 0;i < loadmodel->numtris;i++)
433 temptris[i][0] = LittleLong(pintriangles[i].vertindex[0]);
434 temptris[i][1] = LittleLong(pintriangles[i].vertindex[1]);
435 temptris[i][2] = LittleLong(pintriangles[i].vertindex[2]);
436 if (!LittleLong(pintriangles[i].facesfront)) // backface
438 if (vertonseam[temptris[i][0]]) temptris[i][0] += numverts;
439 if (vertonseam[temptris[i][1]]) temptris[i][1] += numverts;
440 if (vertonseam[temptris[i][2]]) temptris[i][2] += numverts;
442 vertusage[temptris[i][0]]++;
443 vertusage[temptris[i][1]]++;
444 vertusage[temptris[i][2]]++;
446 // build remapping table and compact array
448 for (i = 0;i < numverts*2;i++)
452 vertremap[i] = totalverts;
453 vertst[totalverts][0] = vertst[i][0];
454 vertst[totalverts][1] = vertst[i][1];
458 vertremap[i] = -1; // not used at all
460 loadmodel->numverts = totalverts;
461 // remap the triangle references
462 for (i = 0;i < loadmodel->numtris;i++)
464 loadmodel->mdldata_indices[i*3+0] = vertremap[temptris[i][0]];
465 loadmodel->mdldata_indices[i*3+1] = vertremap[temptris[i][1]];
466 loadmodel->mdldata_indices[i*3+2] = vertremap[temptris[i][2]];
468 // store the texture coordinates
469 loadmodel->mdldata_texcoords = Mem_Alloc(loadmodel->mempool, sizeof(float[2]) * totalverts);
470 for (i = 0;i < totalverts;i++)
472 loadmodel->mdldata_texcoords[i*2+0] = vertst[i][0];
473 loadmodel->mdldata_texcoords[i*2+1] = vertst[i][1];
477 loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
478 loadmodel->mdlmd2data_frames = Mem_Alloc(loadmodel->mempool, sizeof(md2frame_t) * totalposes);
479 loadmodel->mdlmd2data_pose = Mem_Alloc(loadmodel->mempool, sizeof(trivertx_t) * totalposes * totalverts);
481 // LordHavoc: doing proper bbox for model
482 aliasbboxmin[0] = aliasbboxmin[1] = aliasbboxmin[2] = 1000000000;
483 aliasbboxmax[0] = aliasbboxmax[1] = aliasbboxmax[2] = -1000000000;
485 Mod_MDL_LoadFrames (startframes, numverts, totalverts, scale, translate);
487 // LordHavoc: fixed model bbox - was //FIXME: do this right
488 //loadmodel->mins[0] = loadmodel->mins[1] = loadmodel->mins[2] = -16;
489 //loadmodel->maxs[0] = loadmodel->maxs[1] = loadmodel->maxs[2] = 16;
490 modelyawradius = sqrt(modelyawradius);
491 modelradius = sqrt(modelradius);
492 // loadmodel->modelradius = modelradius;
493 for (j = 0;j < 3;j++)
495 loadmodel->normalmins[j] = aliasbboxmin[j];
496 loadmodel->normalmaxs[j] = aliasbboxmax[j];
497 loadmodel->rotatedmins[j] = -modelradius;
498 loadmodel->rotatedmaxs[j] = modelradius;
500 loadmodel->yawmins[0] = loadmodel->yawmins[1] = -(loadmodel->yawmaxs[0] = loadmodel->yawmaxs[1] = modelyawradius);
501 loadmodel->yawmins[2] = loadmodel->normalmins[2];
502 loadmodel->yawmaxs[2] = loadmodel->normalmaxs[2];
504 loadmodel->SERAddEntity = Mod_Alias_SERAddEntity;
505 loadmodel->Draw = R_DrawAliasModel;
506 loadmodel->DrawSky = NULL;
507 loadmodel->DrawShadow = NULL;
510 static void Mod_MD2_ConvertVerts (int numverts, vec3_t scale, vec3_t translate, trivertx_t *v, trivertx_t *out)
512 int i, invalidnormals = 0;
515 for (i = 0;i < numverts;i++)
517 VectorCopy(v[i].v, out[i].v);
518 temp[0] = v[i].v[0] * scale[0] + translate[0];
519 temp[1] = v[i].v[1] * scale[1] + translate[1];
520 temp[2] = v[i].v[2] * scale[2] + translate[2];
521 // update bounding box
522 if (temp[0] < aliasbboxmin[0]) aliasbboxmin[0] = temp[0];
523 if (temp[1] < aliasbboxmin[1]) aliasbboxmin[1] = temp[1];
524 if (temp[2] < aliasbboxmin[2]) aliasbboxmin[2] = temp[2];
525 if (temp[0] > aliasbboxmax[0]) aliasbboxmax[0] = temp[0];
526 if (temp[1] > aliasbboxmax[1]) aliasbboxmax[1] = temp[1];
527 if (temp[2] > aliasbboxmax[2]) aliasbboxmax[2] = temp[2];
528 dist = temp[0]*temp[0]+temp[1]*temp[1];
529 if (modelyawradius < dist)
530 modelyawradius = dist;
531 dist += temp[2]*temp[2];
532 if (modelradius < dist)
534 out[i].lightnormalindex = v[i].lightnormalindex;
535 if (out[i].lightnormalindex >= NUMVERTEXNORMALS)
538 out[i].lightnormalindex = 0;
542 Con_Printf("Mod_MD2_ConvertVerts: \"%s\", %i invalid normal indices found\n", loadmodel->name, invalidnormals);
545 void Mod_MD2_ReadHeader(md2_t *in, int *numglcmds)
549 version = LittleLong (in->version);
550 if (version != MD2ALIAS_VERSION)
551 Host_Error ("%s has wrong version number (%i should be %i)",
552 loadmodel->name, version, MD2ALIAS_VERSION);
554 loadmodel->type = mod_alias;
555 loadmodel->aliastype = ALIASTYPE_MD2;
556 loadmodel->SERAddEntity = Mod_Alias_SERAddEntity;
557 loadmodel->Draw = R_DrawAliasModel;
558 loadmodel->DrawSky = NULL;
559 loadmodel->DrawShadow = NULL;
561 if (LittleLong(in->num_tris < 1) || LittleLong(in->num_tris) > MD2MAX_TRIANGLES)
562 Host_Error ("%s has invalid number of triangles: %i", loadmodel->name, LittleLong(in->num_tris));
563 if (LittleLong(in->num_xyz < 1) || LittleLong(in->num_xyz) > MD2MAX_VERTS)
564 Host_Error ("%s has invalid number of vertices: %i", loadmodel->name, LittleLong(in->num_xyz));
565 if (LittleLong(in->num_frames < 1) || LittleLong(in->num_frames) > MD2MAX_FRAMES)
566 Host_Error ("%s has invalid number of frames: %i", loadmodel->name, LittleLong(in->num_frames));
567 if (LittleLong(in->num_skins < 0) || LittleLong(in->num_skins) > MAX_SKINS)
568 Host_Error ("%s has invalid number of skins: %i", loadmodel->name, LittleLong(in->num_skins));
570 end = LittleLong(in->ofs_end);
571 if (LittleLong(in->num_skins) >= 1 && (LittleLong(in->ofs_skins <= 0) || LittleLong(in->ofs_skins) >= end))
572 Host_Error ("%s is not a valid model", loadmodel->name);
573 if (LittleLong(in->ofs_st <= 0) || LittleLong(in->ofs_st) >= end)
574 Host_Error ("%s is not a valid model", loadmodel->name);
575 if (LittleLong(in->ofs_tris <= 0) || LittleLong(in->ofs_tris) >= end)
576 Host_Error ("%s is not a valid model", loadmodel->name);
577 if (LittleLong(in->ofs_frames <= 0) || LittleLong(in->ofs_frames) >= end)
578 Host_Error ("%s is not a valid model", loadmodel->name);
579 if (LittleLong(in->ofs_glcmds <= 0) || LittleLong(in->ofs_glcmds) >= end)
580 Host_Error ("%s is not a valid model", loadmodel->name);
582 loadmodel->numskins = LittleLong(in->num_skins);
583 loadmodel->numverts = LittleLong(in->num_xyz);
584 // loadmodel->md2num_st = LittleLong(in->num_st);
585 loadmodel->numtris = LittleLong(in->num_tris);
586 loadmodel->numframes = LittleLong(in->num_frames);
587 *numglcmds = LittleLong(in->num_glcmds);
589 loadmodel->flags = 0; // there are no MD2 flags
590 loadmodel->synctype = ST_RAND;
593 void Mod_MD2_LoadSkins(char *in)
597 if (loadmodel->numskins)
599 loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numskins);
600 loadmodel->skinframes = Mem_Alloc(loadmodel->mempool, sizeof(skinframe_t) * loadmodel->numskins);
601 for (i = 0;i < loadmodel->numskins;i++)
603 loadmodel->skinscenes[i].firstframe = i;
604 loadmodel->skinscenes[i].framecount = 1;
605 loadmodel->skinscenes[i].loop = true;
606 loadmodel->skinscenes[i].framerate = 10;
607 loadmodel->skinframes[i].base = loadtextureimagewithmask (loadmodel->texturepool, in, 0, 0, true, r_mipskins.integer, true);
608 loadmodel->skinframes[i].fog = image_masktex;
609 loadmodel->skinframes[i].pants = NULL;
610 loadmodel->skinframes[i].shirt = NULL;
611 loadmodel->skinframes[i].glow = NULL;
612 loadmodel->skinframes[i].merged = NULL;
613 in += MD2MAX_SKINNAME;
619 void Mod_MD2_LoadTriangles(md2triangle_t *in)
622 loadmodel->md2data_tris = Mem_Alloc(loadmodel->mempool, loadmodel->numtris * sizeof(md2triangle_t));
623 // swap the triangle list
624 for (i = 0;i < loadmodel->numtris;i++)
626 for (j = 0;j < 3;j++)
628 loadmodel->md2data_tris[i].index_xyz[j] = LittleShort (in[i].index_xyz[j]);
629 loadmodel->md2data_tris[i].index_st[j] = LittleShort (in[i].index_st[j]);
630 if (loadmodel->md2data_tris[i].index_xyz[j] >= loadmodel->md2num_xyz)
631 Host_Error ("%s has invalid vertex indices", loadmodel->name);
632 if (loadmodel->md2data_tris[i].index_st[j] >= loadmodel->md2num_st)
633 Host_Error ("%s has invalid vertex indices", loadmodel->name);
639 void Mod_MD2_LoadFrames(void *start)
643 md2frame_t *pinframe;
644 // LordHavoc: doing proper bbox for model
645 aliasbboxmin[0] = aliasbboxmin[1] = aliasbboxmin[2] = 1000000000;
646 aliasbboxmax[0] = aliasbboxmax[1] = aliasbboxmax[2] = -1000000000;
650 datapointer = (long) start;
652 loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, loadmodel->numframes * sizeof(animscene_t));
653 loadmodel->mdlmd2data_frames = Mem_Alloc(loadmodel->mempool, loadmodel->numframes * sizeof(md2frame_t));
654 loadmodel->mdlmd2data_pose = Mem_Alloc(loadmodel->mempool, loadmodel->numverts * loadmodel->numframes * sizeof(trivertx_t));
656 for (i = 0;i < loadmodel->numframes;i++)
658 pinframe = (md2frame_t *)datapointer;
659 datapointer += sizeof(md2frame_t);
660 strcpy(loadmodel->mdlmd2data_frames[i].name, pinframe->name);
661 for (j = 0;j < 3;j++)
663 loadmodel->mdlmd2data_frames[i].scale[j] = LittleFloat(pinframe->scale[j]);
664 loadmodel->mdlmd2data_frames[i].translate[j] = LittleFloat(pinframe->translate[j]);
666 Mod_MD2_ConvertVerts (loadmodel->numverts, loadmodel->mdlmd2data_frames[i].scale, loadmodel->mdlmd2data_frames[i].translate, (void *)datapointer, &loadmodel->mdlmd2data_pose[i * loadmodel->numverts]);
667 datapointer += loadmodel->numverts * sizeof(trivertx_t);
669 strcpy(loadmodel->animscenes[i].name, loadmodel->mdlmd2data_frames[i].name);
670 loadmodel->animscenes[i].firstframe = i;
671 loadmodel->animscenes[i].framecount = 1;
672 loadmodel->animscenes[i].framerate = 10;
673 loadmodel->animscenes[i].loop = true;
676 // LordHavoc: model bbox
677 modelyawradius = sqrt(modelyawradius);
678 modelradius = sqrt(modelradius);
679 // loadmodel->modelradius = modelradius;
680 for (j = 0;j < 3;j++)
682 loadmodel->normalmins[j] = aliasbboxmin[j];
683 loadmodel->normalmaxs[j] = aliasbboxmax[j];
684 loadmodel->rotatedmins[j] = -modelradius;
685 loadmodel->rotatedmaxs[j] = modelradius;
687 loadmodel->yawmins[0] = loadmodel->yawmins[1] = -(loadmodel->yawmaxs[0] = loadmodel->yawmaxs[1] = modelyawradius);
688 loadmodel->yawmins[2] = loadmodel->normalmins[2];
689 loadmodel->yawmaxs[2] = loadmodel->normalmaxs[2];
692 void Mod_MD2_LoadGLCmds(int *in, int numglcmds)
695 // load the draw list
696 loadmodel->md2data_glcmds = Mem_Alloc(loadmodel->mempool, numglcmds * sizeof(int));
697 for (i = 0;i < numglcmds;i++)
698 loadmodel->md2data_glcmds[i] = LittleLong(in[i]);
701 void Mod_LoadQ2AliasModel (model_t *mod, void *buffer)
707 base = (long) buffer;
708 Mod_MD2_ReadHeader(pinmodel, &numglcmds);
709 Mod_MD2_LoadSkins((void*)(base + LittleLong(pinmodel->ofs_skins)));
710 // Mod_MD2_LoadTriangles((void*)(base + LittleLong(pinmodel->ofs_tris)));
711 Mod_MD2_LoadFrames((void*)(base + LittleLong(pinmodel->ofs_frames)));
712 Mod_MD2_LoadGLCmds((void*)(base + LittleLong(pinmodel->ofs_glcmds)), numglcmds);
715 static void zymswapintblock(int *m, int size)
725 void Mod_LoadZymoticModel(model_t *mod, void *buffer)
727 int i, pbase, *bonecount;
728 unsigned int count, a, b, c, *renderlist, *renderlistend;
729 rtexture_t **texture;
731 zymtype1header_t *pinmodel, *pheader;
734 float corner[2], modelradius;
736 pinmodel = (void *)buffer;
738 if (memcmp(pinmodel->id, "ZYMOTICMODEL", 12))
739 Host_Error ("Mod_LoadZymoticModel: %s is not a zymotic model\n");
741 if (BigLong(pinmodel->type) != 1)
742 Host_Error ("Mod_LoadZymoticModel: only type 1 (skeletal pose) models are currently supported (name = %s)\n", loadmodel->name);
744 loadmodel->type = mod_alias;
745 loadmodel->aliastype = ALIASTYPE_ZYM;
747 loadmodel->zymdata_header = pheader = Mem_Alloc(loadmodel->mempool, BigLong(pinmodel->filesize));
749 pbase = (int) pheader;
751 memcpy(pheader, pinmodel, BigLong(pinmodel->filesize));
754 #define SWAPLONG(var) var = BigLong(var)
755 #define SWAPFLOAT(var) var = BigFloat(var)
756 #define SWAPVEC(var) SWAPFLOAT(var[0]);SWAPFLOAT(var[1]);SWAPFLOAT(var[2])
757 SWAPLONG(pheader->type);
758 SWAPLONG(pheader->filesize);
759 SWAPVEC(pheader->mins);
760 SWAPVEC(pheader->maxs);
761 SWAPFLOAT(pheader->radius);
762 SWAPLONG(pheader->numverts);
763 SWAPLONG(pheader->numtris);
764 SWAPLONG(pheader->numshaders);
765 SWAPLONG(pheader->numbones);
766 SWAPLONG(pheader->numscenes);
768 #define SWAPLUMPINFO(var) SWAPLONG(pheader->lump_##var.start);SWAPLONG(pheader->lump_##var.length)
769 SWAPLUMPINFO(scenes);
772 SWAPLUMPINFO(vertbonecounts);
774 SWAPLUMPINFO(texcoords);
775 SWAPLUMPINFO(render);
776 SWAPLUMPINFO(shaders);
777 SWAPLUMPINFO(trizone);
779 loadmodel->flags = 0; // there are no flags
780 loadmodel->numframes = pheader->numscenes;
781 loadmodel->synctype = ST_SYNC;
782 loadmodel->numtris = pheader->numtris;
784 // FIXME: add skin support and texturing and shaders and...
785 loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) + sizeof(skinframe_t));
786 loadmodel->skinscenes[0].firstframe = 0;
787 loadmodel->skinscenes[0].framecount = 1;
788 loadmodel->skinscenes[0].loop = true;
789 loadmodel->skinscenes[0].framerate = 10;
790 loadmodel->skinframes = (void *)(loadmodel->skinscenes + 1);
791 loadmodel->skinframes->base = NULL;
792 loadmodel->skinframes->fog = NULL;
793 loadmodel->skinframes->pants = NULL;
794 loadmodel->skinframes->shirt = NULL;
795 loadmodel->skinframes->glow = NULL;
796 loadmodel->skinframes->merged = NULL;
797 loadmodel->numskins = 1;
799 // go through the lumps, swapping things
801 // zymlump_t lump_scenes; // zymscene_t scene[numscenes]; // name and other information for each scene (see zymscene struct)
802 scene = (void *) (pheader->lump_scenes.start + pbase);
803 loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
804 for (i = 0;i < pheader->numscenes;i++)
806 SWAPVEC(scene->mins);
807 SWAPVEC(scene->maxs);
808 SWAPFLOAT(scene->radius);
809 SWAPFLOAT(scene->framerate);
810 SWAPLONG(scene->flags);
811 SWAPLONG(scene->start);
812 SWAPLONG(scene->length);
814 memcpy(loadmodel->animscenes[i].name, scene->name, 32);
815 loadmodel->animscenes[i].firstframe = scene->start;
816 loadmodel->animscenes[i].framecount = scene->length;
817 loadmodel->animscenes[i].framerate = scene->framerate;
818 loadmodel->animscenes[i].loop = (scene->flags & ZYMSCENEFLAG_NOLOOP) == 0;
823 // zymlump_t lump_poses; // float pose[numposes][numbones][3][4]; // animation data
824 zymswapintblock((void *) (pheader->lump_poses.start + pbase), pheader->lump_poses.length);
826 // zymlump_t lump_bones; // zymbone_t bone[numbones];
827 bone = (void *) (pheader->lump_bones.start + pbase);
828 for (i = 0;i < pheader->numbones;i++)
830 SWAPLONG(bone[i].flags);
831 SWAPLONG(bone[i].parent);
832 if (bone[i].parent >= i)
833 Host_Error("Mod_LoadZymoticModel: bone[i].parent >= i in %s\n", loadmodel->name);
836 // zymlump_t lump_vertbonecounts; // int vertbonecounts[numvertices]; // how many bones influence each vertex (separate mainly to make this compress better)
837 zymswapintblock((void *) (pheader->lump_vertbonecounts.start + pbase), pheader->lump_vertbonecounts.length);
838 bonecount = (void *) (pheader->lump_vertbonecounts.start + pbase);
839 for (i = 0;i < pheader->numbones;i++)
840 if (bonecount[i] < 1)
841 Host_Error("Mod_LoadZymoticModel: bone vertex count < 1 in %s\n", loadmodel->name);
843 // zymlump_t lump_verts; // zymvertex_t vert[numvertices]; // see vertex struct
844 zymswapintblock((void *) (pheader->lump_verts.start + pbase), pheader->lump_verts.length);
846 // zymlump_t lump_texcoords; // float texcoords[numvertices][2];
847 zymswapintblock((void *) (pheader->lump_texcoords.start + pbase), pheader->lump_texcoords.length);
849 // zymlump_t lump_render; // int renderlist[rendersize]; // sorted by shader with run lengths (int count), shaders are sequentially used, each run can be used with glDrawElements (each triangle is 3 int indices)
850 zymswapintblock((void *) (pheader->lump_render.start + pbase), pheader->lump_render.length);
851 // validate renderlist and swap winding order of tris
852 renderlist = (void *) (pheader->lump_render.start + pbase);
853 renderlistend = (void *) ((byte *) renderlist + pheader->lump_render.length);
854 i = pheader->numshaders * sizeof(int) + pheader->numtris * sizeof(int[3]);
855 if (pheader->lump_render.length != i)
856 Host_Error("Mod_LoadZymoticModel: renderlist is wrong size in %s (is %i bytes, should be %i bytes)\n", loadmodel->name, pheader->lump_render.length, i);
857 for (i = 0;i < pheader->numshaders;i++)
859 if (renderlist >= renderlistend)
860 Host_Error("Mod_LoadZymoticModel: corrupt renderlist in %s (wrong size)\n", loadmodel->name);
861 count = *renderlist++;
862 if (renderlist + count * 3 > renderlistend)
863 Host_Error("Mod_LoadZymoticModel: corrupt renderlist in %s (wrong size)\n", loadmodel->name);
869 if (a >= pheader->numverts || b >= pheader->numverts || c >= pheader->numverts)
870 Host_Error("Mod_LoadZymoticModel: corrupt renderlist in %s (out of bounds index)\n", loadmodel->name);
878 // zymlump_t lump_shaders; // char shadername[numshaders][32]; // shaders used on this model
879 shadername = (void *) (pheader->lump_shaders.start + pbase);
880 texture = (void *) shadername;
881 for (i = 0;i < pheader->numshaders;i++)
884 rt = loadtextureimage(loadmodel->texturepool, shadername, 0, 0, true, r_mipskins.integer, true);
886 *texture++ = rt; // reuse shader name list for texture pointers
889 // zymlump_t lump_trizone; // byte trizone[numtris]; // see trizone explanation
890 zymswapintblock((void *) (pheader->lump_trizone.start + pbase), pheader->lump_trizone.length);
893 modelradius = pheader->radius;
894 // loadmodel->modelradius = pheader->radius;
895 for (i = 0;i < 3;i++)
897 loadmodel->normalmins[i] = pheader->mins[i];
898 loadmodel->normalmaxs[i] = pheader->maxs[i];
899 loadmodel->rotatedmins[i] = -modelradius;
900 loadmodel->rotatedmaxs[i] = modelradius;
902 corner[0] = max(fabs(loadmodel->normalmins[0]), fabs(loadmodel->normalmaxs[0]));
903 corner[1] = max(fabs(loadmodel->normalmins[1]), fabs(loadmodel->normalmaxs[1]));
904 loadmodel->yawmaxs[0] = loadmodel->yawmaxs[1] = sqrt(corner[0]*corner[0]+corner[1]*corner[1]);
905 if (loadmodel->yawmaxs[0] > modelradius)
906 loadmodel->yawmaxs[0] = loadmodel->yawmaxs[1] = modelradius;
907 loadmodel->yawmins[0] = loadmodel->yawmins[1] = -loadmodel->yawmaxs[0];
908 loadmodel->yawmins[2] = loadmodel->normalmins[2];
909 loadmodel->yawmaxs[2] = loadmodel->normalmaxs[2];
911 loadmodel->SERAddEntity = Mod_Alias_SERAddEntity;
912 loadmodel->Draw = R_DrawAliasModel;
913 loadmodel->DrawSky = NULL;
914 loadmodel->DrawShadow = NULL;