1 // -*- C++ -*-
2 #ifndef _GLClientAndServerBuffer_h_
3 #define _GLClientAndServerBuffer_h_
4 
5 #include <GG/Base.h>
6 
7 #include <vector>
8 
9 namespace GG {
10 
11 ///////////////////////////////////////////////////////////////////////////
12 // GLBufferBase common base class for Buffer classes
13 ///////////////////////////////////////////////////////////////////////////
14 class GG_API GLBufferBase
15 {
16 public:
17     GLBufferBase();
18 
19     /** Required to automatically drop server buffer in case of delete. */
20     virtual ~GLBufferBase();
21 
22     // use this if you want to make sure that two buffers both
23     // have server buffers or not, drops the buffer for mixed cases
24     void harmonizeBufferType(GLBufferBase& other);
25 
26 protected:
27     // drops the server buffer if one exists
28     void dropServerBuffer();
29 
30     GLuint      b_name;
31 };
32 
33 ///////////////////////////////////////////////////////////////////////////
34 // GLClientAndServerBufferBase
35 // template class for buffers with different types of content
36 ///////////////////////////////////////////////////////////////////////////
37 template <typename vtype>
38 class GG_API GLClientAndServerBufferBase : public GLBufferBase
39 {
40 private:
41     GLClientAndServerBufferBase(); // default ctor forbidden,
42                                    // buffer needs to know number
43                                    // of elements per item
44 public:
45     GLClientAndServerBufferBase(std::size_t elementsPerItem);
46     std::size_t size() const;
47     bool        empty() const;
48 
49     // pre-allocate space for item data
50     void reserve(std::size_t num_items);
51 
52     // store items, buffers usually store tupels, convenience functions
53     // do not use while server buffer exists
54     void store(vtype item);
55     void store(vtype item1, vtype item2);
56     void store(vtype item1, vtype item2, vtype item3);
57     void store(vtype item1, vtype item2, vtype item3, vtype item4);
58 
59     // try to store the buffered data in a server buffer
60     void createServerBuffer();
61 
62     // drops a server buffer if one exists,
63     // clears the client side buffer
64     void clear();
65 
66 protected:
67     std::vector<vtype>  b_data;
68     std::size_t         b_size;
69     std::size_t         b_elements_per_item;
70 
71     // used in derived classes to activate the buffer
72     // implementations should use glBindBuffer, gl...Pointer if
73     // server buffer exists (b_name! = 0), just gl...Pointer otherwise
74     virtual void activate() const = 0;
75 };
76 
77 ///////////////////////////////////////////////////////////////////////////
78 // GLRGBAColorBuffer specialized class for RGBA color values
79 ///////////////////////////////////////////////////////////////////////////
80 class GG_API GLRGBAColorBuffer : public GLClientAndServerBufferBase<unsigned char>
81 {
82 public:
83     GLRGBAColorBuffer();
84     void store(const Clr& color);
85     void activate() const override;
86 };
87 
88 ///////////////////////////////////////////////////////////////////////////
89 // GL2DVertexBuffer specialized class for 2d vertex data
90 ///////////////////////////////////////////////////////////////////////////
91 class GG_API GL2DVertexBuffer : public GLClientAndServerBufferBase<float>
92 {
93 public:
94     GL2DVertexBuffer();
95     void store(const Pt& pt);
96     void store(X x, Y y);
97     void store(X x, float y);
98     void store(float x, Y y);
99     void store(float x, float y);
100     void activate() const override;
101 };
102 
103 ///////////////////////////////////////////////////////////////////////////
104 // GLTexCoordBuffer specialized class for texture coordinate data
105 ///////////////////////////////////////////////////////////////////////////
106 class GG_API GLTexCoordBuffer : public GLClientAndServerBufferBase<float>
107 {
108 public:
109     GLTexCoordBuffer();
110     void activate() const override;
111 };
112 
113 ///////////////////////////////////////////////////////////////////////////
114 // GL3DVertexBuffer specialized class for 3d vertex data
115 ///////////////////////////////////////////////////////////////////////////
116 class GG_API GL3DVertexBuffer : public GLClientAndServerBufferBase<float>
117 {
118 public:
119     GL3DVertexBuffer();
120     void store(float x, float y, float z);
121     void activate() const override;
122 };
123 
124 ///////////////////////////////////////////////////////////////////////////
125 // GLNormalBuffer specialized class for 3d normal data
126 ///////////////////////////////////////////////////////////////////////////
127 class GG_API GLNormalBuffer : public GLClientAndServerBufferBase<float>
128 {
129 public:
130     GLNormalBuffer();
131     void store(float x, float y, float z);
132     void activate() const override;
133 };
134 
135 }
136 
137 #endif
138