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