]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - gl_models.c
fixed a bunch of signed/unsigned mismatch warnings in newer gcc versions (mostly...
[xonotic/darkplaces.git] / gl_models.c
index 4a703a2f8cfb208bda13e6412361c16830bce374..3cbd420b12be0ca761d9df031822a86daca6c56b 100644 (file)
@@ -9,34 +9,66 @@ typedef struct
 } zymbonematrix;
 
 // LordHavoc: vertex arrays
-
-float *aliasvertcolorbuf;
-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;
+int aliasvertmax = 0;
+void *aliasvertarrays = NULL;
+float *aliasvertcolor4fbuf = NULL;
+float *aliasvertcolor4f = NULL; // this may point at aliasvertcolorbuf or at vertex arrays in the mesh backend
+float *aliasvert_vertex3f = NULL;
+float *aliasvert_svector3f = NULL;
+float *aliasvert_tvector3f = NULL;
+float *aliasvert_normal3f = NULL;
+
+float *aliasvertcolor2_4f = NULL;
 int *aliasvertusage;
 zymbonematrix *zymbonepose;
 
 mempool_t *gl_models_mempool;
 
+#define expandaliasvert(newmax) if ((newmax) > aliasvertmax) gl_models_allocarrays(newmax)
+
+void gl_models_allocarrays(int newmax)
+{
+       qbyte *data;
+       aliasvertmax = newmax;
+       if (aliasvertarrays != NULL)
+               Mem_Free(aliasvertarrays);
+       aliasvertarrays = Mem_Alloc(gl_models_mempool, aliasvertmax * (sizeof(float[4+4+3+3+3+3]) + sizeof(int[3])));
+       data = aliasvertarrays;
+       aliasvertcolor4f = aliasvertcolor4fbuf = (void *)data;data += aliasvertmax * sizeof(float[4]);
+       aliasvertcolor2_4f = (void *)data;data += aliasvertmax * sizeof(float[4]); // used temporarily for tinted coloring
+       aliasvert_vertex3f = (void *)data;data += aliasvertmax * sizeof(float[3]);
+       aliasvert_svector3f = (void *)data;data += aliasvertmax * sizeof(float[3]);
+       aliasvert_tvector3f = (void *)data;data += aliasvertmax * sizeof(float[3]);
+       aliasvert_normal3f = (void *)data;data += aliasvertmax * sizeof(float[3]);
+       aliasvertusage = (void *)data;data += aliasvertmax * sizeof(int[3]);
+}
+
+void gl_models_freearrays(void)
+{
+       aliasvertmax = 0;
+       if (aliasvertarrays != NULL)
+               Mem_Free(aliasvertarrays);
+       aliasvertarrays = NULL;
+       aliasvertcolor4f = aliasvertcolor4fbuf = NULL;
+       aliasvertcolor2_4f = NULL;
+       aliasvert_vertex3f = NULL;
+       aliasvert_svector3f = NULL;
+       aliasvert_tvector3f = NULL;
+       aliasvert_normal3f = NULL;
+       aliasvertusage = NULL;
+}
+
 void gl_models_start(void)
 {
        // allocate vertex processing arrays
        gl_models_mempool = Mem_AllocPool("GL_Models");
-       aliasvertcolor = aliasvertcolorbuf = Mem_Alloc(gl_models_mempool, sizeof(float[MD2MAX_VERTS][4]));
-       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]));
+       gl_models_allocarrays(4096);
 }
 
 void gl_models_shutdown(void)
 {
+       gl_models_freearrays();
        Mem_FreePool(&gl_models_mempool);
 }
 
@@ -49,160 +81,87 @@ void GL_Models_Init(void)
        R_RegisterModule("GL_Models", gl_models_start, gl_models_shutdown, gl_models_newmap);
 }
 
