]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - gl_models.c
added loadtextureimagebumpasnmap
[xonotic/darkplaces.git] / gl_models.c
index c55f9c33787e20fbf2fd8a827e6a0d499d2b8034..128214a185f50d984d8526651f33c152848a4936 100644 (file)
@@ -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]));
@@ -48,189 +49,170 @@ void GL_Models_Init(void)
        R_RegisterModule("GL_Models", gl_models_start, gl_models_shutdown, gl_models_newmap);
 }
 
-void R_AliasLerpVerts(int vertcount, float *vertices, float *normals,
-               float lerp1, const trivertx_t *verts1, const vec3_t fscale1, const vec3_t translate1,
-               float lerp2, const trivertx_t *verts2, const vec3_t fscale2, const vec3_t translate2,
-               float lerp3, const trivertx_t *verts3, const vec3_t fscale3, const vec3_t translate3,
-               float lerp4, const trivertx_t *verts4, const vec3_t fscale4, const vec3_t translate4)
+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 = vertices;
-       avn = normals;
-       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_LerpMDLMD2Vertices(const entity_render_t *ent, float *vertices, float *normals)
-{
-       const md2frame_t *frame1, *frame2, *frame3, *frame4;
-       const trivertx_t *frame1verts, *frame2verts, *frame3verts, *frame4verts;
-       const model_t *model = ent->model;
-
-       frame1 = &model->mdlmd2data_frames[ent->frameblend[0].frame];
-       frame2 = &model->mdlmd2data_frames[ent->frameblend[1].frame];
-       frame3 = &model->mdlmd2data_frames[ent->frameblend[2].frame];
-       frame4 = &model->mdlmd2data_frames[ent->frameblend[3].frame];
-       frame1verts = &model->mdlmd2data_pose[ent->frameblend[0].frame * model->numverts];
-       frame2verts = &model->mdlmd2data_pose[ent->frameblend[1].frame * model->numverts];
-       frame3verts = &model->mdlmd2data_pose[ent->frameblend[2].frame * model->numverts];
-       frame4verts = &model->mdlmd2data_pose[ent->frameblend[3].frame * model->numverts];
-       R_AliasLerpVerts(model->numverts, vertices, normals,
-               ent->frameblend[0].lerp, frame1verts, frame1->scale, frame1->translate,
-               ent->frameblend[1].lerp, frame2verts, frame2->scale, frame2->translate,
-               ent->frameblend[2].lerp, frame3verts, frame3->scale, frame3->translate,
-               ent->frameblend[3].lerp, frame4verts, frame4->scale, frame4->translate);
+               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, fullbright, pantsfullbright, shirtfullbright, colormapped, tex;
-       float pantscolor[3], shirtcolor[3];
-       float fog, ifog, colorscale;
+       int c, fullbright, layernum;
+       float tint[3], fog, ifog, colorscale;
        vec3_t diff;
        qbyte *bcolor;
        rmeshstate_t m;
-       model_t *model;
-       skinframe_t *skinframe;
        const entity_render_t *ent = calldata1;
-       int blendfunc1, blendfunc2;
+       aliasmesh_t *mesh = ent->model->aliasdata_meshes + calldata2;
+       aliaslayer_t *layer;
+       aliasskin_t *skin;
 
        R_Mesh_Matrix(&ent->matrix);
 
-       model = ent->model;
-       R_Mesh_ResizeCheck(model->numverts);
-
-       skinframe = R_FetchSkinFrame(ent);
-
-       fullbright = (ent->effects & EF_FULLBRIGHT) != 0;
-
        fog = 0;
        if (fogenabled)
        {
@@ -249,267 +231,117 @@ void R_DrawQ1Q2AliasModelCallback (const void *calldata1, int calldata2)
        }
        ifog = 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;
-       }
-
-       R_LerpMDLMD2Vertices(ent, varray_vertex, aliasvertnorm);
-       memcpy(varray_texcoord[0], model->mdlmd2data_texcoords, model->numverts * sizeof(float[4]));
-       if (!skinframe->base && !skinframe->pants && !skinframe->shirt && !skinframe->glow)
+       memset(&m, 0, sizeof(m));
+       skin = R_FetchAliasSkin(ent, mesh);
+       R_Mesh_ResizeCheck(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]));
+       for (layernum = 0, layer = skin->data_layers;layernum < skin->num_layers;layernum++, layer++)
        {
-               // untextured
-               memset(&m, 0, sizeof(m));
-               m.blendfunc1 = blendfunc1;
-               m.blendfunc2 = blendfunc2;
-               colorscale = r_colorscale;
-               if (gl_combine.integer)
+               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)
                {
-                       colorscale *= 0.25f;
-                       m.texrgbscale[0] = 4;
+                       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_Draw(mesh->num_vertices, mesh->num_triangles, mesh->data_elements);
+                       continue;
                }
