]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - model_alias.c
Removed collision_prefernudgedfraction cvar and trace.realfraction field, this has...
[xonotic/darkplaces.git] / model_alias.c
index fe4793a01f2f14afb5db7424a806e1b7144f2743..3da3b6202a0a54ee372fc1f50929af611d44b498 100644 (file)
@@ -21,7 +21,15 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 #include "quakedef.h"
 #include "image.h"
 #include "r_shadow.h"
+#include "mod_skeletal_animatevertices_generic.h"
+#ifdef SSE_POSSIBLE
+#include "mod_skeletal_animatevertices_sse.h"
+#endif
 
+#ifdef SSE_POSSIBLE
+static qboolean r_skeletal_use_sse_defined = false;
+cvar_t r_skeletal_use_sse = {0, "r_skeletal_use_sse", "1", "use SSE for skeletal model animation"};
+#endif
 cvar_t r_skeletal_debugbone = {0, "r_skeletal_debugbone", "-1", "development cvar for testing skeletal model code"};
 cvar_t r_skeletal_debugbonecomponent = {0, "r_skeletal_debugbonecomponent", "3", "development cvar for testing skeletal model code"};
 cvar_t r_skeletal_debugbonevalue = {0, "r_skeletal_debugbonevalue", "100", "development cvar for testing skeletal model code"};
