All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
GlBuffer.h
1 // Copyright eeGeo Ltd (2012-2014), All Rights Reserved
2 
3 #pragma once
4 
5 #include "Types.h"
6 #include "Text.h"
7 #include "Rendering.h"
8 #include "Graphics.h"
9 
10 
11 #include <vector>
12 
13 namespace Eegeo
14 {
15  namespace Text
16  {
17 
18 #if defined(EEGEO_UNITY)
19 #pragma clang diagnostic push
20 #pragma clang diagnostic ignored "-Wunused-private-field"
21 #endif
22 
24  {
25  public:
26  GlBufferBase(GLenum bufferType,
27  GLenum hint,
28  int strideSize,
29  int bufferChainCount);
30 
31  ~GlBufferBase();
32 
33  void Reserve(int elementCount);
34 
35  void Shrink(int elementCount);
36 
37  void Bind() const;
38 
39  void Unbind() const;
40 
41  void Swap();
42 
43  protected:
44 
45  void Upload(const void* pData, int elementOffset, int elementCount) const;
46 
47  private:
48  void ResizeBuffer(int elementCount);
49 
50 
51 
52  std::vector<GLuint> m_glBuffers;
53  GLenum m_bufferType;
54  GLenum m_hint;
55  int m_strideSize;
56  int m_elementCapacity;
57  int m_currentBufferIndex;
58  GLuint m_currentBuffer;
59  };
60 
61 #if defined(EEGEO_UNITY)
62 #pragma clang diagnostic pop
63 #endif
64 
65  template <typename T>
66  class GlBuffer : public GlBufferBase
67  {
68  public:
69  typedef T ElementType;
70  typedef GlBuffer<T> ThisType;
71  typedef std::vector<ElementType> ElementVectorType;
72 
73  inline static GlBuffer<ElementType>* Create(GLenum bufferType, GLenum hint, int bufferChainCount = 1)
74  {
75  return Eegeo_NEW(GlBuffer<ElementType>)(bufferType, hint, bufferChainCount);
76  }
77 
78  void Reset()
79  {
80  m_elementOffset = 0;
81  }
82 
83  void Upload(const ElementVectorType& elements)
84  {
85  const void* pData = static_cast<const void*>(elements.data());
86 
87  GlBufferBase::Upload(pData, m_elementOffset, static_cast<int>(elements.size()));
88 
89  m_elementOffset += static_cast<int>(elements.size());
90  }
91 
92  int GetElementOffset() const { return m_elementOffset; }
93 
94  private:
95  GlBuffer(GLenum bufferType,
96  GLenum hint,
97  int bufferChainCount)
98  : GlBufferBase(bufferType, hint, static_cast<int>(sizeof(ElementType)), bufferChainCount)
99  , m_elementOffset(0)
100  {
101 
102  }
103 
104  int m_elementOffset;
105  };
106  }
107 }