-               m.tex[0] = R_GetTexture(r_notexture);
-               R_Mesh_State(&m);
-               c_alias_polys += model->numtris;
-               for (i = 0;i < model->numverts * 4;i += 4)
+               if ((layer->flags & ALIASLAYER_ADD) || ((layer->flags & ALIASLAYER_ALPHA) && (ent->effects & EF_ADDITIVE)))
                {
-                       varray_texcoord[0][i + 0] *= 8.0f;
-                       varray_texcoord[0][i + 1] *= 8.0f;
+                       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;
                }
-               R_LightModel(ent, model->numverts, varray_vertex, aliasvertnorm, varray_color, colorscale, colorscale, colorscale, false);
-               GL_UseColorArray();
-               R_Mesh_Draw(model->numverts, model->numtris, model->mdlmd2data_indices);
-               return;
-       }
-
-       colormapped = !skinframe->merged || (ent->colormap >= 0 && skinframe->base && (skinframe->pants || skinframe->shirt));
-       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;
-       }
-
-       tex = colormapped ? R_GetTexture(skinframe->base) : R_GetTexture(skinframe->merged);
-       if (tex)
-       {
-               memset(&m, 0, sizeof(m));
-               m.blendfunc1 = blendfunc1;
-               m.blendfunc2 = blendfunc2;
                colorscale = r_colorscale;
+               m.texrgbscale[0] = 1;
                if (gl_combine.integer)
                {
                        colorscale *= 0.25f;
                        m.texrgbscale[0] = 4;
                }
-               m.tex[0] = tex;
+               m.tex[0] = R_GetTexture(layer->texture);
                R_Mesh_State(&m);
-               if (fullbright)
-                       GL_Color(colorscale * ifog, colorscale * ifog, colorscale * ifog, ent->alpha);
-               else
+               if (layer->flags & ALIASLAYER_COLORMAP_PANTS)
                {
-                       GL_UseColorArray();
-                       R_LightModel(ent, model->numverts, varray_vertex, aliasvertnorm, varray_color, colorscale * ifog, colorscale * ifog, colorscale * ifog, false);
+                       // 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);
                }
-               R_Mesh_Draw(model->numverts, model->numtris, model->mdlmd2data_indices);
-               c_alias_polys += model->numtris;
-               blendfunc1 = GL_SRC_ALPHA;
-               blendfunc2 = GL_ONE;
-       }
-
-       if (colormapped)
-       {
-               if (skinframe->pants)
-               {
-                       tex = R_GetTexture(skinframe->pants);
-                       if (tex)
-                       {
-                               memset(&m, 0, sizeof(m));
-                               m.blendfunc1 = blendfunc1;
-                               m.blendfunc2 = blendfunc2;
-                               colorscale = r_colorscale;
-                               if (gl_combine.integer)
-                               {
-                                       colorscale *= 0.25f;
-                                       m.texrgbscale[0] = 4;
-                               }
-                               m.tex[0] = tex;
-                               R_Mesh_State(&m);
-                               if (pantsfullbright)
-                                       GL_Color(pantscolor[0] * colorscale * ifog, pantscolor[1] * colorscale * ifog, pantscolor[2] * colorscale * ifog, ent->alpha);
-                               else
-                               {
-                                       GL_UseColorArray();
-                                       R_LightModel(ent, model->numverts, varray_vertex, aliasvertnorm, varray_color, pantscolor[0] * colorscale * ifog, pantscolor[1] * colorscale * ifog, pantscolor[2] * colorscale * ifog, false);
-                               }
-                               R_Mesh_Draw(model->numverts, model->numtris, model->mdlmd2data_indices);
-                               c_alias_polys += model->numtris;
-                               blendfunc1 = GL_SRC_ALPHA;
-                               blendfunc2 = GL_ONE;
-                       }
-               }
-               if (skinframe->shirt)
+               else if (layer->flags & ALIASLAYER_COLORMAP_SHIRT)
                {
-                       tex = R_GetTexture(skinframe->shirt);
-                       if (tex)
-                       {
-                               memset(&m, 0, sizeof(m));
-                               m.blendfunc1 = blendfunc1;
-                               m.blendfunc2 = blendfunc2;
-                               colorscale = r_colorscale;
-                               if (gl_combine.integer)
-                               {
-                                       colorscale *= 0.25f;
-                                       m.texrgbscale[0] = 4;
-                               }
-                               m.tex[0] = tex;
-                               R_Mesh_State(&m);
-                               if (shirtfullbright)
-                                       GL_Color(shirtcolor[0] * colorscale * ifog, shirtcolor[1] * colorscale * ifog, shirtcolor[2] * colorscale * ifog, ent->alpha);
-                               else
-                               {
-                                       GL_UseColorArray();
-                                       R_LightModel(ent, model->numverts, varray_vertex, aliasvertnorm, varray_color, shirtcolor[0] * colorscale * ifog, shirtcolor[1] * colorscale * ifog, shirtcolor[2] * colorscale * ifog, false);
-                               }
-                               R_Mesh_Draw(model->numverts, model->numtris, model->mdlmd2data_indices);
-                               c_alias_polys += model->numtris;
-                               blendfunc1 = GL_SRC_ALPHA;
-                               blendfunc2 = GL_ONE;
-                       }
+                       // 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);
                }
-       }
-       if (skinframe->glow)
-       {
-               tex = R_GetTexture(skinframe->glow);
-               if (tex)
+               else
                {
-                       memset(&m, 0, sizeof(m));
-                       m.blendfunc1 = blendfunc1;
-                       m.blendfunc2 = blendfunc2;
-                       m.tex[0] = tex;
-                       R_Mesh_State(&m);
-
-                       blendfunc1 = GL_SRC_ALPHA;
-                       blendfunc2 = GL_ONE;
-                       c_alias_polys += model->numtris;
-                       GL_Color(ifog * r_colorscale, ifog * r_colorscale, ifog * r_colorscale, ent->alpha);
-                       R_Mesh_Draw(model->numverts, model->numtris, model->mdlmd2data_indices);
+                       tint[0] = tint[1] = tint[2] = 1;
+                       fullbright = false;
                }
-       }
-       if (fog)
-       {
-               memset(&m, 0, sizeof(m));
-               m.blendfunc1 = GL_SRC_ALPHA;
-               m.blendfunc2 = GL_ONE;
-               m.tex[0] = R_GetTexture(skinframe->fog);
-               R_Mesh_State(&m);
-
-               c_alias_polys += model->numtris;
-               GL_Color(fogcolor[0] * fog * r_colorscale, fogcolor[1] * fog * r_colorscale, fogcolor[2] * fog * r_colorscale, ent->alpha);
-               R_Mesh_Draw(model->numverts, model->numtris, model->mdlmd2data_indices);
+               VectorScale(tint, ifog * colorscale, tint);
+               if (!(layer->flags & ALIASLAYER_DIFFUSE))
+                       fullbright = true;
+               if (ent->effects & EF_FULLBRIGHT)
+                       fullbright = true;
+               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);
+               c_alias_polys += mesh->num_triangles;
+               R_Mesh_Draw(mesh->num_vertices, mesh->num_triangles, mesh->data_elements);
        }
 }
 
 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
 
        c_models++;
 
