MinGfx Toolkit  1.0
A minimal library for writing cross-platform (Windows, OSX, linux) graphics programs.
mesh.h
Go to the documentation of this file.
1 /*
2  This file is part of the MinGfx Project.
3 
4  Copyright (c) 2017,2018 Regents of the University of Minnesota.
5  All Rights Reserved.
6 
7  Original Author(s) of this File:
8  Dan Keefe, 2018, University of Minnesota
9 
10  Author(s) of Significant Updates/Modifications to the File:
11  ...
12  */
13 
14 #ifndef SRC_MESH_H_
15 #define SRC_MESH_H_
16 
17 
18 
19 #include "bvh.h"
20 #include "color.h"
21 #include "opengl_headers.h"
22 #include "point2.h"
23 #include "point3.h"
24 #include "vector3.h"
25 
26 #include <vector>
27 
28 
29 namespace mingfx {
30 
31 class Matrix4;
32 
127 class Mesh {
128 public:
130  Mesh();
131 
133  Mesh(const Mesh &other);
134 
135  virtual ~Mesh();
136 
137 
142  void LoadFromOBJ(const std::string &filename);
143 
144 
145 
146  // ---- TRIANGLE LIST MODE ----
147  // No indices are stored, each set of 3 vertices forms a triangle, and if the
148  // triangles share vertices, those vertices need to be repeated.
149 
155  int AddTriangle(Point3 v1, Point3 v2, Point3 v3);
156 
159  void UpdateTriangle(int triangle_id, Point3 v1, Point3 v2, Point3 v3);
160 
163  void SetNormals(int triangle_id, Vector3 n1, Vector3 n2, Vector3 n3);
164 
167  void SetColors(int triangle_id, Color c1, Color c2, Color c3);
168 
172  void SetTexCoords(int triangle_id, int texture_unit, Point2 uv1, Point2 uv2, Point2 uv3);
173 
174 
175 
176  // ---- INDEXED TRIANGLES MODE ----
177  // Vertices are stored in an array and indices are stored in a separate array
178  // each set of 3 indices into the vertex array defines one triangle. Here,
179  // you cannot add one triangle at a time to the mesh. Instead you must set
180  // the arrays of indices, vertices, and other attributes for the mesh at
181  // once.
182 
184  void SetVertices(const std::vector<Point3> &verts);
185 
187  void SetNormals(const std::vector<Vector3> &norms);
188 
190  void SetColors(const std::vector<Color> &colors);
191 
193  void SetTexCoords(int texture_unit, const std::vector<Point2> &tex_coords);
194 
198  void SetIndices(const std::vector<unsigned int> index_array);
199 
200 
201  void SetInstanceTransforms(const std::vector<Matrix4> &xforms);
202 
203 
204  // ---- These functions can be used instead of the above if you are working with
205  // regular C-style arrays and floats rather than the higher level types like
206  // Point3 and Vector3. ----
207 
211  void SetVertices(float *verts_array, int num_verts);
212 
217  void SetNormals(float *norms_array, int num_norms);
218 
223  void SetColors(float *colors_array, int num_colors);
224 
229  void SetTexCoords(int texture_unit, float *tex_coords_array, int num_tex_coords);
230 
235  void SetIndices(unsigned int *index_array, int num_indices);
236 
237 
238 
247 
252  void Draw();
253 
254 
255 
263 
271 
272 
276  void BuildBVH();
277 
283 
284  // Access to properties indexed by vertex number
285 
287  int num_vertices() const;
288 
291  Point3 read_vertex_data(int vertex_id) const;
292 
295  Vector3 read_normal_data(int vertex_id) const;
296 
299  Color read_color_data(int vertex_id) const;
300 
303  Point2 read_tex_coords_data(int texture_unit, int vertex_id) const;
304 
305 
306  // Access to triangles
307 
309  int num_triangles() const;
310 
312  // of unsigned ints. Use the SetIndices() function to set (or edit) the indices for the mesh.
313  std::vector<unsigned int> read_triangle_indices_data(int triangle_id) const;
314 
315 
316 private:
317  std::vector<float> verts_;
318  std::vector<float> norms_;
319  std::vector<float> colors_;
320  std::vector< std::vector<float> > tex_coords_;
321  std::vector<unsigned int> indices_;
322  std::vector<float> instance_xforms_;
323 
324  bool gpu_dirty_;
325  GLuint vertex_buffer_;
326  GLuint vertex_array_;
327  GLuint element_buffer_;
328 
329  bool bvh_dirty_;
330  BVH bvh_;
331 };
332 
333 
334 } // end namespace
335 
336 
337 #endif
A Bounding Volume Hierarchy (BVH) data structure that can be used to accelerate ray-object intersecti...
Definition: bvh.h:40
Represents a 4-component (R,G,B,A) color, stored internally in a float array to be compatable with Op...
Definition: color.h:41
A triangle mesh data structure that can be rendered with a ShaderProgram like DefaultShader.
Definition: mesh.h:127
void SetVertices(float *verts_array, int num_verts)
Sets the vertex array for the mesh directly. Vertices are stored as (x,y,z), (x,y,...
void SetColors(int triangle_id, Color c1, Color c2, Color c3)
Sets per-vertex colors for the three vertices of a triangle that has already been added to the mesh.
std::vector< unsigned int > read_triangle_indices_data(int triangle_id) const
Read only access to the indices that make up a particular triangle. Data are returned as a 3-element ...
void SetTexCoords(int triangle_id, int texture_unit, Point2 uv1, Point2 uv2, Point2 uv3)
Sets the texture coordinates for the three vertices of a triangle that has already been added to the ...
void CalcPerFaceNormals()
This (re)calculates the normals for the mesh and stores them with the mesh data structure.
void SetVertices(const std::vector< Point3 > &verts)
Sets the vertex array for the mesh directly.
void SetIndices(const std::vector< unsigned int > index_array)
Sets the indices into the vertex array to use to create the triangles. Each consecutive set of 3 indi...
int AddTriangle(Point3 v1, Point3 v2, Point3 v3)
Adds a triangle to the mesh datastructure and returns a triangle ID.
void UpdateTriangle(int triangle_id, Point3 v1, Point3 v2, Point3 v3)
Updates the vertex positions for a triangle that has already been added to the mesh.
Vector3 read_normal_data(int vertex_id) const
Read only access to per-vertex normal data. Data are returned as a Vector3. Indexed by vertex number....
void SetNormals(int triangle_id, Vector3 n1, Vector3 n2, Vector3 n3)
Sets the normals for the three vertices of a triangle that has already been added to the mesh.
Point2 read_tex_coords_data(int texture_unit, int vertex_id) const
Read only access to per-vertex texture coordinates data. Data are returned as a Point2....
Mesh()
Creates an empty mesh.
void SetNormals(float *norms_array, int num_norms)
Sets the normal array for the mesh directly. Normals are stored as (x,y,z), (x,y,z),...
void SetInstanceTransforms(const std::vector< Matrix4 > &xforms)
void Draw()
This sends the mesh vertices and attributes down the graphics pipe using glDrawArrays() for the non-i...
Mesh(const Mesh &other)
Copies all data and sets GPU dirty bit for the new mesh.
void SetIndices(unsigned int *index_array, int num_indices)
Sets the indices into the vertex array to use to create the triangles. Each consecutive set of 3 indi...
void LoadFromOBJ(const std::string &filename)
This reads a mesh stored in the common Wavefront Obj file format.
void BuildBVH()
This (re)calculates a Bounding Volume Hierarchy for the mesh, which can be used together with Ray::Fa...
BVH * bvh_ptr()
Returns a pointer to the underlying BVH data structure.
int num_triangles() const
The total number of triangles in the mesh.
int num_vertices() const
The total number of vertices in the mesh.
void SetColors(const std::vector< Color > &colors)
Sets the per-vertex colors array for the mesh directly.
void SetTexCoords(int texture_unit, const std::vector< Point2 > &tex_coords)
Sets a texture coordinates array for the mesh directly.
Color read_color_data(int vertex_id) const
Read only access to per-vertex color data. Data are returned as a Color. Indexed by vertex number....
void UpdateGPUMemory()
This copies the entire mesh data structure to a vertex array in GPU memory, which must happen before ...
void SetTexCoords(int texture_unit, float *tex_coords_array, int num_tex_coords)
Sets a texture coordinates array for the mesh directly. Tex coords are stored as (u,...
void CalcPerVertexNormals()
This (re)calculates the normals for the mesh and stores them with the mesh data structure.
virtual ~Mesh()
void SetNormals(const std::vector< Vector3 > &norms)
Sets the normal array for the mesh directly.
Point3 read_vertex_data(int vertex_id) const
Read only access to the vertex position data. Data are returned as a Point3. Indexed by vertex number...
void SetColors(float *colors_array, int num_colors)
Sets the per-vertex colors array for the mesh directly. Colors are stored as (r,g,...
A 2D Point with floating point coordinates, used for storing 2D texture coordinates,...
Definition: point2.h:28
A 3D Point with floating point coordinates, used for storing vertices and all sorts of other 3D graph...
Definition: point3.h:52
A 3D Vector with floating point coordinates, used for storing normals and all sorts of other 3D graph...
Definition: vector3.h:62
Namespace for the MinGfx Toolkit.
Definition: aabb.h:21