-void R_Model_Alias_GetMeshVerts(const entity_render_t *ent, aliasmesh_t *mesh, float *vertices, float *normals, float *svectors, float *tvectors)
+#define MODELARRAY_VERTEX 0
+#define MODELARRAY_SVECTOR 1
+#define MODELARRAY_TVECTOR 2
+#define MODELARRAY_NORMAL 3
+
+void R_Model_Alias_GetMesh_Array3f(const entity_render_t *ent, aliasmesh_t *mesh, int whicharray, float *out3f)
 {
        int i, vertcount;
        float lerp1, lerp2, lerp3, lerp4;
-       const aliasvertex_t *verts1, *verts2, *verts3, *verts4;
+       const float *vertsbase, *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");
+       switch(whicharray)
+       {
+       case MODELARRAY_VERTEX:vertsbase = mesh->data_aliasvertex3f;break;
+       case MODELARRAY_SVECTOR:vertsbase = mesh->data_aliassvector3f;break;
+       case MODELARRAY_TVECTOR:vertsbase = mesh->data_aliastvector3f;break;
+       case MODELARRAY_NORMAL:vertsbase = mesh->data_aliasnormal3f;break;
+       default:
+               Host_Error("R_Model_Alias_GetBlendedArray: unknown whicharray %i\n", whicharray);
+               return;
+       }
 
        vertcount = mesh->num_vertices;
-       verts1 = mesh->data_vertices + ent->frameblend[0].frame * vertcount;
+       verts1 = vertsbase + ent->frameblend[0].frame * vertcount * 3;
        lerp1 = ent->frameblend[0].lerp;
        if (ent->frameblend[1].lerp)
        {
-               verts2 = mesh->data_vertices + ent->frameblend[1].frame * vertcount;
+               verts2 = vertsbase + ent->frameblend[1].frame * vertcount * 3;
                lerp2 = ent->frameblend[1].lerp;
                if (ent->frameblend[2].lerp)
                {
-                       verts3 = mesh->data_vertices + ent->frameblend[2].frame * vertcount;
+                       verts3 = vertsbase + ent->frameblend[2].frame * vertcount * 3;
                        lerp3 = ent->frameblend[2].lerp;
                        if (ent->frameblend[3].lerp)
                        {
-                               verts4 = mesh->data_vertices + ent->frameblend[3].frame * vertcount;
+                               verts4 = vertsbase + ent->frameblend[3].frame * vertcount * 3;
                                lerp4 = ent->frameblend[3].lerp;
-                               // generate vertices
-                               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)
-                               {
-                                       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);
+                               for (i = 0;i < vertcount * 3;i++)
+                                       VectorMAMAMAM(lerp1, verts1 + i, lerp2, verts2 + i, lerp3, verts3 + i, lerp4, verts4 + i, out3f + i);
                        }
                        else
-                       {
-                               // generate vertices
-                               if (svectors != NULL)
-                               {
-                                       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);
-                       }
+                               for (i = 0;i < vertcount * 3;i++)
+                                       VectorMAMAM(lerp1, verts1 + i, lerp2, verts2 + i, lerp3, verts3 + i, out3f + i);
                }
                else
-               {
-                       // generate vertices
-                       if (svectors != NULL)
-                       {
-                               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);
-               }
+                       for (i = 0;i < vertcount * 3;i++)
+                               VectorMAM(lerp1, verts1 + i, lerp2, verts2 + i, out3f + i);
        }
        else
-       {
-               // generate vertices
-               if (svectors != NULL)
-               {
-                       for (i = 0;i < vertcount;i++, vertices += 4, normals += 4, svectors += 4, tvectors += 4, verts1++)
-                       {
-                               VectorM(lerp1, verts1->origin, vertices);
-                               VectorM(lerp1, verts1->normal, normals);
-                               VectorM(lerp1, verts1->svector, svectors);
-                               CrossProduct(svectors, normals, tvectors);
-                       }
-               }
-               else if (normals != NULL)
-               {
-                       for (i = 0;i < vertcount;i++, vertices += 4, normals += 4, 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);
-       }
+               memcpy(out3f, verts1, vertcount * sizeof(float[3]));
 }
 
