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