@@ -29,32 +37,39 @@ cvar_t r_skeletal_debugtranslatex = {0, "r_skeletal_debugtranslatex", "1", "deve
 cvar_t r_skeletal_debugtranslatey = {0, "r_skeletal_debugtranslatey", "1", "development cvar for testing skeletal model code"};
 cvar_t r_skeletal_debugtranslatez = {0, "r_skeletal_debugtranslatez", "1", "development cvar for testing skeletal model code"};
 cvar_t mod_alias_supporttagscale = {0, "mod_alias_supporttagscale", "1", "support scaling factors in bone/tag attachment matrices as supported by MD3"};
+cvar_t mod_alias_force_animated = {0, "mod_alias_force_animated", "", "if set to an non-empty string, overrides the is-animated flag of any alias models (for benchmarking)"};
 
 float mod_md3_sin[320];
 
-void Mod_AliasInit (void)
+static size_t Mod_Skeletal_AnimateVertices_maxbonepose = 0;
+static void *Mod_Skeletal_AnimateVertices_bonepose = NULL;
+void Mod_Skeletal_FreeBuffers(void)
 {
-       int i;
-       Cvar_RegisterVariable(&r_skeletal_debugbone);
-       Cvar_RegisterVariable(&r_skeletal_debugbonecomponent);
-       Cvar_RegisterVariable(&r_skeletal_debugbonevalue);
-       Cvar_RegisterVariable(&r_skeletal_debugtranslatex);
-       Cvar_RegisterVariable(&r_skeletal_debugtranslatey);
-       Cvar_RegisterVariable(&r_skeletal_debugtranslatez);
-       Cvar_RegisterVariable(&mod_alias_supporttagscale);
-       for (i = 0;i < 320;i++)
-               mod_md3_sin[i] = sin(i * M_PI * 2.0f / 256.0);
+       if(Mod_Skeletal_AnimateVertices_bonepose)
+               Mem_Free(Mod_Skeletal_AnimateVertices_bonepose);
+       Mod_Skeletal_AnimateVertices_maxbonepose = 0;
+       Mod_Skeletal_AnimateVertices_bonepose = NULL;
+}
+void *Mod_Skeletal_AnimateVertices_AllocBuffers(size_t nbytes)
+{
+       if(Mod_Skeletal_AnimateVertices_maxbonepose < nbytes)
+       {
+               if(Mod_Skeletal_AnimateVertices_bonepose)
+                       Mem_Free(Mod_Skeletal_AnimateVertices_bonepose);
+               Mod_Skeletal_AnimateVertices_bonepose = Z_Malloc(nbytes);
+               Mod_Skeletal_AnimateVertices_maxbonepose = nbytes;
+       }
+       return Mod_Skeletal_AnimateVertices_bonepose;
 }
 
-void Mod_Skeletal_AnimateVertices(const dp_model_t *model, const frameblend_t *frameblend, const skeleton_t *skeleton, float *vertex3f, float *normal3f, float *svector3f, float *tvector3f)
+void Mod_Skeletal_BuildTransforms(const dp_model_t * RESTRICT model, const frameblend_t * RESTRICT frameblend, const skeleton_t *skeleton, float * RESTRICT bonepose, float * RESTRICT boneposerelative)
 {
-#define MAX_BONES 256
-       // vertex weighted skeletal
-       int i, k;
-       int blends;
-       float boneposerelative[MAX_BONES][12];
-       float m[12], bonepose[MAX_BONES][12];
+       int i, blends;
+       float m[12];
 
+       if (!bonepose)
+               bonepose = (float * RESTRICT) Mod_Skeletal_AnimateVertices_AllocBuffers(sizeof(float[12]) * model->num_bones);
+               
        if (skeleton && !skeleton->relativetransforms)
                skeleton = NULL;
 
@@ -65,204 +80,200 @@ void Mod_Skeletal_AnimateVertices(const dp_model_t *model, const frameblend_t *f
                {
                        Matrix4x4_ToArray12FloatD3D(&skeleton->relativetransforms[i], m);
                        if (model->data_bones[i].parent >= 0)
-                               R_ConcatTransforms(bonepose[model->data_bones[i].parent], m, bonepose[i]);
+                               R_ConcatTransforms(bonepose + model->data_bones[i].parent * 12, m, bonepose + i * 12);
                        else
-                               for (k = 0;k < 12;k++)
-                                       bonepose[i][k] = m[k];
-       
+                               memcpy(bonepose + i * 12, m, sizeof(m));
+
                        // create a relative deformation matrix to describe displacement
                        // from the base mesh, which is used by the actual weighting
-                       R_ConcatTransforms(bonepose[i], model->data_baseboneposeinverse + i * 12, boneposerelative[i]);
+                       R_ConcatTransforms(bonepose + i * 12, model->data_baseboneposeinverse + i * 12, boneposerelative + i * 12);
                }
        }
        else
        {
-               float originscale = model->num_posescale;
-               float x,y,z,w,lerp;
-               const short *pose6s;
                for (i = 0;i < model->num_bones;i++)
                {
-                       for (k = 0;k < 12;k++)
-                               m[k] = 0;
-                       for (blends = 0;blends < MAX_FRAMEBLENDS && frameblend[blends].lerp > 0;blends++)
+                       // blend by transform each quaternion/translation into a dual-quaternion first, then blending
+                       const short * RESTRICT pose7s = model->data_poses7s + 7 * (frameblend[0].subframe * model->num_bones + i);
+                       float lerp = frameblend[0].lerp,
+                               tx = pose7s[0], ty = pose7s[1], tz = pose7s[2],
+                               rx = pose7s[3] * lerp,
+                               ry = pose7s[4] * lerp,
+                               rz = pose7s[5] * lerp,
+                               rw = pose7s[6] * lerp,
+                               dx = tx*rw + ty*rz - tz*ry,
+                               dy = -tx*rz + ty*rw + tz*rx,
+                               dz = tx*ry - ty*rx + tz*rw,
+                               dw = -tx*rx - ty*ry - tz*rz,
+                               scale, sx, sy, sz, sw;
+                       for (blends = 1;blends < MAX_FRAMEBLENDS && frameblend[blends].lerp > 0;blends++)
                        {
-                               pose6s = model->data_poses6s + 6 * (frameblend[blends].subframe * model->num_bones + i);
-                               lerp = frameblend[blends].lerp;
-                               x = pose6s[3] * (1.0f / 32767.0f);
-                               y = pose6s[4] * (1.0f / 32767.0f);
-                               z = pose6s[5] * (1.0f / 32767.0f);
-                               w = 1.0f - (x*x+y*y+z*z);
-                               w = w > 0.0f ? -sqrt(w) : 0.0f;
-                               m[ 0] += (1-2*(y*y+z*z)) * lerp;
-                               m[ 1] += (  2*(x*y-z*w)) * lerp;
-                               m[ 2] += (  2*(x*z+y*w)) * lerp;
-                               m[ 3] += (pose6s[0] * originscale) * lerp;
-                               m[ 4] += (  2*(x*y+z*w)) * lerp;
-                               m[ 5] += (1-2*(x*x+z*z)) * lerp;
-                               m[ 6] += (  2*(y*z-x*w)) * lerp;
-                               m[ 7] += (pose6s[1] * originscale) * lerp;
-                               m[ 8] += (  2*(x*z-y*w)) * lerp;
-                               m[ 9] += (  2*(y*z+x*w)) * lerp;
-                               m[10] += (1-2*(x*x+y*y)) * lerp;
-                               m[11] += (pose6s[2] * originscale) * lerp;
+                               const short * RESTRICT pose7s = model->data_poses7s + 7 * (frameblend[blends].subframe * model->num_bones + i);
+                               float lerp = frameblend[blends].lerp,
+                                       tx = pose7s[0], ty = pose7s[1], tz = pose7s[2],
+                                       qx = pose7s[3], qy = pose7s[4], qz = pose7s[5], qw = pose7s[6];
+                               if(rx*qx + ry*qy + rz*qz + rw*qw < 0) lerp = -lerp;
+                               qx *= lerp;
+                               qy *= lerp;
+                               qz *= lerp;
+                               qw *= lerp;
+                               rx += qx;
+                               ry += qy;
+                               rz += qz;
+                               rw += qw;
+                               dx += tx*qw + ty*qz - tz*qy;
+                               dy += -tx*qz + ty*qw + tz*qx;
+                               dz += tx*qy - ty*qx + tz*qw;
+                               dw += -tx*qx - ty*qy - tz*qz;
                        }
-                       VectorNormalize(m    );
-                       VectorNormalize(m + 4);
-                       VectorNormalize(m + 8);
+                       // generate a matrix from the dual-quaternion, implicitly normalizing it in the process
+                       scale = 1.0f / (rx*rx + ry*ry + rz*rz + rw*rw);
+                       sx = rx * scale;
+                       sy = ry * scale;
+                       sz = rz * scale;
+                       sw = rw * scale;
+                       m[0] = sw*rw + sx*rx - sy*ry - sz*rz;
+                       m[1] = 2*(sx*ry - sw*rz);
+                       m[2] = 2*(sx*rz + sw*ry);
+                       m[3] = model->num_posescale*(dx*sw - dy*sz + dz*sy - dw*sx);
+                       m[4] = 2*(sx*ry + sw*rz);
+                       m[5] = sw*rw + sy*ry - sx*rx - sz*rz;
+                       m[6] = 2*(sy*rz - sw*rx);
+                       m[7] = model->num_posescale*(dx*sz + dy*sw - dz*sx - dw*sy);
+                       m[8] = 2*(sx*rz - sw*ry);
+                       m[9] = 2*(sy*rz + sw*rx);
+                       m[10] = sw*rw + sz*rz - sx*rx - sy*ry;
+                       m[11] = model->num_posescale*(dy*sx + dz*sw - dx*sy - dw*sz);
                        if (i == r_skeletal_debugbone.integer)
                                m[r_skeletal_debugbonecomponent.integer % 12] += r_skeletal_debugbonevalue.value;
                        m[3] *= r_skeletal_debugtranslatex.value;
                        m[7] *= r_skeletal_debugtranslatey.value;
                        m[11] *= r_skeletal_debugtranslatez.value;
                        if (model->data_bones[i].parent >= 0)
-                               R_ConcatTransforms(bonepose[model->data_bones[i].parent], m, bonepose[i]);
+                               R_ConcatTransforms(bonepose + model->data_bones[i].parent * 12, m, bonepose + i * 12);
                        else
-                               for (k = 0;k < 12;k++)
-                                       bonepose[i][k] = m[k];
+                               memcpy(bonepose + i * 12, m, sizeof(m));
                        // create a relative deformation matrix to describe displacement
                        // from the base mesh, which is used by the actual weighting
-                       R_ConcatTransforms(bonepose[i], model->data_baseboneposeinverse + i * 12, boneposerelative[i]);
+                       R_ConcatTransforms(bonepose + i * 12, model->data_baseboneposeinverse + i * 12, boneposerelative + i * 12);
                }
        }
+}
+
+static void Mod_Skeletal_AnimateVertices(const dp_model_t * RESTRICT model, const frameblend_t * RESTRICT frameblend, const skeleton_t *skeleton, float * RESTRICT vertex3f, float * RESTRICT normal3f, float * RESTRICT svector3f, float * RESTRICT tvector3f)
+{
+
+       if (!model->surfmesh.num_vertices)
+               return;
 
-       // blend the vertex bone weights
-       // special case for the extremely common wf[0] == 1 because it saves 3 multiplies per array when compared to the other case (w[0] is always 1 if only one bone controls this vertex, artists only use multiple bones for certain special cases)
-       // special case for the first bone because it avoids the need to memset the arrays before filling
-       if (vertex3f)
+       if (!model->num_bones)
        {
-               const float *v = model->surfmesh.data_vertex3f;
-               const int *wi = model->surfmesh.data_vertexweightindex4i;
-               const float *wf = model->surfmesh.data_vertexweightinfluence4f;
-               memset(vertex3f, 0, sizeof(float[3]) * model->surfmesh.num_vertices);
-               for (i = 0;i < model->surfmesh.num_vertices;i++, v += 3, wi += 4, wf += 4, vertex3f += 3)
-               {
-                       if (wf[0] == 1)
-                       {
-                               const float *m = boneposerelative[wi[0]];
-                               vertex3f[0] = (v[0] * m[0] + v[1] * m[1] + v[2] * m[ 2] + m[ 3]);
-                               vertex3f[1] = (v[0] * m[4] + v[1] * m[5] + v[2] * m[ 6] + m[ 7]);
-                               vertex3f[2] = (v[0] * m[8] + v[1] * m[9] + v[2] * m[10] + m[11]);
-                       }
-                       else
-                       {
-                               const float *m = boneposerelative[wi[0]];
-                               float f = wf[0];
-                               vertex3f[0] = f * (v[0] * m[0] + v[1] * m[1] + v[2] * m[ 2] + m[ 3]);
-                               vertex3f[1] = f * (v[0] * m[4] + v[1] * m[5] + v[2] * m[ 6] + m[ 7]);
-                               vertex3f[2] = f * (v[0] * m[8] + v[1] * m[9] + v[2] * m[10] + m[11]);
-                               for (k = 1;k < 4 && wf[k];k++)
-                               {
-                                       const float *m = boneposerelative[wi[k]];
-                                       float f = wf[k];
-                                       vertex3f[0] += f * (v[0] * m[0] + v[1] * m[1] + v[2] * m[ 2] + m[ 3]);
-                                       vertex3f[1] += f * (v[0] * m[4] + v[1] * m[5] + v[2] * m[ 6] + m[ 7]);
-                                       vertex3f[2] += f * (v[0] * m[8] + v[1] * m[9] + v[2] * m[10] + m[11]);
-                               }
-                       }
-               }
+               if (vertex3f) memcpy(vertex3f, model->surfmesh.data_vertex3f, model->surfmesh.num_vertices*sizeof(float[3]));
+               if (normal3f) memcpy(normal3f, model->surfmesh.data_normal3f, model->surfmesh.num_vertices*sizeof(float[3]));
+               if (svector3f) memcpy(svector3f, model->surfmesh.data_svector3f, model->surfmesh.num_vertices*sizeof(float[3]));
+               if (tvector3f) memcpy(tvector3f, model->surfmesh.data_tvector3f, model->surfmesh.num_vertices*sizeof(float[3]));
+               return;
        }
-       if (normal3f)
-       {
-               const float *n = model->surfmesh.data_normal3f;
-               const int *wi = model->surfmesh.data_vertexweightindex4i;
-               const float *wf = model->surfmesh.data_vertexweightinfluence4f;
-               memset(normal3f, 0, sizeof(float[3]) * model->surfmesh.num_vertices);
-               for (i = 0;i < model->surfmesh.num_vertices;i++, n += 3, wi += 4, wf += 4, normal3f += 3)
+
+#ifdef SSE_POSSIBLE
+       if(r_skeletal_use_sse_defined)
+               if(r_skeletal_use_sse.integer)
                {
-                       if (wf[0] == 1)
-                       {
-                               const float *m = boneposerelative[wi[0]];
-                               normal3f[0] = (n[0] * m[0] + n[1] * m[1] + n[2] * m[ 2]);
-                               normal3f[1] = (n[0] * m[4] + n[1] * m[5] + n[2] * m[ 6]);
-                               normal3f[2] = (n[0] * m[8] + n[1] * m[9] + n[2] * m[10]);
-                       }
-                       else
-                       {
-                               const float *m = boneposerelative[wi[0]];
-                               float f = wf[0];
-                               normal3f[0] = f * (n[0] * m[0] + n[1] * m[1] + n[2] * m[ 2]);
-                               normal3f[1] = f * (n[0] * m[4] + n[1] * m[5] + n[2] * m[ 6]);
-                               normal3f[2] = f * (n[0] * m[8] + n[1] * m[9] + n[2] * m[10]);
-                               for (k = 1;k < 4 && wf[k];k++)
-                               {
-                                       const float *m = boneposerelative[wi[k]];
-                                       float f = wf[k];
-                                       normal3f[0] += f * (n[0] * m[0] + n[1] * m[1] + n[2] * m[ 2]);
-                                       normal3f[1] += f * (n[0] * m[4] + n[1] * m[5] + n[2] * m[ 6]);
-                                       normal3f[2] += f * (n[0] * m[8] + n[1] * m[9] + n[2] * m[10]);
-                               }
-                       }
+                       Mod_Skeletal_AnimateVertices_SSE(model, frameblend, skeleton, vertex3f, normal3f, svector3f, tvector3f);
+                       return;
                }
-       }
-       if (svector3f)
+#endif
+       Mod_Skeletal_AnimateVertices_Generic(model, frameblend, skeleton, vertex3f, normal3f, svector3f, tvector3f);
+}
+
+void Mod_AliasInit (void)
+{
+       int i;
+       Cvar_RegisterVariable(&r_skeletal_debugbone);
+       Cvar_RegisterVariable(&r_skeletal_debugbonecomponent);
+       Cvar_RegisterVariable(&r_skeletal_debugbonevalue);
+       Cvar_RegisterVariable(&r_skeletal_debugtranslatex);
+       Cvar_RegisterVariable(&r_skeletal_debugtranslatey);
+       Cvar_RegisterVariable(&r_skeletal_debugtranslatez);
+       Cvar_RegisterVariable(&mod_alias_supporttagscale);
+       Cvar_RegisterVariable(&mod_alias_force_animated);
+       for (i = 0;i < 320;i++)
+               mod_md3_sin[i] = sin(i * M_PI * 2.0f / 256.0);
+#ifdef SSE_POSSIBLE
+       if(Sys_HaveSSE())
        {
-               const float *sv = model->surfmesh.data_svector3f;
-               const int *wi = model->surfmesh.data_vertexweightindex4i;
-               const float *wf = model->surfmesh.data_vertexweightinfluence4f;
-               memset(svector3f, 0, sizeof(float[3]) * model->surfmesh.num_vertices);
-               for (i = 0;i < model->surfmesh.num_vertices;i++, sv += 3, wi += 4, wf += 4, svector3f += 3)
+               Con_Printf("Skeletal animation uses SSE code path\n");
+               r_skeletal_use_sse_defined = true;
+               Cvar_RegisterVariable(&r_skeletal_use_sse);
+       }
+       else
+               Con_Printf("Skeletal animation uses generic code path (SSE disabled or not detected)\n");
+#else
+       Con_Printf("Skeletal animation uses generic code path (SSE not compiled in)\n");
+#endif
+}
+
+static int Mod_Skeletal_AddBlend(dp_model_t *model, const blendweights_t *newweights)
+{
+       int i;
+       blendweights_t *weights;
+       if(!newweights->influence[1])
+               return newweights->index[0];
+       weights = model->surfmesh.data_blendweights;
+       for (i = 0;i < model->surfmesh.num_blends;i++, weights++)
+       {
+               if (!memcmp(weights, newweights, sizeof(blendweights_t)))
+                       return model->num_bones + i;
+       }
+       model->surfmesh.num_blends++;
+       memcpy(weights, newweights, sizeof(blendweights_t));
+       return model->num_bones + i;
+}
+
+static int Mod_Skeletal_CompressBlend(dp_model_t *model, const int *newindex, const float *newinfluence)
+{
+       int i, total;
+       float scale;
+       blendweights_t newweights;
+       if(!newinfluence[1])
+               return newindex[0];
+       scale = 0;
+       for (i = 0;i < 4;i++)
+               scale += newinfluence[i];
+       scale = 255.0f / scale;
+       total = 0;
+       for (i = 0;i < 4;i++)
+       {
+               newweights.index[i] = newindex[i];
+               newweights.influence[i] = (unsigned char)(newinfluence[i] * scale);
+               total += newweights.influence[i];
+       }       
+       while (total > 255)
+       {
+               for (i = 0;i < 4;i++)
                {
-                       if (wf[0] == 1)
-                       {
-                               const float *m = boneposerelative[wi[0]];
-                               svector3f[0] = (sv[0] * m[0] + sv[1] * m[1] + sv[2] * m[ 2]);
-                               svector3f[1] = (sv[0] * m[4] + sv[1] * m[5] + sv[2] * m[ 6]);
-                               svector3f[2] = (sv[0] * m[8] + sv[1] * m[9] + sv[2] * m[10]);
-                       }
-                       else
-                       {
-                               const float *m = boneposerelative[wi[0]];
-                               float f = wf[0];
-                               svector3f[0] = f * (sv[0] * m[0] + sv[1] * m[1] + sv[2] * m[ 2]);
-                               svector3f[1] = f * (sv[0] * m[4] + sv[1] * m[5] + sv[2] * m[ 6]);
-                               svector3f[2] = f * (sv[0] * m[8] + sv[1] * m[9] + sv[2] * m[10]);
-                               for (k = 1;k < 4 && wf[k];k++)
-                               {
-                                       const float *m = boneposerelative[wi[k]];
-                                       float f = wf[k];
-                                       svector3f[0] += f * (sv[0] * m[0] + sv[1] * m[1] + sv[2] * m[ 2]);
-                                       svector3f[1] += f * (sv[0] * m[4] + sv[1] * m[5] + sv[2] * m[ 6]);
-                                       svector3f[2] += f * (sv[0] * m[8] + sv[1] * m[9] + sv[2] * m[10]);
-                               }
+                       if(newweights.influence[i] > 0 && total > 255) 
+                       { 
+                               newweights.influence[i]--;
+                               total--; 
                        }
                }
        }
-       if (tvector3f)
+       while (total < 255)
        {
-               const float *tv = model->surfmesh.data_tvector3f;
-               const int *wi = model->surfmesh.data_vertexweightindex4i;
-               const float *wf = model->surfmesh.data_vertexweightinfluence4f;
-               memset(tvector3f, 0, sizeof(float[3]) * model->surfmesh.num_vertices);
-               for (i = 0;i < model->surfmesh.num_vertices;i++, tv += 3, wi += 4, wf += 4, tvector3f += 3)
+               for (i = 0; i < 4;i++)
                {
-                       if (wf[0] == 1)
-                       {
-                               const float *m = boneposerelative[wi[0]];
-                               tvector3f[0] = (tv[0] * m[0] + tv[1] * m[1] + tv[2] * m[ 2]);
-                               tvector3f[1] = (tv[0] * m[4] + tv[1] * m[5] + tv[2] * m[ 6]);
-                               tvector3f[2] = (tv[0] * m[8] + tv[1] * m[9] + tv[2] * m[10]);
-                       }
-                       else
-                       {
-                               const float *m = boneposerelative[wi[0]];
-                               float f = wf[0];
-                               tvector3f[0] = f * (tv[0] * m[0] + tv[1] * m[1] + tv[2] * m[ 2]);
-                               tvector3f[1] = f * (tv[0] * m[4] + tv[1] * m[5] + tv[2] * m[ 6]);
-                               tvector3f[2] = f * (tv[0] * m[8] + tv[1] * m[9] + tv[2] * m[10]);
-                               for (k = 1;k < 4 && wf[k];k++)
-                               {
-                                       const float *m = boneposerelative[wi[k]];
-                                       float f = wf[k];
-                                       tvector3f[0] += f * (tv[0] * m[0] + tv[1] * m[1] + tv[2] * m[ 2]);
-                                       tvector3f[1] += f * (tv[0] * m[4] + tv[1] * m[5] + tv[2] * m[ 6]);
-                                       tvector3f[2] += f * (tv[0] * m[8] + tv[1] * m[9] + tv[2] * m[10]);
-                               }
+                       if(newweights.influence[i] < 255 && total < 255) 
+                       { 
+                               newweights.influence[i]++; 
+                               total++; 
                        }
                }
        }
+       return Mod_Skeletal_AddBlend(model, &newweights);
 }
 
-void Mod_MD3_AnimateVertices(const dp_model_t *model, const frameblend_t *frameblend, const skeleton_t *skeleton, float *vertex3f, float *normal3f, float *svector3f, float *tvector3f)
+static void Mod_MD3_AnimateVertices(const dp_model_t * RESTRICT model, const frameblend_t * RESTRICT frameblend, const skeleton_t *skeleton, float * RESTRICT vertex3f, float * RESTRICT normal3f, float * RESTRICT svector3f, float * RESTRICT tvector3f)
 {
        // vertex morph
        int i, numblends, blendnum;
@@ -302,7 +313,7 @@ void Mod_MD3_AnimateVertices(const dp_model_t *model, const frameblend_t *frameb
                }
                // the yaw and pitch stored in md3 models are 8bit quantized angles
                // (0-255), and as such a lookup table is very well suited to
-               // decoding them, and since cosine is equivilant to sine with an
+               // decoding them, and since cosine is equivalent to sine with an
                // extra 45 degree rotation, this uses one lookup table for both
                // sine and cosine with a +64 bias to get cosine.
                if (normal3f)
@@ -350,8 +361,7 @@ void Mod_MD3_AnimateVertices(const dp_model_t *model, const frameblend_t *frameb
                }
        }
 }
-
-void Mod_MDL_AnimateVertices(const dp_model_t *model, const frameblend_t *frameblend, const skeleton_t *skeleton, float *vertex3f, float *normal3f, float *svector3f, float *tvector3f)
+static void Mod_MDL_AnimateVertices(const dp_model_t * RESTRICT model, const frameblend_t * RESTRICT frameblend, const skeleton_t *skeleton, float * RESTRICT vertex3f, float * RESTRICT normal3f, float * RESTRICT svector3f, float * RESTRICT tvector3f)
 {
        // vertex morph
        int i, numblends, blendnum;
@@ -481,11 +491,11 @@ int Mod_Alias_GetTagMatrix(const dp_model_t *model, const frameblend_t *frameble
                for (blendindex = 0;blendindex < MAX_FRAMEBLENDS && frameblend[blendindex].lerp > 0;blendindex++)
                {
                        lerp = frameblend[blendindex].lerp;
-                       Matrix4x4_FromBonePose6s(&bonematrix, model->num_posescale, model->data_poses6s + 6 * (frameblend[blendindex].subframe * model->num_bones + tagindex));
+                       Matrix4x4_FromBonePose7s(&bonematrix, model->num_posescale, model->data_poses7s + 7 * (frameblend[blendindex].subframe * model->num_bones + tagindex));
                        parenttagindex = tagindex;
                        while ((parenttagindex = model->data_bones[parenttagindex].parent) >= 0)
                        {
-                               Matrix4x4_FromBonePose6s(&parentbonematrix, model->num_posescale, model->data_poses6s + 6 * (frameblend[blendindex].subframe * model->num_bones + parenttagindex));
+                               Matrix4x4_FromBonePose7s(&parentbonematrix, model->num_posescale, model->data_poses7s + 7 * (frameblend[blendindex].subframe * model->num_bones + parenttagindex));
                                tempbonematrix = bonematrix;
                                Matrix4x4_Concat(&bonematrix, &parentbonematrix, &tempbonematrix);
                        }
@@ -544,7 +554,7 @@ int Mod_Alias_GetExtendedTagInfoForIndex(const dp_model_t *model, unsigned int s
                for (blendindex = 0;blendindex < MAX_FRAMEBLENDS && frameblend[blendindex].lerp > 0;blendindex++)
                {
                        lerp = frameblend[blendindex].lerp;
-                       Matrix4x4_FromBonePose6s(&bonematrix, model->num_posescale, model->data_poses6s + 6 * (frameblend[blendindex].subframe * model->num_bones + tagindex));
+                       Matrix4x4_FromBonePose7s(&bonematrix, model->num_posescale, model->data_poses7s + 7 * (frameblend[blendindex].subframe * model->num_bones + tagindex));
                        Matrix4x4_Accumulate(&blendmatrix, &bonematrix, lerp);
                }
                *tag_localmatrix = blendmatrix;
@@ -600,7 +610,7 @@ static void Mod_BuildBaseBonePoses(void)
        basebonepose = (matrix4x4_t *)Mem_Alloc(tempmempool, loadmodel->num_bones * sizeof(matrix4x4_t));
        for (boneindex = 0;boneindex < loadmodel->num_bones;boneindex++)
        {
-               Matrix4x4_FromBonePose6s(&bonematrix, loadmodel->num_posescale, loadmodel->data_poses6s + 6 * boneindex);
+               Matrix4x4_FromBonePose7s(&bonematrix, loadmodel->num_posescale, loadmodel->data_poses7s + 7 * boneindex);
                if (loadmodel->data_bones[boneindex].parent >= 0)
                {
                        tempbonematrix = bonematrix;
@@ -613,25 +623,70 @@ static void Mod_BuildBaseBonePoses(void)
        Mem_Free(basebonepose);
 }
 
-static void Mod_Alias_CalculateBoundingBox(void)
+static qboolean Mod_Alias_CalculateBoundingBox(void)
 {
        int vnum;
        qboolean firstvertex = true;
        float dist, yawradius, radius;
        float *v;
-       float *vertex3f;
-       frameblend_t frameblend[MAX_FRAMEBLENDS];
-       memset(frameblend, 0, sizeof(frameblend));
-       frameblend[0].lerp = 1;
-       vertex3f = (float *) Mem_Alloc(loadmodel->mempool, loadmodel->surfmesh.num_vertices * sizeof(float[3]));
+       qboolean isanimated = false;
        VectorClear(loadmodel->normalmins);
        VectorClear(loadmodel->normalmaxs);
        yawradius = 0;
        radius = 0;
-       for (frameblend[0].subframe = 0;frameblend[0].subframe < loadmodel->num_poses;frameblend[0].subframe++)
+       if (loadmodel->AnimateVertices)
+       {
+               float *vertex3f, *refvertex3f;
+               frameblend_t frameblend[MAX_FRAMEBLENDS];
+               memset(frameblend, 0, sizeof(frameblend));
+               frameblend[0].lerp = 1;
+               vertex3f = (float *) Mem_Alloc(loadmodel->mempool, loadmodel->surfmesh.num_vertices * sizeof(float[3]) * 2);
+               refvertex3f = NULL;
+               for (frameblend[0].subframe = 0;frameblend[0].subframe < loadmodel->num_poses;frameblend[0].subframe++)
+               {
+                       loadmodel->AnimateVertices(loadmodel, frameblend, NULL, vertex3f, NULL, NULL, NULL);
+                       if (!refvertex3f)
+                       {
+                               // make a copy of the first frame for comparing all others
+                               refvertex3f = vertex3f + loadmodel->surfmesh.num_vertices * 3;
+                               memcpy(refvertex3f, vertex3f, loadmodel->surfmesh.num_vertices * sizeof(float[3]));
+                       }
+                       else
+                       {
+                               if (!isanimated && memcmp(refvertex3f, vertex3f, loadmodel->surfmesh.num_vertices * sizeof(float[3])))
+                                       isanimated = true;
+                       }
+                       for (vnum = 0, v = vertex3f;vnum < loadmodel->surfmesh.num_vertices;vnum++, v += 3)
+                       {
+                               if (firstvertex)
+                               {
+                                       firstvertex = false;
+                                       VectorCopy(v, loadmodel->normalmins);
+                                       VectorCopy(v, loadmodel->normalmaxs);
+                               }
+                               else
+                               {
+                                       if (loadmodel->normalmins[0] > v[0]) loadmodel->normalmins[0] = v[0];
+                                       if (loadmodel->normalmins[1] > v[1]) loadmodel->normalmins[1] = v[1];
+                                       if (loadmodel->normalmins[2] > v[2]) loadmodel->normalmins[2] = v[2];
+                                       if (loadmodel->normalmaxs[0] < v[0]) loadmodel->normalmaxs[0] = v[0];
+                                       if (loadmodel->normalmaxs[1] < v[1]) loadmodel->normalmaxs[1] = v[1];
+                                       if (loadmodel->normalmaxs[2] < v[2]) loadmodel->normalmaxs[2] = v[2];
+                               }
+                               dist = v[0] * v[0] + v[1] * v[1];
+                               if (yawradius < dist)
+                                       yawradius = dist;
+                               dist += v[2] * v[2];
+                               if (radius < dist)
+                                       radius = dist;
+                       }
+               }
+               if (vertex3f)
+                       Mem_Free(vertex3f);
+       }
+       else
        {
-               loadmodel->AnimateVertices(loadmodel, frameblend, NULL, vertex3f, NULL, NULL, NULL);
-               for (vnum = 0, v = vertex3f;vnum < loadmodel->surfmesh.num_vertices;vnum++, v += 3)
+               for (vnum = 0, v = loadmodel->surfmesh.data_vertex3f;vnum < loadmodel->surfmesh.num_vertices;vnum++, v += 3)
                {
                        if (firstvertex)
                        {
@@ -656,8 +711,6 @@ static void Mod_Alias_CalculateBoundingBox(void)
                                radius = dist;
                }
        }
-       if (vertex3f)
-               Mem_Free(vertex3f);
        radius = sqrt(radius);
        yawradius = sqrt(yawradius);
        loadmodel->yawmins[0] = loadmodel->yawmins[1] = -yawradius;
@@ -668,6 +721,7 @@ static void Mod_Alias_CalculateBoundingBox(void)
        loadmodel->rotatedmaxs[0] = loadmodel->rotatedmaxs[1] = loadmodel->rotatedmaxs[2] = radius;
        loadmodel->radius = radius;
        loadmodel->radius2 = radius * radius;
+       return isanimated;
 }
 
 static void Mod_Alias_MorphMesh_CompileFrames(void)
@@ -703,19 +757,13 @@ static void Mod_MDLMD2MD3_TraceLine(dp_model_t *model, const frameblend_t *frame
        int i;
        float segmentmins[3], segmentmaxs[3];
        msurface_t *surface;
-       static int maxvertices = 0;
-       static float *vertex3f = NULL;
+       float vertex3fbuf[1024*3];
+       float *vertex3f = vertex3fbuf;
        memset(trace, 0, sizeof(*trace));
        trace->fraction = 1;
-       trace->realfraction = 1;
        trace->hitsupercontentsmask = hitsupercontentsmask;
-       if (maxvertices < model->surfmesh.num_vertices)
-       {
-               if (vertex3f)
-                       Z_Free(vertex3f);
-               maxvertices = (model->surfmesh.num_vertices + 255) & ~255;
-               vertex3f = (float *)Z_Malloc(maxvertices * sizeof(float[3]));
-       }
+       if (model->surfmesh.num_vertices > 1024)
+               vertex3f = (float *)Mem_Alloc(tempmempool, model->surfmesh.num_vertices * sizeof(float[3]));
        segmentmins[0] = min(start[0], end[0]) - 1;
        segmentmins[1] = min(start[1], end[1]) - 1;
        segmentmins[2] = min(start[2], end[2]) - 1;
@@ -725,17 +773,18 @@ static void Mod_MDLMD2MD3_TraceLine(dp_model_t *model, const frameblend_t *frame
        model->AnimateVertices(model, frameblend, skeleton, vertex3f, NULL, NULL, NULL);
        for (i = 0, surface = model->data_surfaces;i < model->num_surfaces;i++, surface++)
                Collision_TraceLineTriangleMeshFloat(trace, start, end, model->surfmesh.num_triangles, model->surfmesh.data_element3i, vertex3f, 0, NULL, SUPERCONTENTS_SOLID | (surface->texture->basematerialflags & MATERIALFLAGMASK_TRANSLUCENT ? 0 : SUPERCONTENTS_OPAQUE), 0, surface->texture, segmentmins, segmentmaxs);
+       if (vertex3f != vertex3fbuf)
+               Mem_Free(vertex3f);
 }
 
-static int maxvertices = 0;
-static float *vertex3f = NULL;
-
 static void Mod_MDLMD2MD3_TraceBox(dp_model_t *model, const frameblend_t *frameblend, const skeleton_t *skeleton, trace_t *trace, const vec3_t start, const vec3_t boxmins, const vec3_t boxmaxs, const vec3_t end, int hitsupercontentsmask)
 {
        int i;
        vec3_t shiftstart, shiftend;
        float segmentmins[3], segmentmaxs[3];
        msurface_t *surface;
+       float vertex3fbuf[1024*3];
+       float *vertex3f = vertex3fbuf;
        colboxbrushf_t thisbrush_start, thisbrush_end;
        vec3_t boxstartmins, boxstartmaxs, boxendmins, boxendmaxs;
 
@@ -751,15 +800,9 @@ static void Mod_MDLMD2MD3_TraceBox(dp_model_t *model, const frameblend_t *frameb
        // box trace, performed as brush trace
        memset(trace, 0, sizeof(*trace));
        trace->fraction = 1;
-       trace->realfraction = 1;
        trace->hitsupercontentsmask = hitsupercontentsmask;
-       if (maxvertices < model->surfmesh.num_vertices)
-       {
-               if (vertex3f)
-                       Z_Free(vertex3f);
-               maxvertices = (model->surfmesh.num_vertices + 255) & ~255;
-               vertex3f = (float *)Z_Malloc(maxvertices * sizeof(float[3]));
-       }
+       if (model->surfmesh.num_vertices > 1024)
+               vertex3f = (float *)Mem_Alloc(tempmempool, model->surfmesh.num_vertices * sizeof(float[3]));
        segmentmins[0] = min(start[0], end[0]) + boxmins[0] - 1;
        segmentmins[1] = min(start[1], end[1]) + boxmins[1] - 1;
        segmentmins[2] = min(start[2], end[2]) + boxmins[2] - 1;
@@ -772,16 +815,11 @@ static void Mod_MDLMD2MD3_TraceBox(dp_model_t *model, const frameblend_t *frameb
        VectorAdd(end, boxmaxs, boxendmaxs);
        Collision_BrushForBox(&thisbrush_start, boxstartmins, boxstartmaxs, 0, 0, NULL);
        Collision_BrushForBox(&thisbrush_end, boxendmins, boxendmaxs, 0, 0, NULL);
-       if (maxvertices < model->surfmesh.num_vertices)
-       {
-               if (vertex3f)
-                       Z_Free(vertex3f);
-               maxvertices = (model->surfmesh.num_vertices + 255) & ~255;
-               vertex3f = (float *)Z_Malloc(maxvertices * sizeof(float[3]));
-       }
        model->AnimateVertices(model, frameblend, skeleton, vertex3f, NULL, NULL, NULL);
        for (i = 0, surface = model->data_surfaces;i < model->num_surfaces;i++, surface++)
                Collision_TraceBrushTriangleMeshFloat(trace, &thisbrush_start.brush, &thisbrush_end.brush, model->surfmesh.num_triangles, model->surfmesh.data_element3i, vertex3f, 0, NULL, SUPERCONTENTS_SOLID | (surface->texture->basematerialflags & MATERIALFLAGMASK_TRANSLUCENT ? 0 : SUPERCONTENTS_OPAQUE), 0, surface->texture, segmentmins, segmentmaxs);
+       if (vertex3f != vertex3fbuf)
+               Mem_Free(vertex3f);
 }
 
 static void Mod_ConvertAliasVerts (int inverts, trivertx_t *v, trivertx_t *out, int *vertremap)
@@ -853,7 +891,6 @@ static void Mod_MDL_LoadFrames (unsigned char* datapointer, int inverts, int *ve
                // read frames
                for (i = 0;i < groupframes;i++)
                {
-                       pinframe = (daliasframe_t *)datapointer;
                        datapointer += sizeof(daliasframe_t);
                        Mod_ConvertAliasVerts(inverts, (trivertx_t *)datapointer, loadmodel->surfmesh.data_morphmdlvertex + pose * loadmodel->surfmesh.num_vertices, vertremap);
                        datapointer += sizeof(trivertx_t) * inverts;
@@ -888,14 +925,27 @@ static void Mod_BuildAliasSkinFromSkinFrame(texture_t *texture, skinframe_t *ski
        if (texture->currentskinframe->hasalpha)
                texture->basematerialflags |= MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW;
        texture->currentmaterialflags = texture->basematerialflags;
+       texture->offsetmapping = OFFSETMAPPING_DEFAULT;
+       texture->offsetscale = 1;
+       texture->offsetbias = 0;
        texture->specularscalemod = 1;
        texture->specularpowermod = 1;
+       texture->surfaceflags = 0;
+       texture->supercontents = SUPERCONTENTS_SOLID;
+       if (!(texture->basematerialflags & MATERIALFLAG_BLENDED))
+               texture->supercontents |= SUPERCONTENTS_OPAQUE;
+       texture->transparentsort = TRANSPARENTSORT_DISTANCE;
+       // WHEN ADDING DEFAULTS HERE, REMEMBER TO PUT DEFAULTS IN ALL LOADERS
+       // JUST GREP FOR "specularscalemod = 1".
 }
 
 void Mod_BuildAliasSkinsFromSkinFiles(texture_t *skin, skinfile_t *skinfile, const char *meshname, const char *shadername)
 {
        int i;
+       char stripbuf[MAX_QPATH];
        skinfileitem_t *skinfileitem;
+       if(developer_extra.integer)
+               Con_DPrintf("Looking up texture for %s (default: %s)\n", meshname, shadername);
        if (skinfile)
        {
                // the skin += loadmodel->num_surfaces part of this is because data_textures on alias models is arranged as [numskins][numsurfaces]
@@ -908,7 +958,10 @@ void Mod_BuildAliasSkinsFromSkinFiles(texture_t *skin, skinfile_t *skinfile, con
                                // leave the skin unitialized (nodraw) if the replacement is "common/nodraw" or "textures/common/nodraw"
                                if (!strcmp(skinfileitem->name, meshname))
                                {
-                                       Mod_LoadTextureFromQ3Shader(skin, skinfileitem->replacement, true, true, (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_ALPHA | TEXF_PICMIP | TEXF_COMPRESS);
+                                       Image_StripImageExtension(skinfileitem->replacement, stripbuf, sizeof(stripbuf));
+                                       if(developer_extra.integer)
+                                               Con_DPrintf("--> got %s from skin file\n", stripbuf);
+                                       Mod_LoadTextureFromQ3Shader(skin, stripbuf, true, true, (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_ALPHA | TEXF_PICMIP | TEXF_COMPRESS);
                                        break;
                                }
                        }
@@ -916,12 +969,19 @@ void Mod_BuildAliasSkinsFromSkinFiles(texture_t *skin, skinfile_t *skinfile, con
                        {
                                // don't render unmentioned meshes
                                Mod_BuildAliasSkinFromSkinFrame(skin, NULL);
+                               if(developer_extra.integer)
+                                       Con_DPrintf("--> skipping\n");
                                skin->basematerialflags = skin->currentmaterialflags = MATERIALFLAG_NOSHADOW | MATERIALFLAG_NODRAW;
                        }
                }
        }
        else
-               Mod_LoadTextureFromQ3Shader(skin, shadername, true, true, (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_ALPHA | TEXF_PICMIP | TEXF_COMPRESS);
+       {
+               if(developer_extra.integer)
+                       Con_DPrintf("--> using default\n");
+               Image_StripImageExtension(shadername, stripbuf, sizeof(stripbuf));
+               Mod_LoadTextureFromQ3Shader(skin, stripbuf, true, true, (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_ALPHA | TEXF_PICMIP | TEXF_COMPRESS);
+       }
 }
 
 #define BOUNDI(VALUE,MIN,MAX) if (VALUE < MIN || VALUE >= MAX) Host_Error("model %s has an invalid ##VALUE (%d exceeds %d - %d)", loadmodel->name, VALUE, MIN, MAX);
@@ -948,6 +1008,7 @@ void Mod_IDP0_Load(dp_model_t *mod, void *buffer, void *bufferend)
        float *vertst;
        int *vertonseam, *vertremap;
        skinfile_t *skinfiles;
+       char vabuf[1024];
 
        datapointer = (unsigned char *)buffer;
        pinmodel = (mdl_t *)datapointer;
@@ -961,7 +1022,6 @@ void Mod_IDP0_Load(dp_model_t *mod, void *buffer, void *bufferend)
        loadmodel->modeldatatypestring = "MDL";
 
        loadmodel->type = mod_alias;
-       loadmodel->AnimateVertices = Mod_MDL_AnimateVertices;
        loadmodel->DrawSky = NULL;
        loadmodel->DrawAddWaterPlanes = NULL;
        loadmodel->Draw = R_Q1BSP_Draw;
@@ -975,7 +1035,9 @@ void Mod_IDP0_Load(dp_model_t *mod, void *buffer, void *bufferend)
        loadmodel->DrawLight = R_Q1BSP_DrawLight;
        loadmodel->TraceBox = Mod_MDLMD2MD3_TraceBox;
        loadmodel->TraceLine = Mod_MDLMD2MD3_TraceLine;
+       // FIXME add TraceBrush!
        loadmodel->PointSuperContents = NULL;
+       loadmodel->AnimateVertices = Mod_MDL_AnimateVertices;
 
        loadmodel->num_surfaces = 1;
        loadmodel->nummodelsurfaces = loadmodel->num_surfaces;
@@ -1073,8 +1135,8 @@ void Mod_IDP0_Load(dp_model_t *mod, void *buffer, void *bufferend)
        for (i = 0;i < numverts;i++)
        {
                vertonseam[i] = LittleLong(pinstverts[i].onseam);
-               vertst[i*2+0] = (LittleLong(pinstverts[i].s) + 0.5) * scales;
-               vertst[i*2+1] = (LittleLong(pinstverts[i].t) + 0.5) * scalet;
+               vertst[i*2+0] = LittleLong(pinstverts[i].s) * scales;
+               vertst[i*2+1] = LittleLong(pinstverts[i].t) * scalet;
                vertst[(i+numverts)*2+0] = vertst[i*2+0] + 0.5;
                vertst[(i+numverts)*2+1] = vertst[i*2+1];
        }
@@ -1136,10 +1198,14 @@ void Mod_IDP0_Load(dp_model_t *mod, void *buffer, void *bufferend)
 // load the frames
        loadmodel->animscenes = (animscene_t *)Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
        loadmodel->surfmesh.data_morphmdlvertex = (trivertx_t *)Mem_Alloc(loadmodel->mempool, sizeof(trivertx_t) * loadmodel->surfmesh.num_morphframes * loadmodel->surfmesh.num_vertices);
-       loadmodel->surfmesh.data_neighbor3i = (int *)Mem_Alloc(loadmodel->mempool, loadmodel->surfmesh.num_triangles * sizeof(int[3]));
+       if (r_enableshadowvolumes.integer)
+       {
+               loadmodel->surfmesh.data_neighbor3i = (int *)Mem_Alloc(loadmodel->mempool, loadmodel->surfmesh.num_triangles * sizeof(int[3]));
+       }
        Mod_MDL_LoadFrames (startframes, numverts, vertremap);
-       Mod_BuildTriangleNeighbors(loadmodel->surfmesh.data_neighbor3i, loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.num_triangles);
-       Mod_Alias_CalculateBoundingBox();
+       if (loadmodel->surfmesh.data_neighbor3i)
+               Mod_BuildTriangleNeighbors(loadmodel->surfmesh.data_neighbor3i, loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.num_triangles);
+       loadmodel->surfmesh.isanimated = Mod_Alias_CalculateBoundingBox();
        Mod_Alias_MorphMesh_CompileFrames();
 
        Mem_Free(vertst);
@@ -1220,7 +1286,7 @@ void Mod_IDP0_Load(dp_model_t *mod, void *buffer, void *bufferend)
                // check for skins that don't exist in the model, but do exist as external images
                // (this was added because yummyluv kept pestering me about support for it)
                // TODO: support shaders here?
-               while ((tempskinframe = R_SkinFrame_LoadExternal(va("%s_%i", loadmodel->name, loadmodel->numskins), (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_ALPHA | TEXF_PICMIP | TEXF_COMPRESS, false)))
+               while ((tempskinframe = R_SkinFrame_LoadExternal(va(vabuf, sizeof(vabuf), "%s_%i", loadmodel->name, loadmodel->numskins), (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_ALPHA | TEXF_PICMIP | TEXF_COMPRESS, false)))
                {
                        // expand the arrays to make room
                        tempskinscenes = loadmodel->skinscenes;
@@ -1259,7 +1325,27 @@ void Mod_IDP0_Load(dp_model_t *mod, void *buffer, void *bufferend)
        surface->num_firstvertex = 0;
        surface->num_vertices = loadmodel->surfmesh.num_vertices;
 
-       loadmodel->surfmesh.isanimated = loadmodel->numframes > 1 || loadmodel->animscenes[0].framecount > 1;
+       if(mod_alias_force_animated.string[0])
+               loadmodel->surfmesh.isanimated = mod_alias_force_animated.integer != 0;
+
+       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;
+       }
+
+       // because shaders can do somewhat unexpected things, check for unusual features now
+       for (i = 0;i < loadmodel->num_textures;i++)
+       {
+               if (loadmodel->data_textures[i].basematerialflags & (MATERIALFLAG_SKY))
+                       mod->DrawSky = R_Q1BSP_DrawSky;
+               if (loadmodel->data_textures[i].basematerialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION | MATERIALFLAG_CAMERA))
+                       mod->DrawAddWaterPlanes = R_Q1BSP_DrawAddWaterPlanes;
+       }
 }
 
 void Mod_IDP2_Load(dp_model_t *mod, void *buffer, void *bufferend)
@@ -1294,7 +1380,6 @@ void Mod_IDP2_Load(dp_model_t *mod, void *buffer, void *bufferend)
        loadmodel->modeldatatypestring = "MD2";
 
        loadmodel->type = mod_alias;
-       loadmodel->AnimateVertices = Mod_MDL_AnimateVertices;
        loadmodel->DrawSky = NULL;
        loadmodel->DrawAddWaterPlanes = NULL;
        loadmodel->Draw = R_Q1BSP_Draw;
@@ -1309,6 +1394,7 @@ void Mod_IDP2_Load(dp_model_t *mod, void *buffer, void *bufferend)
        loadmodel->TraceBox = Mod_MDLMD2MD3_TraceBox;
        loadmodel->TraceLine = Mod_MDLMD2MD3_TraceLine;
        loadmodel->PointSuperContents = NULL;
+       loadmodel->AnimateVertices = Mod_MDL_AnimateVertices;
 
        if (LittleLong(pinmodel->num_tris) < 1 || LittleLong(pinmodel->num_tris) > 65536)
                Host_Error ("%s has invalid number of triangles: %i", loadmodel->name, LittleLong(pinmodel->num_tris));
@@ -1345,14 +1431,17 @@ void Mod_IDP2_Load(dp_model_t *mod, void *buffer, void *bufferend)
 
        loadmodel->num_surfaces = 1;
        loadmodel->nummodelsurfaces = loadmodel->num_surfaces;
-       data = (unsigned char *)Mem_Alloc(loadmodel->mempool, loadmodel->num_surfaces * sizeof(msurface_t) + loadmodel->num_surfaces * sizeof(int) + loadmodel->numframes * sizeof(animscene_t) + loadmodel->numframes * sizeof(float[6]) + loadmodel->surfmesh.num_triangles * sizeof(int[3]) + loadmodel->surfmesh.num_triangles * sizeof(int[3]));
+       data = (unsigned char *)Mem_Alloc(loadmodel->mempool, loadmodel->num_surfaces * sizeof(msurface_t) + loadmodel->num_surfaces * sizeof(int) + loadmodel->numframes * sizeof(animscene_t) + loadmodel->numframes * sizeof(float[6]) + loadmodel->surfmesh.num_triangles * sizeof(int[3]) + (r_enableshadowvolumes.integer ? loadmodel->surfmesh.num_triangles * sizeof(int[3]) : 0));
        loadmodel->data_surfaces = (msurface_t *)data;data += loadmodel->num_surfaces * sizeof(msurface_t);
        loadmodel->sortedmodelsurfaces = (int *)data;data += loadmodel->num_surfaces * sizeof(int);
        loadmodel->sortedmodelsurfaces[0] = 0;
        loadmodel->animscenes = (animscene_t *)data;data += loadmodel->numframes * sizeof(animscene_t);
        loadmodel->surfmesh.data_morphmd2framesize6f = (float *)data;data += loadmodel->numframes * sizeof(float[6]);
        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]);
+       if (r_enableshadowvolumes.integer)
+       {
+               loadmodel->surfmesh.data_neighbor3i = (int *)data;data += loadmodel->surfmesh.num_triangles * sizeof(int[3]);
+       }
 
        loadmodel->synctype = ST_RAND;
 
@@ -1496,9 +1585,12 @@ void Mod_IDP2_Load(dp_model_t *mod, void *buffer, void *bufferend)
 
        Mem_Free(vertremap);
 
-       Mod_BuildTriangleNeighbors(loadmodel->surfmesh.data_neighbor3i, loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.num_triangles);
-       Mod_Alias_CalculateBoundingBox();
+       if (loadmodel->surfmesh.data_neighbor3i)
+               Mod_BuildTriangleNeighbors(loadmodel->surfmesh.data_neighbor3i, loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.num_triangles);
+       loadmodel->surfmesh.isanimated = Mod_Alias_CalculateBoundingBox();
        Mod_Alias_MorphMesh_CompileFrames();
+       if(mod_alias_force_animated.string[0])
+               loadmodel->surfmesh.isanimated = mod_alias_force_animated.integer != 0;
 
        surface = loadmodel->data_surfaces;
        surface->texture = loadmodel->data_textures;
@@ -1507,7 +1599,24 @@ void Mod_IDP2_Load(dp_model_t *mod, void *buffer, void *bufferend)
        surface->num_firstvertex = 0;
        surface->num_vertices = loadmodel->surfmesh.num_vertices;
 
-       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;
+       }
+
+       // because shaders can do somewhat unexpected things, check for unusual features now
+       for (i = 0;i < loadmodel->num_textures;i++)
+       {
+               if (loadmodel->data_textures[i].basematerialflags & (MATERIALFLAG_SKY))
+                       mod->DrawSky = R_Q1BSP_DrawSky;
+               if (loadmodel->data_textures[i].basematerialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION | MATERIALFLAG_CAMERA))
+                       mod->DrawAddWaterPlanes = R_Q1BSP_DrawAddWaterPlanes;
+       }
 }
 
 void Mod_IDP3_Load(dp_model_t *mod, void *buffer, void *bufferend)
@@ -1537,7 +1646,6 @@ void Mod_IDP3_Load(dp_model_t *mod, void *buffer, void *bufferend)
        loadmodel->modeldatatypestring = "MD3";
 
        loadmodel->type = mod_alias;
-       loadmodel->AnimateVertices = Mod_MD3_AnimateVertices;
        loadmodel->DrawSky = NULL;
        loadmodel->DrawAddWaterPlanes = NULL;
        loadmodel->Draw = R_Q1BSP_Draw;
@@ -1552,6 +1660,7 @@ void Mod_IDP3_Load(dp_model_t *mod, void *buffer, void *bufferend)
        loadmodel->TraceBox = Mod_MDLMD2MD3_TraceBox;
        loadmodel->TraceLine = Mod_MDLMD2MD3_TraceLine;
        loadmodel->PointSuperContents = NULL;
+       loadmodel->AnimateVertices = Mod_MD3_AnimateVertices;
        loadmodel->synctype = ST_RAND;
        // convert model flags to EF flags (MF_ROCKET becomes EF_ROCKET, etc)
        i = LittleLong (pinmodel->flags);
@@ -1612,7 +1721,7 @@ void Mod_IDP3_Load(dp_model_t *mod, void *buffer, void *bufferend)
        loadmodel->nummodelsurfaces = loadmodel->num_surfaces;
        loadmodel->num_textures = loadmodel->num_surfaces * loadmodel->numskins;
        loadmodel->num_texturesperskin = loadmodel->num_surfaces;
-       data = (unsigned char *)Mem_Alloc(loadmodel->mempool, loadmodel->num_surfaces * sizeof(msurface_t) + loadmodel->num_surfaces * sizeof(int) + loadmodel->num_surfaces * loadmodel->numskins * sizeof(texture_t) + meshtriangles * sizeof(int[3]) + meshtriangles * sizeof(int[3]) + (meshvertices <= 65536 ? meshtriangles * sizeof(unsigned short[3]) : 0) + meshvertices * sizeof(float[2]) + meshvertices * loadmodel->numframes * sizeof(md3vertex_t));
+       data = (unsigned char *)Mem_Alloc(loadmodel->mempool, loadmodel->num_surfaces * sizeof(msurface_t) + loadmodel->num_surfaces * sizeof(int) + loadmodel->num_surfaces * loadmodel->numskins * sizeof(texture_t) + meshtriangles * sizeof(int[3]) + (r_enableshadowvolumes.integer ? meshtriangles * sizeof(int[3]) : 0) + (meshvertices <= 65536 ? meshtriangles * sizeof(unsigned short[3]) : 0) + meshvertices * sizeof(float[2]) + meshvertices * loadmodel->numframes * sizeof(md3vertex_t));
        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);
@@ -1621,11 +1730,16 @@ void Mod_IDP3_Load(dp_model_t *mod, void *buffer, void *bufferend)
        loadmodel->surfmesh.num_morphframes = loadmodel->numframes; // TODO: remove?
        loadmodel->num_poses = loadmodel->surfmesh.num_morphframes;
        loadmodel->surfmesh.data_element3i = (int *)data;data += meshtriangles * sizeof(int[3]);
-       loadmodel->surfmesh.data_neighbor3i = (int *)data;data += meshtriangles * sizeof(int[3]);
+       if (r_enableshadowvolumes.integer)
+       {
+               loadmodel->surfmesh.data_neighbor3i = (int *)data;data += meshtriangles * sizeof(int[3]);
+       }
        loadmodel->surfmesh.data_texcoordtexture2f = (float *)data;data += meshvertices * sizeof(float[2]);
        loadmodel->surfmesh.data_morphmd3vertex = (md3vertex_t *)data;data += meshvertices * loadmodel->numframes * sizeof(md3vertex_t);
        if (meshvertices <= 65536)
+       {
                loadmodel->surfmesh.data_element3s = (unsigned short *)data;data += meshtriangles * sizeof(unsigned short[3]);
+       }
 
        meshvertices = 0;
        meshtriangles = 0;
@@ -1671,14 +1785,33 @@ void Mod_IDP3_Load(dp_model_t *mod, void *buffer, void *bufferend)
        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_BuildTriangleNeighbors(loadmodel->surfmesh.data_neighbor3i, loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.num_triangles);
+       if (loadmodel->surfmesh.data_neighbor3i)
+               Mod_BuildTriangleNeighbors(loadmodel->surfmesh.data_neighbor3i, loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.num_triangles);
        Mod_Alias_MorphMesh_CompileFrames();
-       Mod_Alias_CalculateBoundingBox();
+       loadmodel->surfmesh.isanimated = Mod_Alias_CalculateBoundingBox();
        Mod_FreeSkinFiles(skinfiles);
        Mod_MakeSortedSurfaces(loadmodel);
+       if(mod_alias_force_animated.string[0])
+               loadmodel->surfmesh.isanimated = mod_alias_force_animated.integer != 0;
 
-       loadmodel->surfmesh.isanimated = loadmodel->numframes > 1
-            || (loadmodel->animscenes && 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;
+       }
+
+       // because shaders can do somewhat unexpected things, check for unusual features now
+       for (i = 0;i < loadmodel->num_textures;i++)
+       {
+               if (loadmodel->data_textures[i].basematerialflags & (MATERIALFLAG_SKY))
+                       mod->DrawSky = R_Q1BSP_DrawSky;
+               if (loadmodel->data_textures[i].basematerialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION | MATERIALFLAG_CAMERA))
+                       mod->DrawAddWaterPlanes = R_Q1BSP_DrawAddWaterPlanes;
+       }
 }
 
 void Mod_ZYMOTICMODEL_Load(dp_model_t *mod, void *buffer, void *bufferend)
@@ -1753,7 +1886,6 @@ void Mod_ZYMOTICMODEL_Load(dp_model_t *mod, void *buffer, void *bufferend)
                return;
        }
 
-       loadmodel->AnimateVertices = Mod_Skeletal_AnimateVertices;
        loadmodel->DrawSky = NULL;
        loadmodel->DrawAddWaterPlanes = NULL;
        loadmodel->Draw = R_Q1BSP_Draw;
@@ -1768,6 +1900,7 @@ void Mod_ZYMOTICMODEL_Load(dp_model_t *mod, void *buffer, void *bufferend)
        loadmodel->TraceBox = Mod_MDLMD2MD3_TraceBox;
        loadmodel->TraceLine = Mod_MDLMD2MD3_TraceLine;
        loadmodel->PointSuperContents = NULL;
+       loadmodel->AnimateVertices = Mod_Skeletal_AnimateVertices;
 
        loadmodel->numframes = pheader->numscenes;
        loadmodel->num_surfaces = pheader->numshaders;
@@ -1787,6 +1920,7 @@ void Mod_ZYMOTICMODEL_Load(dp_model_t *mod, void *buffer, void *bufferend)
        }
 
        // model bbox
+       // LordHavoc: actually we blow this away later with Mod_Alias_CalculateBoundingBox()
        modelradius = pheader->radius;
        for (i = 0;i < 3;i++)
        {
@@ -1859,25 +1993,33 @@ void Mod_ZYMOTICMODEL_Load(dp_model_t *mod, void *buffer, void *bufferend)
        loadmodel->nummodelsurfaces = loadmodel->num_surfaces;
        loadmodel->num_textures = loadmodel->num_surfaces * loadmodel->numskins;
        loadmodel->num_texturesperskin = loadmodel->num_surfaces;
-       data = (unsigned char *)Mem_Alloc(loadmodel->mempool, loadmodel->num_surfaces * sizeof(msurface_t) + loadmodel->num_surfaces * sizeof(int) + loadmodel->num_surfaces * loadmodel->numskins * sizeof(texture_t) + meshtriangles * sizeof(int[3]) + meshtriangles * sizeof(int[3]) + (meshvertices <= 65536 ? meshtriangles * sizeof(unsigned short[3]) : 0) + meshvertices * sizeof(float[14]) + meshvertices * sizeof(int[4]) + meshvertices * sizeof(float[4]) + loadmodel->num_poses * loadmodel->num_bones * sizeof(short[6]) + loadmodel->num_bones * sizeof(float[12]));
+       data = (unsigned char *)Mem_Alloc(loadmodel->mempool, loadmodel->num_surfaces * sizeof(msurface_t) + loadmodel->num_surfaces * sizeof(int) + loadmodel->num_surfaces * loadmodel->numskins * sizeof(texture_t) + meshtriangles * sizeof(int[3]) + (r_enableshadowvolumes.integer ? meshtriangles * sizeof(int[3]) : 0) + (meshvertices <= 65536 ? meshtriangles * sizeof(unsigned short[3]) : 0) + meshvertices * (sizeof(float[14]) + sizeof(unsigned short) + sizeof(unsigned char[2][4])) + loadmodel->num_poses * loadmodel->num_bones * sizeof(short[7]) + loadmodel->num_bones * sizeof(float[12]));
        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.num_vertices = meshvertices;
        loadmodel->surfmesh.num_triangles = meshtriangles;
        loadmodel->surfmesh.data_element3i = (int *)data;data += meshtriangles * sizeof(int[3]);
-       loadmodel->surfmesh.data_neighbor3i = (int *)data;data += meshtriangles * sizeof(int[3]);
+       if (r_enableshadowvolumes.integer)
+       {
+               loadmodel->surfmesh.data_neighbor3i = (int *)data;data += meshtriangles * sizeof(int[3]);
+       }
        loadmodel->surfmesh.data_vertex3f = (float *)data;data += meshvertices * sizeof(float[3]);
        loadmodel->surfmesh.data_svector3f = (float *)data;data += meshvertices * sizeof(float[3]);
        loadmodel->surfmesh.data_tvector3f = (float *)data;data += meshvertices * sizeof(float[3]);
        loadmodel->surfmesh.data_normal3f = (float *)data;data += meshvertices * sizeof(float[3]);
        loadmodel->surfmesh.data_texcoordtexture2f = (float *)data;data += meshvertices * sizeof(float[2]);
-       loadmodel->surfmesh.data_vertexweightindex4i = (int *)data;data += meshvertices * sizeof(int[4]);
-       loadmodel->surfmesh.data_vertexweightinfluence4f = (float *)data;data += meshvertices * sizeof(float[4]);
+       loadmodel->surfmesh.data_skeletalindex4ub = (unsigned char *)data;data += meshvertices * sizeof(unsigned char[4]);
+       loadmodel->surfmesh.data_skeletalweight4ub = (unsigned char *)data;data += meshvertices * sizeof(unsigned char[4]);
        loadmodel->data_baseboneposeinverse = (float *)data;data += loadmodel->num_bones * sizeof(float[12]);
+       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->data_poses7s = (short *)data;data += loadmodel->num_poses * loadmodel->num_bones * sizeof(short[7]);
+       loadmodel->surfmesh.data_blendweights = NULL;
 
        //zymlump_t lump_poses; // float pose[numposes][numbones][3][4]; // animation data
        poses = (float *) (pheader->lump_poses.start + pbase);
@@ -1917,7 +2059,7 @@ void Mod_ZYMOTICMODEL_Load(dp_model_t *mod, void *buffer, void *bufferend)
                        VectorNormalize(pose + 4);
                        VectorNormalize(pose + 8);
                        Matrix4x4_FromArray12FloatD3D(&posematrix, pose);
-                       Matrix4x4_ToBonePose6s(&posematrix, loadmodel->num_poseinvscale, loadmodel->data_poses6s + 6*(i*loadmodel->num_bones+j));
+                       Matrix4x4_ToBonePose7s(&posematrix, loadmodel->num_poseinvscale, loadmodel->data_poses7s + 7*(i*loadmodel->num_bones+j));
                }
        }
 
@@ -1955,8 +2097,15 @@ void Mod_ZYMOTICMODEL_Load(dp_model_t *mod, void *buffer, void *bufferend)
                loadmodel->surfmesh.data_vertex3f[j*3+1] = relativeorigin[0] * m[4] + relativeorigin[1] * m[5] + relativeorigin[2] * m[ 6] + m[ 7];
                loadmodel->surfmesh.data_vertex3f[j*3+2] = relativeorigin[0] * m[8] + relativeorigin[1] * m[9] + relativeorigin[2] * m[10] + m[11];
                // store the weight as the primary weight on this vertex
-               loadmodel->surfmesh.data_vertexweightindex4i[j*4+0] = boneindex;
-               loadmodel->surfmesh.data_vertexweightinfluence4f[j*4+0] = 1;
+               loadmodel->surfmesh.blends[j] = boneindex;
+               loadmodel->surfmesh.data_skeletalindex4ub[j*4  ] = boneindex;
+               loadmodel->surfmesh.data_skeletalindex4ub[j*4+1] = 0;
+               loadmodel->surfmesh.data_skeletalindex4ub[j*4+2] = 0;
+               loadmodel->surfmesh.data_skeletalindex4ub[j*4+3] = 0;
+               loadmodel->surfmesh.data_skeletalweight4ub[j*4  ] = 255;
+               loadmodel->surfmesh.data_skeletalweight4ub[j*4+1] = 0;
+               loadmodel->surfmesh.data_skeletalweight4ub[j*4+2] = 0;
+               loadmodel->surfmesh.data_skeletalweight4ub[j*4+3] = 0;
        }
        Z_Free(bonepose);
        // normals and tangents are calculated after elements are loaded
@@ -2037,11 +2186,32 @@ void Mod_ZYMOTICMODEL_Load(dp_model_t *mod, void *buffer, void *bufferend)
                        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_BuildNormals(0, loadmodel->surfmesh.num_vertices, loadmodel->surfmesh.num_triangles, loadmodel->surfmesh.data_vertex3f, loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.data_normal3f, r_smoothnormals_areaweighting.integer != 0);
+       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, r_smoothnormals_areaweighting.integer != 0);
+       if (loadmodel->surfmesh.data_neighbor3i)
+               Mod_BuildTriangleNeighbors(loadmodel->surfmesh.data_neighbor3i, loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.num_triangles);
+       loadmodel->surfmesh.isanimated = Mod_Alias_CalculateBoundingBox();
+       if(mod_alias_force_animated.string[0])
+               loadmodel->surfmesh.isanimated = mod_alias_force_animated.integer != 0;
+
+       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;
+       }
 
-       loadmodel->surfmesh.isanimated = loadmodel->numframes > 1 || loadmodel->animscenes[0].framecount > 1;
+       // because shaders can do somewhat unexpected things, check for unusual features now
+       for (i = 0;i < loadmodel->num_textures;i++)
+       {
+               if (loadmodel->data_textures[i].basematerialflags & (MATERIALFLAG_SKY))
+                       mod->DrawSky = R_Q1BSP_DrawSky;
+               if (loadmodel->data_textures[i].basematerialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION | MATERIALFLAG_CAMERA))
+                       mod->DrawAddWaterPlanes = R_Q1BSP_DrawAddWaterPlanes;
+       }
 }
 
 void Mod_DARKPLACESMODEL_Load(dp_model_t *mod, void *buffer, void *bufferend)
@@ -2100,7 +2270,6 @@ void Mod_DARKPLACESMODEL_Load(dp_model_t *mod, void *buffer, void *bufferend)
                return;
        }
 
