]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - gl_models.c
transform matrix is now separate from R_Mesh_State, a new function called R_Mesh_Matr...
[xonotic/darkplaces.git] / gl_models.c
index 47efa6d27a32090b6c7d285f0f8e10dd11c280e5..38064f11425ccf43dd950384777f89f1e3190e2a 100644 (file)
@@ -20,8 +20,6 @@ float *aliasvertnorm;
 int *aliasvertusage;
 zymbonematrix *zymbonepose;
 
-rmeshinfo_t aliasmeshinfo;
-
 mempool_t *gl_models_mempool;
 
 void gl_models_start(void)
@@ -52,6 +50,7 @@ void GL_Models_Init(void)
        R_RegisterModule("GL_Models", gl_models_start, gl_models_shutdown, gl_models_newmap);
 }
 
+/*
 void R_AliasTransformVerts(int vertcount)
 {
        vec3_t point;
@@ -71,16 +70,17 @@ void R_AliasTransformVerts(int vertcount)
                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)
+               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;
@@ -200,312 +200,281 @@ 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);
-       R_Mesh_Render();
-
-       // 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)
-{
-       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;
-       }
-}
-
-skinframe_t *R_FetchSkinFrame(void)
+skinframe_t *R_FetchSkinFrame(const entity_render_t *ent)
 {
-       model_t *model = currentrenderentity->model;
-       if (model->skinscenes[currentrenderentity->skinnum].framecount > 1)
-               return &model->skinframes[model->skinscenes[currentrenderentity->skinnum].firstframe + (int) (cl.time * 10) % model->skinscenes[currentrenderentity->skinnum].framecount];
+       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[currentrenderentity->skinnum].firstframe];
+               return &model->skinframes[model->skinscenes[s].firstframe];
 }
 
-void R_SetupMDLMD2Frames(float colorr, float colorg, float colorb)
+void R_SetupMDLMD2Frames(const entity_render_t *ent, float colorr, float colorg, float colorb)
 {
-       md2frame_t *frame1, *frame2, *frame3, *frame4;
-       trivertx_t *frame1verts, *frame2verts, *frame3verts, *frame4verts;
-       model_t *model;
-       model = currentrenderentity->model;
-
-       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];
+       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,
-               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);
+               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(model->numverts, colorr, colorg, colorb, false);
+       R_LightModel(ent, model->numverts, colorr, colorg, colorb, false);
 
-       R_AliasTransformVerts(model->numverts);
+       //R_AliasTransformVerts(model->numverts);
 }
 
-void R_DrawQ1Q2AliasModel (float fog)
+void R_DrawQ1Q2AliasModelCallback (const void *calldata1, int calldata2)
 {
+       int i, c, pantsfullbright, shirtfullbright, colormapped;
+       float pantscolor[3], shirtcolor[3];
+       float fog;
+       vec3_t diff;
+       qbyte *bcolor;
+       rmeshstate_t m;
        model_t *model;
        skinframe_t *skinframe;
+       const entity_render_t *ent = calldata1;
+       int blendfunc1, blendfunc2;
 
-       model = currentrenderentity->model;
+//     softwaretransformforentity(ent);
+       R_Mesh_Matrix(&ent->matrix);
 
-       skinframe = R_FetchSkinFrame();
-       if (fog && !(currentrenderentity->effects & EF_ADDITIVE))
+       fog = 0;
+       if (fogenabled)
        {
-               R_SetupMDLMD2Frames(1 - fog, 1 - fog, 1 - fog);
-
-               memset(&aliasmeshinfo, 0, sizeof(aliasmeshinfo));
+               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
+       }
 
-               aliasmeshinfo.vertex = aliasvert;
-               aliasmeshinfo.vertexstep = sizeof(float[4]);
-               aliasmeshinfo.numverts = model->numverts;
-               aliasmeshinfo.numtriangles = model->numtris;
-               aliasmeshinfo.index = model->mdlmd2data_indices;
-               aliasmeshinfo.colorstep = sizeof(float[4]);
-               aliasmeshinfo.texcoords[0] = model->mdlmd2data_texcoords;
-               aliasmeshinfo.texcoordstep[0] = sizeof(float[2]);
+       model = ent->model;
+       R_Mesh_ResizeCheck(model->numverts, model->numtris);
 
-               if (currentrenderentity->effects & EF_ADDITIVE)
-               {
-                       aliasmeshinfo.transparent = true;
-                       aliasmeshinfo.blendfunc1 = GL_SRC_ALPHA;
-                       aliasmeshinfo.blendfunc2 = GL_ONE;
-               }
-               else if (currentrenderentity->alpha != 1.0 || skinframe->fog != NULL)
-               {
-                       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;
-               }
+       skinframe = R_FetchSkinFrame(ent);
 
-               if (skinframe->base || skinframe->pants || skinframe->shirt || skinframe->glow || skinframe->merged)
-               {
-                       if (currentrenderentity->colormap >= 0 && (skinframe->base || skinframe->pants || skinframe->shirt))
-                       {
-                               int c;
-                               qbyte *color;
-                               if (skinframe->base)
-                                       R_DrawModelMesh(skinframe->base, aliasvertcolor, 0, 0, 0);
-                               if (skinframe->pants)
-                               {
-                                       c = (currentrenderentity->colormap & 0xF) << 4;c += (c >= 128 && c < 224) ? 4 : 12; // 128-224 are backwards ranges
-                                       color = (qbyte *) (&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));
-                                       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);
-                                       }
-                               }
-                               if (skinframe->shirt)
-                               {
-                                       c = currentrenderentity->colormap & 0xF0      ;c += (c >= 128 && c < 224) ? 4 : 12; // 128-224 are backwards ranges
-                                       color = (qbyte *) (&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));
-                                       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);
-                                       }
-                               }
-                       }
-                       else
-                       {
-                               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);
-                               }
-                       }
-                       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 (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
+       {
+               blendfunc1 = GL_ONE;
+               blendfunc2 = GL_ZERO;
+       }
 
-               aliasmeshinfo.tex[0] = R_GetTexture(skinframe->fog);
-               aliasmeshinfo.blendfunc1 = GL_SRC_ALPHA;
-               aliasmeshinfo.blendfunc2 = GL_ONE;
-               aliasmeshinfo.color = NULL;
+       if (!skinframe->base && !skinframe->pants && !skinframe->shirt && !skinframe->glow)
+       {
+               // untextured
+               memset(&m, 0, sizeof(m));
+               m.blendfunc1 = blendfunc1;
+               m.blendfunc2 = blendfunc2;
+               m.wantoverbright = true;
+               m.tex[0] = R_GetTexture(r_notexture);
+               R_Mesh_State(&m);
+
+               c_alias_polys += model->numtris;
+               memcpy(varray_element, model->mdlmd2data_indices, model->numtris * sizeof(int[3]));
+               for (i = 0;i < model->numverts * 2;i++)
+                       varray_texcoord[0][i] = model->mdlmd2data_texcoords[i] * 8.0f;
+               aliasvert = varray_vertex;
+               aliasvertcolor = varray_color;
+               R_SetupMDLMD2Frames(ent, mesh_colorscale, mesh_colorscale, mesh_colorscale);
+               aliasvert = aliasvertbuf;
+               aliasvertcolor = aliasvertcolorbuf;
+               R_Mesh_Draw(model->numverts, model->numtris);
+               return;
+       }
 
-               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);
-               R_Mesh_Render();
-       }
-       else if (currentrenderentity->colormap >= 0 || !skinframe->merged || skinframe->glow || !r_quickmodels.integer)
+       colormapped = !skinframe->merged || (ent->colormap >= 0 && skinframe->base && (skinframe->pants || skinframe->shirt));
+       if (!colormapped && !fog && !skinframe->glow && !skinframe->fog)
        {
-               R_SetupMDLMD2Frames(1 - fog, 1 - fog, 1 - fog);
+               // fastpath for the normal situation (one texture)
+               memset(&m, 0, sizeof(m));
+               m.blendfunc1 = blendfunc1;
+               m.blendfunc2 = blendfunc2;
+               m.wantoverbright = true;
+               m.tex[0] = R_GetTexture(skinframe->merged);
+               R_Mesh_State(&m);
+
+               c_alias_polys += model->numtris;
+               memcpy(varray_element, model->mdlmd2data_indices, model->numtris * sizeof(int[3]));
+               memcpy(varray_texcoord[0], model->mdlmd2data_texcoords, model->numverts * sizeof(float[2]));
+               aliasvert = varray_vertex;
+               aliasvertcolor = varray_color;
+               R_SetupMDLMD2Frames(ent, mesh_colorscale, mesh_colorscale, mesh_colorscale);
+               aliasvert = aliasvertbuf;
+               aliasvertcolor = aliasvertcolorbuf;
+               R_Mesh_Draw(model->numverts, model->numtris);
+               return;
+       }
 
-               memset(&aliasmeshinfo, 0, sizeof(aliasmeshinfo));
+       R_SetupMDLMD2Frames(ent, 1 - fog, 1 - fog, 1 - fog);
 
-               aliasmeshinfo.vertex = aliasvert;
-               aliasmeshinfo.vertexstep = sizeof(float[4]);
-               aliasmeshinfo.numverts = model->numverts;
-               aliasmeshinfo.numtriangles = model->numtris;
-               aliasmeshinfo.index = model->mdlmd2data_indices;
-               aliasmeshinfo.colorstep = sizeof(float[4]);
-               aliasmeshinfo.texcoords[0] = model->mdlmd2data_texcoords;
-               aliasmeshinfo.texcoordstep[0] = sizeof(float[2]);
+       if (colormapped)
+       {
+               // 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;
+       }
 
-               if (currentrenderentity->effects & EF_ADDITIVE)
-               {
-                       aliasmeshinfo.transparent = true;
-                       aliasmeshinfo.blendfunc1 = GL_SRC_ALPHA;
-                       aliasmeshinfo.blendfunc2 = GL_ONE;
-               }
-               else if (currentrenderentity->alpha != 1.0 || skinframe->fog != NULL)
-               {
-                       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;
-               }
+       memset(&m, 0, sizeof(m));
+       m.blendfunc1 = blendfunc1;
+       m.blendfunc2 = blendfunc2;
+       m.wantoverbright = true;
+       m.tex[0] = colormapped ? R_GetTexture(skinframe->base) : R_GetTexture(skinframe->merged);
+       if (m.tex[0])
+       {
+               R_Mesh_State(&m);
+
+               blendfunc1 = GL_SRC_ALPHA;
+               blendfunc2 = GL_ONE;
+               c_alias_polys += model->numtris;
+               R_ModulateColors(aliasvertcolor, varray_color, model->numverts, mesh_colorscale, mesh_colorscale, mesh_colorscale);
+               memcpy(varray_element, model->mdlmd2data_indices, model->numtris * sizeof(int[3]));
+               memcpy(varray_vertex, aliasvert, model->numverts * sizeof(float[4]));
+               memcpy(varray_texcoord[0], model->mdlmd2data_texcoords, model->numverts * sizeof(float[2]));
+               R_Mesh_Draw(model->numverts, model->numtris);
+       }
 
-               if (skinframe->base || skinframe->pants || skinframe->shirt || skinframe->glow || skinframe->merged)
+       if (colormapped)
+       {
+               if (skinframe->pants)
                {
-                       if (currentrenderentity->colormap >= 0 && (skinframe->base || skinframe->pants || skinframe->shirt))
+                       memset(&m, 0, sizeof(m));
+                       m.blendfunc1 = blendfunc1;
+                       m.blendfunc2 = blendfunc2;
+                       m.wantoverbright = true;
+                       m.tex[0] = R_GetTexture(skinframe->pants);
+                       if (m.tex[0])
                        {
-                               int c;
-                               qbyte *color;
-                               if (skinframe->base)
-                                       R_DrawModelMesh(skinframe->base, aliasvertcolor, 0, 0, 0);
-                               if (skinframe->pants)
-                               {
-                                       c = (currentrenderentity->colormap & 0xF) << 4;c += (c >= 128 && c < 224) ? 4 : 12; // 128-224 are backwards ranges
-                                       color = (qbyte *) (&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));
-                                       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);
-                                       }
-                               }
-                               if (skinframe->shirt)
-                               {
-                                       c = currentrenderentity->colormap & 0xF0      ;c += (c >= 128 && c < 224) ? 4 : 12; // 128-224 are backwards ranges
-                                       color = (qbyte *) (&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));
-                                       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);
-                                       }
-                               }
+                               R_Mesh_State(&m);
+
+                               blendfunc1 = GL_SRC_ALPHA;
+                               blendfunc2 = GL_ONE;
+                               c_alias_polys += model->numtris;
+                               if (pantsfullbright)
+                                       R_FillColors(varray_color, model->numverts, pantscolor[0] * mesh_colorscale, pantscolor[1] * mesh_colorscale, pantscolor[2] * mesh_colorscale, ent->alpha);
+                               else
+                                       R_ModulateColors(aliasvertcolor, varray_color, model->numverts, pantscolor[0] * mesh_colorscale, pantscolor[1] * mesh_colorscale, pantscolor[2] * mesh_colorscale);
+                               memcpy(varray_element, model->mdlmd2data_indices, model->numtris * sizeof(int[3]));
+                               memcpy(varray_vertex, aliasvert, model->numverts * sizeof(float[4]));
+                               memcpy(varray_texcoord[0], model->mdlmd2data_texcoords, model->numverts * sizeof(float[2]));
+                               R_Mesh_Draw(model->numverts, model->numtris);
                        }
-                       else
+               }
+               if (skinframe->shirt)
+               {
+                       memset(&m, 0, sizeof(m));
+                       m.blendfunc1 = blendfunc1;
+                       m.blendfunc2 = blendfunc2;
+                       m.wantoverbright = true;
+                       m.tex[0] = R_GetTexture(skinframe->shirt);
+                       if (m.tex[0])
                        {
-                               if (skinframe->merged)
-                                       R_DrawModelMesh(skinframe->merged, aliasvertcolor, 0, 0, 0);
+                               R_Mesh_State(&m);
+
+                               blendfunc1 = GL_SRC_ALPHA;
+                               blendfunc2 = GL_ONE;
+                               c_alias_polys += model->numtris;
+                               if (shirtfullbright)
+                                       R_FillColors(varray_color, model->numverts, shirtcolor[0] * mesh_colorscale, shirtcolor[1] * mesh_colorscale, shirtcolor[2] * mesh_colorscale, ent->alpha);
                                else
-                               {
-                                       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);
-                               }
+                                       R_ModulateColors(aliasvertcolor, varray_color, model->numverts, shirtcolor[0] * mesh_colorscale, shirtcolor[1] * mesh_colorscale, shirtcolor[2] * mesh_colorscale);
+                               memcpy(varray_element, model->mdlmd2data_indices, model->numtris * sizeof(int[3]));
+                               memcpy(varray_vertex, aliasvert, model->numverts * sizeof(float[4]));
+                               memcpy(varray_texcoord[0], model->mdlmd2data_texcoords, model->numverts * sizeof(float[2]));
+                               R_Mesh_Draw(model->numverts, model->numtris);
                        }
-                       if (skinframe->glow) R_DrawModelMesh(skinframe->glow, NULL, 1 - fog, 1 - fog, 1 - fog);
                }
-               else
-                       R_DrawModelMesh(0, NULL, 1 - fog, 1 - fog, 1 - fog);
        }
-       else
+       if (skinframe->glow)
        {
-               rmeshbufferinfo_t bufmesh;
-               memset(&bufmesh, 0, sizeof(bufmesh));
-               if (currentrenderentity->effects & EF_ADDITIVE)
-               {
-                       bufmesh.transparent = true;
-                       bufmesh.blendfunc1 = GL_SRC_ALPHA;
-                       bufmesh.blendfunc2 = GL_ONE;
-               }
-               else if (currentrenderentity->alpha != 1.0 || skinframe->fog != NULL)
+               memset(&m, 0, sizeof(m));
+               m.blendfunc1 = blendfunc1;
+               m.blendfunc2 = blendfunc2;
+               m.wantoverbright = true;
+               m.tex[0] = R_GetTexture(skinframe->glow);
+               if (m.tex[0])
                {
-                       bufmesh.transparent = true;
-                       bufmesh.blendfunc1 = GL_SRC_ALPHA;
-                       bufmesh.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
+                       R_Mesh_State(&m);
+
+                       blendfunc1 = GL_SRC_ALPHA;
+                       blendfunc2 = GL_ONE;
+                       c_alias_polys += model->numtris;
+                       R_FillColors(varray_color, model->numverts, (1 - fog) * mesh_colorscale, (1 - fog) * mesh_colorscale, (1 - fog) * mesh_colorscale, ent->alpha);
+                       memcpy(varray_element, model->mdlmd2data_indices, model->numtris * sizeof(int[3]));
+                       memcpy(varray_vertex, aliasvert, model->numverts * sizeof(float[4]));
+                       memcpy(varray_texcoord[0], model->mdlmd2data_texcoords, model->numverts * sizeof(float[2]));
+                       R_Mesh_Draw(model->numverts, model->numtris);
                }
-               else
-               {
-                       bufmesh.transparent = false;
-                       bufmesh.blendfunc1 = GL_ONE;
-                       bufmesh.blendfunc2 = GL_ZERO;
-               }
-               bufmesh.numtriangles = model->numtris;
-               bufmesh.numverts = model->numverts;
-               bufmesh.tex[0] = R_GetTexture(skinframe->merged);
-
-               R_Mesh_Draw_GetBuffer(&bufmesh, true);
-
-               aliasvert = bufmesh.vertex;
-               aliasvertcolor = bufmesh.color;
-               memcpy(bufmesh.index, model->mdlmd2data_indices, bufmesh.numtriangles * sizeof(int[3]));
-               memcpy(bufmesh.texcoords[0], model->mdlmd2data_texcoords, bufmesh.numverts * sizeof(float[2]));
-
-               fog = bufmesh.colorscale * (1 - fog);
-               R_SetupMDLMD2Frames(fog, fog, fog);
-
-               aliasvert = aliasvertbuf;
-               aliasvertcolor = aliasvertcolorbuf;
-               R_Mesh_Render();
+       }
+       if (fog)
+       {
+               memset(&m, 0, sizeof(m));
+               m.blendfunc1 = GL_SRC_ALPHA;
+               m.blendfunc2 = GL_ONE;
+               m.wantoverbright = false;
+               m.tex[0] = R_GetTexture(skinframe->fog);
+               R_Mesh_State(&m);
+
+               c_alias_polys += model->numtris;
+               R_FillColors(varray_color, model->numverts, fogcolor[0] * fog * mesh_colorscale, fogcolor[1] * fog * mesh_colorscale, fogcolor[2] * fog * mesh_colorscale, ent->alpha);
+               memcpy(varray_element, model->mdlmd2data_indices, model->numtris * sizeof(int[3]));
+               memcpy(varray_vertex, aliasvert, model->numverts * sizeof(float[4]));
+               memcpy(varray_texcoord[0], model->mdlmd2data_texcoords, model->numverts * sizeof(float[2]));
+               R_Mesh_Draw(model->numverts, model->numtris);
        }
 }
 
-int ZymoticLerpBones(int count, zymbonematrix *bonebase, frameblend_t *blend, zymbone_t *bone)
+int ZymoticLerpBones(int count, const zymbonematrix *bonebase, const frameblend_t *blend, const zymbone_t *bone)
 {
        int i;
        float lerp1, lerp2, lerp3, lerp4;
-       zymbonematrix *out, rootmatrix, m, *bone1, *bone2, *bone3, *bone4;
+       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];
@@ -519,6 +488,19 @@ int ZymoticLerpBones(int count, zymbonematrix *bonebase, frameblend_t *blend, zy
        rootmatrix.m[2][1] = -softwaretransform_matrix[2][0];
        rootmatrix.m[2][2] = softwaretransform_matrix[2][2];
        rootmatrix.m[2][3] = softwaretransform_matrix[2][3];
+       */
+       rootmatrix.m[0][0] = 1;
+       rootmatrix.m[0][1] = 0;
+       rootmatrix.m[0][2] = 0;
+       rootmatrix.m[0][3] = 0;
+       rootmatrix.m[1][0] = 0;
+       rootmatrix.m[1][1] = 1;
+       rootmatrix.m[1][2] = 0;
+       rootmatrix.m[1][3] = 0;
+       rootmatrix.m[2][0] = 0;
+       rootmatrix.m[2][1] = 0;
+       rootmatrix.m[2][2] = 1;
+       rootmatrix.m[2][3] = 0;
 
        bone1 = bonebase + blend[0].frame * count;
        lerp1 = blend[0].lerp;
