[3242] | 1 | #include "VisibilitySolutionLoader.h"
|
---|
[3243] | 2 | #include "ViewCellsTree.h"
|
---|
| 3 | #include "Bvh.h"
|
---|
| 4 | #include <stack>
|
---|
[3242] | 5 |
|
---|
| 6 |
|
---|
| 7 | using namespace std;
|
---|
| 8 |
|
---|
| 9 |
|
---|
| 10 | namespace CHCDemoEngine
|
---|
| 11 | {
|
---|
| 12 |
|
---|
[3243] | 13 |
|
---|
| 14 | typedef vector<ViewCell *> ViewCellsContainer;
|
---|
| 15 |
|
---|
| 16 |
|
---|
| 17 | ViewCellsTree *VisibilitySolutionLoader::Load(const std::string &filename,
|
---|
[3257] | 18 | Bvh *bvh,
|
---|
| 19 | float viewCellsScaleFactor)
|
---|
[3242] | 20 | {
|
---|
[3243] | 21 | FILE *fr = fopen(filename.c_str(), "rb");
|
---|
| 22 |
|
---|
[3244] | 23 | cout << "Loading visibility solution from file '" + filename + "'" << endl;
|
---|
[3243] | 24 |
|
---|
| 25 | if (fr == NULL)
|
---|
| 26 | {
|
---|
| 27 | cerr << "Error: Cannot open file for reading" << endl;
|
---|
| 28 | return NULL;
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | float totalSamples;
|
---|
| 32 | float totalTime;
|
---|
| 33 |
|
---|
| 34 | fread(&totalSamples, sizeof(float), 1, fr);
|
---|
| 35 | fread(&totalTime, sizeof(float), 1, fr);
|
---|
| 36 |
|
---|
[3244] | 37 | cout << "loading view cells" << endl;
|
---|
| 38 |
|
---|
[3243] | 39 | ViewCellsTree *viewCellsTree = new ViewCellsTree();
|
---|
| 40 |
|
---|
[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 |
|
---|
| 102 | if (!node->IsLeaf())
|
---|
| 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 |
|
---|
| 118 | bool VisibilitySolutionLoader::LoadPvs(FILE *fw, Bvh *bvh)
|
---|
| 119 | {
|
---|
| 120 | int number, entries;
|
---|
| 121 |
|
---|
| 122 | fread(&number, sizeof(int), 1, fw);
|
---|
| 123 |
|
---|
[3244] | 124 | if (!number)
|
---|
[3243] | 125 | {
|
---|
[3244] | 126 | cerr << "Warning: empty PVSs in visibility solution" << endl;
|
---|
[3243] | 127 | return true;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | if (number != mViewCells.size())
|
---|
| 131 | {
|
---|
[3244] | 132 | cerr << "Warning: Number of view cells (" << number << ", " << (int)mViewCells.size() << ") does not match when loading PVSs!" << endl;
|
---|
[3243] | 133 | return false;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | BvhNodeContainer nodes;
|
---|
| 137 | CreateIdSortedList(bvh, nodes);
|
---|
| 138 |
|
---|
| 139 | for (int i = 0; i < number; ++ i)
|
---|
| 140 | {
|
---|
| 141 | fread(&entries, sizeof(int), 1, fw);
|
---|
| 142 |
|
---|
[3245] | 143 | for (int j = 0; j < entries; ++ j)
|
---|
[3243] | 144 | {
|
---|
| 145 | int objectId;
|
---|
| 146 | float time;
|
---|
| 147 |
|
---|
| 148 | fread(&objectId, sizeof(int), 1, fw);
|
---|
| 149 | fread(&time, sizeof(float), 1, fw);
|
---|
| 150 |
|
---|
| 151 | BvhNode *node = nodes[objectId];
|
---|
| 152 |
|
---|
[3259] | 153 | mViewCells[i]->mPvs.AddEntry(bvh, node);
|
---|
[3243] | 154 | }
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | return true;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 |
|
---|
| 161 | void VisibilitySolutionLoader::AllocateLeafViewCells(ViewCellsTree *viewCellsTree)
|
---|
| 162 | {
|
---|
| 163 | stack< pair<ViewCellsTreeNode *, AxisAlignedBox3> > nodeStack;
|
---|
| 164 |
|
---|
| 165 | nodeStack.push(pair<ViewCellsTreeNode *, AxisAlignedBox3>
|
---|
| 166 | (viewCellsTree->mRoot, viewCellsTree->mBox));
|
---|
| 167 |
|
---|
| 168 | int id = 0;
|
---|
| 169 |
|
---|
| 170 | mViewCells.clear();
|
---|
[3245] | 171 | int axes[] = {0, 0, 0};
|
---|
| 172 |
|
---|
[3243] | 173 | while (!nodeStack.empty())
|
---|
| 174 | {
|
---|
| 175 | ViewCellsTreeNode *node = nodeStack.top().first;
|
---|
| 176 |
|
---|
| 177 | if (node->IsLeaf())
|
---|
| 178 | {
|
---|
[3244] | 179 | if (!node->mViewCell)
|
---|
[3243] | 180 | {
|
---|
| 181 | node->mViewCell = new ViewCell();
|
---|
| 182 | node->mViewCell->SetId(id ++);
|
---|
| 183 | node->mViewCell->SetBox(nodeStack.top().second);
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | mViewCells.push_back(node->mViewCell);
|
---|
| 187 |
|
---|
| 188 | nodeStack.pop();
|
---|
| 189 | }
|
---|
| 190 | else
|
---|
| 191 | {
|
---|
| 192 | AxisAlignedBox3 box = nodeStack.top().second;
|
---|
| 193 | nodeStack.pop();
|
---|
| 194 |
|
---|
[3245] | 195 | AxisAlignedBox3 newBox1 = box;
|
---|
| 196 | AxisAlignedBox3 newBox2 = box;
|
---|
| 197 | /*if (node->mPosition < newBox.Min(node->mAxis))
|
---|
| 198 | cout << "e: " << node->mPosition << " " << newBox.Min(node->mAxis) << endl;
|
---|
| 199 | else if (node->mPosition > newBox.Min(node->mAxis))
|
---|
| 200 | cout << "o: " << node->mPosition << " " << newBox.Min(node->mAxis) << endl;
|
---|
| 201 | */
|
---|
| 202 | newBox1.SetMin(node->mAxis, node->mPosition);
|
---|
| 203 | newBox2.SetMax(node->mAxis, node->mPosition);
|
---|
[3243] | 204 |
|
---|
[3245] | 205 | ++ axes[node->mAxis];
|
---|
[3243] | 206 |
|
---|
[3245] | 207 | if (node->mAxis == 1)
|
---|
| 208 | {
|
---|
| 209 | nodeStack.push(pair<ViewCellsTreeNode *, AxisAlignedBox3>(node->mBack, newBox2));
|
---|
| 210 | nodeStack.push(pair<ViewCellsTreeNode *, AxisAlignedBox3>(node->mFront, newBox1));
|
---|
| 211 | }
|
---|
| 212 | else
|
---|
| 213 | {
|
---|
| 214 | nodeStack.push(pair<ViewCellsTreeNode *, AxisAlignedBox3>(node->mFront, newBox1));
|
---|
| 215 | nodeStack.push(pair<ViewCellsTreeNode *, AxisAlignedBox3>(node->mBack, newBox2));
|
---|
| 216 | }
|
---|
[3243] | 217 | }
|
---|
| 218 | }
|
---|
| 219 |
|
---|
[3257] | 220 | cout << "loaded " << id << " view cells" << endl;
|
---|
[3243] | 221 | }
|
---|
| 222 |
|
---|
| 223 |
|
---|
[3242] | 224 | } |
---|