-       loadmodel->AnimateVertices = Mod_Skeletal_AnimateVertices;
        loadmodel->DrawSky = NULL;
        loadmodel->DrawAddWaterPlanes = NULL;
        loadmodel->Draw = R_Q1BSP_Draw;
@@ -2115,8 +2284,10 @@ void Mod_DARKPLACESMODEL_Load(dp_model_t *mod, void *buffer, void *bufferend)
        loadmodel->TraceBox = Mod_MDLMD2MD3_TraceBox;
        loadmodel->TraceLine = Mod_MDLMD2MD3_TraceLine;
        loadmodel->PointSuperContents = NULL;
+       loadmodel->AnimateVertices = Mod_Skeletal_AnimateVertices;
 
        // model bbox
+       // LordHavoc: actually we blow this away later with Mod_Alias_CalculateBoundingBox()
        for (i = 0;i < 3;i++)
        {
                loadmodel->normalmins[i] = pheader->mins[i];
@@ -2154,28 +2325,36 @@ void Mod_DARKPLACESMODEL_Load(dp_model_t *mod, void *buffer, void *bufferend)
        loadmodel->num_textures = loadmodel->num_surfaces * loadmodel->numskins;
        loadmodel->num_texturesperskin = loadmodel->num_surfaces;
        // do most allocations as one merged chunk
-       data = (unsigned char *)Mem_Alloc(loadmodel->mempool, loadmodel->num_surfaces * sizeof(msurface_t) + loadmodel->num_surfaces * sizeof(int) + loadmodel->num_surfaces * loadmodel->numskins * sizeof(texture_t) + meshtriangles * sizeof(int[3]) + (meshvertices <= 65536 ? meshtriangles * sizeof(unsigned short[3]) : 0) + meshtriangles * sizeof(int[3]) + meshvertices * (sizeof(float[14]) + sizeof(int[4]) + sizeof(float[4])) + 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));
+       data = (unsigned char *)Mem_Alloc(loadmodel->mempool, loadmodel->num_surfaces * sizeof(msurface_t) + loadmodel->num_surfaces * sizeof(int) + loadmodel->num_surfaces * loadmodel->numskins * sizeof(texture_t) + meshtriangles * sizeof(int[3]) + (meshvertices <= 65536 ? meshtriangles * sizeof(unsigned short[3]) : 0) + (r_enableshadowvolumes.integer ? meshtriangles * sizeof(int[3]) : 0) + meshvertices * (sizeof(float[14]) + sizeof(unsigned short) + sizeof(unsigned char[2][4])) + loadmodel->num_poses * loadmodel->num_bones * sizeof(short[7]) + loadmodel->num_bones * sizeof(float[12]) + loadmodel->numskins * sizeof(animscene_t) + loadmodel->num_bones * sizeof(aliasbone_t) + loadmodel->numframes * sizeof(animscene_t));
        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.num_vertices = meshvertices;
        loadmodel->surfmesh.num_triangles = meshtriangles;
        loadmodel->surfmesh.data_element3i = (int *)data;data += meshtriangles * sizeof(int[3]);
