1 #pragma once
2 
3 #include <vector>
4 
5 #include <libcurv/viewer/glfw.h>
6 #include "vertexLayout.h"
7 
8 #define MAX_INDEX_VALUE 65535
9 
10 /*
11  * Vbo - Drawable collection of geometry contained in a vertex buffer and (optionally) an index buffer
12  */
13 
14 class Vbo {
15 public:
16 
17     Vbo();
18     Vbo(VertexLayout* _vertexlayout, GLenum _drawMode = GL_TRIANGLES);
19     virtual ~Vbo();
20 
21     /*
22      * Set Vertex Layout for the Vbo object
23      */
24     void setVertexLayout(VertexLayout* _vertexLayout);
25 
26     /*
27      * Set Draw mode for the Vbo object
28      */
29     void setDrawMode(GLenum _drawMode = GL_TRIANGLES);
30 
31 
32     /*
33      * Adds a single vertex to the mesh; _vertex must be a pointer to the beginning of a vertex structured
34      * according to the VertexLayout associated with this mesh
35      */
36     void addVertex(GLbyte* _vertex);
37 
38     /*
39      * Adds _nVertices vertices to the mesh; _vertices must be a pointer to the beginning of a contiguous
40      * block of _nVertices vertices structured according to the VertexLayout associated with this mesh
41      */
42     void addVertices(GLbyte* _vertices, int _nVertices);
43 
44     /*
45      * Adds a single index to the mesh; indices are unsigned shorts
46      */
47     void addIndex(GLushort* _index);
48 
49     /*
50      * Adds _nIndices indices to the mesh; _indices must be a pointer to the beginning of a contiguous
51      * block of _nIndices unsigned short indices
52      */
53     void addIndices(GLushort* _indices, int _nIndices);
54 
numIndices()55     int numIndices() const { return m_indices.size(); };
numVertices()56     int numVertices() const { return m_nVertices; };
getVertexLayout()57     VertexLayout* getVertexLayout() { return m_vertexLayout; };
58 
59     /*
60      * Copies all added vertices and indices into OpenGL buffer objects; After geometry is uploaded,
61      * no more vertices or indices can be added
62      */
63     void upload();
64 
65     /*
66      * Renders the geometry in this mesh using the ShaderProgram _shader; if geometry has not already
67      * been uploaded it will be uploaded at this point
68      */
69     void draw(const Shader* _shader);
70 
71 private:
72 
73     VertexLayout* m_vertexLayout;
74 
75     std::vector<GLbyte> m_vertexData;
76     GLuint  m_glVertexBuffer;
77     int     m_nVertices;
78 
79     std::vector<GLushort> m_indices;
80     GLuint  m_glIndexBuffer;
81     int     m_nIndices;
82 
83     GLenum  m_drawMode;
84 
85     bool    m_isUploaded;
86 };
87