]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - gl_models.c
fix for 16bit textures looking awful (bug in the alpha check for 8bit)
[xonotic/darkplaces.git] / gl_models.c
index 6f90d87ba9a74ae62ba8dd3cf058abfd879c0fba..9d85d0791ead11a9e7e62fc81f46a62d66d27ebf 100644 (file)
@@ -1,7 +1,7 @@
 
 #include "quakedef.h"
-
-cvar_t r_quickmodels = {0, "r_quickmodels", "1"};
+#include "cl_collision.h"
+#include "r_shadow.h"
 
 typedef struct
 {
@@ -10,13 +10,13 @@ typedef struct
 
 // 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 *aliasvert_svectors;
+float *aliasvert_tvectors;
+float *aliasvert_normals;
 
 float *aliasvertcolor2;
-float *aliasvertnorm;
 int *aliasvertusage;
 zymbonematrix *zymbonepose;
 
@@ -26,9 +26,10 @@ void gl_models_start(void)
 {
        // 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]));
+       aliasvert_svectors = Mem_Alloc(gl_models_mempool, sizeof(float[MD2MAX_VERTS][4]));
+       aliasvert_tvectors = Mem_Alloc(gl_models_mempool, sizeof(float[MD2MAX_VERTS][4]));
+       aliasvert_normals = 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]));
@@ -45,212 +46,172 @@ void gl_models_newmap(void)
 
 void GL_Models_Init(void)
 {
-       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;
-       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--;
-       }
-}
-*/
-
-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)
+void R_Model_Alias_GetMeshVerts(const entity_render_t *ent, aliasmesh_t *mesh, float *vertices, float *normals, float *svectors, float *tvectors)
 {
-       int i;
-       vec3_t scale1, scale2, scale3, scale4, translate;
-       const float *n1, *n2, *n3, *n4;
-       float *av, *avn;
-       av = aliasvert;
-       avn = aliasvertnorm;
-       VectorScale(fscale1, lerp1, scale1);
-       if (lerp2)
+       int i, vertcount;
+       float lerp1, lerp2, lerp3, lerp4;
+       const aliasvertex_t *verts1, *verts2, *verts3, *verts4;
+
+       if (vertices == NULL)
+               Host_Error("R_Model_Alias_GetMeshVerts: vertices == NULL.\n");
+       if (svectors != NULL && (tvectors == NULL || normals == NULL))
+               Host_Error("R_Model_Alias_GetMeshVerts: svectors requires tvectors and normals.\n");
+       if (tvectors != NULL && (svectors == NULL || normals == NULL))
+               Host_Error("R_Model_Alias_GetMeshVerts: tvectors requires svectors and normals.\n");
+
+       vertcount = mesh->num_vertices;
+       verts1 = mesh->data_vertices + ent->frameblend[0].frame * vertcount;
+       lerp1 = ent->frameblend[0].lerp;
+       if (ent->frameblend[1].lerp)
        {
-               VectorScale(fscale2, lerp2, scale2);
-               if (lerp3)
+               verts2 = mesh->data_vertices + ent->frameblend[1].frame * vertcount;
+               lerp2 = ent->frameblend[1].lerp;
+               if (ent->frameblend[2].lerp)
                {
-                       VectorScale(fscale3, lerp3, scale3);
-                       if (lerp4)
+                       verts3 = mesh->data_vertices + ent->frameblend[2].frame * vertcount;
+                       lerp3 = ent->frameblend[2].lerp;
+                       if (ent->frameblend[3].lerp)
                        {
-                               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;
+                               verts4 = mesh->data_vertices + ent->frameblend[3].frame * vertcount;
+                               lerp4 = ent->frameblend[3].lerp;
                                // generate vertices
-                               for (i = 0;i < vertcount;i++)
+                               if (svectors != NULL)
+                               {
+                                       for (i = 0;i < vertcount;i++, vertices += 4, normals += 4, svectors += 4, tvectors += 4, verts1++, verts2++, verts3++, verts4++)
+                                       {
+                                               VectorMAMAMAM(lerp1, verts1->origin, lerp2, verts2->origin, lerp3, verts3->origin, lerp4, verts4->origin, vertices);
+                                               VectorMAMAMAM(lerp1, verts1->normal, lerp2, verts2->normal, lerp3, verts3->normal, lerp4, verts4->normal, normals);
+                                               VectorMAMAMAM(lerp1, verts1->svector, lerp2, verts2->svector, lerp3, verts3->svector, lerp4, verts4->svector, svectors);
+                                               CrossProduct(svectors, normals, tvectors);
+                                       }
+                               }
+                               else if (normals != NULL)
                                {
-                                       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++;
+                                       for (i = 0;i < vertcount;i++, vertices += 4, normals += 4, verts1++, verts2++, verts3++, verts4++)
+                                       {
+                                               VectorMAMAMAM(lerp1, verts1->origin, lerp2, verts2->origin, lerp3, verts3->origin, lerp4, verts4->origin, vertices);
+                                               VectorMAMAMAM(lerp1, verts1->normal, lerp2, verts2->normal, lerp3, verts3->normal, lerp4, verts4->normal, normals);
+                                       }
                                }
+                               else
+                                       for (i = 0;i < vertcount;i++, vertices += 4, verts1++, verts2++, verts3++, verts4++)
+                                               VectorMAMAMAM(lerp1, verts1->origin, lerp2, verts2->origin, lerp3, verts3->origin, lerp4, verts4->origin, vertices);
                        }
                        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++)
+                               if (svectors != NULL)
                                {
-                                       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++;
+                                       for (i = 0;i < vertcount;i++, vertices += 4, normals += 4, svectors += 4, tvectors += 4, verts1++, verts2++, verts3++)
+                                       {
+                                               VectorMAMAM(lerp1, verts1->origin, lerp2, verts2->origin, lerp3, verts3->origin, vertices);
+                                               VectorMAMAM(lerp1, verts1->normal, lerp2, verts2->normal, lerp3, verts3->normal, normals);
+                                               VectorMAMAM(lerp1, verts1->svector, lerp2, verts2->svector, lerp3, verts3->svector, svectors);
+                                               CrossProduct(svectors, normals, tvectors);
+                                       }
                                }
+                               else if (normals != NULL)
+                               {
+                                       for (i = 0;i < vertcount;i++, vertices += 4, normals += 4, verts1++, verts2++, verts3++)
+                                       {
+                                               VectorMAMAM(lerp1, verts1->origin, lerp2, verts2->origin, lerp3, verts3->origin, vertices);
+                                               VectorMAMAM(lerp1, verts1->normal, lerp2, verts2->normal, lerp3, verts3->normal, normals);
+                                       }
+                               }
+                               else
+                                       for (i = 0;i < vertcount;i++, vertices += 4, verts1++, verts2++, verts3++)
+                                               VectorMAMAM(lerp1, verts1->origin, lerp2, verts2->origin, lerp3, verts3->origin, vertices);
                        }
                }
                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++)
