1 | #include "VisibilitySolutionLoader.h"
|
---|
2 | #include "ViewCellsTree.h"
|
---|
3 | #include "Bvh.h"
|
---|
4 | #include "SceneEntity.h"
|
---|
5 | #include <stack>
|
---|
6 |
|
---|
7 |
|
---|
8 | using namespace std;
|
---|
9 |
|
---|
10 |
|
---|
11 | namespace CHCDemoEngine
|
---|
12 | {
|
---|
13 |
|
---|
14 |
|
---|
15 | typedef vector<ViewCell *> ViewCellsContainer;
|
---|
16 |
|
---|
17 |
|
---|
18 | ViewCellsTree *VisibilitySolutionLoader::Load(const std::string &filename,
|
---|
19 | Bvh *bvh,
|
---|
20 | float &totalSamples,
|
---|
21 | float &totalTime,
|
---|
22 | float viewCellsScaleFactor
|
---|
23 | )
|
---|
24 | {
|
---|
25 | FILE *fr = fopen(filename.c_str(), "rb");
|
---|
26 |
|
---|
27 | cout << "Loading visibility solution from file '" + filename + "'" << endl;
|
---|
28 |
|
---|
29 | if (fr == NULL)
|
---|
30 | {
|
---|
31 | cerr << "Error: Cannot open file for reading" << endl;
|
---|
32 | return NULL;
|
---|
33 | }
|
---|
34 |
|
---|
35 | fread(&totalSamples, sizeof(float), 1, fr);
|
---|
36 | fread(&totalTime, sizeof(float), 1, fr);
|
---|
37 |
|
---|
38 | cout << "visibity solution created with " << totalSamples << " in " << totalTime << " secs" << endl;
|
---|
39 | cout << "loading view cells" << endl;
|
---|
40 |
|
---|
41 | ViewCellsTree *viewCellsTree = new ViewCellsTree();
|
---|
42 | bool ok = viewCellsTree->_LoadFromFile(fr, viewCellsScaleFactor);
|
---|
43 |
|
---|
44 | cout << "finished loading view cells" << endl;
|
---|
45 |
|
---|
46 |
|
---|
47 | if (ok)
|
---|
48 | {
|
---|
49 | // skip loading of bvh nodes
|
---|
50 | int buffer[6];
|
---|
51 | fread(buffer, sizeof(int), 6, fr);
|
---|
52 |
|
---|
53 | const int numNodes = buffer[5];
|
---|
54 | const int numTriangleIds = buffer[2];
|
---|
55 |
|
---|
56 | // skip triangle ids
|
---|
57 | cout << "skipping " << numTriangleIds << " triangle ids" << endl;
|
---|
58 |
|
---|
59 | int tid;
|
---|
60 | for (int i = 0; i < numTriangleIds; ++ i)
|
---|
61 | fread(&tid, sizeof(int), 1, fr);
|
---|
62 |
|
---|
63 | cout << "skipping " << numNodes << " bvh nodes" << endl;
|
---|
64 |
|
---|
65 | for (int i = 0; i < numNodes; ++ i)
|
---|
66 | fread(buffer, sizeof(int), 4, fr);
|
---|
67 |
|
---|
68 |
|
---|
69 | cout << "allocating view cells" << endl;
|
---|
70 |
|
---|
71 | AllocateLeafViewCells(viewCellsTree);
|
---|
72 |
|
---|
73 | cout << "loading pvss" << endl;
|
---|
74 |
|
---|
75 | ok = LoadPvs(fr, bvh);
|
---|
76 |
|
---|
77 | cout << "finished loading pvss" << endl;
|
---|
78 | }
|
---|
79 |
|
---|
80 | fclose(fr);
|
---|
81 |
|
---|
82 | if (ok)
|
---|
83 | cout << "Visibility solution loaded" << endl;
|
---|
84 | else
|
---|
85 | cerr << "Error: loading visibility solution failed." << endl;
|
---|
86 |
|
---|
87 | return viewCellsTree;
|
---|
88 | }
|
---|
89 |
|
---|
90 |
|
---|
91 | bool VisibilitySolutionLoader::CreateIdSortedList(Bvh *bvh, BvhNodeContainer &nodes)
|
---|
92 | {
|
---|
93 | std::stack<BvhNode *> tStack;
|
---|
94 | tStack.push(bvh->GetStaticRoot());
|
---|
95 |
|
---|
96 | while (!tStack.empty())
|
---|
97 | {
|
---|
98 | BvhNode *node = tStack.top();
|
---|
99 | tStack.pop();
|
---|
100 |
|
---|
101 | nodes.push_back(node);
|
---|
102 |
|
---|
103 | if (!node->IsVirtualLeaf())
|
---|
104 | {
|
---|
105 | BvhInterior *interior = static_cast<BvhInterior *>(node);
|
---|
106 |
|
---|
107 | BvhNode *front = interior->GetFront();
|
---|
108 | BvhNode *back = interior->GetBack();
|
---|
109 |
|
---|
110 | tStack.push(front);
|
---|
111 | tStack.push(back);
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | return true;
|
---|
116 | }
|
---|
117 |
|
---|
118 |
|
---|
119 | bool VisibilitySolutionLoader::CreateIdSortedList2(BvhNode *n,
|
---|
120 | BvhNodeContainer &nodes)
|
---|
121 | {
|
---|
122 | nodes.push_back(n);
|
---|
123 |
|
---|
124 | if (!n->IsLeaf() && !n->IsVirtualLeaf())
|
---|
125 | {
|
---|
126 | BvhInterior *interior = static_cast<BvhInterior *>(n);
|
---|
127 |
|
---|
128 | BvhNode *front = interior->GetFront();
|
---|
129 | BvhNode *back = interior->GetBack();
|
---|
130 |
|
---|
131 | CreateIdSortedList2(front, nodes);
|
---|
132 | CreateIdSortedList2(back, nodes);
|
---|
133 | }
|
---|
134 |
|
---|
135 | return true;
|
---|
136 | }
|
---|
137 |
|
---|
138 |
|
---|
139 | bool VisibilitySolutionLoader::LoadPvs(FILE *fw, Bvh *bvh)
|
---|
140 | {
|
---|
141 | int number, entries;
|
---|
142 | fread(&number, sizeof(int), 1, fw);
|
---|
143 |
|
---|
144 | if (!number)
|
---|
145 | {
|
---|
146 | cerr << "Warning: empty PVSs in visibility solution" << endl;
|
---|
147 | return true;
|
---|
148 | }
|
---|
149 |
|
---|
150 | if (number != mViewCells.size())
|
---|
151 | {
|
---|
152 | cerr << "Warning: Number of view cells (" << number << ", "
|
---|
153 | << (int)mViewCells.size() << ") does not match when loading PVSs!" << endl;
|
---|
154 | return false;
|
---|
155 | }
|
---|
156 |
|
---|
157 | BvhNodeContainer nodes;
|
---|
158 | CreateIdSortedList2(bvh->GetStaticRoot(), nodes);
|
---|
159 | //CreateIdSortedList(bvh, nodes);
|
---|
160 |
|
---|
161 | const int tenpercent = number / 10;
|
---|
162 | int j = 0;
|
---|
163 |
|
---|
164 | for (int i = 0; i < number; ++ i)
|
---|
165 | {
|
---|
166 | if ((i % tenpercent) == (tenpercent - 1))
|
---|
167 | {
|
---|
168 | j += 10;
|
---|
169 | cout << j << " percent of the view cells loaded" << endl;
|
---|
170 | }
|
---|
171 |
|
---|
172 | fread(&entries, sizeof(int), 1, fw);
|
---|
173 |
|
---|
174 | for (int j = 0; j < entries; ++ j)
|
---|
175 | {
|
---|
176 | int objectId;
|
---|
177 | float time;
|
---|
178 |
|
---|
179 | fread(&objectId, sizeof(int), 1, fw);
|
---|
180 | fread(&time, sizeof(float), 1, fw);
|
---|
181 |
|
---|
182 | BvhNode *node = nodes[objectId];
|
---|
183 | mViewCells[i]->mPvs.AddEntry(bvh, node, time);
|
---|
184 | }
|
---|
185 | }
|
---|
186 |
|
---|
187 | return true;
|
---|
188 | }
|
---|
189 |
|
---|
190 |
|
---|
191 | void VisibilitySolutionLoader::AllocateLeafViewCells(ViewCellsTree *viewCellsTree)
|
---|
192 | {
|
---|
193 | stack< pair<ViewCellsTreeNode *, AxisAlignedBox3> > nodeStack;
|
---|
194 |
|
---|
195 | nodeStack.push(pair<ViewCellsTreeNode *, AxisAlignedBox3>
|
---|
196 | (viewCellsTree->mRoot, viewCellsTree->mBox));
|
---|
197 |
|
---|
198 | int id = 0;
|
---|
199 |
|
---|
200 | mViewCells.clear();
|
---|
201 | int axes[] = {0, 0, 0};
|
---|
202 |
|
---|
203 | while (!nodeStack.empty())
|
---|
204 | {
|
---|
205 | ViewCellsTreeNode *node = nodeStack.top().first;
|
---|
206 |
|
---|
207 | if (node->IsLeaf())
|
---|
208 | {
|
---|
209 | if (!node->mViewCell)
|
---|
210 | {
|
---|
211 | node->mViewCell = new ViewCell();
|
---|
212 | node->mViewCell->SetId(id ++);
|
---|
213 | node->mViewCell->SetBox(nodeStack.top().second);
|
---|
214 | }
|
---|
215 |
|
---|
216 | mViewCells.push_back(node->mViewCell);
|
---|
217 |
|
---|
218 | nodeStack.pop();
|
---|
219 | }
|
---|
220 | else
|
---|
221 | {
|
---|
222 | AxisAlignedBox3 box = nodeStack.top().second;
|
---|
223 | nodeStack.pop();
|
---|
224 |
|
---|
225 | AxisAlignedBox3 newBox1 = box;
|
---|
226 | AxisAlignedBox3 newBox2 = box;
|
---|
227 |
|
---|
228 | newBox1.SetMin(node->mAxis, node->mPosition);
|
---|
229 | newBox2.SetMax(node->mAxis, node->mPosition);
|
---|
230 |
|
---|
231 | ++ axes[node->mAxis];
|
---|
232 |
|
---|
233 | if (node->mAxis == 1)
|
---|
234 | {
|
---|
235 | nodeStack.push(pair<ViewCellsTreeNode *, AxisAlignedBox3>(node->mBack, newBox2));
|
---|
236 | nodeStack.push(pair<ViewCellsTreeNode *, AxisAlignedBox3>(node->mFront, newBox1));
|
---|
237 | }
|
---|
238 | else
|
---|
239 | {
|
---|
240 | nodeStack.push(pair<ViewCellsTreeNode *, AxisAlignedBox3>(node->mFront, newBox1));
|
---|
241 | nodeStack.push(pair<ViewCellsTreeNode *, AxisAlignedBox3>(node->mBack, newBox2));
|
---|
242 | }
|
---|
243 | }
|
---|
244 | }
|
---|
245 |
|
---|
246 | cout << "loaded " << id << " view cells" << endl;
|
---|
247 | }
|
---|
248 |
|
---|
249 |
|
---|
250 | } |
---|