]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - gl_models.c
bmodel shadow volumes
[xonotic/darkplaces.git] / gl_models.c
index fa2b3df1e9c2b32395de4730a4fe5495f17a3ecb..4d5a876f82b293c75dcab11e9ddae36ecdf07e4b 100644 (file)
@@ -1,83 +1,26 @@
 
 #include "quakedef.h"
+#include "cl_collision.h"
+#include "r_shadow.h"
 
-//cvar_t gl_transform = {0, "gl_transform", "1"};
-cvar_t gl_lockarrays = {0, "gl_lockarrays", "1"};
-cvar_t r_zymdebug = {0, "r_zymdebug", "0"};
-cvar_t r_zymdebug_dist = {0, "r_zymdebug_dist", "4"};
+cvar_t r_quickmodels = {0, "r_quickmodels", "1"};
 
 typedef struct
 {
        float m[3][4];
 } zymbonematrix;
 
-// LordHavoc: vertex array
-float *aliasvert;
-float *aliasvertnorm;
-float *aliasvertcolor;
-float *aliasvertcolor2;
-zymbonematrix *zymbonepose;
-int *aliasvertusage;
+// LordHavoc: vertex arrays
 
-rmeshinfo_t aliasmeshinfo;
-
-rtexture_t *chrometexture;
-
-int arraylocked = false;
-void GL_LockArray(int first, int count)
-{
-       if (gl_supportslockarrays && gl_lockarrays.integer)
-       {
-               qglLockArraysEXT(first, count);
-               arraylocked = true;
-       }
-}
-
-void GL_UnlockArray(void)
-{
-       if (arraylocked)
-       {
-               qglUnlockArraysEXT();
-               arraylocked = false;
-       }
-}
+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
 
-/*
-void GL_SetupModelTransform (vec3_t origin, vec3_t angles, vec_t scale)
-{
-       glTranslatef (origin[0], origin[1], origin[2]);
-
-       if (scale != 1)
-               glScalef (scale, scale, scale);
-       if (angles[1])
-           glRotatef (angles[1],  0, 0, 1);
-       if (angles[0])
-           glRotatef (-angles[0],  0, 1, 0);
-       if (angles[2])
-           glRotatef (angles[2],  1, 0, 0);
-}
-*/
-
-rtexturepool_t *chrometexturepool;
-
-// currently unused reflection effect texture
-void makechrometexture(void)
-{
-       int i;
-       byte noise[64*64];
-       byte data[64*64][4];
-
-       fractalnoise(noise, 64, 8);
-
-       // 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;
-       }
-
-       chrometexture = R_LoadTexture (chrometexturepool, "chrometexture", 64, 64, &data[0][0], TEXTYPE_RGBA, TEXF_MIPMAP | TEXF_PRECACHE);
-}
+float *aliasvertcolor2;
+float *aliasvertnorm;
+int *aliasvertusage;
+zymbonematrix *zymbonepose;
 
 mempool_t *gl_models_mempool;
 
@@ -85,19 +28,16 @@ void gl_models_start(void)
 {
        // allocate vertex processing arrays
        gl_models_mempool = Mem_AllocPool("GL_Models");
-       aliasvert = Mem_Alloc(gl_models_mempool, sizeof(float[MD2MAX_VERTS][3]));
+       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]));
-       aliasvertcolor = Mem_Alloc(gl_models_mempool, sizeof(float[MD2MAX_VERTS][4]));
        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]));
-       chrometexturepool = R_AllocTexturePool();
-       makechrometexture();
 }
 
 void gl_models_shutdown(void)
 {
-       R_FreeTexturePool(&chrometexturepool);
        Mem_FreePool(&gl_models_mempool);
 }
 
@@ -107,52 +47,45 @@ void gl_models_newmap(void)
 
 void GL_Models_Init(void)
 {
-//     Cvar_RegisterVariable(&gl_transform);
-       Cvar_RegisterVariable(&gl_lockarrays);
-       Cvar_RegisterVariable(&r_zymdebug);
-       Cvar_RegisterVariable(&r_zymdebug_dist);
+       Cvar_RegisterVariable(&r_quickmodels);
 
        R_RegisterModule("GL_Models", gl_models_start, gl_models_shutdown, gl_models_newmap);
 }
 
+/*
 void R_AliasTransformVerts(int vertcount)
 {
        vec3_t point;
-       float *av, *avn;
+       float *av;
        av = aliasvert;
-       avn = aliasvertnorm;
        while (vertcount >= 4)
        {
-               VectorCopy(av, point);softwaretransform(point, av);av += 3;
-               VectorCopy(av, point);softwaretransform(point, av);av += 3;
-               VectorCopy(av, point);softwaretransform(point, av);av += 3;
-               VectorCopy(av, point);softwaretransform(point, av);av += 3;
-               VectorCopy(avn, point);softwaretransformdirection(point, avn);avn += 3;
-               VectorCopy(avn, point);softwaretransformdirection(point, avn);avn += 3;
-               VectorCopy(avn, point);softwaretransformdirection(point, avn);avn += 3;
-               VectorCopy(avn, point);softwaretransformdirection(point, avn);avn += 3;
+               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 += 3;
-               VectorCopy(avn, point);softwaretransformdirection(point, avn);avn += 3;
+               VectorCopy(av, point);softwaretransform(point, av);av += 4;
                vertcount--;
        }
 }
+*/
 