+                       if (svectors != NULL)
                        {
-                               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++;
+                               for (i = 0;i < vertcount;i++, vertices += 4, normals += 4, svectors += 4, tvectors += 4, verts1++, verts2++)
+                               {
+                                       VectorMAM(lerp1, verts1->origin, lerp2, verts2->origin, vertices);
+                                       VectorMAM(lerp1, verts1->normal, lerp2, verts2->normal, normals);
+                                       VectorMAM(lerp1, verts1->svector, lerp2, verts2->svector, svectors);
+                                       CrossProduct(svectors, normals, tvectors);
+                               }
                        }
+                       else if (normals != NULL)
+                       {
+                               for (i = 0;i < vertcount;i++, vertices += 4, normals += 4, verts1++, verts2++)
+                               {
+                                       VectorMAM(lerp1, verts1->origin, lerp2, verts2->origin, vertices);
+                                       VectorMAM(lerp1, verts1->normal, lerp2, verts2->normal, normals);
+                               }
+                       }
+                       else
+                               for (i = 0;i < vertcount;i++, vertices += 4, verts1++, verts2++)
+                                       VectorMAM(lerp1, verts1->origin, lerp2, verts2->origin, vertices);
                }
        }
        else
        {
-               translate[0] = translate1[0] * lerp1;
-               translate[1] = translate1[1] * lerp1;
-               translate[2] = translate1[2] * lerp1;
                // generate vertices
-               if (lerp1 != 1)
+               if (svectors != NULL)
                {
-                       // general but almost never used case
-                       for (i = 0;i < vertcount;i++)
+                       for (i = 0;i < vertcount;i++, vertices += 4, normals += 4, svectors += 4, tvectors += 4, verts1++)
                        {
-                               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++;
+                               VectorM(lerp1, verts1->origin, vertices);
+                               VectorM(lerp1, verts1->normal, normals);
+                               VectorM(lerp1, verts1->svector, svectors);
+                               CrossProduct(svectors, normals, tvectors);
                        }
                }
-               else
+               else if (normals != NULL)
                {
-                       // fast normal case
-                       for (i = 0;i < vertcount;i++)
+                       for (i = 0;i < vertcount;i++, vertices += 4, normals += 4, verts1++)
                        {
-                               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++;
+                               VectorM(lerp1, verts1->origin, vertices);
+                               VectorM(lerp1, verts1->normal, normals);
                        }
                }
+               else if (lerp1 != 1)
+               {
+                       for (i = 0;i < vertcount;i++, vertices += 4, verts1++)
+                               VectorM(lerp1, verts1->origin, vertices);
+               }
+               else
+                       for (i = 0;i < vertcount;i++, vertices += 4, verts1++)
+                               VectorCopy(verts1->origin, vertices);
        }
 }
 
-skinframe_t *R_FetchSkinFrame(const entity_render_t *ent)
+aliasskin_t *R_FetchAliasSkin(const entity_render_t *ent, const aliasmesh_t *mesh)
 {
        model_t *model = ent->model;
-       unsigned int s = (unsigned int) ent->skinnum;
-       if (s >= model->numskins)
+       int s = ent->skinnum;
+       if ((unsigned int)s >= (unsigned int)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];
+               s = model->skinscenes[s].firstframe + (int) (cl.time * model->skinscenes[s].framerate) % model->skinscenes[s].framecount;
        else
-               return &model->skinframes[model->skinscenes[s].firstframe];
-}
-
-void R_SetupMDLMD2Frames(const entity_render_t *ent, float colorr, float colorg, float colorb)
-{
-       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);
+               s = model->skinscenes[s].firstframe;
+       if (s >= mesh->num_skins)
+               s = 0;
+       return mesh->data_skins + s;
 }
 
-void R_DrawQ1Q2AliasModelCallback (const void *calldata1, int calldata2)
+void R_DrawAliasModelCallback (const void *calldata1, int calldata2)
 {
-       int i, c, pantsfullbright, shirtfullbright, colormapped;
-       float pantscolor[3], shirtcolor[3];
-       float fog;
+       int c, fullbright, layernum;
+       float tint[3], fog, ifog, colorscale;
        vec3_t diff;
        qbyte *bcolor;
-       rmeshbufferinfo_t m;
-       model_t *model;
-       skinframe_t *skinframe;
+       rmeshstate_t m;
        const entity_render_t *ent = calldata1;
-       int blendfunc1, blendfunc2;
+       aliasmesh_t *mesh = ent->model->aliasdata_meshes + calldata2;
+       aliaslayer_t *layer;
+       aliasskin_t *skin;
 
-//     softwaretransformforentity(ent);
+       R_Mesh_Matrix(&ent->matrix);
 
        fog = 0;
        if (fogenabled)
@@ -268,216 +229,281 @@ void R_DrawQ1Q2AliasModelCallback (const void *calldata1, int calldata2)
                // 1. render model as normal, scaled by inverse of fog alpha (darkens it)
                // 2. render fog as additive
        }