@@ -760,132 +742,132 @@ void ZymoticCalcNormals(int vertcount, int shadercount, int *renderlist)
        }
 }
 
-void R_DrawZymoticModelMesh(zymtype1header_t *m)
+void R_DrawZymoticModelMeshCallback (const void *calldata1, int calldata2)
 {
+       float fog;
+       vec3_t diff;
        int i, *renderlist;
-       rtexture_t **texture;
+       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[4]);
-       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[0];
+       R_Mesh_ResizeCheck(numverts, numtriangles);
 
-       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);
-               R_Mesh_Render();
-               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, float fog)
-{
-       int i, *renderlist;
+       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));
+       mstate.wantoverbright = true;
+       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;
+       }
+       mstate.tex[0] = R_GetTexture(texture);
+       R_Mesh_State(&mstate);
 
-       aliasmeshinfo.cr = fogcolor[0];
-       aliasmeshinfo.cg = fogcolor[1];
-       aliasmeshinfo.cb = fogcolor[2];
-       aliasmeshinfo.ca = currentrenderentity->alpha * fog;
+       c_alias_polys += numtriangles;
+       memcpy(varray_element, renderlist + 1, numtriangles * sizeof(int[3]));
+       memcpy(varray_vertex, aliasvert, numverts * sizeof(float[4]));
+       R_ModulateColors(aliasvertcolor, varray_color, numverts, mesh_colorscale, mesh_colorscale, mesh_colorscale);
+       memcpy(varray_texcoord[0], (float *)(m->lump_texcoords.start + (int) m), numverts * sizeof(float[2]));
+       R_Mesh_Draw(numverts, numtriangles);
 