-void R_AliasLerpVerts(int vertcount,
-                                         float lerp1, trivertx_t *verts1, vec3_t fscale1, vec3_t translate1,
-                                         float lerp2, trivertx_t *verts2, vec3_t fscale2, vec3_t translate2,
-                                         float lerp3, trivertx_t *verts3, vec3_t fscale3, vec3_t translate3,
-                                         float lerp4, trivertx_t *verts4, vec3_t fscale4, vec3_t translate4)
+void R_AliasLerpVerts(int vertcount, float *vertices, float *normals,
+               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 scale1, scale2, scale3, scale4, translate;
-       float *n1, *n2, *n3, *n4;
+       const float *n1, *n2, *n3, *n4;
        float *av, *avn;
-       av = aliasvert;
-       avn = aliasvertnorm;
+       av = vertices;
+       avn = normals;
        VectorScale(fscale1, lerp1, scale1);
        if (lerp2)
        {
@@ -179,7 +112,7 @@ void R_AliasLerpVerts(int vertcount,
                                        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 += 3;
+                                       av += 4;
                                        avn += 3;
                                        verts1++;verts2++;verts3++;verts4++;
                                }
@@ -201,7 +134,7 @@ void R_AliasLerpVerts(int vertcount,
                                        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 += 3;
+                                       av += 4;
                                        avn += 3;
                                        verts1++;verts2++;verts3++;
                                }
@@ -223,7 +156,7 @@ void R_AliasLerpVerts(int vertcount,
                                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 += 3;
+                               av += 4;
                                avn += 3;
                                verts1++;verts2++;
                        }
@@ -247,7 +180,7 @@ void R_AliasLerpVerts(int vertcount,
                                avn[0] = n1[0] * lerp1;
                                avn[1] = n1[1] * lerp1;
                                avn[2] = n1[2] * lerp1;
-                               av += 3;
+                               av += 4;
                                avn += 3;
                                verts1++;
                        }
@@ -261,7 +194,7 @@ void R_AliasLerpVerts(int vertcount,
                                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 += 3;
+                               av += 4;
                                avn += 3;
                                verts1++;
                        }
@@ -269,106 +202,59 @@ void R_AliasLerpVerts(int vertcount,
        }
 }
 
-void R_DrawModelMesh(rtexture_t *skin, float *colors, float cred, float cgreen, float cblue)
-{
-       aliasmeshinfo.tex[0] = R_GetTexture(skin);
-       aliasmeshinfo.color = colors;
-       if (colors == NULL)
-       {
-               aliasmeshinfo.cr = cred;
-               aliasmeshinfo.cg = cgreen;
-               aliasmeshinfo.cb = cblue;
-               aliasmeshinfo.ca = currentrenderentity->alpha;
-       }
-
-       c_alias_polys += aliasmeshinfo.numtriangles;
-       R_Mesh_Draw(&aliasmeshinfo);
-
-       // leave it in a state for additional passes
-       aliasmeshinfo.blendfunc1 = GL_SRC_ALPHA;
-       aliasmeshinfo.blendfunc2 = GL_ONE;
-}
-
-void R_TintModel(float *in, float *out, int verts, float r, float g, float b)
+skinframe_t *R_FetchSkinFrame(const entity_render_t *ent)
 {
-       int i;
-       for (i = 0;i < verts;i++)
-       {
-               out[0] = in[0] * r;
-               out[1] = in[1] * g;
-               out[2] = in[2] * b;
-               out[3] = in[3];
-               in += 4;
-               out += 4;
-       }
+       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
+               return &model->skinframes[model->skinscenes[s].firstframe];
 }
 
-void R_SetupMDLMD2Frames(skinframe_t **skinframe)
+void R_LerpMDLMD2Vertices(const entity_render_t *ent, float *vertices, float *normals)
 {
-       md2frame_t *frame1, *frame2, *frame3, *frame4;
-       trivertx_t *frame1verts, *frame2verts, *frame3verts, *frame4verts;
-       model_t *model;
-       model = currentrenderentity->model;
-
-       if (model->skinscenes[currentrenderentity->skinnum].framecount > 1)
-               *skinframe = &model->skinframes[model->skinscenes[currentrenderentity->skinnum].firstframe + (int) (cl.time * 10) % model->skinscenes[currentrenderentity->skinnum].framecount];
-       else
-               *skinframe = &model->skinframes[model->skinscenes[currentrenderentity->skinnum].firstframe];
-
-       frame1 = &model->mdlmd2data_frames[currentrenderentity->frameblend[0].frame];
-       frame2 = &model->mdlmd2data_frames[currentrenderentity->frameblend[1].frame];
-       frame3 = &model->mdlmd2data_frames[currentrenderentity->frameblend[2].frame];
-       frame4 = &model->mdlmd2data_frames[currentrenderentity->frameblend[3].frame];
-       frame1verts = &model->mdlmd2data_pose[currentrenderentity->frameblend[0].frame * model->numverts];
-       frame2verts = &model->mdlmd2data_pose[currentrenderentity->frameblend[1].frame * model->numverts];
-       frame3verts = &model->mdlmd2data_pose[currentrenderentity->frameblend[2].frame * model->numverts];
-       frame4verts = &model->mdlmd2data_pose[currentrenderentity->frameblend[3].frame * model->numverts];
-       /*
-       if (currentrenderentity->frameblend[0].lerp)
-               Con_Printf("frame1: %i/%i %s scale %f %f %f translate %f %f %f\n", currentrenderentity->frameblend[0].frame, model->numframes, frame1->name, frame1->scale[0], frame1->scale[1], frame1->scale[2], frame1->translate[0], frame1->translate[1], frame1->translate[2]);
-       if (currentrenderentity->frameblend[1].lerp)
-               Con_Printf("frame2: %i/%i %s scale %f %f %f translate %f %f %f\n", currentrenderentity->frameblend[0].frame, model->numframes, frame2->name, frame2->scale[0], frame2->scale[1], frame2->scale[2], frame2->translate[0], frame2->translate[1], frame2->translate[2]);
-       if (currentrenderentity->frameblend[2].lerp)
-               Con_Printf("frame3: %i/%i %s scale %f %f %f translate %f %f %f\n", currentrenderentity->frameblend[0].frame, model->numframes, frame3->name, frame3->scale[0], frame3->scale[1], frame3->scale[2], frame3->translate[0], frame3->translate[1], frame3->translate[2]);
-       if (currentrenderentity->frameblend[3].lerp)
-               Con_Printf("frame4: %i/%i %s scale %f %f %f translate %f %f %f\n", currentrenderentity->frameblend[0].frame, model->numframes, frame4->name, frame4->scale[0], frame4->scale[1], frame4->scale[2], frame4->translate[0], frame4->translate[1], frame4->translate[2]);
-       */
-       R_AliasLerpVerts(model->numverts,
-               currentrenderentity->frameblend[0].lerp, frame1verts, frame1->scale, frame1->translate,
-               currentrenderentity->frameblend[1].lerp, frame2verts, frame2->scale, frame2->translate,
-               currentrenderentity->frameblend[2].lerp, frame3verts, frame3->scale, frame3->translate,
-               currentrenderentity->frameblend[3].lerp, frame4verts, frame4->scale, frame4->translate);
-       R_AliasTransformVerts(model->numverts);
-
-       R_LightModel(model->numverts);
+       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, vertices, normals,
+               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);
 }
 
