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

Generated on Fri Dec 23 05:19:55 2005 for Particles by doxygen1.2.15