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