MCPcopy Create free account
hub / github.com/TheRealMJP/BakingLab / BuildBVH

Function BuildBVH

BakingLab/MeshBaker.cpp:699–801  ·  view source on GitHub ↗

Builds a BVH tree for an entire model/scene

Source from the content-addressed store, hash-verified

697
698// Builds a BVH tree for an entire model/scene
699static void BuildBVH(const Model& model, BVHData& bvhData, ID3D11Device* d3dDevice, RTCDevice device)
700{
701 if(bvhData.Scene != nullptr)
702 {
703 rtcDeleteScene(bvhData.Scene);
704 bvhData.Scene = nullptr;
705 }
706 bvhData.Scene = rtcDeviceNewScene(device, RTC_SCENE_DYNAMIC, RTC_INTERSECT1);
707 bvhData.Device = device;
708
709 // Count the total number of vertices and triangles
710 uint32 totalNumVertices = 0;
711 uint32 totalNumTriangles = 0;
712 for(uint64 i = 0; i < model.Meshes().size(); ++i)
713 {
714 const Mesh& mesh = model.Meshes()[i];
715 Assert_(mesh.VertexStride() == sizeof(Vertex));
716 totalNumVertices += mesh.NumVertices();
717 totalNumTriangles += mesh.NumIndices() / 3;
718 }
719
720 bvhData.Triangles.resize(totalNumTriangles);
721 bvhData.Vertices.resize(totalNumVertices);
722 bvhData.MaterialIndices.resize(totalNumTriangles);
723 std::vector<Float4> vertices(totalNumVertices);
724
725 uint32 vtxOffset = 0;
726 uint32 triOffset = 0;
727
728 // Add the data for each mesh
729 for(uint64 meshIdx = 0; meshIdx < model.Meshes().size(); ++meshIdx)
730 {
731 const Mesh& mesh = model.Meshes()[meshIdx];
732 const Vertex* vertexData = reinterpret_cast<const Vertex*>(mesh.Vertices());
733 const uint8* indexData = mesh.Indices();
734 const uint32 numVertices = mesh.NumVertices();
735 const uint32 numIndices = mesh.NumIndices();
736 const uint32 indexSize = mesh.IndexSize();
737 const uint32 vertexStride = mesh.VertexStride();
738
739 // Prepare the triangles
740 const uint32 numTriangles = numIndices / 3;
741 for(uint64 partIdx = 0; partIdx < mesh.MeshParts().size(); ++partIdx)
742 {
743 const MeshPart& meshPart = mesh.MeshParts()[partIdx];
744 const uint32 startTriangle = meshPart.IndexStart / 3;
745 const uint32 endTriangle = (meshPart.IndexStart + meshPart.IndexCount) / 3;
746 for(uint32 i = startTriangle; i < endTriangle; ++i)
747 {
748 const uint32 idx0 = GetIndex(indexData, i * 3 + 0, indexSize) + vtxOffset;
749 const uint32 idx1 = GetIndex(indexData, i * 3 + 1, indexSize) + vtxOffset;
750 const uint32 idx2 = GetIndex(indexData, i * 3 + 2, indexSize) + vtxOffset;
751
752 bvhData.Triangles[i + triOffset] = Uint3(idx0, idx1, idx2);
753 bvhData.MaterialIndices[i + triOffset] = meshPart.MaterialIdx;
754 }
755 }
756

Callers 2

InitializeMethod · 0.85
UpdateMethod · 0.85

Calls 14

GetIndexFunction · 0.85
Uint3Class · 0.85
ExceptionClass · 0.85
GetTextureDataFunction · 0.85
VertexStrideMethod · 0.80
NumVerticesMethod · 0.80
NumIndicesMethod · 0.80
VerticesMethod · 0.80
IndicesMethod · 0.80
IndexSizeMethod · 0.80
Float4Class · 0.50
sizeMethod · 0.45

Tested by

no test coverage detected