[235] | 1 | #include "ViewCell.h"
|
---|
| 2 | #include "Mesh.h"
|
---|
[308] | 3 | #include "Intersectable.h"
|
---|
[235] | 4 | #include "MeshKdTree.h"
|
---|
| 5 | #include "Triangle3.h"
|
---|
| 6 |
|
---|
[366] | 7 | ViewCell::HierarchyType ViewCell::sHierarchy = BSP;
|
---|
| 8 |
|
---|
[352] | 9 | ViewCell::ViewCell(): MeshInstance(NULL), mPiercingRays(0)
|
---|
[265] | 10 | {
|
---|
| 11 | }
|
---|
[235] | 12 |
|
---|
[372] | 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:
|
---|
[430] | 26 | Debug << "should not come here 3" << endl;
|
---|
[372] | 27 | return NULL;
|
---|
| 28 | }
|
---|
| 29 | }
|
---|
| 30 |
|
---|
[419] | 31 | const ViewCellPvs &ViewCell::GetPvs() const
|
---|
| 32 | {
|
---|
| 33 | return mPvs;
|
---|
| 34 | }
|
---|
| 35 |
|
---|
[372] | 36 | ViewCellPvs &ViewCell::GetPvs()
|
---|
| 37 | {
|
---|
| 38 | return mPvs;
|
---|
| 39 | }
|
---|
| 40 |
|
---|
[235] | 41 | int ViewCell::Type() const
|
---|
| 42 | {
|
---|
[308] | 43 | return VIEW_CELL;
|
---|
[235] | 44 | }
|
---|
| 45 |
|
---|
| 46 | void ViewCell::DeriveViewCells(const ObjectContainer &objects,
|
---|
| 47 | ViewCellContainer &viewCells,
|
---|
[239] | 48 | const int maxViewCells)
|
---|
[235] | 49 | {
|
---|
[237] | 50 | // maximal max viewcells
|
---|
[366] | 51 | int limit = maxViewCells > 0 ?
|
---|
| 52 | Min((int)objects.size(), maxViewCells) : (int)objects.size();
|
---|
[237] | 53 |
|
---|
[338] | 54 | for (int i = 0; i < limit; ++ i)
|
---|
[235] | 55 | {
|
---|
| 56 | Intersectable *object = objects[i];
|
---|
[256] | 57 |
|
---|
[235] | 58 | // extract the mesh instances
|
---|
| 59 | if (object->Type() == Intersectable::MESH_INSTANCE)
|
---|
| 60 | {
|
---|
| 61 | MeshInstance *inst = dynamic_cast<MeshInstance *>(object);
|
---|
| 62 |
|
---|
[366] | 63 | ViewCell *viewCell = Generate(inst->GetMesh());
|
---|
[235] | 64 | viewCells.push_back(viewCell);
|
---|
| 65 | }
|
---|
[237] | 66 | //TODO: transformed meshes
|
---|
[235] | 67 | }
|
---|
[372] | 68 | }
|
---|
| 69 |
|
---|
| 70 | ViewCell *ViewCell::ExtrudeViewCell(const Triangle3 &baseTri, const float height)
|
---|
[260] | 71 | {
|
---|
[384] | 72 | // one mesh per view cell
|
---|
[260] | 73 | Mesh *mesh = new Mesh();
|
---|
[261] | 74 |
|
---|
| 75 | //-- construct prism
|
---|
[260] | 76 |
|
---|
[261] | 77 | // bottom
|
---|
[264] | 78 | mesh->mFaces.push_back(new Face(2,1,0));
|
---|
[261] | 79 | // top
|
---|
[264] | 80 | mesh->mFaces.push_back(new Face(3,4,5));
|
---|
[261] | 81 | // sides
|
---|
[264] | 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));
|
---|
[261] | 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
|
---|
[384] | 92 | for (int i = 0; i < 3; ++ i)
|
---|
| 93 | mesh->mVertices.push_back(baseTri.mVertices[i]);
|
---|
[312] | 94 |
|
---|
[261] | 95 | // add top vertices
|
---|
[384] | 96 | for (int i = 0; i < 3; ++ i)
|
---|
[312] | 97 | mesh->mVertices.push_back(baseTri.mVertices[i] + height * triNorm);
|
---|
[261] | 98 |
|
---|
[263] | 99 | mesh->Preprocess();
|
---|
| 100 |
|
---|
[372] | 101 | return Generate(mesh);
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | ViewCell *ViewCell::Merge(ViewCell &front, ViewCell &back)
|
---|
| 105 | {
|
---|
[366] | 106 | ViewCell *vc = Generate();
|
---|
[362] | 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());
|
---|
[352] | 112 | stable_sort(back.mPiercingRays.begin(), back.mPiercingRays.end());
|
---|
| 113 |
|
---|
[362] | 114 | std::merge(front.mPiercingRays.begin(), front.mPiercingRays.end(),
|
---|
| 115 | back.mPiercingRays.begin(), back.mPiercingRays.end(),
|
---|
| 116 | vc->mPiercingRays.begin());
|
---|
[352] | 117 |
|
---|
[372] | 118 | return vc;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | void ViewCell::AddPassingRay(const Ray &ray, const int contributions)
|
---|
| 122 | {
|
---|
| 123 | mPassingRays.AddRay(ray, contributions);
|
---|
[366] | 124 | } |
---|