-void R_DrawQ1AliasModel (void)
+void R_DrawQ1Q2AliasModelCallback (const void *calldata1, int calldata2)
 {
-       float fog;
+       int i, c, pantsfullbright, shirtfullbright, colormapped, tex;
+       float pantscolor[3], shirtcolor[3];
+       float fog, colorscale;
        vec3_t diff;
+       qbyte *bcolor;
+       rmeshstate_t m;
        model_t *model;
        skinframe_t *skinframe;
+       const entity_render_t *ent = calldata1;
+       int blendfunc1, blendfunc2;
 
-       model = currentrenderentity->model;
-
-       R_SetupMDLMD2Frames(&skinframe);
-
-       memset(&aliasmeshinfo, 0, sizeof(aliasmeshinfo));
-
-       aliasmeshinfo.vertex = aliasvert;
-       aliasmeshinfo.vertexstep = sizeof(float[3]);
-       aliasmeshinfo.numverts = model->numverts;
-       aliasmeshinfo.numtriangles = model->numtris;
-       aliasmeshinfo.index = model->mdldata_indices;
-       aliasmeshinfo.colorstep = sizeof(float[4]);
-       aliasmeshinfo.texcoords[0] = model->mdldata_texcoords;
-       aliasmeshinfo.texcoordstep[0] = sizeof(float[2]);
+//     softwaretransformforentity(ent);
+       R_Mesh_Matrix(&ent->matrix);
 
        fog = 0;
        if (fogenabled)
        {
-               VectorSubtract(currentrenderentity->origin, r_origin, diff);
+               VectorSubtract(ent->origin, r_origin, diff);
                fog = DotProduct(diff,diff);
                if (fog < 0.01f)
                        fog = 0.01f;
@@ -382,353 +268,364 @@ void R_DrawQ1AliasModel (void)
                // 2. render fog as additive
        }
 
-       if (currentrenderentity->effects & EF_ADDITIVE)
+       model = ent->model;
+       R_Mesh_ResizeCheck(model->numverts);
+
+       skinframe = R_FetchSkinFrame(ent);
+
+       if (ent->effects & EF_ADDITIVE)
        {
-               aliasmeshinfo.transparent = true;
-               aliasmeshinfo.blendfunc1 = GL_SRC_ALPHA;
-               aliasmeshinfo.blendfunc2 = GL_ONE;
+               blendfunc1 = GL_SRC_ALPHA;
+               blendfunc2 = GL_ONE;
        }
-       else if (currentrenderentity->alpha != 1.0 || skinframe->fog != NULL)
+       else if (ent->alpha != 1.0 || skinframe->fog != NULL)
        {
-               aliasmeshinfo.transparent = true;
-               aliasmeshinfo.blendfunc1 = GL_SRC_ALPHA;
-               aliasmeshinfo.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
+               blendfunc1 = GL_SRC_ALPHA;
+               blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
        }
        else
        {
-               aliasmeshinfo.transparent = false;
-               aliasmeshinfo.blendfunc1 = GL_ONE;
-               aliasmeshinfo.blendfunc2 = GL_ZERO;
+               blendfunc1 = GL_ONE;
+               blendfunc2 = GL_ZERO;
+       }
+
+       colorscale = r_colorscale;
+       if (gl_combine.integer)
+               colorscale *= 0.25f;
+
+       if (!skinframe->base && !skinframe->pants && !skinframe->shirt && !skinframe->glow)
+       {
+               // untextured
+               memset(&m, 0, sizeof(m));
+               m.blendfunc1 = blendfunc1;
+               m.blendfunc2 = blendfunc2;
+               if (gl_combine.integer)
+                       m.texrgbscale[0] = 4;
+               m.tex[0] = R_GetTexture(r_notexture);
+               R_Mesh_State(&m);
+
+               c_alias_polys += model->numtris;
+               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_LerpMDLMD2Vertices(ent, aliasvert, aliasvertnorm);
+               R_LightModel(ent, model->numverts, colorscale, colorscale, colorscale, false);
+               aliasvert = aliasvertbuf;
+               aliasvertcolor = aliasvertcolorbuf;
+               GL_UseColorArray();
+               R_Mesh_Draw(model->numverts, model->numtris, model->mdlmd2data_indices);
+               return;
        }
 
-       // darken source
-       if (fog)
-               R_TintModel(aliasvertcolor, aliasvertcolor, model->numverts, 1 - fog, 1 - fog, 1 - fog);
 
-       if (skinframe->base || skinframe->pants || skinframe->shirt || skinframe->glow || skinframe->merged)
+       colormapped = !skinframe->merged || (ent->colormap >= 0 && skinframe->base && (skinframe->pants || skinframe->shirt));
+       if (!colormapped && !fog && !skinframe->glow && !skinframe->fog)
+       {
+               // fastpath for the normal situation (one texture)
+               memset(&m, 0, sizeof(m));
+               m.blendfunc1 = blendfunc1;
+               m.blendfunc2 = blendfunc2;
+               if (gl_combine.integer)
+                       m.texrgbscale[0] = 4;
+               m.tex[0] = R_GetTexture(skinframe->merged);
+               R_Mesh_State(&m);
+
+               c_alias_polys += model->numtris;
+               memcpy(varray_texcoord[0], model->mdlmd2data_texcoords, model->numverts * sizeof(float[2]));
+               aliasvert = varray_vertex;
+               aliasvertcolor = varray_color;
+               R_LerpMDLMD2Vertices(ent, aliasvert, aliasvertnorm);
+               R_LightModel(ent, model->numverts, colorscale, colorscale, colorscale, false);
+               aliasvert = aliasvertbuf;
+               aliasvertcolor = aliasvertcolorbuf;
+               GL_UseColorArray();
+               R_Mesh_Draw(model->numverts, model->numtris, model->mdlmd2data_indices);
+               return;
+       }
+
+       R_LerpMDLMD2Vertices(ent, aliasvert, aliasvertnorm);
+       R_LightModel(ent, model->numverts, colorscale * (1 - fog), colorscale * (1 - fog), colorscale * (1 - fog), false);
+
+       if (colormapped)
        {
-               if (currentrenderentity->colormap >= 0 && (skinframe->base || skinframe->pants || skinframe->shirt))
+               // 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
+       {
+               pantscolor[0] = pantscolor[1] = pantscolor[2] = shirtcolor[0] = shirtcolor[1] = shirtcolor[2] = 1;
+               pantsfullbright = shirtfullbright = false;
+       }
+
+       tex = colormapped ? R_GetTexture(skinframe->base) : R_GetTexture(skinframe->merged);
+       if (tex)
+       {
+               memset(&m, 0, sizeof(m));
+               m.blendfunc1 = blendfunc1;
+               m.blendfunc2 = blendfunc2;
+               if (gl_combine.integer)
+                       m.texrgbscale[0] = 4;
+               m.tex[0] = tex;
+               R_Mesh_State(&m);
+
+               blendfunc1 = GL_SRC_ALPHA;
+               blendfunc2 = GL_ONE;
+               c_alias_polys += model->numtris;
+               R_ModulateColors(aliasvertcolor, varray_color, model->numverts, colorscale, colorscale, colorscale);
+               memcpy(varray_vertex, aliasvert, model->numverts * sizeof(float[4]));
+               memcpy(varray_texcoord[0], model->mdlmd2data_texcoords, model->numverts * sizeof(float[2]));
+               GL_UseColorArray();
+               R_Mesh_Draw(model->numverts, model->numtris, model->mdlmd2data_indices);
+       }
+
+       if (colormapped)
+       {
+               if (skinframe->pants)
                {
-                       int c;
-                       byte *color;
-                       if (skinframe->base)
-                               R_DrawModelMesh(skinframe->base, aliasvertcolor, 0, 0, 0);
-                       if (skinframe->pants)
+                       tex = R_GetTexture(skinframe->pants);
+                       if (tex)
                        {
-                               c = (currentrenderentity->colormap & 0xF) << 4;c += (c >= 128 && c < 224) ? 4 : 12; // 128-224 are backwards ranges
-                               color = (byte *) (&d_8to24table[c]);
-                               if (c >= 224) // fullbright ranges
-                                       R_DrawModelMesh(skinframe->pants, NULL, color[0] * (1.0f / 255.0f), color[1] * (1.0f / 255.0f), color[2] * (1.0f / 255.0f));
+                               memset(&m, 0, sizeof(m));
+                               m.blendfunc1 = blendfunc1;
+                               m.blendfunc2 = blendfunc2;
+                               if (gl_combine.integer)
+                                       m.texrgbscale[0] = 4;
+                               m.tex[0] = tex;
+                               R_Mesh_State(&m);
+
+                               blendfunc1 = GL_SRC_ALPHA;
+                               blendfunc2 = GL_ONE;
+                               c_alias_polys += model->numtris;
+                               if (pantsfullbright)
+                                       GL_Color(pantscolor[0] * colorscale, pantscolor[1] * colorscale, pantscolor[2] * colorscale, ent->alpha);
                                else
                                {
-                                       R_TintModel(aliasvertcolor, aliasvertcolor2, model->numverts, color[0] * (1.0f / 255.0f), color[1] * (1.0f / 255.0f), color[2] * (1.0f / 255.0f));
-                                       R_DrawModelMesh(skinframe->pants, aliasvertcolor2, 0, 0, 0);
+                                       GL_UseColorArray();
+                                       R_ModulateColors(aliasvertcolor, varray_color, model->numverts, pantscolor[0] * colorscale, pantscolor[1] * colorscale, pantscolor[2] * colorscale);
                                }
+                               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, model->mdlmd2data_indices);
                        }
-                       if (skinframe->shirt)
+               }
+               if (skinframe->shirt)
+               {
+                       tex = R_GetTexture(skinframe->shirt);
+                       if (tex)
                        {
-                               c = currentrenderentity->colormap & 0xF0      ;c += (c >= 128 && c < 224) ? 4 : 12; // 128-224 are backwards ranges
-                               color = (byte *) (&d_8to24table[c]);
-                               if (c >= 224) // fullbright ranges
-                                       R_DrawModelMesh(skinframe->shirt, NULL, color[0] * (1.0f / 255.0f), color[1] * (1.0f / 255.0f), color[2] * (1.0f / 255.0f));
+                               memset(&m, 0, sizeof(m));
+                               m.blendfunc1 = blendfunc1;
+                               m.blendfunc2 = blendfunc2;
+                               if (gl_combine.integer)
+                                       m.texrgbscale[0] = 4;
+                               m.tex[0] = tex;
+                               R_Mesh_State(&m);
+
+                               blendfunc1 = GL_SRC_ALPHA;
+                               blendfunc2 = GL_ONE;
+                               c_alias_polys += model->numtris;
+                               if (shirtfullbright)
+                                       GL_Color(shirtcolor[0] * colorscale, shirtcolor[1] * colorscale, shirtcolor[2] * colorscale, ent->alpha);
                                else
                                {
-                                       R_TintModel(aliasvertcolor, aliasvertcolor2, model->numverts, color[0] * (1.0f / 255.0f), color[1] * (1.0f / 255.0f), color[2] * (1.0f / 255.0f));
-                                       R_DrawModelMesh(skinframe->shirt, aliasvertcolor2, 0, 0, 0);
+                                       GL_UseColorArray();
+                                       R_ModulateColors(aliasvertcolor, varray_color, model->numverts, shirtcolor[0] * colorscale, shirtcolor[1] * colorscale, shirtcolor[2] * colorscale);
                                }
+                               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, model->mdlmd2data_indices);
                        }
                }
-               else
+       }
+       if (skinframe->glow)
+       {
+               tex = R_GetTexture(skinframe->glow);
+               if (tex)
                {
-                       if (skinframe->merged)
-                               R_DrawModelMesh(skinframe->merged, aliasvertcolor, 0, 0, 0);
-                       else
-                       {
-                               if (skinframe->base) R_DrawModelMesh(skinframe->base, aliasvertcolor, 0, 0, 0);
-                               if (skinframe->pants) R_DrawModelMesh(skinframe->pants, aliasvertcolor, 0, 0, 0);
-                               if (skinframe->shirt) R_DrawModelMesh(skinframe->shirt, aliasvertcolor, 0, 0, 0);
-                       }
+                       memset(&m, 0, sizeof(m));
+                       m.blendfunc1 = blendfunc1;
+                       m.blendfunc2 = blendfunc2;
+                       m.tex[0] = tex;
+                       R_Mesh_State(&m);
+
+                       blendfunc1 = GL_SRC_ALPHA;
+                       blendfunc2 = GL_ONE;
+                       c_alias_polys += model->numtris;
+                       memcpy(varray_vertex, aliasvert, model->numverts * sizeof(float[4]));
+                       memcpy(varray_texcoord[0], model->mdlmd2data_texcoords, model->numverts * sizeof(float[2]));
+                       GL_Color((1 - fog) * r_colorscale, (1 - fog) * r_colorscale, (1 - fog) * r_colorscale, ent->alpha);
+                       R_Mesh_Draw(model->numverts, model->numtris, model->mdlmd2data_indices);
                }
-               if (skinframe->glow) R_DrawModelMesh(skinframe->glow, NULL, 1 - fog, 1 - fog, 1 - fog);
        }
