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

Revision 3286, 5.3 KB checked in by mattausch, 15 years ago (diff)

worked on visualizations

Line 
1#include "VisibilitySolutionLoader.h"
2#include "ViewCellsTree.h"
3#include "Bvh.h"
4#include "SceneEntity.h"
5#include <stack>
6
7
8using namespace std;
9
10       
11namespace CHCDemoEngine
12{
13
14
15typedef vector<ViewCell *> ViewCellsContainer;
16
17
18ViewCellsTree *VisibilitySolutionLoader::Load(const std::string &filename,
19                                                                                          Bvh *bvh,
20                                                                                          float &totalSamples,
21                                                                                          float &totalTime,
22                                                                                          float viewCellsScaleFactor
23                                                                                          )
24{
25        FILE *fr = fopen(filename.c_str(), "rb");
26 
27        cout << "Loading visibility solution from file '" + filename + "'" << endl;
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
38        cout << "loading view cells" << endl;
39
40        ViewCellsTree *viewCellsTree = new ViewCellsTree();
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->IsVirtualLeaf())
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::CreateIdSortedList2(BvhNode *n,
119                                                                                                   BvhNodeContainer &nodes)
120{
121        nodes.push_back(n);
122
123        if (!n->IsLeaf() && !n->IsVirtualLeaf())
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
138bool VisibilitySolutionLoader::LoadPvs(FILE *fw, Bvh *bvh)
139{
140        int number, entries;
141        fread(&number, sizeof(int), 1, fw);
142
143        if (!number)
144        {
145                cerr << "Warning: empty PVSs in visibility solution" << endl;
146                return true;
147        }
148
149        if (number != mViewCells.size())
150        {
151                cerr << "Warning: Number of view cells (" << number << ", "
152                         << (int)mViewCells.size() << ") does not match when loading PVSs!" << endl;
153                return false;
154        }
155
156        BvhNodeContainer nodes;
157        CreateIdSortedList2(bvh->GetStaticRoot(), nodes);
158        //CreateIdSortedList(bvh, nodes);
159
160        const int tenpercent = number / 10;
161        int j = 0;
162
163        for (int i = 0; i < number; ++ i)
164        {
165                if ((i % tenpercent) == (tenpercent - 1))
166                {
167                        j += 10;
168                        cout << j << " percent of the view cells loaded" << endl;
169                }
170
171                fread(&entries, sizeof(int), 1, fw);
172
173                for (int j = 0; j < entries; ++ j)
174                {
175                        int objectId;
176                        float time;
177               
178                        fread(&objectId, sizeof(int), 1, fw);
179                        fread(&time, sizeof(float), 1, fw);
180       
181                        BvhNode *node = nodes[objectId];
182                        mViewCells[i]->mPvs.AddEntry(bvh, node, time);
183                }
184        }
185
186        return true;
187}
188
189
190void VisibilitySolutionLoader::AllocateLeafViewCells(ViewCellsTree *viewCellsTree)
191{
192        stack< pair<ViewCellsTreeNode *, AxisAlignedBox3> > nodeStack;
193
194        nodeStack.push(pair<ViewCellsTreeNode *, AxisAlignedBox3>
195                           (viewCellsTree->mRoot, viewCellsTree->mBox));
196
197        int id = 0;
198
199        mViewCells.clear();
200        int axes[] = {0, 0, 0};
201
202        while (!nodeStack.empty())
203        {
204                ViewCellsTreeNode *node = nodeStack.top().first;
205               
206                if (node->IsLeaf())
207                {
208                        if (!node->mViewCell)
209                        {
210                                node->mViewCell = new ViewCell();
211                                node->mViewCell->SetId(id ++);
212                                node->mViewCell->SetBox(nodeStack.top().second);
213                        }
214
215                        mViewCells.push_back(node->mViewCell);
216
217                        nodeStack.pop();
218                }
219                else
220                {
221                        AxisAlignedBox3 box = nodeStack.top().second;
222                        nodeStack.pop();
223
224                        AxisAlignedBox3 newBox1 = box;
225                        AxisAlignedBox3 newBox2 = box;
226               
227                        newBox1.SetMin(node->mAxis, node->mPosition);
228                        newBox2.SetMax(node->mAxis, node->mPosition);
229
230                        ++ axes[node->mAxis];
231
232                        if (node->mAxis == 1)
233                        {
234                                nodeStack.push(pair<ViewCellsTreeNode *, AxisAlignedBox3>(node->mBack, newBox2));
235                                nodeStack.push(pair<ViewCellsTreeNode *, AxisAlignedBox3>(node->mFront, newBox1));
236                        }
237                        else
238                        {
239                                nodeStack.push(pair<ViewCellsTreeNode *, AxisAlignedBox3>(node->mFront, newBox1));
240                                nodeStack.push(pair<ViewCellsTreeNode *, AxisAlignedBox3>(node->mBack, newBox2));
241                        }
242                }
243        }
244
245        cout << "loaded " << id << " view cells" << endl;
246}
247
248
249}
Note: See TracBrowser for help on using the repository browser.