]> 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 80d6e1e8f4a414d09a200cefa31bb214e12193da..3cbd420b12be0ca761d9df031822a86daca6c56b 100644 (file)
@@ -13,6 +13,7 @@ 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;
@@ -31,10 +32,11 @@ void gl_models_allocarrays(int newmax)
        aliasvertmax = newmax;
        if (aliasvertarrays != NULL)
                Mem_Free(aliasvertarrays);
-       aliasvertarrays = Mem_Alloc(gl_models_mempool, aliasvertmax * (sizeof(float[4+4+3+3+3]) + sizeof(int[3])));
+       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]);
@@ -49,6 +51,7 @@ void gl_models_freearrays(void)
        aliasvertarrays = NULL;
        aliasvertcolor4f = aliasvertcolor4fbuf = NULL;
        aliasvertcolor2_4f = NULL;
+       aliasvert_vertex3f = NULL;
        aliasvert_svector3f = NULL;
        aliasvert_tvector3f = NULL;
        aliasvert_normal3f = NULL;
@@ -158,7 +161,7 @@ aliasskin_t *R_FetchAliasSkin(const entity_render_t *ent, const aliasmesh_t *mes
 void R_DrawAliasModelCallback (const void *calldata1, int calldata2)
 {
        int c, fullbright, layernum, firstpass;
-       float tint[3], fog, ifog, colorscale;
+       float tint[3], fog, ifog, colorscale, ambientcolor4f[4], *fcolor;
        vec3_t diff;
        qbyte *bcolor;
        rmeshstate_t m;
@@ -166,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);
 
@@ -188,7 +192,6 @@ void R_DrawAliasModelCallback (const void *calldata1, int calldata2)
        ifog = 1 - fog;
 
        firstpass = true;
-       memset(&m, 0, sizeof(m));
        skin = R_FetchAliasSkin(ent, mesh);
        for (layernum = 0, layer = skin->data_layers;layernum < skin->num_layers;layernum++, layer++)
        {
@@ -201,6 +204,7 @@ void R_DrawAliasModelCallback (const void *calldata1, int calldata2)
                         || ((layer->flags & ALIASLAYER_DIFFUSE) && (r_shadow_realtime_world.integer && r_ambient.integer <= 0 && r_fullbright.integer == 0 && !(ent->effects & EF_FULLBRIGHT))))
                                continue;
                }
+               memset(&m, 0, sizeof(m));
                if (!firstpass || (ent->effects & EF_ADDITIVE))
                {
                        m.blendfunc1 = GL_SRC_ALPHA;
@@ -226,15 +230,32 @@ void R_DrawAliasModelCallback (const void *calldata1, int calldata2)
                        colorscale *= 0.25f;
                        m.texrgbscale[0] = 4;
                }
-               R_Mesh_State(&m);
                c_alias_polys += mesh->num_triangles;
-               R_Mesh_GetSpace(mesh->num_vertices);
-               if (layer->texture != NULL)
-                       R_Mesh_CopyTexCoord2f(0, mesh->data_texcoord2f, mesh->num_vertices);
+               if (gl_mesh_copyarrays.integer)
+               {
+                       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
+               {
+                       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;
+               }
                if (layer->flags & ALIASLAYER_FOG)
                {
                        colorscale *= fog;
-                       R_Model_Alias_GetMesh_Array3f(ent, mesh, MODELARRAY_VERTEX, varray_vertex3f);
                        GL_Color(fogcolor[0] * colorscale, fogcolor[1] * colorscale, fogcolor[2] * colorscale, ent->alpha);
                }
                else
@@ -257,7 +278,6 @@ void R_DrawAliasModelCallback (const void *calldata1, int calldata2)
                                fullbright = false;
                        }
                        colorscale *= ifog;
-                       R_Model_Alias_GetMesh_Array3f(ent, mesh, MODELARRAY_VERTEX, varray_vertex3f);
                        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)
@@ -267,10 +287,45 @@ void R_DrawAliasModelCallback (const void *calldata1, int calldata2)
                        }
                        else
                        {
-                               R_Model_Alias_GetMesh_Array3f(ent, mesh, MODELARRAY_NORMAL, aliasvert_normal3f);
-                               R_LightModel(ent, mesh->num_vertices, varray_vertex3f, aliasvert_normal3f, varray_color4f, tint[0] * colorscale, tint[1] * colorscale, tint[2] * colorscale, false);
+                               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);
        }
 }
@@ -299,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;
@@ -318,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
@@ -326,27 +383,52 @@ 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_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)
+               if (gl_mesh_copyarrays.integer)
+               {
+                       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
                {
-                       dist = DotProduct(v, planenormal) - planedist;
-                       if (dist > 0)
-                               VectorMA(v, dist, projection, v);
+                       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_element3i);
@@ -380,7 +462,7 @@ void R_Model_Alias_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightor
 void R_Model_Alias_DrawLight(entity_render_t *ent, vec3_t relativelightorigin, vec3_t relativeeyeorigin, float lightradius, float *lightcolor, const matrix4x4_t *matrix_modeltofilter, const matrix4x4_t *matrix_modeltoattenuationxyz, const matrix4x4_t *matrix_modeltoattenuationz)
 {
        int c, meshnum, layernum;
-       float fog, ifog, lightcolor2[3], *vertices;
+       float fog, ifog, lightcolor2[3];
        vec3_t diff;
        qbyte *bcolor;
        aliasmesh_t *mesh;
@@ -416,8 +498,7 @@ void R_Model_Alias_DrawLight(entity_render_t *ent, vec3_t relativelightorigin, v
                if (skin->flags & ALIASSKIN_TRANSPARENT)
                        continue;
                expandaliasvert(mesh->num_vertices);
-               vertices = R_Shadow_VertexBuffer(mesh->num_vertices);
-               R_Model_Alias_GetMesh_Array3f(ent, mesh, MODELARRAY_VERTEX, 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);
@@ -433,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_element3i, vertices, aliasvert_svector3f, aliasvert_tvector3f, aliasvert_normal3f, mesh->data_texcoord2f, 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)
                        {
@@ -462,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_element3i, vertices, aliasvert_svector3f, aliasvert_tvector3f, aliasvert_normal3f, mesh->data_texcoord2f, 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);
                        }
                }
        }
@@ -730,7 +811,7 @@ void ZymoticCalcNormal3f(int vertcount, float *vertex3f, float *normal3f, int sh
 
 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;
@@ -801,7 +882,13 @@ void R_DrawZymoticModelMeshCallback (const void *calldata1, int calldata2)
        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);
-       R_LightModel(ent, numverts, varray_vertex3f, aliasvert_normal3f, varray_color4f, ifog * colorscale, ifog * colorscale, ifog * colorscale, false);
+       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;