-       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_DrawQ1Q2AliasModelCallback(ent, 0);
+       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);
+       }
 }
 
-extern cvar_t r_shadows;
 void R_Model_Alias_DrawFakeShadow (entity_render_t *ent)
 {
-       int i;
+       int i, meshnum;
+       aliasmesh_t *mesh;
+       aliasskin_t *skin;
        rmeshstate_t m;
-       model_t *model;
        float *v, planenormal[3], planedist, dist, projection[3], floororigin[3], surfnormal[3], lightdirection[3], v2[3];
 
-       /*
-       if (r_shadows.integer > 1)
-       {
-               float f, lightscale, lightcolor[3];
-               vec3_t temp;
-               mlight_t *sl;
-               rdlight_t *rd;
-               memset(&m, 0, sizeof(m));
-               m.blendfunc1 = GL_ONE;
-               m.blendfunc2 = GL_ONE;
-               R_Mesh_State(&m);
-               R_Mesh_Matrix(&ent->matrix);
-               for (i = 0, sl = cl.worldmodel->lights;i < cl.worldmodel->numlights;i++, sl++)
-               {
-                       if (d_lightstylevalue[sl->style] > 0)
-                       {
-                               VectorSubtract(ent->origin, sl->origin, temp);
-                               f = DotProduct(temp,temp);
-                               if (f < (ent->model->radius2 + sl->cullradius2))
-                               {
-                                       model = ent->model;
-                                       R_Mesh_ResizeCheck(model->numverts * 2);
-                                       R_LerpMDLMD2Vertices(ent, varray_vertex, aliasvertnorm);
-                                       Matrix4x4_Transform(&ent->inversematrix, sl->origin, temp);
-                                       GL_Color(0.1 * r_colorscale, 0.025 * r_colorscale, 0.0125 * r_colorscale, 1);
-                                       R_Shadow_Volume(model->numverts, model->numtris, varray_vertex, model->mdlmd2data_indices, model->mdlmd2data_triangleneighbors, temp, sl->cullradius + model->radius - sqrt(f), true);
-                                       GL_UseColorArray();
-                                       lightscale = d_lightstylevalue[sl->style] * (1.0f / 65536.0f);
-                                       VectorScale(sl->light, lightscale, lightcolor);
-                                       R_Shadow_VertexLight(model->numverts, varray_vertex, aliasvertnorm, temp, sl->cullradius2, sl->distbias, sl->subtract, lightcolor);
-                                       R_Mesh_Draw(model->numverts, model->numtris, model->mdlmd2data_indices);
-                               }
-                       }
-               }
-               for (i = 0, rd = r_dlight;i < r_numdlights;i++, rd++)
-               {
-                       if (ent != rd->ent)
-                       {
-                               VectorSubtract(ent->origin, rd->origin, temp);
-                               f = DotProduct(temp,temp);
-                               if (f < (ent->model->radius2 + rd->cullradius2))
-                               {
-                                       model = ent->model;
-                                       R_Mesh_ResizeCheck(model->numverts * 2);
-                                       R_LerpMDLMD2Vertices(ent, varray_vertex, aliasvertnorm);
-                                       Matrix4x4_Transform(&ent->inversematrix, rd->origin, temp);
-                                       GL_Color(0.1 * r_colorscale, 0.025 * r_colorscale, 0.0125 * r_colorscale, 1);
-                                       R_Shadow_Volume(model->numverts, model->numtris, varray_vertex, model->mdlmd2data_indices, model->mdlmd2data_triangleneighbors, temp, rd->cullradius + model->radius - sqrt(f), true);
-                                       GL_UseColorArray();
-                                       R_Shadow_VertexLight(model->numverts, varray_vertex, aliasvertnorm, temp, rd->cullradius2, LIGHTOFFSET, rd->subtract, rd->light);
-                                       R_Mesh_Draw(model->numverts, model->numtris, model->mdlmd2data_indices);
-                               }
-                       }
-               }
+       if ((ent->effects & EF_ADDITIVE) || ent->alpha < 1)
                return;
-       }
-       */
 
        lightdirection[0] = 0.5;
        lightdirection[1] = 0.2;
@@ -522,16 +354,11 @@ void R_Model_Alias_DrawFakeShadow (entity_render_t *ent)
 
        R_Mesh_Matrix(&ent->matrix);
 
-       model = ent->model;
-       R_Mesh_ResizeCheck(model->numverts);
-
        memset(&m, 0, sizeof(m));
        m.blendfunc1 = GL_SRC_ALPHA;
        m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
        R_Mesh_State(&m);
-
-       c_alias_polys += model->numtris;
-       R_LerpMDLMD2Vertices(ent, varray_vertex, aliasvertnorm);
+       GL_Color(0, 0, 0, 0.5);
 
        // put a light direction in the entity's coordinate space
        Matrix4x4_Transform3x3(&ent->inversematrix, lightdirection, projection);
@@ -547,48 +374,133 @@ void R_Model_Alias_DrawFakeShadow (entity_render_t *ent)
 
        dist = -1.0f / DotProduct(projection, planenormal);
        VectorScale(projection, dist, projection);
-       for (i = 0, v = varray_vertex;i < model->numverts;i++, v += 4)
+       for (meshnum = 0, mesh = ent->model->aliasdata_meshes;meshnum < ent->model->aliasnum_meshes;meshnum++)
        {
-               dist = DotProduct(v, planenormal) - planedist;
-               if (dist > 0)
-               //if (i & 1)
-                       VectorMA(v, dist, projection, v);
+               skin = R_FetchAliasSkin(ent, mesh);
+               if (skin->flags & ALIASSKIN_TRANSPARENT)
+                       continue;
+               R_Mesh_ResizeCheck(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);
        }
-       GL_Color(0, 0, 0, 0.5);
-       R_Mesh_Draw(model->numverts, model->numtris, model->mdlmd2data_indices);
-}
-
-void R_Model_Alias_DrawDepth(entity_render_t *ent)
-{
-       R_Mesh_ResizeCheck(ent->model->numverts);
-       R_LerpMDLMD2Vertices(ent, varray_vertex, aliasvertnorm);
-       R_Mesh_Draw(ent->model->numverts, ent->model->numtris, ent->model->mdlmd2data_indices);
 }
 
