]> git.xonotic.org Git - xonotic/darkplaces.git/commitdiff
Experimental new patch welding code by "someone", fixing the problem with seams when...
authordivverent <divverent@d7cf8633-e32d-0410-b094-e92efae38249>
Sat, 14 Mar 2009 18:31:04 +0000 (18:31 +0000)
committerdivverent <divverent@d7cf8633-e32d-0410-b094-e92efae38249>
Sat, 14 Mar 2009 18:31:04 +0000 (18:31 +0000)
Also adds a mode for flat patch sides (so they no longer get tesselated at all).

http://www.alientrap.org/forum/viewtopic.php?t=4200

The submitted patch cleaned up a little, but it's probably not perfect yet.

PLEASE READ, VERIFY AND TEST.

git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@8808 d7cf8633-e32d-0410-b094-e92efae38249

curves.c
curves.h
model_brush.c

index 3a9e22a1f26ec4c38243e8967a4dd173a31aae24..e4ef52ca1ca997c2ded8fe40953de196a756de8b 100644 (file)
--- a/curves.c
+++ b/curves.c
@@ -40,17 +40,35 @@ double bsplinesample(int dimensions, double t, double *param)
 */
 
 #include "quakedef.h"
+#include "mathlib.h"
 
 #include <math.h>
 #include "curves.h"
 
