Irrlicht 3D Engine: CMeshBuffer.h Source File

| Irrlicht 3D Engine |

CMeshBuffer.h
Go to the documentation of this file.

    00001 // Copyright (C) 2002-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 __T_MESH_BUFFER_H_INCLUDED__
    00006 #define __T_MESH_BUFFER_H_INCLUDED__
    00007
    00008 #include "irrArray.h"
    00009 #include "IMeshBuffer.h"
    00010
    00011 namespace irr
    00012 {
    00013 namespace scene
    00014 {
    00016     template <class T>
    00017     class CMeshBuffer : public IMeshBuffer
    00018     {
    00019     public:
    00021         CMeshBuffer():ChangedID_Vertex(1),ChangedID_Index(1),MappingHint_Vertex(EHM_NEVER), MappingHint_Index(EHM_NEVER)
    00022         {
    00023             #ifdef _DEBUG
    00024             setDebugName("SMeshBuffer");
    00025             #endif
    00026         }
    00027
    00028
    00030
    00031         virtual const video::SMaterial& getMaterial() const
    00032         {
    00033             return Material;
    00034         }
    00035
    00036
    00038
    00039         virtual video::SMaterial& getMaterial()
    00040         {
    00041             return Material;
    00042         }
    00043
    00044
    00046
    00047         virtual const void* getVertices() const
    00048         {
    00049             return Vertices.const_pointer();
    00050         }
    00051
    00052
    00054
    00055         virtual void* getVertices()
    00056         {
    00057             return Vertices.pointer();
    00058         }
    00059
    00060
    00062
    00063         virtual u32 getVertexCount() const
    00064         {
    00065             return Vertices.size();
    00066         }
    00067
    00069
    00070         virtual video::E_INDEX_TYPE getIndexType() const
    00071         {
    00072             return video::EIT_16BIT;
    00073         }
    00074
    00076
    00077         virtual const u16* getIndices() const
    00078         {
    00079             return Indices.const_pointer();
    00080         }
    00081
    00082
    00084
    00085         virtual u16* getIndices()
    00086         {
    00087             return Indices.pointer();
    00088         }
    00089
    00090
    00092
    00093         virtual u32 getIndexCount() const
    00094         {
    00095             return Indices.size();
    00096         }
    00097
    00098
    00100
    00101         virtual const core::aabbox3d<f32>& getBoundingBox() const
    00102         {
    00103             return BoundingBox;
    00104         }
    00105
    00106
    00108
    00109
    00110         virtual void setBoundingBox(const core::aabbox3df& box)
    00111         {
    00112             BoundingBox = box;
    00113         }
    00114
    00115
    00117
    00118         virtual void recalculateBoundingBox()
    00119         {
    00120             if (Vertices.empty())
    00121                 BoundingBox.reset(0,0,0);
    00122             else
    00123             {
    00124                 BoundingBox.reset(Vertices[0].Pos);
    00125                 for (u32 i=1; i<Vertices.size(); ++i)
    00126                     BoundingBox.addInternalPoint(Vertices[i].Pos);
    00127             }
    00128         }
    00129
    00130
    00132
    00133         virtual video::E_VERTEX_TYPE getVertexType() const
    00134         {
    00135             return T().getType();
    00136         }
    00137
    00139         virtual const core::vector3df& getPosition(u32 i) const
    00140         {
    00141             return Vertices[i].Pos;
    00142         }
    00143
    00145         virtual core::vector3df& getPosition(u32 i)
    00146         {
    00147             return Vertices[i].Pos;
    00148         }
    00149
    00151         virtual const core::vector3df& getNormal(u32 i) const
    00152         {
    00153             return Vertices[i].Normal;
    00154         }
    00155
    00157         virtual core::vector3df& getNormal(u32 i)
    00158         {
    00159             return Vertices[i].Normal;
    00160         }
    00161
    00163         virtual const core::vector2df& getTCoords(u32 i) const
    00164         {
    00165             return Vertices[i].TCoords;
    00166         }
    00167
    00169         virtual core::vector2df& getTCoords(u32 i)
    00170         {
    00171             return Vertices[i].TCoords;
    00172         }
    00173
    00174
    00176
    00180         virtual void append(const void* const vertices, u32 numVertices, const u16* const indices, u32 numIndices)
    00181         {
    00182             if (vertices == getVertices())
    00183                 return;
    00184
    00185             const u32 vertexCount = getVertexCount();
    00186             u32 i;
    00187
    00188             Vertices.reallocate(vertexCount+numVertices);
    00189             for (i=0; i<numVertices; ++i)
    00190             {
    00191                 Vertices.push_back(reinterpret_cast<const T*>(vertices)[i]);
    00192                 BoundingBox.addInternalPoint(reinterpret_cast<const T*>(vertices)[i].Pos);
    00193             }
    00194
    00195             Indices.reallocate(getIndexCount()+numIndices);
    00196             for (i=0; i<numIndices; ++i)
    00197             {
    00198                 Indices.push_back(indices[i]+vertexCount);
    00199             }
    00200         }
    00201
    00202
    00204
    00209         virtual void append(const IMeshBuffer* const other)
    00210         {
    00211             /*
    00212             if (this==other)
    00213                 return;
    00214
    00215             const u32 vertexCount = getVertexCount();
    00216             u32 i;
    00217
    00218             Vertices.reallocate(vertexCount+other->getVertexCount());
    00219             for (i=0; i<other->getVertexCount(); ++i)
    00220             {
    00221                 Vertices.push_back(reinterpret_cast<const T*>(other->getVertices())[i]);
    00222             }
    00223
    00224             Indices.reallocate(getIndexCount()+other->getIndexCount());
    00225             for (i=0; i<other->getIndexCount(); ++i)
    00226             {
    00227                 Indices.push_back(other->getIndices()[i]+vertexCount);
    00228             }
    00229             BoundingBox.addInternalBox(other->getBoundingBox());
    00230             */
    00231         }
    00232
    00233
    00235         virtual E_HARDWARE_MAPPING getHardwareMappingHint_Vertex() const
    00236         {
    00237             return MappingHint_Vertex;
    00238         }
    00239
    00241         virtual E_HARDWARE_MAPPING getHardwareMappingHint_Index() const
    00242         {
    00243             return MappingHint_Index;
    00244         }
    00245
    00247         virtual void setHardwareMappingHint( E_HARDWARE_MAPPING NewMappingHint, E_BUFFER_TYPE Buffer=EBT_VERTEX_AND_INDEX )
    00248         {
    00249             if (Buffer==EBT_VERTEX_AND_INDEX || Buffer==EBT_VERTEX)
    00250                 MappingHint_Vertex=NewMappingHint;
    00251             if (Buffer==EBT_VERTEX_AND_INDEX || Buffer==EBT_INDEX)
    00252                 MappingHint_Index=NewMappingHint;
    00253         }
    00254
    00255
    00257         virtual void setDirty(E_BUFFER_TYPE Buffer=EBT_VERTEX_AND_INDEX)
    00258         {
    00259             if (Buffer==EBT_VERTEX_AND_INDEX ||Buffer==EBT_VERTEX)
    00260                 ++ChangedID_Vertex;
    00261             if (Buffer==EBT_VERTEX_AND_INDEX || Buffer==EBT_INDEX)
    00262                 ++ChangedID_Index;
    00263         }
    00264
    00266
    00267         virtual u32 getChangedID_Vertex() const {return ChangedID_Vertex;}
    00268
    00270
    00271         virtual u32 getChangedID_Index() const {return ChangedID_Index;}
    00272
    00273         u32 ChangedID_Vertex;
    00274         u32 ChangedID_Index;
    00275
    00277         E_HARDWARE_MAPPING MappingHint_Vertex;
    00278         E_HARDWARE_MAPPING MappingHint_Index;
    00279
    00281         video::SMaterial Material;
    00283         core::array<T> Vertices;
    00285         core::array<u16> Indices;
    00287         core::aabbox3d<f32> BoundingBox;
    00288     };
    00289
    00291     typedef CMeshBuffer<video::S3DVertex> SMeshBuffer;
    00293     typedef CMeshBuffer<video::S3DVertex2TCoords> SMeshBufferLightMap;
    00295     typedef CMeshBuffer<video::S3DVertexTangents> SMeshBufferTangents;
    00296 } // end namespace scene
    00297 } // end namespace irr
    00298
    00299 #endif
    00300
    00301