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

Revision 3285, 6.2 KB checked in by mattausch, 15 years ago (diff)

working on viz for submission

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/*      ofstream outstream("test.log");
160
161        BvhNode *f = ((BvhInterior *)bvh->GetStaticRoot())->GetFront();
162        BvhNode *b = ((BvhInterior *)bvh->GetStaticRoot())->GetBack();
163
164        outstream << "front: " << f->GetFirstEntity() << " " << f->GetLastEntity() << endl;
165        outstream << "back: " << b->GetFirstEntity() << " " << b->GetLastEntity() << endl;
166
167        for (size_t i = 0; i < nodes.size(); ++ i)
168        {
169                BvhNode *n = nodes[i];
170                //if (nodes[i]->GetId() >= nodes.size()) cout << "id " << nodes[i]->GetId() << endl;
171                if (n->IsVirtualLeaf())
172                {
173                        int geometrySize;
174                        AxisAlignedBox3 box;
175                        SceneEntity **ent = bvh->GetGeometry(n, geometrySize);
176                        box = SceneEntity::ComputeBoundingBox(ent, geometrySize);
177
178                        outstream << n->GetFirstEntity() << " " << n->GetLastEntity() << " " << n->GetBox() << " " << box << endl;
179                }
180        }
181*/             
182        for (int i = 0; i < number; ++ i)
183        {
184                if ((i % number / 10) ==  (number / 10 - 1))
185                        cout << 100 * (i + 1) / 100 << "percent loaded" << endl;
186
187                fread(&entries, sizeof(int), 1, fw);
188
189                for (int j = 0; j < entries; ++ j)
190                {
191                        int objectId;
192                        float time;
193               
194                        fread(&objectId, sizeof(int), 1, fw);
195                        fread(&time, sizeof(float), 1, fw);
196       
197                        BvhNode *node = nodes[objectId];
198                        //if (i == 100) outstream << "t " << time << " ";
199                        mViewCells[i]->mPvs.AddEntry(bvh, node, time);
200                }
201        }
202
203        //outstream.close();
204
205        return true;
206}
207
208
209void VisibilitySolutionLoader::AllocateLeafViewCells(ViewCellsTree *viewCellsTree)
210{
211        stack< pair<ViewCellsTreeNode *, AxisAlignedBox3> > nodeStack;
212
213        nodeStack.push(pair<ViewCellsTreeNode *, AxisAlignedBox3>
214                           (viewCellsTree->mRoot, viewCellsTree->mBox));
215
216        int id = 0;
217
218        mViewCells.clear();
219        int axes[] = {0, 0, 0};
220
221        while (!nodeStack.empty())
222        {
223                ViewCellsTreeNode *node = nodeStack.top().first;
224               
225                if (node->IsLeaf())
226                {
227                        if (!node->mViewCell)
228                        {
229                                node->mViewCell = new ViewCell();
230                                node->mViewCell->SetId(id ++);
231                                node->mViewCell->SetBox(nodeStack.top().second);
232                        }
233
234                        mViewCells.push_back(node->mViewCell);
235
236                        nodeStack.pop();
237                }
238                else
239                {
240                        AxisAlignedBox3 box = nodeStack.top().second;
241                        nodeStack.pop();
242
243                        AxisAlignedBox3 newBox1 = box;
244                        AxisAlignedBox3 newBox2 = box;
245               
246                        newBox1.SetMin(node->mAxis, node->mPosition);
247                        newBox2.SetMax(node->mAxis, node->mPosition);
248
249                        ++ axes[node->mAxis];
250
251                        if (node->mAxis == 1)
252                        {
253                                nodeStack.push(pair<ViewCellsTreeNode *, AxisAlignedBox3>(node->mBack, newBox2));
254                                nodeStack.push(pair<ViewCellsTreeNode *, AxisAlignedBox3>(node->mFront, newBox1));
255                        }
256                        else
257                        {
258                                nodeStack.push(pair<ViewCellsTreeNode *, AxisAlignedBox3>(node->mFront, newBox1));
259                                nodeStack.push(pair<ViewCellsTreeNode *, AxisAlignedBox3>(node->mBack, newBox2));
260                        }
261                }
262        }
263
264        cout << "loaded " << id << " view cells" << endl;
265}
266
267
268}
Note: See TracBrowser for help on using the repository browser.