+       ifog = 1 - fog;
 
-       model = ent->model;
-
-       skinframe = R_FetchSkinFrame(ent);
-
-       if (ent->effects & EF_ADDITIVE)
-       {
-               blendfunc1 = GL_SRC_ALPHA;
-               blendfunc2 = GL_ONE;
-       }
-       else if (ent->alpha != 1.0 || skinframe->fog != NULL)
-       {
-               blendfunc1 = GL_SRC_ALPHA;
-               blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
-       }
-       else
+       memset(&m, 0, sizeof(m));
+       skin = R_FetchAliasSkin(ent, mesh);
+       for (layernum = 0, layer = skin->data_layers;layernum < skin->num_layers;layernum++, layer++)
        {
-               blendfunc1 = GL_ONE;
-               blendfunc2 = GL_ZERO;
+               if (((layer->flags & ALIASLAYER_NODRAW_IF_NOTCOLORMAPPED) && ent->colormap < 0)
+                || ((layer->flags & ALIASLAYER_NODRAW_IF_COLORMAPPED) && ent->colormap >= 0)
+                ||  (layer->flags & ALIASLAYER_DRAW_PER_LIGHT))
+                       continue;
+               if (layer->flags & ALIASLAYER_FOG)
+               {
+                       m.blendfunc1 = GL_SRC_ALPHA;
+                       m.blendfunc2 = GL_ONE;
+                       colorscale = r_colorscale;
+                       m.texrgbscale[0] = 1;
+                       m.tex[0] = R_GetTexture(layer->texture);
+                       R_Mesh_State(&m);
+                       GL_Color(fogcolor[0] * fog * colorscale, fogcolor[1] * fog * colorscale, fogcolor[2] * fog * colorscale, ent->alpha);
+                       c_alias_polys += mesh->num_triangles;
+                       R_Mesh_GetSpace(mesh->num_vertices);
+                       R_Model_Alias_GetMeshVerts(ent, mesh, varray_vertex, aliasvert_normals, NULL, NULL);
+                       memcpy(varray_texcoord[0], mesh->data_texcoords, mesh->num_vertices * sizeof(float[4]));
+                       R_Mesh_Draw(mesh->num_vertices, mesh->num_triangles, mesh->data_elements);
+                       continue;
+               }
+               if ((layer->flags & ALIASLAYER_ADD) || ((layer->flags & ALIASLAYER_ALPHA) && (ent->effects & EF_ADDITIVE)))
+               {
+                       m.blendfunc1 = GL_SRC_ALPHA;
+                       m.blendfunc2 = GL_ONE;
+               }
+               else if ((layer->flags & ALIASLAYER_ALPHA) || ent->alpha != 1.0)
+               {
+                       m.blendfunc1 = GL_SRC_ALPHA;
+                       m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
+               }
+               else
+               {
+                       m.blendfunc1 = GL_ONE;
+                       m.blendfunc2 = GL_ZERO;
+               }
+               colorscale = r_colorscale;
+               m.texrgbscale[0] = 1;
+               if (gl_combine.integer)
+               {
+                       colorscale *= 0.25f;
+                       m.texrgbscale[0] = 4;
+               }
+               m.tex[0] = R_GetTexture(layer->texture);
+               R_Mesh_State(&m);
+               if (layer->flags & ALIASLAYER_COLORMAP_PANTS)
+               {
+                       // 128-224 are backwards ranges
+                       c = (ent->colormap & 0xF) << 4;c += (c >= 128 && c < 224) ? 4 : 12;
+                       bcolor = (qbyte *) (&palette_complete[c]);
+                       fullbright = c >= 224;
+                       VectorScale(bcolor, (1.0f / 255.0f), tint);
+               }
+               else if (layer->flags & ALIASLAYER_COLORMAP_SHIRT)
+               {
+                       // 128-224 are backwards ranges
+                       c = (ent->colormap & 0xF0);c += (c >= 128 && c < 224) ? 4 : 12;
+                       bcolor = (qbyte *) (&palette_complete[c]);
+                       fullbright = c >= 224;
+                       VectorScale(bcolor, (1.0f / 255.0f), tint);
+               }
+               else
+               {
+                       tint[0] = tint[1] = tint[2] = 1;
+                       fullbright = false;
+               }
+               VectorScale(tint, ifog * colorscale, tint);
+               if (!(layer->flags & ALIASLAYER_DIFFUSE))
+                       fullbright = true;
+               if (ent->effects & EF_FULLBRIGHT)
+                       fullbright = true;
+               c_alias_polys += mesh->num_triangles;
+               R_Mesh_GetSpace(mesh->num_vertices);
+               R_Model_Alias_GetMeshVerts(ent, mesh, varray_vertex, aliasvert_normals, NULL, NULL);
+               memcpy(varray_texcoord[0], mesh->data_texcoords, mesh->num_vertices * sizeof(float[4]));
+               if (fullbright)
+                       GL_Color(tint[0], tint[1], tint[2], ent->alpha);
+               else
+                       R_LightModel(ent, mesh->num_vertices, varray_vertex, aliasvert_normals, varray_color, tint[0], tint[1], tint[2], false);
+               R_Mesh_Draw(mesh->num_vertices, mesh->num_triangles, mesh->data_elements);
        }
+}
 
