1 | #ifndef _Mesh_H__ |
---|
2 | #define _Mesh_H__ |
---|
3 | |
---|
4 | #include <vector> |
---|
5 | using namespace std; |
---|
6 | #include "Intersectable.h" |
---|
7 | #include "Plane3.h" |
---|
8 | #include "Matrix4x4.h" |
---|
9 | #include "AxisAlignedBox3.h" |
---|
10 | #include "Material.h" |
---|
11 | |
---|
12 | |
---|
13 | class MeshInstance; |
---|
14 | class MeshKdTree; |
---|
15 | |
---|
16 | /// default vertex container for Mesh |
---|
17 | typedef std::vector<Vector3> VertexContainer; |
---|
18 | |
---|
19 | /// vertex index container |
---|
20 | typedef std::vector<int> VertexIndexContainer; |
---|
21 | |
---|
22 | |
---|
23 | /** Patch used as an element of the mesh */ |
---|
24 | class Face { |
---|
25 | public: |
---|
26 | Face():mVertexIndices() {} |
---|
27 | Face(const int a, const int b, const int c):mVertexIndices(3) { |
---|
28 | mVertexIndices[0] = a; |
---|
29 | mVertexIndices[1] = b; |
---|
30 | mVertexIndices[2] = c; |
---|
31 | } |
---|
32 | |
---|
33 | Face(const int a, const int b, const int c, const int d):mVertexIndices(4) { |
---|
34 | mVertexIndices[0] = a; |
---|
35 | mVertexIndices[1] = b; |
---|
36 | mVertexIndices[2] = c; |
---|
37 | mVertexIndices[3] = d; |
---|
38 | } |
---|
39 | |
---|
40 | Face(const VertexIndexContainer &vertices):mVertexIndices(vertices) {} |
---|
41 | |
---|
42 | /// list of vertex pointers |
---|
43 | VertexIndexContainer mVertexIndices; |
---|
44 | }; |
---|
45 | |
---|
46 | /// default patch container for Mesh |
---|
47 | typedef std::vector<Face *> FaceContainer; |
---|
48 | |
---|
49 | /** Mesh containing polygonal patches */ |
---|
50 | class Mesh { |
---|
51 | |
---|
52 | |
---|
53 | public: |
---|
54 | |
---|
55 | /// Default constructor |
---|
56 | Mesh():mVertices(), mFaces(), mMaterial(NULL), mKdTree(NULL) {} |
---|
57 | |
---|
58 | /// Constructor with container preallocation |
---|
59 | Mesh(const int vertices, |
---|
60 | const int faces):mVertices(), |
---|
61 | mFaces(), |
---|
62 | mMaterial(NULL) |
---|
63 | { |
---|
64 | mVertices.reserve(vertices); |
---|
65 | mFaces.reserve(faces); |
---|
66 | } |
---|
67 | |
---|
68 | ~Mesh() { |
---|
69 | for (int i=0; i < mFaces.size(); i++) |
---|
70 | delete mFaces[i]; |
---|
71 | } |
---|
72 | |
---|
73 | void AddFace(Face *face) |
---|
74 | { |
---|
75 | mFaces.push_back(face); |
---|
76 | } |
---|
77 | |
---|
78 | void Preprocess(); |
---|
79 | |
---|
80 | /** Axis aligned bounding box of the mesh */ |
---|
81 | AxisAlignedBox3 mBoundingBox; |
---|
82 | |
---|
83 | /** Vertices forming the mesh */ |
---|
84 | VertexContainer mVertices; |
---|
85 | |
---|
86 | /** Patches forming the mesh */ |
---|
87 | FaceContainer mFaces; |
---|
88 | |
---|
89 | /** Global mesh material */ |
---|
90 | Material *mMaterial; |
---|
91 | |
---|
92 | /** true if the mesh is a convex mesh */ |
---|
93 | bool mIsConvex; |
---|
94 | |
---|
95 | MeshKdTree *mKdTree; |
---|
96 | |
---|
97 | int |
---|
98 | CastRay( |
---|
99 | Ray &ray, |
---|
100 | MeshInstance *instance |
---|
101 | ); |
---|
102 | |
---|
103 | int |
---|
104 | CastRayToSelectedFaces( |
---|
105 | Ray &ray, |
---|
106 | const vector<int> &faces, |
---|
107 | MeshInstance *instance |
---|
108 | ); |
---|
109 | |
---|
110 | int |
---|
111 | CastRayToFace( |
---|
112 | const int faceIndex, |
---|
113 | Ray &ray, |
---|
114 | float &nearestT, |
---|
115 | int &nearestFace, |
---|
116 | MeshInstance *instance |
---|
117 | ); |
---|
118 | |
---|
119 | |
---|
120 | int |
---|
121 | RayFaceIntersection(const int faceIndex, |
---|
122 | const Ray &ray, |
---|
123 | float &t, |
---|
124 | const float nearestT |
---|
125 | ); |
---|
126 | |
---|
127 | Plane3 GetFacePlane(const int faceIndex); |
---|
128 | |
---|
129 | AxisAlignedBox3 GetFaceBox(const int faceIndex); |
---|
130 | |
---|
131 | }; |
---|
132 | |
---|
133 | class MeshInstance : public Intersectable { |
---|
134 | protected: |
---|
135 | static int mailID; |
---|
136 | int mailbox; |
---|
137 | float mVisibility; |
---|
138 | Mesh *mMesh; |
---|
139 | |
---|
140 | public: |
---|
141 | MeshInstance(Mesh *mesh):mMesh(mesh), mVisibility(1.0f), mailbox(0) |
---|
142 | { |
---|
143 | } |
---|
144 | |
---|
145 | void Mail() { mailbox = mailID; } |
---|
146 | static void NewMail() { mailID++; } |
---|
147 | bool Mailed() const { return mailbox == mailID; } |
---|
148 | |
---|
149 | |
---|
150 | Mesh *GetMesh() { return mMesh; } |
---|
151 | |
---|
152 | virtual AxisAlignedBox3 GetBox() { |
---|
153 | return mMesh->mBoundingBox; |
---|
154 | } |
---|
155 | |
---|
156 | virtual int |
---|
157 | CastRay( |
---|
158 | Ray &ray |
---|
159 | ); |
---|
160 | |
---|
161 | virtual int |
---|
162 | CastRay( |
---|
163 | Ray &ray, |
---|
164 | const vector<int> &faces |
---|
165 | ); |
---|
166 | |
---|
167 | |
---|
168 | }; |
---|
169 | |
---|
170 | |
---|
171 | class MeshTransformedInstance : public MeshInstance |
---|
172 | { |
---|
173 | public: |
---|
174 | MeshTransformedInstance(Mesh *mesh):MeshInstance(mesh) |
---|
175 | { |
---|
176 | mWorldTransform = IdentityMatrix(); |
---|
177 | } |
---|
178 | |
---|
179 | virtual AxisAlignedBox3 GetBox() { |
---|
180 | return Transform(mMesh->mBoundingBox, |
---|
181 | mWorldTransform); |
---|
182 | } |
---|
183 | |
---|
184 | virtual int |
---|
185 | CastRay( |
---|
186 | Ray &ray |
---|
187 | ); |
---|
188 | |
---|
189 | private: |
---|
190 | Matrix4x4 mWorldTransform; |
---|
191 | |
---|
192 | }; |
---|
193 | |
---|
194 | |
---|
195 | |
---|
196 | #endif |
---|