+ for(i = 0; i < patchtesscount; ++i)
+ {
+ 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)
+ {
+ 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)
+ {
+ 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;
+ }
+
+ if(xtess <= 1 && ytess <= 1)
+ break;
+
+ if(numvertices > min(r_subdivisions_maxvertices.integer, 262144) * groupsize)
+ {
+ if (xtess > ytess)
+ xtess--;
+ else
+ ytess--;
+ continue; // try again
+ }
+
+ break;
+ }
+
+ // 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;
+
+ patchtess[j].grouped = true;
+ patchtess[j].xtess = xtess;
+ patchtess[j].ytess = ytess;
+ patchtess[j].cxtess = cxtess;
+ patchtess[j].cytess = cytess;
+ }
+ }
+