-       if (!skinframe->base && !skinframe->pants && !skinframe->shirt && !skinframe->glow)
-       {
-               // untextured
-               memset(&m, 0, sizeof(m));
-               m.blendfunc1 = blendfunc1;
-               m.blendfunc2 = blendfunc2;
-               m.numtriangles = model->numtris;
-               m.numverts = model->numverts;
-               m.tex[0] = R_GetTexture(r_notexture);
-               m.matrix = ent->matrix;
-
-               c_alias_polys += m.numtriangles;
-               if (R_Mesh_Draw_GetBuffer(&m, true))
-               {
-                       memcpy(m.index, model->mdlmd2data_indices, m.numtriangles * sizeof(int[3]));
-                       for (i = 0;i < m.numverts * 2;i++)
-                               m.texcoords[0][i] = model->mdlmd2data_texcoords[i] * 8.0f;
+void R_Model_Alias_Draw(entity_render_t *ent)
+{
+       int meshnum;
+       aliasmesh_t *mesh;
+       if (ent->alpha < (1.0f / 64.0f))
+               return; // basically completely transparent
 
-                       aliasvert = m.vertex;
-                       aliasvertcolor = m.color;
-                       R_SetupMDLMD2Frames(ent, m.colorscale, m.colorscale, m.colorscale);
-                       aliasvert = aliasvertbuf;
-                       aliasvertcolor = aliasvertcolorbuf;
+       c_models++;
 
-                       R_Mesh_Render();
-               }
-               return;
+       for (meshnum = 0, mesh = ent->model->aliasdata_meshes;meshnum < ent->model->aliasnum_meshes;meshnum++, mesh++)
+       {
+               if (ent->effects & EF_ADDITIVE || ent->alpha != 1.0 || R_FetchAliasSkin(ent, mesh)->flags & ALIASSKIN_TRANSPARENT)
+                       R_MeshQueue_AddTransparent(ent->origin, R_DrawAliasModelCallback, ent, meshnum);
+               else
+                       R_DrawAliasModelCallback(ent, meshnum);
        }
+}
 
+void R_Model_Alias_DrawFakeShadow (entity_render_t *ent)
+{
+       int i, meshnum;
+       aliasmesh_t *mesh;
+       aliasskin_t *skin;
+       rmeshstate_t m;
+       float *v, planenormal[3], planedist, dist, projection[3], floororigin[3], surfnormal[3], lightdirection[3], v2[3];
 
-       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;
-               m.numtriangles = model->numtris;
-               m.numverts = model->numverts;
-               m.tex[0] = R_GetTexture(skinframe->merged);
-               m.matrix = ent->matrix;
-
-               c_alias_polys += m.numtriangles;
-               if (R_Mesh_Draw_GetBuffer(&m, true))
-               {
-                       memcpy(m.index, model->mdlmd2data_indices, m.numtriangles * sizeof(int[3]));
-                       memcpy(m.texcoords[0], model->mdlmd2data_texcoords, m.numverts * sizeof(float[2]));
+       if ((ent->effects & EF_ADDITIVE) || ent->alpha < 1)
+               return;
 
-                       aliasvert = m.vertex;
-                       aliasvertcolor = m.color;
-                       R_SetupMDLMD2Frames(ent, m.colorscale, m.colorscale, m.colorscale);
-                       aliasvert = aliasvertbuf;
-                       aliasvertcolor = aliasvertcolorbuf;
+       lightdirection[0] = 0.5;
+       lightdirection[1] = 0.2;
+       lightdirection[2] = -1;
+       VectorNormalizeFast(lightdirection);
 
-                       R_Mesh_Render();
-               }
+       VectorMA(ent->origin, 65536.0f, lightdirection, v2);
+       if (CL_TraceLine(ent->origin, v2, floororigin, surfnormal, 0, false, NULL) == 1)
                return;
-       }
 
-       R_SetupMDLMD2Frames(ent, 1 - fog, 1 - fog, 1 - fog);
+       R_Mesh_Matrix(&ent->matrix);
 
-       if (colormapped)
+       memset(&m, 0, sizeof(m));
+       m.blendfunc1 = GL_SRC_ALPHA;
+       m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
+       R_Mesh_State(&m);
+       GL_Color(0, 0, 0, 0.5);
+
+       // put a light direction in the entity's coordinate space
+       Matrix4x4_Transform3x3(&ent->inversematrix, lightdirection, projection);
+       VectorNormalizeFast(projection);
+
+       // put the plane's normal in the entity's coordinate space
+       Matrix4x4_Transform3x3(&ent->inversematrix, surfnormal, planenormal);
+       VectorNormalizeFast(planenormal);
+
+       // put the plane's distance in the entity's coordinate space
+       VectorSubtract(floororigin, ent->origin, floororigin);
+       planedist = DotProduct(floororigin, surfnormal) + 2;
+
+       dist = -1.0f / DotProduct(projection, planenormal);
+       VectorScale(projection, dist, projection);
+       for (meshnum = 0, mesh = ent->model->aliasdata_meshes;meshnum < ent->model->aliasnum_meshes;meshnum++)
        {
-               // 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);
+               skin = R_FetchAliasSkin(ent, mesh);
+               if (skin->flags & ALIASSKIN_TRANSPARENT)
+                       continue;
+               R_Mesh_GetSpace(mesh->num_vertices);
+               R_Model_Alias_GetMeshVerts(ent, mesh, varray_vertex, NULL, NULL, NULL);
+               for (i = 0, v = varray_vertex;i < mesh->num_vertices;i++, v += 4)
+               {
+                       dist = DotProduct(v, planenormal) - planedist;
+                       if (dist > 0)
+                               VectorMA(v, dist, projection, v);
+               }
+               c_alias_polys += mesh->num_triangles;
+               R_Mesh_Draw(mesh->num_vertices, mesh->num_triangles, mesh->data_elements);
        }
-       else
+}
+
+void R_Model_Alias_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius)
+{
+       int meshnum;
+       aliasmesh_t *mesh;
+       aliasskin_t *skin;
+       float projectdistance;
+       if (ent->effects & EF_ADDITIVE || ent->alpha < 1)
+               return;
+       projectdistance = lightradius + ent->model->radius - sqrt(DotProduct(relativelightorigin, relativelightorigin));
+       if (projectdistance > 0.1)
        {
-               pantscolor[0] = pantscolor[1] = pantscolor[2] = shirtcolor[0] = shirtcolor[1] = shirtcolor[2] = 1;
-               pantsfullbright = shirtfullbright = false;
+               R_Mesh_Matrix(&ent->matrix);
+               for (meshnum = 0, mesh = ent->model->aliasdata_meshes;meshnum < ent->model->aliasnum_meshes;meshnum++, mesh++)
+               {
+                       skin = R_FetchAliasSkin(ent, mesh);
+                       if (skin->flags & ALIASSKIN_TRANSPARENT)
+                               continue;
+                       R_Mesh_GetSpace(mesh->num_vertices * 2);
+                       R_Model_Alias_GetMeshVerts(ent, mesh, varray_vertex, NULL, NULL, NULL);
+                       R_Shadow_Volume(mesh->num_vertices, mesh->num_triangles, mesh->data_elements, mesh->data_neighbors, relativelightorigin, lightradius, projectdistance);
+               }
        }
+}
 