+aliaslayer_t r_aliasnoskinlayers[2] = {{ALIASLAYER_DIFFUSE, NULL, NULL}, {ALIASLAYER_FOG | ALIASLAYER_FORCEDRAW_IF_FIRSTPASS, NULL, NULL}};
+aliasskin_t r_aliasnoskin = {0, 2, r_aliasnoskinlayers};
 aliasskin_t *R_FetchAliasSkin(const entity_render_t *ent, const aliasmesh_t *mesh)
 {
        model_t *model = ent->model;
-       int s = ent->skinnum;
-       if ((unsigned int)s >= (unsigned int)model->numskins)
-               s = 0;
-       if (model->skinscenes[s].framecount > 1)
-               s = model->skinscenes[s].firstframe + (int) (cl.time * model->skinscenes[s].framerate) % model->skinscenes[s].framecount;
+       if (model->numskins)
+       {
+               int s = ent->skinnum;
+               if ((unsigned int)s >= (unsigned int)model->numskins)
+                       s = 0;
+               if (model->skinscenes[s].framecount > 1)
+                       s = model->skinscenes[s].firstframe + (int) (cl.time * model->skinscenes[s].framerate) % model->skinscenes[s].framecount;
+               else
+                       s = model->skinscenes[s].firstframe;
+               if (s >= mesh->num_skins)
+                       s = 0;
+               return mesh->data_skins + s;
+       }
        else
-               s = model->skinscenes[s].firstframe;
-       if (s >= mesh->num_skins)
-               s = 0;
-       return mesh->data_skins + s;
+       {
+               r_aliasnoskinlayers[0].texture = r_notexture;
+               return &r_aliasnoskin;
+       }
 }
 
 void R_DrawAliasModelCallback (const void *calldata1, int calldata2)
 {
-       int c, fullbright, layernum;
-       float tint[3], fog, ifog, colorscale;
+       int c, fullbright, layernum, firstpass;
+       float tint[3], fog, ifog, colorscale, ambientcolor4f[4], *fcolor;
        vec3_t diff;
        qbyte *bcolor;
        rmeshstate_t m;
@@ -210,6 +169,7 @@ void R_DrawAliasModelCallback (const void *calldata1, int calldata2)
        aliasmesh_t *mesh = ent->model->aliasdata_meshes + calldata2;
        aliaslayer_t *layer;
        aliasskin_t *skin;
+       rcachearrayrequest_t request;
 
        R_Mesh_Matrix(&ent->matrix);
 
@@ -231,36 +191,26 @@ void R_DrawAliasModelCallback (const void *calldata1, int calldata2)
        }
        ifog = 1 - fog;
 
-       memset(&m, 0, sizeof(m));
+       firstpass = true;
        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++)
        {
-               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)
+               if (!(layer->flags & ALIASLAYER_FORCEDRAW_IF_FIRSTPASS) || !firstpass)
                {
-                       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;
+                       if (((layer->flags & ALIASLAYER_NODRAW_IF_NOTCOLORMAPPED) && ent->colormap < 0)
+                        || ((layer->flags & ALIASLAYER_NODRAW_IF_COLORMAPPED) && ent->colormap >= 0)
+                        || ((layer->flags & ALIASLAYER_FOG) && !fogenabled)
+                        ||  (layer->flags & ALIASLAYER_SPECULAR)
+                        || ((layer->flags & ALIASLAYER_DIFFUSE) && (r_shadow_realtime_world.integer && r_ambient.integer <= 0 && r_fullbright.integer == 0 && !(ent->effects & EF_FULLBRIGHT))))
+                               continue;
                }
-               if ((layer->flags & ALIASLAYER_ADD) || ((layer->flags & ALIASLAYER_ALPHA) && (ent->effects & EF_ADDITIVE)))
+               memset(&m, 0, sizeof(m));
+               if (!firstpass || (ent->effects & EF_ADDITIVE))
                {
                        m.blendfunc1 = GL_SRC_ALPHA;
                        m.blendfunc2 = GL_ONE;
                }