-       else
-               R_DrawModelMesh(0, NULL, 1 - fog, 1 - fog, 1 - fog);
-
        if (fog)
        {
-               aliasmeshinfo.tex[0] = R_GetTexture(skinframe->fog);
-               aliasmeshinfo.blendfunc1 = GL_SRC_ALPHA;
-               aliasmeshinfo.blendfunc2 = GL_ONE;
-               aliasmeshinfo.color = NULL;
-
-               aliasmeshinfo.cr = fogcolor[0];
-               aliasmeshinfo.cg = fogcolor[1];
-               aliasmeshinfo.cb = fogcolor[2];
-               aliasmeshinfo.ca = currentrenderentity->alpha * fog;
-
-               c_alias_polys += aliasmeshinfo.numtriangles;
-               R_Mesh_Draw(&aliasmeshinfo);
+               memset(&m, 0, sizeof(m));
+               m.blendfunc1 = GL_SRC_ALPHA;
+               m.blendfunc2 = GL_ONE;
+               m.tex[0] = R_GetTexture(skinframe->fog);
+               R_Mesh_State(&m);
+
+               c_alias_polys += model->numtris;
+               memcpy(varray_vertex, aliasvert, model->numverts * sizeof(float[4]));
+               memcpy(varray_texcoord[0], model->mdlmd2data_texcoords, model->numverts * sizeof(float[2]));
+               GL_Color(fogcolor[0] * fog * r_colorscale, fogcolor[1] * fog * r_colorscale, fogcolor[2] * fog * r_colorscale, ent->alpha);
+               R_Mesh_Draw(model->numverts, model->numtris, model->mdlmd2data_indices);
        }
 }
 
