]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - model_shared.c
fixed sv_waterfriction code so it is now used
[xonotic/darkplaces.git] / model_shared.c
index 5277a4e76d31886a8c6c463db80d3d605b247c63..9bffa3130efb19292080b1b95107ab660bd5cd36 100644 (file)
@@ -502,19 +502,19 @@ void Mod_BuildTriangleNeighbors(int *neighbors, const int *elements, int numtria
 }
 #endif
 
-void Mod_ValidateElements(int *elements, int numtriangles, int numverts, const char *filename, int fileline)
+void Mod_ValidateElements(int *elements, int numtriangles, int firstvertex, int numverts, const char *filename, int fileline)
 {
-       int i, warned = false;
+       int i, warned = false, endvertex = firstvertex + numverts;
        for (i = 0;i < numtriangles * 3;i++)
        {
-               if ((unsigned int)elements[i] >= (unsigned int)numverts)
+               if (elements[i] < firstvertex || elements[i] >= endvertex)
                {
                        if (!warned)
                        {
                                warned = true;
                                Con_Printf("Mod_ValidateElements: out of bounds elements detected at %s:%d\n", filename, fileline);
                        }
-                       elements[i] = 0;
+                       elements[i] = firstvertex;
                }
        }
 }
@@ -605,20 +605,16 @@ void Mod_BuildBumpVectors(const float *v0, const float *v1, const float *v2, con
 }
 
 // warning: this is a very expensive function!