-               else if ((layer->flags & ALIASLAYER_ALPHA) || ent->alpha != 1.0)
+               else if ((skin->flags & ALIASSKIN_TRANSPARENT) || ent->alpha != 1.0)
                {
                        m.blendfunc1 = GL_SRC_ALPHA;
                        m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
@@ -270,47 +220,113 @@ void R_DrawAliasModelCallback (const void *calldata1, int calldata2)
                        m.blendfunc1 = GL_ONE;
                        m.blendfunc2 = GL_ZERO;
                }
+               firstpass = false;
+               expandaliasvert(mesh->num_vertices);
                colorscale = r_colorscale;
                m.texrgbscale[0] = 1;
-               if (gl_combine.integer)
+               m.tex[0] = R_GetTexture(layer->texture);
+               if (gl_combine.integer && layer->flags & (ALIASLAYER_DIFFUSE | ALIASLAYER_SPECULAR))
                {
                        colorscale *= 0.25f;
                        m.texrgbscale[0] = 4;
                }
-               m.tex[0] = R_GetTexture(layer->texture);
-               R_Mesh_State(&m);
-               if (layer->flags & ALIASLAYER_COLORMAP_PANTS)
+               c_alias_polys += mesh->num_triangles;
+               if (gl_mesh_copyarrays.integer)
                {
-                       // 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_State(&m);
+                       R_Mesh_GetSpace(mesh->num_vertices);
+                       if (layer->texture != NULL)
+                               R_Mesh_CopyTexCoord2f(0, mesh->data_texcoord2f, mesh->num_vertices);
+                       R_Model_Alias_GetMesh_Array3f(ent, mesh, MODELARRAY_VERTEX, varray_vertex3f);
                }
-               else if (layer->flags & ALIASLAYER_COLORMAP_SHIRT)
+               else
                {
-                       // 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);
+                       m.pointervertexcount = mesh->num_vertices;
+                       memset(&request, 0, sizeof(request));
+                       request.data_size = mesh->num_vertices * sizeof(float[3]);
+                       request.id_pointer2 = mesh->data_aliasvertex3f;
+                       request.id_number1 = layernum;
+                       request.id_number2 = 0;
+                       request.id_number3 = CRC_Block((void *)ent->frameblend, sizeof(ent->frameblend));
+                       if (R_Mesh_CacheArray(&request))
+                               R_Model_Alias_GetMesh_Array3f(ent, mesh, MODELARRAY_VERTEX, request.data);
+                       m.pointer_vertex = request.data;
+                       m.pointer_texcoord[0] = layer->texture != NULL ? mesh->data_texcoord2f : NULL;
                }
-               else
+               if (layer->flags & ALIASLAYER_FOG)
                {
-                       tint[0] = tint[1] = tint[2] = 1;
-                       fullbright = false;
+                       colorscale *= fog;
+                       GL_Color(fogcolor[0] * colorscale, fogcolor[1] * colorscale, fogcolor[2] * colorscale, ent->alpha);
                }