-void R_DrawQ2AliasModel (void)
+extern cvar_t r_shadows;
+void R_DrawQ1Q2AliasModelFakeShadow (entity_render_t *ent)
 {
-       int *order, count;
-       vec3_t diff;
-       skinframe_t *skinframe;
+       int i;
+       rmeshstate_t m;
        model_t *model;
-
-       model = currentrenderentity->model;
-
-       R_SetupMDLMD2Frames(&skinframe);
-
-       if (!r_render.integer)
-               return;
-
-       // FIXME FIXME FIXME rewrite loader to convert to triangle mesh
-       glBindTexture(GL_TEXTURE_2D, R_GetTexture(skinframe->base));
-
-       if (currentrenderentity->effects & EF_ADDITIVE)
-       {
-               glBlendFunc(GL_SRC_ALPHA, GL_ONE); // additive rendering
-               glEnable(GL_BLEND);
-               glDepthMask(0);
-       }
-       else if (currentrenderentity->alpha != 1.0 || R_TextureHasAlpha(skinframe->base))
-       {
-               glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-               glEnable(GL_BLEND);
-               glDepthMask(0);
-       }
-       else
-       {
-               glDisable(GL_BLEND);
-               glDepthMask(1);
-       }
-
-       // LordHavoc: big mess...
-       // using vertex arrays only slightly, although it is enough to prevent duplicates
-       // (saving half the transforms)
-       glVertexPointer(3, GL_FLOAT, sizeof(float[3]), aliasvert);
-       glColorPointer(4, GL_FLOAT, sizeof(float[4]), aliasvertcolor);
-       glEnableClientState(GL_VERTEX_ARRAY);
-       glEnableClientState(GL_COLOR_ARRAY);
-
-       GL_LockArray(0, model->numverts);
-
-       order = model->md2data_glcmds;
-       while(1)
-       {
-               if (!(count = *order++))
-                       break;
-               if (count > 0)
-                       glBegin(GL_TRIANGLE_STRIP);
-               else
+       float *v, planenormal[3], planedist, dist, projection[3], floororigin[3], surfnormal[3], lightdirection[3], v2[3];
+       mlight_t *sl;
+       rdlight_t *rd;
+
+       if (r_shadows.integer > 1)
+       {
+               float f, lightscale, lightcolor[3];
+               vec3_t temp;
+               memset(&m, 0, sizeof(m));
+               m.blendfunc1 = GL_ONE;
+               m.blendfunc2 = GL_ONE;
+               R_Mesh_State(&m);
+               R_Mesh_Matrix(&ent->matrix);
+               for (i = 0, sl = cl.worldmodel->lights;i < cl.worldmodel->numlights;i++, sl++)
                {
-                       glBegin(GL_TRIANGLE_FAN);
-                       count = -count;
+                       if (d_lightstylevalue[sl->style] > 0)
+                       {
+                               VectorSubtract(ent->origin, sl->origin, temp);
+                               f = DotProduct(temp,temp);
+                               if (f < (ent->model->radius2 + sl->cullradius2))
+                               {
+                                       model = ent->model;
+                                       R_Mesh_ResizeCheck(model->numverts * 2);
+                                       R_LerpMDLMD2Vertices(ent, varray_vertex, aliasvertnorm);
+                                       Matrix4x4_Transform(&ent->inversematrix, sl->origin, temp);
+                                       GL_Color(0.1 * r_colorscale, 0.025 * r_colorscale, 0.0125 * r_colorscale, 1);
+                                       R_Shadow_Volume(model->numverts, model->numtris, model->mdlmd2data_indices, model->mdlmd2data_triangleneighbors, temp, sl->cullradius + model->radius - sqrt(f), true);
+                                       GL_UseColorArray();
+                                       lightscale = d_lightstylevalue[sl->style] * (1.0f / 65536.0f);
+                                       VectorScale(sl->light, lightscale, lightcolor);
+                                       R_Shadow_VertexLight(model->numverts, aliasvertnorm, temp, sl->cullradius2, sl->distbias, sl->subtract, lightcolor);
+                                       R_Mesh_Draw(model->numverts, model->numtris, model->mdlmd2data_indices);
+                               }
+                       }
                }
-               do
+               for (i = 0, rd = r_dlight;i < r_numdlights;i++, rd++)
                {
-                       glTexCoord2f(((float *)order)[0], ((float *)order)[1]);
-                       glArrayElement(order[2]);
-                       order += 3;
+                       if (ent != rd->ent)
+                       {
+                               VectorSubtract(ent->origin, rd->origin, temp);
+                               f = DotProduct(temp,temp);
+                               if (f < (ent->model->radius2 + rd->cullradius2))
+                               {
+                                       model = ent->model;
+                                       R_Mesh_ResizeCheck(model->numverts * 2);
+                                       R_LerpMDLMD2Vertices(ent, varray_vertex, aliasvertnorm);
+                                       Matrix4x4_Transform(&ent->inversematrix, rd->origin, temp);
+                                       GL_Color(0.1 * r_colorscale, 0.025 * r_colorscale, 0.0125 * r_colorscale, 1);
+                                       R_Shadow_Volume(model->numverts, model->numtris, model->mdlmd2data_indices, model->mdlmd2data_triangleneighbors, temp, rd->cullradius + model->radius - sqrt(f), true);
+                                       GL_UseColorArray();
+                                       R_Shadow_VertexLight(model->numverts, aliasvertnorm, temp, rd->cullradius2, LIGHTOFFSET, rd->subtract, rd->light);
+                                       R_Mesh_Draw(model->numverts, model->numtris, model->mdlmd2data_indices);
+                               }
+                       }
                }
-               while (count--);
+               return;
        }
 
-       GL_UnlockArray();
+       lightdirection[0] = 0.5;
+       lightdirection[1] = 0.2;
+       lightdirection[2] = -1;
+       VectorNormalizeFast(lightdirection);
 
-       glDisableClientState(GL_COLOR_ARRAY);
-       glDisableClientState(GL_VERTEX_ARRAY);
+       VectorMA(ent->origin, 65536.0f, lightdirection, v2);
+       if (CL_TraceLine(ent->origin, v2, floororigin, surfnormal, 0, false, NULL) == 1)
+               return;
 
-       if (fogenabled)
-       {
-               glDisable (GL_TEXTURE_2D);
-               glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-               glEnable (GL_BLEND);
-               glDepthMask(0); // disable zbuffer updates
+       R_Mesh_Matrix(&ent->matrix);
 
-               VectorSubtract(currentrenderentity->origin, r_origin, diff);
-               glColor4f(fogcolor[0], fogcolor[1], fogcolor[2], currentrenderentity->alpha * exp(fogdensity/DotProduct(diff,diff)));
+       model = ent->model;
+       R_Mesh_ResizeCheck(model->numverts);
 
-               // LordHavoc: big mess...
-               // using vertex arrays only slightly, although it is enough to prevent duplicates
-               // (saving half the transforms)
-               glVertexPointer(3, GL_FLOAT, sizeof(float[3]), aliasvert);
-               glEnableClientState(GL_VERTEX_ARRAY);
+       memset(&m, 0, sizeof(m));
+       m.blendfunc1 = GL_SRC_ALPHA;
+       m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
+       R_Mesh_State(&m);
 
-               GL_LockArray(0, model->numverts);
+       c_alias_polys += model->numtris;
+       R_LerpMDLMD2Vertices(ent, varray_vertex, aliasvertnorm);
 
-               order = model->md2data_glcmds;
-               while(1)
-               {
-                       if (!(count = *order++))
-                               break;
-                       if (count > 0)
-                               glBegin(GL_TRIANGLE_STRIP);
-                       else
-                       {
-                               glBegin(GL_TRIANGLE_FAN);
-                               count = -count;
-                       }
-                       do
-                       {
-                               glArrayElement(order[2]);
-                               order += 3;
-                       }
-                       while (count--);
-               }
+       // put a light direction in the entity's coordinate space
+       Matrix4x4_Transform3x3(&ent->inversematrix, lightdirection, projection);
+       VectorNormalizeFast(projection);
 
-               GL_UnlockArray();
+       // put the plane's normal in the entity's coordinate space
+       Matrix4x4_Transform3x3(&ent->inversematrix, surfnormal, planenormal);
+       VectorNormalizeFast(planenormal);
 
-               glDisableClientState(GL_VERTEX_ARRAY);
+       // put the plane's distance in the entity's coordinate space
+       VectorSubtract(floororigin, ent->origin, floororigin);
+       planedist = DotProduct(floororigin, surfnormal) + 2;
 
-               glEnable (GL_TEXTURE_2D);
-               glColor3f (1,1,1);
+       dist = -1.0f / DotProduct(projection, planenormal);
+       VectorScale(projection, dist, projection);
+       for (i = 0, v = varray_vertex;i < model->numverts;i++, v += 4)
+       {
+               dist = DotProduct(v, planenormal) - planedist;
+               if (dist > 0)
+               //if (i & 1)
+                       VectorMA(v, dist, projection, v);
        }
-
-       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-       glEnable (GL_BLEND);
-       glDepthMask(1);
+       GL_Color(0, 0, 0, 0.5);
+       R_Mesh_Draw(model->numverts, model->numtris, model->mdlmd2data_indices);
 }
 
-static int zymdebug;
-static float bestdist;
-
-int ZymoticLerpBones(int count, zymbonematrix *bonebase, frameblend_t *blend, zymbone_t *bone/*, float rootorigin[3], float rootangles[3], float rootscale*/)
+int ZymoticLerpBones(int count, const zymbonematrix *bonebase, const frameblend_t *blend, const zymbone_t *bone)
 {
-       int i/*, j*/;
-       float lerp1, lerp2, lerp3, lerp4/*, ang[3]*/;
-       zymbonematrix *out, modelmatrix, swapmatrix, m, *bone1, *bone2, *bone3, *bone4;
-       //int a, b, c;
-
-       memset(&swapmatrix, 0, sizeof(zymbonematrix));
-
-       //ang[0] = (zymdebug & 3) * 90;zymdebug /= 4;
-       //ang[1] = (zymdebug & 3) * 90;zymdebug /= 4;
-       //ang[2] = (zymdebug & 3) * 90;zymdebug /= 4;
-       /*
-       ang[0] = 0;
-       ang[1] = -90;
-       ang[2] = 0;
-       AngleVectorsFLU(ang, swapmatrix.m[0], swapmatrix.m[1], swapmatrix.m[2]);
-       */
-       /*
-       i = zymdebug % 3;zymdebug /= 3;
-       j = zymdebug % 3;zymdebug /= 3;
-       lerp1 = (zymdebug & 1) ? -1 : 1;zymdebug /= 2;
-       if (swapmatrix.m[i][j])
-               return false;
-       swapmatrix.m[i][j] = lerp1;
-       i = zymdebug % 3;zymdebug /= 3;
-       j = zymdebug % 3;zymdebug /= 3;
-       lerp1 = (zymdebug & 1) ? -1 : 1;zymdebug /= 2;
-       if (swapmatrix.m[i][j])
-               return false;
-       swapmatrix.m[i][j] = lerp1;
-       i = zymdebug % 3;zymdebug /= 3;
-       j = zymdebug % 3;zymdebug /= 3;
-       lerp1 = (zymdebug & 1) ? -1 : 1;zymdebug /= 2;
-       if (swapmatrix.m[i][j])
-               return false;
-       swapmatrix.m[i][j] = lerp1;
-       */
-       /*
-       lerp1 = (zymdebug & 1) ? -1 : 1;zymdebug /= 2;
-       swapmatrix.m[0][zymdebug % 3] = lerp1;zymdebug /= 3;
-       lerp1 = (zymdebug & 1) ? -1 : 1;zymdebug /= 2;
-       swapmatrix.m[1][zymdebug % 3] = lerp1;zymdebug /= 3;
-       lerp1 = (zymdebug & 1) ? -1 : 1;zymdebug /= 2;
-       swapmatrix.m[2][zymdebug % 3] = lerp1;zymdebug /= 3;
-       */
-       /*
-       for (i = 0;i < 3;i++)
-       {
-               for (j = 0;j < 3;j++)
-               {
-                       swapmatrix.m[i][j] = (zymdebug % 3) - 1;
-                       zymdebug /= 3;
-               }
-       }
-       */
-       /*
-       for (i = 0;i < 3;i++)
-       {
-               if (fabs(swapmatrix.m[i][0] * swapmatrix.m[i][0] + swapmatrix.m[i][1] * swapmatrix.m[i][1] + swapmatrix.m[i][2] * swapmatrix.m[i][2] - 1) > 0.01f)
-                       return false;
-               if (fabs(swapmatrix.m[0][i] * swapmatrix.m[0][i] + swapmatrix.m[1][i] * swapmatrix.m[1][i] + swapmatrix.m[2][i] * swapmatrix.m[2][i] - 1) > 0.01f)
-                       return false;
-       }
-       */
-       //if (fabs(DotProduct(swapmatrix.m[0], swapmatrix.m[0]) + DotProduct(swapmatrix.m[0], swapmatrix.m[0]) + DotProduct(swapmatrix.m[0], swapmatrix.m[0]) - 3) > 0.01f)
-       //      return false;
-
-       swapmatrix.m[0][1] = -1;
-       swapmatrix.m[1][0] = 1;
-       swapmatrix.m[2][2] = 1;
-
-       memset(&modelmatrix, 0, sizeof(zymbonematrix));
-
-       /*
-       a = b = c = 0;
-       switch (zymdebug % 6)
-       {
-       case 0: a = 0;b = 1;c = 2;break;
-       case 1: a = 1;b = 0;c = 2;break;
-       case 2: a = 2;b = 0;c = 1;break;
-       case 3: a = 0;b = 2;c = 1;break;
-       case 4: a = 1;b = 2;c = 0;break;
-       case 5: a = 2;b = 1;c = 0;break;
-       }
-       zymdebug /= 6;
-       AngleVectors(rootangles, m.m[a], m.m[b], m.m[c]);
-       if (zymdebug & 1)
-               VectorNegate(m.m[0], m.m[0]);
-       zymdebug /= 2;
-       if (zymdebug & 1)
-               VectorNegate(m.m[1], m.m[1]);
-       zymdebug /= 2;
-       if (zymdebug & 1)
-               VectorNegate(m.m[2], m.m[2]);
-       zymdebug /= 2;
-       if (zymdebug & 1)
-       {
-               for (a = 0;a < 3;a++)
-                       for (b = 0;b < 3;b++)
-                               modelmatrix.m[a][b] = m.m[b][a];
-       }
-       else
-       {
-               for (a = 0;a < 3;a++)
-                       for (b = 0;b < 3;b++)
-                               modelmatrix.m[a][b] = m.m[a][b];
-       }
-       zymdebug /= 2;
-       VectorScale(modelmatrix.m[0], rootscale, modelmatrix.m[0]);
-       VectorScale(modelmatrix.m[1], rootscale, modelmatrix.m[1]);
-       VectorScale(modelmatrix.m[2], rootscale, modelmatrix.m[2]);
-       */
+       int i;
+       float lerp1, lerp2, lerp3, lerp4;
+       zymbonematrix *out, rootmatrix, m;
+       const zymbonematrix *bone1, *bone2, *bone3, *bone4;
 
        /*
-       AngleVectors(rootangles, modelmatrix.m[0], modelmatrix.m[1], modelmatrix.m[2]);
-       VectorScale(modelmatrix.m[0], rootscale, modelmatrix.m[0]);
-       VectorScale(modelmatrix.m[1], rootscale, modelmatrix.m[1]);
-       VectorScale(modelmatrix.m[2], rootscale, modelmatrix.m[2]);
-       modelmatrix.m[0][3] = rootorigin[0];
-       modelmatrix.m[1][3] = rootorigin[1];
-       modelmatrix.m[2][3] = rootorigin[2];
-
-       memcpy(&m, &swapmatrix, sizeof(zymbonematrix));
-       if (zymdebug & 1)
-               R_ConcatTransforms(&m.m[0], &modelmatrix.m[0], &swapmatrix.m[0]);
-       else
-               R_ConcatTransforms(&modelmatrix.m[0], &m.m[0], &swapmatrix.m[0]);
-       zymdebug /= 2;
+       // 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];
        */
-
-       memcpy(&m, &swapmatrix, sizeof(zymbonematrix));
-       R_ConcatTransforms(&softwaretransform_matrix[0], &m.m[0], &swapmatrix.m[0]);
+       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;
@@ -761,9 +658,9 @@ int ZymoticLerpBones(int count, zymbonematrix *bonebase, frameblend_t *blend, zy
                                        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], &m.m[0], &out->m[0]);
+                                               R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &m.m[0][0], &out->m[0][0]);
                                        else
-                                               R_ConcatTransforms(&swapmatrix.m[0], &m.m[0], &out->m[0]);
+                                               R_ConcatTransforms(&rootmatrix.m[0][0], &m.m[0][0], &out->m[0][0]);
                                        bone1++;
                                        bone2++;
                                        bone3++;
@@ -790,9 +687,9 @@ int ZymoticLerpBones(int count, zymbonematrix *bonebase, frameblend_t *blend, zy
                                        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], &m.m[0], &out->m[0]);
