transform matrix is now separate from R_Mesh_State, a new function called R_Mesh_Matr...
[xonotic/darkplaces.git] / gl_models.c
index 42b68fc..38064f1 100644 (file)
 
 #include "quakedef.h"
 
+cvar_t r_quickmodels = {0, "r_quickmodels", "1"};
+
 typedef struct
 {
        float m[3][4];
 } zymbonematrix;
 
-// LordHavoc: vertex array
-float *aliasvert;
-float *modelaliasvert;
+// LordHavoc: vertex arrays
+
+float *aliasvertbuf;
+float *aliasvertcolorbuf;
+float *aliasvert; // this may point at aliasvertbuf or at vertex arrays in the mesh backend
+float *aliasvertcolor; // this may point at aliasvertcolorbuf or at vertex arrays in the mesh backend
+
+float *aliasvertcolor2;
 float *aliasvertnorm;
-byte *aliasvertcolor;
-byte *aliasvertcolor2;
-zymbonematrix *zymbonepose;
 int *aliasvertusage;
+zymbonematrix *zymbonepose;
 
-int chrometexture;
+mempool_t *gl_models_mempool;
 
-void makechrometexture()
+void gl_models_start(void)
 {
-       int i;
-       byte noise[64*64];
-       byte data[64*64][4];
-
-       fractalnoise(noise, 64, 16);
-
-       // convert to RGBA data
-       for (i = 0;i < 64*64;i++)
-       {
-               data[i][0] = data[i][1] = data[i][2] = noise[i];
-               data[i][3] = 255;
-       }
+       // allocate vertex processing arrays
+       gl_models_mempool = Mem_AllocPool("GL_Models");
+       aliasvert = aliasvertbuf = Mem_Alloc(gl_models_mempool, sizeof(float[MD2MAX_VERTS][4]));
+       aliasvertcolor = aliasvertcolorbuf = Mem_Alloc(gl_models_mempool, sizeof(float[MD2MAX_VERTS][4]));
+       aliasvertnorm = Mem_Alloc(gl_models_mempool, sizeof(float[MD2MAX_VERTS][3]));
+       aliasvertcolor2 = Mem_Alloc(gl_models_mempool, sizeof(float[MD2MAX_VERTS][4])); // used temporarily for tinted coloring
+       zymbonepose = Mem_Alloc(gl_models_mempool, sizeof(zymbonematrix[256]));
+       aliasvertusage = Mem_Alloc(gl_models_mempool, sizeof(int[MD2MAX_VERTS]));
+}
 
-       chrometexture = GL_LoadTexture ("chrometexture", 64, 64, &data[0][0], true, false, 4);
+void gl_models_shutdown(void)
+{
+       Mem_FreePool(&gl_models_mempool);
 }
 
-void gl_models_start()
+void gl_models_newmap(void)
 {
-       // allocate vertex processing arrays
-       aliasvert = qmalloc(sizeof(float[MD2MAX_VERTS][3]));
-       modelaliasvert = qmalloc(sizeof(float[MD2MAX_VERTS][3]));
-       aliasvertnorm = qmalloc(sizeof(float[MD2MAX_VERTS][3]));
-       aliasvertcolor = qmalloc(sizeof(byte[MD2MAX_VERTS][4]));
-       aliasvertcolor2 = qmalloc(sizeof(byte[MD2MAX_VERTS][4])); // used temporarily for tinted coloring
-       zymbonepose = qmalloc(sizeof(zymbonematrix[256]));
-       aliasvertusage = qmalloc(sizeof(int[MD2MAX_VERTS]));
-       makechrometexture();
 }
 
-void gl_models_shutdown()
+void GL_Models_Init(void)
 {
-       qfree(aliasvert);
-       qfree(aliasvertnorm);
-       qfree(aliasvertcolor);
-       qfree(aliasvertcolor2);
-       qfree(zymbonepose);
-       qfree(aliasvertusage);
+       Cvar_RegisterVariable(&r_quickmodels);
+
+       R_RegisterModule("GL_Models", gl_models_start, gl_models_shutdown, gl_models_newmap);
 }
 
-void GL_Models_Init()
+/*
+void R_AliasTransformVerts(int vertcount)
 {
-       R_RegisterModule("GL_Models", gl_models_start, gl_models_shutdown);
+       vec3_t point;
+       float *av;
+       av = aliasvert;
+       while (vertcount >= 4)
+       {
+               VectorCopy(av, point);softwaretransform(point, av);av += 4;
+               VectorCopy(av, point);softwaretransform(point, av);av += 4;
+               VectorCopy(av, point);softwaretransform(point, av);av += 4;
+               VectorCopy(av, point);softwaretransform(point, av);av += 4;
+               vertcount -= 4;
+       }
+       while(vertcount > 0)
+       {
+               VectorCopy(av, point);softwaretransform(point, av);av += 4;
+               vertcount--;
+       }
 }
+*/
 
