Stellarium 0.12.4
StelVertexBuffer.hpp
1 /*
2  * Stellarium
3  * Copyright (C) 2012 Ferdinand Majerech
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18  */
19 
20 #ifndef _STELVERTEXBUFFER_HPP_
21 #define _STELVERTEXBUFFER_HPP_
22 
23 #include "core/VecMath.hpp"
24 #include "StelVertexAttribute.hpp"
25 #include "StelVertexBufferBackend.hpp"
26 
27 
31 enum PrimitiveType
32 {
34  PrimitiveType_Points,
35 
37  PrimitiveType_Triangles,
38 
41  PrimitiveType_TriangleStrip,
42 
45  PrimitiveType_TriangleFan,
46 
48  PrimitiveType_Lines,
49 
51  PrimitiveType_LineStrip,
52 
54  PrimitiveType_LineLoop,
55 };
56 
61 {
62 public:
63  virtual ~StelAbstractVertexBuffer(){};
64 };
65 
171 template<class V> class StelVertexBuffer : public StelAbstractVertexBuffer
172 {
174 friend class StelRenderer;
175 friend class TestStelVertexBuffer;
176 
177 public:
180  {
181  delete backend;
182  }
183 
189  void addVertex(const V& vertex)
190  {
191  Q_ASSERT_X(!locked_, Q_FUNC_INFO,
192  "Trying to add a vertex to a locked vertex buffer");
193  backend->addVertex(static_cast<const void*>(&vertex));
194  ++vertexCount;
195  }
196 
203  V getVertex(const int index) const
204  {
205  Q_ASSERT_X(!locked_, Q_FUNC_INFO,
206  "Trying to get a vertex in a locked vertex buffer");
207  Q_ASSERT_X(index < vertexCount, Q_FUNC_INFO, "Vertex index out of bounds");
208  // Using a char array instead of a V directly avoids calling the
209  // default constructor of V (which might not be defined, and the
210  // default-constructed data would be overwritten anyway)
211 
212  unsigned char storage[sizeof(V)];
213  void* result = &storage[0];
214  backend->getVertex(index, result);
215  return *static_cast<V*>(result);
216  }
217 
224  void setVertex(const int index, const V& vertex)
225  {
226  Q_ASSERT_X(!locked_, Q_FUNC_INFO,
227  "Trying to set a vertex in a locked vertex buffer");
228  Q_ASSERT_X(index < vertexCount, Q_FUNC_INFO, "Vertex index out of bounds");
229  backend->setVertex(index, static_cast<const void*>(&vertex));
230  }
231 
233  void lock()
234  {
235  locked_ = true;
236  backend->lock();
237  }
238 
240  void unlock()
241  {
242  backend->unlock();
243  locked_ = false;
244  }
245 
247  bool locked() const
248  {
249  return locked_;
250  }
251 
253  int length() const
254  {
255  return vertexCount;
256  }
257 
259  PrimitiveType primitiveType() const
260  {
261  return primitiveType_;
262  }
263 
271  virtual void clear()
272  {
273  Q_ASSERT_X(!locked_, Q_FUNC_INFO, "Trying to clear a locked vertex buffer");
274  vertexCount = 0;
275  backend->clear();
276  }
277 
278 private:
282  StelVertexBuffer(StelVertexBufferBackend* backend, const PrimitiveType primitiveType)
283  : locked_(false)
284  , vertexCount(0)
285  , primitiveType_(primitiveType)
286  , backend(backend)
287  {
288  backend->validateVertexType(sizeof(V));
289  }
290 
292  bool locked_;
293 
295  int vertexCount;
296 
298  PrimitiveType primitiveType_;
299 
301  StelVertexBufferBackend* backend;
302 };
303 
304 #endif // _STELVERTEXBUFFER_HPP_