| 426 | } |
| 427 | |
| 428 | void ClippedPolyList::generateNormals() |
| 429 | { |
| 430 | PROFILE_SCOPE( ClippedPolyList_GenerateNormals ); |
| 431 | |
| 432 | AssertFatal(mNormalList.size() == mVertexList.size(), "Normals count does not match vertex count!"); |
| 433 | |
| 434 | U32 i, polyCount; |
| 435 | VectorF normal; |
| 436 | PolyListIterator polyIter; |
| 437 | IndexListIterator indexIter; |
| 438 | |
| 439 | Vector<VectorF>::iterator normalIter = mNormalList.begin(); |
| 440 | U32 n = 0; |
| 441 | for ( ; normalIter != mNormalList.end(); normalIter++, n++ ) |
| 442 | { |
| 443 | // Skip normals that already have values. |
| 444 | if ( !normalIter->isZero() ) |
| 445 | continue; |
| 446 | |
| 447 | // Average all the face normals which |
| 448 | // share this vertex index. |
| 449 | indexIter = mIndexList.begin(); |
| 450 | normal.zero(); |
| 451 | polyCount = 0; |
| 452 | i = 0; |
| 453 | |
| 454 | for ( ; indexIter != mIndexList.end(); indexIter++, i++ ) |
| 455 | { |
| 456 | if ( n != *indexIter ) |
| 457 | continue; |
| 458 | |
| 459 | polyIter = mPolyList.begin(); |
| 460 | for ( ; polyIter != mPolyList.end(); polyIter++ ) |
| 461 | { |
| 462 | const Poly& poly = *polyIter; |
| 463 | if ( i < poly.vertexStart || i > poly.vertexStart + poly.vertexCount ) |
| 464 | continue; |
| 465 | |
| 466 | ++polyCount; |
| 467 | normal += poly.plane; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | // Average it. |
| 472 | if ( polyCount > 0 ) |
| 473 | normal /= (F32)polyCount; |
| 474 | |
| 475 | // Note: we use a temporary for the normal averaging |
| 476 | // then copy the result to limit the number of arrays |
| 477 | // we're touching during the innermost loop. |
| 478 | *normalIter = normal; |
| 479 | } |
| 480 | } |