#ifndef __T_MESH_BUFFER_H_INCLUDED__
#define __T_MESH_BUFFER_H_INCLUDED__
#include "irrArray.h"
#include "IMeshBuffer.h"
namespace irr
{
namespace scene
{
template <class T>
class CMeshBuffer : public IMeshBuffer
{
public:
CMeshBuffer():ChangedID_Vertex(1),ChangedID_Index(1),MappingHint_Vertex(EHM_NEVER), MappingHint_Index(EHM_NEVER)
{
#ifdef _DEBUG
setDebugName("SMeshBuffer");
#endif
}
virtual const video::SMaterial& getMaterial() const
{
return Material;
}
virtual video::SMaterial& getMaterial()
{
return Material;
}
virtual const void* getVertices() const
{
return Vertices.const_pointer();
}
virtual void* getVertices()
{
return Vertices.pointer();
}
virtual u32 getVertexCount() const
{
return Vertices.size();
}
virtual video::E_INDEX_TYPE getIndexType() const
{
return video::EIT_16BIT;
}
virtual const u16* getIndices() const
{
return Indices.const_pointer();
}
virtual u16* getIndices()
{
return Indices.pointer();
}
virtual u32 getIndexCount() const
{
return Indices.size();
}
virtual const core::aabbox3d<f32>& getBoundingBox() const
{
return BoundingBox;
}
virtual void setBoundingBox(const core::aabbox3df& box)
{
BoundingBox = box;
}
virtual void recalculateBoundingBox()
{
if (Vertices.empty())
BoundingBox.reset(0,0,0);
else
{
BoundingBox.reset(Vertices[0].Pos);
for (u32 i=1; i<Vertices.size(); ++i)
BoundingBox.addInternalPoint(Vertices[i].Pos);
}
}
virtual video::E_VERTEX_TYPE getVertexType() const
{
return T().getType();
}
virtual const core::vector3df& getPosition(u32 i) const
{
return Vertices[i].Pos;
}
virtual core::vector3df& getPosition(u32 i)
{
return Vertices[i].Pos;
}
virtual const core::vector3df& getNormal(u32 i) const
{
return Vertices[i].Normal;
}
virtual core::vector3df& getNormal(u32 i)
{
return Vertices[i].Normal;
}
virtual const core::vector2df& getTCoords(u32 i) const
{
return Vertices[i].TCoords;
}
virtual core::vector2df& getTCoords(u32 i)
{
return Vertices[i].TCoords;
}
virtual void append(const void* const vertices, u32 numVertices, const u16* const indices, u32 numIndices)
{
if (vertices == getVertices())
return;
const u32 vertexCount = getVertexCount();
u32 i;
Vertices.reallocate(vertexCount+numVertices);
for (i=0; i<numVertices; ++i)
{
Vertices.push_back(reinterpret_cast<const T*>(vertices)[i]);
BoundingBox.addInternalPoint(reinterpret_cast<const T*>(vertices)[i].Pos);
}
Indices.reallocate(getIndexCount()+numIndices);
for (i=0; i<numIndices; ++i)
{
Indices.push_back(indices[i]+vertexCount);
}
}
virtual void append(const IMeshBuffer* const other)
{
}
virtual E_HARDWARE_MAPPING getHardwareMappingHint_Vertex() const
{
return MappingHint_Vertex;
}
virtual E_HARDWARE_MAPPING getHardwareMappingHint_Index() const
{
return MappingHint_Index;
}
virtual void setHardwareMappingHint( E_HARDWARE_MAPPING NewMappingHint, E_BUFFER_TYPE Buffer=EBT_VERTEX_AND_INDEX )
{
if (Buffer==EBT_VERTEX_AND_INDEX || Buffer==EBT_VERTEX)
MappingHint_Vertex=NewMappingHint;
if (Buffer==EBT_VERTEX_AND_INDEX || Buffer==EBT_INDEX)
MappingHint_Index=NewMappingHint;
}
virtual void setDirty(E_BUFFER_TYPE Buffer=EBT_VERTEX_AND_INDEX)
{
if (Buffer==EBT_VERTEX_AND_INDEX ||Buffer==EBT_VERTEX)
++ChangedID_Vertex;
if (Buffer==EBT_VERTEX_AND_INDEX || Buffer==EBT_INDEX)
++ChangedID_Index;
}
virtual u32 getChangedID_Vertex() const {return ChangedID_Vertex;}
virtual u32 getChangedID_Index() const {return ChangedID_Index;}
u32 ChangedID_Vertex;
u32 ChangedID_Index;
E_HARDWARE_MAPPING MappingHint_Vertex;
E_HARDWARE_MAPPING MappingHint_Index;
video::SMaterial Material;
core::array<T> Vertices;
core::array<u16> Indices;
core::aabbox3d<f32> BoundingBox;
};
typedef CMeshBuffer<video::S3DVertex> SMeshBuffer;
typedef CMeshBuffer<video::S3DVertex2TCoords> SMeshBufferLightMap;
typedef CMeshBuffer<video::S3DVertexTangents> SMeshBufferTangents;
}
}
#endif