optimizedPolyList.cpp 14.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------

#include "math/mMath.h"
#include "core/color.h"
#include "console/console.h"
#include "collision/optimizedPolyList.h"
#include "materials/baseMatInstance.h"
#include "materials/materialDefinition.h"

//----------------------------------------------------------------------------

OptimizedPolyList::OptimizedPolyList()
{
   VECTOR_SET_ASSOCIATION(mPoints);
   VECTOR_SET_ASSOCIATION(mNormals);
   VECTOR_SET_ASSOCIATION(mUV0s);
   VECTOR_SET_ASSOCIATION(mUV1s);
   VECTOR_SET_ASSOCIATION(mVertexList);
   VECTOR_SET_ASSOCIATION(mIndexList);
   VECTOR_SET_ASSOCIATION(mPlaneList);
   VECTOR_SET_ASSOCIATION(mPolyList);

   mIndexList.reserve(100);

   mCurrObject       = NULL;
   mBaseMatrix       = MatrixF::Identity;
   mMatrix           = MatrixF::Identity;
   mTransformMatrix  = MatrixF::Identity;
   mScale.set(1.0f, 1.0f, 1.0f);

   mPlaneTransformer.setIdentity();

   mInterestNormalRegistered = false;
}

OptimizedPolyList::~OptimizedPolyList()
{
   mPoints.clear();
   mNormals.clear();
   mUV0s.clear();
   mUV1s.clear();
   mVertexList.clear();
   mIndexList.clear();
   mPlaneList.clear();
   mPolyList.clear();
}


//----------------------------------------------------------------------------
void OptimizedPolyList::clear()
{
   mPoints.clear();
   mNormals.clear();
   mUV0s.clear();
   mUV1s.clear();
   mVertexList.clear();
   mIndexList.clear();
   mPlaneList.clear();
   mPolyList.clear();
}

//----------------------------------------------------------------------------
U32 OptimizedPolyList::insertPoint(const Point3F& point)
{
   S32 retIdx = -1;

   // Apply the transform
   Point3F transPoint = point;
   transPoint *= mScale;
   mMatrix.mulP(transPoint);

   for (U32 i = 0; i < mPoints.size(); i++)
   {
      if (mPoints[i].equal(transPoint))
      {
         retIdx = i;
         break;
      }
   }

   if (retIdx == -1)
   {
      retIdx = mPoints.size();
      mPoints.push_back(transPoint);
   }

   return (U32)retIdx;
}

U32 OptimizedPolyList::insertNormal(const Point3F& normal)
{
   S32 retIdx = -1;

   // Apply the transform
   Point3F transNormal;
   mMatrix.mulV( normal, &transNormal );

   for (U32 i = 0; i < mNormals.size(); i++)
   {
      if (mNormals[i].equal(transNormal))
      {
         retIdx = i;
         break;
      }
   }

   if (retIdx == -1)
   {
      retIdx = mNormals.size();
      mNormals.push_back(transNormal);
   }

   return (U32)retIdx;
}

U32 OptimizedPolyList::insertUV0(const Point2F& uv)
{
   S32 retIdx = -1;

   for (U32 i = 0; i < mUV0s.size(); i++)
   {
      if (mUV0s[i].equal(uv))
      {
         retIdx = i;
         break;
      }
   }

   if (retIdx == -1)
   {
      retIdx = mUV0s.size();
      mUV0s.push_back(uv);
   }

   return (U32)retIdx;
}

U32 OptimizedPolyList::insertUV1(const Point2F& uv)
{
   S32 retIdx = -1;

   for (U32 i = 0; i < mUV1s.size(); i++)
   {
      if (mUV1s[i].equal(uv))
      {
         retIdx = i;
         break;
      }
   }

   if (retIdx == -1)
   {
      retIdx = mUV1s.size();
      mUV1s.push_back(uv);
   }

   return (U32)retIdx;
}

U32 OptimizedPolyList::insertPlane(const PlaneF& plane)
{
   S32 retIdx = -1;

   // Apply the transform
   PlaneF transPlane;
   mPlaneTransformer.transform(plane, transPlane);

   for (U32 i = 0; i < mPlaneList.size(); i++)
   {
      if (mPlaneList[i].equal(transPlane) &&
          mFabs( mPlaneList[i].d - transPlane.d ) < POINT_EPSILON)
      {
         retIdx = i;
         break;
      }
   }

   if (retIdx == -1)
   {
      retIdx = mPlaneList.size();
      mPlaneList.push_back(transPlane);
   }

   return (U32)retIdx;
}