-void Mod_BuildTextureVectorsAndNormals(int firstvertex, int numvertices, int numtriangles, const float *vertex3f, const float *texcoord2f, const int *elements, float *svector3f, float *tvector3f, float *normal3f, qboolean areaweighting)
+void Mod_BuildTextureVectorsFromNormals(int firstvertex, int numvertices, int numtriangles, const float *vertex3f, const float *texcoord2f, const float *normal3f, const int *elements, float *svector3f, float *tvector3f, qboolean areaweighting)
 {
        int i, tnum;
-       float sdir[3], tdir[3], normal[3], *v;
-       const float *v0, *v1, *v2, *tc0, *tc1, *tc2;
+       float sdir[3], tdir[3], normal[3], *sv, *tv;
+       const float *v0, *v1, *v2, *tc0, *tc1, *tc2, *n;
        float f, tangentcross[3], v10[3], v20[3], tc10[2], tc20[2];
        const int *e;
        // clear the vectors
-       if (svector3f)
-               memset(svector3f + 3 * firstvertex, 0, numvertices * sizeof(float[3]));
-       if (tvector3f)
-               memset(tvector3f + 3 * firstvertex, 0, numvertices * sizeof(float[3]));
-       if (normal3f)
-               memset(normal3f + 3 * firstvertex, 0, numvertices * sizeof(float[3]));
+       memset(svector3f + 3 * firstvertex, 0, numvertices * sizeof(float[3]));
+       memset(tvector3f + 3 * firstvertex, 0, numvertices * sizeof(float[3]));
        // process each vertex of each triangle and accumulate the results
        for (tnum = 0, e = elements;tnum < numtriangles;tnum++, e += 3)
        {
@@ -653,17 +649,6 @@ void Mod_BuildTextureVectorsAndNormals(int firstvertex, int numvertices, int num
                tdir[1] = tc10[0] * v20[1] - tc20[0] * v10[1];
                tdir[2] = tc10[0] * v20[2] - tc20[0] * v10[2];
 
-               // make the tangents completely perpendicular to the surface normal
-               // 12 multiply, 4 add, 6 subtract
-               f = DotProduct(sdir, normal);
-               sdir[0] -= f * normal[0];
-               sdir[1] -= f * normal[1];
-               sdir[2] -= f * normal[2];
-               f = DotProduct(tdir, normal);
-               tdir[0] -= f * normal[0];
-               tdir[1] -= f * normal[1];
-               tdir[2] -= f * normal[2];
-
                // if texture is mapped the wrong way (counterclockwise), the tangents
                // have to be flipped, this is detected by calculating a normal from the
                // two tangents, and seeing if it is opposite the surface normal
@@ -679,62 +664,52 @@ void Mod_BuildTextureVectorsAndNormals(int firstvertex, int numvertices, int num
                {
                        VectorNormalize(sdir);
                        VectorNormalize(tdir);
-                       VectorNormalize(normal);
                }
-               if (svector3f)
-                       for (i = 0;i < 3;i++)
-                               VectorAdd(svector3f + e[i]*3, sdir, svector3f + e[i]*3);
-               if (tvector3f)
-                       for (i = 0;i < 3;i++)
-                               VectorAdd(tvector3f + e[i]*3, tdir, tvector3f + e[i]*3);
-               if (normal3f)
-                       for (i = 0;i < 3;i++)
-                               VectorAdd(normal3f + e[i]*3, normal, normal3f + e[i]*3);
+               for (i = 0;i < 3;i++)
+               {
+                       VectorAdd(svector3f + e[i]*3, sdir, svector3f + e[i]*3);
+                       VectorAdd(tvector3f + e[i]*3, tdir, tvector3f + e[i]*3);
+               }
+       }
+       // make the tangents completely perpendicular to the surface normal, and
+       // then normalize them
+       // 16 assignments, 2 divide, 2 sqrt, 2 negates, 14 adds, 24 multiplies
+       for (i = 0, sv = svector3f + 3 * firstvertex, tv = tvector3f + 3 * firstvertex, n = normal3f + 3 * firstvertex;i < numvertices;i++, sv += 3, tv += 3, n += 3)
+       {
+               f = -DotProduct(sv, n);
+               VectorMA(sv, f, n, sv);
+               VectorNormalize(sv);
+               f = -DotProduct(tv, n);
+               VectorMA(tv, f, n, tv);
+               VectorNormalize(tv);
        }
-       // now we could divide the vectors by the number of averaged values on
-       // each vertex...  but instead normalize them
-       // 4 assignments, 1 divide, 1 sqrt, 2 adds, 6 multiplies
-       if (svector3f)
-               for (i = 0, v = svector3f + 3 * firstvertex;i < numvertices;i++, v += 3)
-                       VectorNormalize(v);
-       // 4 assignments, 1 divide, 1 sqrt, 2 adds, 6 multiplies
-       if (tvector3f)
-               for (i = 0, v = tvector3f + 3 * firstvertex;i < numvertices;i++, v += 3)
-                       VectorNormalize(v);
-       // 4 assignments, 1 divide, 1 sqrt, 2 adds, 6 multiplies
-       if (normal3f)
-               for (i = 0, v = normal3f + 3 * firstvertex;i < numvertices;i++, v += 3)
-                       VectorNormalize(v);
 }
 
-surfmesh_t *Mod_AllocSurfMesh(mempool_t *mempool, int numvertices, int numtriangles, qboolean lightmapoffsets, qboolean vertexcolors, qboolean neighbors)
+void Mod_AllocSurfMesh(mempool_t *mempool, int numvertices, int numtriangles, qboolean lightmapoffsets, qboolean vertexcolors, qboolean neighbors)
 {
-       surfmesh_t *mesh;
        unsigned char *data;
-       mesh = (surfmesh_t *)Mem_Alloc(mempool, sizeof(surfmesh_t) + numvertices * (3 + 3 + 3 + 3 + 2 + 2 + (vertexcolors ? 4 : 0)) * sizeof(float) + numvertices * (lightmapoffsets ? 1 : 0) * sizeof(int) + numtriangles * (3 + (neighbors ? 3 : 0)) * sizeof(int));
-       mesh->num_vertices = numvertices;
-       mesh->num_triangles = numtriangles;
-       data = (unsigned char *)(mesh + 1);
-       if (mesh->num_vertices)
+       data = (unsigned char *)Mem_Alloc(mempool, numvertices * (3 + 3 + 3 + 3 + 2 + 2 + (vertexcolors ? 4 : 0)) * sizeof(float) + numvertices * (lightmapoffsets ? 1 : 0) * sizeof(int) + numtriangles * (3 + (neighbors ? 3 : 0)) * sizeof(int));
+       loadmodel->surfmesh.num_vertices = numvertices;
+       loadmodel->surfmesh.num_triangles = numtriangles;
+       if (loadmodel->surfmesh.num_vertices)
        {
-               mesh->data_vertex3f = (float *)data, data += sizeof(float[3]) * mesh->num_vertices;
-               mesh->data_svector3f = (float *)data, data += sizeof(float[3]) * mesh->num_vertices;
-               mesh->data_tvector3f = (float *)data, data += sizeof(float[3]) * mesh->num_vertices;
-               mesh->data_normal3f = (float *)data, data += sizeof(float[3]) * mesh->num_vertices;
-               mesh->data_texcoordtexture2f = (float *)data, data += sizeof(float[2]) * mesh->num_vertices;
-               mesh->data_texcoordlightmap2f = (float *)data, data += sizeof(float[2]) * mesh->num_vertices;
+               loadmodel->surfmesh.data_vertex3f = (float *)data, data += sizeof(float[3]) * loadmodel->surfmesh.num_vertices;
+               loadmodel->surfmesh.data_svector3f = (float *)data, data += sizeof(float[3]) * loadmodel->surfmesh.num_vertices;
+               loadmodel->surfmesh.data_tvector3f = (float *)data, data += sizeof(float[3]) * loadmodel->surfmesh.num_vertices;
+               loadmodel->surfmesh.data_normal3f = (float *)data, data += sizeof(float[3]) * loadmodel->surfmesh.num_vertices;
+               loadmodel->surfmesh.data_texcoordtexture2f = (float *)data, data += sizeof(float[2]) * loadmodel->surfmesh.num_vertices;
+               loadmodel->surfmesh.data_texcoordlightmap2f = (float *)data, data += sizeof(float[2]) * loadmodel->surfmesh.num_vertices;
                if (vertexcolors)
-                       mesh->data_lightmapcolor4f = (float *)data, data += sizeof(float[4]) * mesh->num_vertices;
+                       loadmodel->surfmesh.data_lightmapcolor4f = (float *)data, data += sizeof(float[4]) * loadmodel->surfmesh.num_vertices;
                if (lightmapoffsets)
-                       mesh->data_lightmapoffsets = (int *)data, data += sizeof(int) * mesh->num_vertices;
+                       loadmodel->surfmesh.data_lightmapoffsets = (int *)data, data += sizeof(int) * loadmodel->surfmesh.num_vertices;
        }
-       if (mesh->num_triangles)
+       if (loadmodel->surfmesh.num_triangles)
        {
-               mesh->data_element3i = (int *)data, data += sizeof(int[3]) * mesh->num_triangles;
+               loadmodel->surfmesh.data_element3i = (int *)data, data += sizeof(int[3]) * loadmodel->surfmesh.num_triangles;
                if (neighbors)
-                       mesh->data_neighbor3i = (int *)data, data += sizeof(int[3]) * mesh->num_triangles;
+                       loadmodel->surfmesh.data_neighbor3i = (int *)data, data += sizeof(int[3]) * loadmodel->surfmesh.num_triangles;
        }
-       return mesh;
 }
 
 shadowmesh_t *Mod_ShadowMesh_Alloc(mempool_t *mempool, int maxverts, int maxtriangles, rtexture_t *map_diffuse, rtexture_t *map_specular, rtexture_t *map_normal, int light, int neighbors, int expandable)
@@ -1047,7 +1022,7 @@ int Mod_LoadSkinFrame_Internal(skinframe_t *skinframe, const char *basename, int
                                        break;
                        if (i < width * height * 4)
                        {
-                               unsigned char *fogpixels = Mem_Alloc(loadmodel->mempool, width * height * 4);
+                               unsigned char *fogpixels = (unsigned char *)Mem_Alloc(loadmodel->mempool, width * height * 4);
                                memcpy(fogpixels, skindata, width * height * 4);
                                for (i = 0;i < width * height * 4;i += 4)
                                        fogpixels[i] = fogpixels[i+1] = fogpixels[i+2] = 255;