1 // Copyright (C) 2002-2012 Nikolaus Gebhardt
\r
2 // This file is part of the "Irrlicht Engine".
\r
3 // For conditions of distribution and use, see copyright notice in irrlicht.h
\r
5 #ifndef __T_MESH_BUFFER_H_INCLUDED__
\r
6 #define __T_MESH_BUFFER_H_INCLUDED__
\r
8 #include "irrArray.h"
\r
9 #include "IMeshBuffer.h"
\r
15 //! Template implementation of the IMeshBuffer interface
\r
17 class CMeshBuffer : public IMeshBuffer
\r
20 //! Default constructor for empty meshbuffer
\r
22 : ChangedID_Vertex(1), ChangedID_Index(1)
\r
23 , MappingHint_Vertex(EHM_NEVER), MappingHint_Index(EHM_NEVER)
\r
24 , PrimitiveType(EPT_TRIANGLES)
\r
27 setDebugName("CMeshBuffer");
\r
32 //! Get material of this meshbuffer
\r
33 /** \return Material of this buffer */
\r
34 virtual const video::SMaterial& getMaterial() const _IRR_OVERRIDE_
\r
40 //! Get material of this meshbuffer
\r
41 /** \return Material of this buffer */
\r
42 virtual video::SMaterial& getMaterial() _IRR_OVERRIDE_
\r
48 //! Get pointer to vertices
\r
49 /** \return Pointer to vertices. */
\r
50 virtual const void* getVertices() const _IRR_OVERRIDE_
\r
52 return Vertices.const_pointer();
\r
56 //! Get pointer to vertices
\r
57 /** \return Pointer to vertices. */
\r
58 virtual void* getVertices() _IRR_OVERRIDE_
\r
60 return Vertices.pointer();
\r
64 //! Get number of vertices
\r
65 /** \return Number of vertices. */
\r
66 virtual u32 getVertexCount() const _IRR_OVERRIDE_
\r
68 return Vertices.size();
\r
71 //! Get type of index data which is stored in this meshbuffer.
\r
72 /** \return Index type of this buffer. */
\r
73 virtual video::E_INDEX_TYPE getIndexType() const _IRR_OVERRIDE_
\r
75 return video::EIT_16BIT;
\r
78 //! Get pointer to indices
\r
79 /** \return Pointer to indices. */
\r
80 virtual const u16* getIndices() const _IRR_OVERRIDE_
\r
82 return Indices.const_pointer();
\r
86 //! Get pointer to indices
\r
87 /** \return Pointer to indices. */
\r
88 virtual u16* getIndices() _IRR_OVERRIDE_
\r
90 return Indices.pointer();
\r
94 //! Get number of indices
\r
95 /** \return Number of indices. */
\r
96 virtual u32 getIndexCount() const _IRR_OVERRIDE_
\r
98 return Indices.size();
\r
102 //! Get the axis aligned bounding box
\r
103 /** \return Axis aligned bounding box of this buffer. */
\r
104 virtual const core::aabbox3d<f32>& getBoundingBox() const _IRR_OVERRIDE_
\r
106 return BoundingBox;
\r
110 //! Set the axis aligned bounding box
\r
111 /** \param box New axis aligned bounding box for this buffer. */
\r
112 //! set user axis aligned bounding box
\r
113 virtual void setBoundingBox(const core::aabbox3df& box) _IRR_OVERRIDE_
\r
119 //! Recalculate the bounding box.
\r
120 /** should be called if the mesh changed. */
\r
121 virtual void recalculateBoundingBox() _IRR_OVERRIDE_
\r
123 if (!Vertices.empty())
\r
125 BoundingBox.reset(Vertices[0].Pos);
\r
126 const irr::u32 vsize = Vertices.size();
\r
127 for (u32 i=1; i<vsize; ++i)
\r
128 BoundingBox.addInternalPoint(Vertices[i].Pos);
\r
131 BoundingBox.reset(0,0,0);
\r
136 //! Get type of vertex data stored in this buffer.
\r
137 /** \return Type of vertex data. */
\r
138 virtual video::E_VERTEX_TYPE getVertexType() const _IRR_OVERRIDE_
\r
140 return T::getType();
\r
143 //! returns position of vertex i
\r
144 virtual const core::vector3df& getPosition(u32 i) const _IRR_OVERRIDE_
\r
146 return Vertices[i].Pos;
\r
149 //! returns position of vertex i
\r
150 virtual core::vector3df& getPosition(u32 i) _IRR_OVERRIDE_
\r
152 return Vertices[i].Pos;
\r
155 //! returns normal of vertex i
\r
156 virtual const core::vector3df& getNormal(u32 i) const _IRR_OVERRIDE_
\r
158 return Vertices[i].Normal;
\r
161 //! returns normal of vertex i
\r
162 virtual core::vector3df& getNormal(u32 i) _IRR_OVERRIDE_
\r
164 return Vertices[i].Normal;
\r
167 //! returns texture coord of vertex i
\r
168 virtual const core::vector2df& getTCoords(u32 i) const _IRR_OVERRIDE_
\r
170 return Vertices[i].TCoords;
\r
173 //! returns texture coord of vertex i
\r
174 virtual core::vector2df& getTCoords(u32 i) _IRR_OVERRIDE_
\r
176 return Vertices[i].TCoords;
\r
180 //! Append the vertices and indices to the current buffer
\r
181 /** Only works for compatible types, i.e. either the same type
\r
182 or the main buffer is of standard type. Otherwise, behavior is
\r
185 virtual void append(const void* const vertices, u32 numVertices, const u16* const indices, u32 numIndices) _IRR_OVERRIDE_
\r
187 if (vertices == getVertices())
\r
190 const u32 vertexCount = getVertexCount();
\r
193 Vertices.reallocate(vertexCount+numVertices);
\r
194 for (i=0; i<numVertices; ++i)
\r
196 Vertices.push_back(reinterpret_cast<const T*>(vertices)[i]);
\r
197 BoundingBox.addInternalPoint(reinterpret_cast<const T*>(vertices)[i].Pos);
\r
200 Indices.reallocate(getIndexCount()+numIndices);
\r
201 for (i=0; i<numIndices; ++i)
\r
203 Indices.push_back(indices[i]+vertexCount);
\r
208 //! Append the meshbuffer to the current buffer
\r
209 /** Only works for compatible types, i.e. either the same type
\r
210 or the main buffer is of standard type. Otherwise, behavior is
\r
212 \param other Meshbuffer to be appended to this one.
\r
214 virtual void append(const IMeshBuffer* const other) _IRR_OVERRIDE_
\r
220 const u32 vertexCount = getVertexCount();
\r
223 Vertices.reallocate(vertexCount+other->getVertexCount());
\r
224 for (i=0; i<other->getVertexCount(); ++i)
\r
226 Vertices.push_back(reinterpret_cast<const T*>(other->getVertices())[i]);
\r
229 Indices.reallocate(getIndexCount()+other->getIndexCount());
\r
230 for (i=0; i<other->getIndexCount(); ++i)
\r
232 Indices.push_back(other->getIndices()[i]+vertexCount);
\r
234 BoundingBox.addInternalBox(other->getBoundingBox());
\r
239 //! get the current hardware mapping hint
\r
240 virtual E_HARDWARE_MAPPING getHardwareMappingHint_Vertex() const _IRR_OVERRIDE_
\r
242 return MappingHint_Vertex;
\r
245 //! get the current hardware mapping hint
\r
246 virtual E_HARDWARE_MAPPING getHardwareMappingHint_Index() const _IRR_OVERRIDE_
\r
248 return MappingHint_Index;
\r
251 //! set the hardware mapping hint, for driver
\r
252 virtual void setHardwareMappingHint( E_HARDWARE_MAPPING NewMappingHint, E_BUFFER_TYPE Buffer=EBT_VERTEX_AND_INDEX ) _IRR_OVERRIDE_
\r
254 if (Buffer==EBT_VERTEX_AND_INDEX || Buffer==EBT_VERTEX)
\r
255 MappingHint_Vertex=NewMappingHint;
\r
256 if (Buffer==EBT_VERTEX_AND_INDEX || Buffer==EBT_INDEX)
\r
257 MappingHint_Index=NewMappingHint;
\r
260 //! Describe what kind of primitive geometry is used by the meshbuffer
\r
261 virtual void setPrimitiveType(E_PRIMITIVE_TYPE type) _IRR_OVERRIDE_
\r
263 PrimitiveType = type;
\r
266 //! Get the kind of primitive geometry which is used by the meshbuffer
\r
267 virtual E_PRIMITIVE_TYPE getPrimitiveType() const _IRR_OVERRIDE_
\r
269 return PrimitiveType;
\r
272 //! flags the mesh as changed, reloads hardware buffers
\r
273 virtual void setDirty(E_BUFFER_TYPE Buffer=EBT_VERTEX_AND_INDEX) _IRR_OVERRIDE_
\r
275 if (Buffer==EBT_VERTEX_AND_INDEX ||Buffer==EBT_VERTEX)
\r
276 ++ChangedID_Vertex;
\r
277 if (Buffer==EBT_VERTEX_AND_INDEX || Buffer==EBT_INDEX)
\r
281 //! Get the currently used ID for identification of changes.
\r
282 /** This shouldn't be used for anything outside the VideoDriver. */
\r
283 virtual u32 getChangedID_Vertex() const _IRR_OVERRIDE_ {return ChangedID_Vertex;}
\r
285 //! Get the currently used ID for identification of changes.
\r
286 /** This shouldn't be used for anything outside the VideoDriver. */
\r
287 virtual u32 getChangedID_Index() const _IRR_OVERRIDE_ {return ChangedID_Index;}
\r
289 u32 ChangedID_Vertex;
\r
290 u32 ChangedID_Index;
\r
292 //! hardware mapping hint
\r
293 E_HARDWARE_MAPPING MappingHint_Vertex;
\r
294 E_HARDWARE_MAPPING MappingHint_Index;
\r
296 //! Material for this meshbuffer.
\r
297 video::SMaterial Material;
\r
298 //! Vertices of this buffer
\r
299 core::array<T> Vertices;
\r
300 //! Indices into the vertices of this buffer.
\r
301 core::array<u16> Indices;
\r
302 //! Bounding box of this meshbuffer.
\r
303 core::aabbox3d<f32> BoundingBox;
\r
304 //! Primitive type used for rendering (triangles, lines, ...)
\r
305 E_PRIMITIVE_TYPE PrimitiveType;
\r
308 //! Standard meshbuffer
\r
309 typedef CMeshBuffer<video::S3DVertex> SMeshBuffer;
\r
310 //! Meshbuffer with two texture coords per vertex, e.g. for lightmaps
\r
311 typedef CMeshBuffer<video::S3DVertex2TCoords> SMeshBufferLightMap;
\r
312 //! Meshbuffer with vertices having tangents stored, e.g. for normal mapping
\r
313 typedef CMeshBuffer<video::S3DVertexTangents> SMeshBufferTangents;
\r
314 } // end namespace scene
\r
315 } // end namespace irr
\r