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