Stellarium 0.12.4
StelVertexBufferBackend.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 _STELVERTEXBUFFERBACKEND_HPP_
21 #define _STELVERTEXBUFFERBACKEND_HPP_
22 
23 #include <QVector>
24 
25 #include "StelVertexAttribute.hpp"
26 
27 
31 static const int MAX_VERTEX_ATTRIBUTES = 8;
32 
56 {
61  struct Attributes
62  {
64  StelVertexAttribute attributes[MAX_VERTEX_ATTRIBUTES];
66  int sizes[MAX_VERTEX_ATTRIBUTES];
68  int count;
69 
71  Attributes(const QVector<StelVertexAttribute>& attributeVector)
72  : count(attributeVector.size())
73  {
74  for(int a = 0; a < count; ++a)
75  {
76  Q_ASSERT_X(count < MAX_VERTEX_ATTRIBUTES, Q_FUNC_INFO,
77  "Too many vertex attributes (increase MAX_VERTEX_ATTRIBUTES"
78  "in StelVertexBufferBackend.hpp)");
79  attributes[a] = attributeVector[a];
80  sizes[a] = attributeSize(attributes[a].type);
81  }
82  }
83  };
84 
85 public:
87  StelVertexBufferBackend(const QVector<StelVertexAttribute>& attributes)
88  : attributes(attributes)
89  {
90  }
91 
94 
102  virtual void addVertex(const void* const vertexInPtr) = 0;
103 
112  virtual void getVertex(const int index, void* const vertexOutPtr) const = 0;
113 
123  virtual void setVertex(const int index, const void* const vertexInPtr) = 0;
124 
126  virtual void lock() = 0;
127 
129  virtual void unlock() = 0;
130 
136  virtual void clear() = 0;
137 
139  void validateVertexType(const int vertexSize)
140  {
141 #ifdef NDEBUG
142  Q_UNUSED(vertexSize);
143 #endif
144  // We have no way of looking at each data member of the vertex type,
145  // but we can at least enforce that their total length matches.
146  int bytes = 0;
147 
148  for(int attrib = 0; attrib < attributes.count; ++attrib)
149  {
150  bytes += attributes.sizes[attrib];
151  }
152 
153  Q_ASSERT_X(bytes == vertexSize, Q_FUNC_INFO,
154  "Size of the vertex type in bytes doesn't match the sum of sizes of "
155  "all vertex attributes as reported by \"attributes\" data member.");
156  }
157 
158 protected:
160  const Attributes attributes;
161 };
162 
163 
164 #endif // _STELVERTEXBUFFERBACKEND_HPP_