-       for (i = 0;i < m->numshaders;i++)
+       if (fog)
        {
-               aliasmeshinfo.numtriangles = *renderlist++;
-               aliasmeshinfo.index = renderlist;
-               c_alias_polys += aliasmeshinfo.numtriangles;
-               R_Mesh_Draw(&aliasmeshinfo);
-               R_Mesh_Render();
-               renderlist += aliasmeshinfo.numtriangles * 3;
+               memset(&mstate, 0, sizeof(mstate));
+               mstate.wantoverbright = false;
+               mstate.blendfunc1 = GL_SRC_ALPHA;
+               mstate.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
+               // FIXME: need alpha mask for fogging...
+               //mstate.tex[0] = R_GetTexture(texture);
+               R_Mesh_State(&mstate);
+
+               c_alias_polys += numtriangles;
+               memcpy(varray_element, renderlist + 1, numtriangles * sizeof(int[3]));
+               memcpy(varray_vertex, aliasvert, numverts * sizeof(float[4]));
+               R_FillColors(varray_color, numverts, fogcolor[0] * mesh_colorscale, fogcolor[1] * mesh_colorscale, fogcolor[2] * mesh_colorscale, ent->alpha * fog);
+               //memcpy(mesh_texcoord[0], (float *)(m->lump_texcoords.start + (int) m), numverts * sizeof(float[2]));
+               R_Mesh_Draw(numverts, numtriangles);
        }
 }
 
