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

Revision 3282, 6.0 KB checked in by mattausch, 15 years ago (diff)

probably found export error

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