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.
25 static cvar_t r_mipskins = {CVAR_SAVE, "r_mipskins", "0"};
27 void Mod_AliasInit (void)
29 Cvar_RegisterVariable(&r_mipskins);
32 static void Mod_CalcAliasModelBBoxes (void)
35 float dist, yawradius, radius;
38 VectorClear(loadmodel->normalmins);
39 VectorClear(loadmodel->normalmaxs);
42 for (meshnum = 0, mesh = loadmodel->aliasdata_meshes;meshnum < loadmodel->aliasnum_meshes;meshnum++, mesh++)
44 for (vnum = 0, v = mesh->data_aliasvertex3f;vnum < mesh->num_vertices * mesh->num_frames;vnum++, v += 3)
46 if (loadmodel->normalmins[0] > v[0]) loadmodel->normalmins[0] = v[0];
47 if (loadmodel->normalmins[1] > v[1]) loadmodel->normalmins[1] = v[1];
48 if (loadmodel->normalmins[2] > v[2]) loadmodel->normalmins[2] = v[2];
49 if (loadmodel->normalmaxs[0] < v[0]) loadmodel->normalmaxs[0] = v[0];
50 if (loadmodel->normalmaxs[1] < v[1]) loadmodel->normalmaxs[1] = v[1];
51 if (loadmodel->normalmaxs[2] < v[2]) loadmodel->normalmaxs[2] = v[2];
52 dist = v[0] * v[0] + v[1] * v[1];
60 radius = sqrt(radius);
61 yawradius = sqrt(yawradius);
62 loadmodel->yawmins[0] = loadmodel->yawmins[1] = -yawradius;
63 loadmodel->yawmaxs[0] = loadmodel->yawmaxs[1] = yawradius;
64 loadmodel->yawmins[2] = loadmodel->normalmins[2];
65 loadmodel->yawmaxs[2] = loadmodel->normalmaxs[2];
66 loadmodel->rotatedmins[0] = loadmodel->rotatedmins[1] = loadmodel->rotatedmins[2] = -radius;
67 loadmodel->rotatedmaxs[0] = loadmodel->rotatedmaxs[1] = loadmodel->rotatedmaxs[2] = radius;
68 loadmodel->radius = radius;
69 loadmodel->radius2 = radius * radius;
72 static void Mod_ConvertAliasVerts (int inverts, vec3_t scale, vec3_t translate, trivertx_t *v, float *out3f, int *vertremap)
76 for (i = 0;i < inverts;i++)
78 if (vertremap[i] < 0 && vertremap[i+inverts] < 0) // only used vertices need apply...
80 temp[0] = v[i].v[0] * scale[0] + translate[0];
81 temp[1] = v[i].v[1] * scale[1] + translate[1];
82 temp[2] = v[i].v[2] * scale[2] + translate[2];
83 j = vertremap[i]; // not onseam
85 VectorCopy(temp, out3f + j * 3);
86 j = vertremap[i+inverts]; // onseam
88 VectorCopy(temp, out3f + j * 3);
92 static void Mod_MDL_LoadFrames (qbyte* datapointer, int inverts, vec3_t scale, vec3_t translate, int *vertremap)
94 int i, f, pose, groupframes;
96 daliasframetype_t *pframetype;
97 daliasframe_t *pinframe;
99 daliasinterval_t *intervals;
102 scene = loadmodel->animscenes;
103 for (f = 0;f < loadmodel->numframes;f++)
105 pframetype = (daliasframetype_t *)datapointer;
106 datapointer += sizeof(daliasframetype_t);
107 if (LittleLong (pframetype->type) == ALIAS_SINGLE)
109 // a single frame is still treated as a group
116 group = (daliasgroup_t *)datapointer;
117 datapointer += sizeof(daliasgroup_t);
118 groupframes = LittleLong (group->numframes);
120 // intervals (time per frame)
121 intervals = (daliasinterval_t *)datapointer;
122 datapointer += sizeof(daliasinterval_t) * groupframes;
124 interval = LittleFloat (intervals->interval); // FIXME: support variable framerate groups
125 if (interval < 0.01f)
126 Host_Error("Mod_MDL_LoadFrames: invalid interval");
129 // get scene name from first frame
130 pinframe = (daliasframe_t *)datapointer;
132 strcpy(scene->name, pinframe->name);
133 scene->firstframe = pose;
134 scene->framecount = groupframes;
135 scene->framerate = 1.0f / interval;
140 for (i = 0;i < groupframes;i++)
142 pinframe = (daliasframe_t *)datapointer;
143 datapointer += sizeof(daliasframe_t);
144 Mod_ConvertAliasVerts(inverts, scale, translate, (trivertx_t *)datapointer, loadmodel->aliasdata_meshes->data_aliasvertex3f + pose * loadmodel->aliasdata_meshes->num_vertices * 3, vertremap);
145 Mod_BuildTextureVectorsAndNormals(loadmodel->aliasdata_meshes->num_vertices, loadmodel->aliasdata_meshes->num_triangles, loadmodel->aliasdata_meshes->data_aliasvertex3f + pose * loadmodel->aliasdata_meshes->num_vertices * 3, loadmodel->aliasdata_meshes->data_texcoord2f, loadmodel->aliasdata_meshes->data_element3i, loadmodel->aliasdata_meshes->data_aliassvector3f + pose * loadmodel->aliasdata_meshes->num_vertices * 3, loadmodel->aliasdata_meshes->data_aliastvector3f + pose * loadmodel->aliasdata_meshes->num_vertices * 3, loadmodel->aliasdata_meshes->data_aliasnormal3f + pose * loadmodel->aliasdata_meshes->num_vertices * 3);
146 datapointer += sizeof(trivertx_t) * inverts;
152 aliaslayer_t mod_alias_layersbuffer[16]; // 7 currently used
153 void Mod_BuildAliasSkinFromSkinFrame(aliasskin_t *skin, skinframe_t *skinframe)
157 memset(&mod_alias_layersbuffer, 0, sizeof(mod_alias_layersbuffer));
158 layer = mod_alias_layersbuffer;
159 layer->flags = ALIASLAYER_SPECULAR;
160 layer->texture = skinframe->gloss;
161 layer->nmap = skinframe->nmap;
163 if (skinframe->merged != NULL)
165 layer->flags = ALIASLAYER_DIFFUSE | ALIASLAYER_NODRAW_IF_COLORMAPPED;
166 layer->texture = skinframe->merged;
167 layer->nmap = skinframe->nmap;
170 if (skinframe->base != NULL)
172 layer->flags = ALIASLAYER_DIFFUSE;
173 if (skinframe->merged != NULL)
174 layer->flags |= ALIASLAYER_NODRAW_IF_NOTCOLORMAPPED;
175 layer->texture = skinframe->base;
176 layer->nmap = skinframe->nmap;
179 if (skinframe->pants != NULL)
181 layer->flags = ALIASLAYER_DIFFUSE | ALIASLAYER_NODRAW_IF_NOTCOLORMAPPED | ALIASLAYER_COLORMAP_PANTS;
182 layer->texture = skinframe->pants;
183 layer->nmap = skinframe->nmap;
186 if (skinframe->shirt != NULL)
188 layer->flags = ALIASLAYER_DIFFUSE | ALIASLAYER_NODRAW_IF_NOTCOLORMAPPED | ALIASLAYER_COLORMAP_SHIRT;
189 layer->texture = skinframe->shirt;
190 layer->nmap = skinframe->nmap;
194 if (skinframe->glow != NULL)
197 layer->texture = skinframe->glow;
199 layer->flags = ALIASLAYER_FOG;
200 layer->texture = skinframe->fog;
205 layer->flags = ALIASLAYER_FOG | ALIASLAYER_FORCEDRAW_IF_FIRSTPASS;
206 layer->texture = skinframe->fog;
211 // fog texture only exists if some pixels are transparent...
212 if (skinframe->fog != NULL)
213 skin->flags |= ALIASSKIN_TRANSPARENT;
215 skin->num_layers = layer - mod_alias_layersbuffer;
216 skin->data_layers = Mem_Alloc(loadmodel->mempool, skin->num_layers * sizeof(aliaslayer_t));
217 memcpy(skin->data_layers, mod_alias_layersbuffer, skin->num_layers * sizeof(aliaslayer_t));
220 #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);
221 #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);
222 extern void R_Model_Alias_Draw(entity_render_t *ent);
223 extern void R_Model_Alias_DrawFakeShadow(entity_render_t *ent);
224 extern void R_Model_Alias_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius);
225 extern void R_Model_Alias_DrawLight(entity_render_t *ent, vec3_t relativelightorigin, vec3_t relativeeyeorigin, float lightradius, float *lightcolor, const matrix4x4_t *matrix_modeltofilter, const matrix4x4_t *matrix_modeltoattenuationxyz, const matrix4x4_t *matrix_modeltoattenuationz);
226 void Mod_LoadQ1AliasModel (model_t *mod, void *buffer)
228 int i, j, version, totalskins, skinwidth, skinheight, groupframes, groupskins, numverts;
229 float scales, scalet, scale[3], translate[3], interval;
231 stvert_t *pinstverts;
232 dtriangle_t *pintriangles;
233 daliasskintype_t *pinskintype;
234 daliasskingroup_t *pinskingroup;
235 daliasskininterval_t *pinskinintervals;
236 daliasframetype_t *pinframetype;
237 daliasgroup_t *pinframegroup;
238 qbyte *datapointer, *startframes, *startskins;
239 char name[MAX_QPATH];
240 skinframe_t tempskinframe;
241 animscene_t *tempskinscenes;
242 skinframe_t *tempskinframes;
244 int *vertonseam, *vertremap;
246 datapointer = buffer;
247 pinmodel = (mdl_t *)datapointer;
248 datapointer += sizeof(mdl_t);
250 version = LittleLong (pinmodel->version);
251 if (version != ALIAS_VERSION)
252 Host_Error ("%s has wrong version number (%i should be %i)",
253 loadmodel->name, version, ALIAS_VERSION);
255 loadmodel->type = mod_alias;
256 loadmodel->aliastype = ALIASTYPE_ALIAS;
257 loadmodel->DrawSky = NULL;
258 loadmodel->Draw = R_Model_Alias_Draw;
259 loadmodel->DrawFakeShadow = R_Model_Alias_DrawFakeShadow;
260 loadmodel->DrawShadowVolume = R_Model_Alias_DrawShadowVolume;
261 loadmodel->DrawLight = R_Model_Alias_DrawLight;
263 loadmodel->aliasnum_meshes = 1;
264 loadmodel->aliasdata_meshes = Mem_Alloc(loadmodel->mempool, sizeof(aliasmesh_t));
266 loadmodel->numskins = LittleLong(pinmodel->numskins);
267 BOUNDI(loadmodel->numskins,0,256);
268 skinwidth = LittleLong (pinmodel->skinwidth);
269 BOUNDI(skinwidth,0,4096);
270 skinheight = LittleLong (pinmodel->skinheight);
271 BOUNDI(skinheight,0,4096);
272 numverts = LittleLong(pinmodel->numverts);
273 BOUNDI(numverts,0,65536);
274 loadmodel->aliasdata_meshes->num_triangles = LittleLong(pinmodel->numtris);
275 BOUNDI(loadmodel->aliasdata_meshes->num_triangles,0,65536 / 3); // max elements limit, rather than max triangles limit
276 loadmodel->numframes = LittleLong(pinmodel->numframes);
277 BOUNDI(loadmodel->numframes,0,65536);
278 loadmodel->synctype = LittleLong (pinmodel->synctype);
279 BOUNDI(loadmodel->synctype,0,2);
280 loadmodel->flags = LittleLong (pinmodel->flags);
282 for (i = 0;i < 3;i++)
284 scale[i] = LittleFloat (pinmodel->scale[i]);
285 translate[i] = LittleFloat (pinmodel->scale_origin[i]);
288 startskins = datapointer;
290 for (i = 0;i < loadmodel->numskins;i++)
292 pinskintype = (daliasskintype_t *)datapointer;
293 datapointer += sizeof(daliasskintype_t);
294 if (LittleLong(pinskintype->type) == ALIAS_SKIN_SINGLE)
298 pinskingroup = (daliasskingroup_t *)datapointer;
299 datapointer += sizeof(daliasskingroup_t);
300 groupskins = LittleLong(pinskingroup->numskins);
301 datapointer += sizeof(daliasskininterval_t) * groupskins;
304 for (j = 0;j < groupskins;j++)
306 datapointer += skinwidth * skinheight;
311 pinstverts = (stvert_t *)datapointer;
312 datapointer += sizeof(stvert_t) * numverts;
314 pintriangles = (dtriangle_t *)datapointer;
315 datapointer += sizeof(dtriangle_t) * loadmodel->aliasdata_meshes->num_triangles;
317 startframes = datapointer;
318 loadmodel->aliasdata_meshes->num_frames = 0;
319 for (i = 0;i < loadmodel->numframes;i++)
321 pinframetype = (daliasframetype_t *)datapointer;
322 datapointer += sizeof(daliasframetype_t);
323 if (LittleLong (pinframetype->type) == ALIAS_SINGLE)
327 pinframegroup = (daliasgroup_t *)datapointer;
328 datapointer += sizeof(daliasgroup_t);
329 groupframes = LittleLong(pinframegroup->numframes);
330 datapointer += sizeof(daliasinterval_t) * groupframes;
333 for (j = 0;j < groupframes;j++)
335 datapointer += sizeof(daliasframe_t);
336 datapointer += sizeof(trivertx_t) * numverts;
337 loadmodel->aliasdata_meshes->num_frames++;
342 loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, loadmodel->numskins * sizeof(animscene_t));
343 loadmodel->skinframes = Mem_Alloc(loadmodel->mempool, totalskins * sizeof(skinframe_t));
345 datapointer = startskins;
346 for (i = 0;i < loadmodel->numskins;i++)
348 pinskintype = (daliasskintype_t *)datapointer;
349 datapointer += sizeof(daliasskintype_t);
351 if (pinskintype->type == ALIAS_SKIN_SINGLE)
358 pinskingroup = (daliasskingroup_t *)datapointer;
359 datapointer += sizeof(daliasskingroup_t);
361 groupskins = LittleLong (pinskingroup->numskins);
363 pinskinintervals = (daliasskininterval_t *)datapointer;
364 datapointer += sizeof(daliasskininterval_t) * groupskins;
366 interval = LittleFloat(pinskinintervals[0].interval);
367 if (interval < 0.01f)
368 Host_Error("Mod_LoadQ1AliasModel: invalid interval\n");
371 sprintf(loadmodel->skinscenes[i].name, "skin %i", i);
372 loadmodel->skinscenes[i].firstframe = totalskins;
373 loadmodel->skinscenes[i].framecount = groupskins;
374 loadmodel->skinscenes[i].framerate = 1.0f / interval;
375 loadmodel->skinscenes[i].loop = true;
377 for (j = 0;j < groupskins;j++)
380 sprintf (name, "%s_%i_%i", loadmodel->name, i, j);
382 sprintf (name, "%s_%i", loadmodel->name, i);
383 if (!Mod_LoadSkinFrame(loadmodel->skinframes + totalskins, name, (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_CLAMP | TEXF_ALPHA, true, false, true))
384 Mod_LoadSkinFrame_Internal(loadmodel->skinframes + totalskins, name, (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_CLAMP | TEXF_ALPHA, true, false, true, (qbyte *)datapointer, skinwidth, skinheight);
385 datapointer += skinwidth * skinheight;
389 // check for skins that don't exist in the model, but do exist as external images
390 // (this was added because yummyluv kept pestering me about support for it)
393 sprintf (name, "%s_%i", loadmodel->name, loadmodel->numskins);
394 if (Mod_LoadSkinFrame (&tempskinframe, name, (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_CLAMP | TEXF_ALPHA, true, false, true))
396 // expand the arrays to make room
397 tempskinscenes = loadmodel->skinscenes;
398 tempskinframes = loadmodel->skinframes;
399 loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, (loadmodel->numskins + 1) * sizeof(animscene_t));
400 loadmodel->skinframes = Mem_Alloc(loadmodel->mempool, (totalskins + 1) * sizeof(skinframe_t));
401 memcpy(loadmodel->skinscenes, tempskinscenes, loadmodel->numskins * sizeof(animscene_t));
402 memcpy(loadmodel->skinframes, tempskinframes, totalskins * sizeof(skinframe_t));
403 Mem_Free(tempskinscenes);
404 Mem_Free(tempskinframes);
405 // store the info about the new skin
406 strcpy(loadmodel->skinscenes[loadmodel->numskins].name, name);
407 loadmodel->skinscenes[loadmodel->numskins].firstframe = totalskins;
408 loadmodel->skinscenes[loadmodel->numskins].framecount = 1;
409 loadmodel->skinscenes[loadmodel->numskins].framerate = 10.0f;
410 loadmodel->skinscenes[loadmodel->numskins].loop = true;
411 loadmodel->skinframes[totalskins] = tempskinframe;
412 loadmodel->numskins++;
419 loadmodel->aliasdata_meshes->num_skins = 0;
420 for (i = 0;i < loadmodel->numskins;i++)
421 loadmodel->aliasdata_meshes->num_skins += loadmodel->skinscenes[i].framecount;
422 loadmodel->aliasdata_meshes->data_skins = Mem_Alloc(loadmodel->mempool, loadmodel->aliasdata_meshes->num_skins * sizeof(aliasskin_t));
424 // store texture coordinates into temporary array, they will be stored
425 // after usage is determined (triangle data)
426 vertst = Mem_Alloc(tempmempool, numverts * 2 * sizeof(float[2]));
427 vertremap = Mem_Alloc(tempmempool, numverts * 3 * sizeof(int));
428 vertonseam = vertremap + numverts * 2;
430 scales = 1.0 / skinwidth;
431 scalet = 1.0 / skinheight;
432 for (i = 0;i < numverts;i++)
434 vertonseam[i] = LittleLong(pinstverts[i].onseam);
435 vertst[i*2+0] = (LittleLong(pinstverts[i].s) + 0.5) * scales;
436 vertst[i*2+1] = (LittleLong(pinstverts[i].t) + 0.5) * scalet;
437 vertst[(i+numverts)*2+0] = vertst[i*2+0] + 0.5;
438 vertst[(i+numverts)*2+1] = vertst[i*2+1];
441 // load triangle data
442 loadmodel->aliasdata_meshes->data_element3i = Mem_Alloc(loadmodel->mempool, sizeof(int[3]) * loadmodel->aliasdata_meshes->num_triangles);
444 // read the triangle elements
445 for (i = 0;i < loadmodel->aliasdata_meshes->num_triangles;i++)
446 for (j = 0;j < 3;j++)
447 loadmodel->aliasdata_meshes->data_element3i[i*3+j] = LittleLong(pintriangles[i].vertindex[j]);
448 // validate (note numverts is used because this is the original data)
449 Mod_ValidateElements(loadmodel->aliasdata_meshes->data_element3i, loadmodel->aliasdata_meshes->num_triangles, numverts, __FILE__, __LINE__);
450 // now butcher the elements according to vertonseam and tri->facesfront
451 // and then compact the vertex set to remove duplicates
452 for (i = 0;i < loadmodel->aliasdata_meshes->num_triangles;i++)
453 if (!LittleLong(pintriangles[i].facesfront)) // backface
454 for (j = 0;j < 3;j++)
455 if (vertonseam[loadmodel->aliasdata_meshes->data_element3i[i*3+j]])
456 loadmodel->aliasdata_meshes->data_element3i[i*3+j] += numverts;
458 // (this uses vertremap to count usage to save some memory)
459 for (i = 0;i < numverts*2;i++)
461 for (i = 0;i < loadmodel->aliasdata_meshes->num_triangles*3;i++)
462 vertremap[loadmodel->aliasdata_meshes->data_element3i[i]]++;
463 // build remapping table and compact array
464 loadmodel->aliasdata_meshes->num_vertices = 0;
465 for (i = 0;i < numverts*2;i++)
469 vertremap[i] = loadmodel->aliasdata_meshes->num_vertices;
470 vertst[loadmodel->aliasdata_meshes->num_vertices*2+0] = vertst[i*2+0];
471 vertst[loadmodel->aliasdata_meshes->num_vertices*2+1] = vertst[i*2+1];
472 loadmodel->aliasdata_meshes->num_vertices++;
475 vertremap[i] = -1; // not used at all
477 // remap the elements to the new vertex set
478 for (i = 0;i < loadmodel->aliasdata_meshes->num_triangles * 3;i++)
479 loadmodel->aliasdata_meshes->data_element3i[i] = vertremap[loadmodel->aliasdata_meshes->data_element3i[i]];
480 // store the texture coordinates
481 loadmodel->aliasdata_meshes->data_texcoord2f = Mem_Alloc(loadmodel->mempool, sizeof(float[2]) * loadmodel->aliasdata_meshes->num_vertices);
482 for (i = 0;i < loadmodel->aliasdata_meshes->num_vertices;i++)
484 loadmodel->aliasdata_meshes->data_texcoord2f[i*2+0] = vertst[i*2+0];
485 loadmodel->aliasdata_meshes->data_texcoord2f[i*2+1] = vertst[i*2+1];
489 loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
490 loadmodel->aliasdata_meshes->data_aliasvertex3f = Mem_Alloc(loadmodel->mempool, sizeof(float[4][3]) * loadmodel->aliasdata_meshes->num_frames * loadmodel->aliasdata_meshes->num_vertices);
491 loadmodel->aliasdata_meshes->data_aliassvector3f = loadmodel->aliasdata_meshes->data_aliasvertex3f + loadmodel->aliasdata_meshes->num_frames * loadmodel->aliasdata_meshes->num_vertices * 3 * 1;
492 loadmodel->aliasdata_meshes->data_aliastvector3f = loadmodel->aliasdata_meshes->data_aliasvertex3f + loadmodel->aliasdata_meshes->num_frames * loadmodel->aliasdata_meshes->num_vertices * 3 * 2;
493 loadmodel->aliasdata_meshes->data_aliasnormal3f = loadmodel->aliasdata_meshes->data_aliasvertex3f + loadmodel->aliasdata_meshes->num_frames * loadmodel->aliasdata_meshes->num_vertices * 3 * 3;
494 Mod_MDL_LoadFrames (startframes, numverts, scale, translate, vertremap);
495 loadmodel->aliasdata_meshes->data_neighbor3i = Mem_Alloc(loadmodel->mempool, loadmodel->aliasdata_meshes->num_triangles * sizeof(int[3]));
496 Mod_BuildTriangleNeighbors(loadmodel->aliasdata_meshes->data_neighbor3i, loadmodel->aliasdata_meshes->data_element3i, loadmodel->aliasdata_meshes->num_triangles);
497 for (i = 0;i < loadmodel->aliasdata_meshes->num_skins;i++)
498 Mod_BuildAliasSkinFromSkinFrame(loadmodel->aliasdata_meshes->data_skins + i, loadmodel->skinframes + i);
499 Mod_CalcAliasModelBBoxes();
505 static void Mod_MD2_ConvertVerts (vec3_t scale, vec3_t translate, trivertx_t *v, float *out3f, int numverts, int *vertremap)
509 for (i = 0;i < numverts;i++, out3f += 3)
511 in = v + vertremap[i];
512 out3f[0] = in->v[0] * scale[0] + translate[0];
513 out3f[1] = in->v[1] * scale[1] + translate[1];
514 out3f[2] = in->v[2] * scale[2] + translate[2];
518 void Mod_LoadQ2AliasModel (model_t *mod, void *buffer)
520 int i, j, k, hashindex, num, numxyz, numst, xyz, st, skinwidth, skinheight, *vertremap, version, end, numverts;
521 float *stverts, s, t, scale[3], translate[3];
523 qbyte *base, *datapointer;
524 md2frame_t *pinframe;
526 md2triangle_t *intri;
527 unsigned short *inst;
530 struct md2verthash_s *next;
534 *hash, **md2verthash, *md2verthashdata;
539 version = LittleLong (pinmodel->version);
540 if (version != MD2ALIAS_VERSION)
541 Host_Error ("%s has wrong version number (%i should be %i)",
542 loadmodel->name, version, MD2ALIAS_VERSION);
544 loadmodel->type = mod_alias;
545 loadmodel->aliastype = ALIASTYPE_ALIAS;
546 loadmodel->DrawSky = NULL;
547 loadmodel->Draw = R_Model_Alias_Draw;
548 loadmodel->DrawFakeShadow = R_Model_Alias_DrawFakeShadow;
549 loadmodel->DrawShadowVolume = R_Model_Alias_DrawShadowVolume;
550 loadmodel->DrawLight = R_Model_Alias_DrawLight;
552 if (LittleLong(pinmodel->num_tris < 1) || LittleLong(pinmodel->num_tris) > (65536 / 3))
553 Host_Error ("%s has invalid number of triangles: %i", loadmodel->name, LittleLong(pinmodel->num_tris));
554 if (LittleLong(pinmodel->num_xyz < 1) || LittleLong(pinmodel->num_xyz) > 65536)
555 Host_Error ("%s has invalid number of vertices: %i", loadmodel->name, LittleLong(pinmodel->num_xyz));
556 if (LittleLong(pinmodel->num_frames < 1) || LittleLong(pinmodel->num_frames) > 65536)
557 Host_Error ("%s has invalid number of frames: %i", loadmodel->name, LittleLong(pinmodel->num_frames));
558 if (LittleLong(pinmodel->num_skins < 0) || LittleLong(pinmodel->num_skins) > 256)
559 Host_Error ("%s has invalid number of skins: %i", loadmodel->name, LittleLong(pinmodel->num_skins));
561 end = LittleLong(pinmodel->ofs_end);
562 if (LittleLong(pinmodel->num_skins) >= 1 && (LittleLong(pinmodel->ofs_skins <= 0) || LittleLong(pinmodel->ofs_skins) >= end))
563 Host_Error ("%s is not a valid model", loadmodel->name);
564 if (LittleLong(pinmodel->ofs_st <= 0) || LittleLong(pinmodel->ofs_st) >= end)
565 Host_Error ("%s is not a valid model", loadmodel->name);
566 if (LittleLong(pinmodel->ofs_tris <= 0) || LittleLong(pinmodel->ofs_tris) >= end)
567 Host_Error ("%s is not a valid model", loadmodel->name);
568 if (LittleLong(pinmodel->ofs_frames <= 0) || LittleLong(pinmodel->ofs_frames) >= end)
569 Host_Error ("%s is not a valid model", loadmodel->name);
570 if (LittleLong(pinmodel->ofs_glcmds <= 0) || LittleLong(pinmodel->ofs_glcmds) >= end)
571 Host_Error ("%s is not a valid model", loadmodel->name);
573 loadmodel->aliasnum_meshes = 1;
574 loadmodel->aliasdata_meshes = Mem_Alloc(loadmodel->mempool, sizeof(aliasmesh_t));
576 loadmodel->numskins = LittleLong(pinmodel->num_skins);
577 numxyz = LittleLong(pinmodel->num_xyz);
578 numst = LittleLong(pinmodel->num_st);
579 loadmodel->aliasdata_meshes->num_triangles = LittleLong(pinmodel->num_tris);
580 loadmodel->numframes = LittleLong(pinmodel->num_frames);
581 loadmodel->aliasdata_meshes->num_frames = loadmodel->numframes;
582 loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, loadmodel->numframes * sizeof(animscene_t));
584 loadmodel->flags = 0; // there are no MD2 flags
585 loadmodel->synctype = ST_RAND;
588 inskin = (void*)(base + LittleLong(pinmodel->ofs_skins));
589 if (loadmodel->numskins)
591 // skins found (most likely not a player model)
592 loadmodel->skinframes = Mem_Alloc(loadmodel->mempool, sizeof(skinframe_t) * loadmodel->numskins);
593 for (i = 0;i < loadmodel->numskins;i++, inskin += MD2_SKINNAME)
594 Mod_LoadSkinFrame (loadmodel->skinframes + i, inskin, (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_ALPHA | TEXF_CLAMP | TEXF_PRECACHE, true, false, true);
598 // no skins (most likely a player model)
599 loadmodel->numskins = 1;
600 loadmodel->skinframes = Mem_Alloc(loadmodel->mempool, sizeof(skinframe_t) * loadmodel->numskins);
602 loadmodel->aliasdata_meshes->num_skins = loadmodel->numskins;
603 loadmodel->aliasdata_meshes->data_skins = Mem_Alloc(loadmodel->mempool, loadmodel->aliasdata_meshes->num_skins * sizeof(aliasskin_t));
605 loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numskins);
606 for (i = 0;i < loadmodel->numskins;i++)
608 loadmodel->skinscenes[i].firstframe = i;
609 loadmodel->skinscenes[i].framecount = 1;
610 loadmodel->skinscenes[i].loop = true;
611 loadmodel->skinscenes[i].framerate = 10;
615 // load the triangles and stvert data
616 inst = (void*)(base + LittleLong(pinmodel->ofs_st));
617 intri = (void*)(base + LittleLong(pinmodel->ofs_tris));
618 skinwidth = LittleLong(pinmodel->skinwidth);
619 skinheight = LittleLong(pinmodel->skinheight);
621 stverts = Mem_Alloc(tempmempool, numst * sizeof(float[2]));
622 s = 1.0f / skinwidth;
623 t = 1.0f / skinheight;
624 for (i = 0;i < numst;i++)
626 j = (unsigned short) LittleShort(inst[i*2+0]);
627 k = (unsigned short) LittleShort(inst[i*2+1]);
628 if (j >= skinwidth || k >= skinheight)
631 Host_Error("Mod_MD2_LoadGeometry: invalid skin coordinate (%i %i) on vert %i of model %s\n", j, k, i, loadmodel->name);
633 stverts[i*2+0] = j * s;
634 stverts[i*2+1] = k * t;
637 md2verthash = Mem_Alloc(tempmempool, 256 * sizeof(hash));
638 md2verthashdata = Mem_Alloc(tempmempool, loadmodel->aliasdata_meshes->num_triangles * 3 * sizeof(*hash));
639 // swap the triangle list
641 loadmodel->aliasdata_meshes->data_element3i = Mem_Alloc(loadmodel->mempool, loadmodel->aliasdata_meshes->num_triangles * sizeof(int[3]));
642 for (i = 0;i < loadmodel->aliasdata_meshes->num_triangles;i++)
644 for (j = 0;j < 3;j++)
646 xyz = (unsigned short) LittleShort (intri[i].index_xyz[j]);
647 st = (unsigned short) LittleShort (intri[i].index_st[j]);
648 if (xyz >= numxyz || st >= numst)
650 Mem_Free(md2verthash);
651 Mem_Free(md2verthashdata);
654 Host_Error("Mod_MD2_LoadGeometry: invalid xyz index (%i) on triangle %i of model %s\n", xyz, i, loadmodel->name);
656 Host_Error("Mod_MD2_LoadGeometry: invalid st index (%i) on triangle %i of model %s\n", st, i, loadmodel->name);
660 hashindex = (xyz * 17 + st) & 255;
661 for (hash = md2verthash[hashindex];hash;hash = hash->next)
662 if (hash->xyz == xyz && hash->st[0] == s && hash->st[1] == t)
666 hash = md2verthashdata + num++;
670 hash->next = md2verthash[hashindex];
671 md2verthash[hashindex] = hash;
673 loadmodel->aliasdata_meshes->data_element3i[i*3+j] = (hash - md2verthashdata);
680 loadmodel->aliasdata_meshes->num_vertices = numverts;
681 vertremap = Mem_Alloc(loadmodel->mempool, num * sizeof(int));
682 loadmodel->aliasdata_meshes->data_texcoord2f = Mem_Alloc(loadmodel->mempool, num * sizeof(float[2]));
683 for (i = 0;i < num;i++)
685 hash = md2verthashdata + i;
686 vertremap[i] = hash->xyz;
687 loadmodel->aliasdata_meshes->data_texcoord2f[i*2+0] = hash->st[0];
688 loadmodel->aliasdata_meshes->data_texcoord2f[i*2+1] = hash->st[1];
691 Mem_Free(md2verthash);
692 Mem_Free(md2verthashdata);
695 datapointer = (base + LittleLong(pinmodel->ofs_frames));
696 loadmodel->aliasdata_meshes->data_aliasvertex3f = Mem_Alloc(loadmodel->mempool, numverts * loadmodel->aliasdata_meshes->num_frames * sizeof(float[4][3]));
697 loadmodel->aliasdata_meshes->data_aliassvector3f = loadmodel->aliasdata_meshes->data_aliasvertex3f + numverts * loadmodel->aliasdata_meshes->num_frames * 3 * 1;
698 loadmodel->aliasdata_meshes->data_aliastvector3f = loadmodel->aliasdata_meshes->data_aliasvertex3f + numverts * loadmodel->aliasdata_meshes->num_frames * 3 * 2;
699 loadmodel->aliasdata_meshes->data_aliasnormal3f = loadmodel->aliasdata_meshes->data_aliasvertex3f + numverts * loadmodel->aliasdata_meshes->num_frames * 3 * 3;
700 for (i = 0;i < loadmodel->aliasdata_meshes->num_frames;i++)
702 pinframe = (md2frame_t *)datapointer;
703 datapointer += sizeof(md2frame_t);
704 for (j = 0;j < 3;j++)
706 scale[j] = LittleFloat(pinframe->scale[j]);
707 translate[j] = LittleFloat(pinframe->translate[j]);
709 Mod_MD2_ConvertVerts(scale, translate, (void *)datapointer, loadmodel->aliasdata_meshes->data_aliasvertex3f + i * numverts * 3, numverts, vertremap);
710 Mod_BuildTextureVectorsAndNormals(loadmodel->aliasdata_meshes->num_vertices, loadmodel->aliasdata_meshes->num_triangles, loadmodel->aliasdata_meshes->data_aliasvertex3f + i * loadmodel->aliasdata_meshes->num_vertices * 3, loadmodel->aliasdata_meshes->data_texcoord2f, loadmodel->aliasdata_meshes->data_element3i, loadmodel->aliasdata_meshes->data_aliassvector3f + i * loadmodel->aliasdata_meshes->num_vertices * 3, loadmodel->aliasdata_meshes->data_aliastvector3f + i * loadmodel->aliasdata_meshes->num_vertices * 3, loadmodel->aliasdata_meshes->data_aliasnormal3f + i * loadmodel->aliasdata_meshes->num_vertices * 3);
711 datapointer += numxyz * sizeof(trivertx_t);
713 strcpy(loadmodel->animscenes[i].name, pinframe->name);
714 loadmodel->animscenes[i].firstframe = i;
715 loadmodel->animscenes[i].framecount = 1;
716 loadmodel->animscenes[i].framerate = 10;
717 loadmodel->animscenes[i].loop = true;
722 loadmodel->aliasdata_meshes->data_neighbor3i = Mem_Alloc(loadmodel->mempool, loadmodel->aliasdata_meshes->num_triangles * sizeof(int[3]));
723 Mod_BuildTriangleNeighbors(loadmodel->aliasdata_meshes->data_neighbor3i, loadmodel->aliasdata_meshes->data_element3i, loadmodel->aliasdata_meshes->num_triangles);
724 for (i = 0;i < loadmodel->aliasdata_meshes->num_skins;i++)
725 Mod_BuildAliasSkinFromSkinFrame(loadmodel->aliasdata_meshes->data_skins + i, loadmodel->skinframes + i);
726 Mod_CalcAliasModelBBoxes();
729 void Mod_LoadQ3AliasModel(model_t *mod, void *buffer)
732 md3modelheader_t *pinmodel;
733 md3frameinfo_t *pinframe;
736 skinframe_t tempskinframe;
740 if (memcmp(pinmodel->identifier, "IDP3", 4))
741 Host_Error ("%s is not a MD3 (IDP3) file\n", loadmodel->name);
742 version = LittleLong (pinmodel->version);
743 if (version != MD3VERSION)
744 Host_Error ("%s has wrong version number (%i should be %i)",
745 loadmodel->name, version, MD3VERSION);
747 loadmodel->type = mod_alias;
748 loadmodel->aliastype = ALIASTYPE_ALIAS;
749 loadmodel->DrawSky = NULL;
750 loadmodel->Draw = R_Model_Alias_Draw;
751 loadmodel->DrawFakeShadow = R_Model_Alias_DrawFakeShadow;
752 loadmodel->DrawShadowVolume = R_Model_Alias_DrawShadowVolume;
753 loadmodel->DrawLight = R_Model_Alias_DrawLight;
754 loadmodel->flags = 0;
755 loadmodel->synctype = ST_RAND;
757 // set up some global info about the model
758 loadmodel->numframes = LittleLong(pinmodel->num_frames);
759 loadmodel->numskins = 1;
760 loadmodel->aliasnum_meshes = LittleLong(pinmodel->num_meshes);
761 loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, loadmodel->numskins * sizeof(animscene_t));
762 loadmodel->skinscenes[0].firstframe = 0;
763 loadmodel->skinscenes[0].framecount = 1;
764 loadmodel->skinscenes[0].loop = true;
765 loadmodel->skinscenes[0].framerate = 10;
768 loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, loadmodel->numframes * sizeof(animscene_t));
769 for (i = 0, pinframe = (md3frameinfo_t *)((qbyte *)pinmodel + LittleLong(pinmodel->lump_frameinfo));i < loadmodel->numframes;i++, pinframe++)
771 strcpy(loadmodel->animscenes[i].name, pinframe->name);
772 loadmodel->animscenes[i].firstframe = i;
773 loadmodel->animscenes[i].framecount = 1;
774 loadmodel->animscenes[i].framerate = 10;
775 loadmodel->animscenes[i].loop = true;
778 // tags are not loaded yet
781 loadmodel->aliasdata_meshes = Mem_Alloc(loadmodel->mempool, loadmodel->aliasnum_meshes * sizeof(aliasmesh_t));
782 for (i = 0, pinmesh = (md3mesh_t *)((qbyte *)pinmodel + LittleLong(pinmodel->lump_meshes));i < loadmodel->aliasnum_meshes;i++, pinmesh = (md3mesh_t *)((qbyte *)pinmesh + LittleLong(pinmesh->lump_end)))
784 if (memcmp(pinmesh->identifier, "IDP3", 4))
785 Host_Error("Mod_LoadQ3AliasModel: invalid mesh identifier (not IDP3)\n");
786 mesh = loadmodel->aliasdata_meshes + i;
787 mesh->num_skins = loadmodel->numskins;
788 mesh->num_frames = LittleLong(pinmesh->num_frames);
789 mesh->num_vertices = LittleLong(pinmesh->num_vertices);
790 mesh->num_triangles = LittleLong(pinmesh->num_triangles);
791 mesh->data_skins = Mem_Alloc(loadmodel->mempool, mesh->num_skins * sizeof(aliasskin_t));
792 mesh->data_element3i = Mem_Alloc(loadmodel->mempool, mesh->num_triangles * sizeof(int[3]));
793 mesh->data_neighbor3i = Mem_Alloc(loadmodel->mempool, mesh->num_triangles * sizeof(int[3]));
794 mesh->data_texcoord2f = Mem_Alloc(loadmodel->mempool, mesh->num_vertices * sizeof(float[2]));
795 mesh->data_aliasvertex3f = Mem_Alloc(loadmodel->mempool, mesh->num_vertices * mesh->num_frames * sizeof(float[4][3]));
796 mesh->data_aliassvector3f = mesh->data_aliasvertex3f + mesh->num_vertices * mesh->num_frames * 3 * 1;
797 mesh->data_aliastvector3f = mesh->data_aliasvertex3f + mesh->num_vertices * mesh->num_frames * 3 * 2;
798 mesh->data_aliasnormal3f = mesh->data_aliasvertex3f + mesh->num_vertices * mesh->num_frames * 3 * 3;
799 for (j = 0;j < mesh->num_triangles * 3;j++)
800 mesh->data_element3i[j] = LittleLong(((int *)((qbyte *)pinmesh + pinmesh->lump_elements))[j]);
801 for (j = 0;j < mesh->num_vertices;j++)
803 mesh->data_texcoord2f[j * 2 + 0] = LittleLong(((float *)((qbyte *)pinmesh + pinmesh->lump_texcoords))[j * 2 + 0]);
804 mesh->data_texcoord2f[j * 2 + 1] = LittleLong(((float *)((qbyte *)pinmesh + pinmesh->lump_texcoords))[j * 2 + 1]);
806 for (j = 0;j < mesh->num_vertices * mesh->num_frames;j++)
808 mesh->data_aliasvertex3f[j * 3 + 0] = LittleShort(((short *)((qbyte *)pinmesh + pinmesh->lump_framevertices))[j * 4 + 0]) * (1.0f / 64.0f);
809 mesh->data_aliasvertex3f[j * 3 + 1] = LittleShort(((short *)((qbyte *)pinmesh + pinmesh->lump_framevertices))[j * 4 + 1]) * (1.0f / 64.0f);
810 mesh->data_aliasvertex3f[j * 3 + 2] = LittleShort(((short *)((qbyte *)pinmesh + pinmesh->lump_framevertices))[j * 4 + 2]) * (1.0f / 64.0f);
812 for (j = 0;j < mesh->num_frames;j++)
813 Mod_BuildTextureVectorsAndNormals(mesh->num_vertices, mesh->num_triangles, mesh->data_aliasvertex3f + j * mesh->num_vertices * 3, mesh->data_texcoord2f, mesh->data_element3i, mesh->data_aliassvector3f + j * mesh->num_vertices * 3, mesh->data_aliastvector3f + j * mesh->num_vertices * 3, mesh->data_aliasnormal3f + j * mesh->num_vertices * 3);
815 memset(&tempskinframe, 0, sizeof(tempskinframe));
816 if (LittleLong(pinmesh->num_shaders) >= 1 && ((md3shader_t *)((qbyte *) pinmesh + pinmesh->lump_shaders))->name[0])
817 Mod_LoadSkinFrame (&tempskinframe, ((md3shader_t *)((qbyte *) pinmesh + pinmesh->lump_shaders))->name, (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_ALPHA | TEXF_CLAMP | TEXF_PRECACHE, true, false, true);
818 Mod_ValidateElements(mesh->data_element3i, mesh->num_triangles, mesh->num_vertices, __FILE__, __LINE__);
819 Mod_BuildTriangleNeighbors(mesh->data_neighbor3i, mesh->data_element3i, mesh->num_triangles);
820 Mod_BuildAliasSkinFromSkinFrame(mesh->data_skins, &tempskinframe);
822 Mod_CalcAliasModelBBoxes();
825 extern void R_Model_Zymotic_DrawSky(entity_render_t *ent);
826 extern void R_Model_Zymotic_Draw(entity_render_t *ent);
827 extern void R_Model_Zymotic_DrawFakeShadow(entity_render_t *ent);
828 extern void R_Model_Zymotic_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius);
829 extern void R_Model_Zymotic_DrawLight(entity_render_t *ent, vec3_t relativelightorigin, vec3_t relativeeyeorigin, float lightradius, float *lightcolor, const matrix4x4_t *matrix_modeltofilter, const matrix4x4_t *matrix_modeltoattenuationxyz, const matrix4x4_t *matrix_modeltoattenuationz);
830 void Mod_LoadZymoticModel(model_t *mod, void *buffer)
832 zymtype1header_t *pinmodel, *pheader;
835 pinmodel = (void *)buffer;
837 if (memcmp(pinmodel->id, "ZYMOTICMODEL", 12))
838 Host_Error ("Mod_LoadZymoticModel: %s is not a zymotic model\n");
839 if (BigLong(pinmodel->type) != 1)
840 Host_Error ("Mod_LoadZymoticModel: only type 1 (skeletal pose) models are currently supported (name = %s)\n", loadmodel->name);
842 loadmodel->type = mod_alias;
843 loadmodel->aliastype = ALIASTYPE_ZYM;
844 loadmodel->DrawSky = NULL;
845 loadmodel->Draw = R_Model_Zymotic_Draw;
846 loadmodel->DrawFakeShadow = NULL;//R_Model_Zymotic_DrawFakeShadow;
847 loadmodel->DrawShadowVolume = NULL;//R_Model_Zymotic_DrawShadowVolume;
848 loadmodel->DrawLight = NULL;//R_Model_Zymotic_DrawLight;
852 pheader->type = BigLong(pinmodel->type);
853 pheader->filesize = BigLong(pinmodel->filesize);
854 pheader->mins[0] = BigFloat(pinmodel->mins[0]);
855 pheader->mins[1] = BigFloat(pinmodel->mins[1]);
856 pheader->mins[2] = BigFloat(pinmodel->mins[2]);
857 pheader->maxs[0] = BigFloat(pinmodel->maxs[0]);
858 pheader->maxs[1] = BigFloat(pinmodel->maxs[1]);
859 pheader->maxs[2] = BigFloat(pinmodel->maxs[2]);
860 pheader->radius = BigFloat(pinmodel->radius);
861 pheader->numverts = loadmodel->zymnum_verts = BigLong(pinmodel->numverts);
862 pheader->numtris = loadmodel->zymnum_tris = BigLong(pinmodel->numtris);
863 pheader->numshaders = loadmodel->zymnum_shaders = BigLong(pinmodel->numshaders);
864 pheader->numbones = loadmodel->zymnum_bones = BigLong(pinmodel->numbones);
865 pheader->numscenes = loadmodel->zymnum_scenes = BigLong(pinmodel->numscenes);
866 pheader->lump_scenes.start = BigLong(pinmodel->lump_scenes.start);
867 pheader->lump_scenes.length = BigLong(pinmodel->lump_scenes.length);
868 pheader->lump_poses.start = BigLong(pinmodel->lump_poses.start);
869 pheader->lump_poses.length = BigLong(pinmodel->lump_poses.length);
870 pheader->lump_bones.start = BigLong(pinmodel->lump_bones.start);
871 pheader->lump_bones.length = BigLong(pinmodel->lump_bones.length);
872 pheader->lump_vertbonecounts.start = BigLong(pinmodel->lump_vertbonecounts.start);
873 pheader->lump_vertbonecounts.length = BigLong(pinmodel->lump_vertbonecounts.length);
874 pheader->lump_verts.start = BigLong(pinmodel->lump_verts.start);
875 pheader->lump_verts.length = BigLong(pinmodel->lump_verts.length);
876 pheader->lump_texcoords.start = BigLong(pinmodel->lump_texcoords.start);
877 pheader->lump_texcoords.length = BigLong(pinmodel->lump_texcoords.length);
878 pheader->lump_render.start = BigLong(pinmodel->lump_render.start);
879 pheader->lump_render.length = BigLong(pinmodel->lump_render.length);
880 pheader->lump_shaders.start = BigLong(pinmodel->lump_shaders.start);
881 pheader->lump_shaders.length = BigLong(pinmodel->lump_shaders.length);
882 pheader->lump_trizone.start = BigLong(pinmodel->lump_trizone.start);
883 pheader->lump_trizone.length = BigLong(pinmodel->lump_trizone.length);
885 loadmodel->flags = 0; // there are no flags
886 loadmodel->numframes = pheader->numscenes;
887 loadmodel->synctype = ST_SYNC;
888 //loadmodel->numtris = pheader->numtris;
889 //loadmodel->numverts = 0;
893 float modelradius, corner[2];
895 modelradius = pheader->radius;
896 for (i = 0;i < 3;i++)
898 loadmodel->normalmins[i] = pheader->mins[i];
899 loadmodel->normalmaxs[i] = pheader->maxs[i];
900 loadmodel->rotatedmins[i] = -modelradius;
901 loadmodel->rotatedmaxs[i] = modelradius;
903 corner[0] = max(fabs(loadmodel->normalmins[0]), fabs(loadmodel->normalmaxs[0]));
904 corner[1] = max(fabs(loadmodel->normalmins[1]), fabs(loadmodel->normalmaxs[1]));
905 loadmodel->yawmaxs[0] = loadmodel->yawmaxs[1] = sqrt(corner[0]*corner[0]+corner[1]*corner[1]);
906 if (loadmodel->yawmaxs[0] > modelradius)
907 loadmodel->yawmaxs[0] = loadmodel->yawmaxs[1] = modelradius;
908 loadmodel->yawmins[0] = loadmodel->yawmins[1] = -loadmodel->yawmaxs[0];
909 loadmodel->yawmins[2] = loadmodel->normalmins[2];
910 loadmodel->yawmaxs[2] = loadmodel->normalmaxs[2];
911 loadmodel->radius = modelradius;
912 loadmodel->radius2 = modelradius * modelradius;
916 // FIXME: add shaders, and make them switchable shader sets and...
917 loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) + sizeof(skinframe_t));
918 loadmodel->skinscenes[0].firstframe = 0;
919 loadmodel->skinscenes[0].framecount = 1;
920 loadmodel->skinscenes[0].loop = true;
921 loadmodel->skinscenes[0].framerate = 10;
922 loadmodel->skinframes = (void *)(loadmodel->skinscenes + 1);
923 loadmodel->numskins = 1;
926 // go through the lumps, swapping things
931 // zymlump_t lump_scenes; // zymscene_t scene[numscenes]; // name and other information for each scene (see zymscene struct)
932 loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
933 scene = (void *) (pheader->lump_scenes.start + pbase);
934 numposes = pheader->lump_poses.length / pheader->numbones / sizeof(float[3][4]);
935 for (i = 0;i < pheader->numscenes;i++)
937 memcpy(loadmodel->animscenes[i].name, scene->name, 32);
938 loadmodel->animscenes[i].firstframe = BigLong(scene->start);
939 loadmodel->animscenes[i].framecount = BigLong(scene->length);
940 loadmodel->animscenes[i].framerate = BigFloat(scene->framerate);
941 loadmodel->animscenes[i].loop = (BigLong(scene->flags) & ZYMSCENEFLAG_NOLOOP) == 0;
942 if ((unsigned int) loadmodel->animscenes[i].firstframe >= (unsigned int) numposes)
943 Host_Error("Mod_LoadZymoticModel: scene firstframe (%i) >= numposes (%i)\n", loadmodel->animscenes[i].firstframe, numposes);
944 if ((unsigned int) loadmodel->animscenes[i].firstframe + (unsigned int) loadmodel->animscenes[i].framecount > (unsigned int) numposes)
945 Host_Error("Mod_LoadZymoticModel: scene firstframe (%i) + framecount (%i) >= numposes (%i)\n", loadmodel->animscenes[i].firstframe, loadmodel->animscenes[i].framecount, numposes);
946 if (loadmodel->animscenes[i].framerate < 0)
947 Host_Error("Mod_LoadZymoticModel: scene framerate (%f) < 0\n", loadmodel->animscenes[i].framerate);
955 // zymlump_t lump_poses; // float pose[numposes][numbones][3][4]; // animation data
956 loadmodel->zymdata_poses = Mem_Alloc(loadmodel->mempool, pheader->lump_poses.length);
957 poses = (void *) (pheader->lump_poses.start + pbase);
958 for (i = 0;i < pheader->lump_poses.length / 4;i++)
959 loadmodel->zymdata_poses[i] = BigFloat(poses[i]);
965 // zymlump_t lump_bones; // zymbone_t bone[numbones];
966 loadmodel->zymdata_bones = Mem_Alloc(loadmodel->mempool, pheader->numbones * sizeof(zymbone_t));
967 bone = (void *) (pheader->lump_bones.start + pbase);
968 for (i = 0;i < pheader->numbones;i++)
970 memcpy(loadmodel->zymdata_bones[i].name, bone[i].name, sizeof(bone[i].name));
971 loadmodel->zymdata_bones[i].flags = BigLong(bone[i].flags);
972 loadmodel->zymdata_bones[i].parent = BigLong(bone[i].parent);
973 if (loadmodel->zymdata_bones[i].parent >= i)
974 Host_Error("Mod_LoadZymoticModel: bone[%i].parent >= %i in %s\n", i, i, loadmodel->name);
980 // zymlump_t lump_vertbonecounts; // int vertbonecounts[numvertices]; // how many bones influence each vertex (separate mainly to make this compress better)
981 loadmodel->zymdata_vertbonecounts = Mem_Alloc(loadmodel->mempool, pheader->numverts * sizeof(int));
982 bonecount = (void *) (pheader->lump_vertbonecounts.start + pbase);
983 for (i = 0;i < pheader->numverts;i++)
985 loadmodel->zymdata_vertbonecounts[i] = BigLong(bonecount[i]);
986 if (loadmodel->zymdata_vertbonecounts[i] < 1)
987 Host_Error("Mod_LoadZymoticModel: bone vertex count < 1 in %s\n", loadmodel->name);
993 zymvertex_t *vertdata;
994 // zymlump_t lump_verts; // zymvertex_t vert[numvertices]; // see vertex struct
995 loadmodel->zymdata_verts = Mem_Alloc(loadmodel->mempool, pheader->lump_verts.length);
996 vertdata = (void *) (pheader->lump_verts.start + pbase);
997 for (i = 0;i < pheader->lump_verts.length / (int) sizeof(zymvertex_t);i++)
999 loadmodel->zymdata_verts[i].bonenum = BigLong(vertdata[i].bonenum);
1000 loadmodel->zymdata_verts[i].origin[0] = BigFloat(vertdata[i].origin[0]);
1001 loadmodel->zymdata_verts[i].origin[1] = BigFloat(vertdata[i].origin[1]);
1002 loadmodel->zymdata_verts[i].origin[2] = BigFloat(vertdata[i].origin[2]);
1008 float *intexcoord2f, *outtexcoord2f;
1009 // zymlump_t lump_texcoords; // float texcoords[numvertices][2];
1010 loadmodel->zymdata_texcoords = outtexcoord2f = Mem_Alloc(loadmodel->mempool, pheader->numverts * sizeof(float[2]));
1011 intexcoord2f = (void *) (pheader->lump_texcoords.start + pbase);
1012 for (i = 0;i < pheader->numverts;i++)
1014 outtexcoord2f[i*2+0] = BigFloat(intexcoord2f[i*2+0]);
1015 // flip T coordinate for OpenGL
1016 outtexcoord2f[i*2+1] = 1 - BigFloat(intexcoord2f[i*2+1]);
1021 int i, count, *renderlist, *renderlistend, *outrenderlist;
1022 // 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)
1023 loadmodel->zymdata_renderlist = Mem_Alloc(loadmodel->mempool, pheader->lump_render.length);
1024 // byteswap, validate, and swap winding order of tris
1025 count = pheader->numshaders * sizeof(int) + pheader->numtris * sizeof(int[3]);
1026 if (pheader->lump_render.length != count)
1027 Host_Error("Mod_LoadZymoticModel: renderlist is wrong size in %s (is %i bytes, should be %i bytes)\n", loadmodel->name, pheader->lump_render.length, count);
1028 outrenderlist = loadmodel->zymdata_renderlist = Mem_Alloc(loadmodel->mempool, count);
1029 renderlist = (void *) (pheader->lump_render.start + pbase);
1030 renderlistend = (void *) ((qbyte *) renderlist + pheader->lump_render.length);
1031 for (i = 0;i < pheader->numshaders;i++)
1033 if (renderlist >= renderlistend)
1034 Host_Error("Mod_LoadZymoticModel: corrupt renderlist in %s (wrong size)\n", loadmodel->name);
1035 count = BigLong(*renderlist);renderlist++;
1036 if (renderlist + count * 3 > renderlistend)
1037 Host_Error("Mod_LoadZymoticModel: corrupt renderlist in %s (wrong size)\n", loadmodel->name);
1038 *outrenderlist++ = count;
1041 outrenderlist[2] = BigLong(renderlist[0]);
1042 outrenderlist[1] = BigLong(renderlist[1]);
1043 outrenderlist[0] = BigLong(renderlist[2]);
1044 if ((unsigned int)outrenderlist[0] >= (unsigned int)pheader->numverts
1045 || (unsigned int)outrenderlist[1] >= (unsigned int)pheader->numverts
1046 || (unsigned int)outrenderlist[2] >= (unsigned int)pheader->numverts)
1047 Host_Error("Mod_LoadZymoticModel: corrupt renderlist in %s (out of bounds index)\n", loadmodel->name);
1057 // zymlump_t lump_shaders; // char shadername[numshaders][32]; // shaders used on this model
1058 loadmodel->zymdata_textures = Mem_Alloc(loadmodel->mempool, pheader->numshaders * sizeof(rtexture_t *));
1059 shadername = (void *) (pheader->lump_shaders.start + pbase);
1060 for (i = 0;i < pheader->numshaders;i++)
1061 loadmodel->zymdata_textures[i] = loadtextureimage(loadmodel->texturepool, shadername + i * 32, 0, 0, true, TEXF_ALPHA | TEXF_PRECACHE | (r_mipskins.integer ? TEXF_MIPMAP : 0));
1065 // zymlump_t lump_trizone; // byte trizone[numtris]; // see trizone explanation
1066 loadmodel->zymdata_trizone = Mem_Alloc(loadmodel->mempool, pheader->numtris);
1067 memcpy(loadmodel->zymdata_trizone, (void *) (pheader->lump_trizone.start + pbase), pheader->numtris);