Mesh.h
Go to the documentation of this file.00001 /**<!--------------------------------------------------------------------> 00002 @class Mesh 00003 @author Travis Fischer (fisch0920@gmail.com) 00004 @author Nong Li (nongli@gmail.com) 00005 @date Spring 2008 00006 00007 @brief 00008 Representation of a triangular mesh stored in the obj format. 00009 It consists of a list of vertices's and a list of faces that contain 00010 indices in the vertex list. 00011 <!-------------------------------------------------------------------->**/ 00012 00013 #ifndef MESH_H_ 00014 #define MESH_H_ 00015 00016 #include <shapes/Transformable.h> 00017 #include <shapes/MeshTriangle.h> 00018 #include <utils/PropertyMap.h> 00019 00020 #include <GL/gl.h> 00021 00022 /// Temporary struct for loading mesh data 00023 struct MeshData { 00024 std::vector<Vertex> vertices; 00025 std::vector<Normal> normals; 00026 std::vector<UV> uvs; 00027 std::vector<MeshTriangle> triangles; 00028 00029 std::string fileName; 00030 }; 00031 00032 class SpatialAccel; 00033 00034 class Mesh : public Transformable, public PropertyMap { 00035 public: 00036 ///@name Constructors 00037 //@{----------------------------------------------------------------- 00038 00039 /// Constructs a mesh out of the specified MeshData 00040 Mesh(const MeshData &data); 00041 00042 /// Constructs a mesh with the specified vertex/triangle count 00043 Mesh(unsigned nVertices, unsigned nNormals, unsigned nUVs, 00044 unsigned nTriangles); 00045 00046 /// Copy constructor 00047 Mesh(const Mesh &mesh); 00048 00049 /// Cleans up vertex and triangle data 00050 virtual ~Mesh(); 00051 00052 00053 //@}----------------------------------------------------------------- 00054 ///@name Core functionality 00055 //@{----------------------------------------------------------------- 00056 00057 /** 00058 * @brief 00059 * Initializes this Mesh and its underlying SpatialAccel 00060 * 00061 * @param forceUpdate if true, reinitializes this mesh, including 00062 * recreating the underlying SpatialAccel and dirtying this mesh's 00063 * OpenGL preview display list 00064 */ 00065 virtual void init(bool forceUpdate); 00066 00067 /** 00068 * @brief 00069 * Initializes this Mesh and its underlying SpatialAccel 00070 */ 00071 virtual void init(); 00072 00073 /** 00074 * @brief 00075 * Displays an OpenGL preview of this mesh; optimized for repeated 00076 * calls via a compiled OpenGL display list 00077 */ 00078 virtual void preview(); 00079 00080 virtual real_t getIntersection(const Ray &ray, SurfacePoint &pt); 00081 00082 virtual bool intersects(const Ray &ray, real_t tMax = INFINITY); 00083 00084 /** 00085 * Computes the normals. The vertex positions should be set. The normals 00086 * will be averaged (Gourand Shading) 00087 */ 00088 void computeNormals(); 00089 00090 00091 //@}----------------------------------------------------------------- 00092 ///@name Sampling functionality 00093 //@{----------------------------------------------------------------- 00094 00095 /** 00096 * @returns the point on the surface of this shape corresponding to the 00097 * given UV coordinates in 'pt' 00098 */ 00099 virtual void getPoint(SurfacePoint &pt, const UV &uv); 00100 00101 /** 00102 * @returns the point on the surface of this shape corresponding to the 00103 * given UV coordinates 00104 */ 00105 virtual Point3 getPosition(const UV &uv); 00106 00107 00108 //@}----------------------------------------------------------------- 00109 ///@name Accessors / Mutators 00110 //@{----------------------------------------------------------------- 00111 00112 /// @returns the number of vertices 00113 inline unsigned getNoVertices() const { 00114 return m_nVertices; 00115 } 00116 00117 /// @returns the number of normals 00118 inline unsigned getNoNormals() const { 00119 return m_nNormals; 00120 } 00121 00122 /// @returns the number of uvs 00123 inline unsigned getNoUVs() const { 00124 return m_nUVs; 00125 } 00126 00127 /// @returns the number of triangles 00128 inline unsigned getNoTriangles() const { 00129 return m_nTriangles; 00130 } 00131 00132 /// @returns a pointer to the vertex data and the number of vertices 00133 inline Vertex *getVertices(unsigned &nVertices) const { 00134 nVertices = m_nVertices; 00135 00136 return m_vertices; 00137 } 00138 00139 /// @returns a pointer to the normal data and the number of normals 00140 inline Normal *getNormals(unsigned &nNormals) const { 00141 nNormals = m_nNormals; 00142 00143 return m_normals; 00144 } 00145 00146 /// @returns a pointer to the uv data and the number of uvs 00147 inline UV *getUVs(unsigned &nUVs) const { 00148 nUVs = m_nUVs; 00149 00150 return m_uvs; 00151 } 00152 00153 /// @returns a pointer to the triangle data and the number of triangles 00154 inline MeshTriangle *getTriangles(unsigned &nTriangles) const { 00155 nTriangles = m_nTriangles; 00156 00157 return m_triangles; 00158 } 00159 00160 /// @returns a pointer to the vertex data 00161 inline Vertex *getVertices() const { 00162 return m_vertices; 00163 } 00164 00165 /// @returns a pointer to the normal data 00166 inline Normal *getNormals() const { 00167 return m_normals; 00168 } 00169 00170 /// @returns a pointer to the uv data 00171 inline UV *getUVs() const { 00172 return m_uvs; 00173 } 00174 00175 /// @returns a pointer to the triangle data 00176 inline MeshTriangle *getTriangles() const { 00177 return m_triangles; 00178 } 00179 00180 void setPreviewDirty(); 00181 00182 00183 //@}----------------------------------------------------------------- 00184 00185 protected: 00186 virtual void _getUV(SurfacePoint &pt) const; 00187 virtual void _getGeometricNormal(SurfacePoint &pt) const; 00188 00189 /** 00190 * @returns the aggregate surface area of all triangles in this mesh 00191 */ 00192 virtual real_t _getSurfaceArea(); 00193 00194 protected: 00195 unsigned m_nVertices; 00196 unsigned m_nNormals; 00197 unsigned m_nUVs; 00198 unsigned m_nTriangles; 00199 00200 Vertex *m_vertices; 00201 Normal *m_normals; 00202 UV *m_uvs; 00203 MeshTriangle *m_triangles; 00204 00205 // display list 00206 GLuint m_batch; 00207 00208 SpatialAccel *m_spatialAccel; 00209 00210 bool m_enableAccelPreview; 00211 }; 00212 00213 #endif // MESH_H_ 00214
Generated on 28 Feb 2009 for Milton by
1.5.6