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->IsVirtualLeaf())
|
---|
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->IsVirtualLeaf())
|
---|
58 | {
|
---|
59 | int m;
|
---|
60 | BvhInterior *interior = static_cast<BvhInterior *>(node);
|
---|
61 |
|
---|
62 | ConstructTriangleRangeMap(bvh, interior->GetFront(), first, m);
|
---|
63 | int newFirst = m + 1;
|
---|
64 | ConstructTriangleRangeMap(bvh, interior->GetBack(), newFirst, last);
|
---|
65 |
|
---|
66 | mTriangleRangeMap[node] = pair<int, int>(first, last);
|
---|
67 | }
|
---|
68 | else
|
---|
69 | {
|
---|
70 | //first = currentIdx;
|
---|
71 | last = first + bvh->CountTriangles(node) - 1;
|
---|
72 | //currentIdx = last;
|
---|
73 |
|
---|
74 | mTriangleRangeMap[node] = pair<int, int>(first, last);
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 |
|
---|
79 | bool BvhExporter::Export(const string &filename, Bvh *bvh)
|
---|
80 | {
|
---|
81 | cout << "Exporting bvh to file '" << filename.c_str() << "'" << endl;
|
---|
82 |
|
---|
83 | FILE *fw = fopen(filename.c_str(), "wb");
|
---|
84 |
|
---|
85 | if (fw == NULL)
|
---|
86 | {
|
---|
87 | cerr << "Error: Cannot open file for writing" << endl;
|
---|
88 | return false;
|
---|
89 | }
|
---|
90 |
|
---|
91 | int buffer[6];
|
---|
92 | buffer[0] = MAGIC;
|
---|
93 | buffer[1] = BVH_VERSION * 1000;
|
---|
94 | buffer[3] = 0; // construction method
|
---|
95 | buffer[4] = 1; // termination leaves
|
---|
96 | buffer[5] = bvh->CountNumVirtualNodes(bvh->GetStaticRoot());
|
---|
97 |
|
---|
98 | int first = 0, last;
|
---|
99 | ConstructTriangleRangeMap(bvh, bvh->GetStaticRoot(), first, last);
|
---|
100 |
|
---|
101 | BvhNodeContainer nodes;
|
---|
102 | bvh->CollectVirtualLeaves(bvh->GetStaticRoot(), nodes);
|
---|
103 |
|
---|
104 | vector<int> triangles;
|
---|
105 |
|
---|
106 | BvhNodeContainer::const_iterator it, it_end = nodes.end();
|
---|
107 |
|
---|
108 | for (it = nodes.begin(); it != it_end; ++ it)
|
---|
109 | {
|
---|
110 | BvhNode *node = *it;
|
---|
111 |
|
---|
112 | int f = mTriangleRangeMap[node].first;
|
---|
113 | int l = mTriangleRangeMap[node].second;
|
---|
114 |
|
---|
115 | for (int i = f; i <= l; ++ i)
|
---|
116 | {
|
---|
117 | triangles.push_back(i);
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | buffer[2] = (int)triangles.size();
|
---|
122 | fwrite(buffer, sizeof(int), 6, fw);
|
---|
123 |
|
---|
124 | cout << "writing " << (int)triangles.size() << " triangle ids ..." << endl;
|
---|
125 |
|
---|
126 | // export sorted triangle IDs
|
---|
127 | for (size_t i = 0; i < triangles.size(); ++ i)
|
---|
128 | {
|
---|
129 | fwrite(&triangles[i], sizeof(int), 1, fw);
|
---|
130 | }
|
---|
131 |
|
---|
132 | cout << "writing bvh nodes ..." << endl;
|
---|
133 |
|
---|
134 | SaveNode(bvh->GetStaticRoot(), fw);
|
---|
135 |
|
---|
136 | cout << "successfully exported bvh" << endl;
|
---|
137 | fclose(fw);
|
---|
138 |
|
---|
139 | return true;
|
---|
140 | }
|
---|
141 |
|
---|
142 |
|
---|
143 | } |
---|