]> git.lizzy.rs Git - irrlicht.git/blob - include/CMeshBuffer.h
Fix COSOperator::getSystemMemory
[irrlicht.git] / include / CMeshBuffer.h
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
4 \r
5 #ifndef __T_MESH_BUFFER_H_INCLUDED__\r
6 #define __T_MESH_BUFFER_H_INCLUDED__\r
7 \r
8 #include "irrArray.h"\r
9 #include "IMeshBuffer.h"\r
10 \r
11 namespace irr\r
12 {\r
13 namespace scene\r
14 {\r
15         //! Template implementation of the IMeshBuffer interface\r
16         template <class T>\r
17         class CMeshBuffer : public IMeshBuffer\r
18         {\r
19         public:\r
20                 //! Default constructor for empty meshbuffer\r
21                 CMeshBuffer()\r
22                         : ChangedID_Vertex(1), ChangedID_Index(1)\r
23                         , MappingHint_Vertex(EHM_NEVER), MappingHint_Index(EHM_NEVER)\r
24                         , PrimitiveType(EPT_TRIANGLES)\r
25                 {\r
26                         #ifdef _DEBUG\r
27                         setDebugName("CMeshBuffer");\r
28                         #endif\r
29                 }\r
30 \r
31 \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
35                 {\r
36                         return Material;\r
37                 }\r
38 \r
39 \r
40                 //! Get material of this meshbuffer\r
41                 /** \return Material of this buffer */\r
42                 virtual video::SMaterial& getMaterial() _IRR_OVERRIDE_\r
43                 {\r
44                         return Material;\r
45                 }\r
46 \r
47 \r
48                 //! Get pointer to vertices\r
49                 /** \return Pointer to vertices. */\r
50                 virtual const void* getVertices() const _IRR_OVERRIDE_\r
51                 {\r
52                         return Vertices.const_pointer();\r
53                 }\r
54 \r
55 \r
56                 //! Get pointer to vertices\r
57                 /** \return Pointer to vertices. */\r
58                 virtual void* getVertices() _IRR_OVERRIDE_\r
59                 {\r
60                         return Vertices.pointer();\r
61                 }\r
62 \r
63 \r
64                 //! Get number of vertices\r
65                 /** \return Number of vertices. */\r
66                 virtual u32 getVertexCount() const _IRR_OVERRIDE_\r
67                 {\r
68                         return Vertices.size();\r
69                 }\r
70 \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
74                 {\r
75                         return video::EIT_16BIT;\r
76                 }\r
77 \r
78                 //! Get pointer to indices\r
79                 /** \return Pointer to indices. */\r
80                 virtual const u16* getIndices() const _IRR_OVERRIDE_\r
81                 {\r
82                         return Indices.const_pointer();\r
83                 }\r
84 \r
85 \r
86                 //! Get pointer to indices\r
87                 /** \return Pointer to indices. */\r
88                 virtual u16* getIndices() _IRR_OVERRIDE_\r
89                 {\r
90                         return Indices.pointer();\r
91                 }\r
92 \r
93 \r
94                 //! Get number of indices\r
95                 /** \return Number of indices. */\r
96                 virtual u32 getIndexCount() const _IRR_OVERRIDE_\r
97                 {\r
98                         return Indices.size();\r
99                 }\r
100 \r
101 \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
105                 {\r
106                         return BoundingBox;\r
107                 }\r
108 \r
109 \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
114                 {\r
115                         BoundingBox = box;\r
116                 }\r
117 \r
118 \r
119                 //! Recalculate the bounding box.\r
120                 /** should be called if the mesh changed. */\r
121                 virtual void recalculateBoundingBox() _IRR_OVERRIDE_\r
122                 {\r
123                         if (!Vertices.empty())\r
124                         {\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
129                         }\r
130                         else\r
131                                 BoundingBox.reset(0,0,0);\r
132 \r
133                 }\r
134 \r
135 \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
139                 {\r
140                         return T::getType();\r
141                 }\r
142 \r
143                 //! returns position of vertex i\r
144                 virtual const core::vector3df& getPosition(u32 i) const _IRR_OVERRIDE_\r
145                 {\r
146                         return Vertices[i].Pos;\r
147                 }\r
148 \r
149                 //! returns position of vertex i\r
150                 virtual core::vector3df& getPosition(u32 i) _IRR_OVERRIDE_\r
151                 {\r
152                         return Vertices[i].Pos;\r
153                 }\r
154 \r
155                 //! returns normal of vertex i\r
156                 virtual const core::vector3df& getNormal(u32 i) const _IRR_OVERRIDE_\r
157                 {\r
158                         return Vertices[i].Normal;\r
159                 }\r
160 \r
161                 //! returns normal of vertex i\r
162                 virtual core::vector3df& getNormal(u32 i) _IRR_OVERRIDE_\r
163                 {\r
164                         return Vertices[i].Normal;\r
165                 }\r
166 \r
167                 //! returns texture coord of vertex i\r
168                 virtual const core::vector2df& getTCoords(u32 i) const _IRR_OVERRIDE_\r
169                 {\r
170                         return Vertices[i].TCoords;\r
171                 }\r
172 \r
173                 //! returns texture coord of vertex i\r
174                 virtual core::vector2df& getTCoords(u32 i) _IRR_OVERRIDE_\r
175                 {\r
176                         return Vertices[i].TCoords;\r
177                 }\r
178 \r
179 \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
183                 undefined.\r
184                 */\r
185                 virtual void append(const void* const vertices, u32 numVertices, const u16* const indices, u32 numIndices) _IRR_OVERRIDE_\r
186                 {\r
187                         if (vertices == getVertices())\r
188                                 return;\r
189 \r
190                         const u32 vertexCount = getVertexCount();\r
191                         u32 i;\r
192 \r
193                         Vertices.reallocate(vertexCount+numVertices);\r
194                         for (i=0; i<numVertices; ++i)\r
195                         {\r
196                                 Vertices.push_back(static_cast<const T*>(vertices)[i]);\r
197                                 BoundingBox.addInternalPoint(static_cast<const T*>(vertices)[i].Pos);\r
198                         }\r
199 \r
200                         Indices.reallocate(getIndexCount()+numIndices);\r
201                         for (i=0; i<numIndices; ++i)\r
202                         {\r
203                                 Indices.push_back(indices[i]+vertexCount);\r
204                         }\r
205                 }\r
206 \r
207 \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
211                 undefined.\r
212                 \param other Meshbuffer to be appended to this one.\r
213                 */\r
214                 virtual void append(const IMeshBuffer* const other) _IRR_OVERRIDE_\r
215                 {\r
216                         /*\r
217                         if (this==other)\r
218                                 return;\r
219 \r
220                         const u32 vertexCount = getVertexCount();\r
221                         u32 i;\r
222 \r
223                         Vertices.reallocate(vertexCount+other->getVertexCount());\r
224                         for (i=0; i<other->getVertexCount(); ++i)\r
225                         {\r
226                                 Vertices.push_back(reinterpret_cast<const T*>(other->getVertices())[i]);\r
227                         }\r
228 \r
229                         Indices.reallocate(getIndexCount()+other->getIndexCount());\r
230                         for (i=0; i<other->getIndexCount(); ++i)\r
231                         {\r
232                                 Indices.push_back(other->getIndices()[i]+vertexCount);\r
233                         }\r
234                         BoundingBox.addInternalBox(other->getBoundingBox());\r
235                         */\r
236                 }\r
237 \r
238 \r
239                 //! get the current hardware mapping hint\r
240                 virtual E_HARDWARE_MAPPING getHardwareMappingHint_Vertex() const _IRR_OVERRIDE_\r
241                 {\r
242                         return MappingHint_Vertex;\r
243                 }\r
244 \r
245                 //! get the current hardware mapping hint\r
246                 virtual E_HARDWARE_MAPPING getHardwareMappingHint_Index() const _IRR_OVERRIDE_\r
247                 {\r
248                         return MappingHint_Index;\r
249                 }\r
250 \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
253                 {\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
258                 }\r
259 \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
262                 {\r
263                         PrimitiveType = type;\r
264                 }\r
265 \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
268                 {\r
269                         return PrimitiveType;\r
270                 }\r
271 \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
274                 {\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
278                                 ++ChangedID_Index;\r
279                 }\r
280 \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
284 \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
288 \r
289                 u32 ChangedID_Vertex;\r
290                 u32 ChangedID_Index;\r
291 \r
292                 //! hardware mapping hint\r
293                 E_HARDWARE_MAPPING MappingHint_Vertex;\r
294                 E_HARDWARE_MAPPING MappingHint_Index;\r
295 \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
306         };\r
307 \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
316 \r
317 #endif\r
318 \r
319 \r