-extern vec3_t softwaretransform_x;
-extern vec3_t softwaretransform_y;
-extern vec3_t softwaretransform_z;
-extern vec_t softwaretransform_scale;
-extern vec3_t softwaretransform_offset;
-extern cvar_t r_modelsdonttransformnormals;
-void R_AliasLerpVerts(int vertcount, float lerp, trivert2 *verts1, vec3_t scale1, vec3_t translate1, trivert2 *verts2, vec3_t scale2, vec3_t translate2)
+void R_AliasLerpVerts(int vertcount,
+               float lerp1, const trivertx_t *verts1, const vec3_t fscale1, const vec3_t translate1,
+               float lerp2, const trivertx_t *verts2, const vec3_t fscale2, const vec3_t translate2,
+               float lerp3, const trivertx_t *verts3, const vec3_t fscale3, const vec3_t translate3,
+               float lerp4, const trivertx_t *verts4, const vec3_t fscale4, const vec3_t translate4)
 {
        int i;
-       vec3_t point, matrix_x, matrix_y, matrix_z;
+       vec3_t scale1, scale2, scale3, scale4, translate;
+       const float *n1, *n2, *n3, *n4;
        float *av, *avn;
        av = aliasvert;
        avn = aliasvertnorm;
-       VectorScale(softwaretransform_x, softwaretransform_scale, matrix_x);
-       VectorScale(softwaretransform_y, softwaretransform_scale, matrix_y);
-       VectorScale(softwaretransform_z, softwaretransform_scale, matrix_z);
-       if (lerp < 0) lerp = 0;
-       if (lerp > 1) lerp = 1;
-       if (lerp != 0)
+       VectorScale(fscale1, lerp1, scale1);
+       if (lerp2)
        {
-               float ilerp, ilerp127, lerp127, scalex1, scalex2, translatex, scaley1, scaley2, translatey, scalez1, scalez2, translatez;
-               ilerp = 1 - lerp;
-               ilerp127 = ilerp * (1.0 / 127.0);
-               lerp127 = lerp * (1.0 / 127.0);
-               // calculate combined interpolation variables
-               scalex1 = scale1[0] * ilerp;scalex2 = scale2[0] *  lerp;translatex = translate1[0] * ilerp + translate2[0] *  lerp;
-               scaley1 = scale1[1] * ilerp;scaley2 = scale2[1] *  lerp;translatey = translate1[1] * ilerp + translate2[1] *  lerp;
-               scalez1 = scale1[2] * ilerp;scalez2 = scale2[2] *  lerp;translatez = translate1[2] * ilerp + translate2[2] *  lerp;
-               // generate vertices
-               if (r_modelsdonttransformnormals.value)
+               VectorScale(fscale2, lerp2, scale2);
+               if (lerp3)
                {
-                       float *modelav = modelaliasvert;
-                       for (i = 0;i < vertcount;i++)
+                       VectorScale(fscale3, lerp3, scale3);
+                       if (lerp4)
                        {
-                               // rotate, scale, and translate the vertex locations
-                               point[0] = verts1->v[0] * scalex1 + verts2->v[0] * scalex2 + translatex;
-                               point[1] = verts1->v[1] * scaley1 + verts2->v[1] * scaley2 + translatey;
-                               point[2] = verts1->v[2] * scalez1 + verts2->v[2] * scalez2 + translatez;
-                               // save mostly un-transformed copy for lighting
-                               modelav[0] = point[0] * softwaretransform_scale;
-                               modelav[1] = point[1] * softwaretransform_scale;
-                               modelav[2] = point[2] * softwaretransform_scale;
-                               av[0] = point[0] * matrix_x[0] + point[1] * matrix_y[0] + point[2] * matrix_z[0] + softwaretransform_offset[0];
-                               av[1] = point[0] * matrix_x[1] + point[1] * matrix_y[1] + point[2] * matrix_z[1] + softwaretransform_offset[1];
-                               av[2] = point[0] * matrix_x[2] + point[1] * matrix_y[2] + point[2] * matrix_z[2] + softwaretransform_offset[2];
-                               // decompress but do not transform the normals
-                               avn[0] = verts1->n[0] * ilerp127 + verts2->n[0] * lerp127;
-                               avn[1] = verts1->n[1] * ilerp127 + verts2->n[1] * lerp127;
-                               avn[2] = verts1->n[2] * ilerp127 + verts2->n[2] * lerp127;
-                               modelav += 3;
-                               av += 3;
-                               avn += 3;
-                               verts1++;verts2++;
+                               VectorScale(fscale4, lerp4, scale4);
+                               translate[0] = translate1[0] * lerp1 + translate2[0] * lerp2 + translate3[0] * lerp3 + translate4[0] * lerp4;
+                               translate[1] = translate1[1] * lerp1 + translate2[1] * lerp2 + translate3[1] * lerp3 + translate4[1] * lerp4;
+                               translate[2] = translate1[2] * lerp1 + translate2[2] * lerp2 + translate3[2] * lerp3 + translate4[2] * lerp4;
+                               // generate vertices
+                               for (i = 0;i < vertcount;i++)
+                               {
+                                       av[0] = verts1->v[0] * scale1[0] + verts2->v[0] * scale2[0] + verts3->v[0] * scale3[0] + verts4->v[0] * scale4[0] + translate[0];
+                                       av[1] = verts1->v[1] * scale1[1] + verts2->v[1] * scale2[1] + verts3->v[1] * scale3[1] + verts4->v[1] * scale4[1] + translate[1];
+                                       av[2] = verts1->v[2] * scale1[2] + verts2->v[2] * scale2[2] + verts3->v[2] * scale3[2] + verts4->v[2] * scale4[2] + translate[2];
+                                       n1 = m_bytenormals[verts1->lightnormalindex];
+                                       n2 = m_bytenormals[verts2->lightnormalindex];
+                                       n3 = m_bytenormals[verts3->lightnormalindex];
+                                       n4 = m_bytenormals[verts4->lightnormalindex];
+                                       avn[0] = n1[0] * lerp1 + n2[0] * lerp2 + n3[0] * lerp3 + n4[0] * lerp4;
+                                       avn[1] = n1[1] * lerp1 + n2[1] * lerp2 + n3[1] * lerp3 + n4[1] * lerp4;
+                                       avn[2] = n1[2] * lerp1 + n2[2] * lerp2 + n3[2] * lerp3 + n4[2] * lerp4;
+                                       av += 4;
+                                       avn += 3;
+                                       verts1++;verts2++;verts3++;verts4++;
+                               }
+                       }
+                       else
+                       {
+                               translate[0] = translate1[0] * lerp1 + translate2[0] * lerp2 + translate3[0] * lerp3;
+                               translate[1] = translate1[1] * lerp1 + translate2[1] * lerp2 + translate3[1] * lerp3;
+                               translate[2] = translate1[2] * lerp1 + translate2[2] * lerp2 + translate3[2] * lerp3;
+                               // generate vertices
+                               for (i = 0;i < vertcount;i++)
+                               {
+                                       av[0] = verts1->v[0] * scale1[0] + verts2->v[0] * scale2[0] + verts3->v[0] * scale3[0] + translate[0];
+                                       av[1] = verts1->v[1] * scale1[1] + verts2->v[1] * scale2[1] + verts3->v[1] * scale3[1] + translate[1];
+                                       av[2] = verts1->v[2] * scale1[2] + verts2->v[2] * scale2[2] + verts3->v[2] * scale3[2] + translate[2];
+                                       n1 = m_bytenormals[verts1->lightnormalindex];
+                                       n2 = m_bytenormals[verts2->lightnormalindex];
+                                       n3 = m_bytenormals[verts3->lightnormalindex];
+                                       avn[0] = n1[0] * lerp1 + n2[0] * lerp2 + n3[0] * lerp3;
+                                       avn[1] = n1[1] * lerp1 + n2[1] * lerp2 + n3[1] * lerp3;
+                                       avn[2] = n1[2] * lerp1 + n2[2] * lerp2 + n3[2] * lerp3;
+                                       av += 4;
+                                       avn += 3;
+                                       verts1++;verts2++;verts3++;
+                               }
                        }
                }
                else
                {
+                       translate[0] = translate1[0] * lerp1 + translate2[0] * lerp2;
+                       translate[1] = translate1[1] * lerp1 + translate2[1] * lerp2;
+                       translate[2] = translate1[2] * lerp1 + translate2[2] * lerp2;
+                       // generate vertices
                        for (i = 0;i < vertcount;i++)
                        {
-                               // rotate, scale, and translate the vertex locations
-                               point[0] = verts1->v[0] * scalex1 + verts2->v[0] * scalex2 + translatex;
-                               point[1] = verts1->v[1] * scaley1 + verts2->v[1] * scaley2 + translatey;
-                               point[2] = verts1->v[2] * scalez1 + verts2->v[2] * scalez2 + translatez;
-                               av[0] = point[0] * matrix_x[0] + point[1] * matrix_y[0] + point[2] * matrix_z[0] + softwaretransform_offset[0];
-                               av[1] = point[0] * matrix_x[1] + point[1] * matrix_y[1] + point[2] * matrix_z[1] + softwaretransform_offset[1];
-                               av[2] = point[0] * matrix_x[2] + point[1] * matrix_y[2] + point[2] * matrix_z[2] + softwaretransform_offset[2];
-                               // rotate the normals
-                               point[0] = verts1->n[0] * ilerp127 + verts2->n[0] * lerp127;
-                               point[1] = verts1->n[1] * ilerp127 + verts2->n[1] * lerp127;
-                               point[2] = verts1->n[2] * ilerp127 + verts2->n[2] * lerp127;
-                               avn[0] = point[0] * softwaretransform_x[0] + point[1] * softwaretransform_y[0] + point[2] * softwaretransform_z[0];
-                               avn[1] = point[0] * softwaretransform_x[1] + point[1] * softwaretransform_y[1] + point[2] * softwaretransform_z[1];
-                               avn[2] = point[0] * softwaretransform_x[2] + point[1] * softwaretransform_y[2] + point[2] * softwaretransform_z[2];
-                               av += 3;
+                               av[0] = verts1->v[0] * scale1[0] + verts2->v[0] * scale2[0] + translate[0];
+                               av[1] = verts1->v[1] * scale1[1] + verts2->v[1] * scale2[1] + translate[1];
+                               av[2] = verts1->v[2] * scale1[2] + verts2->v[2] * scale2[2] + translate[2];
+                               n1 = m_bytenormals[verts1->lightnormalindex];
+                               n2 = m_bytenormals[verts2->lightnormalindex];
+                               avn[0] = n1[0] * lerp1 + n2[0] * lerp2;
+                               avn[1] = n1[1] * lerp1 + n2[1] * lerp2;
+                               avn[2] = n1[2] * lerp1 + n2[2] * lerp2;
+                               av += 4;
                                avn += 3;
                                verts1++;verts2++;
                        }
@@ -144,52 +162,37 @@ void R_AliasLerpVerts(int vertcount, float lerp, trivert2 *verts1, vec3_t scale1
        }
        else
        {
+               translate[0] = translate1[0] * lerp1;
+               translate[1] = translate1[1] * lerp1;
+               translate[2] = translate1[2] * lerp1;
                // generate vertices
-               if (r_modelsdonttransformnormals.value)
+               if (lerp1 != 1)
                {
-                       float *modelav = modelaliasvert;
+                       // general but almost never used case
                        for (i = 0;i < vertcount;i++)
                        {
-                               // rotate, scale, and translate the vertex locations
-                               point[0] = verts1->v[0] * scale1[0] + translate1[0];
-                               point[1] = verts1->v[1] * scale1[1] + translate1[1];
-                               point[2] = verts1->v[2] * scale1[2] + translate1[2];
-                               // save mostly un-transformed copy for lighting
-                               modelav[0] = point[0] * softwaretransform_scale;
-                               modelav[1] = point[1] * softwaretransform_scale;
-                               modelav[2] = point[2] * softwaretransform_scale;
-                               av[0] = point[0] * matrix_x[0] + point[1] * matrix_y[0] + point[2] * matrix_z[0] + softwaretransform_offset[0];
-                               av[1] = point[0] * matrix_x[1] + point[1] * matrix_y[1] + point[2] * matrix_z[1] + softwaretransform_offset[1];
-                               av[2] = point[0] * matrix_x[2] + point[1] * matrix_y[2] + point[2] * matrix_z[2] + softwaretransform_offset[2];
-                               // decompress normal but do not rotate it
-                               avn[0] = verts1->n[0] * (1.0f / 127.0f);
-                               avn[1] = verts1->n[1] * (1.0f / 127.0f);
-                               avn[2] = verts1->n[2] * (1.0f / 127.0f);
-                               modelav += 3;
-                               av += 3;
+                               av[0] = verts1->v[0] * scale1[0] + translate[0];
+                               av[1] = verts1->v[1] * scale1[1] + translate[1];
+                               av[2] = verts1->v[2] * scale1[2] + translate[2];
+                               n1 = m_bytenormals[verts1->lightnormalindex];
+                               avn[0] = n1[0] * lerp1;
+                               avn[1] = n1[1] * lerp1;
+                               avn[2] = n1[2] * lerp1;
+                               av += 4;
                                avn += 3;
                                verts1++;
                        }
                }
                else
                {
+                       // fast normal case
                        for (i = 0;i < vertcount;i++)
                        {
-                               // rotate, scale, and translate the vertex locations
-                               point[0] = verts1->v[0] * scale1[0] + translate1[0];
-                               point[1] = verts1->v[1] * scale1[1] + translate1[1];
-                               point[2] = verts1->v[2] * scale1[2] + translate1[2];
-                               av[0] = point[0] * matrix_x[0] + point[1] * matrix_y[0] + point[2] * matrix_z[0] + softwaretransform_offset[0];
-                               av[1] = point[0] * matrix_x[1] + point[1] * matrix_y[1] + point[2] * matrix_z[1] + softwaretransform_offset[1];
-                               av[2] = point[0] * matrix_x[2] + point[1] * matrix_y[2] + point[2] * matrix_z[2] + softwaretransform_offset[2];
-                               // rotate the normals
-                               point[0] = verts1->n[0] * (1.0f / 127.0f);
-                               point[1] = verts1->n[1] * (1.0f / 127.0f);
-                               point[2] = verts1->n[2] * (1.0f / 127.0f);
-                               avn[0] = point[0] * softwaretransform_x[0] + point[1] * softwaretransform_y[0] + point[2] * softwaretransform_z[0];
-                               avn[1] = point[0] * softwaretransform_x[1] + point[1] * softwaretransform_y[1] + point[2] * softwaretransform_z[1];
-                               avn[2] = point[0] * softwaretransform_x[2] + point[1] * softwaretransform_y[2] + point[2] * softwaretransform_z[2];
-                               av += 3;
+                               av[0] = verts1->v[0] * scale1[0] + translate[0];
+                               av[1] = verts1->v[1] * scale1[1] + translate[1];
+                               av[2] = verts1->v[2] * scale1[2] + translate[2];
+                               VectorCopy(m_bytenormals[verts1->lightnormalindex], avn);
+                               av += 4;
                                avn += 3;
                                verts1++;
                        }
@@ -197,559 +200,450 @@ void R_AliasLerpVerts(int vertcount, float lerp, trivert2 *verts1, vec3_t scale1
        }
 }
 
-float R_CalcAnimLerp(entity_t *ent, int pose, float lerpscale)
-{
-       if (ent->draw_lastmodel == ent->model && ent->draw_lerpstart <= cl.time)
-       {
-               if (pose != ent->draw_pose)
-               {
-                       ent->draw_lastpose = ent->draw_pose;
-                       ent->draw_pose = pose;
-                       ent->draw_lerpstart = cl.time;
-                       return 0;
-               }
-               else
-                       return ((cl.time - ent->draw_lerpstart) * lerpscale);
-       }
-       else // uninitialized
-       {
-               ent->draw_lastmodel = ent->model;
-               ent->draw_lastpose = ent->draw_pose = pose;
-               ent->draw_lerpstart = cl.time;
-               return 0;
-       }
-}
-
-void GL_DrawModelMesh(int skin, byte *colors, maliashdr_t *maliashdr)
+skinframe_t *R_FetchSkinFrame(const entity_render_t *ent)
 {
-       int i;
-       if (!r_render.value)
-               return;
-       glBindTexture(GL_TEXTURE_2D, skin);
-       if (!colors)
-       {
-               if (lighthalf)
-                       glColor3f(0.5f, 0.5f, 0.5f);
-               else
-                       glColor3f(1.0f, 1.0f, 1.0f);
-       }
-       if (gl_vertexarrays.value)
-       {
-               if (colors)
-               {
-                       qglColorPointer(4, GL_UNSIGNED_BYTE, 0, colors);
-                       glEnableClientState(GL_COLOR_ARRAY);
-               }
-
-               qglTexCoordPointer(2, GL_FLOAT, 0, (void *)((int) maliashdr->texdata + (int) maliashdr));
-               glEnableClientState(GL_TEXTURE_COORD_ARRAY);
-
-               qglDrawElements(GL_TRIANGLES, maliashdr->numtris * 3, GL_UNSIGNED_SHORT, (void *)((int) maliashdr + maliashdr->tridata));
-
-               glDisableClientState(GL_TEXTURE_COORD_ARRAY);
-
-               if (colors)
-                       glDisableClientState(GL_COLOR_ARRAY);
-       }
+       model_t *model = ent->model;
+       unsigned int s = (unsigned int) ent->skinnum;
+       if (s >= model->numskins)
+               s = 0;
+       if (model->skinscenes[s].framecount > 1)
+               return &model->skinframes[model->skinscenes[s].firstframe + (int) (cl.time * 10) % model->skinscenes[s].framecount];
        else
-       {
-               unsigned short *in, index;
-               float *tex;
-               in = (void *)((int) maliashdr + maliashdr->tridata);
-               glBegin(GL_TRIANGLES);
-               tex = (void *)((int) maliashdr + maliashdr->texdata);
-               for (i = 0;i < maliashdr->numtris * 3;i++)
-               {
-                       index = *in++;
-                       glTexCoord2f(tex[index*2], tex[index*2+1]);
-                       if (colors)
-                               glColor4f(colors[index*4] * (1.0f / 255.0f), colors[index*4+1] * (1.0f / 255.0f), colors[index*4+2] * (1.0f / 255.0f), colors[index*4+3] * (1.0f / 255.0f));
-                       glVertex3fv(&aliasvert[index*3]);
-               }
-               glEnd();
-       }
-       // leave it in a state for additional passes
-       glDepthMask(0);
-       glEnable(GL_BLEND);
-       glBlendFunc(GL_SRC_ALPHA, GL_ONE); // additive
+               return &model->skinframes[model->skinscenes[s].firstframe];
 }
 
-void R_TintModel(byte *in, byte *out, int verts, byte *color)
+void R_SetupMDLMD2Frames(const entity_render_t *ent, float colorr, float colorg, float colorb)
 {
-       int i;
-       byte r = color[0];
-       byte g = color[1];
-       byte b = color[2];
-       for (i = 0;i < verts;i++)
-       {
-               out[0] = (byte) ((in[0] * r) >> 8);
-               out[1] = (byte) ((in[1] * g) >> 8);
-               out[2] = (byte) ((in[2] * b) >> 8);
-               out[3] =          in[3];
-               in += 4;
-               out += 4;
-       }
+       const md2frame_t *frame1, *frame2, *frame3, *frame4;
+       const trivertx_t *frame1verts, *frame2verts, *frame3verts, *frame4verts;
+       const model_t *model = ent->model;
+
+       frame1 = &model->mdlmd2data_frames[ent->frameblend[0].frame];
+       frame2 = &model->mdlmd2data_frames[ent->frameblend[1].frame];
+       frame3 = &model->mdlmd2data_frames[ent->frameblend[2].frame];
+       frame4 = &model->mdlmd2data_frames[ent->frameblend[3].frame];
+       frame1verts = &model->mdlmd2data_pose[ent->frameblend[0].frame * model->numverts];
+       frame2verts = &model->mdlmd2data_pose[ent->frameblend[1].frame * model->numverts];
+       frame3verts = &model->mdlmd2data_pose[ent->frameblend[2].frame * model->numverts];
+       frame4verts = &model->mdlmd2data_pose[ent->frameblend[3].frame * model->numverts];
+       R_AliasLerpVerts(model->numverts,
+               ent->frameblend[0].lerp, frame1verts, frame1->scale, frame1->translate,
+               ent->frameblend[1].lerp, frame2verts, frame2->scale, frame2->translate,
+               ent->frameblend[2].lerp, frame3verts, frame3->scale, frame3->translate,
+               ent->frameblend[3].lerp, frame4verts, frame4->scale, frame4->translate);
+
+       R_LightModel(ent, model->numverts, colorr, colorg, colorb, false);
+
+       //R_AliasTransformVerts(model->numverts);
 }
 
-/*
-=================
-R_DrawAliasFrame
-
-=================
-*/
-extern vec3_t lightspot;
-void R_LightModel(int numverts, vec3_t center, vec3_t basecolor);
-void R_DrawAliasFrame (maliashdr_t *maliashdr, float alpha, vec3_t color, entity_t *ent, int shadow, vec3_t org, vec3_t angles, int frame, int *skin, int colormap, int effects, int flags)
+void R_DrawQ1Q2AliasModelCallback (const void *calldata1, int calldata2)
 {
-       int             i, pose;
-       float   lerpscale, lerp;
-       maliasframe_t *frameinfo;
+       int i, c, pantsfullbright, shirtfullbright, colormapped;
+       float pantscolor[3], shirtcolor[3];
+       float fog;
+       vec3_t diff;
+       qbyte *bcolor;
+       rmeshstate_t m;
+       model_t *model;
+       skinframe_t *skinframe;
+       const entity_render_t *ent = calldata1;
+       int blendfunc1, blendfunc2;
 
-       softwaretransformforentity(ent);
+//     softwaretransformforentity(ent);
+       R_Mesh_Matrix(&ent->matrix);
 
-       if ((frame >= maliashdr->numframes) || (frame < 0))
+       fog = 0;
+       if (fogenabled)
        {
-               Con_DPrintf ("R_AliasSetupFrame: no such frame %d\n", frame);
-               frame = 0;
+               VectorSubtract(ent->origin, r_origin, diff);
+               fog = DotProduct(diff,diff);
+               if (fog < 0.01f)
+                       fog = 0.01f;
+               fog = exp(fogdensity/fog);
+               if (fog > 1)
+                       fog = 1;
+               if (fog < 0.01f)
+                       fog = 0;
+               // fog method: darken, additive fog
+               // 1. render model as normal, scaled by inverse of fog alpha (darkens it)
+               // 2. render fog as additive
        }
 
-       frameinfo = ((maliasframe_t *)((int) maliashdr + maliashdr->framedata)) + frame;
-       pose = frameinfo->start;
+       model = ent->model;
+       R_Mesh_ResizeCheck(model->numverts, model->numtris);
+
+       skinframe = R_FetchSkinFrame(ent);
 
-       if (frameinfo->length > 1)
+       if (ent->effects & EF_ADDITIVE)
+       {
+               blendfunc1 = GL_SRC_ALPHA;
+               blendfunc2 = GL_ONE;
+       }
+       else if (ent->alpha != 1.0 || skinframe->fog != NULL)
        {
-               lerpscale = frameinfo->rate;
-               pose += (int)(cl.time * frameinfo->rate) % frameinfo->length;
+               blendfunc1 = GL_SRC_ALPHA;
+               blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
        }
        else
-               lerpscale = 10.0f;
-
-       lerp = R_CalcAnimLerp(ent, pose, lerpscale);
+       {
+               blendfunc1 = GL_ONE;
+               blendfunc2 = GL_ZERO;
+       }
+
+       if (!skinframe->base && !skinframe->pants && !skinframe->shirt && !skinframe->glow)
+       {
+               // untextured
+               memset(&m, 0, sizeof(m));
+               m.blendfunc1 = blendfunc1;
+               m.blendfunc2 = blendfunc2;
+               m.wantoverbright = true;
+               m.tex[0] = R_GetTexture(r_notexture);
+               R_Mesh_State(&m);
+
+               c_alias_polys += model->numtris;
+               memcpy(varray_element, model->mdlmd2data_indices, model->numtris * sizeof(int[3]));
+               for (i = 0;i < model->numverts * 2;i++)
+                       varray_texcoord[0][i] = model->mdlmd2data_texcoords[i] * 8.0f;
+               aliasvert = varray_vertex;
+               aliasvertcolor = varray_color;
+               R_SetupMDLMD2Frames(ent, mesh_colorscale, mesh_colorscale, mesh_colorscale);
+               aliasvert = aliasvertbuf;
+               aliasvertcolor = aliasvertcolorbuf;
+               R_Mesh_Draw(model->numverts, model->numtris);
+               return;
+       }
 
-       R_AliasLerpVerts(maliashdr->numverts, lerp, (trivert2 *)((int) maliashdr + maliashdr->posedata) + ent->draw_lastpose * maliashdr->numverts, maliashdr->scale, maliashdr->scale_origin, (trivert2 *)((int) maliashdr + maliashdr->posedata) + ent->draw_pose * maliashdr->numverts, maliashdr->scale, maliashdr->scale_origin);
 
-       // prep the vertex array as early as possible
-       if (r_render.value)
+       colormapped = !skinframe->merged || (ent->colormap >= 0 && skinframe->base && (skinframe->pants || skinframe->shirt));
+       if (!colormapped && !fog && !skinframe->glow && !skinframe->fog)
        {
-               if (gl_vertexarrays.value)
-               {
-                       qglVertexPointer(3, GL_FLOAT, 0, aliasvert);
-                       glEnableClientState(GL_VERTEX_ARRAY);
-               }
+               // fastpath for the normal situation (one texture)
+               memset(&m, 0, sizeof(m));
+               m.blendfunc1 = blendfunc1;
+               m.blendfunc2 = blendfunc2;
+               m.wantoverbright = true;
+               m.tex[0] = R_GetTexture(skinframe->merged);
+               R_Mesh_State(&m);
+
+               c_alias_polys += model->numtris;
+               memcpy(varray_element, model->mdlmd2data_indices, model->numtris * sizeof(int[3]));
+               memcpy(varray_texcoord[0], model->mdlmd2data_texcoords, model->numverts * sizeof(float[2]));
+               aliasvert = varray_vertex;
+               aliasvertcolor = varray_color;
+               R_SetupMDLMD2Frames(ent, mesh_colorscale, mesh_colorscale, mesh_colorscale);
+               aliasvert = aliasvertbuf;
+               aliasvertcolor = aliasvertcolorbuf;
+               R_Mesh_Draw(model->numverts, model->numtris);
+               return;
        }
 
-       R_LightModel(maliashdr->numverts, org, color);
+       R_SetupMDLMD2Frames(ent, 1 - fog, 1 - fog, 1 - fog);
 
-       if (!r_render.value)
-               return;
-       glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
-       glShadeModel(GL_SMOOTH);
-       if (effects & EF_ADDITIVE)
+       if (colormapped)
        {
-               glBlendFunc(GL_SRC_ALPHA, GL_ONE); // additive rendering
-               glEnable(GL_BLEND);
-               glDepthMask(0);
+               // 128-224 are backwards ranges
+               c = (ent->colormap & 0xF) << 4;c += (c >= 128 && c < 224) ? 4 : 12;
+               bcolor = (qbyte *) (&d_8to24table[c]);
+               pantsfullbright = c >= 224;
+               VectorScale(bcolor, (1.0f / 255.0f), pantscolor);
+               c = (ent->colormap & 0xF0);c += (c >= 128 && c < 224) ? 4 : 12;
+               bcolor = (qbyte *) (&d_8to24table[c]);
+               shirtfullbright = c >= 224;
+               VectorScale(bcolor, (1.0f / 255.0f), shirtcolor);
        }
-       else if (alpha != 1.0)
+       else
        {
-               glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-               glEnable(GL_BLEND);
-               glDepthMask(0);
+               pantscolor[0] = pantscolor[1] = pantscolor[2] = shirtcolor[0] = shirtcolor[1] = shirtcolor[2] = 1;
+               pantsfullbright = shirtfullbright = false;
        }
-       else
+
+       memset(&m, 0, sizeof(m));
+       m.blendfunc1 = blendfunc1;
+       m.blendfunc2 = blendfunc2;
+       m.wantoverbright = true;
+       m.tex[0] = colormapped ? R_GetTexture(skinframe->base) : R_GetTexture(skinframe->merged);
+       if (m.tex[0])
        {
-               glDisable(GL_BLEND);
-               glDepthMask(1);
+               R_Mesh_State(&m);
+
+               blendfunc1 = GL_SRC_ALPHA;
+               blendfunc2 = GL_ONE;
+               c_alias_polys += model->numtris;
+               R_ModulateColors(aliasvertcolor, varray_color, model->numverts, mesh_colorscale, mesh_colorscale, mesh_colorscale);
+               memcpy(varray_element, model->mdlmd2data_indices, model->numtris * sizeof(int[3]));
+               memcpy(varray_vertex, aliasvert, model->numverts * sizeof(float[4]));
+               memcpy(varray_texcoord[0], model->mdlmd2data_texcoords, model->numverts * sizeof(float[2]));
+               R_Mesh_Draw(model->numverts, model->numtris);
        }
 
-       if (skin[0] || skin[1] || skin[2] || skin[3] || skin[4])
+       if (colormapped)
        {
-               if (colormap >= 0 && (skin[0] || skin[1] || skin[2]))
+               if (skinframe->pants)
                {
-                       int c;
-                       if (skin[0])
-                               GL_DrawModelMesh(skin[0], aliasvertcolor, maliashdr);
-                       if (skin[1])
+                       memset(&m, 0, sizeof(m));
+                       m.blendfunc1 = blendfunc1;
+                       m.blendfunc2 = blendfunc2;
+                       m.wantoverbright = true;
+                       m.tex[0] = R_GetTexture(skinframe->pants);
+                       if (m.tex[0])
                        {
-                               c = (colormap & 0xF) << 4;c += (c >= 128 && c < 224) ? 4 : 12; // 128-224 are backwards ranges
-                               R_TintModel(aliasvertcolor, aliasvertcolor2, maliashdr->numverts, (byte *) (&d_8to24table[c]));
-                               GL_DrawModelMesh(skin[1], aliasvertcolor2, maliashdr);
-                       }
-                       if (skin[2])
-                       {
-                               c = colormap & 0xF0      ;c += (c >= 128 && c < 224) ? 4 : 12; // 128-224 are backwards ranges
-                               R_TintModel(aliasvertcolor, aliasvertcolor2, maliashdr->numverts, (byte *) (&d_8to24table[c]));
-                               GL_DrawModelMesh(skin[2], aliasvertcolor2, maliashdr);
+                               R_Mesh_State(&m);
+
+                               blendfunc1 = GL_SRC_ALPHA;
+                               blendfunc2 = GL_ONE;
+                               c_alias_polys += model->numtris;
+                               if (pantsfullbright)
+                                       R_FillColors(varray_color, model->numverts, pantscolor[0] * mesh_colorscale, pantscolor[1] * mesh_colorscale, pantscolor[2] * mesh_colorscale, ent->alpha);
+                               else
+                                       R_ModulateColors(aliasvertcolor, varray_color, model->numverts, pantscolor[0] * mesh_colorscale, pantscolor[1] * mesh_colorscale, pantscolor[2] * mesh_colorscale);
+                               memcpy(varray_element, model->mdlmd2data_indices, model->numtris * sizeof(int[3]));
+                               memcpy(varray_vertex, aliasvert, model->numverts * sizeof(float[4]));
+                               memcpy(varray_texcoord[0], model->mdlmd2data_texcoords, model->numverts * sizeof(float[2]));
+                               R_Mesh_Draw(model->numverts, model->numtris);
                        }
                }
-               else
+               if (skinframe->shirt)
                {
-                       if (skin[4]) GL_DrawModelMesh(skin[4], aliasvertcolor, maliashdr);
-                       else
+                       memset(&m, 0, sizeof(m));
+                       m.blendfunc1 = blendfunc1;
+                       m.blendfunc2 = blendfunc2;
+                       m.wantoverbright = true;
+                       m.tex[0] = R_GetTexture(skinframe->shirt);
+                       if (m.tex[0])
                        {
-                               if (skin[0]) GL_DrawModelMesh(skin[0], aliasvertcolor, maliashdr);
-                               if (skin[1]) GL_DrawModelMesh(skin[1], aliasvertcolor, maliashdr);
-                               if (skin[2]) GL_DrawModelMesh(skin[2], aliasvertcolor, maliashdr);
+                               R_Mesh_State(&m);
+
+                               blendfunc1 = GL_SRC_ALPHA;
+                               blendfunc2 = GL_ONE;
+                               c_alias_polys += model->numtris;
+                               if (shirtfullbright)
+                                       R_FillColors(varray_color, model->numverts, shirtcolor[0] * mesh_colorscale, shirtcolor[1] * mesh_colorscale, shirtcolor[2] * mesh_colorscale, ent->alpha);
+                               else
+                                       R_ModulateColors(aliasvertcolor, varray_color, model->numverts, shirtcolor[0] * mesh_colorscale, shirtcolor[1] * mesh_colorscale, shirtcolor[2] * mesh_colorscale);
+                               memcpy(varray_element, model->mdlmd2data_indices, model->numtris * sizeof(int[3]));
+                               memcpy(varray_vertex, aliasvert, model->numverts * sizeof(float[4]));
+                               memcpy(varray_texcoord[0], model->mdlmd2data_texcoords, model->numverts * sizeof(float[2]));
+                               R_Mesh_Draw(model->numverts, model->numtris);
                        }
                }
-               if (skin[3]) GL_DrawModelMesh(skin[3], NULL, maliashdr);
        }
-       else
-               GL_DrawModelMesh(0, NULL, maliashdr);
-
-       if (fogenabled)
+       if (skinframe->glow)
        {
-               vec3_t diff;
-               glDisable (GL_TEXTURE_2D);
-               glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-               glEnable (GL_BLEND);
-               glDepthMask(0); // disable zbuffer updates
-
-               VectorSubtract(org, r_refdef.vieworg, diff);
-               glColor4f(fogcolor[0], fogcolor[1], fogcolor[2], exp(fogdensity/DotProduct(diff,diff)));
-
-               if (gl_vertexarrays.value)
-               {
-                       qglDrawElements(GL_TRIANGLES, maliashdr->numtris * 3, GL_UNSIGNED_SHORT, (void *)((int) maliashdr + maliashdr->tridata));
-               }
-               else
+               memset(&m, 0, sizeof(m));
+               m.blendfunc1 = blendfunc1;
+               m.blendfunc2 = blendfunc2;
+               m.wantoverbright = true;
+               m.tex[0] = R_GetTexture(skinframe->glow);
+               if (m.tex[0])
                {
-                       unsigned short *in;
-                       in = (void *)((int) maliashdr + maliashdr->tridata);
-                       glBegin(GL_TRIANGLES);
-                       for (i = 0;i < maliashdr->numtris * 3;i++)
-                               glVertex3fv(&aliasvert[*in++ * 3]);
-                       glEnd();
-               }
+                       R_Mesh_State(&m);
 
-               glEnable (GL_TEXTURE_2D);
-               glColor3f (1,1,1);
+                       blendfunc1 = GL_SRC_ALPHA;
+                       blendfunc2 = GL_ONE;
+                       c_alias_polys += model->numtris;
+                       R_FillColors(varray_color, model->numverts, (1 - fog) * mesh_colorscale, (1 - fog) * mesh_colorscale, (1 - fog) * mesh_colorscale, ent->alpha);
+                       memcpy(varray_element, model->mdlmd2data_indices, model->numtris * sizeof(int[3]));
+                       memcpy(varray_vertex, aliasvert, model->numverts * sizeof(float[4]));
+                       memcpy(varray_texcoord[0], model->mdlmd2data_texcoords, model->numverts * sizeof(float[2]));
+                       R_Mesh_Draw(model->numverts, model->numtris);
+               }
        }
-       if (gl_vertexarrays.value)
-               glDisableClientState(GL_VERTEX_ARRAY);
-
-       if (!fogenabled && r_shadows.value && !(effects & EF_ADDITIVE) && shadow)
+       if (fog)
        {
-               // flatten it to make a shadow
-               float *av = aliasvert + 2, l = lightspot[2] + 0.125;
-               av = aliasvert + 2;
-               for (i = 0;i < maliashdr->numverts;i++, av+=3)
-                       if (*av > l)
-                               *av = l;
-               glDisable (GL_TEXTURE_2D);
-               glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-               glEnable (GL_BLEND);
-               glDepthMask(0); // disable zbuffer updates
-               glColor4f (0,0,0,0.5 * alpha);
+               memset(&m, 0, sizeof(m));
+               m.blendfunc1 = GL_SRC_ALPHA;
+               m.blendfunc2 = GL_ONE;
+               m.wantoverbright = false;
+               m.tex[0] = R_GetTexture(skinframe->fog);
+               R_Mesh_State(&m);
 
-               if (gl_vertexarrays.value)
-               {
-                       qglVertexPointer(3, GL_FLOAT, 0, aliasvert);
-                       glEnableClientState(GL_VERTEX_ARRAY);
-                       qglDrawElements(GL_TRIANGLES, maliashdr->numtris * 3, GL_UNSIGNED_SHORT, (void *)((int) maliashdr + maliashdr->tridata));
-                       glDisableClientState(GL_VERTEX_ARRAY);
-               }
-               else
-               {
-                       unsigned short *in;
-                       in = (void *)((int) maliashdr + maliashdr->tridata);
-                       glBegin(GL_TRIANGLES);
-                       for (i = 0;i < maliashdr->numtris * 3;i++)
-                               glVertex3fv(&aliasvert[*in++ * 3]);
-                       glEnd();
-               }
-
-               glEnable (GL_TEXTURE_2D);
-               glColor3f (1,1,1);
+               c_alias_polys += model->numtris;
+               R_FillColors(varray_color, model->numverts, fogcolor[0] * fog * mesh_colorscale, fogcolor[1] * fog * mesh_colorscale, fogcolor[2] * fog * mesh_colorscale, ent->alpha);
+               memcpy(varray_element, model->mdlmd2data_indices, model->numtris * sizeof(int[3]));
+               memcpy(varray_vertex, aliasvert, model->numverts * sizeof(float[4]));
+               memcpy(varray_texcoord[0], model->mdlmd2data_texcoords, model->numverts * sizeof(float[2]));
+               R_Mesh_Draw(model->numverts, model->numtris);
        }
-
-       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-       glEnable (GL_BLEND);
-       glDepthMask(1);
 }
 
-/*
-=================
-R_DrawQ2AliasFrame
-
-=================
-*/
-void R_DrawQ2AliasFrame (md2mem_t *pheader, float alpha, vec3_t color, entity_t *ent, int shadow, vec3_t org, vec3_t angles, int frame, int skin, int effects, int flags)
+int ZymoticLerpBones(int count, const zymbonematrix *bonebase, const frameblend_t *blend, const zymbone_t *bone)
 {
-       int *order, count;
-       float lerp;
-       md2memframe_t *frame1, *frame2;
-
-       if (r_render.value)
-               glBindTexture(GL_TEXTURE_2D, skin);
-
-       softwaretransformforentity(ent);
-
-       if ((frame >= pheader->num_frames) || (frame < 0))
-       {
-               Con_DPrintf ("R_SetupQ2AliasFrame: no such frame %d\n", frame);
-               frame = 0;
-       }
-
-       lerp = R_CalcAnimLerp(ent, frame, 10);
-
-       frame1 = (void *)((int) pheader + pheader->ofs_frames + (pheader->framesize * ent->draw_lastpose));
-       frame2 = (void *)((int) pheader + pheader->ofs_frames + (pheader->framesize * ent->draw_pose));
-       R_AliasLerpVerts(pheader->num_xyz, lerp, frame1->verts, frame1->scale, frame1->translate, frame2->verts, frame2->scale, frame2->translate);
-
-       R_LightModel(pheader->num_xyz, org, color);
-
-       if (!r_render.value)
-               return;
-       if (gl_vertexarrays.value)
-       {
-               // LordHavoc: big mess...
-               // using arrays only slightly, although it is enough to prevent duplicates
-               // (saving half the transforms)
-               qglVertexPointer(3, GL_FLOAT, 0, aliasvert);
-               qglColorPointer(4, GL_UNSIGNED_BYTE, 0, aliasvertcolor);
-               glEnableClientState(GL_VERTEX_ARRAY);
-               glEnableClientState(GL_COLOR_ARRAY);
-
-               order = (int *)((int)pheader + pheader->ofs_glcmds);
-               while(1)
-               {
-                       if (!(count = *order++))
-                               break;
-                       if (count > 0)
-                               glBegin(GL_TRIANGLE_STRIP);
-                       else
-                       {
-                               glBegin(GL_TRIANGLE_FAN);
-                               count = -count;
-                       }
-                       do
+       int i;
+       float lerp1, lerp2, lerp3, lerp4;
+       zymbonematrix *out, rootmatrix, m;
+       const zymbonematrix *bone1, *bone2, *bone3, *bone4;
+
+       /*
+       // LordHavoc: combine transform from zym coordinate space to quake coordinate space with model to world transform matrix
+       rootmatrix.m[0][0] = softwaretransform_matrix[0][1];
+       rootmatrix.m[0][1] = -softwaretransform_matrix[0][0];
+       rootmatrix.m[0][2] = softwaretransform_matrix[0][2];
+       rootmatrix.m[0][3] = softwaretransform_matrix[0][3];
+       rootmatrix.m[1][0] = softwaretransform_matrix[1][1];
+       rootmatrix.m[1][1] = -softwaretransform_matrix[1][0];
+       rootmatrix.m[1][2] = softwaretransform_matrix[1][2];
+       rootmatrix.m[1][3] = softwaretransform_matrix[1][3];
+       rootmatrix.m[2][0] = softwaretransform_matrix[2][1];
+       rootmatrix.m[2][1] = -softwaretransform_matrix[2][0];
+       rootmatrix.m[2][2] = softwaretransform_matrix[2][2];
+       rootmatrix.m[2][3] = softwaretransform_matrix[2][3];
+       */
+       rootmatrix.m[0][0] = 1;
+       rootmatrix.m[0][1] = 0;
+       rootmatrix.m[0][2] = 0;
+       rootmatrix.m[0][3] = 0;
+       rootmatrix.m[1][0] = 0;
+       rootmatrix.m[1][1] = 1;
+       rootmatrix.m[1][2] = 0;
+       rootmatrix.m[1][3] = 0;
+       rootmatrix.m[2][0] = 0;
+       rootmatrix.m[2][1] = 0;
+       rootmatrix.m[2][2] = 1;
+       rootmatrix.m[2][3] = 0;
+
+       bone1 = bonebase + blend[0].frame * count;
+       lerp1 = blend[0].lerp;
+       if (blend[1].lerp)
+       {
+               bone2 = bonebase + blend[1].frame * count;
+               lerp2 = blend[1].lerp;
+               if (blend[2].lerp)
+               {
+                       bone3 = bonebase + blend[2].frame * count;
+                       lerp3 = blend[2].lerp;
+                       if (blend[3].lerp)
                        {
-                               glTexCoord2f(((float *)order)[0], ((float *)order)[1]);
-                               qglArrayElement(order[2]);
-                               order += 3;
+                               // 4 poses
+                               bone4 = bonebase + blend[3].frame * count;
+                               lerp4 = blend[3].lerp;
+                               for (i = 0, out = zymbonepose;i < count;i++, out++)
+                               {
+                                       // interpolate matrices
+                                       m.m[0][0] = bone1->m[0][0] * lerp1 + bone2->m[0][0] * lerp2 + bone3->m[0][0] * lerp3 + bone4->m[0][0] * lerp4;
+                                       m.m[0][1] = bone1->m[0][1] * lerp1 + bone2->m[0][1] * lerp2 + bone3->m[0][1] * lerp3 + bone4->m[0][1] * lerp4;
+                                       m.m[0][2] = bone1->m[0][2] * lerp1 + bone2->m[0][2] * lerp2 + bone3->m[0][2] * lerp3 + bone4->m[0][2] * lerp4;
+                                       m.m[0][3] = bone1->m[0][3] * lerp1 + bone2->m[0][3] * lerp2 + bone3->m[0][3] * lerp3 + bone4->m[0][3] * lerp4;
+                                       m.m[1][0] = bone1->m[1][0] * lerp1 + bone2->m[1][0] * lerp2 + bone3->m[1][0] * lerp3 + bone4->m[1][0] * lerp4;
+                                       m.m[1][1] = bone1->m[1][1] * lerp1 + bone2->m[1][1] * lerp2 + bone3->m[1][1] * lerp3 + bone4->m[1][1] * lerp4;
+                                       m.m[1][2] = bone1->m[1][2] * lerp1 + bone2->m[1][2] * lerp2 + bone3->m[1][2] * lerp3 + bone4->m[1][2] * lerp4;
+                                       m.m[1][3] = bone1->m[1][3] * lerp1 + bone2->m[1][3] * lerp2 + bone3->m[1][3] * lerp3 + bone4->m[1][3] * lerp4;
+                                       m.m[2][0] = bone1->m[2][0] * lerp1 + bone2->m[2][0] * lerp2 + bone3->m[2][0] * lerp3 + bone4->m[2][0] * lerp4;
+                                       m.m[2][1] = bone1->m[2][1] * lerp1 + bone2->m[2][1] * lerp2 + bone3->m[2][1] * lerp3 + bone4->m[2][1] * lerp4;
+                                       m.m[2][2] = bone1->m[2][2] * lerp1 + bone2->m[2][2] * lerp2 + bone3->m[2][2] * lerp3 + bone4->m[2][2] * lerp4;
+                                       m.m[2][3] = bone1->m[2][3] * lerp1 + bone2->m[2][3] * lerp2 + bone3->m[2][3] * lerp3 + bone4->m[2][3] * lerp4;
+                                       if (bone->parent >= 0)
+                                               R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &m.m[0][0], &out->m[0][0]);
+                                       else
+                                               R_ConcatTransforms(&rootmatrix.m[0][0], &m.m[0][0], &out->m[0][0]);
+                                       bone1++;
+                                       bone2++;
+                                       bone3++;
+                                       bone4++;
+                                       bone++;
+                               }
                        }
-                       while (count--);
-               }
-
-               glDisableClientState(GL_COLOR_ARRAY);
-               glDisableClientState(GL_VERTEX_ARRAY);
-       }
-       else
-       {
-               order = (int *)((int)pheader + pheader->ofs_glcmds);
-               while(1)
-               {
-                       if (!(count = *order++))
-                               break;
-                       if (count > 0)
-                               glBegin(GL_TRIANGLE_STRIP);
                        else
                        {
-                               glBegin(GL_TRIANGLE_FAN);
-                               count = -count;
-                       }
-                       do
-                       {
-                               glTexCoord2f(((float *)order)[0], ((float *)order)[1]);
-                               glColor4f(aliasvertcolor[order[2] * 4] * (1.0f / 255.0f), aliasvertcolor[order[2] * 4 + 1] * (1.0f / 255.0f), aliasvertcolor[order[2] * 4 + 2] * (1.0f / 255.0f), aliasvertcolor[order[2] * 4 + 3] * (1.0f / 255.0f));
-                               glVertex3fv(&aliasvert[order[2] * 3]);
-                               order += 3;
-                       }
-                       while (count--);
-               }
-       }
-
-       if (fogenabled)
-       {
-               glDisable (GL_TEXTURE_2D);
-               glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-               glEnable (GL_BLEND);
-               glDepthMask(0); // disable zbuffer updates
-               {
-                       vec3_t diff;
-                       VectorSubtract(org, r_refdef.vieworg, diff);
-                       glColor4f(fogcolor[0], fogcolor[1], fogcolor[2], exp(fogdensity/DotProduct(diff,diff)));
-               }
-
-               if (gl_vertexarrays.value)
-               {
-                       // LordHavoc: big mess...
-                       // using arrays only slightly, although it is enough to prevent duplicates
-                       // (saving half the transforms)
-                       qglVertexPointer(3, GL_FLOAT, 0, aliasvert);
-                       glEnableClientState(GL_VERTEX_ARRAY);
-
-                       order = (int *)((int)pheader + pheader->ofs_glcmds);
-                       while(1)
-                       {
-                               if (!(count = *order++))
-                                       break;
-                               if (count > 0)
-                                       glBegin(GL_TRIANGLE_STRIP);
-                               else
-                               {
-                                       glBegin(GL_TRIANGLE_FAN);
-                                       count = -count;
-                               }
-                               do
+                               // 3 poses
+                               for (i = 0, out = zymbonepose;i < count;i++, out++)
                                {
-                                       qglArrayElement(order[2]);
-                                       order += 3;
+                                       // interpolate matrices
+                                       m.m[0][0] = bone1->m[0][0] * lerp1 + bone2->m[0][0] * lerp2 + bone3->m[0][0] * lerp3;
+                                       m.m[0][1] = bone1->m[0][1] * lerp1 + bone2->m[0][1] * lerp2 + bone3->m[0][1] * lerp3;
+                                       m.m[0][2] = bone1->m[0][2] * lerp1 + bone2->m[0][2] * lerp2 + bone3->m[0][2] * lerp3;
+                                       m.m[0][3] = bone1->m[0][3] * lerp1 + bone2->m[0][3] * lerp2 + bone3->m[0][3] * lerp3;
+                                       m.m[1][0] = bone1->m[1][0] * lerp1 + bone2->m[1][0] * lerp2 + bone3->m[1][0] * lerp3;
+                                       m.m[1][1] = bone1->m[1][1] * lerp1 + bone2->m[1][1] * lerp2 + bone3->m[1][1] * lerp3;
+                                       m.m[1][2] = bone1->m[1][2] * lerp1 + bone2->m[1][2] * lerp2 + bone3->m[1][2] * lerp3;
+                                       m.m[1][3] = bone1->m[1][3] * lerp1 + bone2->m[1][3] * lerp2 + bone3->m[1][3] * lerp3;
+                                       m.m[2][0] = bone1->m[2][0] * lerp1 + bone2->m[2][0] * lerp2 + bone3->m[2][0] * lerp3;
+                                       m.m[2][1] = bone1->m[2][1] * lerp1 + bone2->m[2][1] * lerp2 + bone3->m[2][1] * lerp3;
+                                       m.m[2][2] = bone1->m[2][2] * lerp1 + bone2->m[2][2] * lerp2 + bone3->m[2][2] * lerp3;
+                                       m.m[2][3] = bone1->m[2][3] * lerp1 + bone2->m[2][3] * lerp2 + bone3->m[2][3] * lerp3;
+                                       if (bone->parent >= 0)
+                                               R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &m.m[0][0], &out->m[0][0]);
+                                       else
+                                               R_ConcatTransforms(&rootmatrix.m[0][0], &m.m[0][0], &out->m[0][0]);
+                                       bone1++;
+                                       bone2++;
+                                       bone3++;
+                                       bone++;
                                }
-                               while (count--);
                        }
-
-                       glDisableClientState(GL_VERTEX_ARRAY);
                }
                else
                {
-                       order = (int *)((int)pheader + pheader->ofs_glcmds);
-                       while(1)
+                       // 2 poses
+                       for (i = 0, out = zymbonepose;i < count;i++, out++)
                        {
-                               if (!(count = *order++))
-                                       break;
-                               if (count > 0)
-                                       glBegin(GL_TRIANGLE_STRIP);
+                               // interpolate matrices
+                               m.m[0][0] = bone1->m[0][0] * lerp1 + bone2->m[0][0] * lerp2;
+                               m.m[0][1] = bone1->m[0][1] * lerp1 + bone2->m[0][1] * lerp2;
+                               m.m[0][2] = bone1->m[0][2] * lerp1 + bone2->m[0][2] * lerp2;
+                               m.m[0][3] = bone1->m[0][3] * lerp1 + bone2->m[0][3] * lerp2;
+                               m.m[1][0] = bone1->m[1][0] * lerp1 + bone2->m[1][0] * lerp2;
+                               m.m[1][1] = bone1->m[1][1] * lerp1 + bone2->m[1][1] * lerp2;
+                               m.m[1][2] = bone1->m[1][2] * lerp1 + bone2->m[1][2] * lerp2;
+                               m.m[1][3] = bone1->m[1][3] * lerp1 + bone2->m[1][3] * lerp2;
+                               m.m[2][0] = bone1->m[2][0] * lerp1 + bone2->m[2][0] * lerp2;
+                               m.m[2][1] = bone1->m[2][1] * lerp1 + bone2->m[2][1] * lerp2;
+                               m.m[2][2] = bone1->m[2][2] * lerp1 + bone2->m[2][2] * lerp2;
+                               m.m[2][3] = bone1->m[2][3] * lerp1 + bone2->m[2][3] * lerp2;
+                               if (bone->parent >= 0)
+                                       R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &m.m[0][0], &out->m[0][0]);
                                else
-                               {
-                                       glBegin(GL_TRIANGLE_FAN);
-                                       count = -count;
-                               }
-                               do
-                               {
-                                       glVertex3fv(&aliasvert[order[2] * 3]);
-                                       order += 3;
-                               }
-                               while (count--);
+                                       R_ConcatTransforms(&rootmatrix.m[0][0], &m.m[0][0], &out->m[0][0]);
+                               bone1++;
+                               bone2++;
+                               bone++;
                        }
                }
-
-               glEnable (GL_TEXTURE_2D);
-               glColor3f (1,1,1);
        }
-
-       if (!fogenabled && r_shadows.value && !(effects & EF_ADDITIVE) && shadow)
+       else
        {
-               int i;
-               float *av = aliasvert + 2, l = lightspot[2] + 0.125;
-               av = aliasvert + 2;
-               for (i = 0;i < pheader->num_xyz;i++, av+=3)
-                       if (*av > l)
-                               *av = l;
-               glDisable (GL_TEXTURE_2D);
-               glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-               glEnable (GL_BLEND);
-               glDepthMask(0); // disable zbuffer updates
-               glColor4f (0,0,0,0.5 * alpha);
-
-               if (gl_vertexarrays.value)
+               // 1 pose
+               if (lerp1 != 1)
                {
-                       qglVertexPointer(3, GL_FLOAT, 0, aliasvert);
-                       glEnableClientState(GL_VERTEX_ARRAY);
-                                               
-                       while(1)
+                       // lerp != 1.0
+                       for (i = 0, out = zymbonepose;i < count;i++, out++)
                        {
-                               if (!(count = *order++))
-                                       break;
-                               if (count > 0)
-                                       glBegin(GL_TRIANGLE_STRIP);
+                               // interpolate matrices
+                               m.m[0][0] = bone1->m[0][0] * lerp1;
+                               m.m[0][1] = bone1->m[0][1] * lerp1;
+                               m.m[0][2] = bone1->m[0][2] * lerp1;
+                               m.m[0][3] = bone1->m[0][3] * lerp1;
+                               m.m[1][0] = bone1->m[1][0] * lerp1;
+                               m.m[1][1] = bone1->m[1][1] * lerp1;
+                               m.m[1][2] = bone1->m[1][2] * lerp1;
+                               m.m[1][3] = bone1->m[1][3] * lerp1;
+                               m.m[2][0] = bone1->m[2][0] * lerp1;
+                               m.m[2][1] = bone1->m[2][1] * lerp1;
+                               m.m[2][2] = bone1->m[2][2] * lerp1;
+                               m.m[2][3] = bone1->m[2][3] * lerp1;
+                               if (bone->parent >= 0)
+                                       R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &m.m[0][0], &out->m[0][0]);
                                else
-                               {
-                                       glBegin(GL_TRIANGLE_FAN);
-                                       count = -count;
-                               }
-                               do
-                               {
-                                       qglArrayElement(order[2]);
-                                       order += 3;
-                               }
-                               while (count--);
+                                       R_ConcatTransforms(&rootmatrix.m[0][0], &m.m[0][0], &out->m[0][0]);
+                               bone1++;
+                               bone++;
                        }
-
-                       glDisableClientState(GL_VERTEX_ARRAY);
                }
                else
                {
-                       while(1)
+                       // lerp == 1.0
+                       for (i = 0, out = zymbonepose;i < count;i++, out++)
                        {
-                               if (!(count = *order++))
-                                       break;
-                               if (count > 0)
-                                       glBegin(GL_TRIANGLE_STRIP);
+                               if (bone->parent >= 0)
+                                       R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &bone1->m[0][0], &out->m[0][0]);
                                else
-                               {
-                                       glBegin(GL_TRIANGLE_FAN);
-                                       count = -count;
-                               }
-                               do
-                               {
-                                       glVertex3fv(&aliasvert[order[2] * 3]);
-                                       order += 3;
-                               }
-                               while (count--);
+                                       R_ConcatTransforms(&rootmatrix.m[0][0], &bone1->m[0][0], &out->m[0][0]);
+                               bone1++;
+                               bone++;
                        }
                }
-
-               glEnable (GL_TEXTURE_2D);
-               glColor3f (1,1,1);
-       }
-
-       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-       glEnable (GL_BLEND);
-       glDepthMask(1);
-}
-
-void ZymoticLerpBones(int count, float lerp2, zymbonematrix *bone1, zymbonematrix *bone2, zymbone_t *bone, float rootorigin[3], float rootangles[3])
-{
-       float lerp1;
-       zymbonematrix *out, rootmatrix, m;
-       lerp1 = 1 - lerp2;
-       out = zymbonepose;
-       AngleVectors(rootangles, rootmatrix.m[0], rootmatrix.m[1], rootmatrix.m[2]);
-       rootmatrix.m[0][3] = rootorigin[0];
-       rootmatrix.m[1][3] = rootorigin[1];
-       rootmatrix.m[2][3] = rootorigin[2];
-       if (lerp1 != 1) // interpolation
-       {
-               while(count--)
-               {
-                       // interpolate matrices
-                       m.m[0][0] = bone1->m[0][0] * lerp1 + bone2->m[0][0] * lerp2;
-                       m.m[0][1] = bone1->m[0][1] * lerp1 + bone2->m[0][1] * lerp2;
-                       m.m[0][2] = bone1->m[0][2] * lerp1 + bone2->m[0][2] * lerp2;
-                       m.m[0][3] = bone1->m[0][3] * lerp1 + bone2->m[0][3] * lerp2;
-                       m.m[1][0] = bone1->m[1][0] * lerp1 + bone2->m[1][0] * lerp2;
-                       m.m[1][1] = bone1->m[1][1] * lerp1 + bone2->m[1][1] * lerp2;
-                       m.m[1][2] = bone1->m[1][2] * lerp1 + bone2->m[1][2] * lerp2;
-                       m.m[1][3] = bone1->m[1][3] * lerp1 + bone2->m[1][3] * lerp2;
-                       m.m[2][0] = bone1->m[2][0] * lerp1 + bone2->m[2][0] * lerp2;
-                       m.m[2][1] = bone1->m[2][1] * lerp1 + bone2->m[2][1] * lerp2;
-                       m.m[2][2] = bone1->m[2][2] * lerp1 + bone2->m[2][2] * lerp2;
-                       m.m[2][3] = bone1->m[2][3] * lerp1 + bone2->m[2][3] * lerp2;
-                       if (bone->parent >= 0)
-                               R_ConcatTransforms(&zymbonepose[bone->parent].m[0], &m.m[0], &out->m[0]);
-                       else
-                               R_ConcatTransforms(&rootmatrix.m[0], &m.m[0], &out->m[0]);
-                       bone1++;
-                       bone2++;
-                       bone++;
-                       out++;
-               }
-       }
-       else // no interpolation
-       {
-               while(count--)
-               {
-                       if (bone->parent >= 0)
-                               R_ConcatTransforms(&zymbonepose[bone->parent].m[0], &bone1->m[0], &out->m[0]);
-                       else
-                               R_ConcatTransforms(&rootmatrix.m[0], &bone1->m[0], &out->m[0]);
-                       bone1++;
-                       bone++;
-                       out++;
-               }
        }
+       return true;
 }
 
 void ZymoticTransformVerts(int vertcount, int *bonecounts, zymvertex_t *vert)