-       loadmodel->surfmesh.data_neighbor3i = (int *)data;data += meshtriangles * sizeof(int[3]);
+       if (r_enableshadowvolumes.integer)
+       {
+               loadmodel->surfmesh.data_neighbor3i = (int *)data;data += meshtriangles * sizeof(int[3]);
+       }
        loadmodel->surfmesh.data_vertex3f = (float *)data;data += meshvertices * sizeof(float[3]);
        loadmodel->surfmesh.data_svector3f = (float *)data;data += meshvertices * sizeof(float[3]);
        loadmodel->surfmesh.data_tvector3f = (float *)data;data += meshvertices * sizeof(float[3]);
        loadmodel->surfmesh.data_normal3f = (float *)data;data += meshvertices * sizeof(float[3]);
        loadmodel->surfmesh.data_texcoordtexture2f = (float *)data;data += meshvertices * sizeof(float[2]);
-       loadmodel->surfmesh.data_vertexweightindex4i = (int *)data;data += meshvertices * sizeof(int[4]);
-       loadmodel->surfmesh.data_vertexweightinfluence4f = (float *)data;data += meshvertices * sizeof(float[4]);
+       loadmodel->surfmesh.data_skeletalindex4ub = (unsigned char *)data;data += meshvertices * sizeof(unsigned char[4]);
+       loadmodel->surfmesh.data_skeletalweight4ub = (unsigned char *)data;data += meshvertices * sizeof(unsigned char[4]);
        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 (meshvertices <= 65536)
