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

Revision 3284, 6.0 KB checked in by mattausch, 15 years ago (diff)
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 viewCellsScaleFactor)
21{
22        FILE *fr = fopen(filename.c_str(), "rb");
23 
24        cout << "Loading visibility solution from file '" + filename + "'" << endl;
25 
26        if (fr == NULL)
27        {
28                cerr << "Error: Cannot open file for reading" << endl;
29                return NULL;
30        }
31
32        float totalSamples;
33        float totalTime;
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                fread(&entries, sizeof(int), 1, fw);
185
186                for (int j = 0; j < entries; ++ j)
187                {
188                        int objectId;
189                        float time;
190               
191                        fread(&objectId, sizeof(int), 1, fw);
192                        fread(&time, sizeof(float), 1, fw);
193       
194                        BvhNode *node = nodes[objectId];
195                        if (i == 100)
196                                outstream << "t " << time << " ";
197                        mViewCells[i]->mPvs.AddEntry(bvh, node, time);
198                }
199        }
200
201        outstream.close();
202
203        return true;
204}
205
206
207void VisibilitySolutionLoader::AllocateLeafViewCells(ViewCellsTree *viewCellsTree)
208{
209        stack< pair<ViewCellsTreeNode *, AxisAlignedBox3> > nodeStack;
210
211        nodeStack.push(pair<ViewCellsTreeNode *, AxisAlignedBox3>
212                           (viewCellsTree->mRoot, viewCellsTree->mBox));
213
214        int id = 0;
215
216        mViewCells.clear();
217        int axes[] = {0, 0, 0};
218
219        while (!nodeStack.empty())
220        {
221                ViewCellsTreeNode *node = nodeStack.top().first;
222               
223                if (node->IsLeaf())
224                {
225                        if (!node->mViewCell)
226                        {
227                                node->mViewCell = new ViewCell();
228                                node->mViewCell->SetId(id ++);
229                                node->mViewCell->SetBox(nodeStack.top().second);
230                        }
231
232                        mViewCells.push_back(node->mViewCell);
233
234                        nodeStack.pop();
235                }
236                else
237                {
238                        AxisAlignedBox3 box = nodeStack.top().second;
239                        nodeStack.pop();
240
241                        AxisAlignedBox3 newBox1 = box;
242                        AxisAlignedBox3 newBox2 = box;
243               
244                        newBox1.SetMin(node->mAxis, node->mPosition);
245                        newBox2.SetMax(node->mAxis, node->mPosition);
246
247                        ++ axes[node->mAxis];
248
249                        if (node->mAxis == 1)
250                        {
251                                nodeStack.push(pair<ViewCellsTreeNode *, AxisAlignedBox3>(node->mBack, newBox2));
252                                nodeStack.push(pair<ViewCellsTreeNode *, AxisAlignedBox3>(node->mFront, newBox1));
253                        }
254                        else
255                        {
256                                nodeStack.push(pair<ViewCellsTreeNode *, AxisAlignedBox3>(node->mFront, newBox1));
257                                nodeStack.push(pair<ViewCellsTreeNode *, AxisAlignedBox3>(node->mBack, newBox2));
258                        }
259                }
260        }
261
262        cout << "loaded " << id << " view cells" << endl;
263}
264
265
266}
Note: See TracBrowser for help on using the repository browser.