@@ -760,6 +654,8 @@ void ZymoticTransformVerts(int vertcount, int *bonecounts, zymvertex_t *vert)
        while(vertcount--)
        {
                c = *bonecounts++;
+               // FIXME: validate bonecounts at load time (must be >= 1)
+               // FIXME: need 4th component in origin, for how much of the translate to blend in
                if (c == 1)
                {
                        matrix = &zymbonepose[vert->bonenum];
@@ -780,25 +676,17 @@ void ZymoticTransformVerts(int vertcount, int *bonecounts, zymvertex_t *vert)
                                vert++;
                        }
                }
-               out += 3;
+               out += 4;
        }
 }
 
-float ixtable[4096];
-
 void ZymoticCalcNormals(int vertcount, int shadercount, int *renderlist)
 {
        int a, b, c, d;
-       float *out, v1[3], v2[3], normal[3];
+       float *out, v1[3], v2[3], normal[3], s;
        int *u;
-       if (!ixtable[1])
-       {
-               ixtable[0] = 0;
-               for (a = 1;a < 4096;a++)
-                       ixtable[a] = 1.0f / a;
-       }
        // clear normals
-       memset(aliasvertnorm, 0, sizeof(float[3]) * vertcount);
+       memset(aliasvertnorm, 0, sizeof(float) * vertcount * 3);
        memset(aliasvertusage, 0, sizeof(int) * vertcount);
        // parse render list and accumulate surface normals
        while(shadercount--)
@@ -806,9 +694,9 @@ void ZymoticCalcNormals(int vertcount, int shadercount, int *renderlist)
                d = *renderlist++;
                while (d--)
                {
-                       a = renderlist[0]*3;
-                       b = renderlist[1]*3;
-                       c = renderlist[2]*3;
+                       a = renderlist[0]*4;
+                       b = renderlist[1]*4;
+                       c = renderlist[2]*4;
                        v1[0] = aliasvert[a+0] - aliasvert[b+0];
                        v1[1] = aliasvert[a+1] - aliasvert[b+1];
                        v1[2] = aliasvert[a+2] - aliasvert[b+2];
@@ -816,23 +704,27 @@ void ZymoticCalcNormals(int vertcount, int shadercount, int *renderlist)
                        v2[1] = aliasvert[c+1] - aliasvert[b+1];
                        v2[2] = aliasvert[c+2] - aliasvert[b+2];
                        CrossProduct(v1, v2, normal);
-                       VectorNormalize(normal);
+                       VectorNormalizeFast(normal);
                        // add surface normal to vertices
+                       a = renderlist[0] * 3;
                        aliasvertnorm[a+0] += normal[0];
                        aliasvertnorm[a+1] += normal[1];
                        aliasvertnorm[a+2] += normal[2];
-                       aliasvertusage[a]++;
-                       aliasvertnorm[b+0] += normal[0];
-                       aliasvertnorm[b+1] += normal[1];
-                       aliasvertnorm[b+2] += normal[2];
-                       aliasvertusage[b]++;
-                       aliasvertnorm[c+0] += normal[0];
-                       aliasvertnorm[c+1] += normal[1];
-                       aliasvertnorm[c+2] += normal[2];
-                       aliasvertusage[c]++;
+                       aliasvertusage[renderlist[0]]++;
+                       a = renderlist[1] * 3;
+                       aliasvertnorm[a+0] += normal[0];
+                       aliasvertnorm[a+1] += normal[1];
+                       aliasvertnorm[a+2] += normal[2];
+                       aliasvertusage[renderlist[1]]++;
+                       a = renderlist[2] * 3;
+                       aliasvertnorm[a+0] += normal[0];
+                       aliasvertnorm[a+1] += normal[1];
+                       aliasvertnorm[a+2] += normal[2];
+                       aliasvertusage[renderlist[2]]++;
                        renderlist += 3;
                }
        }