+                                               R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &m.m[0][0], &out->m[0][0]);
                                        else
-                                               R_ConcatTransforms(&swapmatrix.m[0], &m.m[0], &out->m[0]);
+                                               R_ConcatTransforms(&rootmatrix.m[0][0], &m.m[0][0], &out->m[0][0]);
                                        bone1++;
                                        bone2++;
                                        bone3++;
@@ -819,9 +716,9 @@ int ZymoticLerpBones(int count, zymbonematrix *bonebase, frameblend_t *blend, zy
                                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]);
+                                       R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &m.m[0][0], &out->m[0][0]);
                                else
-                                       R_ConcatTransforms(&swapmatrix.m[0], &m.m[0], &out->m[0]);
+                                       R_ConcatTransforms(&rootmatrix.m[0][0], &m.m[0][0], &out->m[0][0]);
                                bone1++;
                                bone2++;
                                bone++;
@@ -850,9 +747,9 @@ int ZymoticLerpBones(int count, zymbonematrix *bonebase, frameblend_t *blend, zy
                                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], &m.m[0], &out->m[0]);
+                                       R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &m.m[0][0], &out->m[0][0]);
                                else
-                                       R_ConcatTransforms(&swapmatrix.m[0], &m.m[0], &out->m[0]);
+                                       R_ConcatTransforms(&rootmatrix.m[0][0], &m.m[0][0], &out->m[0][0]);
                                bone1++;
                                bone++;
                        }
