Stellarium 0.12.0
Home · All Namespaces · All Classes · Functions · Coding Style · Scripting · Plugins · Renderer · File Structure

StelVertexBuffer.hpp

00001 /*
00002  * Stellarium
00003  * Copyright (C) 2012 Ferdinand Majerech
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * as published by the Free Software Foundation; either version 2
00008  * of the License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA  02110-1335, USA.
00018  */
00019 
00020 #ifndef _STELVERTEXBUFFER_HPP_
00021 #define _STELVERTEXBUFFER_HPP_
00022 
00023 #include "core/VecMath.hpp"
00024 #include "StelVertexAttribute.hpp"
00025 #include "StelVertexBufferBackend.hpp"
00026 
00027 
00031 enum PrimitiveType
00032 {
00034     PrimitiveType_Points,
00035 
00037     PrimitiveType_Triangles,
00038 
00041     PrimitiveType_TriangleStrip,
00042 
00045     PrimitiveType_TriangleFan,
00046 
00048     PrimitiveType_Lines, 
00049 
00051     PrimitiveType_LineStrip,
00052 
00054     PrimitiveType_LineLoop,
00055 };
00056 
00060 class StelAbstractVertexBuffer
00061 {
00062 public:
00063     virtual ~StelAbstractVertexBuffer(){};
00064 };
00065 
00171 template<class V> class StelVertexBuffer : public StelAbstractVertexBuffer
00172 {
00174 friend class StelRenderer;
00175 friend class TestStelVertexBuffer;
00176 
00177 public:
00179     ~StelVertexBuffer()
00180     {
00181         delete backend;
00182     }
00183     
00189     void addVertex(const V& vertex)
00190     {
00191         Q_ASSERT_X(!locked_, Q_FUNC_INFO,
00192                    "Trying to add a vertex to a locked vertex buffer");
00193         backend->addVertex(static_cast<const void*>(&vertex));
00194         ++vertexCount;
00195     }
00196     
00203     V getVertex(const int index) const
00204     {
00205         Q_ASSERT_X(!locked_, Q_FUNC_INFO,
00206                    "Trying to get a vertex in a locked vertex buffer");
00207         Q_ASSERT_X(index < vertexCount, Q_FUNC_INFO, "Vertex index out of bounds");
00208         // Using a char array instead of a V directly avoids calling the 
00209         // default constructor of V (which might not be defined, and the 
00210         // default-constructed data would be overwritten anyway)
00211         
00212         unsigned char storage[sizeof(V)];
00213         void* result = &storage[0];
00214         backend->getVertex(index, result);
00215         return *static_cast<V*>(result);
00216     }
00217     
00224     void setVertex(const int index, const V& vertex)
00225     {
00226         Q_ASSERT_X(!locked_, Q_FUNC_INFO,
00227                    "Trying to set a vertex in a locked vertex buffer");
00228         Q_ASSERT_X(index < vertexCount, Q_FUNC_INFO, "Vertex index out of bounds");
00229         backend->setVertex(index, static_cast<const void*>(&vertex));
00230     }
00231     
00233     void lock() 
00234     {
00235         locked_ = true;
00236         backend->lock();
00237     }
00238     
00240     void unlock()
00241     {
00242         backend->unlock();
00243         locked_ = false;
00244     }
00245     
00247     bool locked() const
00248     {
00249         return locked_;
00250     }
00251     
00253     int length() const
00254     {
00255         return vertexCount;
00256     }
00257 
00259     PrimitiveType primitiveType() const 
00260     {
00261         return primitiveType_;
00262     }
00263     
00271     virtual void clear()
00272     {
00273         Q_ASSERT_X(!locked_, Q_FUNC_INFO, "Trying to clear a locked vertex buffer");
00274         vertexCount = 0;
00275         backend->clear();
00276     }
00277 
00278 private:
00282     StelVertexBuffer(StelVertexBufferBackend* backend, const PrimitiveType primitiveType)
00283         : locked_(false)
00284         , vertexCount(0)
00285         , primitiveType_(primitiveType)
00286         , backend(backend)
00287     {
00288         backend->validateVertexType(sizeof(V));
00289     }
00290     
00292     bool locked_;
00293     
00295     int vertexCount;
00296 
00298     PrimitiveType primitiveType_;
00299     
00301     StelVertexBufferBackend* backend;
00302 };
00303 
00304 #endif // _STELVERTEXBUFFER_HPP_
Generated on Thu Jan 31 14:05:40 2013 for Stellarium by  doxygen 1.6.3