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 << "Info: 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 | ViewCellsTree *viewCellsTree = new ViewCellsTree();
|
---|
37 |
|
---|
38 | bool ok = viewCellsTree->_LoadFromFile(fr);
|
---|
39 |
|
---|
40 | if (ok)
|
---|
41 | {
|
---|
42 | // skip bvh loading
|
---|
43 | int buffer[6];
|
---|
44 | fread(buffer, sizeof(int), 6, fr);
|
---|
45 |
|
---|
46 | const size_t numNodes = buffer[5];
|
---|
47 |
|
---|
48 | for (int i = 0; i < numNodes; ++ i)
|
---|
49 | fread(buffer, sizeof(int), 4, fr);
|
---|
50 |
|
---|
51 | AllocateLeafViewCells(viewCellsTree);
|
---|
52 |
|
---|
53 | ok = LoadPvs(fr, bvh);
|
---|
54 | }
|
---|
55 |
|
---|
56 | fclose(fr);
|
---|
57 |
|
---|
58 | if (ok)
|
---|
59 | cerr << "Info: visibility solution loaded" << endl;
|
---|
60 | else
|
---|
61 | cerr << "Info: loading visibility solution failed." << endl;
|
---|
62 |
|
---|
63 | return viewCellsTree;
|
---|
64 | }
|
---|
65 |
|
---|
66 |
|
---|
67 | bool VisibilitySolutionLoader::CreateIdSortedList(Bvh *bvh, BvhNodeContainer &nodes)
|
---|
68 | {
|
---|
69 | std::stack<BvhNode *> tStack;
|
---|
70 | tStack.push(bvh->GetRoot());
|
---|
71 |
|
---|
72 | while (!tStack.empty())
|
---|
73 | {
|
---|
74 | BvhNode *node = tStack.top();
|
---|
75 | tStack.pop();
|
---|
76 |
|
---|
77 | nodes.push_back(node);
|
---|
78 |
|
---|
79 | if (!node->IsLeaf())
|
---|
80 | {
|
---|
81 | BvhInterior *interior = static_cast<BvhInterior *>(node);
|
---|
82 |
|
---|
83 | BvhNode *front = interior->GetFront();
|
---|
84 | BvhNode *back = interior->GetBack();
|
---|
85 |
|
---|
86 | tStack.push(front);
|
---|
87 | tStack.push(back);
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | return true;
|
---|
92 | }
|
---|
93 |
|
---|
94 |
|
---|
95 | bool VisibilitySolutionLoader::LoadPvs(FILE *fw, Bvh *bvh)
|
---|
96 | {
|
---|
97 | int number, entries;
|
---|
98 |
|
---|
99 | fread(&number, sizeof(int), 1, fw);
|
---|
100 |
|
---|
101 | if (number == 0)
|
---|
102 | {
|
---|
103 | cerr << "Info: Warning empty PVSs in visibility solution" << endl;
|
---|
104 | return true;
|
---|
105 | }
|
---|
106 |
|
---|
107 | if (number != mViewCells.size())
|
---|
108 | {
|
---|
109 | cerr << "Info: Number of view cells does not match when loading PVSs!" << endl;
|
---|
110 | return false;
|
---|
111 | }
|
---|
112 |
|
---|
113 | BvhNodeContainer nodes;
|
---|
114 | CreateIdSortedList(bvh, nodes);
|
---|
115 |
|
---|
116 | for (int i = 0; i < number; ++ i)
|
---|
117 | {
|
---|
118 | fread(&entries, sizeof(int), 1, fw);
|
---|
119 |
|
---|
120 | for (int j=0; j < entries; ++ j)
|
---|
121 | {
|
---|
122 | int objectId;
|
---|
123 | float time;
|
---|
124 |
|
---|
125 | fread(&objectId, sizeof(int), 1, fw);
|
---|
126 | fread(&time, sizeof(float), 1, fw);
|
---|
127 |
|
---|
128 | BvhNode *node = nodes[objectId];
|
---|
129 |
|
---|
130 | int geometrySize;
|
---|
131 | SceneEntity **entities;
|
---|
132 | entities = bvh->GetGeometry(node, geometrySize);
|
---|
133 |
|
---|
134 | for (int k = 0; k < geometrySize; ++ k)
|
---|
135 | {
|
---|
136 | mViewCells[i]->mPvs.AddEntry(entities[k]);
|
---|
137 | }
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | return true;
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | void VisibilitySolutionLoader::AllocateLeafViewCells(ViewCellsTree *viewCellsTree)
|
---|
146 | {
|
---|
147 | stack< pair<ViewCellsTreeNode *, AxisAlignedBox3> > nodeStack;
|
---|
148 |
|
---|
149 | nodeStack.push(pair<ViewCellsTreeNode *, AxisAlignedBox3>
|
---|
150 | (viewCellsTree->mRoot, viewCellsTree->mBox));
|
---|
151 |
|
---|
152 | int id = 0;
|
---|
153 |
|
---|
154 | mViewCells.clear();
|
---|
155 |
|
---|
156 | while (!nodeStack.empty())
|
---|
157 | {
|
---|
158 | ViewCellsTreeNode *node = nodeStack.top().first;
|
---|
159 |
|
---|
160 | if (node->IsLeaf())
|
---|
161 | {
|
---|
162 | if (node->mViewCell == NULL)
|
---|
163 | {
|
---|
164 | node->mViewCell = new ViewCell();
|
---|
165 | node->mViewCell->SetId(id ++);
|
---|
166 | node->mViewCell->SetBox(nodeStack.top().second);
|
---|
167 | }
|
---|
168 |
|
---|
169 | mViewCells.push_back(node->mViewCell);
|
---|
170 |
|
---|
171 | nodeStack.pop();
|
---|
172 | }
|
---|
173 | else
|
---|
174 | {
|
---|
175 | AxisAlignedBox3 box = nodeStack.top().second;
|
---|
176 | nodeStack.pop();
|
---|
177 |
|
---|
178 | AxisAlignedBox3 newBox = box;
|
---|
179 | newBox.SetMin(node->mAxis, node->mPosition);
|
---|
180 |
|
---|
181 | nodeStack.push(
|
---|
182 | pair<ViewCellsTreeNode *, AxisAlignedBox3>(node->mFront, newBox)
|
---|
183 | );
|
---|
184 |
|
---|
185 | newBox = box;
|
---|
186 | newBox.SetMax(node->mAxis, node->mPosition);
|
---|
187 |
|
---|
188 | nodeStack.push(
|
---|
189 | pair<ViewCellsTreeNode *, AxisAlignedBox3>(node->mBack, newBox)
|
---|
190 | );
|
---|
191 | }
|
---|
192 | }
|
---|
193 |
|
---|
194 | cout << "#of viewcells = " << id << endl;
|
---|
195 | }
|
---|
196 |
|
---|
197 |
|
---|
198 | } |
---|