+       // FIXME: precalc this
        // average surface normals
        out = aliasvertnorm;
        u = aliasvertusage;
@@ -840,359 +732,142 @@ void ZymoticCalcNormals(int vertcount, int shadercount, int *renderlist)
        {
                if (*u > 1)
                {
-                       a = ixtable[*u];
-                       out[0] *= a;
-                       out[1] *= a;
-                       out[2] *= a;
+                       s = ixtable[*u];
+                       out[0] *= s;
+                       out[1] *= s;
+                       out[2] *= s;
                }
                u++;
                out += 3;
        }
 }
 
-void GL_DrawZymoticModelMesh(byte *colors, zymtype1header_t *m)
-{
-       int i, c, *renderlist, *texturenum;
-       if (!r_render.value)
-               return;
-       renderlist = (int *)(m->lump_render.start + (int) m);
-       texturenum = (int *)(m->lump_shaders.start + (int) m);
-       if (gl_vertexarrays.value)
-       {
-               qglVertexPointer(3, GL_FLOAT, 0, aliasvert);
-               glEnableClientState(GL_VERTEX_ARRAY);
-
-               qglColorPointer(4, GL_UNSIGNED_BYTE, 0, colors);
-               glEnableClientState(GL_COLOR_ARRAY);
-
-               qglTexCoordPointer(2, GL_FLOAT, 0, (float *)(m->lump_texcoords.start + (int) m));
-               glEnableClientState(GL_TEXTURE_COORD_ARRAY);
-
-               for (i = 0;i < m->numshaders;i++)
-               {
-                       c = (*renderlist++) * 3;
-                       glBindTexture(GL_TEXTURE_2D, *texturenum++);
-                       qglDrawElements(GL_TRIANGLES, c, GL_UNSIGNED_INT, renderlist);
-                       renderlist += c;
-               }
-
-               glDisableClientState(GL_TEXTURE_COORD_ARRAY);
-
-               glDisableClientState(GL_COLOR_ARRAY);
-
-               glDisableClientState(GL_VERTEX_ARRAY);
-       }
-       else
-       {
-               int index;
-               float *tex;
-               tex = (float *)(m->lump_texcoords.start + (int) m);
-
-               for (i = 0;i < m->numshaders;i++)
-               {
-                       c = *renderlist++;
-                       glBindTexture(GL_TEXTURE_2D, *texturenum++);
-                       glBegin(GL_TRIANGLES);
-                       while (c--)
-                       {
-                               index = *renderlist++;
-                               glTexCoord2fv(tex + index*2);
-                               glColor4ubv(colors + index*4);
-                               glVertex3fv(aliasvert + index*3);
-                               index = *renderlist++;
-                               glTexCoord2fv(tex + index*2);
-                               glColor4ubv(colors + index*4);
-                               glVertex3fv(aliasvert + index*3);
-                               index = *renderlist++;
-                               glTexCoord2fv(tex + index*2);
-                               glColor4ubv(colors + index*4);
-                               glVertex3fv(aliasvert + index*3);
-                       }
-                       glEnd();
-               }
-       }
-}
-
-void GL_DrawZymoticModelMeshFog(vec3_t org, zymtype1header_t *m)
+void R_DrawZymoticModelMeshCallback (const void *calldata1, int calldata2)
 {
+       float fog;
        vec3_t diff;
-       int i, c, *renderlist;
-       if (!r_render.value)
-               return;
+       int i, *renderlist;
+       zymtype1header_t *m;
+       rtexture_t *texture;
+       rmeshstate_t mstate;
+       const entity_render_t *ent = calldata1;
+       int shadernum = calldata2;
+       int numverts, numtriangles;
+
+       R_Mesh_Matrix(&ent->matrix);
+
+       // find the vertex index list and texture
+       m = ent->model->zymdata_header;
        renderlist = (int *)(m->lump_render.start + (int) m);
-       glDisable(GL_TEXTURE_2D);
-       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-       glEnable (GL_BLEND);
-       glDepthMask(0); // disable zbuffer updates
-
-       VectorSubtract(org, r_refdef.vieworg, diff);
-       glColor4f(fogcolor[0], fogcolor[1], fogcolor[2], exp(fogdensity/DotProduct(diff,diff)));
-       if (gl_vertexarrays.value)
-       {
-               qglVertexPointer(3, GL_FLOAT, 0, aliasvert);
-               glEnableClientState(GL_VERTEX_ARRAY);
+       for (i = 0;i < shadernum;i++)
+               renderlist += renderlist[0] * 3 + 1;
+       texture = ((rtexture_t **)(m->lump_shaders.start + (int) m))[shadernum];
 
-               for (i = 0;i < m->numshaders;i++)
-               {
-                       c = (*renderlist++) * 3;
-                       qglDrawElements(GL_TRIANGLES, c, GL_UNSIGNED_INT, renderlist);
-                       renderlist += c;
-               }
-
-               glDisableClientState(GL_VERTEX_ARRAY);
-       }
-       else
-       {
-               int index;
-               float *tex;
-               tex = (float *)(m->lump_texcoords.start + (int) m);
-
-               glBegin(GL_TRIANGLES);
-               for (i = 0;i < m->numshaders;i++)
-               {
-                       c = *renderlist++;
-                       while (c--)
-                       {
-                               index = *renderlist++;
-                               glVertex3fv(aliasvert + index*3);
-                               index = *renderlist++;
-                               glVertex3fv(aliasvert + index*3);
-                               index = *renderlist++;
-                               glVertex3fv(aliasvert + index*3);
-                       }
-               }
-               glEnd();
-       }
-       glEnable(GL_TEXTURE_2D);
-       glColor3f (1,1,1);
-}
-
-void GL_DrawZymoticModelMeshShadow(zymtype1header_t *m)
-{
-       int i, c, *renderlist;
-       float *av, l;
-       if (!r_render.value)
-               return;
-
-       // flatten it to make a shadow
-       av = aliasvert + 2;
-       l = lightspot[2] + 0.125;
-       for (i = 0;i < m->numverts;i++, av+=3)
-               if (*av > l)
-                       *av = l;
-
-       renderlist = (int *)(m->lump_render.start + (int) m);
-       glDisable(GL_TEXTURE_2D);
-       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-       glEnable (GL_BLEND);
-       glDepthMask(0); // disable zbuffer updates
+       numverts = m->numverts;
+       numtriangles = renderlist[0];
+       R_Mesh_ResizeCheck(numverts, numtriangles);
 
-       glColor4f(0.0f, 0.0f, 0.0f, 0.5f);
-       if (gl_vertexarrays.value)
+       fog = 0;
+       if (fogenabled)
        {
-               qglVertexPointer(3, GL_FLOAT, 0, aliasvert);
-               glEnableClientState(GL_VERTEX_ARRAY);
-
-               for (i = 0;i < m->numshaders;i++)
-               {
-                       c = (*renderlist++) * 3;
-                       qglDrawElements(GL_TRIANGLES, c, GL_UNSIGNED_INT, renderlist);
-                       renderlist += c;
-               }
-
-               glDisableClientState(GL_VERTEX_ARRAY);
+               VectorSubtract(ent->origin, r_origin, diff);
+               fog = DotProduct(diff,diff);
+               if (fog < 0.01f)
+                       fog = 0.01f;
+               fog = exp(fogdensity/fog);
+               if (fog > 1)
+                       fog = 1;
+               if (fog < 0.01f)
+                       fog = 0;
+               // fog method: darken, additive fog
+               // 1. render model as normal, scaled by inverse of fog alpha (darkens it)
+               // 2. render fog as additive
        }
-       else
-       {
-               int index;
-               float *tex;
-               tex = (float *)(m->lump_texcoords.start + (int) m);
 
-               glBegin(GL_TRIANGLES);
-               for (i = 0;i < m->numshaders;i++)
-               {
-                       c = *renderlist++;
-                       while (c--)
-                       {
-                               index = *renderlist++;
-                               glVertex3fv(aliasvert + index*3);
-                               index = *renderlist++;
-                               glVertex3fv(aliasvert + index*3);
-                               index = *renderlist++;
-                               glVertex3fv(aliasvert + index*3);
-                       }
-               }
-               glEnd();
-       }
-       glEnable(GL_TEXTURE_2D);
-       glColor3f (1,1,1);
-}
+       ZymoticLerpBones(m->numbones, (zymbonematrix *)(m->lump_poses.start + (int) m), ent->frameblend, (zymbone_t *)(m->lump_bones.start + (int) m));
+       ZymoticTransformVerts(numverts, (int *)(m->lump_vertbonecounts.start + (int) m), (zymvertex_t *)(m->lump_verts.start + (int) m));
+       ZymoticCalcNormals(numverts, m->numshaders, (int *)(m->lump_render.start + (int) m));
 
