Stellarium 0.12.4
StelQGLIndexBuffer.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 _STELQGLINDEXBUFFER_HPP_
21 #define _STELQGLINDEXBUFFER_HPP_
22 
23 #include <algorithm>
24 #include <QtOpenGL>
25 #include <QVector>
26 #include "StelIndexBuffer.hpp"
27 
28 
38 {
39 // StelQGLRenderer constructs QGL index buffers.
40 friend class StelQGLRenderer;
41 // For optimized projection code.
44 public:
45 
46  // Member functions used by the QGL backends
47 
49  const GLvoid* indices() const
50  {
51  Q_ASSERT_X(locked(), Q_FUNC_INFO,
52  "Trying to access raw data of an unlocked index buffer");
53  if(indexType_ == IndexType_U16) {return indices16.constData();}
54  else if(indexType_ == IndexType_U32) {return indices32.constData();}
55  Q_ASSERT_X(false, Q_FUNC_INFO, "Unknown index type");
56  // Prevents GCC from complaining about exiting a non-void function:
57  return NULL;
58  }
59 
61  uint maxIndex() const
62  {
63  return maxIndex_;
64  }
65 protected:
66  // All bound checks are done in StelIndexBuffer.
67 
68  virtual void addIndex_(const uint index)
69  {
70  const int previousIndexCount = length();
71  maxIndex_ = std::max(index, maxIndex_);
72  if(previousIndexCount < indexCapacity())
73  {
74  // We have the capacity to store the index, so store it.
75  // Parent addIndex increments index count.
76  //
77  // This is copied from setIndex_ for inlining
78  // (setIndex_ can't be inlined as it's virtual)
79  if(indexType_ == IndexType_U16) {indices16[previousIndexCount] = index;}
80  else if(indexType_ == IndexType_U32) {indices32[previousIndexCount] = index;}
81  else{Q_ASSERT_X(false, Q_FUNC_INFO, "Unknown index type");}
82  return;
83  }
84  if(indexType_ == IndexType_U16) {indices16.append(index);}
85  else if(indexType_ == IndexType_U32) {indices32.append(index);}
86  else{Q_ASSERT_X(false, Q_FUNC_INFO, "Unknown index type");}
87  }
88 
89  virtual uint getIndex_(const int which) const
90  {
91  if(indexType_ == IndexType_U16) {return indices16[which];}
92  else if(indexType_ == IndexType_U32) {return indices32[which];}
93  Q_ASSERT_X(false, Q_FUNC_INFO, "Unknown index type");
94  // Prevents GCC from complaining about exiting a non-void function:
95  return -1;
96  }
97 
98  virtual void setIndex_(const int which, const uint index)
99  {
100  if(indexType_ == IndexType_U16) {indices16[which] = index;}
101  else if(indexType_ == IndexType_U32) {indices32[which] = index;}
102  else{Q_ASSERT_X(false, Q_FUNC_INFO, "Unknown index type");}
103  }
104 
105  // No need to do anything, length is zeroed in the parent class.
106  virtual void clear_() {}
107 
108  // No need to do anything here until we use VBOs/QGLIndexBuffer
109  virtual void lock_() {}
110  virtual void unlock_() {}
111 
112 private:
113  // This is not very elegant, but not too wasteful compared to amount
114  // of data in the index buffer, and safe.
115 
117  QVector<uint> indices32;
118 
120  QVector<ushort> indices16;
121 
123  StelQGLIndexBuffer(const IndexType indexType)
124  : StelIndexBuffer(indexType)
125  , maxIndex_(0)
126  {}
127 
129  uint maxIndex_;
130 
132  int indexCapacity() const
133  {
134  if(indexType_ == IndexType_U16) {return indices16.size();}
135  else if(indexType_ == IndexType_U32) {return indices32.size();}
136  Q_ASSERT_X(false, Q_FUNC_INFO, "Unknown index type");
137  // Prevents GCC from complaining about exiting a non-void function:
138  return -1;
139  }
140 };
141 
142 #endif // _STELQGLINDEXBUFFER_HPP_