+       {
                loadmodel->surfmesh.data_element3s = (unsigned short *)data;data += meshtriangles * sizeof(unsigned short[3]);
-       loadmodel->data_poses6s = (short *)data;data += loadmodel->num_poses * loadmodel->num_bones * sizeof(short[6]);
+       }
+       loadmodel->data_poses7s = (short *)data;data += loadmodel->num_poses * loadmodel->num_bones * sizeof(short[7]);
+       loadmodel->surfmesh.data_blendweights = (blendweights_t *)Mem_Alloc(loadmodel->mempool, meshvertices * sizeof(blendweights_t));
 
        for (i = 0;i < loadmodel->numskins;i++)
        {
@@ -2244,7 +2423,7 @@ void Mod_DARKPLACESMODEL_Load(dp_model_t *mod, void *buffer, void *bufferend)
                        VectorNormalize(pose + 4);
                        VectorNormalize(pose + 8);
                        Matrix4x4_FromArray12FloatD3D(&posematrix, pose);
-                       Matrix4x4_ToBonePose6s(&posematrix, loadmodel->num_poseinvscale, loadmodel->data_poses6s + 6*(i*loadmodel->num_bones+j));
+                       Matrix4x4_ToBonePose7s(&posematrix, loadmodel->num_poseinvscale, loadmodel->data_poses7s + 7*(i*loadmodel->num_bones+j));
                }
        }
 
@@ -2304,7 +2483,8 @@ void Mod_DARKPLACESMODEL_Load(dp_model_t *mod, void *buffer, void *bufferend)
                data = (unsigned char *) (pbase + BigLong(dpmmesh->ofs_verts));
                for (j = surface->num_firstvertex;j < surface->num_firstvertex + surface->num_vertices;j++)
                {
-                       float sum;
+                       int weightindex[4] = { 0, 0, 0, 0 };
+                       float weightinfluence[4] = { 0, 0, 0, 0 };
                        int l;
                        int numweights = BigLong(((dpmvertex_t *)data)->numbones);
                        data += sizeof(dpmvertex_t);
@@ -2331,8 +2511,8 @@ void Mod_DARKPLACESMODEL_Load(dp_model_t *mod, void *buffer, void *bufferend)
                                if (!k)
                                {
                                        // store the first (and often only) weight
-                                       loadmodel->surfmesh.data_vertexweightinfluence4f[j*4+0] = influence;
-                                       loadmodel->surfmesh.data_vertexweightindex4i[j*4+0] = boneindex;
+                                       weightinfluence[0] = influence;
+                                       weightindex[0] = boneindex;
                                }
                                else
                                {
@@ -2340,33 +2520,33 @@ void Mod_DARKPLACESMODEL_Load(dp_model_t *mod, void *buffer, void *bufferend)
                                        // (which only accepts up to 4 bones per vertex)
                                        for (l = 0;l < 4;l++)
                                        {
-                                               if (loadmodel->surfmesh.data_vertexweightinfluence4f[j*4+l] < influence)
+                                               if (weightinfluence[l] < influence)
                                                {
                                                        // move weaker influence weights out of the way first
                                                        int l2;
                                                        for (l2 = 3;l2 > l;l2--)
                                                        {
-                                                               loadmodel->surfmesh.data_vertexweightinfluence4f[j*4+l2] = loadmodel->surfmesh.data_vertexweightinfluence4f[j*4+l2-1];
-                                                               loadmodel->surfmesh.data_vertexweightindex4i[j*4+l2] = loadmodel->surfmesh.data_vertexweightindex4i[j*4+l2-1];
+                                                               weightinfluence[l2] = weightinfluence[l2-1];
+                                                               weightindex[l2] = weightindex[l2-1];
                                                        }
                                                        // store the new weight
-                                                       loadmodel->surfmesh.data_vertexweightinfluence4f[j*4+l] = influence;
-                                                       loadmodel->surfmesh.data_vertexweightindex4i[j*4+l] = boneindex;
+                                                       weightinfluence[l] = influence;
+                                                       weightindex[l] = boneindex;
                                                        break;
                                                }
                                        }
                                }
                                data += sizeof(dpmbonevert_t);
                        }
-                       sum = 0;
-                       for (l = 0;l < 4;l++)
-                               sum += loadmodel->surfmesh.data_vertexweightinfluence4f[j*4+l];
-                       if (sum && fabs(sum - 1) > (1.0f / 256.0f))
-                       {
-                               float f = 1.0f / sum;
-                               for (l = 0;l < 4;l++)
-                                       loadmodel->surfmesh.data_vertexweightinfluence4f[j*4+l] *= f;
-                       }
+                       loadmodel->surfmesh.blends[j] = Mod_Skeletal_CompressBlend(loadmodel, weightindex, weightinfluence);
+                       loadmodel->surfmesh.data_skeletalindex4ub[j*4  ] = weightindex[0];
+                       loadmodel->surfmesh.data_skeletalindex4ub[j*4+1] = weightindex[1];
+                       loadmodel->surfmesh.data_skeletalindex4ub[j*4+2] = weightindex[2];
+                       loadmodel->surfmesh.data_skeletalindex4ub[j*4+3] = weightindex[3];
+                       loadmodel->surfmesh.data_skeletalweight4ub[j*4  ] = (unsigned char)(weightinfluence[0]*255.0f);
+                       loadmodel->surfmesh.data_skeletalweight4ub[j*4+1] = (unsigned char)(weightinfluence[1]*255.0f);
+                       loadmodel->surfmesh.data_skeletalweight4ub[j*4+2] = (unsigned char)(weightinfluence[2]*255.0f);
+                       loadmodel->surfmesh.data_skeletalweight4ub[j*4+3] = (unsigned char)(weightinfluence[3]*255.0f);
                }
 
                // since dpm models do not have named sections, reuse their shader name as the section name
@@ -2374,6 +2554,8 @@ void Mod_DARKPLACESMODEL_Load(dp_model_t *mod, void *buffer, void *bufferend)
 
                Mod_ValidateElements(loadmodel->surfmesh.data_element3i + surface->num_firsttriangle * 3, surface->num_triangles, surface->num_firstvertex, surface->num_vertices, __FILE__, __LINE__);
        }
+       if (loadmodel->surfmesh.num_blends < meshvertices)
+               loadmodel->surfmesh.data_blendweights = (blendweights_t *)Mem_Realloc(loadmodel->mempool, loadmodel->surfmesh.data_blendweights, loadmodel->surfmesh.num_blends * sizeof(blendweights_t));
        Z_Free(bonepose);
        Mod_FreeSkinFiles(skinfiles);
        Mod_MakeSortedSurfaces(loadmodel);
@@ -2383,10 +2565,31 @@ void Mod_DARKPLACESMODEL_Load(dp_model_t *mod, void *buffer, void *bufferend)
                for (i = 0;i < loadmodel->surfmesh.num_triangles*3;i++)
                        loadmodel->surfmesh.data_element3s[i] = loadmodel->surfmesh.data_element3i[i];
        Mod_BuildBaseBonePoses();
-       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_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, r_smoothnormals_areaweighting.integer != 0);
+       if (loadmodel->surfmesh.data_neighbor3i)
+               Mod_BuildTriangleNeighbors(loadmodel->surfmesh.data_neighbor3i, loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.num_triangles);
+       loadmodel->surfmesh.isanimated = Mod_Alias_CalculateBoundingBox();
+       if(mod_alias_force_animated.string[0])
+               loadmodel->surfmesh.isanimated = mod_alias_force_animated.integer != 0;
+
+       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;
+       }
 
-       loadmodel->surfmesh.isanimated = loadmodel->numframes > 1 || loadmodel->animscenes[0].framecount > 1;
+       // because shaders can do somewhat unexpected things, check for unusual features now
+       for (i = 0;i < loadmodel->num_textures;i++)
+       {
+               if (loadmodel->data_textures[i].basematerialflags & (MATERIALFLAG_SKY))
+                       mod->DrawSky = R_Q1BSP_DrawSky;
+               if (loadmodel->data_textures[i].basematerialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION | MATERIALFLAG_CAMERA))
+                       mod->DrawAddWaterPlanes = R_Q1BSP_DrawAddWaterPlanes;
+       }
 }
 
 // no idea why PSK/PSA files contain weird quaternions but they do...
@@ -2420,7 +2623,6 @@ void Mod_PSKMODEL_Load(dp_model_t *mod, void *buffer, void *bufferend)
        loadmodel->modeldatatypestring = "PSK";
 
        loadmodel->type = mod_alias;
-       loadmodel->AnimateVertices = Mod_Skeletal_AnimateVertices;
        loadmodel->DrawSky = NULL;
        loadmodel->DrawAddWaterPlanes = NULL;
        loadmodel->Draw = R_Q1BSP_Draw;
@@ -2435,14 +2637,15 @@ void Mod_PSKMODEL_Load(dp_model_t *mod, void *buffer, void *bufferend)
        loadmodel->TraceBox = Mod_MDLMD2MD3_TraceBox;
        loadmodel->TraceLine = Mod_MDLMD2MD3_TraceLine;
        loadmodel->PointSuperContents = NULL;
+       loadmodel->AnimateVertices = Mod_Skeletal_AnimateVertices;
        loadmodel->synctype = ST_RAND;
 
        FS_StripExtension(loadmodel->name, animname, sizeof(animname));
        strlcat(animname, ".psa", sizeof(animname));
        animbuffer = animfilebuffer = FS_LoadFile(animname, loadmodel->mempool, false, &filesize);
        animbufferend = (void *)((unsigned char*)animbuffer + (int)filesize);
-       if (animbuffer == NULL)
-               Host_Error("%s: can't find .psa file (%s)", loadmodel->name, animname);
+       if (!animbuffer)
+               animbufferend = animbuffer;
 
        numpnts = 0;
        pnts = NULL;
@@ -2760,15 +2963,19 @@ void Mod_PSKMODEL_Load(dp_model_t *mod, void *buffer, void *bufferend)
                        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)
+       if (!numpnts || !pnts || !numvtxw || !vtxw || !numfaces || !faces || !nummatts || !matts || !numbones || !bones || !numrawweights || !rawweights)
                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);
+       if (numanims)
+       {
+               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);
+       }
+       else
+               loadmodel->numframes = loadmodel->num_poses = 1;
 
        meshvertices = numvtxw;
        meshtriangles = numfaces;
@@ -2785,27 +2992,35 @@ void Mod_PSKMODEL_Load(dp_model_t *mod, void *buffer, void *bufferend)
        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(int[4]) + loadmodel->surfmesh.num_vertices * sizeof(float[4]) + 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);
