+ else if (!strcmp(pchunk->id, "VTXW0000"))
+ {
+ pskvtxw_t *p;
+ if (recordsize != sizeof(*p))
+ Host_Error("%s: %s has unsupported recordsize", loadmodel->name, pchunk->id);
+ // byteswap in place and keep the pointer
+ numvtxw = numrecords;
+ vtxw = (pskvtxw_t *)buffer;
+ for (index = 0, p = (pskvtxw_t *)buffer;index < numrecords;index++, p++)
+ {
+ p->pntsindex = LittleShort(p->pntsindex);
+ p->texcoord[0] = LittleFloat(p->texcoord[0]);
+ p->texcoord[1] = LittleFloat(p->texcoord[1]);
+ if (p->pntsindex >= numpnts)
+ {
+ Con_Printf("%s: vtxw->pntsindex %i >= numpnts %i\n", loadmodel->name, p->pntsindex, numpnts);
+ p->pntsindex = 0;
+ }
+ }
+ buffer = p;
+ }
+ else if (!strcmp(pchunk->id, "FACE0000"))
+ {
+ pskface_t *p;
+ if (recordsize != sizeof(*p))
+ Host_Error("%s: %s has unsupported recordsize", loadmodel->name, pchunk->id);
+ // byteswap in place and keep the pointer
+ numfaces = numrecords;
+ faces = (pskface_t *)buffer;
+ for (index = 0, p = (pskface_t *)buffer;index < numrecords;index++, p++)
+ {
+ p->vtxwindex[0] = LittleShort(p->vtxwindex[0]);
+ p->vtxwindex[1] = LittleShort(p->vtxwindex[1]);
+ p->vtxwindex[2] = LittleShort(p->vtxwindex[2]);
+ p->group = LittleLong(p->group);
+ if (p->vtxwindex[0] >= numvtxw)
+ {
+ Con_Printf("%s: face->vtxwindex[0] %i >= numvtxw %i\n", loadmodel->name, p->vtxwindex[0], numvtxw);
+ p->vtxwindex[0] = 0;
+ }
+ if (p->vtxwindex[1] >= numvtxw)
+ {
+ Con_Printf("%s: face->vtxwindex[1] %i >= numvtxw %i\n", loadmodel->name, p->vtxwindex[1], numvtxw);
+ p->vtxwindex[1] = 0;
+ }
+ if (p->vtxwindex[2] >= numvtxw)
+ {
+ Con_Printf("%s: face->vtxwindex[2] %i >= numvtxw %i\n", loadmodel->name, p->vtxwindex[2], numvtxw);
+ p->vtxwindex[2] = 0;
+ }
+ }
+ buffer = p;
+ }
+ else if (!strcmp(pchunk->id, "MATT0000"))
+ {
+ pskmatt_t *p;
+ if (recordsize != sizeof(*p))
+ Host_Error("%s: %s has unsupported recordsize", loadmodel->name, pchunk->id);
+ // byteswap in place and keep the pointer
+ nummatts = numrecords;
+ matts = (pskmatt_t *)buffer;
+ for (index = 0, p = (pskmatt_t *)buffer;index < numrecords;index++, p++)
+ {
+ // nothing to do
+ }
+ buffer = p;
+ }
+ else if (!strcmp(pchunk->id, "REFSKELT"))
+ {
+ pskboneinfo_t *p;
+ if (recordsize != sizeof(*p))
+ Host_Error("%s: %s has unsupported recordsize", loadmodel->name, pchunk->id);
+ // byteswap in place and keep the pointer
+ numbones = numrecords;
+ bones = (pskboneinfo_t *)buffer;
+ for (index = 0, p = (pskboneinfo_t *)buffer;index < numrecords;index++, p++)
+ {
+ p->numchildren = LittleLong(p->numchildren);
+ p->parent = LittleLong(p->parent);
+ p->basepose.quat[0] = LittleFloat(p->basepose.quat[0]);
+ p->basepose.quat[1] = LittleFloat(p->basepose.quat[1]);
+ p->basepose.quat[2] = LittleFloat(p->basepose.quat[2]);
+ p->basepose.quat[3] = LittleFloat(p->basepose.quat[3]);
+ p->basepose.origin[0] = LittleFloat(p->basepose.origin[0]);
+ p->basepose.origin[1] = LittleFloat(p->basepose.origin[1]);
+ p->basepose.origin[2] = LittleFloat(p->basepose.origin[2]);
+ p->basepose.unknown = LittleFloat(p->basepose.unknown);
+ p->basepose.size[0] = LittleFloat(p->basepose.size[0]);
+ p->basepose.size[1] = LittleFloat(p->basepose.size[1]);
+ p->basepose.size[2] = LittleFloat(p->basepose.size[2]);
+#ifdef PSKQUATNEGATIONS
+ if (index)
+ {
+ p->basepose.quat[0] *= -1;
+ p->basepose.quat[1] *= -1;
+ p->basepose.quat[2] *= -1;
+ }
+ else
+ {
+ p->basepose.quat[0] *= 1;
+ p->basepose.quat[1] *= -1;
+ p->basepose.quat[2] *= 1;
+ }
+#endif
+ if (p->parent < 0 || p->parent >= numbones)
+ {
+ Con_Printf("%s: bone->parent %i >= numbones %i\n", loadmodel->name, p->parent, numbones);
+ p->parent = 0;
+ }
+ }
+ buffer = p;
+ }
+ else if (!strcmp(pchunk->id, "RAWWEIGHTS"))
+ {
+ pskrawweights_t *p;
+ if (recordsize != sizeof(*p))
+ Host_Error("%s: %s has unsupported recordsize", loadmodel->name, pchunk->id);
+ // byteswap in place and keep the pointer
+ numrawweights = numrecords;
+ rawweights = (pskrawweights_t *)buffer;
+ for (index = 0, p = (pskrawweights_t *)buffer;index < numrecords;index++, p++)
+ {
+ p->weight = LittleFloat(p->weight);
+ p->pntsindex = LittleLong(p->pntsindex);
+ p->boneindex = LittleLong(p->boneindex);
+ if (p->pntsindex < 0 || p->pntsindex >= numpnts)
+ {
+ Con_Printf("%s: weight->pntsindex %i >= numpnts %i\n", loadmodel->name, p->pntsindex, numpnts);
+ p->pntsindex = 0;
+ }
+ if (p->boneindex < 0 || p->boneindex >= numbones)
+ {
+ Con_Printf("%s: weight->boneindex %i >= numbones %i\n", loadmodel->name, p->boneindex, numbones);
+ p->boneindex = 0;
+ }
+ }
+ buffer = p;
+ }
+ }
+
+ while (animbuffer < animbufferend)
+ {
+ pchunk = (pskchunk_t *)animbuffer;
+ animbuffer = (void *)((unsigned char *)animbuffer + sizeof(pskchunk_t));
+ version = LittleLong(pchunk->version);
+ recordsize = LittleLong(pchunk->recordsize);
+ numrecords = LittleLong(pchunk->numrecords);
+ if (developer_extra.integer)
+ Con_DPrintf("%s: %s %x: %i * %i = %i\n", animname, pchunk->id, version, recordsize, numrecords, recordsize * numrecords);
+ if (version != 0x1e83b9 && version != 0x1e9179 && version != 0x2e && version != 0x12f2bc && version != 0x12f2f0)
+ Con_Printf ("%s: chunk %s has unknown version %x (0x1e83b9, 0x1e9179, 0x2e, 0x12f2bc, 0x12f2f0 are currently supported), trying to load anyway!\n", animname, pchunk->id, version);
+ if (!strcmp(pchunk->id, "ANIMHEAD"))
+ {
+ // nothing to do
+ }
+ else if (!strcmp(pchunk->id, "BONENAMES"))
+ {
+ pskboneinfo_t *p;
+ if (recordsize != sizeof(*p))
+ Host_Error("%s: %s has unsupported recordsize", animname, pchunk->id);
+ // byteswap in place and keep the pointer
+ numanimbones = numrecords;
+ //animbones = (pskboneinfo_t *)animbuffer;
+ // NOTE: supposedly psa does not need to match the psk model, the
+ // bones missing from the psa would simply use their base
+ // positions from the psk, but this is hard for me to implement
+ // and people can easily make animations that match.
+ if (numanimbones != numbones)
+ Host_Error("%s: this loader only supports animations with the same bones as the mesh", loadmodel->name);
+ for (index = 0, p = (pskboneinfo_t *)animbuffer;index < numrecords;index++, p++)
+ {
+ p->numchildren = LittleLong(p->numchildren);
+ p->parent = LittleLong(p->parent);
+ p->basepose.quat[0] = LittleFloat(p->basepose.quat[0]);
+ p->basepose.quat[1] = LittleFloat(p->basepose.quat[1]);
+ p->basepose.quat[2] = LittleFloat(p->basepose.quat[2]);
+ p->basepose.quat[3] = LittleFloat(p->basepose.quat[3]);
+ p->basepose.origin[0] = LittleFloat(p->basepose.origin[0]);
+ p->basepose.origin[1] = LittleFloat(p->basepose.origin[1]);
+ p->basepose.origin[2] = LittleFloat(p->basepose.origin[2]);
+ p->basepose.unknown = LittleFloat(p->basepose.unknown);
+ p->basepose.size[0] = LittleFloat(p->basepose.size[0]);
+ p->basepose.size[1] = LittleFloat(p->basepose.size[1]);
+ p->basepose.size[2] = LittleFloat(p->basepose.size[2]);
+#ifdef PSKQUATNEGATIONS
+ if (index)
+ {
+ p->basepose.quat[0] *= -1;
+ p->basepose.quat[1] *= -1;
+ p->basepose.quat[2] *= -1;
+ }
+ else
+ {
+ p->basepose.quat[0] *= 1;
+ p->basepose.quat[1] *= -1;
+ p->basepose.quat[2] *= 1;
+ }
+#endif
+ if (p->parent < 0 || p->parent >= numanimbones)
+ {
+ Con_Printf("%s: bone->parent %i >= numanimbones %i\n", animname, p->parent, numanimbones);
+ p->parent = 0;
+ }
+ // check that bones are the same as in the base
+ if (strcmp(p->name, bones[index].name) || p->parent != bones[index].parent)
+ Host_Error("%s: this loader only supports animations with the same bones as the mesh", animname);
+ }
+ animbuffer = p;
+ }
+ else if (!strcmp(pchunk->id, "ANIMINFO"))
+ {
+ pskaniminfo_t *p;
+ if (recordsize != sizeof(*p))
+ Host_Error("%s: %s has unsupported recordsize", animname, pchunk->id);
+ // byteswap in place and keep the pointer
+ numanims = numrecords;
+ anims = (pskaniminfo_t *)animbuffer;
+ for (index = 0, p = (pskaniminfo_t *)animbuffer;index < numrecords;index++, p++)
+ {
+ p->numbones = LittleLong(p->numbones);
+ p->playtime = LittleFloat(p->playtime);
+ p->fps = LittleFloat(p->fps);
+ p->firstframe = LittleLong(p->firstframe);
+ p->numframes = LittleLong(p->numframes);
+ if (p->numbones != numbones)
+ Con_Printf("%s: animinfo->numbones != numbones, trying to load anyway!\n", animname);
+ }
+ animbuffer = p;
+ }
+ else if (!strcmp(pchunk->id, "ANIMKEYS"))
+ {
+ pskanimkeys_t *p;
+ if (recordsize != sizeof(*p))
+ Host_Error("%s: %s has unsupported recordsize", animname, pchunk->id);
+ numanimkeys = numrecords;
+ animkeys = (pskanimkeys_t *)animbuffer;
+ for (index = 0, p = (pskanimkeys_t *)animbuffer;index < numrecords;index++, p++)
+ {
+ p->origin[0] = LittleFloat(p->origin[0]);
+ p->origin[1] = LittleFloat(p->origin[1]);
+ p->origin[2] = LittleFloat(p->origin[2]);
+ p->quat[0] = LittleFloat(p->quat[0]);
+ p->quat[1] = LittleFloat(p->quat[1]);
+ p->quat[2] = LittleFloat(p->quat[2]);
+ p->quat[3] = LittleFloat(p->quat[3]);
+ p->frametime = LittleFloat(p->frametime);
+#ifdef PSKQUATNEGATIONS
+ if (index % numbones)
+ {
+ p->quat[0] *= -1;
+ p->quat[1] *= -1;
+ p->quat[2] *= -1;
+ }
+ else
+ {
+ p->quat[0] *= 1;
+ p->quat[1] *= -1;
+ p->quat[2] *= 1;
+ }
+#endif
+ }
+ animbuffer = p;
+ // TODO: allocate bonepose stuff
+ }
+ else
+ Con_Printf("%s: unknown chunk ID \"%s\"\n", animname, pchunk->id);
+ }
+
+ if (!numpnts || !pnts || !numvtxw || !vtxw || !numfaces || !faces || !nummatts || !matts || !numbones || !bones || !numrawweights || !rawweights || !numanims || !anims || !numanimkeys || !animkeys)
+ Host_Error("%s: missing required chunks", loadmodel->name);
+
+ loadmodel->numframes = 0;
+ for (index = 0;index < numanims;index++)
+ loadmodel->numframes += anims[index].numframes;
+
+ if (numanimkeys != numbones * loadmodel->numframes)
+ Host_Error("%s: %s has incorrect number of animation keys", animname, pchunk->id);
+
+ meshvertices = numvtxw;
+ meshtriangles = numfaces;
+
+ // load external .skin files if present
+ skinfiles = Mod_LoadSkinFiles();
+ if (loadmodel->numskins < 1)
+ loadmodel->numskins = 1;
+ loadmodel->num_bones = numbones;
+ loadmodel->num_poses = loadmodel->numframes;
+ loadmodel->nummodelsurfaces = loadmodel->num_surfaces = nummatts;
+ loadmodel->num_textures = loadmodel->num_surfaces * loadmodel->numskins;
+ loadmodel->num_texturesperskin = loadmodel->num_surfaces;
+ loadmodel->surfmesh.num_vertices = meshvertices;
+ loadmodel->surfmesh.num_triangles = meshtriangles;
+ // do most allocations as one merged chunk
+ size = loadmodel->num_surfaces * sizeof(msurface_t) + loadmodel->num_surfaces * sizeof(int) + loadmodel->num_surfaces * loadmodel->numskins * sizeof(texture_t) + loadmodel->surfmesh.num_triangles * sizeof(int[3]) + loadmodel->surfmesh.num_triangles * sizeof(int[3]) + loadmodel->surfmesh.num_vertices * sizeof(float[3]) + loadmodel->surfmesh.num_vertices * sizeof(float[3]) + loadmodel->surfmesh.num_vertices * sizeof(float[3]) + loadmodel->surfmesh.num_vertices * sizeof(float[3]) + loadmodel->surfmesh.num_vertices * sizeof(float[2]) + loadmodel->surfmesh.num_vertices * sizeof(unsigned short) + loadmodel->num_poses * loadmodel->num_bones * sizeof(short[6]) + loadmodel->num_bones * sizeof(float[12]) + loadmodel->numskins * sizeof(animscene_t) + loadmodel->num_bones * sizeof(aliasbone_t) + loadmodel->numframes * sizeof(animscene_t) + ((loadmodel->surfmesh.num_vertices <= 65536) ? (loadmodel->surfmesh.num_triangles * sizeof(unsigned short[3])) : 0);
+ data = (unsigned char *)Mem_Alloc(loadmodel->mempool, size);
+ loadmodel->data_surfaces = (msurface_t *)data;data += loadmodel->num_surfaces * sizeof(msurface_t);
+ loadmodel->sortedmodelsurfaces = (int *)data;data += loadmodel->num_surfaces * sizeof(int);
+ loadmodel->data_textures = (texture_t *)data;data += loadmodel->num_surfaces * loadmodel->numskins * sizeof(texture_t);
+ loadmodel->surfmesh.data_element3i = (int *)data;data += loadmodel->surfmesh.num_triangles * sizeof(int[3]);
+ loadmodel->surfmesh.data_neighbor3i = (int *)data;data += loadmodel->surfmesh.num_triangles * sizeof(int[3]);
+ loadmodel->surfmesh.data_vertex3f = (float *)data;data += loadmodel->surfmesh.num_vertices * sizeof(float[3]);
+ loadmodel->surfmesh.data_svector3f = (float *)data;data += loadmodel->surfmesh.num_vertices * sizeof(float[3]);
+ loadmodel->surfmesh.data_tvector3f = (float *)data;data += loadmodel->surfmesh.num_vertices * sizeof(float[3]);
+ loadmodel->surfmesh.data_normal3f = (float *)data;data += loadmodel->surfmesh.num_vertices * sizeof(float[3]);
+ loadmodel->surfmesh.data_texcoordtexture2f = (float *)data;data += loadmodel->surfmesh.num_vertices * sizeof(float[2]);
+ loadmodel->data_baseboneposeinverse = (float *)data;data += loadmodel->num_bones * sizeof(float[12]);
+ loadmodel->skinscenes = (animscene_t *)data;data += loadmodel->numskins * sizeof(animscene_t);
+ loadmodel->data_bones = (aliasbone_t *)data;data += loadmodel->num_bones * sizeof(aliasbone_t);
+ loadmodel->animscenes = (animscene_t *)data;data += loadmodel->numframes * sizeof(animscene_t);
+ loadmodel->surfmesh.num_blends = 0;
+ loadmodel->surfmesh.blends = (unsigned short *)data;data += meshvertices * sizeof(unsigned short);
+ if (loadmodel->surfmesh.num_vertices <= 65536)
+ loadmodel->surfmesh.data_element3s = (unsigned short *)data;data += loadmodel->surfmesh.num_triangles * sizeof(unsigned short[3]);
+ loadmodel->data_poses6s = (short *)data;data += loadmodel->num_poses * loadmodel->num_bones * sizeof(short[6]);
+ loadmodel->surfmesh.data_blendweights = (blendweights_t *)Mem_Alloc(loadmodel->mempool, loadmodel->surfmesh.num_vertices * sizeof(blendweights_t));
+
+ for (i = 0;i < loadmodel->numskins;i++)
+ {
+ loadmodel->skinscenes[i].firstframe = i;
+ loadmodel->skinscenes[i].framecount = 1;
+ loadmodel->skinscenes[i].loop = true;
+ loadmodel->skinscenes[i].framerate = 10;
+ }
+
+ // create surfaces
+ for (index = 0, i = 0;index < nummatts;index++)
+ {
+ // since psk models do not have named sections, reuse their shader name as the section name
+ Mod_BuildAliasSkinsFromSkinFiles(loadmodel->data_textures + index, skinfiles, matts[index].name, matts[index].name);
+ loadmodel->sortedmodelsurfaces[index] = index;
+ loadmodel->data_surfaces[index].texture = loadmodel->data_textures + index;
+ loadmodel->data_surfaces[index].num_firstvertex = 0;
+ loadmodel->data_surfaces[index].num_vertices = loadmodel->surfmesh.num_vertices;
+ }
+
+ // copy over the vertex locations and texcoords
+ for (index = 0;index < numvtxw;index++)
+ {
+ loadmodel->surfmesh.data_vertex3f[index*3+0] = pnts[vtxw[index].pntsindex].origin[0];
+ loadmodel->surfmesh.data_vertex3f[index*3+1] = pnts[vtxw[index].pntsindex].origin[1];
+ loadmodel->surfmesh.data_vertex3f[index*3+2] = pnts[vtxw[index].pntsindex].origin[2];
+ loadmodel->surfmesh.data_texcoordtexture2f[index*2+0] = vtxw[index].texcoord[0];
+ loadmodel->surfmesh.data_texcoordtexture2f[index*2+1] = vtxw[index].texcoord[1];
+ }
+
+ // loading the faces is complicated because we need to sort them into surfaces by mattindex
+ for (index = 0;index < numfaces;index++)
+ loadmodel->data_surfaces[faces[index].mattindex].num_triangles++;
+ for (index = 0, i = 0;index < nummatts;index++)
+ {
+ loadmodel->data_surfaces[index].num_firsttriangle = i;
+ i += loadmodel->data_surfaces[index].num_triangles;
+ loadmodel->data_surfaces[index].num_triangles = 0;
+ }
+ for (index = 0;index < numfaces;index++)
+ {
+ i = (loadmodel->data_surfaces[faces[index].mattindex].num_firsttriangle + loadmodel->data_surfaces[faces[index].mattindex].num_triangles++)*3;
+ loadmodel->surfmesh.data_element3i[i+0] = faces[index].vtxwindex[0];
+ loadmodel->surfmesh.data_element3i[i+1] = faces[index].vtxwindex[1];
+ loadmodel->surfmesh.data_element3i[i+2] = faces[index].vtxwindex[2];
+ }
+
+ // copy over the bones
+ for (index = 0;index < numbones;index++)
+ {
+ strlcpy(loadmodel->data_bones[index].name, bones[index].name, sizeof(loadmodel->data_bones[index].name));
+ loadmodel->data_bones[index].parent = (index || bones[index].parent > 0) ? bones[index].parent : -1;
+ if (loadmodel->data_bones[index].parent >= index)
+ Host_Error("%s bone[%i].parent >= %i", loadmodel->name, index, index);
+ }
+
+ // sort the psk point weights into the vertex weight tables
+ // (which only accept up to 4 bones per vertex)
+ for (index = 0;index < numvtxw;index++)
+ {
+ int weightindex[4] = { 0, 0, 0, 0 };
+ float weightinfluence[4] = { 0, 0, 0, 0 };
+ int l;
+ for (j = 0;j < numrawweights;j++)
+ {
+ if (rawweights[j].pntsindex == vtxw[index].pntsindex)
+ {
+ int boneindex = rawweights[j].boneindex;
+ float influence = rawweights[j].weight;
+ for (l = 0;l < 4;l++)
+ {
+ if (weightinfluence[l] < influence)
+ {
+ // move lower influence weights out of the way first
+ int l2;
+ for (l2 = 3;l2 > l;l2--)
+ {
+ weightinfluence[l2] = weightinfluence[l2-1];
+ weightindex[l2] = weightindex[l2-1];
+ }
+ // store the new weight
+ weightinfluence[l] = influence;
+ weightindex[l] = boneindex;
+ break;
+ }
+ }
+ }
+ }
+ loadmodel->surfmesh.blends[index] = Mod_Skeletal_CompressBlend(loadmodel, weightindex, weightinfluence);
+ }
+ if (loadmodel->surfmesh.num_blends < loadmodel->surfmesh.num_vertices)
+ loadmodel->surfmesh.data_blendweights = (blendweights_t *)Mem_Realloc(loadmodel->mempool, loadmodel->surfmesh.data_blendweights, loadmodel->surfmesh.num_blends * sizeof(blendweights_t));
+
+ // set up the animscenes based on the anims
+ for (index = 0, i = 0;index < numanims;index++)
+ {
+ for (j = 0;j < anims[index].numframes;j++, i++)
+ {
+ dpsnprintf(loadmodel->animscenes[i].name, sizeof(loadmodel->animscenes[i].name), "%s_%d", anims[index].name, j);
+ loadmodel->animscenes[i].firstframe = i;
+ loadmodel->animscenes[i].framecount = 1;
+ loadmodel->animscenes[i].loop = true;
+ loadmodel->animscenes[i].framerate = anims[index].fps;
+ }
+ }
+
+ // calculate the scaling value for bone origins so they can be compressed to short
+ biggestorigin = 0;
+ for (index = 0;index < numanimkeys;index++)
+ {
+ pskanimkeys_t *k = animkeys + index;
+ biggestorigin = max(biggestorigin, fabs(k->origin[0]));
+ biggestorigin = max(biggestorigin, fabs(k->origin[1]));
+ biggestorigin = max(biggestorigin, fabs(k->origin[2]));
+ }
+ loadmodel->num_posescale = biggestorigin / 32767.0f;
+ loadmodel->num_poseinvscale = 1.0f / loadmodel->num_posescale;
+
+ // load the poses from the animkeys
+ for (index = 0;index < numanimkeys;index++)
+ {
+ pskanimkeys_t *k = animkeys + index;
+ float quat[4];
+ Vector4Copy(k->quat, quat);
+ if (quat[3] > 0)
+ Vector4Negate(quat, quat);
+ Vector4Normalize2(quat, quat);
+ // compress poses to the short[6] format for longterm storage
+ loadmodel->data_poses6s[index*6+0] = k->origin[0] * loadmodel->num_poseinvscale;
+ loadmodel->data_poses6s[index*6+1] = k->origin[1] * loadmodel->num_poseinvscale;
+ loadmodel->data_poses6s[index*6+2] = k->origin[2] * loadmodel->num_poseinvscale;
+ loadmodel->data_poses6s[index*6+3] = quat[0] * 32767.0f;
+ loadmodel->data_poses6s[index*6+4] = quat[1] * 32767.0f;
+ loadmodel->data_poses6s[index*6+5] = quat[2] * 32767.0f;
+ }
+ Mod_FreeSkinFiles(skinfiles);
+ Mem_Free(animfilebuffer);
+ Mod_MakeSortedSurfaces(loadmodel);
+
+ // compute all the mesh information that was not loaded from the file
+ // TODO: honor smoothing groups somehow?
+ if (loadmodel->surfmesh.data_element3s)
+ for (i = 0;i < loadmodel->surfmesh.num_triangles*3;i++)
+ loadmodel->surfmesh.data_element3s[i] = loadmodel->surfmesh.data_element3i[i];
+ Mod_ValidateElements(loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.num_triangles, 0, loadmodel->surfmesh.num_vertices, __FILE__, __LINE__);
+ Mod_BuildBaseBonePoses();
+ Mod_BuildNormals(0, loadmodel->surfmesh.num_vertices, loadmodel->surfmesh.num_triangles, loadmodel->surfmesh.data_vertex3f, loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.data_normal3f, true);
+ Mod_BuildTextureVectorsFromNormals(0, loadmodel->surfmesh.num_vertices, loadmodel->surfmesh.num_triangles, loadmodel->surfmesh.data_vertex3f, loadmodel->surfmesh.data_texcoordtexture2f, loadmodel->surfmesh.data_normal3f, loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.data_svector3f, loadmodel->surfmesh.data_tvector3f, true);
+ Mod_BuildTriangleNeighbors(loadmodel->surfmesh.data_neighbor3i, loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.num_triangles);
+ Mod_Alias_CalculateBoundingBox();
+
+ loadmodel->surfmesh.isanimated = loadmodel->numframes > 1 || loadmodel->animscenes[0].framecount > 1;
+
+ if (!loadmodel->surfmesh.isanimated)
+ {
+ Mod_MakeCollisionBIH(loadmodel, true, &loadmodel->collision_bih);
+ loadmodel->TraceBox = Mod_CollisionBIH_TraceBox;
+ loadmodel->TraceBrush = Mod_CollisionBIH_TraceBrush;
+ loadmodel->TraceLine = Mod_CollisionBIH_TraceLine;
+ loadmodel->TracePoint = Mod_CollisionBIH_TracePoint_Mesh;
+ loadmodel->PointSuperContents = Mod_CollisionBIH_PointSuperContents_Mesh;
+ }
+}
+
+void Mod_INTERQUAKEMODEL_Load(dp_model_t *mod, void *buffer, void *bufferend)
+{
+ unsigned char *data;
+ const char *text;
+ unsigned char *pbase, *pend;
+ iqmheader_t *header;
+ skinfile_t *skinfiles;
+ int i, j, k, meshvertices, meshtriangles;
+ float *vposition = NULL, *vtexcoord = NULL, *vnormal = NULL, *vtangent = NULL;
+ unsigned char *vblendindexes = NULL, *vblendweights = NULL;
+ iqmjoint_t *joint;
+ iqmanim_t *anim;
+ iqmpose_t *pose;
+ iqmmesh_t *mesh;
+ iqmbounds_t *bounds;
+ iqmvertexarray_t *va;
+ unsigned short *framedata;
+ float biggestorigin;
+ const int *inelements;
+ int *outelements;
+ float *outvertex, *outnormal, *outtexcoord, *outsvector, *outtvector;
+
+ pbase = (unsigned char *)buffer;
+ pend = (unsigned char *)bufferend;
+ header = (iqmheader_t *)buffer;
+ if (memcmp(header->id, "INTERQUAKEMODEL", 16))
+ Host_Error ("Mod_INTERQUAKEMODEL_Load: %s is not an Inter-Quake Model", loadmodel->name);
+ if (LittleLong(header->version) != 1)
+ Host_Error ("Mod_INTERQUAKEMODEL_Load: only version 1 models are currently supported (name = %s)", loadmodel->name);
+
+ loadmodel->modeldatatypestring = "IQM";
+
+ loadmodel->type = mod_alias;
+ loadmodel->synctype = ST_RAND;
+
+ // byteswap header
+ header->version = LittleLong(header->version);
+ header->filesize = LittleLong(header->filesize);
+ header->flags = LittleLong(header->flags);
+ header->num_text = LittleLong(header->num_text);
+ header->ofs_text = LittleLong(header->ofs_text);
+ header->num_meshes = LittleLong(header->num_meshes);
+ header->ofs_meshes = LittleLong(header->ofs_meshes);
+ header->num_vertexarrays = LittleLong(header->num_vertexarrays);
+ header->num_vertexes = LittleLong(header->num_vertexes);
+ header->ofs_vertexarrays = LittleLong(header->ofs_vertexarrays);
+ header->num_triangles = LittleLong(header->num_triangles);
+ header->ofs_triangles = LittleLong(header->ofs_triangles);
+ header->ofs_neighbors = LittleLong(header->ofs_neighbors);
+ header->num_joints = LittleLong(header->num_joints);
+ header->ofs_joints = LittleLong(header->ofs_joints);
+ header->num_poses = LittleLong(header->num_poses);
+ header->ofs_poses = LittleLong(header->ofs_poses);
+ header->num_anims = LittleLong(header->num_anims);
+ header->ofs_anims = LittleLong(header->ofs_anims);
+ header->num_frames = LittleLong(header->num_frames);
+ header->num_framechannels = LittleLong(header->num_framechannels);
+ header->ofs_frames = LittleLong(header->ofs_frames);
+ header->ofs_bounds = LittleLong(header->ofs_bounds);
+ header->num_comment = LittleLong(header->num_comment);
+ header->ofs_comment = LittleLong(header->ofs_comment);
+ header->num_extensions = LittleLong(header->num_extensions);
+ header->ofs_extensions = LittleLong(header->ofs_extensions);
+
+ if (header->num_triangles < 1 || header->num_vertexes < 3 || header->num_vertexarrays < 1 || header->num_meshes < 1)
+ {
+ Con_Printf("%s has no geometry\n", loadmodel->name);
+ return;
+ }
+ if (header->num_frames < 1 || header->num_anims < 1)
+ {
+ Con_Printf("%s has no animations\n", loadmodel->name);
+ return;
+ }
+
+ if (pbase + header->ofs_text + header->num_text > pend ||
+ pbase + header->ofs_meshes + header->num_meshes*sizeof(iqmmesh_t) > pend ||
+ pbase + header->ofs_vertexarrays + header->num_vertexarrays*sizeof(iqmvertexarray_t) > pend ||
+ pbase + header->ofs_triangles + header->num_triangles*sizeof(int[3]) > pend ||
+ (header->ofs_neighbors && pbase + header->ofs_neighbors + header->num_triangles*sizeof(int[3]) > pend) ||
+ pbase + header->ofs_joints + header->num_joints*sizeof(iqmjoint_t) > pend ||
+ pbase + header->ofs_poses + header->num_poses*sizeof(iqmpose_t) > pend ||
+ pbase + header->ofs_anims + header->num_anims*sizeof(iqmanim_t) > pend ||
+ pbase + header->ofs_frames + header->num_frames*header->num_framechannels*sizeof(unsigned short) > pend ||
+ (header->ofs_bounds && pbase + header->ofs_bounds + header->num_frames*sizeof(iqmbounds_t) > pend) ||
+ pbase + header->ofs_comment + header->num_comment > pend)
+ {
+ Con_Printf("%s has invalid size or offset information\n", loadmodel->name);
+ return;