+// 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)
+void Q3PatchTriangleElements(int *elements, int width, int height, int firstvertex)
+{
+ int x, y, row0, row1;
+ for (y = 0;y < height - 1;y++)
+ {
+ row0 = firstvertex + (y + 0) * width;
+ row1 = firstvertex + (y + 1) * width;
+ for (x = 0;x < width - 1;x++)
+ {
+ *elements++ = row0;
+ *elements++ = row1;
+ *elements++ = row0 + 1;
+ *elements++ = row1;
+ *elements++ = row1 + 1;
+ *elements++ = row0 + 1;
+ row0++;
+ row1++;
+ }
+ }
+}