[235] | 1 | #include "ViewCell.h"
|
---|
| 2 | #include "Mesh.h"
|
---|
| 3 | #include "MeshKdTree.h"
|
---|
| 4 | #include "Triangle3.h"
|
---|
| 5 |
|
---|
| 6 |
|
---|
| 7 | ViewCell::ViewCell(Mesh *mesh): mMesh(mesh), mPvs(NULL) |
---|
| 8 | { |
---|
| 9 | } |
---|
| 10 | |
---|
[261] | 11 | ViewCell::~ViewCell() |
---|
| 12 | { |
---|
[262] | 13 | // NOTE: should I really do this here? (I know that there is only one mesh per view cell) |
---|
[261] | 14 | DEL_PTR(mMesh); |
---|
| 15 | } |
---|
| 16 | |
---|
[235] | 17 | Mesh *ViewCell::GetMesh() |
---|
| 18 | { |
---|
| 19 | return mMesh; |
---|
| 20 | } |
---|
| 21 | |
---|
| 22 | BspPvs *ViewCell::GetPVS() |
---|
| 23 | { |
---|
| 24 | return mPvs; |
---|
| 25 | } |
---|
| 26 | |
---|
| 27 | AxisAlignedBox3 ViewCell::GetBox()
|
---|
| 28 | {
|
---|
| 29 | return mMesh->mBox;
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | int ViewCell::CastRay(Ray &ray)
|
---|
| 33 | {
|
---|
| 34 | return 0;
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | bool ViewCell::IsConvex() |
---|
| 38 | { |
---|
| 39 | return mMesh->mIsConvex; |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | bool ViewCell::IsWatertight() |
---|
| 43 | { |
---|
| 44 | return mMesh->mIsWatertight; |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | float ViewCell::IntersectionComplexity() |
---|
| 48 | { |
---|
[239] | 49 | return (float)mMesh->mFaces.size(); |
---|
[235] | 50 | } |
---|
| 51 | |
---|
| 52 | int ViewCell::Type() const
|
---|
| 53 | {
|
---|
| 54 | return VIEWCELL;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | void ViewCell::GetRandomSurfacePoint(Vector3 &point, Vector3 &normal)
|
---|
| 58 | {
|
---|
| 59 | point = Vector3(0,0,0);
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | void ViewCell::DeriveViewCells(const ObjectContainer &objects,
|
---|
| 63 | ViewCellContainer &viewCells,
|
---|
[239] | 64 | const int maxViewCells)
|
---|
[235] | 65 | {
|
---|
[237] | 66 | // maximal max viewcells
|
---|
[245] | 67 | int limit = maxViewCells > 0 ? Min((int)objects.size(), maxViewCells) : (int)objects.size();
|
---|
[237] | 68 |
|
---|
[239] | 69 | for (int i = 0; i < limit; ++i)
|
---|
[235] | 70 | {
|
---|
| 71 | Intersectable *object = objects[i];
|
---|
[256] | 72 |
|
---|
[235] | 73 | // extract the mesh instances
|
---|
| 74 | if (object->Type() == Intersectable::MESH_INSTANCE)
|
---|
| 75 | {
|
---|
| 76 | MeshInstance *inst = dynamic_cast<MeshInstance *>(object);
|
---|
| 77 |
|
---|
| 78 | ViewCell *viewCell = new ViewCell(inst->GetMesh());
|
---|
| 79 | viewCells.push_back(viewCell);
|
---|
| 80 | }
|
---|
[237] | 81 | //TODO: transformed meshes
|
---|
[235] | 82 | }
|
---|
[245] | 83 | } |
---|
[260] | 84 | |
---|
[261] | 85 | ViewCell *ViewCell::ExtrudeViewCell(const Triangle3 &baseTri, const float height) |
---|
[260] | 86 | {
|
---|
[261] | 87 | // one mesh per view cell
|
---|
[260] | 88 | Mesh *mesh = new Mesh();
|
---|
[261] | 89 |
|
---|
| 90 | //-- construct prism
|
---|
[260] | 91 |
|
---|
[261] | 92 | // bottom
|
---|
| 93 | mesh->mFaces.push_back(new Face(0,1,2));
|
---|
| 94 | // top
|
---|
| 95 | mesh->mFaces.push_back(new Face(5,4,3));
|
---|
| 96 | // sides
|
---|
| 97 | mesh->mFaces.push_back(new Face(0, 3, 4, 1));
|
---|
| 98 | mesh->mFaces.push_back(new Face(1, 4, 5, 2));
|
---|
| 99 | mesh->mFaces.push_back(new Face(0, 2, 5, 3));
|
---|
| 100 |
|
---|
| 101 |
|
---|
| 102 | //--- extrude new vertices for top of prism
|
---|
| 103 | Vector3 triNorm = baseTri.GetNormal();
|
---|
| 104 |
|
---|
| 105 | Triangle3 topTri;
|
---|
| 106 |
|
---|
| 107 | // add base vertices and calculate top vertices
|
---|
| 108 | for (int i = 0; i < 3; ++i)
|
---|
| 109 | {
|
---|
| 110 | mesh->mVertices.push_back(baseTri.mVertices[i]);
|
---|
| 111 | topTri.mVertices[i] = baseTri.mVertices[i] + height * triNorm;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | // add top vertices
|
---|
| 115 | for (int i = 0; i < 3; ++i)
|
---|
[262] | 116 | mesh->mVertices.push_back(topTri.mVertices[i]);
|
---|
[261] | 117 |
|
---|
[263] | 118 | mesh->Preprocess();
|
---|
| 119 |
|
---|
[260] | 120 | return new ViewCell(mesh); |
---|
| 121 | } |
---|