Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

polygon.cpp

Go to the documentation of this file.
00001 #include <windows.h>
00002 #include "polygon.h"
00003 #include "locmath.h"
00004 #include "collision.h"
00005 #include "general.h"
00006 #include "bsp.h"
00007 #include "mmgr.h"
00008 
00009 POLYGON::POLYGON()
00010 :
00011 numVertices(3)
00012 {
00013     
00014 }
00015 
00016 POLYGON::~POLYGON()
00017 {
00018 }
00019 
00020 VECTOR POLYGON::GetNormal()
00021 {
00022         VECTOR temp; 
00023         float ux;
00024         float uy;
00025         float uz;
00026         float vx;
00027         float vy;
00028         float vz;
00029           ux = Vertex[1].x - Vertex[0].x;
00030           uy = Vertex[1].y - Vertex[0].y;
00031           uz = Vertex[1].z - Vertex[0].z;
00032           vx = Vertex[2].x - Vertex[0].x;
00033           vy = Vertex[2].y - Vertex[0].y;
00034           vz = Vertex[2].z - Vertex[0].z;
00035           temp.x = (uy*vz)-(vy*uz);
00036           temp.y = (uz*vx)-(vz*ux);
00037           temp.z = (ux*vy)-(vx*uy);
00038         return temp;
00039 }
00040 
00041 void POLYGON::SetNormal()
00042 {
00043         float ux;
00044         float uy;
00045         float uz;
00046         float vx;
00047         float vy;
00048         float vz;
00049           ux = Vertex[1].x - Vertex[0].x;
00050           uy = Vertex[1].y - Vertex[0].y;
00051           uz = Vertex[1].z - Vertex[0].z;
00052           vx = Vertex[2].x - Vertex[0].x;
00053           vy = Vertex[2].y - Vertex[0].y;
00054           vz = Vertex[2].z - Vertex[0].z;
00055           Vertex[0].nx = (uy*vz)-(vy*uz);
00056           Vertex[0].ny = (uz*vx)-(vz*ux);
00057           Vertex[0].nz = (ux*vy)-(vx*uy);
00058         Vertex[1].nx = Vertex[0].nx;
00059           Vertex[1].ny = Vertex[0].ny;
00060           Vertex[1].nz = Vertex[0].nz;
00061           Vertex[2].nx = Vertex[0].nx;
00062           Vertex[2].ny = Vertex[0].ny;
00063           Vertex[2].nz = Vertex[0].nz;
00064 }
00065 
00066 /*
00067  Although this function is called SplitPolygon, it really only splits triangles, the
00068  function inputs the triangle to be split and another triangle to be used for the splitting
00069  plane. The parameter 'polygons' is a pointer to 3 triangles for output.
00070  The function splits the input triangle into either 0, 2 or 3 new triangles with
00071  recalculated texture coordinates.
00072  If the polygons pointer happens to be NULL then the function will just set the outputFlag and return.
00073 
00074  The return value will be either Front, Back, TwoFrontOneBack, OneFrontTwoBack or OneFrontOneBack.
00075  Front means that all points are infront of the plane or the polygon lies on the plane and faces the front.
00076  Back means that all points are behind of the plane or the polygon lies on the plane and faces the back.
00077  TwoFrontOneBack means that polygons 1 and 2 are infront of the plane and polygon 3 is behind.
00078  OneFrontTwoBack means that polygon 1 is infront of the plane and polygons 2 and 3 are behind.
00079  OneFrontOneBack means that polygon 1 is infront of the plane and polygon 2 is behind, polygon 3 is not used.
00080 */
00081 /*
00082 int SplitPolygon(POLYGON polygonToSplit, POLYGON planePolygon, POLYGON* polygons)
00083 {
00084     VECTOR planeNormal, polysNormal, pointOnPlane, edge1, edge2, temp;
00085     VERTEX ptA, ptB, outpts[4], inpts[4], intersection;
00086     int count = 0, out_c = 0, in_c = 0, sideA, sideB, outputFlag;
00087     float x1, x2, y1, y2, z1, z2, u1, u2, v1, v2, scale; // texture calculation variables
00088 
00089     // get a point on the plane
00090     pointOnPlane.x = planePolygon.Vertex[0].x;
00091     pointOnPlane.y = planePolygon.Vertex[0].y;
00092     pointOnPlane.z = planePolygon.Vertex[0].z;
00093 
00094     // get the splitting planes normal
00095     edge1.x = planePolygon.Vertex[1].x - planePolygon.Vertex[0].x;
00096     edge1.y = planePolygon.Vertex[1].y - planePolygon.Vertex[0].y;
00097     edge1.z = planePolygon.Vertex[1].z - planePolygon.Vertex[0].z;
00098     edge2.x = planePolygon.Vertex[2].x - planePolygon.Vertex[0].x;
00099     edge2.y = planePolygon.Vertex[2].y - planePolygon.Vertex[0].y;
00100     edge2.z = planePolygon.Vertex[2].z - planePolygon.Vertex[0].z;
00101     planeNormal = CrossVector(edge1, edge2);
00102 
00103     // get the normal of the polygon to split
00104     edge1.x = polygonToSplit.Vertex[1].x - polygonToSplit.Vertex[0].x;
00105     edge1.y = polygonToSplit.Vertex[1].y - polygonToSplit.Vertex[0].y;
00106     edge1.z = polygonToSplit.Vertex[1].z - polygonToSplit.Vertex[0].z;
00107     edge2.x = polygonToSplit.Vertex[2].x - polygonToSplit.Vertex[0].x;
00108     edge2.y = polygonToSplit.Vertex[2].y - polygonToSplit.Vertex[0].y;
00109     edge2.z = polygonToSplit.Vertex[2].z - polygonToSplit.Vertex[0].z;
00110     polysNormal = CrossVector(edge1, edge2);
00111 
00112     // check if the polygon lies on the plane
00113     for (int loop = 0; loop < 3; loop++)
00114     {
00115         temp.x = polygonToSplit.Vertex[loop].x;
00116         temp.y = polygonToSplit.Vertex[loop].y;
00117         temp.z = polygonToSplit.Vertex[loop].z;
00118         if (ClassifyPoint(temp, pointOnPlane, planeNormal) == 0)
00119             count++;
00120         else
00121             break;
00122     }
00123     if (count == 3)
00124     {
00125         if (ClassifyPoint(polysNormal, pointOnPlane, planeNormal) == 1)
00126             return Front;
00127         if (ClassifyPoint(polysNormal, pointOnPlane, planeNormal) == -1)
00128             return Back;
00129     }
00130 
00131     // find if all of the points are infront of or behind the plane
00132     int frontcount = 0, backcount = 0;
00133     for (int loop = 0; loop < 3; loop++)
00134     {
00135         temp.x = polygonToSplit.Vertex[loop].x;
00136         temp.y = polygonToSplit.Vertex[loop].y;
00137         temp.z = polygonToSplit.Vertex[loop].z;
00138         if (ClassifyPoint(temp, pointOnPlane, planeNormal) == 0)
00139         {
00140             frontcount++;
00141             backcount++;
00142         }
00143         else if (ClassifyPoint(temp, pointOnPlane, planeNormal) == 1)
00144             frontcount++;
00145         else if (ClassifyPoint(temp, pointOnPlane, planeNormal) == -1)
00146             backcount++;
00147     }
00148     if (frontcount == 3)
00149             return Front;
00150     if (backcount == 3)
00151             return Back;
00152 
00153     // try to split the polygon
00154     ptA = polygonToSplit.Vertex[2];
00155     temp.x = ptA.x;
00156     temp.y = ptA.y;
00157     temp.z = ptA.z;
00158     sideA = ClassifyPoint(temp, pointOnPlane, planeNormal);
00159     for (int i = -1; ++i < 3;)
00160     {
00161         ptB = polygonToSplit.Vertex[i];
00162         temp.x = ptB.x;
00163         temp.y = ptB.y;
00164         temp.z = ptB.z;
00165         sideB = ClassifyPoint(temp, pointOnPlane, planeNormal);
00166         if (sideB > 0)
00167         {
00168             if (sideA < 0)
00169             {
00170                 // find intersection
00171                 edge1.x = ptA.x;
00172                 edge1.y = ptA.y;
00173                 edge1.z = ptA.z;
00174                 edge2.x = ptB.x;
00175                 edge2.y = ptB.y;
00176                 edge2.z = ptB.z;
00177 
00178                 temp = GetEdgeIntersection(edge1, edge2, planePolygon);
00179                 intersection.x = temp.x;
00180                 intersection.y = temp.y;
00181                 intersection.z = temp.z;
00182 
00183                 // find the new texture coordinates
00184                 x1 = ptB.x - ptA.x;
00185                 y1 = ptB.y - ptA.y;
00186                 z1 = ptB.z - ptA.z;
00187                 x2 = intersection.x - ptA.x;
00188                 y2 = intersection.y - ptA.y;
00189                 z2 = intersection.z - ptA.z;
00190                 u1 = ptA.u;
00191                 u2 = ptB.u;
00192                 v1 = ptA.v;
00193                 v2 = ptB.v;
00194                 scale = sqrt(x2*x2+y2*y2+z2*z2)/sqrt(x1*x1+y1*y1+z1*z1);
00195                 intersection.u = u1 + (u2-u1) * scale;
00196                 intersection.v = v1 + (v2-v1) * scale;
00197 
00198                 outpts[out_c++] = inpts[in_c++] = intersection;
00199             }
00200             inpts[in_c++] = ptB;
00201         }
00202         else if (sideB < 0)
00203         {
00204             if (sideA > 0)
00205             {
00206                 // find intersection
00207                 edge1.x = ptA.x;
00208                 edge1.y = ptA.y;
00209                 edge1.z = ptA.z;
00210                 edge2.x = ptB.x;
00211                 edge2.y = ptB.y;
00212                 edge2.z = ptB.z;
00213 
00214                 temp = GetEdgeIntersection(edge1, edge2, planePolygon);
00215                 intersection.x = temp.x;
00216                 intersection.y = temp.y;
00217                 intersection.z = temp.z;
00218 
00219                 // find the new texture coordinates
00220                 x1 = ptB.x - ptA.x;
00221                 y1 = ptB.y - ptA.y;
00222                 z1 = ptB.z - ptA.z;
00223                 x2 = intersection.x - ptA.x;
00224                 y2 = intersection.y - ptA.y;
00225                 z2 = intersection.z - ptA.z;
00226                 u1 = ptA.u;
00227                 u2 = ptB.u;
00228                 v1 = ptA.v;
00229                 v2 = ptB.v;
00230                 scale = sqrt(x2*x2+y2*y2+z2*z2)/sqrt(x1*x1+y1*y1+z1*z1);
00231                 intersection.u = u1 + (u2-u1) * scale;
00232                 intersection.v = v1 + (v2-v1) * scale;
00233 
00234                 outpts[out_c++] = inpts[in_c++] = intersection;
00235             }
00236             outpts[out_c++] = ptB;
00237         }
00238         else
00239             outpts[out_c++] = inpts[in_c++] = ptB;
00240             ptA = ptB;
00241             sideA = sideB;
00242     }
00243 
00244     if (in_c == 4)          // two polygons are infront, one behind
00245     {
00246         outputFlag = TwoFrontOneBack;
00247         if (polygons)
00248         {
00249             polygons[0].Vertex[0] = inpts[0];
00250             polygons[0].Vertex[1] = inpts[1];
00251             polygons[0].Vertex[2] = inpts[2];
00252             polygons[0].SetNormal();
00253             polygons[1].Vertex[0] = inpts[0];
00254             polygons[1].Vertex[1] = inpts[2];
00255             polygons[1].Vertex[2] = inpts[3];
00256             polygons[1].SetNormal();
00257             polygons[2].Vertex[0] = outpts[0];
00258             polygons[2].Vertex[1] = outpts[1];
00259             polygons[2].Vertex[2] = outpts[2];
00260             polygons[2].SetNormal();
00261         }
00262     }
00263     else if (out_c == 4)    // one polygon is infront, two behind
00264     {
00265         outputFlag = OneFrontTwoBack;
00266         if (polygons)
00267         {
00268             polygons[0].Vertex[0] = inpts[0];
00269             polygons[0].Vertex[1] = inpts[1];
00270             polygons[0].Vertex[2] = inpts[2];
00271             polygons[0].SetNormal();
00272             polygons[1].Vertex[0] = outpts[0];
00273             polygons[1].Vertex[1] = outpts[1];
00274             polygons[1].Vertex[2] = outpts[2];
00275             polygons[1].SetNormal();
00276             polygons[2].Vertex[0] = outpts[0];
00277             polygons[2].Vertex[1] = outpts[2];
00278             polygons[2].Vertex[2] = outpts[3];
00279             polygons[2].SetNormal();
00280         }
00281     }
00282     else if (in_c == 3 && out_c == 3)  // plane bisects the polygon
00283     {
00284         outputFlag = OneFrontOneBack;
00285         if (polygons)
00286         {
00287             polygons[0].Vertex[0] = inpts[0];
00288             polygons[0].Vertex[1] = inpts[1];
00289             polygons[0].Vertex[2] = inpts[2];
00290             polygons[0].SetNormal();
00291             polygons[1].Vertex[0] = outpts[0];
00292             polygons[1].Vertex[1] = outpts[1];
00293             polygons[1].Vertex[2] = outpts[2];
00294             polygons[1].SetNormal();
00295         }
00296     }
00297     else // then polygon must be totally infront of or behind the plane
00298     {
00299         int side;
00300 
00301         for (int loop = 0; loop < 3; loop++)
00302         {
00303             temp.x = polygonToSplit.Vertex[loop].x;
00304             temp.y = polygonToSplit.Vertex[loop].y;
00305             temp.z = polygonToSplit.Vertex[loop].z;
00306             side = ClassifyPoint(temp, pointOnPlane, planeNormal);
00307             if (side == 1)
00308             {
00309                 outputFlag = Front;
00310                 break;
00311             }
00312             else if (side == -1)
00313             {
00314                 outputFlag = Back;
00315                 break;
00316             }
00317         }
00318     }
00319     return outputFlag;
00320 }
00321 */
00322 /*
00323  This function only splits triangles, the function inputs the triangle to be 
00324  split and another triangle to be used for the splitting plane. 
00325  The parameter 'triangles' is a pointer to 3 triangles for output.
00326  The function splits the input triangle into either 0, 2 or 3 new triangles with
00327  recalculated texture coordinates.
00328  If the triangles pointer happens to be NULL then the function will just set the outputFlag and return.
00329 
00330  The return value will be either Front, Back, TwoFrontOneBack, OneFrontTwoBack or OneFrontOneBack.
00331  Front means that all points are infront of the plane or the triangle lies on the plane and faces the front.
00332  Back means that all points are behind of the plane or the triangle lies on the plane and faces the back.
00333  TwoFrontOneBack means that triangles 1 and 2 are infront of the plane and triangle 3 is behind.
00334  OneFrontTwoBack means that triangle 1 is infront of the plane and triangles 2 and 3 are behind.
00335  OneFrontOneBack means that triangle 1 is infront of the plane and triangle 2 is behind, triangle 3 is not used.
00336 */
00337 int SplitPolygon(POLYGON triangleToSplit, POLYGON planeTriangle, POLYGON* triangles)
00338 {
00339     VECTOR planeNormal, polysNormal, pointOnPlane, edge1, edge2, temp;
00340     VERTEX ptA, ptB, outpts[4], inpts[4], intersection;
00341     int count = 0, out_c = 0, in_c = 0, sideA, sideB, outputFlag;
00342     VERTEX texvert1, texvert2; // texture calculation variables
00343     VECTOR t1, t2;
00344     float scale;
00345 
00346     // get a point on the plane
00347     pointOnPlane.x = planeTriangle.Vertex[0].x;
00348     pointOnPlane.y = planeTriangle.Vertex[0].y;
00349     pointOnPlane.z = planeTriangle.Vertex[0].z;
00350 
00351     // get the splitting planes normal
00352     edge1.x = planeTriangle.Vertex[1].x - planeTriangle.Vertex[0].x;
00353     edge1.y = planeTriangle.Vertex[1].y - planeTriangle.Vertex[0].y;
00354     edge1.z = planeTriangle.Vertex[1].z - planeTriangle.Vertex[0].z;
00355     edge2.x = planeTriangle.Vertex[2].x - planeTriangle.Vertex[0].x;
00356     edge2.y = planeTriangle.Vertex[2].y - planeTriangle.Vertex[0].y;
00357     edge2.z = planeTriangle.Vertex[2].z - planeTriangle.Vertex[0].z;
00358     planeNormal = CrossVector(edge1, edge2);
00359 
00360     // get the normal of the triangle to split
00361     edge1.x = triangleToSplit.Vertex[1].x - triangleToSplit.Vertex[0].x;
00362     edge1.y = triangleToSplit.Vertex[1].y - triangleToSplit.Vertex[0].y;
00363     edge1.z = triangleToSplit.Vertex[1].z - triangleToSplit.Vertex[0].z;
00364     edge2.x = triangleToSplit.Vertex[2].x - triangleToSplit.Vertex[0].x;
00365     edge2.y = triangleToSplit.Vertex[2].y - triangleToSplit.Vertex[0].y;
00366     edge2.z = triangleToSplit.Vertex[2].z - triangleToSplit.Vertex[0].z;
00367     polysNormal = CrossVector(edge1, edge2);
00368 
00369     // check if the triangle lies on the plane
00370     for (int loop = 0; loop < 3; loop++)
00371     {
00372         temp.x = triangleToSplit.Vertex[loop].x;
00373         temp.y = triangleToSplit.Vertex[loop].y;
00374         temp.z = triangleToSplit.Vertex[loop].z;
00375         if (ClassifyPoint(temp, pointOnPlane, planeNormal) == 0)
00376             count++;
00377         else
00378             break;
00379     }
00380     if (count == 3)
00381     {
00382         if (ClassifyPoint(polysNormal, pointOnPlane, planeNormal) == 1)
00383             return Front;
00384         if (ClassifyPoint(polysNormal, pointOnPlane, planeNormal) == -1)
00385             return Back;
00386     }
00387 
00388     // find if all of the points are infront of or behind the plane
00389     int frontcount = 0, backcount = 0;
00390     for (int loop = 0; loop < 3; loop++)
00391     {
00392         temp.x = triangleToSplit.Vertex[loop].x;
00393         temp.y = triangleToSplit.Vertex[loop].y;
00394         temp.z = triangleToSplit.Vertex[loop].z;
00395         if (ClassifyPoint(temp, pointOnPlane, planeNormal) == 0)
00396         {
00397             frontcount++;
00398             backcount++;
00399         }
00400         else if (ClassifyPoint(temp, pointOnPlane, planeNormal) == 1)
00401             frontcount++;
00402         else if (ClassifyPoint(temp, pointOnPlane, planeNormal) == -1)
00403             backcount++;
00404     }
00405     if (frontcount == 3)
00406             return Front;
00407     if (backcount == 3)
00408             return Back;
00409 
00410     // try to split the triangle
00411     ptA = triangleToSplit.Vertex[2];
00412     temp.x = ptA.x;
00413     temp.y = ptA.y;
00414     temp.z = ptA.z;
00415     sideA = ClassifyPoint(temp, pointOnPlane, planeNormal);
00416     for (int i = -1; ++i < 3;)
00417     {
00418         ptB = triangleToSplit.Vertex[i];
00419         temp.x = ptB.x;
00420         temp.y = ptB.y;
00421         temp.z = ptB.z;
00422         sideB = ClassifyPoint(temp, pointOnPlane, planeNormal);
00423         if (sideB > 0)
00424         {
00425             if (sideA < 0)
00426             {
00427                 // find intersection
00428                 edge1.x = ptA.x;
00429                 edge1.y = ptA.y;
00430                 edge1.z = ptA.z;
00431                 edge2.x = ptB.x;
00432                 edge2.y = ptB.y;
00433                 edge2.z = ptB.z;
00434 
00435                 temp = GetEdgeIntersection(edge1, edge2, planeTriangle);
00436                 intersection.x = temp.x;
00437                 intersection.y = temp.y;
00438                 intersection.z = temp.z;
00439 
00440                 // find the new texture coordinates
00441                 texvert1.x = ptB.x - ptA.x;
00442                 texvert1.y = ptB.y - ptA.y;
00443                 texvert1.z = ptB.z - ptA.z;
00444                 texvert2.x = intersection.x - ptA.x;
00445                 texvert2.y = intersection.y - ptA.y;
00446                 texvert2.z = intersection.z - ptA.z;
00447                 texvert1.u = ptA.u;
00448                 texvert2.u = ptB.u;
00449                 texvert1.v = ptA.v;
00450                 texvert2.v = ptB.v;
00451                 t1.x = texvert1.x;
00452                 t1.y = texvert1.y;
00453                 t1.z = texvert1.z;
00454                 t2.x = texvert2.x;
00455                 t2.y = texvert2.y;
00456                 t2.z = texvert2.z;
00457                 scale = sqrt(t2.x*t2.x+t2.y*t2.y+t2.z*t2.z)/sqrt(t1.x*t1.x+t1.y*t1.y+t1.z*t1.z);
00458                 intersection.u = texvert1.u + (texvert2.u - texvert1.u) * scale;
00459                 intersection.v = texvert1.v + (texvert2.v - texvert1.v) * scale;
00460 
00461                 outpts[out_c++] = inpts[in_c++] = intersection;
00462             }
00463             inpts[in_c++] = ptB;
00464         }
00465         else if (sideB < 0)
00466         {
00467             if (sideA > 0)
00468             {
00469                 // find intersection
00470                 edge1.x = ptA.x;
00471                 edge1.y = ptA.y;
00472                 edge1.z = ptA.z;
00473                 edge2.x = ptB.x;
00474                 edge2.y = ptB.y;
00475                 edge2.z = ptB.z;
00476 
00477                 temp = GetEdgeIntersection(edge1, edge2, planeTriangle);
00478                 intersection.x = temp.x;
00479                 intersection.y = temp.y;
00480                 intersection.z = temp.z;
00481 
00482                 // find the new texture coordinates
00483                 texvert1.x = ptB.x - ptA.x;
00484                 texvert1.y = ptB.y - ptA.y;
00485                 texvert1.z = ptB.z - ptA.z;
00486                 texvert2.x = intersection.x - ptA.x;
00487                 texvert2.y = intersection.y - ptA.y;
00488                 texvert2.z = intersection.z - ptA.z;
00489                 texvert1.u = ptA.u;
00490                 texvert2.u = ptB.u;
00491                 texvert1.v = ptA.v;
00492                 texvert2.v = ptB.v;
00493                 t1.x = texvert1.x;
00494                 t1.y = texvert1.y;
00495                 t1.z = texvert1.z;
00496                 t2.x = texvert2.x;
00497                 t2.y = texvert2.y;
00498                 t2.z = texvert2.z;
00499                 scale = sqrt(t2.x*t2.x+t2.y*t2.y+t2.z*t2.z)/sqrt(t1.x*t1.x+t1.y*t1.y+t1.z*t1.z);
00500                 intersection.u = texvert1.u + (texvert2.u - texvert1.u) * scale;
00501                 intersection.v = texvert1.v + (texvert2.v - texvert1.v) * scale;
00502 
00503                 outpts[out_c++] = inpts[in_c++] = intersection;
00504             }
00505             outpts[out_c++] = ptB;
00506         }
00507         else
00508             outpts[out_c++] = inpts[in_c++] = ptB;
00509             ptA = ptB;
00510             sideA = sideB;
00511     }
00512 
00513     if (in_c == 4)          // two triangles are infront, one behind
00514     {
00515         outputFlag = TwoFrontOneBack;
00516         if (triangles)
00517         {
00518             triangles[0].Vertex[0] = inpts[0];
00519             triangles[0].Vertex[1] = inpts[1];
00520             triangles[0].Vertex[2] = inpts[2];
00521             triangles[0].numVertices = 3;
00522             triangles[0].SetNormal();
00523             triangles[1].Vertex[0] = inpts[0];
00524             triangles[1].Vertex[1] = inpts[2];
00525             triangles[1].Vertex[2] = inpts[3];
00526             triangles[1].numVertices = 3;
00527             triangles[1].SetNormal();
00528             triangles[2].Vertex[0] = outpts[0];
00529             triangles[2].Vertex[1] = outpts[1];
00530             triangles[2].Vertex[2] = outpts[2];
00531             triangles[2].numVertices = 3;
00532             triangles[2].SetNormal();
00533         }
00534     }
00535     else if (out_c == 4)    // one triangle is infront, two behind
00536     {
00537         outputFlag = OneFrontTwoBack;
00538         if (triangles)
00539         {
00540             triangles[0].Vertex[0] = inpts[0];
00541             triangles[0].Vertex[1] = inpts[1];
00542             triangles[0].Vertex[2] = inpts[2];
00543             triangles[0].numVertices = 3;
00544             triangles[0].SetNormal();
00545             triangles[1].Vertex[0] = outpts[0];
00546             triangles[1].Vertex[1] = outpts[1];
00547             triangles[1].Vertex[2] = outpts[2];
00548             triangles[1].numVertices = 3;
00549             triangles[1].SetNormal();
00550             triangles[2].Vertex[0] = outpts[0];
00551             triangles[2].Vertex[1] = outpts[2];
00552             triangles[2].Vertex[2] = outpts[3];
00553             triangles[2].numVertices = 3;
00554             triangles[2].SetNormal();
00555         }
00556     }
00557     else if (in_c == 3 && out_c == 3)  // plane bisects the triangle
00558     {
00559         outputFlag = OneFrontOneBack;
00560         if (triangles)
00561         {
00562             triangles[0].Vertex[0] = inpts[0];
00563             triangles[0].Vertex[1] = inpts[1];
00564             triangles[0].Vertex[2] = inpts[2];
00565             triangles[0].numVertices = 3;
00566             triangles[0].SetNormal();
00567             triangles[1].Vertex[0] = outpts[0];
00568             triangles[1].Vertex[1] = outpts[1];
00569             triangles[1].Vertex[2] = outpts[2];
00570             triangles[1].numVertices = 3;
00571             triangles[1].SetNormal();
00572         }
00573     }
00574     else // then triangle must be totally infront of or behind the plane
00575     {
00576         int side;
00577 
00578         for (int loop = 0; loop < 3; loop++)
00579         {
00580             temp.x = triangleToSplit.Vertex[loop].x;
00581             temp.y = triangleToSplit.Vertex[loop].y;
00582             temp.z = triangleToSplit.Vertex[loop].z;
00583             side = ClassifyPoint(temp, pointOnPlane, planeNormal);
00584             if (side == 1)
00585             {
00586                 outputFlag = Front;
00587                 break;
00588             }
00589             else if (side == -1)
00590             {
00591                 outputFlag = Back;
00592                 break;
00593             }
00594         }
00595     }
00596     return outputFlag;
00597 }
00598 /*
00599 int ClassifyPolygon(POLYGON* Polygon, POLYGON planePolygon)
00600 {
00601     int numVerts = Polygon->numVertices;
00602     int count = 0;
00603     VECTOR planeNormal, polysNormal, pointOnPlane, edge1, edge2, temp;
00604 
00605     // get a point on the plane
00606     pointOnPlane = planePolygon.Vertex[0].coords;
00607 
00608     // get the planes normal
00609     edge1 = planePolygon.Vertex[1].coords - planePolygon.Vertex[0].coords;
00610     edge2 = planePolygon.Vertex[2].coords - planePolygon.Vertex[0].coords;
00611     planeNormal = CrossVector(edge1, edge2);
00612 
00613     // get the normal of the Polygon
00614     edge1 = Polygon->Vertex[1].coords - Polygon->Vertex[0].coords;
00615     edge2 = Polygon->Vertex[2].coords - Polygon->Vertex[0].coords;
00616     polysNormal = CrossVector(edge1, edge2);
00617 
00618     // check if the polygon lies on the plane
00619     for (int loop = 0; loop < numVerts; loop++)
00620     {
00621         temp = Polygon->Vertex[loop].coords;
00622         if (ClassifyPoint(temp, pointOnPlane, planeNormal) == 0)
00623             count++;
00624         else
00625             break;
00626     }
00627     if (count == numVerts)
00628             return OnPartition;
00629 
00630     // find if all of the points are infront of or behind the plane
00631     int result, frontcount = 0, backcount = 0;
00632     for (int loop = 0; loop < numVerts; loop++)
00633     {
00634         temp = Polygon->Vertex[loop].coords;
00635         result = ClassifyPoint(temp, pointOnPlane, planeNormal);
00636         if (result == 0)
00637         {
00638             frontcount++;
00639             backcount++;
00640         }
00641         else
00642             if (result == 1)
00643                 frontcount++;
00644         else
00645             if (result == -1)
00646                 backcount++;
00647     }
00648     if (frontcount == numVerts)
00649             return Front;
00650     if (backcount == numVerts)
00651             return Back;
00652 
00653     return PolygonWasSplit;
00654 }
00655 
00656 void InvertPolygon(POLYGON* Polygon)
00657 {
00658     POLYGON temppolygon = *Polygon;
00659     for (int loop = 0; loop < Polygon->numVertices; loop++)
00660         Polygon->Vertex[loop] = temppolygon.Vertex[(Polygon->numVertices - 1) - loop];
00661     Polygon->SetNormal();
00662     return;
00663 }
00664 */

Generated on Fri Dec 23 05:20:18 2005 for Portals by doxygen1.2.15