static void dumpBVH()

in libraries/hvvr/raycaster/render.cpp [127:180]


static void dumpBVH(const std::string& filename, const std::vector<std::unique_ptr<Model>>& models) {
    struct DumpedBVHNode {
        uint32_t leafMask;
        union {
            struct Children {
                uint32_t pad0;
                uint32_t offset[4];
            } children;
            struct Leaves {
                // leaf0 = [triIndices[0], triIndices[1]), leaf1 = [triIndices[1], triIndices[2]), ...
                uint32_t triIndices[5];
            } leaves;
        };
    };

    std::vector<DumpedBVHNode> nodes;
    for (uint32_t modelIndex = 0; modelIndex < uint32_t(models.size()); modelIndex++) {
        const Model& model = *(models[modelIndex]);
        const MeshData& meshData = model.getMesh();
        for (const auto& srcNode : meshData.nodes) {
            DumpedBVHNode node;
            memcpy(&node, &srcNode, sizeof(DumpedBVHNode));
            nodes.push_back(node);
        }
    }

    assert(nodes.size() <= INT_MAX);

#if DUMP_BINARY
    FILE* file = fopen((filename + "b").c_str(), "wb");
    char header[4] = {'B', 'F', 'F', 'b'};
    fwrite(header, sizeof(header), 1, file);

    int nodeCount = int(nodes.size());
    fwrite(&nodeCount, sizeof(nodeCount), 1, file);

    fwrite(nodes.data(), sizeof(DumpedBVHNode) * nodeCount, 1, file);
    fclose(file);
#else
    FILE* file = fopen(filename.c_str(), "w");
    fprintf(file, "BFF\n");
    fprintf(file, "%d\n", (int)nodes.size());
    for (const auto& node : nodes) {
        fprintf(file, "%u %u %u %u %u %u\n",
            node.leafMask,
            node.leaves.triIndices[0],
            node.leaves.triIndices[1],
            node.leaves.triIndices[2],
            node.leaves.triIndices[3],
            node.leaves.triIndices[4]);
    }
    fclose(file);
#endif
}