+       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]) + (r_enableshadowvolumes.integer ? loadmodel->surfmesh.num_triangles * sizeof(int[3]) : 0)  + 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 char[4]) + loadmodel->surfmesh.num_vertices * sizeof(unsigned char[4]) + loadmodel->surfmesh.num_vertices * sizeof(unsigned short) + loadmodel->num_poses * loadmodel->num_bones * sizeof(short[7]) + 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]);
+       if (r_enableshadowvolumes.integer)
+       {
+               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->surfmesh.data_vertexweightindex4i = (int *)data;data += loadmodel->surfmesh.num_vertices * sizeof(int[4]);
-       loadmodel->surfmesh.data_vertexweightinfluence4f = (float *)data;data += loadmodel->surfmesh.num_vertices * sizeof(float[4]);
+       loadmodel->surfmesh.data_skeletalindex4ub = (unsigned char *)data;data += loadmodel->surfmesh.num_vertices * sizeof(unsigned char[4]);
+       loadmodel->surfmesh.data_skeletalweight4ub = (unsigned char *)data;data += loadmodel->surfmesh.num_vertices * sizeof(unsigned char[4]);
        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->data_poses7s = (short *)data;data += loadmodel->num_poses * loadmodel->num_bones * sizeof(short[7]);
+       loadmodel->surfmesh.data_blendweights = (blendweights_t *)Mem_Alloc(loadmodel->mempool, loadmodel->surfmesh.num_vertices * sizeof(blendweights_t));
 
        for (i = 0;i < loadmodel->numskins;i++)
        {
@@ -2862,12 +3077,37 @@ void Mod_PSKMODEL_Load(dp_model_t *mod, void *buffer, void *bufferend)
                        Host_Error("%s bone[%i].parent >= %i", loadmodel->name, index, index);
        }
 
+       // convert the basepose data
+       if (loadmodel->num_bones)
+       {
+               int boneindex;
+               matrix4x4_t *basebonepose;
+               float *outinvmatrix = loadmodel->data_baseboneposeinverse;
+               matrix4x4_t bonematrix;
+               matrix4x4_t tempbonematrix;
+               basebonepose = (matrix4x4_t *)Mem_Alloc(tempmempool, loadmodel->num_bones * sizeof(matrix4x4_t));
+               for (boneindex = 0;boneindex < loadmodel->num_bones;boneindex++)
+               {
+                       Matrix4x4_FromOriginQuat(&bonematrix, bones[boneindex].basepose.origin[0], bones[boneindex].basepose.origin[1], bones[boneindex].basepose.origin[2], bones[boneindex].basepose.quat[0], bones[boneindex].basepose.quat[1], bones[boneindex].basepose.quat[2], bones[boneindex].basepose.quat[3]);
+                       if (loadmodel->data_bones[boneindex].parent >= 0)
+                       {
+                               tempbonematrix = bonematrix;
+                               Matrix4x4_Concat(&bonematrix, basebonepose + loadmodel->data_bones[boneindex].parent, &tempbonematrix);
+                       }
+                       basebonepose[boneindex] = bonematrix;
+                       Matrix4x4_Invert_Simple(&tempbonematrix, basebonepose + boneindex);
+                       Matrix4x4_ToArray12FloatD3D(&tempbonematrix, outinvmatrix + 12*boneindex);
+               }
+               Mem_Free(basebonepose);
+       }
+
        // 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;
-               float sum;
                for (j = 0;j < numrawweights;j++)
                {
                        if (rawweights[j].pntsindex == vtxw[index].pntsindex)
@@ -2876,91 +3116,927 @@ void Mod_PSKMODEL_Load(dp_model_t *mod, void *buffer, void *bufferend)
                                float influence = rawweights[j].weight;
                                for (l = 0;l < 4;l++)
                                {
-                                       if (loadmodel->surfmesh.data_vertexweightinfluence4f[index*4+l] < influence)
+                                       if (weightinfluence[l] < influence)
                                        {
                                                // move lower influence weights out of the way first
                                                int l2;
                                                for (l2 = 3;l2 > l;l2--)
                                                {
-                                                       loadmodel->surfmesh.data_vertexweightinfluence4f[index*4+l2] = loadmodel->surfmesh.data_vertexweightinfluence4f[index*4+l2-1];
-                                                       loadmodel->surfmesh.data_vertexweightindex4i[index*4+l2] = loadmodel->surfmesh.data_vertexweightindex4i[index*4+l2-1];
+                                                       weightinfluence[l2] = weightinfluence[l2-1];
+                                                       weightindex[l2] = weightindex[l2-1];
                                                }
                                                // store the new weight
-                                               loadmodel->surfmesh.data_vertexweightinfluence4f[index*4+l] = influence;
-                                               loadmodel->surfmesh.data_vertexweightindex4i[index*4+l] = boneindex;
+                                               weightinfluence[l] = influence;
+                                               weightindex[l] = boneindex;
                                                break;
                                        }
                                }
                        }
                }
-               sum = 0;
-               for (l = 0;l < 4;l++)
-                       sum += loadmodel->surfmesh.data_vertexweightinfluence4f[index*4+l];
-               if (sum && fabs(sum - 1) > (1.0f / 256.0f))
+               loadmodel->surfmesh.blends[index] = Mod_Skeletal_CompressBlend(loadmodel, weightindex, weightinfluence);
+               loadmodel->surfmesh.data_skeletalindex4ub[index*4  ] = weightindex[0];
+               loadmodel->surfmesh.data_skeletalindex4ub[index*4+1] = weightindex[1];
+               loadmodel->surfmesh.data_skeletalindex4ub[index*4+2] = weightindex[2];
+               loadmodel->surfmesh.data_skeletalindex4ub[index*4+3] = weightindex[3];
+               loadmodel->surfmesh.data_skeletalweight4ub[index*4  ] = (unsigned char)(weightinfluence[0]*255.0f);
+               loadmodel->surfmesh.data_skeletalweight4ub[index*4+1] = (unsigned char)(weightinfluence[1]*255.0f);
+               loadmodel->surfmesh.data_skeletalweight4ub[index*4+2] = (unsigned char)(weightinfluence[2]*255.0f);
+               loadmodel->surfmesh.data_skeletalweight4ub[index*4+3] = (unsigned char)(weightinfluence[3]*255.0f);
+       }
+       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
+       if (numanims)
+       {
+               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++)
                {
-                       float f = 1.0f / sum;
-                       for (l = 0;l < 4;l++)
-                               loadmodel->surfmesh.data_vertexweightinfluence4f[index*4+l] *= f;
+                       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[7] format for longterm storage
+                       loadmodel->data_poses7s[index*7+0] = k->origin[0] * loadmodel->num_poseinvscale;
+                       loadmodel->data_poses7s[index*7+1] = k->origin[1] * loadmodel->num_poseinvscale;
+                       loadmodel->data_poses7s[index*7+2] = k->origin[2] * loadmodel->num_poseinvscale;
+                       loadmodel->data_poses7s[index*7+3] = quat[0] * 32767.0f;
+                       loadmodel->data_poses7s[index*7+4] = quat[1] * 32767.0f;
+                       loadmodel->data_poses7s[index*7+5] = quat[2] * 32767.0f;
+                       loadmodel->data_poses7s[index*7+6] = quat[3] * 32767.0f;
                }
        }