@@ -863,9 +760,9 @@ int ZymoticLerpBones(int count, zymbonematrix *bonebase, frameblend_t *blend, zy
                        for (i = 0, out = zymbonepose;i < count;i++, out++)
                        {
                                if (bone->parent >= 0)
-                                       R_ConcatTransforms(&zymbonepose[bone->parent].m[0], &bone1->m[0], &out->m[0]);
+                                       R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &bone1->m[0][0], &out->m[0][0]);
                                else
-                                       R_ConcatTransforms(&swapmatrix.m[0], &bone1->m[0], &out->m[0]);
+                                       R_ConcatTransforms(&rootmatrix.m[0][0], &bone1->m[0][0], &out->m[0][0]);
                                bone1++;
                                bone++;
                        }
@@ -878,8 +775,6 @@ void ZymoticTransformVerts(int vertcount, int *bonecounts, zymvertex_t *vert)
 {
        int c;
        float *out = aliasvert;
-       float v[3];
-       float dist;
        zymbonematrix *matrix;
        while(vertcount--)
        {
@@ -889,39 +784,34 @@ void ZymoticTransformVerts(int vertcount, int *bonecounts, zymvertex_t *vert)
                if (c == 1)
                {
                        matrix = &zymbonepose[vert->bonenum];
-                       v[0] = vert->origin[0] * matrix->m[0][0] + vert->origin[1] * matrix->m[0][1] + vert->origin[2] * matrix->m[0][2] + matrix->m[0][3];
-                       v[1] = vert->origin[0] * matrix->m[1][0] + vert->origin[1] * matrix->m[1][1] + vert->origin[2] * matrix->m[1][2] + matrix->m[1][3];
-                       v[2] = vert->origin[0] * matrix->m[2][0] + vert->origin[1] * matrix->m[2][1] + vert->origin[2] * matrix->m[2][2] + matrix->m[2][3];
+                       out[0] = vert->origin[0] * matrix->m[0][0] + vert->origin[1] * matrix->m[0][1] + vert->origin[2] * matrix->m[0][2] + matrix->m[0][3];
+                       out[1] = vert->origin[0] * matrix->m[1][0] + vert->origin[1] * matrix->m[1][1] + vert->origin[2] * matrix->m[1][2] + matrix->m[1][3];
+                       out[2] = vert->origin[0] * matrix->m[2][0] + vert->origin[1] * matrix->m[2][1] + vert->origin[2] * matrix->m[2][2] + matrix->m[2][3];
                        vert++;
                }
                else
                {
-                       VectorClear(v);
+                       VectorClear(out);
                        while(c--)
                        {
                                matrix = &zymbonepose[vert->bonenum];
-                               v[0] += vert->origin[0] * matrix->m[0][0] + vert->origin[1] * matrix->m[0][1] + vert->origin[2] * matrix->m[0][2] + matrix->m[0][3];
-                               v[1] += vert->origin[0] * matrix->m[1][0] + vert->origin[1] * matrix->m[1][1] + vert->origin[2] * matrix->m[1][2] + matrix->m[1][3];
-                               v[2] += vert->origin[0] * matrix->m[2][0] + vert->origin[1] * matrix->m[2][1] + vert->origin[2] * matrix->m[2][2] + matrix->m[2][3];
+                               out[0] += vert->origin[0] * matrix->m[0][0] + vert->origin[1] * matrix->m[0][1] + vert->origin[2] * matrix->m[0][2] + matrix->m[0][3];
+                               out[1] += vert->origin[0] * matrix->m[1][0] + vert->origin[1] * matrix->m[1][1] + vert->origin[2] * matrix->m[1][2] + matrix->m[1][3];
+                               out[2] += vert->origin[0] * matrix->m[2][0] + vert->origin[1] * matrix->m[2][1] + vert->origin[2] * matrix->m[2][2] + matrix->m[2][3];
                                vert++;
                        }
                }
-               //softwaretransform(v, out);
-               VectorCopy(v, out);
-               dist = DotProduct(out, vpn) - DotProduct(r_origin, vpn);
-               if (dist > bestdist)
-                       bestdist = dist;
-               out += 3;
+               out += 4;
        }
 }
 
 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;
        // 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--)
@@ -929,9 +819,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];
@@ -941,21 +831,25 @@ void ZymoticCalcNormals(int vertcount, int shadercount, int *renderlist)
                        CrossProduct(v1, v2, 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;
@@ -963,183 +857,145 @@ 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 R_DrawZymoticModelMesh(zymtype1header_t *m)
+void R_DrawZymoticModelMeshCallback (const void *calldata1, int calldata2)
 {
-       int i, *renderlist;
-       rtexture_t **texture;
+       float fog, colorscale;
+       vec3_t diff;
+       int i, *renderlist, *elements;
+       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);
 
-       // FIXME: do better fog
+       // find the vertex index list and texture
+       m = ent->model->zymdata_header;
        renderlist = (int *)(m->lump_render.start + (int) m);
-       texture = (rtexture_t **)(m->lump_shaders.start + (int) m);
+       for (i = 0;i < shadernum;i++)
+               renderlist += renderlist[0] * 3 + 1;
+       texture = ((rtexture_t **)(m->lump_shaders.start + (int) m))[shadernum];
 
-       aliasmeshinfo.vertex = aliasvert;
-       aliasmeshinfo.vertexstep = sizeof(float[3]);
-       aliasmeshinfo.color = aliasvertcolor;
-       aliasmeshinfo.colorstep = sizeof(float[4]);
-       aliasmeshinfo.texcoords[0] = (float *)(m->lump_texcoords.start + (int) m);
-       aliasmeshinfo.texcoordstep[0] = sizeof(float[2]);
+       numverts = m->numverts;
+       numtriangles = *renderlist++;
+       elements = renderlist;
+       R_Mesh_ResizeCheck(numverts);
 
-       for (i = 0;i < m->numshaders;i++)
+       fog = 0;
+       if (fogenabled)
        {
-               aliasmeshinfo.tex[0] = R_GetTexture(texture[i]);
-               if (currentrenderentity->effects & EF_ADDITIVE)
-               {
-                       aliasmeshinfo.transparent = true;
-                       aliasmeshinfo.blendfunc1 = GL_SRC_ALPHA;
-                       aliasmeshinfo.blendfunc2 = GL_ONE;
-               }
-               else if (currentrenderentity->alpha != 1.0 || R_TextureHasAlpha(texture[i]))
-               {
-                       aliasmeshinfo.transparent = true;
-                       aliasmeshinfo.blendfunc1 = GL_SRC_ALPHA;
-                       aliasmeshinfo.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
-               }
-               else
-               {
-                       aliasmeshinfo.transparent = false;
-                       aliasmeshinfo.blendfunc1 = GL_ONE;
-                       aliasmeshinfo.blendfunc2 = GL_ZERO;
-               }
-               aliasmeshinfo.numtriangles = *renderlist++;
-               aliasmeshinfo.index = renderlist;
-               c_alias_polys += aliasmeshinfo.numtriangles;
-               R_Mesh_Draw(&aliasmeshinfo);
-               renderlist += aliasmeshinfo.numtriangles * 3;
+               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
        }
-}
 
-void R_DrawZymoticModelMeshFog(vec3_t org, zymtype1header_t *m)
-{
-       int i, *renderlist;
-       vec3_t diff;
+       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));
 