-       memset(&m, 0, sizeof(m));
-       m.blendfunc1 = blendfunc1;
-       m.blendfunc2 = blendfunc2;
-       m.numtriangles = model->numtris;
-       m.numverts = model->numverts;
-       m.matrix = ent->matrix;
-       m.tex[0] = colormapped ? R_GetTexture(skinframe->base) : R_GetTexture(skinframe->merged);
-       if (m.tex[0] && R_Mesh_Draw_GetBuffer(&m, true))
+void R_Model_Alias_DrawLight(entity_render_t *ent, vec3_t relativelightorigin, vec3_t relativeeyeorigin, float lightradius, float *lightcolor, const matrix4x4_t *matrix_modeltofilter, const matrix4x4_t *matrix_modeltoattenuationxyz, const matrix4x4_t *matrix_modeltoattenuationz)
+{
+       int c, meshnum, layernum;
+       float fog, ifog, lightcolor2[3], *vertices;
+       vec3_t diff;
+       qbyte *bcolor;
+       aliasmesh_t *mesh;
+       aliaslayer_t *layer;
+       aliasskin_t *skin;
+
+       if (ent->effects & (EF_ADDITIVE | EF_FULLBRIGHT) || ent->alpha < 1)
+               return;
+
+       R_Mesh_Matrix(&ent->matrix);
+
+       fog = 0;
+       if (fogenabled)
        {
-               blendfunc1 = GL_SRC_ALPHA;
-               blendfunc2 = GL_ONE;
-               c_alias_polys += m.numtriangles;
-               R_ModulateColors(aliasvertcolor, m.color, m.numverts, m.colorscale, m.colorscale, m.colorscale);
-               memcpy(m.index, model->mdlmd2data_indices, m.numtriangles * sizeof(int[3]));
-               memcpy(m.vertex, aliasvert, m.numverts * sizeof(float[4]));
-               memcpy(m.texcoords[0], model->mdlmd2data_texcoords, m.numverts * sizeof(float[2]));
-               R_Mesh_Render();
+               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
        }
+       ifog = 1 - fog;
 
-       if (colormapped)
+       for (meshnum = 0, mesh = ent->model->aliasdata_meshes;meshnum < ent->model->aliasnum_meshes;meshnum++, mesh++)
        {
-               if (skinframe->pants)
+               skin = R_FetchAliasSkin(ent, mesh);
+               if (skin->flags & ALIASSKIN_TRANSPARENT)
+                       continue;
+               vertices = R_Shadow_VertexBuffer(mesh->num_vertices);
+               R_Model_Alias_GetMeshVerts(ent, mesh, vertices, aliasvert_normals, aliasvert_svectors, aliasvert_tvectors);
+               for (layernum = 0, layer = skin->data_layers;layernum < skin->num_layers;layernum++, layer++)
                {
-                       memset(&m, 0, sizeof(m));
-                       m.blendfunc1 = blendfunc1;
-                       m.blendfunc2 = blendfunc2;
-                       m.numtriangles = model->numtris;
-                       m.numverts = model->numverts;
-                       m.matrix = ent->matrix;
-                       m.tex[0] = R_GetTexture(skinframe->pants);
-                       if (m.tex[0] && R_Mesh_Draw_GetBuffer(&m, true))
+                       if (!(layer->flags & ALIASLAYER_DRAW_PER_LIGHT)
+                        || ((layer->flags & ALIASLAYER_NODRAW_IF_NOTCOLORMAPPED) && ent->colormap < 0)
+                        || ((layer->flags & ALIASLAYER_NODRAW_IF_COLORMAPPED) && ent->colormap >= 0))
+                               continue;
+                       lightcolor2[0] = lightcolor[0] * ifog;
+                       lightcolor2[1] = lightcolor[1] * ifog;
+                       lightcolor2[2] = lightcolor[2] * ifog;
+                       if (layer->flags & ALIASLAYER_SPECULAR)
                        {
-                               blendfunc1 = GL_SRC_ALPHA;
-                               blendfunc2 = GL_ONE;
-                               c_alias_polys += m.numtriangles;
-                               if (pantsfullbright)
-                                       R_FillColors(m.color, m.numverts, pantscolor[0] * m.colorscale, pantscolor[1] * m.colorscale, pantscolor[2] * m.colorscale, ent->alpha);
-                               else
-                                       R_ModulateColors(aliasvertcolor, m.color, m.numverts, pantscolor[0] * m.colorscale, pantscolor[1] * m.colorscale, pantscolor[2] * m.colorscale);
-                               memcpy(m.index, model->mdlmd2data_indices, m.numtriangles * sizeof(int[3]));
-                               memcpy(m.vertex, aliasvert, m.numverts * sizeof(float[4]));
-                               memcpy(m.texcoords[0], model->mdlmd2data_texcoords, m.numverts * sizeof(float[2]));
-                               R_Mesh_Render();
+                               c_alias_polys += mesh->num_triangles;
+                               R_Shadow_SpecularLighting(mesh->num_vertices, mesh->num_triangles, mesh->data_elements, vertices, aliasvert_svectors, aliasvert_tvectors, aliasvert_normals, mesh->data_texcoords, relativelightorigin, relativeeyeorigin, lightradius, lightcolor2, matrix_modeltofilter, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, layer->texture, layer->nmap, NULL);
                        }
-               }
-               if (skinframe->shirt)
-               {
-                       memset(&m, 0, sizeof(m));
-                       m.blendfunc1 = blendfunc1;
-                       m.blendfunc2 = blendfunc2;
-                       m.numtriangles = model->numtris;
-                       m.numverts = model->numverts;
-                       m.matrix = ent->matrix;
-                       m.tex[0] = R_GetTexture(skinframe->shirt);
-                       if (m.tex[0] && R_Mesh_Draw_GetBuffer(&m, true))
+                       else if (layer->flags & ALIASLAYER_DIFFUSE)
                        {
-                               blendfunc1 = GL_SRC_ALPHA;
-                               blendfunc2 = GL_ONE;
-                               c_alias_polys += m.numtriangles;
-                               if (shirtfullbright)
-                                       R_FillColors(m.color, m.numverts, shirtcolor[0] * m.colorscale, shirtcolor[1] * m.colorscale, shirtcolor[2] * m.colorscale, ent->alpha);
-                               else
-                                       R_ModulateColors(aliasvertcolor, m.color, m.numverts, shirtcolor[0] * m.colorscale, shirtcolor[1] * m.colorscale, shirtcolor[2] * m.colorscale);
-                               memcpy(m.index, model->mdlmd2data_indices, m.numtriangles * sizeof(int[3]));
-                               memcpy(m.vertex, aliasvert, m.numverts * sizeof(float[4]));
-                               memcpy(m.texcoords[0], model->mdlmd2data_texcoords, m.numverts * sizeof(float[2]));
-                               R_Mesh_Render();
+                               if (layer->flags & ALIASLAYER_COLORMAP_PANTS)
+                               {
+                                       // 128-224 are backwards ranges
+                                       c = (ent->colormap & 0xF) << 4;c += (c >= 128 && c < 224) ? 4 : 12;
+                                       // fullbright passes were already taken care of, so skip them in realtime lighting passes
+                                       if (c >= 224)
+                                               continue;
+                                       bcolor = (qbyte *) (&palette_complete[c]);
+                                       lightcolor2[0] *= bcolor[0] * (1.0f / 255.0f);
+                                       lightcolor2[1] *= bcolor[1] * (1.0f / 255.0f);
+                                       lightcolor2[2] *= bcolor[2] * (1.0f / 255.0f);
+                               }
+                               else if (layer->flags & ALIASLAYER_COLORMAP_SHIRT)
+                               {
+                                       // 128-224 are backwards ranges
+                                       c = (ent->colormap & 0xF0);c += (c >= 128 && c < 224) ? 4 : 12;
+                                       // fullbright passes were already taken care of, so skip them in realtime lighting passes
+                                       if (c >= 224)
+                                               continue;
+                                       bcolor = (qbyte *) (&palette_complete[c]);
+                                       lightcolor2[0] *= bcolor[0] * (1.0f / 255.0f);
+                                       lightcolor2[1] *= bcolor[1] * (1.0f / 255.0f);
+                                       lightcolor2[2] *= bcolor[2] * (1.0f / 255.0f);
+                               }
+                               c_alias_polys += mesh->num_triangles;
+                               R_Shadow_DiffuseLighting(mesh->num_vertices, mesh->num_triangles, mesh->data_elements, vertices, aliasvert_svectors, aliasvert_tvectors, aliasvert_normals, mesh->data_texcoords, relativelightorigin, lightradius, lightcolor2, matrix_modeltofilter, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, layer->texture, layer->nmap, NULL);
                        }
                }
        }
-       if (skinframe->glow)
-       {
-               memset(&m, 0, sizeof(m));
-               m.blendfunc1 = blendfunc1;
-               m.blendfunc2 = blendfunc2;
-               m.numtriangles = model->numtris;
-               m.numverts = model->numverts;
-               m.matrix = ent->matrix;
-               m.tex[0] = R_GetTexture(skinframe->glow);
-               if (m.tex[0] && R_Mesh_Draw_GetBuffer(&m, true))
-               {
-                       blendfunc1 = GL_SRC_ALPHA;
-                       blendfunc2 = GL_ONE;
-                       c_alias_polys += m.numtriangles;
-                       R_FillColors(m.color, m.numverts, (1 - fog) * m.colorscale, (1 - fog) * m.colorscale, (1 - fog) * m.colorscale, ent->alpha);
-                       memcpy(m.index, model->mdlmd2data_indices, m.numtriangles * sizeof(int[3]));
-                       memcpy(m.vertex, aliasvert, m.numverts * sizeof(float[4]));
-                       memcpy(m.texcoords[0], model->mdlmd2data_texcoords, m.numverts * sizeof(float[2]));
-                       R_Mesh_Render();
-               }
-       }
-       if (fog)
-       {
-               memset(&m, 0, sizeof(m));
-               m.blendfunc1 = GL_SRC_ALPHA;
-               m.blendfunc2 = GL_ONE;
-               m.numtriangles = model->numtris;
-               m.numverts = model->numverts;
-               m.matrix = ent->matrix;
-               m.tex[0] = R_GetTexture(skinframe->fog);
-               if (m.tex[0] && R_Mesh_Draw_GetBuffer(&m, true))
-               {
-                       c_alias_polys += m.numtriangles;
-                       R_FillColors(m.color, m.numverts, fogcolor[0] * fog * m.colorscale, fogcolor[1] * fog * m.colorscale, fogcolor[2] * fog * m.colorscale, ent->alpha);
-                       memcpy(m.index, model->mdlmd2data_indices, m.numtriangles * sizeof(int[3]));
-                       memcpy(m.vertex, aliasvert, m.numverts * sizeof(float[4]));
-                       memcpy(m.texcoords[0], model->mdlmd2data_texcoords, m.numverts * sizeof(float[2]));
-                       R_Mesh_Render();
-               }
-       }
 }
 
 int ZymoticLerpBones(int count, const zymbonematrix *bonebase, const frameblend_t *blend, const zymbone_t *bone)
