source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/VisibilitySolutionLoader.cpp @ 3290

Revision 3290, 5.4 KB checked in by mattausch, 15 years ago (diff)
RevLine 
[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
8using namespace std;
9
10       
11namespace CHCDemoEngine
12{
13
[3243]14
15typedef vector<ViewCell *> ViewCellsContainer;
16
17
18ViewCellsTree *VisibilitySolutionLoader::Load(const std::string &filename,
[3257]19                                                                                          Bvh *bvh,
[3285]20                                                                                          float &totalSamples,
21                                                                                          float &totalTime,
22                                                                                          float viewCellsScaleFactor
23                                                                                          )
[3242]24{
[3243]25        FILE *fr = fopen(filename.c_str(), "rb");
26 
[3244]27        cout << "Loading visibility solution from file '" + filename + "'" << endl;
[3243]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
[3289]38        cout << "visibity solution created with " << totalSamples << " in " << totalTime << " secs" << endl;
[3244]39        cout << "loading view cells" << endl;
40
[3243]41        ViewCellsTree *viewCellsTree = new ViewCellsTree();
[3257]42        bool ok = viewCellsTree->_LoadFromFile(fr, viewCellsScaleFactor);
[3244]43
44        cout << "finished loading view cells" << endl;
45
[3243]46 
47        if (ok)
48        {
[3244]49                // skip loading of bvh nodes
[3243]50                int buffer[6];
51                fread(buffer, sizeof(int), 6, fr);
52
[3244]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
[3243]65                for (int i = 0; i < numNodes; ++ i)
66                        fread(buffer, sizeof(int), 4, fr);
67
[3244]68
69                cout << "allocating view cells" << endl;
70
[3243]71                AllocateLeafViewCells(viewCellsTree);
72               
[3244]73                cout << "loading pvss" << endl;
74
[3243]75                ok = LoadPvs(fr, bvh);
[3244]76
77                cout << "finished loading pvss" << endl;
[3243]78        }
79
80        fclose(fr);
81
82        if (ok)
[3244]83                cout << "Visibility solution loaded" << endl;
[3243]84        else
[3244]85                cerr << "Error: loading visibility solution failed." << endl;
[3243]86
87        return viewCellsTree;
88}
89
90
91bool VisibilitySolutionLoader::CreateIdSortedList(Bvh *bvh, BvhNodeContainer &nodes)
92{
93        std::stack<BvhNode *> tStack;
[3245]94        tStack.push(bvh->GetStaticRoot());
[3243]95       
96        while (!tStack.empty())
97        {
98                BvhNode *node = tStack.top();
99                tStack.pop();
100
101                nodes.push_back(node);
102
[3279]103                if (!node->IsVirtualLeaf())
[3243]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
[3242]115        return true;
116}
117
[3243]118
[3279]119bool VisibilitySolutionLoader::CreateIdSortedList2(BvhNode *n,
120                                                                                                   BvhNodeContainer &nodes)
121{
122        nodes.push_back(n);
123
[3282]124        if (!n->IsLeaf() && !n->IsVirtualLeaf())
[3279]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
[3243]139bool VisibilitySolutionLoader::LoadPvs(FILE *fw, Bvh *bvh)
140{
141        int number, entries;
142        fread(&number, sizeof(int), 1, fw);
143
[3244]144        if (!number)
[3243]145        {
[3244]146                cerr << "Warning: empty PVSs in visibility solution" << endl;
[3243]147                return true;
148        }
149
150        if (number != mViewCells.size())
151        {
[3284]152                cerr << "Warning: Number of view cells (" << number << ", "
153                         << (int)mViewCells.size() << ") does not match when loading PVSs!" << endl;
[3243]154                return false;
155        }
156
157        BvhNodeContainer nodes;
[3290]158        CreateIdSortedList2(bvh->GetStaticRoot(), nodes);
159        //CreateIdSortedList(bvh, nodes);
[3279]160
[3286]161        const int tenpercent = number / 10;
162        int j = 0;
[3282]163
[3286]164        for (int i = 0; i < number; ++ i)
[3281]165        {
[3286]166                if ((i % tenpercent) == (tenpercent - 1))
[3282]167                {
[3286]168                        j += 10;
169                        cout << j << " percent of the view cells loaded" << endl;
[3282]170                }
[3285]171
[3243]172                fread(&entries, sizeof(int), 1, fw);
173
[3245]174                for (int j = 0; j < entries; ++ j)
[3243]175                {
176                        int objectId;
177                        float time;
178               
179                        fread(&objectId, sizeof(int), 1, fw);
180                        fread(&time, sizeof(float), 1, fw);
[3271]181       
[3243]182                        BvhNode *node = nodes[objectId];
[3284]183                        mViewCells[i]->mPvs.AddEntry(bvh, node, time);
[3243]184                }
185        }
186
187        return true;
188}
189
190
191void 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();
[3245]201        int axes[] = {0, 0, 0};
202
[3243]203        while (!nodeStack.empty())
204        {
205                ViewCellsTreeNode *node = nodeStack.top().first;
206               
207                if (node->IsLeaf())
208                {
[3244]209                        if (!node->mViewCell)
[3243]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
[3245]225                        AxisAlignedBox3 newBox1 = box;
226                        AxisAlignedBox3 newBox2 = box;
[3279]227               
[3245]228                        newBox1.SetMin(node->mAxis, node->mPosition);
229                        newBox2.SetMax(node->mAxis, node->mPosition);
[3243]230
[3245]231                        ++ axes[node->mAxis];
[3243]232
[3245]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                        }
[3243]243                }
244        }
245
[3257]246        cout << "loaded " << id << " view cells" << endl;
[3243]247}
248
249
[3242]250}
Note: See TracBrowser for help on using the repository browser.