-void R_DrawZymoticModel (float fog)
+void R_DrawZymoticModel (entity_render_t *ent)
 {
+       int i;
        zymtype1header_t *m;
+       rtexture_t *texture;
 
-       // FIXME: do better fog
-       m = currentrenderentity->model->zymdata_header;
-       ZymoticLerpBones(m->numbones, (zymbonematrix *)(m->lump_poses.start + (int) m), currentrenderentity->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(m->numverts, 1 - fog, 1 - fog, 1 - fog, true);
-
-       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 (fog)
-               R_DrawZymoticModelMeshFog(currentrenderentity->origin, m, fog);
+       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)
 {
-       float fog;
-       vec3_t diff;
-
-       if (currentrenderentity->alpha < (1.0f / 64.0f))
+       if (ent->alpha < (1.0f / 64.0f))
                return; // basically completely transparent
 
        c_models++;
 
-       softwaretransformforentity(currentrenderentity);
-
-       fog = 0;
-       if (fogenabled)
-       {
-               VectorSubtract(currentrenderentity->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
-       }
-
-       if (currentrenderentity->model->aliastype == ALIASTYPE_ZYM)
-               R_DrawZymoticModel(fog);
+       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_DrawQ1Q2AliasModel(fog);
+               R_DrawQ1Q2AliasModelCallback(ent, 0);
 }