-/*
-=================
-R_DrawZymoticFrame
-=================
-*/
-void R_DrawZymoticFrame (zymtype1header_t *m, float alpha, vec3_t color, entity_t *ent, int shadow, vec3_t org, vec3_t angles, int frame, int skinblah, int effects, int flags)
-{
-       zymscene_t *scene;
-       float scenetime, scenefrac;
-       int sceneframe1, sceneframe2;
-       zymbonematrix *basebonepose;
-       if ((frame >= m->numscenes) || (frame < 0))
-       {
-               Con_DPrintf ("R_ZymoticSetupFrame: no such frame %d\n", frame);
-               frame = 0;
-       }
+       R_LightModel(ent, numverts, 1 - fog, 1 - fog, 1 - fog, false);
 
-       scene = (zymscene_t *)(m->lump_scenes.start + (int) m) + frame;
-       if (ent->draw_lastmodel != ent->model || ent->draw_pose != frame || ent->draw_lerpstart >= cl.time)
+       memset(&mstate, 0, sizeof(mstate));
+       mstate.wantoverbright = true;
+       if (ent->effects & EF_ADDITIVE)
        {
-               ent->draw_lastmodel = ent->model;
-               ent->draw_lastpose = -1;
-               ent->draw_pose = frame;
-               ent->draw_lerpstart = cl.time;
+               mstate.blendfunc1 = GL_SRC_ALPHA;
+               mstate.blendfunc2 = GL_ONE;
        }
-       scenetime = (cl.time - ent->draw_lerpstart) * scene->framerate;
-       sceneframe1 = (int) scenetime;
-       sceneframe2 = sceneframe1 + 1;
-       scenefrac = scenetime - sceneframe1;
-       if (scene->flags & ZYMSCENEFLAG_NOLOOP)
+       else if (ent->alpha != 1.0 || R_TextureHasAlpha(texture))
        {
-               if (sceneframe1 > (scene->length - 1))
-                       sceneframe1 = (scene->length - 1);
-               if (sceneframe2 > (scene->length - 1))
-                       sceneframe2 = (scene->length - 1);
+               mstate.blendfunc1 = GL_SRC_ALPHA;
+               mstate.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
        }
        else
        {
-               sceneframe1 %= scene->length;
-               sceneframe2 %= scene->length;
+               mstate.blendfunc1 = GL_ONE;
+               mstate.blendfunc2 = GL_ZERO;
        }
-       if (sceneframe2 == sceneframe1)
-               scenefrac = 0;
-
-       basebonepose = (zymbonematrix *)(m->lump_poses.start + (int) m);
-       ZymoticLerpBones(m->numbones, scenefrac, basebonepose + sceneframe1 * m->numbones, basebonepose + sceneframe2 * m->numbones, (zymbone_t *)(m->lump_bones.start + (int) m), org, angles);
-       ZymoticTransformVerts(m->numverts, (int *)(m->lump_vertbonecounts.start + (int) m), (zymvertex_t *)(m->lump_verts.start + (int) m));
-       ZymoticCalcNormals(m->numverts, m->numshaders, (int *)(m->lump_render.start + (int) m));
+       mstate.tex[0] = R_GetTexture(texture);
+       R_Mesh_State(&mstate);
 
-       R_LightModel(m->numverts, org, color);
+       c_alias_polys += numtriangles;
+       memcpy(varray_element, renderlist + 1, numtriangles * sizeof(int[3]));
+       memcpy(varray_vertex, aliasvert, numverts * sizeof(float[4]));
+       R_ModulateColors(aliasvertcolor, varray_color, numverts, mesh_colorscale, mesh_colorscale, mesh_colorscale);
+       memcpy(varray_texcoord[0], (float *)(m->lump_texcoords.start + (int) m), numverts * sizeof(float[2]));
+       R_Mesh_Draw(numverts, numtriangles);
 
-       if (!r_render.value)
-               return;
-       glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
-       glShadeModel(GL_SMOOTH);
-       if (effects & EF_ADDITIVE)
-       {
-               glBlendFunc(GL_SRC_ALPHA, GL_ONE); // additive rendering
-               glEnable(GL_BLEND);
-               glDepthMask(0);
-       }
-       else if (alpha != 1.0)
-       {
-               glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-               glEnable(GL_BLEND);
-               glDepthMask(0);
-       }
-       else
+       if (fog)
        {
-               glDisable(GL_BLEND);
-               glDepthMask(1);
-       }
-
-       GL_DrawZymoticModelMesh(aliasvertcolor, m);
-
-       if (fogenabled)
-               GL_DrawZymoticModelMeshFog(org, m);
+               memset(&mstate, 0, sizeof(mstate));
+               mstate.wantoverbright = false;
+               mstate.blendfunc1 = GL_SRC_ALPHA;
+               mstate.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
+               // FIXME: need alpha mask for fogging...
+               //mstate.tex[0] = R_GetTexture(texture);
+               R_Mesh_State(&mstate);
 
-       if (!fogenabled && r_shadows.value && !(effects & EF_ADDITIVE) && shadow)
-               GL_DrawZymoticModelMeshShadow(m);
-
-       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-       glEnable (GL_BLEND);
-       glDepthMask(1);
+               c_alias_polys += numtriangles;
+               memcpy(varray_element, renderlist + 1, numtriangles * sizeof(int[3]));
+               memcpy(varray_vertex, aliasvert, numverts * sizeof(float[4]));
+               R_FillColors(varray_color, numverts, fogcolor[0] * mesh_colorscale, fogcolor[1] * mesh_colorscale, fogcolor[2] * mesh_colorscale, ent->alpha * fog);
+               //memcpy(mesh_texcoord[0], (float *)(m->lump_texcoords.start + (int) m), numverts * sizeof(float[2]));
+               R_Mesh_Draw(numverts, numtriangles);
+       }
 }
 
