]> de.git.xonotic.org Git - xonotic/netradiant.git/blobdiff - contrib/gtkgensurf/face.cpp
more eol-style
[xonotic/netradiant.git] / contrib / gtkgensurf / face.cpp
index 6c43c677ad14ce925f8a75f26c03dbe6331d12b0..8a5aa822a6867520e4b9afd068c0782f8a8685b4 100644 (file)
-/*\r
-GenSurf plugin for GtkRadiant\r
-Copyright (C) 2001 David Hyde, Loki software and qeradiant.com\r
-\r
-This library is free software; you can redistribute it and/or\r
-modify it under the terms of the GNU Lesser General Public\r
-License as published by the Free Software Foundation; either\r
-version 2.1 of the License, or (at your option) any later version.\r
-\r
-This library is distributed in the hope that it will be useful,\r
-but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
-Lesser General Public License for more details.\r
-\r
-You should have received a copy of the GNU Lesser General Public\r
-License along with this library; if not, write to the Free Software\r
-Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
-*/\r
-\r
-#include <stdlib.h>\r
-#include <math.h>\r
-#include "gensurf.h"\r
-\r
-#define MAX_FACES 128    // Maximum number of faces on a brush\r
-#define        MAX_POINTS_ON_WINDING   64\r
-#define        SIDE_FRONT              0\r
-#define        SIDE_ON                 2\r
-#define        SIDE_BACK               1\r
-#define        SIDE_CROSS              -2\r
-\r
-vec3 vec3_origin = {0,0,0};\r
-\r
-void PlaneFromPoints (float *p0, float *p1, float *p2, PLANE *plane)\r
-{\r
-       vec3 t1, t2;\r
-       vec     length;\r
-       \r
-       VectorSubtract (p0, p1, t1);\r
-       VectorSubtract (p2, p1, t2);\r
-       plane->normal[0] = t1[1]*t2[2] - t1[2]*t2[1];\r
-       plane->normal[1] = t1[2]*t2[0] - t1[0]*t2[2];\r
-       plane->normal[2] = t1[0]*t2[1] - t1[1]*t2[0];\r
-       \r
-       length = (vec)(sqrt(plane->normal[0]*plane->normal[0] +\r
-                               plane->normal[1]*plane->normal[1] +\r
-                                               plane->normal[2]*plane->normal[2]  ));\r
-       if (length == 0)\r
-       {\r
-               VectorClear(plane->normal);\r
-       }\r
-       else\r
-       {\r
-               plane->normal[0] /= length;\r
-               plane->normal[1] /= length;\r
-               plane->normal[2] /= length;\r
-       }\r
-       plane->dist = DotProduct (p0, plane->normal);\r
-}\r
-\r
-void VectorMA (vec3 va, vec scale, vec3 vb, vec3 vc)\r
-{\r
-       vc[0] = va[0] + scale*vb[0];\r
-       vc[1] = va[1] + scale*vb[1];\r
-       vc[2] = va[2] + scale*vb[2];\r
-}\r
-\r
-void CrossProduct (vec3 v1, vec3 v2, vec3 cross)\r
-{\r
-       cross[0] = v1[1]*v2[2] - v1[2]*v2[1];\r
-       cross[1] = v1[2]*v2[0] - v1[0]*v2[2];\r
-       cross[2] = v1[0]*v2[1] - v1[1]*v2[0];\r
-}\r
-\r
-/*\r
-=============\r
-AllocWinding\r
-=============\r
-*/\r
-MY_WINDING     *AllocWinding (int points)\r
-{\r
-       MY_WINDING      *w;\r
-       int                     s;\r
-\r
-       s = sizeof(vec)*3*points + sizeof(int);\r
-       w = (MY_WINDING*)malloc (s);\r
-       memset (w, 0, s); \r
-       return w;\r
-}\r
-\r
-vec VectorNormalize (vec3 in, vec3 out)\r
-{\r
-       vec     length, ilength;\r
-\r
-       length = (vec)(sqrt (in[0]*in[0] + in[1]*in[1] + in[2]*in[2]));\r
-       if (length == 0)\r
-       {\r
-               VectorClear (out);\r
-               return 0;\r
-       }\r
-\r
-       ilength = (vec)1.0/length;\r
-       out[0] = in[0]*ilength;\r
-       out[1] = in[1]*ilength;\r
-       out[2] = in[2]*ilength;\r
-\r
-       return length;\r
-}\r
-\r
-/*\r
-=================\r
-BaseWindingForPlane\r
-=================\r
-*/\r
-MY_WINDING *BaseWindingForPlane (vec3 normal, vec dist)\r
-{\r
-       int                i, x;\r
-       vec        max, v;\r
-       vec3       org, vright, vup;\r
-       MY_WINDING *w;\r
-\r
-// find the major axis\r
-\r
-       max = -BOGUS_RANGE;\r
-       x = -1;\r
-       for (i=0 ; i<3; i++)\r
-       {\r
-               v = (vec)(fabs(normal[i]));\r
-               if (v > max)\r
-               {\r
-                       x = i;\r
-                       max = v;\r
-               }\r
-       }\r
-       if (x==-1) x = 2;\r
-               \r
-       VectorCopy(vec3_origin,vup);\r
-       switch (x)\r
-       {\r
-       case 0:\r
-       case 1:\r
-               vup[2] = 1;\r
-               break;          \r
-       case 2:\r
-               vup[0] = 1;\r
-               break;          \r
-       }\r
-\r
-       v = DotProduct (vup, normal);\r
-       VectorMA (vup, -v, normal, vup);\r
-       VectorNormalize (vup, vup);\r
-               \r
-       VectorScale (normal, dist, org);\r
-       \r
-       CrossProduct (vup, normal, vright);\r
-       \r
-       VectorScale (vup, 65536, vup);\r
-       VectorScale (vright, 65536, vright);\r
-\r
-// project a really big        axis aligned box onto the plane\r
-       w = AllocWinding (4);\r
-       \r
-       VectorSubtract (org, vright, w->p[0]);\r
-       VectorAdd (w->p[0], vup, w->p[0]);\r
-       \r
-       VectorAdd (org, vright, w->p[1]);\r
-       VectorAdd (w->p[1], vup, w->p[1]);\r
-       \r
-       VectorAdd (org, vright, w->p[2]);\r
-       VectorSubtract (w->p[2], vup, w->p[2]);\r
-       \r
-       VectorSubtract (org, vright, w->p[3]);\r
-       VectorSubtract (w->p[3], vup, w->p[3]);\r
-       \r
-       w->numpoints = 4;\r
-       \r
-       return w;       \r
-}\r
-\r
-void FreeWinding (MY_WINDING *w)\r
-{\r
-       if (*(unsigned *)w == 0xdeaddead)\r
-//             Error ("FreeWinding: freed a freed winding");\r
-               return;\r
-       *(unsigned *)w = 0xdeaddead;\r
-\r
-       free (w);\r
-}\r
-\r
-/*\r
-=============\r
-ChopWindingInPlace\r
-=============\r
-*/\r
-void ChopWindingInPlace (MY_WINDING **inout, vec3 normal, vec dist, vec epsilon)\r
-{\r
-       MY_WINDING *in;\r
-       vec        dists[MAX_POINTS_ON_WINDING+4];\r
-       int        sides[MAX_POINTS_ON_WINDING+4];\r
-       int        counts[3];\r
-       static vec dot;         // VC 4.2 optimizer bug if not static\r
-       int        i, j;\r
-       vec        *p1, *p2;\r
-       vec3       mid;\r
-       MY_WINDING *f;\r
-       int        maxpts;\r
-\r
-       in = *inout;\r
-       counts[0] = counts[1] = counts[2] = 0;\r
-\r
-// determine sides for each point\r
-       for (i=0 ; i<in->numpoints ; i++)\r
-       {\r
-               dot = DotProduct (in->p[i], normal);\r
-               dot -= dist;\r
-               dists[i] = dot;\r
-               if (dot > epsilon)\r
-                       sides[i] = SIDE_FRONT;\r
-               else if (dot < -epsilon)\r
-                       sides[i] = SIDE_BACK;\r
-               else\r
-               {\r
-                       sides[i] = SIDE_ON;\r
-               }\r
-               counts[sides[i]]++;\r
-       }\r
-       sides[i] = sides[0];\r
-       dists[i] = dists[0];\r
-       \r
-       if (!counts[0])\r
-       {\r
-               FreeWinding(in);\r
-               *inout = NULL;\r
-               return;\r
-       }\r
-       if (!counts[1])\r
-               return;         // inout stays the same\r
-\r
-       maxpts = in->numpoints+4;       // cant use counts[0]+2 because\r
-                                                               // of fp grouping errors\r
-\r
-       f = AllocWinding (maxpts);\r
-               \r
-       for (i=0 ; i<in->numpoints ; i++)\r
-       {\r
-               p1 = in->p[i];\r
-               \r
-               if (sides[i] == SIDE_ON)\r
-               {\r
-                       VectorCopy (p1, f->p[f->numpoints]);\r
-                       f->numpoints++;\r
-                       continue;\r
-               }\r
-       \r
-               if (sides[i] == SIDE_FRONT)\r
-               {\r
-                       VectorCopy (p1, f->p[f->numpoints]);\r
-                       f->numpoints++;\r
-               }\r
-\r
-               if (sides[i+1] == SIDE_ON || sides[i+1] == sides[i])\r
-                       continue;\r
-                       \r
-       // generate a split point\r
-               p2 = in->p[(i+1)%in->numpoints];\r
-               \r
-               dot = dists[i] / (dists[i]-dists[i+1]);\r
-               for (j=0 ; j<3 ; j++)\r
-               {       // avoid round off error when possible\r
-                       if (normal[j] == 1)\r
-                               mid[j] = dist;\r
-                       else if (normal[j] == -1)\r
-                               mid[j] = -dist;\r
-                       else\r
-                               mid[j] = p1[j] + dot*(p2[j]-p1[j]);\r
-               }\r
-                       \r
-               VectorCopy (mid, f->p[f->numpoints]);\r
-               f->numpoints++;\r
-       }\r
-       \r
-//     if (f->numpoints > maxpts)\r
-//             Error ("ClipWinding: points exceeded estimate");\r
-//     if (f->numpoints > MAX_POINTS_ON_WINDING)\r
-//             Error ("ClipWinding: MAX_POINTS_ON_WINDING");\r
-\r
-       FreeWinding(in);\r
-       *inout = f;\r
-}\r
-\r
-void UseFaceBounds()\r
-{\r
-       LPVOID       vp;\r
-       float        Dot, BestDot;\r
-       float            planepts[3][3];\r
-       int          BestFace;\r
-       int          i, j;\r
-       int          NumFaces;\r
-       vec3         SurfNormal;\r
-       vec3         vmin,vmax;\r
-       _QERFaceData *QERFaceData;\r
-       PLANE        plane[MAX_FACES*2];\r
-       PLANE        pface;\r
-       MY_WINDING   *w;\r
-\r
-       switch(Plane)\r
-       {\r
-       case PLANE_XY1:\r
-               SurfNormal[0] = 0.0;\r
-               SurfNormal[1] = 0.0;\r
-               SurfNormal[2] =-1.0;\r
-               break;\r
-       case PLANE_XZ0:\r
-               SurfNormal[0] = 0.0;\r
-               SurfNormal[1] = 1.0;\r
-               SurfNormal[2] = 0.0;\r
-               break;\r
-       case PLANE_XZ1:\r
-               SurfNormal[0] = 0.0;\r
-               SurfNormal[1] =-1.0;\r
-               SurfNormal[2] = 0.0;\r
-               break;\r
-       case PLANE_YZ0:\r
-               SurfNormal[0] = 1.0;\r
-               SurfNormal[1] = 0.0;\r
-               SurfNormal[2] = 0.0;\r
-               break;\r
-       case PLANE_YZ1:\r
-               SurfNormal[0] =-1.0;\r
-               SurfNormal[1] = 0.0;\r
-               SurfNormal[2] = 0.0;\r
-               break;\r
-       default:\r
-               SurfNormal[0] = 0.0;\r
-               SurfNormal[1] = 0.0;\r
-               SurfNormal[2] = 1.0;\r
-       }\r
-\r
-       i  = g_FuncTable.m_pfnAllocateSelectedBrushHandles();\r
-       vp = g_FuncTable.m_pfnGetSelectedBrushHandle(0);\r
-       NumFaces = g_FuncTable.m_pfnGetFaceCount(vp);\r
-\r
-       BestFace = -1;\r
-       BestDot  = 0.0;\r
-\r
-       for(i=0; i<NumFaces; i++)\r
-       {\r
-               QERFaceData = g_FuncTable.m_pfnGetFaceData(vp,i);\r
-               planepts[0][0] = QERFaceData->m_v1[0];\r
-               planepts[0][1] = QERFaceData->m_v1[1];\r
-               planepts[0][2] = QERFaceData->m_v1[2];\r
-               planepts[1][0] = QERFaceData->m_v2[0];\r
-               planepts[1][1] = QERFaceData->m_v2[1];\r
-               planepts[1][2] = QERFaceData->m_v2[2];\r
-               planepts[2][0] = QERFaceData->m_v3[0];\r
-               planepts[2][1] = QERFaceData->m_v3[1];\r
-               planepts[2][2] = QERFaceData->m_v3[2];\r
-\r
-               PlaneFromPoints (planepts[0], planepts[1], planepts[2], &plane[2*i]);\r
-               VectorSubtract (vec3_origin, plane[2*i].normal, plane[2*i+1].normal);\r
-               plane[2*i+1].dist = -plane[2*i].dist;\r
-\r
-               Dot = DotProduct(plane[2*i].normal,SurfNormal);\r
-               if(Dot > BestDot)\r
-               {\r
-                       BestDot  = Dot;\r
-                       BestFace = i;\r
-                       if(strlen(QERFaceData->m_TextureName))\r
-                               strcpy(Texture[Game][0],QERFaceData->m_TextureName);\r
-               }\r
-       }\r
-       for(i=0; i<NumFaces; i++)\r
-       {\r
-               if(i==BestFace) continue;\r
-               QERFaceData = g_FuncTable.m_pfnGetFaceData(vp,i);\r
-               if(strlen(QERFaceData->m_TextureName))\r
-               {\r
-                       if(strcmp(Texture[Game][0],QERFaceData->m_TextureName))\r
-                               strcpy(Texture[Game][1],QERFaceData->m_TextureName);\r
-               }\r
-       }\r
-\r
-\r
-       g_FuncTable.m_pfnReleaseSelectedBrushHandles();\r
-\r
-       w = BaseWindingForPlane (plane[BestFace*2].normal, plane[BestFace*2].dist);\r
-\r
-       for (i=0 ; i<NumFaces && w; i++)\r
-       {\r
-               if (BestFace == i)\r
-                       continue;\r
-               ChopWindingInPlace (&w, plane[i*2+1].normal, plane[i*2+1].dist, 0);\r
-       }\r
-       if(!w) return;\r
-\r
-       // Get bounding box for this face\r
-       vmin[0] = vmax[0] = w->p[0][0];\r
-       vmin[1] = vmax[1] = w->p[0][1];\r
-       vmin[2] = vmax[2] = w->p[0][2];\r
-       for(j=1; j<w->numpoints; j++)\r
-       {\r
-               vmin[0] = min(vmin[0],w->p[j][0]);\r
-               vmin[1] = min(vmin[1],w->p[j][1]);\r
-               vmin[2] = min(vmin[2],w->p[j][2]);\r
-               vmax[0] = max(vmax[0],w->p[j][0]);\r
-               vmax[1] = max(vmax[1],w->p[j][1]);\r
-               vmax[2] = max(vmax[2],w->p[j][2]);\r
-       }\r
-\r
-       FreeWinding(w);\r
-\r
-       VectorCopy(plane[BestFace*2].normal,pface.normal);\r
-       pface.dist = plane[BestFace*2].dist;\r
-       switch(Plane)\r
-       {\r
-       case PLANE_XZ0:\r
-       case PLANE_XZ1:\r
-               if(pface.normal[1] == 0.) return;\r
-               Hll = vmin[0];\r
-               Hur = vmax[0];\r
-               Vll = vmin[2];\r
-               Vur = vmax[2];\r
-               Z00 = (pface.dist - pface.normal[0]*Hll - pface.normal[2]*Vll)/pface.normal[1];\r
-               Z01 = (pface.dist - pface.normal[0]*Hll - pface.normal[2]*Vur)/pface.normal[1];\r
-               Z10 = (pface.dist - pface.normal[0]*Hur - pface.normal[2]*Vll)/pface.normal[1];\r
-               Z11 = (pface.dist - pface.normal[0]*Hur - pface.normal[2]*Vur)/pface.normal[1];\r
-               break;\r
-       case PLANE_YZ0:\r
-       case PLANE_YZ1:\r
-               if(pface.normal[0] == 0.) return;\r
-               Hll = vmin[1];\r
-               Hur = vmax[1];\r
-               Vll = vmin[2];\r
-               Vur = vmax[2];\r
-               Z00 = (pface.dist - pface.normal[1]*Hll - pface.normal[2]*Vll)/pface.normal[0];\r
-               Z01 = (pface.dist - pface.normal[1]*Hll - pface.normal[2]*Vur)/pface.normal[0];\r
-               Z10 = (pface.dist - pface.normal[1]*Hur - pface.normal[2]*Vll)/pface.normal[0];\r
-               Z11 = (pface.dist - pface.normal[1]*Hur - pface.normal[2]*Vur)/pface.normal[0];\r
-               break;\r
-       default:\r
-               if(pface.normal[2] == 0.) return;\r
-               Hll = vmin[0];\r
-               Hur = vmax[0];\r
-               Vll = vmin[1];\r
-               Vur = vmax[1];\r
-               Z00 = (pface.dist - pface.normal[0]*Hll - pface.normal[1]*Vll)/pface.normal[2];\r
-               Z01 = (pface.dist - pface.normal[0]*Hll - pface.normal[1]*Vur)/pface.normal[2];\r
-               Z10 = (pface.dist - pface.normal[0]*Hur - pface.normal[1]*Vll)/pface.normal[2];\r
-               Z11 = (pface.dist - pface.normal[0]*Hur - pface.normal[1]*Vur)/pface.normal[2];\r
-       }\r
-}\r
+/*
+GenSurf plugin for GtkRadiant
+Copyright (C) 2001 David Hyde, Loki software and qeradiant.com
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Lesser General Public
+License as published by the Free Software Foundation; either
+version 2.1 of the License, or (at your option) any later version.
+
+This library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public
+License along with this library; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+*/
+
+#include <stdlib.h>
+#include <math.h>
+#include "gensurf.h"
+
+#define MAX_FACES 128    // Maximum number of faces on a brush
+#define        MAX_POINTS_ON_WINDING   64
+#define        SIDE_FRONT              0
+#define        SIDE_ON                 2
+#define        SIDE_BACK               1
+#define        SIDE_CROSS              -2
+
+vec3 vec3_origin = {0,0,0};
+
+void PlaneFromPoints (float *p0, float *p1, float *p2, PLANE *plane)
+{
+       vec3 t1, t2;
+       vec     length;
+       
+       VectorSubtract (p0, p1, t1);
+       VectorSubtract (p2, p1, t2);
+       plane->normal[0] = t1[1]*t2[2] - t1[2]*t2[1];
+       plane->normal[1] = t1[2]*t2[0] - t1[0]*t2[2];
+       plane->normal[2] = t1[0]*t2[1] - t1[1]*t2[0];
+       
+       length = (vec)(sqrt(plane->normal[0]*plane->normal[0] +
+                               plane->normal[1]*plane->normal[1] +
+                                               plane->normal[2]*plane->normal[2]  ));
+       if (length == 0)
+       {
+               VectorClear(plane->normal);
+       }
+       else
+       {
+               plane->normal[0] /= length;
+               plane->normal[1] /= length;
+               plane->normal[2] /= length;
+       }
+       plane->dist = DotProduct (p0, plane->normal);
+}
+
+void VectorMA (vec3 va, vec scale, vec3 vb, vec3 vc)
+{
+       vc[0] = va[0] + scale*vb[0];
+       vc[1] = va[1] + scale*vb[1];
+       vc[2] = va[2] + scale*vb[2];
+}
+
+void CrossProduct (vec3 v1, vec3 v2, vec3 cross)
+{
+       cross[0] = v1[1]*v2[2] - v1[2]*v2[1];
+       cross[1] = v1[2]*v2[0] - v1[0]*v2[2];
+       cross[2] = v1[0]*v2[1] - v1[1]*v2[0];
+}
+
+/*
+=============
+AllocWinding
+=============
+*/
+MY_WINDING     *AllocWinding (int points)
+{
+       MY_WINDING      *w;
+       int                     s;
+
+       s = sizeof(vec)*3*points + sizeof(int);
+       w = (MY_WINDING*)malloc (s);
+       memset (w, 0, s); 
+       return w;
+}
+
+vec VectorNormalize (vec3 in, vec3 out)
+{
+       vec     length, ilength;
+
+       length = (vec)(sqrt (in[0]*in[0] + in[1]*in[1] + in[2]*in[2]));
+       if (length == 0)
+       {
+               VectorClear (out);
+               return 0;
+       }
+
+       ilength = (vec)1.0/length;
+       out[0] = in[0]*ilength;
+       out[1] = in[1]*ilength;
+       out[2] = in[2]*ilength;
+
+       return length;
+}
+
+/*
+=================
+BaseWindingForPlane
+=================
+*/
+MY_WINDING *BaseWindingForPlane (vec3 normal, vec dist)
+{
+       int                i, x;
+       vec        max, v;
+       vec3       org, vright, vup;
+       MY_WINDING *w;
+
+// find the major axis
+
+       max = -BOGUS_RANGE;
+       x = -1;
+       for (i=0 ; i<3; i++)
+       {
+               v = (vec)(fabs(normal[i]));
+               if (v > max)
+               {
+                       x = i;
+                       max = v;
+               }
+       }
+       if (x==-1) x = 2;
+               
+       VectorCopy(vec3_origin,vup);
+       switch (x)
+       {
+       case 0:
+       case 1:
+               vup[2] = 1;
+               break;          
+       case 2:
+               vup[0] = 1;
+               break;          
+       }
+
+       v = DotProduct (vup, normal);
+       VectorMA (vup, -v, normal, vup);
+       VectorNormalize (vup, vup);
+               
+       VectorScale (normal, dist, org);
+       
+       CrossProduct (vup, normal, vright);
+       
+       VectorScale (vup, 65536, vup);
+       VectorScale (vright, 65536, vright);
+
+// project a really big        axis aligned box onto the plane
+       w = AllocWinding (4);
+       
+       VectorSubtract (org, vright, w->p[0]);
+       VectorAdd (w->p[0], vup, w->p[0]);
+       
+       VectorAdd (org, vright, w->p[1]);
+       VectorAdd (w->p[1], vup, w->p[1]);
+       
+       VectorAdd (org, vright, w->p[2]);
+       VectorSubtract (w->p[2], vup, w->p[2]);
+       
+       VectorSubtract (org, vright, w->p[3]);
+       VectorSubtract (w->p[3], vup, w->p[3]);
+       
+       w->numpoints = 4;
+       
+       return w;       
+}
+
+void FreeWinding (MY_WINDING *w)
+{
+       if (*(unsigned *)w == 0xdeaddead)
+//             Error ("FreeWinding: freed a freed winding");
+               return;
+       *(unsigned *)w = 0xdeaddead;
+
+       free (w);
+}
+
+/*
+=============
+ChopWindingInPlace
+=============
+*/
+void ChopWindingInPlace (MY_WINDING **inout, vec3 normal, vec dist, vec epsilon)
+{
+       MY_WINDING *in;
+       vec        dists[MAX_POINTS_ON_WINDING+4];
+       int        sides[MAX_POINTS_ON_WINDING+4];
+       int        counts[3];
+       static vec dot;         // VC 4.2 optimizer bug if not static
+       int        i, j;
+       vec        *p1, *p2;
+       vec3       mid;
+       MY_WINDING *f;
+       int        maxpts;
+
+       in = *inout;
+       counts[0] = counts[1] = counts[2] = 0;
+
+// determine sides for each point
+       for (i=0 ; i<in->numpoints ; i++)
+       {
+               dot = DotProduct (in->p[i], normal);
+               dot -= dist;
+               dists[i] = dot;
+               if (dot > epsilon)
+                       sides[i] = SIDE_FRONT;
+               else if (dot < -epsilon)
+                       sides[i] = SIDE_BACK;
+               else
+               {
+                       sides[i] = SIDE_ON;
+               }
+               counts[sides[i]]++;
+       }
+       sides[i] = sides[0];
+       dists[i] = dists[0];
+       
+       if (!counts[0])
+       {
+               FreeWinding(in);
+               *inout = NULL;
+               return;
+       }
+       if (!counts[1])
+               return;         // inout stays the same
+
+       maxpts = in->numpoints+4;       // cant use counts[0]+2 because
+                                                               // of fp grouping errors
+
+       f = AllocWinding (maxpts);
+               
+       for (i=0 ; i<in->numpoints ; i++)
+       {
+               p1 = in->p[i];
+               
+               if (sides[i] == SIDE_ON)
+               {
+                       VectorCopy (p1, f->p[f->numpoints]);
+                       f->numpoints++;
+                       continue;
+               }
+       
+               if (sides[i] == SIDE_FRONT)
+               {
+                       VectorCopy (p1, f->p[f->numpoints]);
+                       f->numpoints++;
+               }
+
+               if (sides[i+1] == SIDE_ON || sides[i+1] == sides[i])
+                       continue;
+                       
+       // generate a split point
+               p2 = in->p[(i+1)%in->numpoints];
+               
+               dot = dists[i] / (dists[i]-dists[i+1]);
+               for (j=0 ; j<3 ; j++)
+               {       // avoid round off error when possible
+                       if (normal[j] == 1)
+                               mid[j] = dist;
+                       else if (normal[j] == -1)
+                               mid[j] = -dist;
+                       else
+                               mid[j] = p1[j] + dot*(p2[j]-p1[j]);
+               }
+                       
+               VectorCopy (mid, f->p[f->numpoints]);
+               f->numpoints++;
+       }
+       
+//     if (f->numpoints > maxpts)
+//             Error ("ClipWinding: points exceeded estimate");
+//     if (f->numpoints > MAX_POINTS_ON_WINDING)
+//             Error ("ClipWinding: MAX_POINTS_ON_WINDING");
+
+       FreeWinding(in);
+       *inout = f;
+}
+
+void UseFaceBounds()
+{
+       LPVOID       vp;
+       float        Dot, BestDot;
+       float            planepts[3][3];
+       int          BestFace;
+       int          i, j;
+       int          NumFaces;
+       vec3         SurfNormal;
+       vec3         vmin,vmax;
+       _QERFaceData *QERFaceData;
+       PLANE        plane[MAX_FACES*2];
+       PLANE        pface;
+       MY_WINDING   *w;
+
+       switch(Plane)
+       {
+       case PLANE_XY1:
+               SurfNormal[0] = 0.0;
+               SurfNormal[1] = 0.0;
+               SurfNormal[2] =-1.0;
+               break;
+       case PLANE_XZ0:
+               SurfNormal[0] = 0.0;
+               SurfNormal[1] = 1.0;
+               SurfNormal[2] = 0.0;
+               break;
+       case PLANE_XZ1:
+               SurfNormal[0] = 0.0;
+               SurfNormal[1] =-1.0;
+               SurfNormal[2] = 0.0;
+               break;
+       case PLANE_YZ0:
+               SurfNormal[0] = 1.0;
+               SurfNormal[1] = 0.0;
+               SurfNormal[2] = 0.0;
+               break;
+       case PLANE_YZ1:
+               SurfNormal[0] =-1.0;
+               SurfNormal[1] = 0.0;
+               SurfNormal[2] = 0.0;
+               break;
+       default:
+               SurfNormal[0] = 0.0;
+               SurfNormal[1] = 0.0;
+               SurfNormal[2] = 1.0;
+       }
+
+       i  = g_FuncTable.m_pfnAllocateSelectedBrushHandles();
+       vp = g_FuncTable.m_pfnGetSelectedBrushHandle(0);
+       NumFaces = g_FuncTable.m_pfnGetFaceCount(vp);
+
+       BestFace = -1;
+       BestDot  = 0.0;
+
+       for(i=0; i<NumFaces; i++)
+       {
+               QERFaceData = g_FuncTable.m_pfnGetFaceData(vp,i);
+               planepts[0][0] = QERFaceData->m_v1[0];
+               planepts[0][1] = QERFaceData->m_v1[1];
+               planepts[0][2] = QERFaceData->m_v1[2];
+               planepts[1][0] = QERFaceData->m_v2[0];
+               planepts[1][1] = QERFaceData->m_v2[1];
+               planepts[1][2] = QERFaceData->m_v2[2];
+               planepts[2][0] = QERFaceData->m_v3[0];
+               planepts[2][1] = QERFaceData->m_v3[1];
+               planepts[2][2] = QERFaceData->m_v3[2];
+
+               PlaneFromPoints (planepts[0], planepts[1], planepts[2], &plane[2*i]);
+               VectorSubtract (vec3_origin, plane[2*i].normal, plane[2*i+1].normal);
+               plane[2*i+1].dist = -plane[2*i].dist;
+
+               Dot = DotProduct(plane[2*i].normal,SurfNormal);
+               if(Dot > BestDot)
+               {
+                       BestDot  = Dot;
+                       BestFace = i;
+                       if(strlen(QERFaceData->m_TextureName))
+                               strcpy(Texture[Game][0],QERFaceData->m_TextureName);
+               }
+       }
+       for(i=0; i<NumFaces; i++)
+       {
+               if(i==BestFace) continue;
+               QERFaceData = g_FuncTable.m_pfnGetFaceData(vp,i);
+               if(strlen(QERFaceData->m_TextureName))
+               {
+                       if(strcmp(Texture[Game][0],QERFaceData->m_TextureName))
+                               strcpy(Texture[Game][1],QERFaceData->m_TextureName);
+               }
+       }
+
+
+       g_FuncTable.m_pfnReleaseSelectedBrushHandles();
+
+       w = BaseWindingForPlane (plane[BestFace*2].normal, plane[BestFace*2].dist);
+
+       for (i=0 ; i<NumFaces && w; i++)
+       {
+               if (BestFace == i)
+                       continue;
+               ChopWindingInPlace (&w, plane[i*2+1].normal, plane[i*2+1].dist, 0);
+       }
+       if(!w) return;
+
+       // Get bounding box for this face
+       vmin[0] = vmax[0] = w->p[0][0];
+       vmin[1] = vmax[1] = w->p[0][1];
+       vmin[2] = vmax[2] = w->p[0][2];
+       for(j=1; j<w->numpoints; j++)
+       {
+               vmin[0] = min(vmin[0],w->p[j][0]);
+               vmin[1] = min(vmin[1],w->p[j][1]);
+               vmin[2] = min(vmin[2],w->p[j][2]);
+               vmax[0] = max(vmax[0],w->p[j][0]);
+               vmax[1] = max(vmax[1],w->p[j][1]);
+               vmax[2] = max(vmax[2],w->p[j][2]);
+       }
+
+       FreeWinding(w);
+
+       VectorCopy(plane[BestFace*2].normal,pface.normal);
+       pface.dist = plane[BestFace*2].dist;
+       switch(Plane)
+       {
+       case PLANE_XZ0:
+       case PLANE_XZ1:
+               if(pface.normal[1] == 0.) return;
+               Hll = vmin[0];
+               Hur = vmax[0];
+               Vll = vmin[2];
+               Vur = vmax[2];
+               Z00 = (pface.dist - pface.normal[0]*Hll - pface.normal[2]*Vll)/pface.normal[1];
+               Z01 = (pface.dist - pface.normal[0]*Hll - pface.normal[2]*Vur)/pface.normal[1];
+               Z10 = (pface.dist - pface.normal[0]*Hur - pface.normal[2]*Vll)/pface.normal[1];
+               Z11 = (pface.dist - pface.normal[0]*Hur - pface.normal[2]*Vur)/pface.normal[1];
+               break;
+       case PLANE_YZ0:
+       case PLANE_YZ1:
+               if(pface.normal[0] == 0.) return;
+               Hll = vmin[1];
+               Hur = vmax[1];
+               Vll = vmin[2];
+               Vur = vmax[2];
+               Z00 = (pface.dist - pface.normal[1]*Hll - pface.normal[2]*Vll)/pface.normal[0];
+               Z01 = (pface.dist - pface.normal[1]*Hll - pface.normal[2]*Vur)/pface.normal[0];
+               Z10 = (pface.dist - pface.normal[1]*Hur - pface.normal[2]*Vll)/pface.normal[0];
+               Z11 = (pface.dist - pface.normal[1]*Hur - pface.normal[2]*Vur)/pface.normal[0];
+               break;
+       default:
+               if(pface.normal[2] == 0.) return;
+               Hll = vmin[0];
+               Hur = vmax[0];
+               Vll = vmin[1];
+               Vur = vmax[1];
+               Z00 = (pface.dist - pface.normal[0]*Hll - pface.normal[1]*Vll)/pface.normal[2];
+               Z01 = (pface.dist - pface.normal[0]*Hll - pface.normal[1]*Vur)/pface.normal[2];
+               Z10 = (pface.dist - pface.normal[0]*Hur - pface.normal[1]*Vll)/pface.normal[2];
+               Z11 = (pface.dist - pface.normal[0]*Hur - pface.normal[1]*Vur)/pface.normal[2];
+       }
+}