-               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);
+               {
+                       if (layer->flags & (ALIASLAYER_COLORMAP_PANTS | ALIASLAYER_COLORMAP_SHIRT))
+                       {
+                               // 128-224 are backwards ranges
+                               if (layer->flags & ALIASLAYER_COLORMAP_PANTS)
+                                       c = (ent->colormap & 0xF) << 4;
+                               else //if (layer->flags & ALIASLAYER_COLORMAP_SHIRT)
+                                       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;
+                       }
+                       colorscale *= ifog;
+                       if (fullbright || !(layer->flags & ALIASLAYER_DIFFUSE) || r_fullbright.integer || (ent->effects & EF_FULLBRIGHT))
+                               GL_Color(tint[0] * colorscale, tint[1] * colorscale, tint[2] * colorscale, ent->alpha);
+                       else if (r_shadow_realtime_world.integer)
+                       {
+                               colorscale *= r_ambient.value * (2.0f / 128.0f);
+                               GL_Color(tint[0] * colorscale, tint[1] * colorscale, tint[2] * colorscale, ent->alpha);
+                       }
+                       else
+                       {
+                               if (R_LightModel(ambientcolor4f, ent, tint[0] * colorscale, tint[1] * colorscale, tint[2] * colorscale, ent->alpha, false))
+                               {
+                                       GL_UseColorArray();
+                                       if (gl_mesh_copyarrays.integer)
+                                       {
+                                               R_Model_Alias_GetMesh_Array3f(ent, mesh, MODELARRAY_NORMAL, aliasvert_normal3f);
+                                               R_LightModel_CalcVertexColors(ambientcolor4f, mesh->num_vertices, varray_vertex3f, aliasvert_normal3f, varray_color4f);
+                                       }
+                                       else
+                                       {
+                                               // request color4f cache
+                                               request.data_size = mesh->num_vertices * sizeof(float[4]);
+                                               request.id_pointer1 = ent;
+                                               request.id_number2 = 2;
+                                               request.id_number3 = CRC_Block((void *)ent->frameblend, sizeof(ent->frameblend)) + CRC_Block((void *)&ent->entlightstime, sizeof(ent->entlightstime));
+                                               if (R_Mesh_CacheArray(&request))
+                                               {
+                                                       // save off the color pointer before we blow away the request
+                                                       fcolor = request.data;
+                                                       m.pointer_color = fcolor;
+                                                       // request normal3f cache
+                                                       request.data_size = mesh->num_vertices * sizeof(float[3]);
+                                                       request.id_pointer1 = NULL;
+                                                       request.id_number2 = 3;
+                                                       request.id_number3 = CRC_Block((void *)ent->frameblend, sizeof(ent->frameblend));
+                                                       if (R_Mesh_CacheArray(&request))
+                                                               R_Model_Alias_GetMesh_Array3f(ent, mesh, MODELARRAY_NORMAL, request.data);
+                                                       R_LightModel_CalcVertexColors(ambientcolor4f, mesh->num_vertices, m.pointer_vertex, request.data, fcolor);
+                                               }
+                                               else
+                                                       m.pointer_color = request.data;
+                                       }
+                               }
+                               else
+                                       GL_Color(ambientcolor4f[0], ambientcolor4f[1], ambientcolor4f[2], ambientcolor4f[3]);
+                       }
+               }
+               if (!gl_mesh_copyarrays.integer)
+                       R_Mesh_State(&m);
+               R_Mesh_Draw(mesh->num_vertices, mesh->num_triangles, mesh->data_element3i);
        }
 }
 
@@ -338,7 +354,8 @@ void R_Model_Alias_DrawFakeShadow (entity_render_t *ent)
        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];
+       float *v, plane[4], dist, projection[3], floororigin[3], surfnormal[3], lightdirection[3], v2[3];
+       rcachearrayrequest_t request;
 
        if ((ent->effects & EF_ADDITIVE) || ent->alpha < 1)
                return;
@@ -357,7 +374,8 @@ void R_Model_Alias_DrawFakeShadow (entity_render_t *ent)
        memset(&m, 0, sizeof(m));
        m.blendfunc1 = GL_SRC_ALPHA;
        m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
-       R_Mesh_State(&m);
+       if (gl_mesh_copyarrays.integer)
+               R_Mesh_State(&m);
        GL_Color(0, 0, 0, 0.5);
 
        // put a light direction in the entity's coordinate space
@@ -365,30 +383,55 @@ void R_Model_Alias_DrawFakeShadow (entity_render_t *ent)
        VectorNormalizeFast(projection);
 
        // put the plane's normal in the entity's coordinate space
-       Matrix4x4_Transform3x3(&ent->inversematrix, surfnormal, planenormal);
-       VectorNormalizeFast(planenormal);
+       Matrix4x4_Transform3x3(&ent->inversematrix, surfnormal, plane);
+       VectorNormalizeFast(plane);
 
        // put the plane's distance in the entity's coordinate space
        VectorSubtract(floororigin, ent->origin, floororigin);