-int modeldlightbits[8];
-extern int r_dlightframecount;
-
-/*
-=================
-R_DrawAliasModel
-
-=================
-*/
-void R_DrawAliasModel (entity_t *ent, int cull, float alpha, model_t *clmodel, int frame, int skin, vec3_t org, vec3_t angles, int effects, int flags, int colormap)
+void R_DrawZymoticModel (entity_render_t *ent)
 {
-       int                     i;
-       vec3_t          mins, maxs, color;
-       mleaf_t         *leaf;
-       void            *modelheader;
-       int                     *skinset;
+       int i;
+       zymtype1header_t *m;
+       rtexture_t *texture;
 
-       if (alpha < (1.0 / 64.0))
+       if (ent->alpha < (1.0f / 64.0f))
                return; // basically completely transparent
 
-       VectorAdd (org, clmodel->mins, mins);
-       VectorAdd (org, clmodel->maxs, maxs);
-
-       if (cull && R_CullBox (mins, maxs))
-               return;
-
        c_models++;
 
-       leaf = Mod_PointInLeaf (org, cl.worldmodel);
-       if (leaf->dlightframe == r_dlightframecount)
-               for (i = 0;i < 8;i++)
-                       modeldlightbits[i] = leaf->dlightbits[i];
-       else
-               for (i = 0;i < 8;i++)
-                       modeldlightbits[i] = 0;
-
-       // get lighting information
-
-       if ((flags & EF_FULLBRIGHT) || (effects & EF_FULLBRIGHT))
-               color[0] = color[1] = color[2] = 256;
-       else
-               R_LightPoint (color, org);
-
-       if (r_render.value)
-               glDisable(GL_ALPHA_TEST);
-
-       if (frame < 0 || frame >= clmodel->numframes)
-       {
-               frame = 0;
-               Con_DPrintf("invalid skin number %d for model %s\n", frame, clmodel->name);
-       }
-
-       if (skin < 0 || skin >= clmodel->numskins)
+       m = ent->model->zymdata_header;
+       for (i = 0;i < m->numshaders;i++)
        {
-               skin = 0;
-               Con_DPrintf("invalid skin number %d for model %s\n", skin, clmodel->name);
+               texture = ((rtexture_t **)(m->lump_shaders.start + (int) m))[i];
+               if (ent->effects & EF_ADDITIVE || ent->alpha != 1.0 || R_TextureHasAlpha(texture))
+                       R_MeshQueue_AddTransparent(ent->origin, R_DrawZymoticModelMeshCallback, ent, i);
+               else
+                       R_DrawZymoticModelMeshCallback(ent, i);
        }
+}
 
