AGL
A graphics library
mesh.h
1 // Copyright, 2020, Savvy Sine, Aline Normoyle
2 #ifndef AGL_MESH_H_
3 #define AGL_MESH_H_
4 
5 #include <vector>
6 #include "agl/agl.h"
7 #include "agl/aglm.h"
8 
9 namespace agl {
10 
18 class Mesh {
19  public:
20  virtual ~Mesh();
21 
32  virtual void render() const = 0;
33 
37  GLuint vao() const { return _vao; }
38 
42  bool hasUV() const { return _hasUV; }
43 
57  bool isDynamic() const { return _isDynamic; }
58 
59  protected:
60  GLuint _nVerts = 0; // Number of unique vertices
61  GLuint _vao = 0; // The Vertex Array Object
62  bool _hasUV = false;
63  bool _isDynamic = false;
64  bool _initialized = false;
65  std::vector<GLuint> _buffers; // vertex buffers
66  std::vector<GLfloat> _data[5]; // State for dynamic meshes
67  enum VertexAttribute {
68  INDEX = 0,
69  POSITION,
70  NORMAL,
71  UV,
72  TANGENT,
73  NUM_ATTRIBUTES
74  };
75 
79  int numVertices() const { return _nVerts; }
80 
98  void setVertexData(VertexAttribute type, int vertexId, const glm::vec4& data);
99 
118  glm::vec4 vertexData(VertexAttribute type, int vertexId) const;
119 
134  virtual void setIsDynamic(bool on);
135 
145  virtual void init() = 0;
146 
153  void initBuffers(
154  std::vector<GLfloat>* points,
155  std::vector<GLfloat>* normals,
156  std::vector<GLfloat>* texCoords = nullptr,
157  std::vector<GLfloat>* tangents = nullptr);
158 
159  virtual void deleteBuffers();
160 };
161 
162 } // namespace agl
163 #endif // AGL_MESH_H_
agl::Mesh::vao
GLuint vao() const
Return the vertex array object corresponding to this mesh.
Definition: mesh.h:37
agl::Mesh::render
virtual void render() const =0
Draw this mesh.
agl::Mesh::hasUV
bool hasUV() const
Return whether this mesh has UV coordinates defined.
Definition: mesh.h:42
agl::Mesh::setIsDynamic
virtual void setIsDynamic(bool on)
Set whether or not this is a dynamic mesh.
Definition: mesh.cpp:120
agl::Mesh::isDynamic
bool isDynamic() const
Query whether or not this is a dynamic mesh.
Definition: mesh.h:57
agl::Mesh::initBuffers
void initBuffers(std::vector< GLfloat > *points, std::vector< GLfloat > *normals, std::vector< GLfloat > *texCoords=nullptr, std::vector< GLfloat > *tangents=nullptr)
Call initBuffers from init() to set the data for this mesh.
Definition: mesh.cpp:9
agl::Mesh::setVertexData
void setVertexData(VertexAttribute type, int vertexId, const glm::vec4 &data)
Set vertex properties.
Definition: mesh.cpp:125
agl::Mesh::numVertices
int numVertices() const
Get the number of vertices.
Definition: mesh.h:79
aglm.h
Global utilities and common third party includes.
agl::Mesh::init
virtual void init()=0
Override init to specifiy vertex data for the mesh.
agl::Mesh::vertexData
glm::vec4 vertexData(VertexAttribute type, int vertexId) const
Get vertex properties.
Definition: mesh.cpp:146
agl::Mesh
Base class for meshes.
Definition: mesh.h:18