Irrlicht 3D Engine
CVertexBuffer.h
Go to the documentation of this file.
00001 // Copyright (C) 2008-2012 Nikolaus Gebhardt
00002 // This file is part of the "Irrlicht Engine".
00003 // For conditions of distribution and use, see copyright notice in irrlicht.h
00004 
00005 #ifndef __C_VERTEX_BUFFER_H_INCLUDED__
00006 #define __C_VERTEX_BUFFER_H_INCLUDED__
00007 
00008 #include "IVertexBuffer.h"
00009 
00010 
00011 namespace irr
00012 {
00013 namespace scene
00014 {
00015 
00016     class CVertexBuffer : public IVertexBuffer
00017     {
00018         class IVertexList
00019         {
00020         public:
00021             virtual ~IVertexList(){};
00022 
00023             virtual u32 stride() const =0;
00024 
00025             virtual u32 size() const =0;
00026 
00027             virtual void push_back (const video::S3DVertex &element) =0;
00028             virtual video::S3DVertex& operator [](const u32 index) const =0;
00029             virtual video::S3DVertex& getLast() =0;
00030             virtual void set_used(u32 usedNow) =0;
00031             virtual void reallocate(u32 new_size) =0;
00032             virtual u32 allocated_size() const =0;
00033             virtual video::S3DVertex* pointer() =0;
00034             virtual video::E_VERTEX_TYPE getType() const =0;
00035         };
00036 
00037         template <class T>
00038         class CSpecificVertexList : public IVertexList
00039         {
00040         public:
00041             core::array<T> Vertices;
00042 
00043             virtual u32 stride() const {return sizeof(T);}
00044 
00045             virtual u32 size() const {return Vertices.size();}
00046 
00047             virtual void push_back (const video::S3DVertex &element)
00048             {Vertices.push_back((T&)element);}
00049 
00050             virtual video::S3DVertex& operator [](const u32 index) const
00051             {return (video::S3DVertex&)Vertices[index];}
00052 
00053             virtual video::S3DVertex& getLast()
00054             {return (video::S3DVertex&)Vertices.getLast();}
00055 
00056             virtual void set_used(u32 usedNow)
00057             {Vertices.set_used(usedNow);}
00058 
00059             virtual void reallocate(u32 new_size)
00060             {Vertices.reallocate(new_size);}
00061 
00062             virtual u32 allocated_size() const
00063             {
00064                 return Vertices.allocated_size();
00065             }
00066 
00067             virtual video::S3DVertex* pointer() {return Vertices.pointer();}
00068 
00069             virtual video::E_VERTEX_TYPE getType() const {return T().getType();}
00070         };
00071 
00072     public:
00073         IVertexList *Vertices;
00074 
00075         CVertexBuffer(video::E_VERTEX_TYPE vertexType) : Vertices(0),
00076                 MappingHint(EHM_NEVER), ChangedID(1)
00077         {
00078             setType(vertexType);
00079         }
00080 
00081         CVertexBuffer(const IVertexBuffer &VertexBufferCopy) :
00082                 Vertices(0), MappingHint(EHM_NEVER),
00083                 ChangedID(1)
00084         {
00085             setType(VertexBufferCopy.getType());
00086             reallocate(VertexBufferCopy.size());
00087 
00088             for (u32 n=0;n<VertexBufferCopy.size();++n)
00089                 push_back(VertexBufferCopy[n]);
00090         }
00091 
00092         virtual ~CVertexBuffer()
00093         {
00094             delete Vertices;
00095         }
00096 
00097 
00098         virtual void setType(video::E_VERTEX_TYPE vertexType)
00099         {
00100             IVertexList *NewVertices=0;
00101 
00102             switch (vertexType)
00103             {
00104                 case video::EVT_STANDARD:
00105                 {
00106                     NewVertices=new CSpecificVertexList<video::S3DVertex>;
00107                     break;
00108                 }
00109                 case video::EVT_2TCOORDS:
00110                 {
00111                     NewVertices=new CSpecificVertexList<video::S3DVertex2TCoords>;
00112                     break;
00113                 }
00114                 case video::EVT_TANGENTS:
00115                 {
00116                     NewVertices=new CSpecificVertexList<video::S3DVertexTangents>;
00117                     break;
00118                 }
00119             }
00120             if (Vertices)
00121             {
00122                 NewVertices->reallocate( Vertices->size() );
00123 
00124                 for(u32 n=0;n<Vertices->size();++n)
00125                     NewVertices->push_back((*Vertices)[n]);
00126 
00127                 delete Vertices;
00128             }
00129 
00130             Vertices=NewVertices;
00131         }
00132 
00133         virtual void* getData() {return Vertices->pointer();}
00134 
00135         virtual video::E_VERTEX_TYPE getType() const {return Vertices->getType();}
00136 
00137         virtual u32 stride() const {return Vertices->stride();}
00138 
00139         virtual u32 size() const
00140         {
00141             return Vertices->size();
00142         }
00143 
00144         virtual void push_back (const video::S3DVertex &element)
00145         {
00146             Vertices->push_back(element);
00147         }
00148 
00149         virtual video::S3DVertex& operator [](const u32 index) const
00150         {
00151             return (*Vertices)[index];
00152         }
00153 
00154         virtual video::S3DVertex& getLast()
00155         {
00156             return Vertices->getLast();
00157         }
00158 
00159         virtual void set_used(u32 usedNow)
00160         {
00161             Vertices->set_used(usedNow);
00162         }
00163 
00164         virtual void reallocate(u32 new_size)
00165         {
00166             Vertices->reallocate(new_size);
00167         }
00168 
00169         virtual u32 allocated_size() const
00170         {
00171             return Vertices->allocated_size();
00172         }
00173 
00174         virtual video::S3DVertex* pointer()
00175         {
00176             return Vertices->pointer();
00177         }
00178 
00180         virtual E_HARDWARE_MAPPING getHardwareMappingHint() const
00181         {
00182             return MappingHint;
00183         }
00184 
00186         virtual void setHardwareMappingHint( E_HARDWARE_MAPPING NewMappingHint )
00187         {
00188             MappingHint=NewMappingHint;
00189         }
00190 
00192         virtual void setDirty()
00193         {
00194             ++ChangedID;
00195         }
00196 
00198 
00199         virtual u32 getChangedID() const {return ChangedID;}
00200 
00201         E_HARDWARE_MAPPING MappingHint;
00202         u32 ChangedID;
00203     };
00204 
00205 
00206 } // end namespace scene
00207 } // end namespace irr
00208 
00209 #endif
00210