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

Generated on Fri Dec 23 05:21:38 2005 for Sound by doxygen1.2.15