1 | #include "ViewCellsTree.h"
|
---|
2 | #include "Timer/PerfTimer.h"
|
---|
3 | #include <stack>
|
---|
4 | #include <string>
|
---|
5 |
|
---|
6 | using namespace std;
|
---|
7 |
|
---|
8 |
|
---|
9 | // MAGIC of all bin exports
|
---|
10 | #ifndef MAGIC
|
---|
11 | #define MAGIC 0x827923
|
---|
12 | #endif
|
---|
13 |
|
---|
14 | #define VIEWCELLS_VERSION 1.0f
|
---|
15 |
|
---|
16 | // hack: this has to be set to 0.05f for powerplant, to 1 otherwise
|
---|
17 | //static const float sScale = 0.05f;
|
---|
18 | static const float sScale = 1.0f;
|
---|
19 |
|
---|
20 |
|
---|
21 | namespace CHCDemoEngine
|
---|
22 | {
|
---|
23 |
|
---|
24 |
|
---|
25 | ViewCell *ViewCellsTree::GetViewCell(const Vector3 &point) const
|
---|
26 | {
|
---|
27 | ViewCellsTreeNode *node = mRoot;
|
---|
28 |
|
---|
29 | while (!node->IsLeaf())
|
---|
30 | {
|
---|
31 | //cout << "pt: " << point[node->mAxis] << " " << node->mPosition << endl;
|
---|
32 | if (point[node->mAxis] < node->mPosition)
|
---|
33 | node = node->mBack;
|
---|
34 | else
|
---|
35 | node = node->mFront;
|
---|
36 | }
|
---|
37 |
|
---|
38 | //cout << "vc: " << node->mViewCell->GetBox() << " pvs " << node->mViewCell->mPvs.Size() << endl;
|
---|
39 |
|
---|
40 | return node->mViewCell;
|
---|
41 | }
|
---|
42 |
|
---|
43 |
|
---|
44 | bool ViewCellsTree::LoadFromFile(const std::string &filename)
|
---|
45 | {
|
---|
46 | cout << "Loading view cells from file '" + filename + "'";
|
---|
47 |
|
---|
48 | FILE *fr = fopen(filename.c_str(), "rb");
|
---|
49 |
|
---|
50 | if (!fr)
|
---|
51 | {
|
---|
52 | cerr << "Error: Cannot open file for reading" << endl;
|
---|
53 | return false;
|
---|
54 | }
|
---|
55 |
|
---|
56 | bool result = _LoadFromFile(fr);
|
---|
57 |
|
---|
58 | cout << "finished loading view cells. " << endl;
|
---|
59 | fclose(fr);
|
---|
60 |
|
---|
61 | return result;
|
---|
62 | }
|
---|
63 |
|
---|
64 |
|
---|
65 | bool ViewCellsTree::_LoadFromFile(FILE *fr)
|
---|
66 | {
|
---|
67 | int buffer[256];
|
---|
68 | fread(buffer, sizeof(int), 2, fr);
|
---|
69 |
|
---|
70 | if (buffer[0] != MAGIC)
|
---|
71 | {
|
---|
72 | cerr << "Error: Wrong file type" << endl;
|
---|
73 | return false;
|
---|
74 | }
|
---|
75 |
|
---|
76 | if (buffer[1] != (int)(1000 * VIEWCELLS_VERSION))
|
---|
77 | {
|
---|
78 | cerr << "Error: Wrong viewcells version" << endl;
|
---|
79 | return false;
|
---|
80 | }
|
---|
81 |
|
---|
82 | AxisAlignedBox3 box;
|
---|
83 | // get the bounding box
|
---|
84 | // exchange x and y coordinates and recompute box
|
---|
85 | fread(&box, sizeof(AxisAlignedBox3), 1, fr);
|
---|
86 |
|
---|
87 | box.Scale(sScale);
|
---|
88 |
|
---|
89 | Vector3 v1(box.Min().x, -box.Min().z, box.Min().y);
|
---|
90 | Vector3 v2(box.Max().x, -box.Max().z, box.Max().y);
|
---|
91 |
|
---|
92 | mBox.Initialize();
|
---|
93 |
|
---|
94 | mBox.Include(v1);
|
---|
95 | mBox.Include(v2);
|
---|
96 |
|
---|
97 | stack<ViewCellsTreeNode **> nodeStack;
|
---|
98 | nodeStack.push(&mRoot);
|
---|
99 |
|
---|
100 | cout << "view cells bounds " << mBox << endl;
|
---|
101 |
|
---|
102 | int numViewCells = 0;
|
---|
103 |
|
---|
104 | int axes[] = {0, 0, 0};
|
---|
105 |
|
---|
106 | while (!nodeStack.empty())
|
---|
107 | {
|
---|
108 | ViewCellsTreeNode *&node = *nodeStack.top();
|
---|
109 | nodeStack.pop();
|
---|
110 | node = new ViewCellsTreeNode();
|
---|
111 |
|
---|
112 | ++ numViewCells;
|
---|
113 |
|
---|
114 | int axis;
|
---|
115 | fread(&axis, sizeof(int), 1, fr);
|
---|
116 |
|
---|
117 | // exchange y and z axis like for whole scene
|
---|
118 | if (axis == 2) axis = 1;
|
---|
119 | else if (axis == 1) axis = 2;
|
---|
120 |
|
---|
121 | node->mAxis = axis;
|
---|
122 |
|
---|
123 | if (!node->IsLeaf())
|
---|
124 | {
|
---|
125 | ++ axes[node->mAxis];
|
---|
126 |
|
---|
127 | float mypos;
|
---|
128 | fread(&mypos, sizeof(float), 1, fr);
|
---|
129 |
|
---|
130 | mypos *= sScale;
|
---|
131 |
|
---|
132 | // changed coordinate system must be considered also here
|
---|
133 | if (node->mAxis == 1)
|
---|
134 | {
|
---|
135 | node->mPosition = -mypos;
|
---|
136 |
|
---|
137 | nodeStack.push(&node->mBack);
|
---|
138 | nodeStack.push(&node->mFront);
|
---|
139 | //cout << mypos << " ";
|
---|
140 | }
|
---|
141 | else
|
---|
142 | {
|
---|
143 | node->mPosition = mypos;
|
---|
144 |
|
---|
145 | nodeStack.push(&node->mFront);
|
---|
146 | nodeStack.push(&node->mBack);
|
---|
147 | }
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | cout << "\nloaded " << numViewCells << " view cells" << " " << axes[0] << " " << axes[1] << " " << axes[2] << endl;
|
---|
152 | return true;
|
---|
153 | }
|
---|
154 |
|
---|
155 |
|
---|
156 | } |
---|