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