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
25 , PrimitiveType(EPT_TRIANGLES)
\r
28 setDebugName("CMeshBuffer");
\r
33 //! Get material of this meshbuffer
\r
34 /** \return Material of this buffer */
\r
35 const video::SMaterial& getMaterial() const override
\r
41 //! Get material of this meshbuffer
\r
42 /** \return Material of this buffer */
\r
43 video::SMaterial& getMaterial() override
\r
49 //! Get pointer to vertices
\r
50 /** \return Pointer to vertices. */
\r
51 const void* getVertices() const override
\r
53 return Vertices.const_pointer();
\r
57 //! Get pointer to vertices
\r
58 /** \return Pointer to vertices. */
\r
59 void* getVertices() override
\r
61 return Vertices.pointer();
\r
65 //! Get number of vertices
\r
66 /** \return Number of vertices. */
\r
67 u32 getVertexCount() const override
\r
69 return Vertices.size();
\r
72 //! Get type of index data which is stored in this meshbuffer.
\r
73 /** \return Index type of this buffer. */
\r
74 video::E_INDEX_TYPE getIndexType() const override
\r
76 return video::EIT_16BIT;
\r
79 //! Get pointer to indices
\r
80 /** \return Pointer to indices. */
\r
81 const u16* getIndices() const override
\r
83 return Indices.const_pointer();
\r
87 //! Get pointer to indices
\r
88 /** \return Pointer to indices. */
\r
89 u16* getIndices() override
\r
91 return Indices.pointer();
\r
95 //! Get number of indices
\r
96 /** \return Number of indices. */
\r
97 u32 getIndexCount() const override
\r
99 return Indices.size();
\r
103 //! Get the axis aligned bounding box
\r
104 /** \return Axis aligned bounding box of this buffer. */
\r
105 const core::aabbox3d<f32>& getBoundingBox() const override
\r
107 return BoundingBox;
\r
111 //! Set the axis aligned bounding box
\r
112 /** \param box New axis aligned bounding box for this buffer. */
\r
113 //! set user axis aligned bounding box
\r
114 void setBoundingBox(const core::aabbox3df& box) override
\r
120 //! Recalculate the bounding box.
\r
121 /** should be called if the mesh changed. */
\r
122 void recalculateBoundingBox() override
\r
124 if (!Vertices.empty())
\r
126 BoundingBox.reset(Vertices[0].Pos);
\r
127 const irr::u32 vsize = Vertices.size();
\r
128 for (u32 i=1; i<vsize; ++i)
\r
129 BoundingBox.addInternalPoint(Vertices[i].Pos);
\r
132 BoundingBox.reset(0,0,0);
\r
137 //! Get type of vertex data stored in this buffer.
\r
138 /** \return Type of vertex data. */
\r
139 video::E_VERTEX_TYPE getVertexType() const override
\r
141 return T::getType();
\r
144 //! returns position of vertex i
\r
145 const core::vector3df& getPosition(u32 i) const override
\r
147 return Vertices[i].Pos;
\r
150 //! returns position of vertex i
\r
151 core::vector3df& getPosition(u32 i) override
\r
153 return Vertices[i].Pos;
\r
156 //! returns normal of vertex i
\r
157 const core::vector3df& getNormal(u32 i) const override
\r
159 return Vertices[i].Normal;
\r
162 //! returns normal of vertex i
\r
163 core::vector3df& getNormal(u32 i) override
\r
165 return Vertices[i].Normal;
\r
168 //! returns texture coord of vertex i
\r
169 const core::vector2df& getTCoords(u32 i) const override
\r
171 return Vertices[i].TCoords;
\r
174 //! returns texture coord of vertex i
\r
175 core::vector2df& getTCoords(u32 i) override
\r
177 return Vertices[i].TCoords;
\r
181 //! Append the vertices and indices to the current buffer
\r
182 /** Only works for compatible types, i.e. either the same type
\r
183 or the main buffer is of standard type. Otherwise, behavior is
\r
186 void append(const void* const vertices, u32 numVertices, const u16* const indices, u32 numIndices) override
\r
188 if (vertices == getVertices())
\r
191 const u32 vertexCount = getVertexCount();
\r
194 Vertices.reallocate(vertexCount+numVertices);
\r
195 for (i=0; i<numVertices; ++i)
\r
197 Vertices.push_back(static_cast<const T*>(vertices)[i]);
\r
198 BoundingBox.addInternalPoint(static_cast<const T*>(vertices)[i].Pos);
\r
201 Indices.reallocate(getIndexCount()+numIndices);
\r
202 for (i=0; i<numIndices; ++i)
\r
204 Indices.push_back(indices[i]+vertexCount);
\r
209 //! Append the meshbuffer to the current buffer
\r
210 /** Only works for compatible types, i.e. either the same type
\r
211 or the main buffer is of standard type. Otherwise, behavior is
\r
213 \param other Meshbuffer to be appended to this one.
\r
215 void append(const IMeshBuffer* const other) override
\r
221 const u32 vertexCount = getVertexCount();
\r
224 Vertices.reallocate(vertexCount+other->getVertexCount());
\r
225 for (i=0; i<other->getVertexCount(); ++i)
\r
227 Vertices.push_back(reinterpret_cast<const T*>(other->getVertices())[i]);
\r
230 Indices.reallocate(getIndexCount()+other->getIndexCount());
\r
231 for (i=0; i<other->getIndexCount(); ++i)
\r
233 Indices.push_back(other->getIndices()[i]+vertexCount);
\r
235 BoundingBox.addInternalBox(other->getBoundingBox());
\r
240 //! get the current hardware mapping hint
\r
241 E_HARDWARE_MAPPING getHardwareMappingHint_Vertex() const override
\r
243 return MappingHint_Vertex;
\r
246 //! get the current hardware mapping hint
\r
247 E_HARDWARE_MAPPING getHardwareMappingHint_Index() const override
\r
249 return MappingHint_Index;
\r
252 //! set the hardware mapping hint, for driver
\r
253 void setHardwareMappingHint( E_HARDWARE_MAPPING NewMappingHint, E_BUFFER_TYPE Buffer=EBT_VERTEX_AND_INDEX ) override
\r
255 if (Buffer==EBT_VERTEX_AND_INDEX || Buffer==EBT_VERTEX)
\r
256 MappingHint_Vertex=NewMappingHint;
\r
257 if (Buffer==EBT_VERTEX_AND_INDEX || Buffer==EBT_INDEX)
\r
258 MappingHint_Index=NewMappingHint;
\r
261 //! Describe what kind of primitive geometry is used by the meshbuffer
\r
262 void setPrimitiveType(E_PRIMITIVE_TYPE type) override
\r
264 PrimitiveType = type;
\r
267 //! Get the kind of primitive geometry which is used by the meshbuffer
\r
268 E_PRIMITIVE_TYPE getPrimitiveType() const override
\r
270 return PrimitiveType;
\r
273 //! flags the mesh as changed, reloads hardware buffers
\r
274 void setDirty(E_BUFFER_TYPE Buffer=EBT_VERTEX_AND_INDEX) override
\r
276 if (Buffer==EBT_VERTEX_AND_INDEX ||Buffer==EBT_VERTEX)
\r
277 ++ChangedID_Vertex;
\r
278 if (Buffer==EBT_VERTEX_AND_INDEX || Buffer==EBT_INDEX)
\r
282 //! Get the currently used ID for identification of changes.
\r
283 /** This shouldn't be used for anything outside the VideoDriver. */
\r
284 u32 getChangedID_Vertex() const override {return ChangedID_Vertex;}
\r
286 //! Get the currently used ID for identification of changes.
\r
287 /** This shouldn't be used for anything outside the VideoDriver. */
\r
288 u32 getChangedID_Index() const override {return ChangedID_Index;}
\r
290 void setHWBuffer(void *ptr) const override {
\r
294 void *getHWBuffer() const override {
\r
299 u32 ChangedID_Vertex;
\r
300 u32 ChangedID_Index;
\r
302 //! hardware mapping hint
\r
303 E_HARDWARE_MAPPING MappingHint_Vertex;
\r
304 E_HARDWARE_MAPPING MappingHint_Index;
\r
305 mutable void *HWBuffer;
\r
307 //! Material for this meshbuffer.
\r
308 video::SMaterial Material;
\r
309 //! Vertices of this buffer
\r
310 core::array<T> Vertices;
\r
311 //! Indices into the vertices of this buffer.
\r
312 core::array<u16> Indices;
\r
313 //! Bounding box of this meshbuffer.
\r
314 core::aabbox3d<f32> BoundingBox;
\r
315 //! Primitive type used for rendering (triangles, lines, ...)
\r
316 E_PRIMITIVE_TYPE PrimitiveType;
\r
319 //! Standard meshbuffer
\r
320 typedef CMeshBuffer<video::S3DVertex> SMeshBuffer;
\r
321 //! Meshbuffer with two texture coords per vertex, e.g. for lightmaps
\r
322 typedef CMeshBuffer<video::S3DVertex2TCoords> SMeshBufferLightMap;
\r
323 //! Meshbuffer with vertices having tangents stored, e.g. for normal mapping
\r
324 typedef CMeshBuffer<video::S3DVertexTangents> SMeshBufferTangents;
\r
325 } // end namespace scene
\r
326 } // end namespace irr
\r