1 | #include "ViewCell.h"
|
---|
2 | #include "Mesh.h"
|
---|
3 | #include "Intersectable.h"
|
---|
4 | #include "MeshKdTree.h"
|
---|
5 | #include "Triangle3.h"
|
---|
6 |
|
---|
7 | ViewCell::HierarchyType ViewCell::sHierarchy = BSP;
|
---|
8 |
|
---|
9 | ViewCell::ViewCell(): MeshInstance(NULL), mPiercingRays(0)
|
---|
10 | {
|
---|
11 | }
|
---|
12 |
|
---|
13 | ViewCell::ViewCell(Mesh *mesh): MeshInstance(mesh), mPiercingRays(0)
|
---|
14 | {
|
---|
15 | }
|
---|
16 |
|
---|
17 | ViewCell *ViewCell::Generate(Mesh *mesh)
|
---|
18 | {
|
---|
19 | switch(sHierarchy)
|
---|
20 | {
|
---|
21 | case KD:
|
---|
22 | return new ViewCell(mesh);
|
---|
23 | case BSP:
|
---|
24 | return new BspViewCell(mesh);
|
---|
25 | default:
|
---|
26 | Debug << "should not come here 3" << endl;
|
---|
27 | return NULL;
|
---|
28 | }
|
---|
29 | }
|
---|
30 |
|
---|
31 | const ViewCellPvs &ViewCell::GetPvs() const
|
---|
32 | {
|
---|
33 | return mPvs;
|
---|
34 | }
|
---|
35 |
|
---|
36 | ViewCellPvs &ViewCell::GetPvs()
|
---|
37 | {
|
---|
38 | return mPvs;
|
---|
39 | }
|
---|
40 |
|
---|
41 | int ViewCell::Type() const
|
---|
42 | {
|
---|
43 | return VIEW_CELL;
|
---|
44 | }
|
---|
45 |
|
---|
46 | void ViewCell::DeriveViewCells(const ObjectContainer &objects,
|
---|
47 | ViewCellContainer &viewCells,
|
---|
48 | const int maxViewCells)
|
---|
49 | {
|
---|
50 | // maximal max viewcells
|
---|
51 | int limit = maxViewCells > 0 ?
|
---|
52 | Min((int)objects.size(), maxViewCells) : (int)objects.size();
|
---|
53 |
|
---|
54 | for (int i = 0; i < limit; ++ i)
|
---|
55 | {
|
---|
56 | Intersectable *object = objects[i];
|
---|
57 |
|
---|
58 | // extract the mesh instances
|
---|
59 | if (object->Type() == Intersectable::MESH_INSTANCE)
|
---|
60 | {
|
---|
61 | MeshInstance *inst = dynamic_cast<MeshInstance *>(object);
|
---|
62 |
|
---|
63 | ViewCell *viewCell = Generate(inst->GetMesh());
|
---|
64 | viewCells.push_back(viewCell);
|
---|
65 | }
|
---|
66 | //TODO: transformed meshes
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | ViewCell *ViewCell::ExtrudeViewCell(const Triangle3 &baseTri, const float height)
|
---|
71 | {
|
---|
72 | // one mesh per view cell
|
---|
73 | Mesh *mesh = new Mesh();
|
---|
74 |
|
---|
75 | //-- construct prism
|
---|
76 |
|
---|
77 | // bottom
|
---|
78 | mesh->mFaces.push_back(new Face(2,1,0));
|
---|
79 | // top
|
---|
80 | mesh->mFaces.push_back(new Face(3,4,5));
|
---|
81 | // sides
|
---|
82 | mesh->mFaces.push_back(new Face(1, 4, 3, 0));
|
---|
83 | mesh->mFaces.push_back(new Face(2, 5, 4, 1));
|
---|
84 | mesh->mFaces.push_back(new Face(3, 5, 2, 0));
|
---|
85 |
|
---|
86 | //--- extrude new vertices for top of prism
|
---|
87 | Vector3 triNorm = baseTri.GetNormal();
|
---|
88 |
|
---|
89 | Triangle3 topTri;
|
---|
90 |
|
---|
91 | // add base vertices and calculate top vertices
|
---|
92 | for (int i = 0; i < 3; ++ i)
|
---|
93 | mesh->mVertices.push_back(baseTri.mVertices[i]);
|
---|
94 |
|
---|
95 | // add top vertices
|
---|
96 | for (int i = 0; i < 3; ++ i)
|
---|
97 | mesh->mVertices.push_back(baseTri.mVertices[i] + height * triNorm);
|
---|
98 |
|
---|
99 | mesh->Preprocess();
|
---|
100 |
|
---|
101 | return Generate(mesh);
|
---|
102 | }
|
---|
103 |
|
---|
104 | ViewCell *ViewCell::Merge(ViewCell &front, ViewCell &back)
|
---|
105 | {
|
---|
106 | ViewCell *vc = Generate();
|
---|
107 | // merge pvs
|
---|
108 | vc->mPvs.Merge(front.mPvs, back.mPvs);
|
---|
109 |
|
---|
110 | // merge ray sets
|
---|
111 | stable_sort(front.mPiercingRays.begin(), front.mPiercingRays.end());
|
---|
112 | stable_sort(back.mPiercingRays.begin(), back.mPiercingRays.end());
|
---|
113 |
|
---|
114 | std::merge(front.mPiercingRays.begin(), front.mPiercingRays.end(),
|
---|
115 | back.mPiercingRays.begin(), back.mPiercingRays.end(),
|
---|
116 | vc->mPiercingRays.begin());
|
---|
117 |
|
---|
118 | return vc;
|
---|
119 | }
|
---|
120 |
|
---|
121 | void ViewCell::AddPassingRay(const Ray &ray, const int contributions)
|
---|
122 | {
|
---|
123 | mPassingRays.AddRay(ray, contributions);
|
---|
124 | } |
---|