-       modelheader = Mod_Extradata (clmodel);
-
-       {
-//             int *skinanimrange = (int *) (clmodel->skinanimrange + (int) modelheader) + skin * 2;
-//             int *skinanim = (int *) (clmodel->skinanim + (int) modelheader);
-               int *skinanimrange = clmodel->skinanimrange + skin * 2;
-               int *skinanim = clmodel->skinanim;
-               i = skinanimrange[0];
-               if (skinanimrange[1] > 1) // animated
-                       i += ((int) (cl.time * 10) % skinanimrange[1]);
-               skinset = skinanim + i*5;
-       }
+void R_DrawQ1Q2AliasModel(entity_render_t *ent)
+{
+       if (ent->alpha < (1.0f / 64.0f))
+               return; // basically completely transparent
 
-       if (r_render.value)
-               glEnable (GL_TEXTURE_2D);
+       c_models++;
 
-       c_alias_polys += clmodel->numtris;
-       if (clmodel->aliastype == ALIASTYPE_ZYM)
-               R_DrawZymoticFrame (modelheader, alpha, color, ent, ent != &cl.viewent, org, angles, frame, 0, effects, flags);
-       else if (clmodel->aliastype == ALIASTYPE_MD2)
-               R_DrawQ2AliasFrame (modelheader, alpha, color, ent, ent != &cl.viewent, org, angles, frame, skinset[0], effects, flags);
+       if (ent->effects & EF_ADDITIVE || ent->alpha != 1.0 || R_FetchSkinFrame(ent)->fog != NULL)
+               R_MeshQueue_AddTransparent(ent->origin, R_DrawQ1Q2AliasModelCallback, ent, 0);
        else
-               R_DrawAliasFrame (modelheader, alpha, color, ent, ent != &cl.viewent, org, angles, frame, skinset, colormap, effects, flags);
+               R_DrawQ1Q2AliasModelCallback(ent, 0);
 }
+