@@ -487,21 +513,6 @@ int ZymoticLerpBones(int count, const zymbonematrix *bonebase, const frameblend_
        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;
@@ -659,10 +670,10 @@ int ZymoticLerpBones(int count, const zymbonematrix *bonebase, const frameblend_
        return true;
 }
 
-void ZymoticTransformVerts(int vertcount, int *bonecounts, zymvertex_t *vert)
+void ZymoticTransformVerts(int vertcount, float *vertex, int *bonecounts, zymvertex_t *vert)
 {
        int c;
-       float *out = aliasvert;
+       float *out = vertex;
        zymbonematrix *matrix;
        while(vertcount--)
        {
@@ -693,13 +704,13 @@ void ZymoticTransformVerts(int vertcount, int *bonecounts, zymvertex_t *vert)
        }
 }
 
-void ZymoticCalcNormals(int vertcount, int shadercount, int *renderlist)
+void ZymoticCalcNormals(int vertcount, float *vertex, float *normals, int shadercount, int *renderlist)
 {
        int a, b, c, d;
        float *out, v1[3], v2[3], normal[3], s;
        int *u;
        // clear normals
-       memset(aliasvertnorm, 0, sizeof(float) * vertcount * 3);
+       memset(normals, 0, sizeof(float) * vertcount * 3);
        memset(aliasvertusage, 0, sizeof(int) * vertcount);
        // parse render list and accumulate surface normals
        while(shadercount--)
@@ -710,36 +721,36 @@ void ZymoticCalcNormals(int vertcount, int shadercount, int *renderlist)
                        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];
-                       v2[0] = aliasvert[c+0] - aliasvert[b+0];
-                       v2[1] = aliasvert[c+1] - aliasvert[b+1];
-                       v2[2] = aliasvert[c+2] - aliasvert[b+2];
+                       v1[0] = vertex[a+0] - vertex[b+0];
+                       v1[1] = vertex[a+1] - vertex[b+1];
+                       v1[2] = vertex[a+2] - vertex[b+2];
+                       v2[0] = vertex[c+0] - vertex[b+0];
+                       v2[1] = vertex[c+1] - vertex[b+1];
+                       v2[2] = vertex[c+2] - vertex[b+2];
                        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];
