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

Revision 3259, 4.9 KB checked in by mattausch, 15 years ago (diff)
Line 
1#include "VisibilitySolutionLoader.h"
2#include "ViewCellsTree.h"
3#include "Bvh.h"
4#include <stack>
5
6
7using namespace std;
8
9       
10namespace CHCDemoEngine
11{
12
13
14typedef vector<ViewCell *> ViewCellsContainer;
15
16
17ViewCellsTree *VisibilitySolutionLoader::Load(const std::string &filename,
18                                                                                          Bvh *bvh,
19                                                                                          float viewCellsScaleFactor)
20{
21        FILE *fr = fopen(filename.c_str(), "rb");
22 
23        cout << "Loading visibility solution from file '" + filename + "'" << endl;
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
37        cout << "loading view cells" << endl;
38
39        ViewCellsTree *viewCellsTree = new ViewCellsTree();
40
41        bool ok = viewCellsTree->_LoadFromFile(fr, viewCellsScaleFactor);
42
43        cout << "finished loading view cells" << endl;
44
45 
46        if (ok)
47        {
48                // skip loading of bvh nodes
49                int buffer[6];
50                fread(buffer, sizeof(int), 6, fr);
51
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
64                for (int i = 0; i < numNodes; ++ i)
65                        fread(buffer, sizeof(int), 4, fr);
66
67
68                cout << "allocating view cells" << endl;
69
70                AllocateLeafViewCells(viewCellsTree);
71               
72                cout << "loading pvss" << endl;
73
74                ok = LoadPvs(fr, bvh);
75
76                cout << "finished loading pvss" << endl;
77        }
78
79        fclose(fr);
80
81        if (ok)
82                cout << "Visibility solution loaded" << endl;
83        else
84                cerr << "Error: loading visibility solution failed." << endl;
85
86        return viewCellsTree;
87}
88
89
90bool VisibilitySolutionLoader::CreateIdSortedList(Bvh *bvh, BvhNodeContainer &nodes)
91{
92        std::stack<BvhNode *> tStack;
93        tStack.push(bvh->GetStaticRoot());
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
114        return true;
115}
116
117
118bool VisibilitySolutionLoader::LoadPvs(FILE *fw, Bvh *bvh)
119{
120        int number, entries;
121
122        fread(&number, sizeof(int), 1, fw);
123
124        if (!number)
125        {
126                cerr << "Warning: empty PVSs in visibility solution" << endl;
127                return true;
128        }
129
130        if (number != mViewCells.size())
131        {
132                cerr << "Warning: Number of view cells (" << number << ", " << (int)mViewCells.size() << ") does not match when loading PVSs!" << endl;
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
143                for (int j = 0; j < entries; ++ j)
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
153                        mViewCells[i]->mPvs.AddEntry(bvh, node);
154                }
155        }
156
157        return true;
158}
159
160
161void 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();
171        int axes[] = {0, 0, 0};
172
173        while (!nodeStack.empty())
174        {
175                ViewCellsTreeNode *node = nodeStack.top().first;
176               
177                if (node->IsLeaf())
178                {
179                        if (!node->mViewCell)
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
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);
204
205                        ++ axes[node->mAxis];
206
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                        }
217                }
218        }
219
220        cout << "loaded " << id << " view cells" << endl;
221}
222
223
224}
Note: See TracBrowser for help on using the repository browser.