-       // FIXME: do better fog
-       renderlist = (int *)(m->lump_render.start + (int) m);
+       R_LightModel(ent, numverts, 1 - fog, 1 - fog, 1 - fog, false);
 
-       aliasmeshinfo.tex[0] = 0;
-       aliasmeshinfo.blendfunc1 = GL_SRC_ALPHA;
-       aliasmeshinfo.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
+       memset(&mstate, 0, sizeof(mstate));
+       if (ent->effects & EF_ADDITIVE)
+       {
+               mstate.blendfunc1 = GL_SRC_ALPHA;
+               mstate.blendfunc2 = GL_ONE;
+       }
+       else if (ent->alpha != 1.0 || R_TextureHasAlpha(texture))
+       {
+               mstate.blendfunc1 = GL_SRC_ALPHA;
+               mstate.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
+       }
+       else
+       {
+               mstate.blendfunc1 = GL_ONE;
+               mstate.blendfunc2 = GL_ZERO;
+       }
+       colorscale = r_colorscale;
+       if (gl_combine.integer)
+       {
+               mstate.texrgbscale[0] = 4;
+               colorscale *= 0.25f;
+       }
+       mstate.tex[0] = R_GetTexture(texture);
+       R_Mesh_State(&mstate);
 
-       VectorSubtract(org, r_origin, diff);
-       aliasmeshinfo.cr = fogcolor[0];
-       aliasmeshinfo.cg = fogcolor[1];
-       aliasmeshinfo.cb = fogcolor[2];
-       aliasmeshinfo.ca = currentrenderentity->alpha * exp(fogdensity/DotProduct(diff,diff));
+       c_alias_polys += numtriangles;
+       memcpy(varray_vertex, aliasvert, numverts * sizeof(float[4]));
+       R_ModulateColors(aliasvertcolor, varray_color, numverts, colorscale, colorscale, colorscale);
+       memcpy(varray_texcoord[0], (float *)(m->lump_texcoords.start + (int) m), numverts * sizeof(float[2]));
+       GL_UseColorArray();
+       R_Mesh_Draw(numverts, numtriangles, elements);
 
-       for (i = 0;i < m->numshaders;i++)
+       if (fog)
        {
-               aliasmeshinfo.numtriangles = *renderlist++;
-               aliasmeshinfo.index = renderlist;
-               c_alias_polys += aliasmeshinfo.numtriangles;
-               R_Mesh_Draw(&aliasmeshinfo);
-               renderlist += aliasmeshinfo.numtriangles * 3;
+               memset(&mstate, 0, sizeof(mstate));
+               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);
+
+               c_alias_polys += numtriangles;
+               memcpy(varray_vertex, aliasvert, numverts * sizeof(float[4]));
+               //memcpy(mesh_texcoord[0], (float *)(m->lump_texcoords.start + (int) m), numverts * sizeof(float[2]));
+               GL_Color(fogcolor[0] * r_colorscale, fogcolor[1] * r_colorscale, fogcolor[2] * r_colorscale, ent->alpha * fog);
+               R_Mesh_Draw(numverts, numtriangles, elements);
        }
 }
 
-void R_DrawZymoticModel (void)
+void R_DrawZymoticModel (entity_render_t *ent)
 {
-       //int a, b, c;
-       int keeptrying;
+       int i;
        zymtype1header_t *m;
-       //vec3_t angles;
-
-       // FIXME: do better fog
-       m = currentrenderentity->model->zymdata_header;
-       keeptrying = true;
-       while(keeptrying)
-       {
-               keeptrying = false;
-               zymdebug = r_zymdebug.integer;
-               /*
-               angles[0] = currentrenderentity->angles[1];
-               angles[1] = currentrenderentity->angles[0] + 180;
-               angles[2] = -currentrenderentity->angles[2];
-               */
-               /*
-               angles[0] = currentrenderentity->angles[0];
-               angles[1] = currentrenderentity->angles[1];
-               angles[2] = currentrenderentity->angles[2];
-               */
-               /*
-               a = b = c = 0;
-               switch (zymdebug % 6)
-               {
-               case 0: a = 0;b = 1;c = 2;break;
-               case 1: a = 1;b = 0;c = 2;break;
-               case 2: a = 2;b = 0;c = 1;break;
-               case 3: a = 0;b = 2;c = 1;break;
-               case 4: a = 1;b = 2;c = 0;break;
-               case 5: a = 2;b = 1;c = 0;break;
-               }
-               zymdebug /= 6;
-               angles[0] = currentrenderentity->angles[a];
-               angles[1] = currentrenderentity->angles[b];
-               angles[2] = currentrenderentity->angles[c];
-               if (zymdebug & 1)
-                       angles[0] = -angles[0];
-               zymdebug /= 2;
-               if (zymdebug & 1)
-                       angles[1] = -angles[1];
-               zymdebug /= 2;
-               if (zymdebug & 1)
-                       angles[2] = -angles[2];
-               zymdebug /= 2;
-               angles[0] += (zymdebug & 3) * 90;
-               zymdebug /= 4;
-               angles[1] += (zymdebug & 3) * 90;
-               zymdebug /= 4;
-               angles[2] += (zymdebug & 3) * 90;
-               zymdebug /= 4;
-               */
-               bestdist = -1000;
-               if (ZymoticLerpBones(m->numbones, (zymbonematrix *)(m->lump_poses.start + (int) m), currentrenderentity->frameblend, (zymbone_t *)(m->lump_bones.start + (int) m)/*, currentrenderentity->origin, angles, currentrenderentity->scale*/))
-                       ZymoticTransformVerts(m->numverts, (int *)(m->lump_vertbonecounts.start + (int) m), (zymvertex_t *)(m->lump_verts.start + (int) m));
-               if (bestdist < r_zymdebug_dist.value)
-               {
-                       Cvar_SetValueQuick(&r_zymdebug, r_zymdebug.integer + 1);
-                       if (zymdebug) // if there is some leftover, unclaimed, don't repeat
-                               Cvar_SetValueQuick(&r_zymdebug, 0);
-                       else
-                               keeptrying = true;
-                       Con_Printf("trying %i, bestdist was: %f\n", r_zymdebug.integer, bestdist);
-               }
-       }
-       ZymoticCalcNormals(m->numverts, m->numshaders, (int *)(m->lump_render.start + (int) m));
-
-       R_LightModel(m->numverts);
+       rtexture_t *texture;
 
-       memset(&aliasmeshinfo, 0, sizeof(aliasmeshinfo));
-       aliasmeshinfo.numverts = m->numverts;
+       if (ent->alpha < (1.0f / 64.0f))
+               return; // basically completely transparent
 
-       R_DrawZymoticModelMesh(m);
+       c_models++;
 
-       if (fogenabled)
-               R_DrawZymoticModelMeshFog(currentrenderentity->origin, m);
+       m = ent->model->zymdata_header;
+       for (i = 0;i < m->numshaders;i++)
+       {
+               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);
+       }
 }
 
-void R_DrawAliasModel (void)
+void R_DrawQ1Q2AliasModel(entity_render_t *ent)
 {
-       if (currentrenderentity->alpha < (1.0f / 64.0f))
+       if (ent->alpha < (1.0f / 64.0f))
                return; // basically completely transparent
 
        c_models++;
 
-       softwaretransformforentity(currentrenderentity);
-
-       if (currentrenderentity->model->aliastype == ALIASTYPE_ZYM)
-               R_DrawZymoticModel();
-       else if (currentrenderentity->model->aliastype == ALIASTYPE_MD2)
-               R_DrawQ2AliasModel();
+       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_DrawQ1AliasModel();
+               R_DrawQ1Q2AliasModelCallback(ent, 0);
 }