U32 OptimizedPolyList::insertMaterial(BaseMatInstance* baseMat)
{
   S32 retIdx = -1;

   if ( !baseMat )
      return retIdx;

   Material* mat = dynamic_cast<Material*>(baseMat->getMaterial());

   for (U32 i = 0; i < mMaterialList.size(); i++)
   {
      Material* testMat = dynamic_cast<Material*>(mMaterialList[i]->getMaterial());

      if (mat && testMat)
      {
         if (testMat == mat)
         {
            retIdx = i;
            break;
         }
      }
      else if (mMaterialList[i] == baseMat)
      {
         retIdx = i;
         break;
      }
   }

   if (retIdx == -1)
   {
      retIdx = mMaterialList.size();
      mMaterialList.push_back(baseMat);
   }

   return (U32)retIdx;
}

U32 OptimizedPolyList::insertVertex(const Point3F& point, const Point3F& normal,
                                    const Point2F& uv0, const Point2F& uv1)
{
   VertIndex vert;

   vert.vertIdx   = insertPoint(point);
   vert.normalIdx = insertNormal(normal);
   vert.uv0Idx    = insertUV0(uv0);
   vert.uv1Idx    = insertUV1(uv1);

   return mVertexList.push_back_unique(vert);
}

U32 OptimizedPolyList::addPoint(const Point3F& p)
{
   return insertVertex(p);
}

U32 OptimizedPolyList::addPlane(const PlaneF& plane)
{
   return insertPlane(plane);
}


//----------------------------------------------------------------------------

void OptimizedPolyList::begin(BaseMatInstance* material, U32 surfaceKey)
{
   mPolyList.increment();
   Poly& poly = mPolyList.last();
   poly.material = insertMaterial(material);
   poly.vertexStart = mIndexList.size();
   poly.surfaceKey = surfaceKey;
   poly.type = TriangleFan;
   poly.object = mCurrObject;
}

void OptimizedPolyList::begin(BaseMatInstance* material, U32 surfaceKey, PolyType type)
{
   begin(material, surfaceKey);

   // Set the type
   mPolyList.last().type = type;
}


//----------------------------------------------------------------------------

void OptimizedPolyList::plane(U32 v1, U32 v2, U32 v3)
{
   /*
   AssertFatal(v1 < mPoints.size() && v2 < mPoints.size() && v3 < mPoints.size(),
      "OptimizedPolyList::plane(): Vertex indices are larger than vertex list size");

   mPolyList.last().plane = addPlane(PlaneF(mPoints[v1], mPoints[v2], mPoints[v3]));
   */

   mPolyList.last().plane = addPlane( PlaneF( mPoints[mVertexList[v1].vertIdx], mPoints[mVertexList[v2].vertIdx], mPoints[mVertexList[v3].vertIdx] ) );
}

void OptimizedPolyList::plane(const PlaneF& p)
{
   mPolyList.last().plane = addPlane(p);
}

void OptimizedPolyList::plane(const U32 index)
{
   AssertFatal(index < mPlaneList.size(), "Out of bounds index!");
   mPolyList.last().plane = index;
}

const PlaneF& OptimizedPolyList::getIndexedPlane(const U32 index)
{
   AssertFatal(index < mPlaneList.size(), "Out of bounds index!");
   return mPlaneList[index];
}


//----------------------------------------------------------------------------

void OptimizedPolyList::vertex(U32 vi)
{
   mIndexList.push_back(vi);
}

void OptimizedPolyList::vertex(const Point3F& p)
{
   mIndexList.push_back(addPoint(p));
}

void OptimizedPolyList::vertex(const Point3F& p,
                               const Point3F& normal,
                               const Point2F& uv0,
                               const Point2F& uv1)
{
   mIndexList.push_back(insertVertex(p, normal, uv0, uv1));
}


//----------------------------------------------------------------------------

bool OptimizedPolyList::isEmpty() const
{
   return !mPolyList.size();
}

void OptimizedPolyList::end()
{
   Poly& poly = mPolyList.last();
   poly.vertexCount = mIndexList.size() - poly.vertexStart;
}

//----------------------------------------------------------------------------