+// Calculate number of resulting vertex rows/columns by given patch size and tesselation factor
+// tess=0 means that we reduce detalization of base 3x3 patches by removing middle row and column of vertices
+// "DimForTess" is "DIMension FOR TESSelation factor"
+// NB: tess=0 actually means that tess must be 0.5, but obviously it can't because it is of int type. (so "a*tess"-like code is replaced by "a/2" if tess=0)
+int Q3PatchDimForTess(int size, int tess)
+{
+       if (tess > 0)
+               return (size - 1) * tess + 1;
+       else if (tess == 0)
+               return (size - 1) / 2 + 1;
+       else
+               return 0; // Maybe warn about wrong tess here?
+}
+
 // usage:
 // to expand a 5x5 patch to 21x21 vertices (4x4 tesselation), one might use this call:
 // Q3PatchSubdivideFloat(3, sizeof(float[3]), outvertices, 5, 5, sizeof(float[3]), patchvertices, 4, 4);
 void Q3PatchTesselateFloat(int numcomponents, int outputstride, float *outputvertices, int patchwidth, int patchheight, int inputstride, float *patchvertices, int tesselationwidth, int tesselationheight)
 {
-       int k, l, x, y, component, outputwidth = (patchwidth-1)*tesselationwidth+1;
+       int k, l, x, y, component, outputwidth = Q3PatchDimForTess(patchwidth, tesselationwidth);
        float px, py, *v, a, b, c, *cp[3][3], temp[3][64];
+       int xmax = max(1, 2*tesselationwidth);
+       int ymax = max(1, 2*tesselationheight);
+       
        // iterate over the individual 3x3 quadratic spline surfaces one at a time
        // expanding them to fill the output array (with some overlap to ensure
        // the edges are filled)
@@ -63,11 +81,11 @@ void Q3PatchTesselateFloat(int numcomponents, int outputstride, float *outputver
                                for (x = 0;x < 3;x++)
                                        cp[y][x] = (float *)((unsigned char *)patchvertices + ((k+y)*patchwidth+(l+x)) * inputstride);
                        // for each row...
-                       for (y = 0;y <= tesselationheight*2;y++)
+                       for (y = 0;y <= ymax;y++)
                        {
                                // calculate control points for this row by collapsing the 3
                                // rows of control points to one row using py
-                               py = (float)y / (float)(tesselationheight*2);
+                               py = (float)y / (float)ymax;
                                // calculate quadratic spline weights for py
                                a = ((1.0f - py) * (1.0f - py));
                                b = ((1.0f - py) * (2.0f * py));
@@ -79,12 +97,12 @@ void Q3PatchTesselateFloat(int numcomponents, int outputstride, float *outputver
                                        temp[2][component] = cp[0][2][component] * a + cp[1][2][component] * b + cp[2][2][component] * c;
                                }
                                // fetch a pointer to the beginning of the output vertex row
-                               v = (float *)((unsigned char *)outputvertices + ((k * tesselationheight + y) * outputwidth + l * tesselationwidth) * outputstride);
+                               v = (float *)((unsigned char *)outputvertices + ((k * ymax / 2 + y) * outputwidth + l * xmax / 2) * outputstride);
                                // for each column of the row...
-                               for (x = 0;x <= (tesselationwidth*2);x++)
+                               for (x = 0;x <= xmax;x++)
                                {
                                        // calculate point based on the row control points
-                                       px = (float)x / (float)(tesselationwidth*2);
+                                       px = (float)x / (float)xmax;
                                        // calculate quadratic spline weights for px
                                        // (could be precalculated)
                                        a = ((1.0f - px) * (1.0f - px));
@@ -118,6 +136,25 @@ void Q3PatchTesselateFloat(int numcomponents, int outputstride, float *outputver
 #endif
 }
 
+static int Q3PatchTesselation(float bestsquareddeviation, float tolerance)
+{
+       float f;
+       f = sqrt(bestsquareddeviation) / tolerance;
+       //if(f < 0.25) // REALLY flat patches
+       if(f < 0.0001) // TOTALLY flat patches
+               return 0;
+       else if(f < 2)
+               return 1;
+       else
+               return (int) floor(log(f) / log(2)) + 1;
+               // this is always at least 2
+               // maps [0.25..0.5[ to -1 (actually, 1 is returned)
+               // maps [0.5..1[ to 0 (actually, 1 is returned)
+               // maps [1..2[ to 1
+               // maps [2..4[ to 2
+               // maps [4..8[ to 4
+}
+
 // returns how much tesselation of each segment is needed to remain under tolerance
 int Q3PatchTesselationOnX(int patchwidth, int patchheight, int components, const float *in, float tolerance)
 {
@@ -139,7 +176,7 @@ int Q3PatchTesselationOnX(int patchwidth, int patchheight, int components, const
                                bestsquareddeviation = squareddeviation;
                }
        }
-       return (int)floor(log(sqrt(bestsquareddeviation) / tolerance) / log(2)) + 1;
+       return Q3PatchTesselation(bestsquareddeviation, tolerance);
 }
 
 // returns how much tesselation of each segment is needed to remain under tolerance
@@ -163,13 +200,137 @@ int Q3PatchTesselationOnY(int patchwidth, int patchheight, int components, const
                                bestsquareddeviation = squareddeviation;
                }
        }
-       return (int)floor(log(sqrt(bestsquareddeviation) / tolerance) / log(2)) + 1;
+       return Q3PatchTesselation(bestsquareddeviation, tolerance);
 }
 
+// Find an equal vertex in array. Check only vertices with odd X and Y
+static int FindEqualOddVertexInArray(int numcomponents, float *vertex, float *vertices, int width, int height)
+{
+       int x, y, j;
+       for (y=0; y<height; y+=2)
+       {
+               for (x=0; x<width; x+=2)
+               {
+                       qboolean found = true;
+                       for (j=0; j<numcomponents; j++)
+                               if (fabs(*(vertex+j) - *(vertices+j)) > 0.05)
+                               // div0: this is notably smaller than the smallest radiant grid
+                               // but large enough so we don't need to get scared of roundoff
+                               // errors
+                               {
+                                       found = false;
+                                       break;
+                               }
+                       if(found)
+                               return y*width+x;
+                       vertices += numcomponents*2;
+               }
+               vertices += numcomponents*(width-1);
+       }
+       return -1;
+}
+
+#define SIDE_INVALID -1
+#define SIDE_X 0
+#define SIDE_Y 1
+
+static int GetSide(int p1, int p2, int width, int height, int *pointdist)
+{
+       int x1 = p1 % width, y1 = p1 / width;
+       int x2 = p2 % width, y2 = p2 / width;
+       if (p1 < 0 || p2 < 0)
+               return SIDE_INVALID;
+       if (x1 == x2)
+       {
+               if (y1 != y2)
+               {
+                       *pointdist = abs(y2 - y1);
+                       return SIDE_Y;
+               }
+               else
+                       return SIDE_INVALID;
+       }
+       else if (y1 == y2)
+       {
+               *pointdist = abs(x2 - x1);
+               return SIDE_X;
+       }
+       else
+               return SIDE_INVALID;
+}
+
+// Increase tesselation of one of two touching patches to make a seamless connection between them
+// Returns 0 in case if patches were not modified, otherwise 1
+int Q3PatchAdjustTesselation(int numcomponents, patchinfo_t *patch1, float *patchvertices1, patchinfo_t *patch2, float *patchvertices2)
+{
+       // what we are doing here is:
+       //   we take for each corner of one patch
+       //   and check if the other patch contains that corner
+       //   once we have a pair of such matches
+
+       struct {int id1,id2;} commonverts[8];
+       int i, j, k, side1, side2, *tess1, *tess2;
+       int dist1, dist2;
+       qboolean modified = false;
+
+       // Potential paired vertices (corners of the first patch)
+       commonverts[0].id1 = 0;
+       commonverts[1].id1 = patch1->xsize-1;
+       commonverts[2].id1 = patch1->xsize*(patch1->ysize-1);
+       commonverts[3].id1 = patch1->xsize*patch1->ysize-1;
+       for (i=0;i<4;++i)
+               commonverts[i].id2 = FindEqualOddVertexInArray(numcomponents, patchvertices1+numcomponents*commonverts[i].id1, patchvertices2, patch2->xsize, patch2->ysize);
+
+       // Corners of the second patch
+       commonverts[4].id2 = 0;
+       commonverts[5].id2 = patch2->xsize-1;
+       commonverts[6].id2 = patch2->xsize*(patch2->ysize-1);
+       commonverts[7].id2 = patch2->xsize*patch2->ysize-1;
+       for (i=4;i<8;++i)
+               commonverts[i].id1 = FindEqualOddVertexInArray(numcomponents, patchvertices2+numcomponents*commonverts[i].id2, patchvertices1, patch1->xsize, patch1->ysize);
+
+       for (i=0;i<8;++i)
+               for (j=i+1;j<8;++j)
+               {
+                       side1 = GetSide(commonverts[i].id1,commonverts[j].id1,patch1->xsize,patch1->ysize,&dist1);
+                       side2 = GetSide(commonverts[i].id2,commonverts[j].id2,patch2->xsize,patch2->ysize,&dist2);
+
+                       if (side1 == SIDE_INVALID || side2 == SIDE_INVALID)
+                               continue;
+
+                       if(dist1 != dist2)
+                       {
+                               // no patch welding if the resolutions mismatch
+                               continue;
+                       }
+
+                       // Update every lod level
+                       for (k=0;k<PATCH_LODS_NUM;++k)
+                       {
+                               tess1 = side1 == SIDE_X ? &patch1->lods[k].xtess : &patch1->lods[k].ytess;
+                               tess2 = side2 == SIDE_X ? &patch2->lods[k].xtess : &patch2->lods[k].ytess;
+                               if (*tess1 != *tess2)
+                               {
+                                       if (*tess1 < *tess2)
+                                               *tess1 = *tess2;
+                                       else
+                                               *tess2 = *tess1;
+                                       modified = true;
+                               }
+                       }
+               }
+
+       return modified;
+}
+
+#undef SIDE_INVALID 
+#undef SIDE_X
+#undef SIDE_Y
+
 // calculates elements for a grid of vertices
 // (such as those produced by Q3PatchTesselate)
 // (note: width and height are the actual vertex size, this produces
-//  (width-1)*(height-1)*2 triangles, 3 elements each)
+// (width-1)*(height-1)*2 triangles, 3 elements each)
 void Q3PatchTriangleElements(int *elements, int width, int height, int firstvertex)
 {
        int x, y, row0, row1;
index af9ba15044c8c09babb08aed7d1f0d18428a26fe..6555a5a01ed86292bca9eb7c95d40adc249456fe 100644 (file)
--- a/curves.h
+++ b/curves.h
@@ -2,6 +2,23 @@
 #ifndef CURVES_H
 #define CURVES_H
 
+#define PATCH_LODS_NUM 2
+#define PATCH_LOD_COLLISION 0
+#define PATCH_LOD_VISUAL 1
+
+typedef struct patchinfo_s
+{
+       int xsize, ysize;
+       struct {
+               int xtess, ytess;
+       } lods[PATCH_LODS_NUM];
+} patchinfo_t;
+
+// Calculate number of resulting vertex rows/columns by given patch size and tesselation factor
+// When tess=0 it means that we reduce detalization of base 3x3 patches by removing middle row and column
+// "DimForTess" is "DIMension FOR TESSelation factor"
+int Q3PatchDimForTess(int size, int tess);
+
 // usage:
 // to expand a 5x5 patch to 21x21 vertices (4x4 tesselation), one might use this call:
 // Q3PatchSubdivideFloat(3, sizeof(float[3]), outvertices, 5, 5, sizeof(float[3]), patchvertices, 4, 4);
@@ -16,5 +33,7 @@ int Q3PatchTesselationOnY(int patchwidth, int patchheight, int components, const
 //  (width-1)*(height-1)*2 triangles, 3 elements each)
 void Q3PatchTriangleElements(int *elements, int width, int height, int firstvertex);
 
+int Q3PatchAdjustTesselation(int numcomponents, patchinfo_t *patch1, float *patchvertices1, patchinfo_t *patch2, float *patchvertices2);
+
 #endif
 
index 1a92b2f5ae73c1815015e5268d6c214d084c7ecb..df122f60f221eb99685e3bceb21f953973b94138 100644 (file)
@@ -32,11 +32,11 @@ cvar_t r_novis = {0, "r_novis", "0", "draws whole level, see also sv_cullentitie
 cvar_t r_picmipworld = {CVAR_SAVE, "r_picmipworld", "1", "whether gl_picmip shall apply to world textures too"};
 cvar_t r_nosurftextures = {0, "r_nosurftextures", "0", "pretends there was no texture lump found in the q1bsp/hlbsp loading (useful for debugging this rare case)"};
 cvar_t r_subdivisions_tolerance = {0, "r_subdivisions_tolerance", "4", "maximum error tolerance on curve subdivision for rendering purposes (in other words, the curves will be given as many polygons as necessary to represent curves at this quality)"};
-cvar_t r_subdivisions_mintess = {0, "r_subdivisions_mintess", "1", "minimum number of subdivisions (values above 1 will smooth curves that don't need it)"};
+cvar_t r_subdivisions_mintess = {0, "r_subdivisions_mintess", "0", "minimum number of subdivisions (values above 0 will smooth curves that don't need it)"};
 cvar_t r_subdivisions_maxtess = {0, "r_subdivisions_maxtess", "1024", "maximum number of subdivisions (prevents curves beyond a certain detail level, limits smoothing)"};
 cvar_t r_subdivisions_maxvertices = {0, "r_subdivisions_maxvertices", "65536", "maximum vertices allowed per subdivided curve"};
 cvar_t r_subdivisions_collision_tolerance = {0, "r_subdivisions_collision_tolerance", "15", "maximum error tolerance on curve subdivision for collision purposes (usually a larger error tolerance than for rendering)"};
-cvar_t r_subdivisions_collision_mintess = {0, "r_subdivisions_collision_mintess", "1", "minimum number of subdivisions (values above 1 will smooth curves that don't need it)"};
+cvar_t r_subdivisions_collision_mintess = {0, "r_subdivisions_collision_mintess", "0", "minimum number of subdivisions (values above 0 will smooth curves that don't need it)"};
 cvar_t r_subdivisions_collision_maxtess = {0, "r_subdivisions_collision_maxtess", "1024", "maximum number of subdivisions (prevents curves beyond a certain detail level, limits smoothing)"};
 cvar_t r_subdivisions_collision_maxvertices = {0, "r_subdivisions_collision_maxvertices", "4225", "maximum vertices allowed per subdivided curve"};
 cvar_t mod_q3bsp_curves_collisions = {0, "mod_q3bsp_curves_collisions", "1", "enables collisions with curves (SLOW)"};
@@ -4675,15 +4675,13 @@ static void Mod_Q3BSP_LoadLightmaps(lump_t *l, lump_t *faceslump)
 
 typedef struct patchtess_s
 {
-       qboolean grouped;
+       patchinfo_t info;
+
+       // Auxiliary data used only by patch loading code in Mod_Q3BSP_LoadFaces
        int surface_id;
-       int xtess, ytess;
-       int xsize, ysize;
-       int cxtess, cytess;
-       int cxsize, cysize;
        float lodgroup[6];
-}
-patchtess_t;
+       float *originalvertex3f;
+patchtess_t;
 
 #define PATCHTESS_SAME_LODGROUP(a,b) \
        ( \
@@ -4699,7 +4697,7 @@ static void Mod_Q3BSP_LoadFaces(lump_t *l)
 {
        q3dface_t *in, *oldin;
        msurface_t *out, *oldout;
-       int i, oldi, j, n, count, invalidelements, patchsize[2], finalwidth, finalheight, xtess, ytess, finalvertices, finaltriangles, firstvertex, firstelement, type, oldnumtriangles, oldnumtriangles2, meshvertices, meshtriangles, numvertices, numtriangles, groupsize, cxtess, cytess;
+       int i, oldi, j, n, count, invalidelements, patchsize[2], finalwidth, finalheight, xtess, ytess, finalvertices, finaltriangles, firstvertex, firstelement, type, oldnumtriangles, oldnumtriangles2, meshvertices, meshtriangles, numvertices, numtriangles, cxtess, cytess;
        float lightmaptcbase[2], lightmaptcscale[2];
        //int *originalelement3i;
        //int *originalneighbor3i;
@@ -4713,6 +4711,7 @@ static void Mod_Q3BSP_LoadFaces(lump_t *l)
        float *v;
        patchtess_t *patchtess = NULL;
        int patchtesscount = 0;
+       qboolean again;
 
        in = (q3dface_t *)(mod_base + l->fileofs);
        if (l->filelen % sizeof(*in))
@@ -4827,8 +4826,8 @@ static void Mod_Q3BSP_LoadFaces(lump_t *l)
                        xtess = bound(r_subdivisions_mintess.integer, xtess, r_subdivisions_maxtess.integer);
                        ytess = bound(r_subdivisions_mintess.integer, ytess, r_subdivisions_maxtess.integer);
                        // bound to sanity settings
-                       xtess = bound(1, xtess, 1024);
-                       ytess = bound(1, ytess, 1024);
+                       xtess = bound(0, xtess, 1024);
+                       ytess = bound(0, ytess, 1024);
 
                        // lower quality collision patches! Same procedure as before, but different cvars
                        // convert patch to Q3FACETYPE_MESH
@@ -4838,25 +4837,25 @@ static void Mod_Q3BSP_LoadFaces(lump_t *l)
                        cxtess = bound(r_subdivisions_collision_mintess.integer, cxtess, r_subdivisions_collision_maxtess.integer);
                        cytess = bound(r_subdivisions_collision_mintess.integer, cytess, r_subdivisions_collision_maxtess.integer);
                        // bound to sanity settings
-                       cxtess = bound(1, cxtess, 1024);
-                       cytess = bound(1, cytess, 1024);
+                       cxtess = bound(0, cxtess, 1024);
+                       cytess = bound(0, cytess, 1024);
 
                        // store it for the LOD grouping step
+                       patchtess[patchtesscount].info.xsize = patchsize[0];
+                       patchtess[patchtesscount].info.ysize = patchsize[1];
+                       patchtess[patchtesscount].info.lods[PATCH_LOD_VISUAL].xtess = xtess;
+                       patchtess[patchtesscount].info.lods[PATCH_LOD_VISUAL].ytess = ytess;
+                       patchtess[patchtesscount].info.lods[PATCH_LOD_COLLISION].xtess = cxtess;
+                       patchtess[patchtesscount].info.lods[PATCH_LOD_COLLISION].ytess = cytess;
+       
                        patchtess[patchtesscount].surface_id = i;
-                       patchtess[patchtesscount].grouped = false;
-                       patchtess[patchtesscount].xtess = xtess;
-                       patchtess[patchtesscount].ytess = ytess;
-                       patchtess[patchtesscount].xsize = patchsize[0];
-                       patchtess[patchtesscount].ysize = patchsize[1];
                        patchtess[patchtesscount].lodgroup[0] = in->specific.patch.mins[0];
                        patchtess[patchtesscount].lodgroup[1] = in->specific.patch.mins[1];
                        patchtess[patchtesscount].lodgroup[2] = in->specific.patch.mins[2];
                        patchtess[patchtesscount].lodgroup[3] = in->specific.patch.maxs[0];
                        patchtess[patchtesscount].lodgroup[4] = in->specific.patch.maxs[1];
                        patchtess[patchtesscount].lodgroup[5] = in->specific.patch.maxs[2];
-
-                       patchtess[patchtesscount].cxtess = cxtess;
-                       patchtess[patchtesscount].cytess = cytess;
+                       patchtess[patchtesscount].originalvertex3f = originalvertex3f;
                        ++patchtesscount;
                        break;
                case Q3FACETYPE_FLARE:
@@ -4871,129 +4870,36 @@ static void Mod_Q3BSP_LoadFaces(lump_t *l)
                meshtriangles += out->num_triangles;
        }
 
-       for(i = 0; i < patchtesscount; ++i)
+       // Fix patches tesselations so that they make no seams
+       do
        {
-               if(patchtess[i].grouped) // already grouped
-                       continue;
-
-               // get the highest required tess parameters for this group
-               groupsize = 0;
-               xtess = ytess = 0;
-               cxtess = cytess = 0;
-               for(j = 0; j < patchtesscount; ++j)
+               again = false;
+               for(i = 0; i < patchtesscount; ++i)
                {
-                       if(patchtess[j].grouped) // already grouped
-                               continue;
-                       if(!PATCHTESS_SAME_LODGROUP(patchtess[i], patchtess[j]))
-                               continue;
-                       if(patchtess[j].xtess > xtess)
-                               xtess = patchtess[j].xtess;
-                       if(patchtess[j].ytess > ytess)
-                               ytess = patchtess[j].ytess;
-                       if(patchtess[j].cxtess > cxtess)
-                               cxtess = patchtess[j].cxtess;
-                       if(patchtess[j].cytess > cytess)
-                               cytess = patchtess[j].cytess;
-                       ++groupsize;
-               }
-
-               if(groupsize == 0)
-               {
-                       Con_Printf("ERROR: patch %d isn't in any LOD group (1)?!?\n", patchtess[i].surface_id);
-                       continue;
-               }
-
-               // bound to user limit on vertices
-               for(;;)
-               {
-                       numvertices = 0;
-                       numtriangles = 0;
-
-                       for(j = 0; j < patchtesscount; ++j)
+                       for(j = i+1; j < patchtesscount; ++j)
                        {
-                               if(patchtess[j].grouped) // already grouped
+                               if (!PATCHTESS_SAME_LODGROUP(patchtess[i], patchtess[j]))
                                        continue;
-                               if(!PATCHTESS_SAME_LODGROUP(patchtess[i], patchtess[j]))
-                                       continue;
-
-                               finalwidth = (patchtess[j].xsize - 1) * xtess + 1;
-                               finalheight = (patchtess[j].ysize - 1) * ytess + 1;
-                               numvertices += finalwidth * finalheight;
-                               numtriangles += (finalwidth - 1) * (finalheight - 1) * 2;
-                       }
 
-                       if(xtess <= 1 && ytess <= 1)
-                               break;
-
-                       if(numvertices > min(r_subdivisions_maxvertices.integer, 262144) * groupsize)
-                       {
-                               if (xtess > ytess)
-                                       xtess--;
-                               else
-                                       ytess--;
-                               continue; // try again
+                               if (Q3PatchAdjustTesselation(3, &patchtess[i].info, patchtess[i].originalvertex3f, &patchtess[j].info, patchtess[j].originalvertex3f) )
+                                       again = true;
                        }
-
-                       break;
                }
+       }
+       while (again);
 
-               // bound to user limit on vertices (collision)
-               for(;;)
-               {
-                       numvertices = 0;
-                       numtriangles = 0;
-
-                       for(j = 0; j < patchtesscount; ++j)
-                       {
-                               if(patchtess[j].grouped) // already grouped
-                                       continue;
-                               if(!PATCHTESS_SAME_LODGROUP(patchtess[i], patchtess[j]))
-                                       continue;
-
-                               finalwidth = (patchtess[j].xsize - 1) * cxtess + 1;
-                               finalheight = (patchtess[j].ysize - 1) * cytess + 1;
-                               numvertices += finalwidth * finalheight;
-                               numtriangles += (finalwidth - 1) * (finalheight - 1) * 2;
-                       }
-
-                       if(cxtess <= 1 && cytess <= 1)
-                               break;
-
-                       if(numvertices > min(r_subdivisions_collision_maxvertices.integer, 262144) * groupsize)
-                       {
-                               if (cxtess > cytess)
-                                       cxtess--;
-                               else
-                                       cytess--;
-                               continue; // try again
-                       }
-
-                       break;
-               }
-
-               for(j = 0; j < patchtesscount; ++j)
-               {
-                       if(patchtess[j].grouped) // already grouped
-                               continue;
-                       if(!PATCHTESS_SAME_LODGROUP(patchtess[i], patchtess[j]))
-                               continue;
-
-                       finalwidth = (patchtess[j].xsize - 1) * xtess + 1;
-                       finalheight = (patchtess[j].ysize - 1) * ytess + 1;
-                       numvertices = finalwidth * finalheight;
-                       numtriangles = (finalwidth - 1) * (finalheight - 1) * 2;
-
-                       oldout[patchtess[j].surface_id].num_vertices = numvertices;
-                       oldout[patchtess[j].surface_id].num_triangles = numtriangles;
-                       meshvertices += oldout[patchtess[j].surface_id].num_vertices;
-                       meshtriangles += oldout[patchtess[j].surface_id].num_triangles;
+       // Calculate resulting number of triangles
+       for(i = 0; i < patchtesscount; ++i)
+       {
+               finalwidth = Q3PatchDimForTess(patchtess[i].info.xsize, patchtess[i].info.lods[PATCH_LOD_VISUAL].xtess);
+               finalheight = Q3PatchDimForTess(patchtess[i].info.ysize,patchtess[i].info.lods[PATCH_LOD_VISUAL].ytess);
+               numvertices = finalwidth * finalheight;
+               numtriangles = (finalwidth - 1) * (finalheight - 1) * 2;
 
-                       patchtess[j].grouped = true;
-                       patchtess[j].xtess = xtess;
-                       patchtess[j].ytess = ytess;
-                       patchtess[j].cxtess = cxtess;
-                       patchtess[j].cytess = cytess;
-               }
+               oldout[patchtess[i].surface_id].num_vertices = numvertices;
+               oldout[patchtess[i].surface_id].num_triangles = numtriangles;
+               meshvertices += oldout[patchtess[i].surface_id].num_vertices;
+               meshtriangles += oldout[patchtess[i].surface_id].num_triangles;
        }
 
        i = oldi;
@@ -5046,24 +4952,24 @@ static void Mod_Q3BSP_LoadFaces(lump_t *l)
                        originaltexcoordlightmap2f = loadmodel->brushq3.data_texcoordlightmap2f + firstvertex * 2;
                        originalcolor4f = loadmodel->brushq3.data_color4f + firstvertex * 4;
 
-                       xtess = ytess = cxtess = cytess = 0;
+                       xtess = ytess = cxtess = cytess = -1;
                        for(j = 0; j < patchtesscount; ++j)
-                               if(patchtess[j].grouped && patchtess[j].surface_id == i)
+                               if(patchtess[j].surface_id == i)
                                {
-                                       xtess = patchtess[j].xtess;
-                                       ytess = patchtess[j].ytess;
-                                       cxtess = patchtess[j].cxtess;
-                                       cytess = patchtess[j].cytess;
+                                       xtess = patchtess[j].info.lods[PATCH_LOD_VISUAL].xtess;
+                                       ytess = patchtess[j].info.lods[PATCH_LOD_VISUAL].ytess;
+                                       cxtess = patchtess[j].info.lods[PATCH_LOD_COLLISION].xtess;
+                                       cytess = patchtess[j].info.lods[PATCH_LOD_COLLISION].ytess;
                                        break;
                                }
-                       if(xtess == 0)
+                       if(xtess == -1)
                        {
-                               Con_Printf("ERROR: patch %d isn't in any LOD group (2)?!?\n", i);
-                               xtess = ytess = cxtess = cytess = 1;
+                               Con_Printf("ERROR: patch %d isn't preprocessed?!?\n", i);
+                               xtess = ytess = cxtess = cytess = 0;
                        }
 
-                       finalwidth = ((patchsize[0] - 1) * xtess) + 1;
-                       finalheight = ((patchsize[1] - 1) * ytess) + 1;
+                       finalwidth = Q3PatchDimForTess(patchsize[0],xtess); //((patchsize[0] - 1) * xtess) + 1;
+                       finalheight = Q3PatchDimForTess(patchsize[1],ytess); //((patchsize[1] - 1) * ytess) + 1;
                        finalvertices = finalwidth * finalheight;
                        finaltriangles = (finalwidth - 1) * (finalheight - 1) * 2;
                        type = Q3FACETYPE_MESH;
@@ -5075,7 +4981,9 @@ static void Mod_Q3BSP_LoadFaces(lump_t *l)
                        Q3PatchTesselateFloat(2, sizeof(float[2]), (loadmodel->surfmesh.data_texcoordlightmap2f + 2 * out->num_firstvertex), patchsize[0], patchsize[1], sizeof(float[2]), originaltexcoordlightmap2f, xtess, ytess);
                        Q3PatchTesselateFloat(4, sizeof(float[4]), (loadmodel->surfmesh.data_lightmapcolor4f + 4 * out->num_firstvertex), patchsize[0], patchsize[1], sizeof(float[4]), originalcolor4f, xtess, ytess);
                        Q3PatchTriangleElements((loadmodel->surfmesh.data_element3i + 3 * out->num_firsttriangle), finalwidth, finalheight, out->num_firstvertex);
+
                        out->num_triangles = Mod_RemoveDegenerateTriangles(out->num_triangles, (loadmodel->surfmesh.data_element3i + 3 * out->num_firsttriangle), (loadmodel->surfmesh.data_element3i + 3 * out->num_firsttriangle), loadmodel->surfmesh.data_vertex3f);
+
                        if (developer.integer >= 100)
                        {
                                if (out->num_triangles < finaltriangles)
@@ -5085,8 +4993,8 @@ static void Mod_Q3BSP_LoadFaces(lump_t *l)
                        }
                        // q3map does not put in collision brushes for curves... ugh
                        // build the lower quality collision geometry
-                       finalwidth = ((patchsize[0] - 1) * cxtess) + 1;
-                       finalheight = ((patchsize[1] - 1) * cytess) + 1;
+                       finalwidth = Q3PatchDimForTess(patchsize[0],cxtess); //((patchsize[0] - 1) * cxtess) + 1;
+                       finalheight = Q3PatchDimForTess(patchsize[1],cytess); //((patchsize[1] - 1) * cytess) + 1;
                        finalvertices = finalwidth * finalheight;
                        finaltriangles = (finalwidth - 1) * (finalheight - 1) * 2;