-void R_Model_Alias_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius, int visiblevolume)
+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)
        {
-               R_Mesh_ResizeCheck(ent->model->numverts * 2);
-               R_LerpMDLMD2Vertices(ent, varray_vertex, aliasvertnorm);
-               R_Shadow_Volume(ent->model->numverts, ent->model->numtris, varray_vertex, ent->model->mdlmd2data_indices, ent->model->mdlmd2data_triangleneighbors, relativelightorigin, lightradius, projectdistance, visiblevolume);
+               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_ResizeCheck(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);
+               }
        }
 }
 
-void R_Model_Alias_DrawLight(entity_render_t *ent, vec3_t relativelightorigin, float lightradius, float lightdistbias, float lightsubtract, float *lightcolor)
+void R_Model_Alias_DrawLight(entity_render_t *ent, vec3_t relativelightorigin, vec3_t relativeeyeorigin, float lightradius, float *lightcolor)
 {
-       R_Mesh_ResizeCheck(ent->model->numverts);
-       R_LerpMDLMD2Vertices(ent, varray_vertex, aliasvertnorm);
-       R_Shadow_Light(ent->model->numverts, aliasvertnorm, relativelightorigin, lightradius, lightdistbias, lightsubtract, lightcolor);
-       GL_UseColorArray();
-       R_Mesh_Draw(ent->model->numverts, ent->model->numtris, ent->model->mdlmd2data_indices);
-}
+       int c, meshnum, layernum;
+       float fog, ifog, lightcolor2[3];
+       vec3_t diff;
+       qbyte *bcolor;
+       aliasmesh_t *mesh;
+       aliaslayer_t *layer;
+       aliasskin_t *skin;
 