+       else
+       {
+               strlcpy(loadmodel->animscenes[0].name, "base", sizeof(loadmodel->animscenes[0].name));
+               loadmodel->animscenes[0].firstframe = 0;
+               loadmodel->animscenes[0].framecount = 1;
+               loadmodel->animscenes[0].loop = true;
+               loadmodel->animscenes[0].framerate = 10;
 
-       // set up the animscenes based on the anims
-       for (index = 0, i = 0;index < numanims;index++)
+               // calculate the scaling value for bone origins so they can be compressed to short
+               biggestorigin = 0;
+               for (index = 0;index < numbones;index++)
+               {
+                       pskboneinfo_t *p = bones + index;
+                       biggestorigin = max(biggestorigin, fabs(p->basepose.origin[0]));
+                       biggestorigin = max(biggestorigin, fabs(p->basepose.origin[1]));
+                       biggestorigin = max(biggestorigin, fabs(p->basepose.origin[2]));
+               }
+               loadmodel->num_posescale = biggestorigin / 32767.0f;
+               loadmodel->num_poseinvscale = 1.0f / loadmodel->num_posescale;
+       
+               // load the basepose as a frame
+               for (index = 0;index < numbones;index++)
+               {
+                       pskboneinfo_t *p = bones + index;
+                       float quat[4];
+                       Vector4Copy(p->basepose.quat, quat);
+                       if (quat[3] > 0)
+                               Vector4Negate(quat, quat);
+                       Vector4Normalize2(quat, quat);
+                       // compress poses to the short[7] format for longterm storage
+                       loadmodel->data_poses7s[index*7+0] = p->basepose.origin[0] * loadmodel->num_poseinvscale;
+                       loadmodel->data_poses7s[index*7+1] = p->basepose.origin[1] * loadmodel->num_poseinvscale;
+                       loadmodel->data_poses7s[index*7+2] = p->basepose.origin[2] * loadmodel->num_poseinvscale;
+                       loadmodel->data_poses7s[index*7+3] = quat[0] * 32767.0f;
+                       loadmodel->data_poses7s[index*7+4] = quat[1] * 32767.0f;
+                       loadmodel->data_poses7s[index*7+5] = quat[2] * 32767.0f;
+                       loadmodel->data_poses7s[index*7+6] = quat[3] * 32767.0f;
+               }
+       }
+
+       Mod_FreeSkinFiles(skinfiles);
+       if (animfilebuffer)
+               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_BuildNormals(0, loadmodel->surfmesh.num_vertices, loadmodel->surfmesh.num_triangles, loadmodel->surfmesh.data_vertex3f, loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.data_normal3f, r_smoothnormals_areaweighting.integer != 0);
+       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, r_smoothnormals_areaweighting.integer != 0);
+       if (loadmodel->surfmesh.data_neighbor3i)
+               Mod_BuildTriangleNeighbors(loadmodel->surfmesh.data_neighbor3i, loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.num_triangles);
+       loadmodel->surfmesh.isanimated = Mod_Alias_CalculateBoundingBox();
+       if(mod_alias_force_animated.string[0])
+               loadmodel->surfmesh.isanimated = mod_alias_force_animated.integer != 0;
+
+       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;
+       }
+
+       // because shaders can do somewhat unexpected things, check for unusual features now
+       for (i = 0;i < loadmodel->num_textures;i++)
        {
-               for (j = 0;j < anims[index].numframes;j++, i++)
+               if (loadmodel->data_textures[i].basematerialflags & (MATERIALFLAG_SKY))
+                       mod->DrawSky = R_Q1BSP_DrawSky;
+               if (loadmodel->data_textures[i].basematerialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION | MATERIALFLAG_CAMERA))
+                       mod->DrawAddWaterPlanes = R_Q1BSP_DrawAddWaterPlanes;
+       }
+}
+
+void Mod_INTERQUAKEMODEL_Load(dp_model_t *mod, void *buffer, void *bufferend)
+{
+       unsigned char *data;
+       const char *text;
+       const unsigned char *pbase, *pend;
+       iqmheader_t header;
+       skinfile_t *skinfiles;
+       int i, j, k, meshvertices, meshtriangles;
+       float biggestorigin;
+       const unsigned int *inelements;
+       int *outelements;
+       const int *inneighbors;
+       int *outneighbors;
+       float *outvertex, *outnormal, *outtexcoord, *outsvector, *outtvector, *outcolor;
+       // this pointers into the file data are read only through Little* functions so they can be unaligned memory
+       const float *vnormal = NULL;
+       const float *vposition = NULL;
+       const float *vtangent = NULL;
+       const float *vtexcoord = NULL;
+       const float *vcolor4f = NULL;
+       const unsigned char *vblendindexes = NULL;
+       const unsigned char *vblendweights = NULL;
+       const unsigned char *vcolor4ub = NULL;
+       const unsigned short *framedata = NULL;
+       // temporary memory allocations (because the data in the file may be misaligned)
+       iqmanim_t *anims = NULL;
+       iqmbounds_t *bounds = NULL;
+       iqmjoint1_t *joint1 = NULL;
+       iqmjoint_t *joint = NULL;
+       iqmmesh_t *meshes = NULL;
+       iqmpose1_t *pose1 = NULL;
+       iqmpose_t *pose = NULL;
+       iqmvertexarray_t *vas = NULL;
+
+       pbase = (unsigned char *)buffer;
+       pend = (unsigned char *)bufferend;
+
+       if (pbase + sizeof(iqmheader_t) > pend)
+               Host_Error ("Mod_INTERQUAKEMODEL_Load: %s is not an Inter-Quake Model %d", loadmodel->name, (int)(pend - pbase));
+
+       // copy struct (otherwise it may be misaligned)
+       // LordHavoc: okay it's definitely not misaligned here, but for consistency...
+       memcpy(&header, pbase, sizeof(iqmheader_t));
+
+       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 && LittleLong(header.version) != 2)
+               Host_Error ("Mod_INTERQUAKEMODEL_Load: only version 1 and 2 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.version == 1)
+       {
+               if (pbase + header.ofs_joints + header.num_joints*sizeof(iqmjoint1_t) > pend ||
+                       pbase + header.ofs_poses + header.num_poses*sizeof(iqmpose1_t) > pend)
+               {
+                       Con_Printf("%s has invalid size or offset information\n", loadmodel->name);
+                       return;
+               }
+       }
+       else
+       {
+               if (pbase + header.ofs_joints + header.num_joints*sizeof(iqmjoint_t) > pend ||
+                       pbase + header.ofs_poses + header.num_poses*sizeof(iqmpose_t) > pend)
+               {
+                       Con_Printf("%s has invalid size or offset information\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_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;
+       }
+
+       // copy structs to make them aligned in memory (otherwise we crash on Sparc and PowerPC and others)
+       if (header.num_vertexarrays)
+               vas = (iqmvertexarray_t *)(pbase + header.ofs_vertexarrays);
+       if (header.num_anims)
+               anims = (iqmanim_t *)(pbase + header.ofs_anims);
+       if (header.ofs_bounds)
+               bounds = (iqmbounds_t *)(pbase + header.ofs_bounds);
+       if (header.num_meshes)
+               meshes = (iqmmesh_t *)(pbase + header.ofs_meshes);
+
+       for (i = 0;i < (int)header.num_vertexarrays;i++)
+       {
+               iqmvertexarray_t va;
+               size_t vsize;
+               va.type = LittleLong(vas[i].type);
+               va.flags = LittleLong(vas[i].flags);
+               va.format = LittleLong(vas[i].format);
+               va.size = LittleLong(vas[i].size);
+               va.offset = LittleLong(vas[i].offset);
+               vsize = header.num_vertexes*va.size;
+               switch (va.format)
+               { 
+               case IQM_FLOAT: vsize *= sizeof(float); break;
+               case IQM_UBYTE: vsize *= sizeof(unsigned char); break;
+               default: continue;
+               }
+               if (pbase + va.offset + vsize > pend)
+                       continue;
+               // no need to copy the vertex data for alignment because LittleLong/LittleShort will be invoked on reading them, and the destination is aligned
+               switch (va.type)
                {
-                       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 = 10;
+               case IQM_POSITION:
+                       if (va.format == IQM_FLOAT && va.size == 3)
+                               vposition = (const float *)(pbase + va.offset);
+                       break;
+               case IQM_TEXCOORD:
+                       if (va.format == IQM_FLOAT && va.size == 2)
+                               vtexcoord = (const float *)(pbase + va.offset);
+                       break;
+               case IQM_NORMAL:
+                       if (va.format == IQM_FLOAT && va.size == 3)
+                               vnormal = (const float *)(pbase + va.offset);
+                       break;
+               case IQM_TANGENT:
+                       if (va.format == IQM_FLOAT && va.size == 4)
+                               vtangent = (const float *)(pbase + va.offset);
+                       break;
+               case IQM_BLENDINDEXES:
+                       if (va.format == IQM_UBYTE && va.size == 4)
+                               vblendindexes = (const unsigned char *)(pbase + va.offset);
+                       break;
+               case IQM_BLENDWEIGHTS:
+                       if (va.format == IQM_UBYTE && va.size == 4)
+                               vblendweights = (const unsigned char *)(pbase + va.offset);
+                       break;
+               case IQM_COLOR:
+                       if (va.format == IQM_FLOAT && va.size == 4)
+                               vcolor4f = (const float *)(pbase + va.offset);
+                       if (va.format == IQM_UBYTE && va.size == 4)
+                               vcolor4ub = (const unsigned char *)(pbase + va.offset);
+                       break;
+               }
+       }
+       if (header.num_vertexes > 0 && (!vposition || !vtexcoord || ((header.num_frames > 0 || header.num_anims > 0) && (!vblendindexes || !vblendweights))))
+       {
+               Con_Printf("%s is missing vertex array data\n", loadmodel->name);
+               return;
+       }
+
+       text = header.num_text && header.ofs_text ? (const char *)(pbase + header.ofs_text) : "";
+
+       loadmodel->DrawSky = NULL;
+       loadmodel->DrawAddWaterPlanes = NULL;
+       loadmodel->Draw = R_Q1BSP_Draw;
+       loadmodel->DrawDepth = R_Q1BSP_DrawDepth;
+       loadmodel->DrawDebug = R_Q1BSP_DrawDebug;
+       loadmodel->DrawPrepass = R_Q1BSP_DrawPrepass;
+       loadmodel->CompileShadowMap = R_Q1BSP_CompileShadowMap;
+       loadmodel->DrawShadowMap = R_Q1BSP_DrawShadowMap;
+       loadmodel->CompileShadowVolume = R_Q1BSP_CompileShadowVolume;
+       loadmodel->DrawShadowVolume = R_Q1BSP_DrawShadowVolume;
+       loadmodel->DrawLight = R_Q1BSP_DrawLight;
+       loadmodel->TraceBox = Mod_MDLMD2MD3_TraceBox;
+       loadmodel->TraceLine = Mod_MDLMD2MD3_TraceLine;
+       loadmodel->PointSuperContents = NULL;
+       loadmodel->AnimateVertices = Mod_Skeletal_AnimateVertices;
+
+       // load external .skin files if present
+       skinfiles = Mod_LoadSkinFiles();
+       if (loadmodel->numskins < 1)
+               loadmodel->numskins = 1;
+
+       loadmodel->numframes = max(header.num_anims, 1);
+       loadmodel->num_bones = header.num_joints;
+       loadmodel->num_poses = max(header.num_frames, 1);
+       loadmodel->nummodelsurfaces = loadmodel->num_surfaces = header.num_meshes;
+       loadmodel->num_textures = loadmodel->num_surfaces * loadmodel->numskins;
+       loadmodel->num_texturesperskin = loadmodel->num_surfaces;
+
+       meshvertices = header.num_vertexes;
+       meshtriangles = header.num_triangles;
+
+       // do most allocations as one merged chunk
+       data = (unsigned char *)Mem_Alloc(loadmodel->mempool, loadmodel->num_surfaces * sizeof(msurface_t) + loadmodel->num_surfaces * sizeof(int) + loadmodel->num_surfaces * loadmodel->numskins * sizeof(texture_t) + meshtriangles * sizeof(int[3]) + (meshvertices <= 65536 ? meshtriangles * sizeof(unsigned short[3]) : 0) + (r_enableshadowvolumes.integer ? meshtriangles * sizeof(int[3]) : 0) + meshvertices * (sizeof(float[14]) + (vcolor4f || vcolor4ub ? sizeof(float[4]) : 0)) + (vblendindexes && vblendweights ? meshvertices * (sizeof(unsigned short) + sizeof(unsigned char[2][4])) : 0) + loadmodel->num_poses * loadmodel->num_bones * sizeof(short[7]) + loadmodel->num_bones * sizeof(float[12]) + loadmodel->numskins * sizeof(animscene_t) + loadmodel->num_bones * sizeof(aliasbone_t) + loadmodel->numframes * sizeof(animscene_t));
+       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.num_vertices = meshvertices;
+       loadmodel->surfmesh.num_triangles = meshtriangles;
+       loadmodel->surfmesh.data_element3i = (int *)data;data += meshtriangles * sizeof(int[3]);
+       if (r_enableshadowvolumes.integer)
+       {
+               loadmodel->surfmesh.data_neighbor3i = (int *)data;data += meshtriangles * sizeof(int[3]);
+       }
+       loadmodel->surfmesh.data_vertex3f = (float *)data;data += meshvertices * sizeof(float[3]);
+       loadmodel->surfmesh.data_svector3f = (float *)data;data += meshvertices * sizeof(float[3]);
+       loadmodel->surfmesh.data_tvector3f = (float *)data;data += meshvertices * sizeof(float[3]);
+       loadmodel->surfmesh.data_normal3f = (float *)data;data += meshvertices * sizeof(float[3]);
+       loadmodel->surfmesh.data_texcoordtexture2f = (float *)data;data += meshvertices * sizeof(float[2]);
+       if (vcolor4f || vcolor4ub)
+       {
+               loadmodel->surfmesh.data_lightmapcolor4f = (float *)data;data += meshvertices * sizeof(float[4]);
+       }
+       if (vblendindexes && vblendweights)
+       {
+               loadmodel->surfmesh.data_skeletalindex4ub = (unsigned char *)data;data += meshvertices * sizeof(unsigned char[4]);
+               loadmodel->surfmesh.data_skeletalweight4ub = (unsigned char *)data;data += meshvertices * sizeof(unsigned char[4]);
+       }
+       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);
+       if (vblendindexes && vblendweights)
+       {
+               loadmodel->surfmesh.num_blends = 0;
+               loadmodel->surfmesh.blends = (unsigned short *)data;data += meshvertices * sizeof(unsigned short);
+       }
+       if (meshvertices <= 65536)
+       {
+               loadmodel->surfmesh.data_element3s = (unsigned short *)data;data += meshtriangles * sizeof(unsigned short[3]);
+       }
+       loadmodel->data_poses7s = (short *)data;data += loadmodel->num_poses * loadmodel->num_bones * sizeof(short[7]);
+       if (vblendindexes && vblendweights)
+               loadmodel->surfmesh.data_blendweights = (blendweights_t *)Mem_Alloc(loadmodel->mempool, meshvertices * 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;
+       }
+
+       // load the bone info
+       if (header.version == 1)
+       {
+               iqmjoint1_t *injoint1 = (iqmjoint1_t *)(pbase + header.ofs_joints);
+               if (loadmodel->num_bones)
+                       joint1 = (iqmjoint1_t *)Mem_Alloc(loadmodel->mempool, loadmodel->num_bones * sizeof(iqmjoint1_t));
+               for (i = 0;i < loadmodel->num_bones;i++)
+               {
+                       matrix4x4_t relbase, relinvbase, pinvbase, invbase;
+                       joint1[i].name = LittleLong(injoint1[i].name);
+                       joint1[i].parent = LittleLong(injoint1[i].parent);
+                       for (j = 0;j < 3;j++)
+                       {
+                               joint1[i].origin[j] = LittleFloat(injoint1[i].origin[j]);
+                               joint1[i].rotation[j] = LittleFloat(injoint1[i].rotation[j]);
+                               joint1[i].scale[j] = LittleFloat(injoint1[i].scale[j]);
+                       }
+                       strlcpy(loadmodel->data_bones[i].name, &text[joint1[i].name], sizeof(loadmodel->data_bones[i].name));
+                       loadmodel->data_bones[i].parent = joint1[i].parent;
+                       if (loadmodel->data_bones[i].parent >= i)
+                               Host_Error("%s bone[%i].parent >= %i", loadmodel->name, i, i);
+                       Matrix4x4_FromDoom3Joint(&relbase, joint1[i].origin[0], joint1[i].origin[1], joint1[i].origin[2], joint1[i].rotation[0], joint1[i].rotation[1], joint1[i].rotation[2]);
+                       Matrix4x4_Invert_Simple(&relinvbase, &relbase);
+                       if (loadmodel->data_bones[i].parent >= 0)
+                       {
+                               Matrix4x4_FromArray12FloatD3D(&pinvbase, loadmodel->data_baseboneposeinverse + 12*loadmodel->data_bones[i].parent);
+                               Matrix4x4_Concat(&invbase, &relinvbase, &pinvbase);
+                               Matrix4x4_ToArray12FloatD3D(&invbase, loadmodel->data_baseboneposeinverse + 12*i);
+                       }
+                       else Matrix4x4_ToArray12FloatD3D(&relinvbase, loadmodel->data_baseboneposeinverse + 12*i);
+               }
+       }
+       else
+       {
+               iqmjoint_t *injoint = (iqmjoint_t *)(pbase + header.ofs_joints);
+               if (header.num_joints)
+                       joint = (iqmjoint_t *)Mem_Alloc(loadmodel->mempool, loadmodel->num_bones * sizeof(iqmjoint_t));
+               for (i = 0;i < loadmodel->num_bones;i++)
+               {
+                       matrix4x4_t relbase, relinvbase, pinvbase, invbase;
+                       joint[i].name = LittleLong(injoint[i].name);
+                       joint[i].parent = LittleLong(injoint[i].parent);
+                       for (j = 0;j < 3;j++)
+                       {
+                               joint[i].origin[j] = LittleFloat(injoint[i].origin[j]);
+                               joint[i].rotation[j] = LittleFloat(injoint[i].rotation[j]);
+                               joint[i].scale[j] = LittleFloat(injoint[i].scale[j]);
+                       }
+                       joint[i].rotation[3] = LittleFloat(injoint[i].rotation[3]);
+                       strlcpy(loadmodel->data_bones[i].name, &text[joint[i].name], sizeof(loadmodel->data_bones[i].name));
+                       loadmodel->data_bones[i].parent = joint[i].parent;
+                       if (loadmodel->data_bones[i].parent >= i)
+                               Host_Error("%s bone[%i].parent >= %i", loadmodel->name, i, i);
+                       if (joint[i].rotation[3] > 0)
+                               Vector4Negate(joint[i].rotation, joint[i].rotation);
+                       Vector4Normalize2(joint[i].rotation, joint[i].rotation);
+                       Matrix4x4_FromDoom3Joint(&relbase, joint[i].origin[0], joint[i].origin[1], joint[i].origin[2], joint[i].rotation[0], joint[i].rotation[1], joint[i].rotation[2]);
+                       Matrix4x4_Invert_Simple(&relinvbase, &relbase);
+                       if (loadmodel->data_bones[i].parent >= 0)
+                       {
+                               Matrix4x4_FromArray12FloatD3D(&pinvbase, loadmodel->data_baseboneposeinverse + 12*loadmodel->data_bones[i].parent);
+                               Matrix4x4_Concat(&invbase, &relinvbase, &pinvbase);
+                               Matrix4x4_ToArray12FloatD3D(&invbase, loadmodel->data_baseboneposeinverse + 12*i);
+                       }       
+                       else Matrix4x4_ToArray12FloatD3D(&relinvbase, loadmodel->data_baseboneposeinverse + 12*i);
                }
        }
 
-       // calculate the scaling value for bone origins so they can be compressed to short
+       // set up the animscenes based on the anims
+       for (i = 0;i < (int)header.num_anims;i++)
+       {
+               iqmanim_t anim;
+               anim.name = LittleLong(anims[i].name);
+               anim.first_frame = LittleLong(anims[i].first_frame);
+               anim.num_frames = LittleLong(anims[i].num_frames);
+               anim.framerate = LittleFloat(anims[i].framerate);
+               anim.flags = LittleLong(anims[i].flags);
+               strlcpy(loadmodel->animscenes[i].name, &text[anim.name], sizeof(loadmodel->animscenes[i].name));
+               loadmodel->animscenes[i].firstframe = anim.first_frame;
+               loadmodel->animscenes[i].framecount = anim.num_frames;
+               loadmodel->animscenes[i].loop = ((anim.flags & IQM_LOOP) != 0);
+               loadmodel->animscenes[i].framerate = anim.framerate;
+       }
+       if (header.num_anims <= 0)
+       {
+               strlcpy(loadmodel->animscenes[0].name, "static", sizeof(loadmodel->animscenes[0].name));
+               loadmodel->animscenes[0].firstframe = 0;
+               loadmodel->animscenes[0].framecount = 1;
+               loadmodel->animscenes[0].loop = true;
+               loadmodel->animscenes[0].framerate = 10;
+       }
+
+       loadmodel->surfmesh.isanimated = loadmodel->num_bones > 1 || loadmodel->numframes > 1 || (loadmodel->animscenes && loadmodel->animscenes[0].framecount > 1);
+       if(mod_alias_force_animated.string[0])
+               loadmodel->surfmesh.isanimated = mod_alias_force_animated.integer != 0;
+
        biggestorigin = 0;
-       for (index = 0;index < numanimkeys;index++)
+       if (header.version == 1)
        {
-               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]));
+               iqmpose1_t *inpose1 = (iqmpose1_t *)(pbase + header.ofs_poses);
+               if (header.num_poses)
+                       pose1 = (iqmpose1_t *)Mem_Alloc(loadmodel->mempool, header.num_poses * sizeof(iqmpose1_t));
+               for (i = 0;i < (int)header.num_poses;i++)
+               {
+                       float f;
+                       pose1[i].parent = LittleLong(inpose1[i].parent);
+                       pose1[i].channelmask = LittleLong(inpose1[i].channelmask);
+                       for (j = 0;j < 9;j++)
+                       {
+                               pose1[i].channeloffset[j] = LittleFloat(inpose1[i].channeloffset[j]);
+                               pose1[i].channelscale[j] = LittleFloat(inpose1[i].channelscale[j]);
+                       }
+                       f = fabs(pose1[i].channeloffset[0]); biggestorigin = max(biggestorigin, f);
+                       f = fabs(pose1[i].channeloffset[1]); biggestorigin = max(biggestorigin, f);
+                       f = fabs(pose1[i].channeloffset[2]); biggestorigin = max(biggestorigin, f);
+                       f = fabs(pose1[i].channeloffset[0] + 0xFFFF*pose1[i].channelscale[0]); biggestorigin = max(biggestorigin, f);
+                       f = fabs(pose1[i].channeloffset[1] + 0xFFFF*pose1[i].channelscale[1]); biggestorigin = max(biggestorigin, f);
+                       f = fabs(pose1[i].channeloffset[2] + 0xFFFF*pose1[i].channelscale[2]); biggestorigin = max(biggestorigin, f);
+               }
+               if (header.num_frames <= 0)
+               {
+                       for (i = 0;i < loadmodel->num_bones;i++)
+                       {
+                               float f;
+                               f = fabs(joint1[i].origin[0]); biggestorigin = max(biggestorigin, f);
+                               f = fabs(joint1[i].origin[1]); biggestorigin = max(biggestorigin, f);
+                               f = fabs(joint1[i].origin[2]); biggestorigin = max(biggestorigin, f);
+                       }
+               }
+       }
+       else
+       {
+               iqmpose_t *inpose = (iqmpose_t *)(pbase + header.ofs_poses);
+               if (header.num_poses)
+                       pose = (iqmpose_t *)Mem_Alloc(loadmodel->mempool, header.num_poses * sizeof(iqmpose_t));
+               for (i = 0;i < (int)header.num_poses;i++)
+               {
+                       float f;
+                       pose[i].parent = LittleLong(inpose[i].parent);
+                       pose[i].channelmask = LittleLong(inpose[i].channelmask);
+                       for (j = 0;j < 10;j++)
+                       {
+                               pose[i].channeloffset[j] = LittleFloat(inpose[i].channeloffset[j]);
+                               pose[i].channelscale[j] = LittleFloat(inpose[i].channelscale[j]);
+                       }
+                       f = fabs(pose[i].channeloffset[0]); biggestorigin = max(biggestorigin, f);
+                       f = fabs(pose[i].channeloffset[1]); biggestorigin = max(biggestorigin, f);
+                       f = fabs(pose[i].channeloffset[2]); biggestorigin = max(biggestorigin, f);
+                       f = fabs(pose[i].channeloffset[0] + 0xFFFF*pose[i].channelscale[0]); biggestorigin = max(biggestorigin, f);
+                       f = fabs(pose[i].channeloffset[1] + 0xFFFF*pose[i].channelscale[1]); biggestorigin = max(biggestorigin, f);
+                       f = fabs(pose[i].channeloffset[2] + 0xFFFF*pose[i].channelscale[2]); biggestorigin = max(biggestorigin, f);
+               }
+               if (header.num_frames <= 0)
+               {
+                       for (i = 0;i < loadmodel->num_bones;i++)
+                       {
+                               float f;
+                               f = fabs(joint[i].origin[0]); biggestorigin = max(biggestorigin, f);
+                               f = fabs(joint[i].origin[1]); biggestorigin = max(biggestorigin, f);
+                               f = fabs(joint[i].origin[2]); biggestorigin = max(biggestorigin, f);
+                       }
+               }
        }
        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;
