00001 #ifndef _VERTEX_STORAGE_
00002 #define _VERTEX_STORAGE_
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00072 template<typename T>
00073 class VertexStorage
00074 {
00075 public:
00076 VertexStorage(int maxVertexCount, int maxIndexCount);
00077 ~VertexStorage();
00078 int getVertexCount();
00079 int getIndexCount();
00080 bool appendVertex( T vertex );
00081 bool appendVertexIndices(int idx1, int idx2, int idx3);
00082 bool appendVertexWithIndices(T vertex, int idx1, int idx2, int idx3);
00083 const int MAX_VERTEX_COUNT;
00084 const int MAX_INDEX_COUNT;
00085 T *vertices;
00086 int *indices;
00087 private:
00088 int nextVertex;
00089 int nextIndex;
00090 };
00091
00103 template <typename T>
00104 VertexStorage<T>::VertexStorage(int maxVertexCount, int maxIndexCount)
00105 : MAX_VERTEX_COUNT(maxVertexCount), MAX_INDEX_COUNT(maxIndexCount),
00106 nextVertex(0), nextIndex(0)
00107 {
00108 vertices = new T[maxVertexCount];
00109 indices = new int[maxIndexCount];
00110 for (int i = 0; i < maxIndexCount; i++)
00111 indices[i] = 0;
00112 }
00113
00117 template <typename T>
00118 VertexStorage<T>::~VertexStorage()
00119 {
00120 if (vertices)
00121 {
00122 for( int i = 0; i < this->MAX_VERTEX_COUNT; i++ )
00123 delete vertices[i];
00124 delete vertices;
00125 }
00126 if (indices)
00127 delete indices;
00128 }
00129
00138 template <typename T>
00139 bool VertexStorage<T>::appendVertex( T vertex )
00140 {
00141 if ( MAX_VERTEX_COUNT <= nextVertex )
00142 return false;
00143 vertices[nextVertex][0] = vertex[0];
00144 vertices[nextVertex][1] = vertex[1];
00145 vertices[nextVertex][2] = vertex[2];
00146 nextVertex++;
00147 return true;
00148 }
00149
00162 template <typename T>
00163 bool VertexStorage<T>::appendVertexIndices(int idx1, int idx2, int idx3)
00164 {
00165 if ( MAX_INDEX_COUNT <= nextIndex + 2 )
00166 return false;
00167 indices[nextIndex] = idx1;
00168 indices[nextIndex + 1] = idx2;
00169 indices[nextIndex + 2] = idx3;
00170 nextIndex += 3;
00171 return true;
00172 }
00173
00187 template <typename T>
00188 bool VertexStorage<T>::appendVertexWithIndices(T vertex, int idx1, int idx2, int idx3)
00189 {
00190 if ( false == this->appendVertex( vertex ) )
00191 return false;
00192 bool result = this->appendVertexIndices( idx1, idx2, idx3 );
00193 if (false == result)
00194 {
00195
00196
00197 if (0 < nextVertex)
00198 nextVertex--;
00199 }
00200 return result;
00201 }
00202
00203 #endif // _VERTEX_STORAGE_