AnimEngine
VertexArrayObject.hpp
1 #pragma once
2 #ifndef KJY_OBJECT_ORIENTED_OPENGL_VERTEX_ARRAY_OBJECT_HPP_
3 #define KJY_OBJECT_ORIENTED_OPENGL_VERTEX_ARRAY_OBJECT_HPP_
4 #include "../OOOGL_Core.hpp"
5 #include "Buffers.hpp"
6 #include <map>
7 #include <string.h>
8 
9 namespace OOOGL{
10 
11 class VertexArrayObject : public OglObject, public OglPossessorInterface<ArrayBuffer, ElementArrayBuffer>{
12  public:
13  VertexArrayObject() : OglObject(_destroyVAO) {
14  glGenVertexArrays(1, getIdPtr());
15  glGetInteger64v(GL_MAX_VERTEX_ATTRIBS, reinterpret_cast<GLint64*>(&(attrib_count)));
16  attrib_enabled_LUT.resize(attrib_count, false);
17  }
18 
19  void bind() const;
20  void unbind() const;
21 
22  void enableVertexAttribArray(GLuint index);
23  void disableVertexAttribArray(GLuint index);
24  void enableAllVertexAttribs();
25  void disableAllVertexAttribs();
26  bool isAttribArrayEnabled(GLuint index) const;
27 
28  void setVertexAtrribPointer(GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid* pointer) const;
29  void setVertexAttribPointerIntegral(GLuint index, GLint size, GLenum type, GLsizei stride, const GLvoid* pointer) const;
30  void setVertexAttribPointerLong(GLuint index, GLint size, GLenum type, GLsizei stride, const GLvoid* pointer) const;
31  void setVertexAttribPointer(GLuint index, const ArrayBuffer::PointerConfig& config) const;
32 
33  void autoVertexAttribPointer(GLuint index, ArrayBuffer vbo);
34 
35  virtual size_t numPossessed() const override {return(_possessed_buffers.size());}
36  virtual void possessObject(ArrayBuffer buffer) override;
37  virtual void possessObject(ElementArrayBuffer buffer) override;
38  virtual bool doesPossessObject(ArrayBuffer buffer) const override;
39  virtual bool doesPossessObject(ElementArrayBuffer buffer) const override;
40  virtual void dispossessObject(const ArrayBuffer& buffer) override;
41  virtual void dispossessObject(const ElementArrayBuffer& buffer) override;
42 
43  void possessBuffer(ArrayBuffer buffer) {possessObject(buffer);}
44  void possessBuffer(ElementArrayBuffer buffer) {possessObject(buffer);}
45  bool doesPossessBuffer(ArrayBuffer buffer) const {return(doesPossessObject(buffer));}
46  bool doesPossessBuffer(ElementArrayBuffer buffer) const {return(doesPossessObject(buffer));}
47  void dispossessBuffer(const ArrayBuffer& buffer) {dispossessObject(buffer);};
48  void dispossessBuffer(const ElementArrayBuffer& buffer) {dispossessObject(buffer);};
49 
50  protected:
51  std::vector<bool> attrib_enabled_LUT;
52  size_t attrib_count = 0;
53 
54  private:
55  static void _destroyVAO(const GLuint* self){
56  glDeleteVertexArrays(1, self);
57  }
58 
59  std::map<GLuint, OpenGlBuffer> _possessed_buffers;
60 
61  protected:
63  public:
64  AttributeIndexOutOfBoundsException(GLuint index) : _whatstr("Out of bounds attribute index '" + std::to_string(index) + "'") {}
65  virtual const char* what() const noexcept override{return(_whatstr.c_str());}
66  private:
67  const std::string _whatstr;
68  };
69 
71  public:
72  NoBufferAtAttribIndexException(GLuint index) : _whatstr("No buffer bound at attribute index '" + std::to_string(index) + "'") {}
73  virtual const char* what() const noexcept override{return(_whatstr.c_str());}
74  private:
75  const std::string _whatstr;
76  };
77 };
78 
79 
81  public:
82  CommonVertexFormatVAO() : VertexArrayObject(), linked_vbos(attrib_count) {linked_elbos.reserve(attrib_count);}
83 
84  size_t numLinkedArrayBuffers() const {return(linked_vbos.size());}
85  size_t numLinkedElementBuffers() const {return(linked_elbos.size());}
86  void linkArrayBuffer(GLuint index, ArrayBuffer buffer);
87  void linkElementBuffer(ElementArrayBuffer buffer);
88 
89  void unlinkArrayBuffer(GLuint index);
90  void unlinkArrayBuffer(ArrayBuffer buffer);
91  void unlinkAllArrayBuffers();
92  void unlinkElementBuffer(ElementArrayBuffer buffer);
93  void unlinkAllElementBuffers();
94 
95  bool hasLinkedBuffer(GLuint index) const;
96  const ArrayBuffer getArrayBuffer(GLuint index) const;
97  ArrayBuffer getArrayBuffer(GLuint index);
98 
99 protected:
101  typedef std::vector<ElementArrayBuffer> LinkedElementsVector_t;
102 
103  LinkedArraysVector_t linked_vbos;
104  LinkedElementsVector_t linked_elbos;
105 
106 public:
107  using vboiterator = LinkedArraysVector_t::iterator;
108  using const_vboiterator = LinkedArraysVector_t::const_iterator;
109 
110  inline const_vboiterator linkedArrayBufsBegin() const {return(linked_vbos.begin());}
111  inline vboiterator linkedArrayBufsBegin() {return(linked_vbos.begin());}
112  inline const_vboiterator linkedArrayBufsEnd() const {return(linked_vbos.end());}
113  inline vboiterator linkedArrayBufsEnd() {return(linked_vbos.end());}
114  inline std::vector<ElementArrayBuffer>::const_iterator linkedElementBufsBegin() const {return(linked_elbos.begin());}
115  inline std::vector<ElementArrayBuffer>::iterator linkedElementBufsBegin() {return(linked_elbos.begin());}
116  inline std::vector<ElementArrayBuffer>::const_iterator linkedElementBufsEnd() const {return(linked_elbos.end());}
117  inline std::vector<ElementArrayBuffer>::iterator linkedElementBufsEnd() {return(linked_elbos.end());}
118 
119  void autoSetupBuffersForDraw();
120  void autoDisableBuffersAfterDraw();
121 };
122 
123 } // Namespace OOOGL
124 
125 #endif
Definition: Buffers.hpp:142
Definition: OOOGL_Core.hpp:119
Definition: OOOGL_Core.hpp:144
Definition: OOOGL_Core.hpp:105
Definition: Buffers.hpp:110
Definition: VertexArrayObject.hpp:11
Definition: Core_Utilities.cpp:3
Definition: VertexArrayObject.hpp:80
Definition: Buffers.hpp:106