+       // load the pose data
+       // this unaligned memory access is safe (LittleShort reads as bytes)
+       framedata = (const unsigned short *)(pbase + header.ofs_frames);
+       if (header.version == 1)
+       {
+               for (i = 0, k = 0;i < (int)header.num_frames;i++)
+               {
+                       for (j = 0;j < (int)header.num_poses;j++, k++)
+                       {
+                               float qx, qy, qz, qw;
+                               loadmodel->data_poses7s[k*7 + 0] = loadmodel->num_poseinvscale * (pose1[j].channeloffset[0] + (pose1[j].channelmask&1 ? (unsigned short)LittleShort(*framedata++) * pose1[j].channelscale[0] : 0));
+                               loadmodel->data_poses7s[k*7 + 1] = loadmodel->num_poseinvscale * (pose1[j].channeloffset[1] + (pose1[j].channelmask&2 ? (unsigned short)LittleShort(*framedata++) * pose1[j].channelscale[1] : 0));
+                               loadmodel->data_poses7s[k*7 + 2] = loadmodel->num_poseinvscale * (pose1[j].channeloffset[2] + (pose1[j].channelmask&4 ? (unsigned short)LittleShort(*framedata++) * pose1[j].channelscale[2] : 0));
+                               qx = pose1[j].channeloffset[3] + (pose1[j].channelmask&8 ? (unsigned short)LittleShort(*framedata++) * pose1[j].channelscale[3] : 0);
+                               qy = pose1[j].channeloffset[4] + (pose1[j].channelmask&16 ? (unsigned short)LittleShort(*framedata++) * pose1[j].channelscale[4] : 0);
+                               qz = pose1[j].channeloffset[5] + (pose1[j].channelmask&32 ? (unsigned short)LittleShort(*framedata++) * pose1[j].channelscale[5] : 0);
+                               qw = 1.0f - (qx*qx + qy*qy + qz*qz);
+                               qw = qw > 0.0f ? -sqrt(qw) : 0.0f;
+                               loadmodel->data_poses7s[k*7 + 3] = 32767.0f * qx;
+                               loadmodel->data_poses7s[k*7 + 4] = 32767.0f * qy;
+                               loadmodel->data_poses7s[k*7 + 5] = 32767.0f * qz;
+                               loadmodel->data_poses7s[k*7 + 6] = 32767.0f * qw;
+                               // skip scale data for now
+                               if(pose1[j].channelmask&64) framedata++;
+                               if(pose1[j].channelmask&128) framedata++;
+                               if(pose1[j].channelmask&256) framedata++;
+                       }
+               }
+               if (header.num_frames <= 0)
+               {
+                       for (i = 0;i < loadmodel->num_bones;i++)
+                       {
+                               float qx, qy, qz, qw;
+                               loadmodel->data_poses7s[i*7 + 0] = loadmodel->num_poseinvscale * joint1[i].origin[0];
+                               loadmodel->data_poses7s[i*7 + 1] = loadmodel->num_poseinvscale * joint1[i].origin[1];
+                               loadmodel->data_poses7s[i*7 + 2] = loadmodel->num_poseinvscale * joint1[i].origin[2];
+                               qx = joint1[i].rotation[0];
+                               qy = joint1[i].rotation[1];
+                               qz = joint1[i].rotation[2];
+                               qw = 1.0f - (qx*qx + qy*qy + qz*qz);
+                               qw = qw > 0.0f ? -sqrt(qw) : 0.0f;
+                               loadmodel->data_poses7s[i*7 + 3] = 32767.0f * qx;
+                               loadmodel->data_poses7s[i*7 + 4] = 32767.0f * qy;
+                               loadmodel->data_poses7s[i*7 + 5] = 32767.0f * qz;
+                               loadmodel->data_poses7s[i*7 + 6] = 32767.0f * qw;
+                       }
+               }
+       }
+       else
+       {
+               for (i = 0, k = 0;i < (int)header.num_frames;i++)       
+               {
+                       for (j = 0;j < (int)header.num_poses;j++, k++)
+                       {
+                               float rot[4];
+                               loadmodel->data_poses7s[k*7 + 0] = loadmodel->num_poseinvscale * (pose[j].channeloffset[0] + (pose[j].channelmask&1 ? (unsigned short)LittleShort(*framedata++) * pose[j].channelscale[0] : 0));
+                               loadmodel->data_poses7s[k*7 + 1] = loadmodel->num_poseinvscale * (pose[j].channeloffset[1] + (pose[j].channelmask&2 ? (unsigned short)LittleShort(*framedata++) * pose[j].channelscale[1] : 0));
+                               loadmodel->data_poses7s[k*7 + 2] = loadmodel->num_poseinvscale * (pose[j].channeloffset[2] + (pose[j].channelmask&4 ? (unsigned short)LittleShort(*framedata++) * pose[j].channelscale[2] : 0));
+                               rot[0] = pose[j].channeloffset[3] + (pose[j].channelmask&8 ? (unsigned short)LittleShort(*framedata++) * pose[j].channelscale[3] : 0);
+                               rot[1] = pose[j].channeloffset[4] + (pose[j].channelmask&16 ? (unsigned short)LittleShort(*framedata++) * pose[j].channelscale[4] : 0);
+                               rot[2] = pose[j].channeloffset[5] + (pose[j].channelmask&32 ? (unsigned short)LittleShort(*framedata++) * pose[j].channelscale[5] : 0);
+                               rot[3] = pose[j].channeloffset[6] + (pose[j].channelmask&64 ? (unsigned short)LittleShort(*framedata++) * pose[j].channelscale[6] : 0);
+                               if (rot[3] > 0)
+                                       Vector4Negate(rot, rot);
+                               Vector4Normalize2(rot, rot);
+                               loadmodel->data_poses7s[k*7 + 3] = 32767.0f * rot[0];
+                               loadmodel->data_poses7s[k*7 + 4] = 32767.0f * rot[1];
+                               loadmodel->data_poses7s[k*7 + 5] = 32767.0f * rot[2];
+                               loadmodel->data_poses7s[k*7 + 6] = 32767.0f * rot[3];
+                               // skip scale data for now
+                               if(pose[j].channelmask&128) framedata++;
+                               if(pose[j].channelmask&256) framedata++;
+                               if(pose[j].channelmask&512) framedata++;
+                       }
+               }
+               if (header.num_frames <= 0)
+               {
+                       for (i = 0;i < loadmodel->num_bones;i++)
+                       {
+                               loadmodel->data_poses7s[i*7 + 0] = loadmodel->num_poseinvscale * joint[i].origin[0];
+                               loadmodel->data_poses7s[i*7 + 1] = loadmodel->num_poseinvscale * joint[i].origin[1];
+                               loadmodel->data_poses7s[i*7 + 2] = loadmodel->num_poseinvscale * joint[i].origin[2];
+                               loadmodel->data_poses7s[i*7 + 3] = 32767.0f * joint[i].rotation[0];
+                               loadmodel->data_poses7s[i*7 + 4] = 32767.0f * joint[i].rotation[1];
+                               loadmodel->data_poses7s[i*7 + 5] = 32767.0f * joint[i].rotation[2];
+                               loadmodel->data_poses7s[i*7 + 6] = 32767.0f * joint[i].rotation[3];
+                       }
+               }
        }
+
+       // load bounding box data
+       if (header.ofs_bounds)
+       {
+               float xyradius = 0, radius = 0;
+               VectorClear(loadmodel->normalmins);
+               VectorClear(loadmodel->normalmaxs);
+               for (i = 0; i < (int)header.num_frames;i++)
+               {
+                       iqmbounds_t bound;
+                       bound.mins[0] = LittleFloat(bounds[i].mins[0]);
+                       bound.mins[1] = LittleFloat(bounds[i].mins[1]);
+                       bound.mins[2] = LittleFloat(bounds[i].mins[2]);
+                       bound.maxs[0] = LittleFloat(bounds[i].maxs[0]);                 
+                       bound.maxs[1] = LittleFloat(bounds[i].maxs[1]); 
+                       bound.maxs[2] = LittleFloat(bounds[i].maxs[2]); 
+                       bound.xyradius = LittleFloat(bounds[i].xyradius);
+                       bound.radius = LittleFloat(bounds[i].radius);
+                       if (!i)
+                       {
+                               VectorCopy(bound.mins, loadmodel->normalmins);
+                               VectorCopy(bound.maxs, loadmodel->normalmaxs);
+                       }
+                       else
+                       {
+                               if (loadmodel->normalmins[0] > bound.mins[0]) loadmodel->normalmins[0] = bound.mins[0];
+                               if (loadmodel->normalmins[1] > bound.mins[1]) loadmodel->normalmins[1] = bound.mins[1];
+                               if (loadmodel->normalmins[2] > bound.mins[2]) loadmodel->normalmins[2] = bound.mins[2];
+                               if (loadmodel->normalmaxs[0] < bound.maxs[0]) loadmodel->normalmaxs[0] = bound.maxs[0];
+                               if (loadmodel->normalmaxs[1] < bound.maxs[1]) loadmodel->normalmaxs[1] = bound.maxs[1];
+                               if (loadmodel->normalmaxs[2] < bound.maxs[2]) loadmodel->normalmaxs[2] = bound.maxs[2];
+                       }
+                       if (bound.xyradius > xyradius)
+                               xyradius = bound.xyradius;
+                       if (bound.radius > radius)
+                               radius = bound.radius;
+               }
+               loadmodel->yawmins[0] = loadmodel->yawmins[1] = -xyradius;
+               loadmodel->yawmaxs[0] = loadmodel->yawmaxs[1] = xyradius;
+               loadmodel->yawmins[2] = loadmodel->normalmins[2];
+               loadmodel->yawmaxs[2] = loadmodel->normalmaxs[2];
+               loadmodel->rotatedmins[0] = loadmodel->rotatedmins[1] = loadmodel->rotatedmins[2] = -radius;
+               loadmodel->rotatedmaxs[0] = loadmodel->rotatedmaxs[1] = loadmodel->rotatedmaxs[2] = radius;
+               loadmodel->radius = radius;
+               loadmodel->radius2 = radius * radius;
+       }
+
+       // load triangle data
+       // this unaligned memory access is safe (LittleLong reads as bytes)
+       inelements = (const unsigned int *)(pbase + header.ofs_triangles);
+       outelements = loadmodel->surfmesh.data_element3i;
+       for (i = 0;i < (int)header.num_triangles;i++)
+       {
+               outelements[0] = LittleLong(inelements[0]);
+               outelements[1] = LittleLong(inelements[1]);
+               outelements[2] = LittleLong(inelements[2]);
+               outelements += 3;
+               inelements += 3;
+       }
+       Mod_ValidateElements(loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.num_triangles, 0, header.num_vertexes, __FILE__, __LINE__);
+
+       if (header.ofs_neighbors && loadmodel->surfmesh.data_neighbor3i)
+       {
+               // this unaligned memory access is safe (LittleLong reads as bytes)
+               inneighbors = (const int *)(pbase + header.ofs_neighbors);
+               outneighbors = loadmodel->surfmesh.data_neighbor3i;
+               for (i = 0;i < (int)header.num_triangles;i++)
+               {
+                       outneighbors[0] = LittleLong(inneighbors[0]);
+                       outneighbors[1] = LittleLong(inneighbors[1]);
+                       outneighbors[2] = LittleLong(inneighbors[2]);
+                       outneighbors += 3;
+                       inneighbors += 3;
+               }
+       }
+
+       // load vertex data
+       // this unaligned memory access is safe (LittleFloat reads as bytes)
+       outvertex = loadmodel->surfmesh.data_vertex3f;
+       for (i = 0;i < (int)header.num_vertexes;i++)
+       {
+               outvertex[0] = LittleFloat(vposition[0]);
+               outvertex[1] = LittleFloat(vposition[1]);
+               outvertex[2] = LittleFloat(vposition[2]);
+               vposition += 3;
+               outvertex += 3;
+       }
+
+       outtexcoord = loadmodel->surfmesh.data_texcoordtexture2f;
+       // this unaligned memory access is safe (LittleFloat reads as bytes)
+       for (i = 0;i < (int)header.num_vertexes;i++)
+       {
+               outtexcoord[0] = LittleFloat(vtexcoord[0]);
+               outtexcoord[1] = LittleFloat(vtexcoord[1]);
+               vtexcoord += 2;
+               outtexcoord += 2;
+       }
+
+       // this unaligned memory access is safe (LittleFloat reads as bytes)
+       if(vnormal)
+       {
+               outnormal = loadmodel->surfmesh.data_normal3f;
+               for (i = 0;i < (int)header.num_vertexes;i++)
+               {
+                       outnormal[0] = LittleFloat(vnormal[0]);
+                       outnormal[1] = LittleFloat(vnormal[1]);
+                       outnormal[2] = LittleFloat(vnormal[2]);
+                       vnormal += 3;
+                       outnormal += 3;
+               }
+       }
+
+       // this unaligned memory access is safe (LittleFloat reads as bytes)
+       if(vnormal && vtangent)
+       {
+               outnormal = loadmodel->surfmesh.data_normal3f;
+               outsvector = loadmodel->surfmesh.data_svector3f;
+               outtvector = loadmodel->surfmesh.data_tvector3f;
+               for (i = 0;i < (int)header.num_vertexes;i++)
+               {
+                       outsvector[0] = LittleFloat(vtangent[0]);
+                       outsvector[1] = LittleFloat(vtangent[1]);
+                       outsvector[2] = LittleFloat(vtangent[2]);
+                       if(LittleFloat(vtangent[3]) < 0)
+                               CrossProduct(outsvector, outnormal, outtvector);
+                       else
+                               CrossProduct(outnormal, outsvector, outtvector);
+                       vtangent += 4;
+                       outnormal += 3;
+                       outsvector += 3;
+                       outtvector += 3;
+               }
+       }
+
+       // this unaligned memory access is safe (all bytes)
+       if (vblendindexes && vblendweights)
+       {
+               for (i = 0; i < (int)header.num_vertexes;i++)
+               {
+                       blendweights_t weights;
+                       memcpy(weights.index, vblendindexes + i*4, 4);
+                       memcpy(weights.influence, vblendweights + i*4, 4);
+                       loadmodel->surfmesh.blends[i] = Mod_Skeletal_AddBlend(loadmodel, &weights);
+                       loadmodel->surfmesh.data_skeletalindex4ub[i*4  ] = weights.index[0];
+                       loadmodel->surfmesh.data_skeletalindex4ub[i*4+1] = weights.index[1];
+                       loadmodel->surfmesh.data_skeletalindex4ub[i*4+2] = weights.index[2];
+                       loadmodel->surfmesh.data_skeletalindex4ub[i*4+3] = weights.index[3];
+                       loadmodel->surfmesh.data_skeletalweight4ub[i*4  ] = weights.influence[0];
+                       loadmodel->surfmesh.data_skeletalweight4ub[i*4+1] = weights.influence[1];
+                       loadmodel->surfmesh.data_skeletalweight4ub[i*4+2] = weights.influence[2];
+                       loadmodel->surfmesh.data_skeletalweight4ub[i*4+3] = weights.influence[3];
+               }
+       }
+
+       if (vcolor4f)
+       {
+               outcolor = loadmodel->surfmesh.data_lightmapcolor4f;
+               // this unaligned memory access is safe (LittleFloat reads as bytes)
+               for (i = 0;i < (int)header.num_vertexes;i++)
+               {
+                       outcolor[0] = LittleFloat(vcolor4f[0]);
+                       outcolor[1] = LittleFloat(vcolor4f[1]);
+                       outcolor[2] = LittleFloat(vcolor4f[2]);
+                       outcolor[3] = LittleFloat(vcolor4f[3]);
+                       vcolor4f += 4;
+                       outcolor += 4;
+               }
+       }
+       else if (vcolor4ub)
+       {
+               outcolor = loadmodel->surfmesh.data_lightmapcolor4f;
+               // this unaligned memory access is safe (all bytes)
+               for (i = 0;i < (int)header.num_vertexes;i++)
+               {
+                       outcolor[0] = vcolor4ub[0] * (1.0f / 255.0f);
+                       outcolor[1] = vcolor4ub[1] * (1.0f / 255.0f);
+                       outcolor[2] = vcolor4ub[2] * (1.0f / 255.0f);
+                       outcolor[3] = vcolor4ub[3] * (1.0f / 255.0f);
+                       vcolor4ub += 4;
+                       outcolor += 4;
+               }
+       }
+
+       // load meshes
+       for (i = 0;i < (int)header.num_meshes;i++)
+       {
+               iqmmesh_t mesh;
+               msurface_t *surface;
+
+               mesh.name = LittleLong(meshes[i].name);
+               mesh.material = LittleLong(meshes[i].material);
+               mesh.first_vertex = LittleLong(meshes[i].first_vertex);
+               mesh.num_vertexes = LittleLong(meshes[i].num_vertexes);
+               mesh.first_triangle = LittleLong(meshes[i].first_triangle);
+               mesh.num_triangles = LittleLong(meshes[i].num_triangles);
+
+               loadmodel->sortedmodelsurfaces[i] = i;
+               surface = loadmodel->data_surfaces + i;
+               surface->texture = loadmodel->data_textures + i;
+               surface->num_firsttriangle = mesh.first_triangle;
+               surface->num_triangles = mesh.num_triangles;
+               surface->num_firstvertex = mesh.first_vertex;
+               surface->num_vertices = mesh.num_vertexes;
+
+               Mod_BuildAliasSkinsFromSkinFiles(loadmodel->data_textures + i, skinfiles, &text[mesh.name], &text[mesh.material]);
+       }
+
        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();
+       if (!vnormal)
+               Mod_BuildNormals(0, loadmodel->surfmesh.num_vertices, loadmodel->surfmesh.num_triangles, loadmodel->surfmesh.data_vertex3f, loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.data_normal3f, r_smoothnormals_areaweighting.integer != 0);
+       if (!vnormal || !vtangent)
+               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, r_smoothnormals_areaweighting.integer != 0);
+       if (!header.ofs_neighbors && loadmodel->surfmesh.data_neighbor3i)
+               Mod_BuildTriangleNeighbors(loadmodel->surfmesh.data_neighbor3i, loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.num_triangles);
+       if (!header.ofs_bounds)
+               Mod_Alias_CalculateBoundingBox();
+
+       if (!loadmodel->surfmesh.isanimated && loadmodel->surfmesh.num_triangles >= 1)
+       {
+               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;
+       }
 
-       loadmodel->surfmesh.isanimated = loadmodel->numframes > 1 || loadmodel->animscenes[0].framecount > 1;
+       if (joint        ) Mem_Free(joint        );joint         = NULL;
+       if (joint1       ) Mem_Free(joint1       );joint1        = NULL;
+       if (pose         ) Mem_Free(pose         );pose          = NULL;
+       if (pose1        ) Mem_Free(pose1        );pose1         = NULL;
+
+       // because shaders can do somewhat unexpected things, check for unusual features now
+       for (i = 0;i < loadmodel->num_textures;i++)
+       {
+               if (loadmodel->data_textures[i].basematerialflags & (MATERIALFLAG_SKY))
+                       mod->DrawSky = R_Q1BSP_DrawSky;
+               if (loadmodel->data_textures[i].basematerialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION | MATERIALFLAG_CAMERA))
+                       mod->DrawAddWaterPlanes = R_Q1BSP_DrawAddWaterPlanes;
+       }
 }