-       planedist = DotProduct(floororigin, surfnormal) + 2;
+       plane[3] = DotProduct(floororigin, surfnormal) + 2;
 
-       dist = -1.0f / DotProduct(projection, planenormal);
+       dist = -1.0f / DotProduct(projection, plane);
        VectorScale(projection, dist, projection);
+       memset(&request, 0, sizeof(request));
        for (meshnum = 0, mesh = ent->model->aliasdata_meshes;meshnum < ent->model->aliasnum_meshes;meshnum++)
        {
                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)
+               if (gl_mesh_copyarrays.integer)
                {
-                       dist = DotProduct(v, planenormal) - planedist;
-                       if (dist > 0)
-                               VectorMA(v, dist, projection, v);
+                       R_Mesh_GetSpace(mesh->num_vertices);
+                       R_Model_Alias_GetMesh_Array3f(ent, mesh, MODELARRAY_VERTEX, varray_vertex3f);
+                       for (i = 0, v = varray_vertex3f;i < mesh->num_vertices;i++, v += 3)
+                       {
+                               dist = DotProduct(v, plane) - plane[3];
+                               if (dist > 0)
+                                       VectorMA(v, dist, projection, v);
+                       }
+               }
+               else
+               {
+                       request.data_size = mesh->num_vertices * sizeof(float[3]);
+                       request.id_pointer1 = mesh;
+                       request.id_number1 = CRC_Block((void *)&ent->matrix, sizeof(ent->matrix));
+                       request.id_number2 = CRC_Block((void *)&plane, sizeof(plane));
+                       request.id_number3 = CRC_Block((void *)&ent->frameblend, sizeof(ent->frameblend));
+                       m.pointervertexcount = mesh->num_vertices;
+                       if (R_Mesh_CacheArray(&request))
+                       {
+                               R_Model_Alias_GetMesh_Array3f(ent, mesh, MODELARRAY_VERTEX, request.data);
+                               for (i = 0, v = request.data;i < mesh->num_vertices;i++, v += 3)
+                               {
+                                       dist = DotProduct(v, plane) - plane[3];
+                                       if (dist > 0)
+                                               VectorMA(v, dist, projection, v);
+                               }
+                       }
+                       m.pointer_vertex = request.data;
+                       R_Mesh_State(&m);
                }
                c_alias_polys += mesh->num_triangles;
-               R_Mesh_Draw(mesh->num_vertices, mesh->num_triangles, mesh->data_elements);
+               R_Mesh_Draw(mesh->num_vertices, mesh->num_triangles, mesh->data_element3i);
        }
 }
 
@@ -409,9 +452,9 @@ void R_Model_Alias_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightor
                        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);
+                       R_Mesh_GetSpace(mesh->num_vertices * 2);
+                       R_Model_Alias_GetMesh_Array3f(ent, mesh, MODELARRAY_VERTEX, varray_vertex3f);
+                       R_Shadow_Volume(mesh->num_vertices, mesh->num_triangles, mesh->data_element3i, mesh->data_neighbor3i, relativelightorigin, lightradius, projectdistance);
                }
        }
 }
