BVHAccel::BVHAccel(const std::vector<Primitive *> &_primitives, size_t max_leaf_size) { this->primitives = _primitives; // edge case if (primitives.empty()) { return; } // calculate root AABB size BBox bb; for (size_t i = 0; i < primitives.size(); ++i) { bb.expand(primitives[i]->get_bbox()); } root = new BVHNode(bb, 0, primitives.size()); // calculate morton code for each primitives for (size_t i = 0; i < primitives.size(); ++i) { unsigned int morton_code = morton3D(bb.getUnitcubePosOf(primitives[i]->get_bbox().centroid())); primitives[i]->morton_code = morton_code; } // sort primitives using morton code std::sort(primitives.begin(), primitives.end(), mortonCompare); //construct BVH based on the mortan code constructBVH(root); }
BVHAccel::BVHAccel(const std::vector<Primitive *> &_primitives, size_t max_leaf_size) { this->primitives = _primitives; // edge case if (primitives.empty()) { return; } // calculate root AABB size BBox bb; for (size_t i = 0; i < primitives.size(); ++i) { bb.expand(primitives[i]->get_bbox()); } root = new BVHNode(bb, 0, primitives.size()); // calculate morton code for each primitives for (size_t i = 0; i < primitives.size(); ++i) { unsigned int morton_code = morton3D(bb.getUnitcubePosOf(primitives[i]->get_bbox().centroid())); primitives[i]->morton_code = morton_code; } // sort primitives using morton code std::sort(primitives.begin(), primitives.end(), mortonCompare); // extract bboxes array std::vector<BBox> bboxes(primitives.size()); for(int i=0; i<primitives.size(); i++) bboxes[i] = primitives[i]->get_bbox(); // extract sorted morton code for parallel binary radix tree construction unsigned int sorted_morton_codes[primitives.size()]; for (size_t i = 0; i < primitives.size(); ++i) { sorted_morton_codes[i] = primitives[i]->morton_code; } // delegate the binary radix tree construction process to GPU cout << "start building parallel brtree" << endl; ParallelBRTreeBuilder builder(sorted_morton_codes, &bboxes[0], primitives.size()); builder.build(); cout << "done." << endl; leaf_nodes = builder.get_leaf_nodes(); internal_nodes = builder.get_internal_nodes(); builder.freeDeviceMemory(); // construct BVH based on Binary Radix Tree constructBVHFromBRTree(); // free the host memory because I am a good programmer builder.freeHostMemory(); }