+                       normals[a+0] += normal[0];
+                       normals[a+1] += normal[1];
+                       normals[a+2] += normal[2];
                        aliasvertusage[renderlist[0]]++;
                        a = renderlist[1] * 3;
-                       aliasvertnorm[a+0] += normal[0];
-                       aliasvertnorm[a+1] += normal[1];
-                       aliasvertnorm[a+2] += normal[2];
+                       normals[a+0] += normal[0];
+                       normals[a+1] += normal[1];
+                       normals[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];
+                       normals[a+0] += normal[0];
+                       normals[a+1] += normal[1];
+                       normals[a+2] += normal[2];
                        aliasvertusage[renderlist[2]]++;
                        renderlist += 3;
                }
        }
        // FIXME: precalc this
        // average surface normals
-       out = aliasvertnorm;
+       out = normals;
        u = aliasvertusage;
        while(vertcount--)
        {
@@ -757,21 +768,26 @@ void ZymoticCalcNormals(int vertcount, int shadercount, int *renderlist)
 
 void R_DrawZymoticModelMeshCallback (const void *calldata1, int calldata2)
 {
-       float fog;
+       float fog, ifog, colorscale;
        vec3_t diff;
-       int i, *renderlist;
-       zymtype1header_t *m;
+       int i, *renderlist, *elements;
        rtexture_t *texture;
-       rmeshbufferinfo_t mbuf;
+       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);
+       renderlist = ent->model->zymdata_renderlist;
        for (i = 0;i < shadernum;i++)
                renderlist += renderlist[0] * 3 + 1;
-       texture = ((rtexture_t **)(m->lump_shaders.start + (int) m))[shadernum];
+       texture = ent->model->zymdata_textures[shadernum];
+
+       numverts = ent->model->zymnum_verts;
+       numtriangles = *renderlist++;
+       elements = renderlist;
 
        fog = 0;
        if (fogenabled)
@@ -789,98 +805,87 @@ void R_DrawZymoticModelMeshCallback (const void *calldata1, int calldata2)
                // 1. render model as normal, scaled by inverse of fog alpha (darkens it)
                // 2. render fog as additive
        }
+       ifog = 1 - fog;
 
-       ZymoticLerpBones(m->numbones, (zymbonematrix *)(m->lump_poses.start + (int) m), ent->frameblend, (zymbone_t *)(m->lump_bones.start + (int) m));
-       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));
-
-       R_LightModel(ent, m->numverts, 1 - fog, 1 - fog, 1 - fog, false);
-
-       memset(&mbuf, 0, sizeof(mbuf));
-       mbuf.numverts = m->numverts;
-       mbuf.numtriangles = renderlist[0];
+       memset(&mstate, 0, sizeof(mstate));
        if (ent->effects & EF_ADDITIVE)
        {
-               mbuf.blendfunc1 = GL_SRC_ALPHA;
-               mbuf.blendfunc2 = GL_ONE;
+               mstate.blendfunc1 = GL_SRC_ALPHA;
+               mstate.blendfunc2 = GL_ONE;
        }
        else if (ent->alpha != 1.0 || R_TextureHasAlpha(texture))
        {
-               mbuf.blendfunc1 = GL_SRC_ALPHA;
-               mbuf.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
+               mstate.blendfunc1 = GL_SRC_ALPHA;
+               mstate.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
        }
        else
        {
-               mbuf.blendfunc1 = GL_ONE;
-               mbuf.blendfunc2 = GL_ZERO;
+               mstate.blendfunc1 = GL_ONE;
+               mstate.blendfunc2 = GL_ZERO;
        }
