[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];
|
---|
| 23 |
|
---|
| 24 | if (node->IsLeaf())
|
---|
| 25 | {
|
---|
| 26 | BvhLeaf *leaf = (BvhLeaf*)node;
|
---|
| 27 |
|
---|
| 28 | buffer[0] = mTriangleRangeMap[leaf].first;
|
---|
| 29 | buffer[1] = mTriangleRangeMap[leaf].second;
|
---|
| 30 | buffer[2] = -1;//leaf->GetAxis();
|
---|
| 31 | buffer[3] = currentIdx ++;
|
---|
| 32 |
|
---|
| 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 | buffer[3] = currentIdx ++;
|
---|
| 43 |
|
---|
| 44 | fwrite(buffer, sizeof(int), 4, fw);
|
---|
| 45 |
|
---|
| 46 | SaveNode(interior->GetFront(), fw);
|
---|
| 47 | SaveNode(interior->GetBack(), fw);
|
---|
| 48 | }
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 |
|
---|
| 52 | void BvhExporter::ConstructTriangleRangeMap(Bvh *bvh,
|
---|
| 53 | BvhNode *node,
|
---|
| 54 | int &first,
|
---|
| 55 | int &last)
|
---|
| 56 | {
|
---|
| 57 | if (!node->IsLeaf())
|
---|
| 58 | {
|
---|
| 59 | int m;
|
---|
| 60 | BvhInterior *interior = static_cast<BvhInterior *>(node);
|
---|
| 61 |
|
---|
| 62 | ConstructTriangleRangeMap(bvh, interior->GetFront(), first, m);
|
---|
| 63 | ConstructTriangleRangeMap(bvh, interior->GetBack(), m, last);
|
---|
| 64 |
|
---|
| 65 | mTriangleRangeMap[node] = pair<int, int>(first, last);
|
---|
| 66 | }
|
---|
| 67 | else
|
---|
| 68 | {
|
---|
| 69 | //first = currentIdx;
|
---|
| 70 | last = first + bvh->CountTriangles(node);
|
---|
| 71 | //currentIdx = last;
|
---|
| 72 |
|
---|
| 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[2] = triangles.size();
|
---|
| 94 | buffer[3] = 0; // construction method
|
---|
| 95 | buffer[4] = 1; // termination leaves
|
---|
| 96 | buffer[5] = bvh->CountNumNodes(bvh->GetStaticRoot());
|
---|
| 97 |
|
---|
| 98 | fwrite(buffer, sizeof(int), 6, fw);
|
---|
| 99 |
|
---|
| 100 | int first, last;
|
---|
| 101 | ConstructTriangleRangeMap(bvh, bvh->GetStaticRoot(), first, last);
|
---|
| 102 |
|
---|
| 103 | BvhNodeContainer nodes;
|
---|
| 104 | bvh->CollectNodes(bvh->GetStaticRoot(), nodes);
|
---|
| 105 |
|
---|
| 106 | vector<int> triangles;
|
---|
| 107 |
|
---|
| 108 | BvhNodeContainer::const_iterator it, it_end = nodes.end();
|
---|
| 109 |
|
---|
| 110 | for (it = nodes.begin(); it != it_end; ++ it)
|
---|
| 111 | {
|
---|
| 112 | BvhNode *node = *it;
|
---|
| 113 |
|
---|
| 114 | int f = mTriangleRangeMap[node].first;
|
---|
| 115 | int b = mTriangleRangeMap[node].second;
|
---|
| 116 |
|
---|
| 117 | for (int i = f; i < b; ++ i)
|
---|
| 118 | {
|
---|
| 119 | triangles.push_back(i);
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
| 122 |
|
---|
[3261] | 123 | cout << "writing " << (int)triangles.size() << " triangle ids ..." << endl;
|
---|
| 124 |
|
---|
[3259] | 125 | // export sorted triangle IDs
|
---|
[3261] | 126 | for (int i = 0; i < (int)triangles.size(); ++i)
|
---|
[3259] | 127 | {
|
---|
| 128 | fwrite(&triangles[i], sizeof(int), 1, fw);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
[3261] | 131 | cout << "writing bvh nodes ..." << endl;
|
---|
| 132 |
|
---|
[3259] | 133 | SaveNode(bvh->GetStaticRoot(), fw);
|
---|
| 134 |
|
---|
| 135 | cout << "successfully exported bvh" << endl;
|
---|
| 136 | fclose(fw);
|
---|
| 137 |
|
---|
| 138 | return true;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 |
|
---|
| 142 | } |
---|