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 | |
---|
15 | /// default vertex container for Mesh |
---|
16 | typedef std::vector<Vector3> VertexContainer; |
---|
17 | |
---|
18 | /// vertex index container |
---|
19 | typedef std::vector<int> VertexIndexContainer; |
---|
20 | |
---|
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) {} |
---|
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 | int |
---|
96 | CastRay( |
---|
97 | Ray &ray, |
---|
98 | MeshInstance *instance |
---|
99 | ); |
---|
100 | |
---|
101 | |
---|
102 | int |
---|
103 | RayFaceIntersection(const int faceIndex, |
---|
104 | const Ray &ray, |
---|
105 | float &t, |
---|
106 | const float nearestT |
---|
107 | ); |
---|
108 | |
---|
109 | Plane3 GetFacePlane(const int faceIndex); |
---|
110 | |
---|
111 | }; |
---|
112 | |
---|
113 | class MeshInstance : public Intersectable { |
---|
114 | protected: |
---|
115 | static int mailID; |
---|
116 | int mailbox; |
---|
117 | float mVisibility; |
---|
118 | Mesh *mMesh; |
---|
119 | |
---|
120 | public: |
---|
121 | MeshInstance(Mesh *mesh):mMesh(mesh), mVisibility(1.0f), mailbox(0) |
---|
122 | { |
---|
123 | } |
---|
124 | |
---|
125 | void Mail() { mailbox = mailID; } |
---|
126 | static void NewMail() { mailID++; } |
---|
127 | bool Mailed() const { return mailbox == mailID; } |
---|
128 | |
---|
129 | |
---|
130 | Mesh *GetMesh() { return mMesh; } |
---|
131 | |
---|
132 | virtual AxisAlignedBox3 GetBox() { |
---|
133 | return mMesh->mBoundingBox; |
---|
134 | } |
---|
135 | |
---|
136 | virtual int |
---|
137 | CastRay( |
---|
138 | Ray &ray |
---|
139 | ); |
---|
140 | |
---|
141 | |
---|
142 | }; |
---|
143 | |
---|
144 | |
---|
145 | class MeshTransformedInstance : public MeshInstance |
---|
146 | { |
---|
147 | public: |
---|
148 | MeshTransformedInstance(Mesh *mesh):MeshInstance(mesh) |
---|
149 | { |
---|
150 | mWorldTransform = IdentityMatrix(); |
---|
151 | } |
---|
152 | |
---|
153 | virtual AxisAlignedBox3 GetBox() { |
---|
154 | return Transform(mMesh->mBoundingBox, |
---|
155 | mWorldTransform); |
---|
156 | } |
---|
157 | |
---|
158 | virtual int |
---|
159 | CastRay( |
---|
160 | Ray &ray |
---|
161 | ); |
---|
162 | |
---|
163 | private: |
---|
164 | Matrix4x4 mWorldTransform; |
---|
165 | |
---|
166 | }; |
---|
167 | |
---|
168 | |
---|
169 | |
---|
170 | #endif |
---|