-       mbuf.tex[0] = R_GetTexture(texture);
-       mbuf.matrix = ent->matrix;
-       if (R_Mesh_Draw_GetBuffer(&mbuf, true))
+       colorscale = r_colorscale;
+       if (gl_combine.integer)
        {
-               c_alias_polys += mbuf.numtriangles;
-               memcpy(mbuf.index, renderlist + 1, mbuf.numtriangles * sizeof(int[3]));
-               memcpy(mbuf.vertex, aliasvert, mbuf.numverts * sizeof(float[4]));
-               R_ModulateColors(aliasvertcolor, mbuf.color, mbuf.numverts, mbuf.colorscale, mbuf.colorscale, mbuf.colorscale);
-               //memcpy(mbuf.color, aliasvertcolor, mbuf.numverts * sizeof(float[4]));
-               memcpy(mbuf.texcoords[0], (float *)(m->lump_texcoords.start + (int) m), mbuf.numverts * sizeof(float[2]));
-               R_Mesh_Render();
+               mstate.texrgbscale[0] = 4;
+               colorscale *= 0.25f;
        }
+       mstate.tex[0] = R_GetTexture(texture);
+       R_Mesh_State(&mstate);
+       ZymoticLerpBones(ent->model->zymnum_bones, (zymbonematrix *) ent->model->zymdata_poses, ent->frameblend, ent->model->zymdata_bones);
+
+       R_Mesh_GetSpace(numverts);
+       ZymoticTransformVerts(numverts, varray_vertex, ent->model->zymdata_vertbonecounts, ent->model->zymdata_verts);
+       memcpy(varray_texcoord[0], ent->model->zymdata_texcoords, ent->model->zymnum_verts * sizeof(float[4]));
+       ZymoticCalcNormals(numverts, varray_vertex, aliasvert_normals, ent->model->zymnum_shaders, ent->model->zymdata_renderlist);
+       R_LightModel(ent, numverts, varray_vertex, aliasvert_normals, varray_color, ifog * colorscale, ifog * colorscale, ifog * colorscale, false);
+       R_Mesh_Draw(numverts, numtriangles, elements);
+       c_alias_polys += numtriangles;
 
        if (fog)
        {
-               memset(&mbuf, 0, sizeof(mbuf));
-               mbuf.numverts = m->numverts;
-               mbuf.numtriangles = renderlist[0];
-               mbuf.blendfunc1 = GL_SRC_ALPHA;
-               mbuf.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
+               memset(&mstate, 0, sizeof(mstate));
+               mstate.blendfunc1 = GL_SRC_ALPHA;
+               mstate.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
                // FIXME: need alpha mask for fogging...
-               //mbuf.tex[0] = R_GetTexture(texture);
-               mbuf.matrix = ent->matrix;
-               if (R_Mesh_Draw_GetBuffer(&mbuf, false))
-               {
-                       c_alias_polys += mbuf.numtriangles;
-                       memcpy(mbuf.index, renderlist + 1, mbuf.numtriangles * sizeof(int[3]));
-                       memcpy(mbuf.vertex, aliasvert, mbuf.numverts * sizeof(float[4]));
-                       R_FillColors(mbuf.color, mbuf.numverts, fogcolor[0] * mbuf.colorscale, fogcolor[1] * mbuf.colorscale, fogcolor[2] * mbuf.colorscale, ent->alpha * fog);
-                       //memcpy(mbuf.texcoords[0], (float *)(m->lump_texcoords.start + (int) m), mbuf.numverts * sizeof(float[2]));
-                       R_Mesh_Render();
-               }
+               //mstate.tex[0] = R_GetTexture(texture);
+               R_Mesh_State(&mstate);
+               GL_Color(fogcolor[0] * r_colorscale, fogcolor[1] * r_colorscale, fogcolor[2] * r_colorscale, ent->alpha * fog);
+               R_Mesh_GetSpace(numverts);
+               ZymoticTransformVerts(numverts, varray_vertex, ent->model->zymdata_vertbonecounts, ent->model->zymdata_verts);
+               R_Mesh_Draw(numverts, numtriangles, elements);
+               c_alias_polys += numtriangles;
        }
 }
 
-void R_DrawZymoticModel (entity_render_t *ent)
+void R_Model_Zymotic_Draw(entity_render_t *ent)
 {
        int i;
-       zymtype1header_t *m;
-       rtexture_t *texture;
 
        if (ent->alpha < (1.0f / 64.0f))
                return; // basically completely transparent
 
        c_models++;
 
-       m = ent->model->zymdata_header;
-       for (i = 0;i < m->numshaders;i++)
+       for (i = 0;i < ent->model->zymnum_shaders;i++)
        {
-               texture = ((rtexture_t **)(m->lump_shaders.start + (int) m))[i];
-               if (ent->effects & EF_ADDITIVE || ent->alpha != 1.0 || R_TextureHasAlpha(texture))
+               if (ent->effects & EF_ADDITIVE || ent->alpha != 1.0 || R_TextureHasAlpha(ent->model->zymdata_textures[i]))
                        R_MeshQueue_AddTransparent(ent->origin, R_DrawZymoticModelMeshCallback, ent, i);
                else
-                       R_MeshQueue_Add(R_DrawZymoticModelMeshCallback, ent, i);
+                       R_DrawZymoticModelMeshCallback(ent, i);
        }
 }
 
-void R_DrawQ1Q2AliasModel(entity_render_t *ent)
+void R_Model_Zymotic_DrawFakeShadow(entity_render_t *ent)
 {
-       if (ent->alpha < (1.0f / 64.0f))
-               return; // basically completely transparent
-
-       c_models++;
+       // FIXME
+}
 
-       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_MeshQueue_Add(R_DrawQ1Q2AliasModelCallback, ent, 0);
+void R_Model_Zymotic_DrawLight(entity_render_t *ent, vec3_t relativelightorigin, float lightradius2, float lightdistbias, float lightsubtract, float *lightcolor)
+{
+       // FIXME
 }
 
+void R_Model_Zymotic_DrawOntoLight(entity_render_t *ent)
+{
+       // FIXME
+}