GLW
Computer Graphics Structures library
ArrayObject.h
1 /*
2  Copyright (C) Skeletex Research, s.r.o. - All Rights Reserved
3  Unauthorized copying of this file, via any medium is strictly prohibited
4  Proprietary and confidential
5 */
6 #ifndef GLW_ARRAY_OBJECT_H
7 #define GLW_ARRAY_OBJECT_H
8 #include <memory>
9 #include <vector>
10 #include <string>
11 
12 #include <glbinding/gl/gl.h>
13 #include <glm/glm.hpp>
14 #include <glm/gtc/type_ptr.hpp>
15 
16 #include <GLW/API.h>
17 
18 namespace glw
19 {
20 
36  enum class DataUsage : uint32_t
37  {
38  stream_draw = uint32_t(gl::GLenum::GL_STREAM_DRAW),
39  stream_read = uint32_t(gl::GLenum::GL_STREAM_READ),
40  stream_copy = uint32_t(gl::GLenum::GL_STREAM_COPY),
41  static_draw = uint32_t(gl::GLenum::GL_STATIC_DRAW),
42  static_read = uint32_t(gl::GLenum::GL_STATIC_READ),
43  static_copy = uint32_t(gl::GLenum::GL_STATIC_COPY),
44  dynamic_draw = uint32_t(gl::GLenum::GL_DYNAMIC_DRAW),
45  dynamic_read = uint32_t(gl::GLenum::GL_DYNAMIC_READ),
46  dynamic_copy = uint32_t(gl::GLenum::GL_DYNAMIC_COPY)
47  };
48 
49 
50 
52  enum class DrawMode : uint32_t
53  {
54  points = uint32_t(gl::GLenum::GL_POINTS),
55  lines = uint32_t(gl::GLenum::GL_LINES),
56  line_strip = uint32_t(gl::GLenum::GL_LINE_STRIP),
57  line_loop = uint32_t(gl::GLenum::GL_LINE_LOOP),
58  triangles = uint32_t(gl::GLenum::GL_TRIANGLES),
59  triangle_strip = uint32_t(gl::GLenum::GL_TRIANGLE_STRIP),
60  triangle_fan = uint32_t(gl::GLenum::GL_TRIANGLE_FAN),
61  patches = uint32_t(gl::GLenum::GL_PATCHES)
62  };
63 
64 
66  struct Attrib
67  {
68  gl::GLenum type; // Type of attribute defined by OpenGL specifier.
69  uint32_t per_vert; // Number of values for a single vertex.
70  uint64_t offset; // Byte offset of first value, from buffer start.
71  uint32_t stride; // Byte space between starts of two consecutive attribute values.
72  Attrib(gl::GLenum gl_type, uint32_t data_per_vert, uint32_t byte_offset, uint32_t byte_stride)
73  : type(gl_type), per_vert(data_per_vert), offset(byte_offset), stride(byte_stride) {};
74  };
75 
76  template <gl::GLenum T, uint32_t N>
77  struct TAttrib : public Attrib
78  {
79  TAttrib() : Attrib(T, N, 0u, 0u) {};
80  TAttrib(uint32_t byte_offset, uint32_t byte_stride)
81  : Attrib(T, N, byte_offset, byte_stride) {};
82  };
83 
84  using Attrib1f = TAttrib<gl::GLenum::GL_FLOAT, 1>;
85  using Attrib2f = TAttrib<gl::GLenum::GL_FLOAT, 2>;
86  using Attrib3f = TAttrib<gl::GLenum::GL_FLOAT, 3>;
87  using Attrib4f = TAttrib<gl::GLenum::GL_FLOAT, 4>;
88  using Attrib1i = TAttrib<gl::GLenum::GL_INT, 1>;
89  using Attrib2i = TAttrib<gl::GLenum::GL_INT, 2>;
90  using Attrib3i = TAttrib<gl::GLenum::GL_INT, 3>;
91  using Attrib4i = TAttrib<gl::GLenum::GL_INT, 4>;
92 
93 
94 
95  using PArrayObject = std::shared_ptr<class ArrayObject>;
96 
98  class GLW_API ArrayObject
99  {
100  public:
101  using BufferName = size_t;
102  using ElementBufferName = size_t;
103 
105  static const BufferName INVALID_NAME { UINT32_MAX };
106 
107 
109  static PArrayObject Create(const size_t vertex_count);
110 
112  explicit ArrayObject(size_t vertex_count);
113 
115  ~ArrayObject();
116 
118  uint64_t GetVertexCount() const;
119 
121  void Bind() const;
122 
124  void Unbind() const;
125 
127  bool IsBound() const;
128 
130  BufferName AddBuffer(const void *data, size_t per_vert_size, DataUsage usage = DataUsage::dynamic_draw);
131 
133  void SetBuffer(BufferName vert_buffer, const void *data, size_t per_vert_size);
134 
136  bool IsBufferValid(BufferName vert_buffer) const;
137 
139  void SetBinding(const Attrib &att, uint32_t shader_location, BufferName buffer);
140 
142  BufferName GetBinding(uint32_t shader_location) const;
143 
145  bool ExistsBinding(uint32_t shader_location) const;
146 
148  BufferName AddBufferBinding1f(uint32_t shader_location, const void *data);
149 
151  BufferName AddBufferBinding2f(uint32_t shader_location, const void *data);
152 
154  BufferName AddBufferBinding3f(uint32_t shader_location, const void *data);
155 
157  BufferName AddBufferBinding4f(uint32_t shader_location, const void *data);
158 
160  BufferName AddBufferBinding1i(uint32_t shader_location, const void *data);
161 
163  BufferName AddBufferBinding2i(uint32_t shader_location, const void *data);
164 
166  BufferName AddBufferBinding3i(uint32_t shader_location, const void *data);
167 
169  BufferName AddBufferBinding4i(uint32_t shader_location, const void *data);
170 
172  ElementBufferName AddElementBuffer(const uint32_t *data, size_t count);
173 
175  void SetDrawMode(DrawMode mode);
176 
178  void Draw() const;
179 
181  void DrawInstanced(uint32_t instance_count) const;
182 
184  void DrawVertices(DrawMode mode) const;
185 
187  void DrawVertices(size_t num_vertices, DrawMode mode) const;
188 
190  void DrawVerticesInstanced(uint32_t instance_count, DrawMode mode) const;
191 
193  void DrawElements(ElementBufferName el_buff, DrawMode mode) const;
194 
196  void DrawElementsInstanced(uint32_t instance_count, ElementBufferName el_buff, DrawMode mode) const;
197 
198  protected:
199  struct ElementBuffer
200  {
201  uint32_t name;
202  size_t primitive_count;
203  };
204  uint32_t vao_ = 0;
205  size_t vertex_count_ = 0;
206  DrawMode default_mode_ = DrawMode::points;
207  std::vector<uint32_t> buffer_names_;
208  std::unordered_map<uint32_t, BufferName> bindings_;
209  std::vector<ElementBuffer> elements_;
210  };
211 
212 }
213 
214 #endif /* !GLW_ARRAY_OBJECT_H */
OpenGL wrapper library namespace. Provides simplified interface for using OpenGL features.
Definition: API.h:17
DataUsage
Hint to the GL implementation as to how a buffer object&#39;s data store will be accessed.
Definition: ArrayObject.h:36
size_t ElementBufferName
Identifier of element buffer.
Definition: ArrayObject.h:102
DrawMode
Specifies what kind of primitives are rendered.
Definition: ArrayObject.h:52
Class to store data arrays. Arrays can be later accessed by shaders when draw is requested.
Definition: ArrayObject.h:98
Array attribute. Defines how specific data are stored in combined buffer. For example how are vertex ...
Definition: ArrayObject.h:66
size_t BufferName
Identifier of vertex buffer.
Definition: ArrayObject.h:101