Stellarium 0.12.4
StelQGLArrayVertexBufferBackend.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 _STELQGLARRAYVERTEXBUFFERBACKEND_HPP_
21 #define _STELQGLARRAYVERTEXBUFFERBACKEND_HPP_
22 
23 #include "VecMath.hpp"
24 #include "StelProjectorType.hpp"
25 #include "StelVertexBuffer.hpp"
26 #include "StelVertexBufferBackend.hpp"
27 #include "StelVertexAttribute.hpp"
28 
29 
34 {
35 public:
37 
38  virtual void addVertex(const void* const vertexInPtr);
39 
40  virtual void getVertex(const int index, void* const vertexOutPtr) const;
41 
42  virtual void setVertex(const int index, const void* const vertexInPtr)
43  {
44  setVertexNonVirtual(index, vertexInPtr);
45  }
46 
47  virtual void lock()
48  {
49  locked = true;
50  }
51 
52  virtual void unlock()
53  {
54  locked = false;
55  }
56 
57  virtual void clear()
58  {
59  vertexCount = 0;
60  }
61 
71  void projectVertices(StelProjector* projector,
72  class StelQGLIndexBuffer* indexBuffer);
73 
75  int length() const
76  {
77  return vertexCount;
78  }
79 
81  PrimitiveType getPrimitiveType() const
82  {
83  return primitiveType;
84  }
85 
86 protected:
88  bool locked;
89 
91  PrimitiveType primitiveType;
92 
95 
98 
103  void* attributeBuffers[AttributeInterpretation_MAX];
104 
113 
119 
122 
129  StelQGLArrayVertexBufferBackend(const PrimitiveType type,
130  const QVector<StelVertexAttribute>& attributes);
131 
132 private:
133 
137  void setVertexNonVirtual(const int index, const void* const vertexInPtr)
138  {
139  // Points to the current attribute (e.g. color, normal, vertex) within the vertex.
140  const unsigned char* attribPtr = static_cast<const unsigned char*>(vertexInPtr);
141  for(int attrib = 0; attrib < attributes.count; ++attrib)
142  {
143  const AttributeInterpretation interpretation =
144  attributes.attributes[attrib].interpretation;
145  //Set each attribute in its buffer.
146  switch(attributes.attributes[attrib].type)
147  {
148  case AttributeType_Vec2f:
149  getAttribute<Vec2f>(interpretation, index)
150  = *reinterpret_cast<const Vec2f*>(attribPtr);
151  break;
152  case AttributeType_Vec3f:
153  getAttribute<Vec3f>(interpretation, index)
154  = *reinterpret_cast<const Vec3f*>(attribPtr);
155  break;
156  case AttributeType_Vec4f:
157  getAttribute<Vec4f>(interpretation, index)
158  = *reinterpret_cast<const Vec4f*>(attribPtr);
159  break;
160  default:
161  Q_ASSERT(false);
162  }
163 
164  // This always works, because the C standard requires that
165  // sizeof(unsigned char) == 1 (that 1 might mean e.g. 16 bits instead of 8
166  // on some platforms, but both the size of attribute and of unsigned char is
167  // measured in the same unit, so it's not a problem.
168  attribPtr += attributes.sizes[attrib];
169  }
170  }
171 
180  template<class A>
181  A& getAttribute(const AttributeInterpretation interpretation, const int vertexIndex)
182  {
183  return static_cast<A*>(attributeBuffers[interpretation])[vertexIndex];
184  }
185 
189  template<class A>
190  const A& getAttributeConst(const AttributeInterpretation interpretation, const int vertexIndex) const
191  {
192  return static_cast<A*>(attributeBuffers[interpretation])[vertexIndex];
193  }
194 
200  int getAttributeIndex(const AttributeInterpretation interpretation) const;
201 };
202 
203 #endif // _STELQGLARRAYVERTEXBUFFERBACKEND_HPP_