Polyhedron OptimizedPolyList::toPolyhedron() const
{
   Polyhedron polyhedron;

   // Add the points, but filter out duplicates.

   Vector< S32 > pointRemap;
   pointRemap.setSize( mPoints.size() );
   pointRemap.fill( -1 );

   const U32 numPoints = mPoints.size();

   for( U32 i = 0; i < numPoints; ++ i )
   {
      bool isDuplicate = false;
      for( U32 npoint = 0; npoint < polyhedron.pointList.size(); ++ npoint )
      {
         if( npoint == i )
            continue;

         if( !polyhedron.pointList[ npoint ].equal( mPoints[ i ] ) )
            continue;

         pointRemap[ i ] = npoint;
         isDuplicate = true;
      }

      if( !isDuplicate )
      {
         pointRemap[ i ] = polyhedron.pointList.size();
         polyhedron.pointList.push_back( mPoints[ i ] );
      }
   }

   // Go through the polys and add all their edges and planes.
   // We will consolidate edges in a second pass.

   const U32 numPolys = mPolyList.size();
   for( U32 i = 0; i < numPolys; ++ i )
   {
      const Poly& poly = mPolyList[ i ];

      // Add the plane.

      const U32 polyIndex = polyhedron.planeList.size();
      polyhedron.planeList.push_back( mPlaneList[ poly.plane ] );

      // Account for polyhedrons expecting planes to
      // face inwards.

      polyhedron.planeList.last().invert();

      // Gather remapped indices according to the
      // current polygon type.

      Vector< U32 > indexList;
      switch( poly.type )
      {
         case TriangleFan:
            AssertFatal( false, "TriangleFan conversion not implemented" );
         case TriangleStrip:
            AssertFatal( false, "TriangleStrip conversion not implemented" );
         case TriangleList:
            {
               Vector< Polyhedron::Edge > tempEdges;

               // Loop over the triangles and gather all unshared edges
               // in tempEdges.  These are the exterior edges of the polygon.

               for( U32 n = poly.vertexStart; n < poly.vertexStart + poly.vertexCount; n += 3 )
               {
                  U32 indices[ 3 ];

                  // Get the remapped indices of the three vertices.

                  indices[ 0 ] = pointRemap[ mVertexList[ mIndexList[ n + 0 ] ].vertIdx ];
                  indices[ 1 ] = pointRemap[ mVertexList[ mIndexList[ n + 1 ] ].vertIdx ];
                  indices[ 2 ] = pointRemap[ mVertexList[ mIndexList[ n + 2 ] ].vertIdx ];

                  // Loop over the three edges.

                  for( U32 d = 0; d < 3; ++ d )
                  {
                     U32 index1 = indices[ d ];
                     U32 index2 = indices[ ( d + 1 ) % 3 ];

                     // See if this edge is already in the list.  If so,
                     // it's a shared edge and thus an interior one.  Remove
                     // it.

                     bool isShared = false;
                     for( U32 nedge = 0; nedge < tempEdges.size(); ++ nedge )
                     {
                        Polyhedron::Edge& edge = tempEdges[ nedge ];
                        if( ( edge.vertex[ 0 ] == index1 && edge.vertex[ 1 ] == index2 ) ||
                            ( edge.vertex[ 0 ] == index2 && edge.vertex[ 1 ] == index1 ) )
                        {
                           tempEdges.erase( nedge );

                           isShared = true;
                           break;
                        }
                     }

                     // If it wasn't in the list, add a new edge.

                     if( !isShared )
                        tempEdges.push_back(
                           Polyhedron::Edge( -1, -1, index1, index2 )
                        );
                  }
               }

               // Walk the edges and gather consecutive indices.

               U32 currentEdge = 0;
               for( U32 n = 0; n < tempEdges.size(); ++ n )
               {
                  // Add first vertex of edge.

                  indexList.push_back( tempEdges[ currentEdge ].vertex[ 0 ] );

                  // Find edge that begins at second vertex.

                  for( U32 nedge = 0; nedge < tempEdges.size(); ++ nedge )
                  {
                     if( nedge == currentEdge )
                        continue;

                     if( tempEdges[ nedge ].vertex[ 0 ] == tempEdges[ currentEdge ].vertex[ 1 ] )
                     {
                        currentEdge = nedge;
                        break;
                     }
                  }
               }
            }
      }

      // Create edges from the indices.  Indices are CCW ordered and
      // we want CW order, so step everything in reverse.

      U32 lastIndex = 0;
      for( S32 n = indexList.size() - 1; n >= 0; -- n )
      {
         polyhedron.edgeList.push_back(
            Polyhedron::Edge(
               polyIndex, 0, // face1 filled later
               indexList[ lastIndex ], indexList[ n ]
            )
         );

         lastIndex = n;
      }
   }

   // Finally, consolidate the edge list by merging all edges that
   // are shared by polygons.

   for( U32 i = 0; i < polyhedron.edgeList.size(); ++ i )
   {
      Polyhedron::Edge& edge = polyhedron.edgeList[ i ];

      // Find the corresponding duplicate edge, if any, and merge
      // it into our current edge.

      for( U32 n = i + 1; n < polyhedron.edgeList.size(); ++ n )
      {
         const Polyhedron::Edge& thisEdge = polyhedron.edgeList[ n ];

         if( ( thisEdge.vertex[ 0 ] == edge.vertex[ 1 ] &&
               thisEdge.vertex[ 1 ] == edge.vertex[ 0 ] ) ||
             ( thisEdge.vertex[ 0 ] == edge.vertex[ 0 ] &&
               thisEdge.vertex[ 1 ] == edge.vertex[ 1 ] ) )
         {
            edge.face[ 1 ] = thisEdge.face[ 0 ];
            polyhedron.edgeList.erase( n );
            break;
         }
      }
   }

   return polyhedron;
}