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 | |
---|
11 | ViewCell::~ViewCell() |
---|
12 | { |
---|
13 | // NOTE: should I really do this here? |
---|
14 | DEL_PTR(mMesh); |
---|
15 | } |
---|
16 | |
---|
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 | { |
---|
49 | return (float)mMesh->mFaces.size(); |
---|
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,
|
---|
64 | const int maxViewCells)
|
---|
65 | {
|
---|
66 | // maximal max viewcells
|
---|
67 | int limit = maxViewCells > 0 ? Min((int)objects.size(), maxViewCells) : (int)objects.size();
|
---|
68 |
|
---|
69 | for (int i = 0; i < limit; ++i)
|
---|
70 | {
|
---|
71 | Intersectable *object = objects[i];
|
---|
72 |
|
---|
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 | }
|
---|
81 | //TODO: transformed meshes
|
---|
82 | }
|
---|
83 | } |
---|
84 | |
---|
85 | ViewCell *ViewCell::ExtrudeViewCell(const Triangle3 &baseTri, const float height) |
---|
86 | {
|
---|
87 | // one mesh per view cell
|
---|
88 | Mesh *mesh = new Mesh();
|
---|
89 |
|
---|
90 | //-- construct prism
|
---|
91 |
|
---|
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)
|
---|
116 | mesh->mVertices.push_back(topTri.mVertices[1]);
|
---|
117 |
|
---|
118 | return new ViewCell(mesh); |
---|
119 | } |
---|