| 546 | } |
| 547 | |
| 548 | void Scene::createMeshUVTiles(const std::vector<MeshDesc>& meshDescs) |
| 549 | { |
| 550 | mMeshUVTiles.resize(meshDescs.size()); |
| 551 | |
| 552 | auto processMeshTile = [&](size_t meshIndex) |
| 553 | { |
| 554 | const MeshDesc& desc = meshDescs[meshIndex]; |
| 555 | // This tile captures any triangles that span more than one unit square, e.g., for tiled textures |
| 556 | Rectangle largeTriangleTile; |
| 557 | std::map<int2, Rectangle> tiles; |
| 558 | |
| 559 | const uint8_t* meshIndexData8 = nullptr; |
| 560 | if (desc.useVertexIndices()) |
| 561 | meshIndexData8 = reinterpret_cast<const uint8_t*>(&mMeshIndexData[desc.ibOffset]); |
| 562 | |
| 563 | const uint tcount = desc.getTriangleCount(); |
| 564 | for (uint tidx = 0; tidx < tcount; ++tidx) |
| 565 | { |
| 566 | // Compute local vertex indices within the mesh. |
| 567 | uint32_t vidx[3] = {}; |
| 568 | if (desc.useVertexIndices()) |
| 569 | { |
| 570 | FALCOR_ASSERT(meshIndexData8 != nullptr); |
| 571 | if (desc.use16BitIndices()) |
| 572 | { |
| 573 | uint byteOffset = tidx * 3 * sizeof(uint16_t); |
| 574 | vidx[0] = reinterpret_cast<const uint16_t*>(meshIndexData8 + byteOffset)[0]; |
| 575 | vidx[1] = reinterpret_cast<const uint16_t*>(meshIndexData8 + byteOffset)[1]; |
| 576 | vidx[2] = reinterpret_cast<const uint16_t*>(meshIndexData8 + byteOffset)[2]; |
| 577 | } |
| 578 | else |
| 579 | { |
| 580 | uint byteOffset = tidx * 3 * sizeof(uint32_t); |
| 581 | vidx[0] = reinterpret_cast<const uint32_t*>(meshIndexData8 + byteOffset)[0]; |
| 582 | vidx[1] = reinterpret_cast<const uint32_t*>(meshIndexData8 + byteOffset)[1]; |
| 583 | vidx[2] = reinterpret_cast<const uint32_t*>(meshIndexData8 + byteOffset)[2]; |
| 584 | } |
| 585 | } |
| 586 | else |
| 587 | { |
| 588 | uint baseIndex = tidx * 3; |
| 589 | vidx[0] = baseIndex + 0; |
| 590 | vidx[1] = baseIndex + 1; |
| 591 | vidx[2] = baseIndex + 2; |
| 592 | } |
| 593 | FALCOR_ASSERT(vidx[0] < desc.vertexCount); |
| 594 | FALCOR_ASSERT(vidx[1] < desc.vertexCount); |
| 595 | FALCOR_ASSERT(vidx[2] < desc.vertexCount); |
| 596 | |
| 597 | // Load vertices from global vertex buffer. |
| 598 | // Note that the mesh local vbOffset is added to address into the global vertex buffer. |
| 599 | StaticVertexData vertices[3]; |
| 600 | vertices[0] = mMeshStaticData[(size_t)desc.vbOffset + vidx[0]].unpack(); |
| 601 | vertices[1] = mMeshStaticData[(size_t)desc.vbOffset + vidx[1]].unpack(); |
| 602 | vertices[2] = mMeshStaticData[(size_t)desc.vbOffset + vidx[2]].unpack(); |
| 603 | |
| 604 | int2 v0 = int2(std::floor(vertices[0].texCrd[0]), std::floor(vertices[0].texCrd[1])); |
| 605 | int2 v1 = int2(std::floor(vertices[1].texCrd[0]), std::floor(vertices[1].texCrd[1])); |