-void R_Model_Alias_DrawOntoLight(entity_render_t *ent)
-{
-       // FIXME
+       if (ent->effects & (EF_ADDITIVE | EF_FULLBRIGHT) || ent->alpha < 1)
+               return;
+
+       R_Mesh_Matrix(&ent->matrix);
+
+       fog = 0;
+       if (fogenabled)
+       {
+               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;
+
+       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_ResizeCheck(mesh->num_vertices);
+               R_Model_Alias_GetMeshVerts(ent, mesh, varray_vertex, aliasvert_normals, aliasvert_svectors, aliasvert_tvectors);
+               for (layernum = 0, layer = skin->data_layers;layernum < skin->num_layers;layernum++, layer++)
+               {
+                       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)
+                       {
+                               c_alias_polys += mesh->num_triangles;
+                               R_Shadow_SpecularLighting(mesh->num_vertices, mesh->num_triangles, mesh->data_elements, aliasvert_svectors, aliasvert_tvectors, aliasvert_normals, mesh->data_texcoords, relativelightorigin, relativeeyeorigin, lightradius, lightcolor2, layer->texture, layer->nmap, NULL);
+                       }
+                       else if (layer->flags & ALIASLAYER_DIFFUSE)
+                       {
+                               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, aliasvert_svectors, aliasvert_tvectors, aliasvert_normals, mesh->data_texcoords, relativelightorigin, lightradius, lightcolor2, layer->texture, layer->nmap, NULL);
+                       }
+               }
+       }
 }
 
 int ZymoticLerpBones(int count, const zymbonematrix *bonebase, const frameblend_t *blend, const zymbone_t *bone)
@@ -919,10 +831,10 @@ void R_DrawZymoticModelMeshCallback (const void *calldata1, int calldata2)
        R_Mesh_State(&mstate);
        ZymoticLerpBones(ent->model->zymnum_bones, (zymbonematrix *) ent->model->zymdata_poses, ent->frameblend, ent->model->zymdata_bones);
        ZymoticTransformVerts(numverts, varray_vertex, ent->model->zymdata_vertbonecounts, ent->model->zymdata_verts);
-       ZymoticCalcNormals(numverts, varray_vertex, aliasvertnorm, ent->model->zymnum_shaders, ent->model->zymdata_renderlist);
+       ZymoticCalcNormals(numverts, varray_vertex, aliasvert_normals, ent->model->zymnum_shaders, ent->model->zymdata_renderlist);
        memcpy(varray_texcoord[0], ent->model->zymdata_texcoords, ent->model->zymnum_verts * sizeof(float[4]));
        GL_UseColorArray();
-       R_LightModel(ent, numverts, varray_vertex, aliasvertnorm, varray_color, ifog * colorscale, ifog * colorscale, ifog * colorscale, false);
+       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;