@@ -454,11 +497,14 @@ void R_Model_Alias_DrawLight(entity_render_t *ent, vec3_t relativelightorigin, 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, aliasvert_normals, aliasvert_svectors, aliasvert_tvectors);
+               expandaliasvert(mesh->num_vertices);
+               R_Model_Alias_GetMesh_Array3f(ent, mesh, MODELARRAY_VERTEX, aliasvert_vertex3f);
+               R_Model_Alias_GetMesh_Array3f(ent, mesh, MODELARRAY_SVECTOR, aliasvert_svector3f);
+               R_Model_Alias_GetMesh_Array3f(ent, mesh, MODELARRAY_TVECTOR, aliasvert_tvector3f);
+               R_Model_Alias_GetMesh_Array3f(ent, mesh, MODELARRAY_NORMAL, aliasvert_normal3f);
                for (layernum = 0, layer = skin->data_layers;layernum < skin->num_layers;layernum++, layer++)
                {
-                       if (!(layer->flags & ALIASLAYER_DRAW_PER_LIGHT)
+                       if (!(layer->flags & (ALIASLAYER_DIFFUSE | ALIASLAYER_SPECULAR))
                         || ((layer->flags & ALIASLAYER_NODRAW_IF_NOTCOLORMAPPED) && ent->colormap < 0)
                         || ((layer->flags & ALIASLAYER_NODRAW_IF_COLORMAPPED) && ent->colormap >= 0))
                                continue;
@@ -468,7 +514,7 @@ void R_Model_Alias_DrawLight(entity_render_t *ent, vec3_t relativelightorigin, v
                        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, matrix_modeltofilter, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, layer->texture, layer->nmap, NULL);
+                               R_Shadow_SpecularLighting(mesh->num_vertices, mesh->num_triangles, mesh->data_element3i, aliasvert_vertex3f, aliasvert_svector3f, aliasvert_tvector3f, aliasvert_normal3f, mesh->data_texcoord2f, relativelightorigin, relativeeyeorigin, lightradius, lightcolor2, matrix_modeltofilter, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, layer->texture, layer->nmap, NULL);
                        }
                        else if (layer->flags & ALIASLAYER_DIFFUSE)
                        {
@@ -497,7 +543,7 @@ void R_Model_Alias_DrawLight(entity_render_t *ent, vec3_t relativelightorigin, v
                                        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, matrix_modeltofilter, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, layer->texture, layer->nmap, NULL);
+                               R_Shadow_DiffuseLighting(mesh->num_vertices, mesh->num_triangles, mesh->data_element3i, aliasvert_vertex3f, aliasvert_svector3f, aliasvert_tvector3f, aliasvert_normal3f, mesh->data_texcoord2f, relativelightorigin, lightradius, lightcolor2, matrix_modeltofilter, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, layer->texture, layer->nmap, NULL);
                        }
                }
        }
@@ -697,17 +743,17 @@ void ZymoticTransformVerts(int vertcount, float *vertex, int *bonecounts, zymver
                                vert++;
                        }
                }
-               out += 4;
+               out += 3;
        }
 }
 
-void ZymoticCalcNormals(int vertcount, float *vertex, float *normals, int shadercount, int *renderlist)
+void ZymoticCalcNormal3f(int vertcount, float *vertex3f, float *normal3f, int shadercount, int *renderlist)
 {
        int a, b, c, d;
        float *out, v1[3], v2[3], normal[3], s;
        int *u;
        // clear normals
-       memset(normals, 0, sizeof(float) * vertcount * 3);
+       memset(normal3f, 0, sizeof(float) * vertcount * 3);
        memset(aliasvertusage, 0, sizeof(int) * vertcount);
        // parse render list and accumulate surface normals
        while(shadercount--)
@@ -718,36 +764,36 @@ void ZymoticCalcNormals(int vertcount, float *vertex, float *normals, int shader
                        a = renderlist[0]*4;
                        b = renderlist[1]*4;
                        c = renderlist[2]*4;
-                       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];
+                       v1[0] = vertex3f[a+0] - vertex3f[b+0];
+                       v1[1] = vertex3f[a+1] - vertex3f[b+1];
+                       v1[2] = vertex3f[a+2] - vertex3f[b+2];
+                       v2[0] = vertex3f[c+0] - vertex3f[b+0];
+                       v2[1] = vertex3f[c+1] - vertex3f[b+1];
+                       v2[2] = vertex3f[c+2] - vertex3f[b+2];
                        CrossProduct(v1, v2, normal);
                        VectorNormalizeFast(normal);
                        // add surface normal to vertices
                        a = renderlist[0] * 3;
-                       normals[a+0] += normal[0];
-                       normals[a+1] += normal[1];
-                       normals[a+2] += normal[2];
+                       normal3f[a+0] += normal[0];
+                       normal3f[a+1] += normal[1];
+                       normal3f[a+2] += normal[2];
                        aliasvertusage[renderlist[0]]++;
                        a = renderlist[1] * 3;
-                       normals[a+0] += normal[0];
-                       normals[a+1] += normal[1];
-                       normals[a+2] += normal[2];
+                       normal3f[a+0] += normal[0];
+                       normal3f[a+1] += normal[1];
+                       normal3f[a+2] += normal[2];
                        aliasvertusage[renderlist[1]]++;
                        a = renderlist[2] * 3;
-                       normals[a+0] += normal[0];
-                       normals[a+1] += normal[1];
-                       normals[a+2] += normal[2];
+                       normal3f[a+0] += normal[0];
+                       normal3f[a+1] += normal[1];
+                       normal3f[a+2] += normal[2];
                        aliasvertusage[renderlist[2]]++;
                        renderlist += 3;
                }
        }
        // FIXME: precalc this
        // average surface normals
