[3259] | 1 | #include "BvhExporter.h"
|
---|
| 2 |
|
---|
| 3 | using namespace std;
|
---|
| 4 |
|
---|
| 5 | // MAGIC of all bin exports
|
---|
| 6 | #ifndef MAGIC
|
---|
| 7 | #define MAGIC 0x827923
|
---|
| 8 | #endif
|
---|
| 9 |
|
---|
| 10 | #define BVH_VERSION 2.1
|
---|
| 11 |
|
---|
| 12 | namespace CHCDemoEngine
|
---|
| 13 | {
|
---|
| 14 |
|
---|
| 15 | static int currentIdx = 0;
|
---|
| 16 |
|
---|
| 17 |
|
---|
| 18 | void BvhExporter::SaveNode(BvhNode *node, FILE *fw)
|
---|
| 19 | {
|
---|
| 20 | if (!node) return;
|
---|
| 21 |
|
---|
| 22 | int buffer[4];
|
---|
[3279] | 23 | buffer[3] = currentIdx ++;
|
---|
[3259] | 24 |
|
---|
[3274] | 25 | if (node->IsVirtualLeaf())
|
---|
[3259] | 26 | {
|
---|
| 27 | BvhLeaf *leaf = (BvhLeaf*)node;
|
---|
| 28 |
|
---|
| 29 | buffer[0] = mTriangleRangeMap[leaf].first;
|
---|
| 30 | buffer[1] = mTriangleRangeMap[leaf].second;
|
---|
| 31 | buffer[2] = -1;//leaf->GetAxis();
|
---|
[3279] | 32 |
|
---|
[3259] | 33 | fwrite(buffer, sizeof(int), 4, fw);
|
---|
| 34 | }
|
---|
| 35 | else
|
---|
| 36 | {
|
---|
| 37 | BvhInterior *interior = (BvhInterior *)node;
|
---|
| 38 |
|
---|
| 39 | buffer[0] = interior->mFirst;
|
---|
| 40 | buffer[1] = interior->mLast;
|
---|
| 41 | buffer[2] = 0;//interior->mAxis;
|
---|
| 42 |
|
---|
| 43 | fwrite(buffer, sizeof(int), 4, fw);
|
---|
| 44 |
|
---|
| 45 | SaveNode(interior->GetFront(), fw);
|
---|
| 46 | SaveNode(interior->GetBack(), fw);
|
---|
| 47 | }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 |
|
---|
| 51 | void BvhExporter::ConstructTriangleRangeMap(Bvh *bvh,
|
---|
| 52 | BvhNode *node,
|
---|
| 53 | int &first,
|
---|
| 54 | int &last)
|
---|
| 55 | {
|
---|
[3274] | 56 | if (!node->IsVirtualLeaf())
|
---|
[3259] | 57 | {
|
---|
| 58 | int m;
|
---|
| 59 | BvhInterior *interior = static_cast<BvhInterior *>(node);
|
---|
| 60 |
|
---|
[3282] | 61 | ConstructTriangleRangeMap(bvh, interior->GetBack(), first, m);
|
---|
[3272] | 62 | int newFirst = m + 1;
|
---|
[3282] | 63 | ConstructTriangleRangeMap(bvh, interior->GetFront(), newFirst, last);
|
---|
[3259] | 64 |
|
---|
| 65 | mTriangleRangeMap[node] = pair<int, int>(first, last);
|
---|
| 66 | }
|
---|
| 67 | else
|
---|
| 68 | {
|
---|
| 69 | //first = currentIdx;
|
---|
[3272] | 70 | last = first + bvh->CountTriangles(node) - 1;
|
---|
[3282] | 71 | //cout << "x " << node->GetFirstEntity() << " " << node->GetLastEntity() << " " << first << " " << last << endl;
|
---|
[3259] | 72 | //currentIdx = last;
|
---|
| 73 | mTriangleRangeMap[node] = pair<int, int>(first, last);
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 |
|
---|
[3260] | 78 | bool BvhExporter::Export(const string &filename, Bvh *bvh)
|
---|
[3259] | 79 | {
|
---|
[3261] | 80 | cout << "Exporting bvh to file '" << filename.c_str() << "'" << endl;
|
---|
[3259] | 81 |
|
---|
| 82 | FILE *fw = fopen(filename.c_str(), "wb");
|
---|
| 83 |
|
---|
| 84 | if (fw == NULL)
|
---|
| 85 | {
|
---|
| 86 | cerr << "Error: Cannot open file for writing" << endl;
|
---|
| 87 | return false;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | int buffer[6];
|
---|
| 91 | buffer[0] = MAGIC;
|
---|
| 92 | buffer[1] = BVH_VERSION * 1000;
|
---|
| 93 | buffer[3] = 0; // construction method
|
---|
| 94 | buffer[4] = 1; // termination leaves
|
---|
[3274] | 95 | buffer[5] = bvh->CountNumVirtualNodes(bvh->GetStaticRoot());
|
---|
[3259] | 96 |
|
---|
[3272] | 97 | int first = 0, last;
|
---|
[3259] | 98 | ConstructTriangleRangeMap(bvh, bvh->GetStaticRoot(), first, last);
|
---|
| 99 |
|
---|
[3273] | 100 | BvhNodeContainer nodes;
|
---|
| 101 | bvh->CollectVirtualLeaves(bvh->GetStaticRoot(), nodes);
|
---|
[3259] | 102 |
|
---|
| 103 | vector<int> triangles;
|
---|
| 104 |
|
---|
[3273] | 105 | BvhNodeContainer::const_iterator it, it_end = nodes.end();
|
---|
[3259] | 106 |
|
---|
| 107 | for (it = nodes.begin(); it != it_end; ++ it)
|
---|
| 108 | {
|
---|
| 109 | BvhNode *node = *it;
|
---|
| 110 |
|
---|
[3282] | 111 | const int f = mTriangleRangeMap[node].first;
|
---|
| 112 | const int l = mTriangleRangeMap[node].second;
|
---|
[3259] | 113 |
|
---|
[3272] | 114 | for (int i = f; i <= l; ++ i)
|
---|
[3259] | 115 | triangles.push_back(i);
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[3272] | 118 | buffer[2] = (int)triangles.size();
|
---|
| 119 | fwrite(buffer, sizeof(int), 6, fw);
|
---|
| 120 |
|
---|
[3261] | 121 | cout << "writing " << (int)triangles.size() << " triangle ids ..." << endl;
|
---|
| 122 |
|
---|
[3259] | 123 | // export sorted triangle IDs
|
---|
[3272] | 124 | for (size_t i = 0; i < triangles.size(); ++ i)
|
---|
[3259] | 125 | {
|
---|
| 126 | fwrite(&triangles[i], sizeof(int), 1, fw);
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[3261] | 129 | cout << "writing bvh nodes ..." << endl;
|
---|
| 130 |
|
---|
[3259] | 131 | SaveNode(bvh->GetStaticRoot(), fw);
|
---|
| 132 |
|
---|
| 133 | cout << "successfully exported bvh" << endl;
|
---|
| 134 | fclose(fw);
|
---|
| 135 |
|
---|
| 136 | return true;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 |
|
---|
| 140 | } |
---|