-       out = normals;
+       out = normal3f;
        u = aliasvertusage;
        while(vertcount--)
        {
@@ -765,7 +811,7 @@ void ZymoticCalcNormals(int vertcount, float *vertex, float *normals, int shader
 
 void R_DrawZymoticModelMeshCallback (const void *calldata1, int calldata2)
 {
-       float fog, ifog, colorscale;
+       float fog, ifog, colorscale, ambientcolor4f[4];
        vec3_t diff;
        int i, *renderlist, *elements;
        rtexture_t *texture;
@@ -785,7 +831,8 @@ void R_DrawZymoticModelMeshCallback (const void *calldata1, int calldata2)
        numverts = ent->model->zymnum_verts;
        numtriangles = *renderlist++;
        elements = renderlist;
-       R_Mesh_ResizeCheck(numverts);
+
+       expandaliasvert(numverts);
 
        fog = 0;
        if (fogenabled)
@@ -830,11 +877,18 @@ void R_DrawZymoticModelMeshCallback (const void *calldata1, int calldata2)
        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);
-       ZymoticTransformVerts(numverts, varray_vertex, ent->model->zymdata_vertbonecounts, ent->model->zymdata_verts);
-       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, aliasvert_normals, varray_color, ifog * colorscale, ifog * colorscale, ifog * colorscale, false);
+
+       R_Mesh_GetSpace(numverts);
+       ZymoticTransformVerts(numverts, varray_vertex3f, ent->model->zymdata_vertbonecounts, ent->model->zymdata_verts);
+       R_Mesh_CopyTexCoord2f(0, ent->model->zymdata_texcoords, ent->model->zymnum_verts);
+       ZymoticCalcNormal3f(numverts, varray_vertex3f, aliasvert_normal3f, ent->model->zymnum_shaders, ent->model->zymdata_renderlist);
+       if (R_LightModel(ambientcolor4f, ent, ifog * colorscale, ifog * colorscale, ifog * colorscale, ent->alpha, false))
+       {
+               GL_UseColorArray();
+               R_LightModel_CalcVertexColors(ambientcolor4f, numverts, varray_vertex3f, aliasvert_normal3f, varray_color4f);
+       }
+       else
+               GL_Color(ambientcolor4f[0], ambientcolor4f[1], ambientcolor4f[2], ambientcolor4f[3]);
        R_Mesh_Draw(numverts, numtriangles, elements);
        c_alias_polys += numtriangles;
 
@@ -847,6 +901,8 @@ void R_DrawZymoticModelMeshCallback (const void *calldata1, int calldata2)
                //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_vertex3f, ent->model->zymdata_vertbonecounts, ent->model->zymdata_verts);
                R_Mesh_Draw(numverts, numtriangles, elements);
                c_alias_polys += numtriangles;
        }
@@ -884,3 +940,4 @@ void R_Model_Zymotic_DrawOntoLight(entity_render_t *ent)
 {
        // FIXME
 }
+