]> git.lizzy.rs Git - irrlicht.git/blob - include/CMeshBuffer.h
Add back LightManager
[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                         , HWBuffer(NULL)\r
25                         , PrimitiveType(EPT_TRIANGLES)\r
26                 {\r
27                         #ifdef _DEBUG\r
28                         setDebugName("CMeshBuffer");\r
29                         #endif\r
30                 }\r
31 \r
32 \r
33                 //! Get material of this meshbuffer\r
34                 /** \return Material of this buffer */\r
35                 const video::SMaterial& getMaterial() const override\r
36                 {\r
37                         return Material;\r
38                 }\r
39 \r
40 \r
41                 //! Get material of this meshbuffer\r
42                 /** \return Material of this buffer */\r
43                 video::SMaterial& getMaterial() override\r
44                 {\r
45                         return Material;\r
46                 }\r
47 \r
48 \r
49                 //! Get pointer to vertices\r
50                 /** \return Pointer to vertices. */\r
51                 const void* getVertices() const override\r
52                 {\r
53                         return Vertices.const_pointer();\r
54                 }\r
55 \r
56 \r
57                 //! Get pointer to vertices\r
58                 /** \return Pointer to vertices. */\r
59                 void* getVertices() override\r
60                 {\r
61                         return Vertices.pointer();\r
62                 }\r
63 \r
64 \r
65                 //! Get number of vertices\r
66                 /** \return Number of vertices. */\r
67                 u32 getVertexCount() const override\r
68                 {\r
69                         return Vertices.size();\r
70                 }\r
71 \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
75                 {\r
76                         return video::EIT_16BIT;\r
77                 }\r
78 \r
79                 //! Get pointer to indices\r
80                 /** \return Pointer to indices. */\r
81                 const u16* getIndices() const override\r
82                 {\r
83                         return Indices.const_pointer();\r
84                 }\r
85 \r
86 \r
87                 //! Get pointer to indices\r
88                 /** \return Pointer to indices. */\r
89                 u16* getIndices() override\r
90                 {\r
91                         return Indices.pointer();\r
92                 }\r
93 \r
94 \r
95                 //! Get number of indices\r
96                 /** \return Number of indices. */\r
97                 u32 getIndexCount() const override\r
98                 {\r
99                         return Indices.size();\r
100                 }\r
101 \r
102 \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
106                 {\r
107                         return BoundingBox;\r
108                 }\r
109 \r
110 \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
115                 {\r
116                         BoundingBox = box;\r
117                 }\r
118 \r
119 \r
120                 //! Recalculate the bounding box.\r
121                 /** should be called if the mesh changed. */\r
122                 void recalculateBoundingBox() override\r
123                 {\r
124                         if (!Vertices.empty())\r
125                         {\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
130                         }\r
131                         else\r
132                                 BoundingBox.reset(0,0,0);\r
133 \r
134                 }\r
135 \r
136 \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
140                 {\r
141                         return T::getType();\r
142                 }\r
143 \r
144                 //! returns position of vertex i\r
145                 const core::vector3df& getPosition(u32 i) const override\r
146                 {\r
147                         return Vertices[i].Pos;\r
148                 }\r
149 \r
150                 //! returns position of vertex i\r
151                 core::vector3df& getPosition(u32 i) override\r
152                 {\r
153                         return Vertices[i].Pos;\r
154                 }\r
155 \r
156                 //! returns normal of vertex i\r
157                 const core::vector3df& getNormal(u32 i) const override\r
158                 {\r
159                         return Vertices[i].Normal;\r
160                 }\r
161 \r
162                 //! returns normal of vertex i\r
163                 core::vector3df& getNormal(u32 i) override\r
164                 {\r
165                         return Vertices[i].Normal;\r
166                 }\r
167 \r
168                 //! returns texture coord of vertex i\r
169                 const core::vector2df& getTCoords(u32 i) const override\r
170                 {\r
171                         return Vertices[i].TCoords;\r
172                 }\r
173 \r
174                 //! returns texture coord of vertex i\r
175                 core::vector2df& getTCoords(u32 i) override\r
176                 {\r
177                         return Vertices[i].TCoords;\r
178                 }\r
179 \r
180 \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
184                 undefined.\r
185                 */\r
186                 void append(const void* const vertices, u32 numVertices, const u16* const indices, u32 numIndices) override\r
187                 {\r
188                         if (vertices == getVertices())\r
189                                 return;\r
190 \r
191                         const u32 vertexCount = getVertexCount();\r
192                         u32 i;\r
193 \r
194                         Vertices.reallocate(vertexCount+numVertices);\r
195                         for (i=0; i<numVertices; ++i)\r
196                         {\r
197                                 Vertices.push_back(static_cast<const T*>(vertices)[i]);\r
198                                 BoundingBox.addInternalPoint(static_cast<const T*>(vertices)[i].Pos);\r
199                         }\r
200 \r
201                         Indices.reallocate(getIndexCount()+numIndices);\r
202                         for (i=0; i<numIndices; ++i)\r
203                         {\r
204                                 Indices.push_back(indices[i]+vertexCount);\r
205                         }\r
206                 }\r
207 \r
208 \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
212                 undefined.\r
213                 \param other Meshbuffer to be appended to this one.\r
214                 */\r
215                 void append(const IMeshBuffer* const other) override\r
216                 {\r
217                         /*\r
218                         if (this==other)\r
219                                 return;\r
220 \r
221                         const u32 vertexCount = getVertexCount();\r
222                         u32 i;\r
223 \r
224                         Vertices.reallocate(vertexCount+other->getVertexCount());\r
225                         for (i=0; i<other->getVertexCount(); ++i)\r
226                         {\r
227                                 Vertices.push_back(reinterpret_cast<const T*>(other->getVertices())[i]);\r
228                         }\r
229 \r
230                         Indices.reallocate(getIndexCount()+other->getIndexCount());\r
231                         for (i=0; i<other->getIndexCount(); ++i)\r
232                         {\r
233                                 Indices.push_back(other->getIndices()[i]+vertexCount);\r
234                         }\r
235                         BoundingBox.addInternalBox(other->getBoundingBox());\r
236                         */\r
237                 }\r
238 \r
239 \r
240                 //! get the current hardware mapping hint\r
241                 E_HARDWARE_MAPPING getHardwareMappingHint_Vertex() const override\r
242                 {\r
243                         return MappingHint_Vertex;\r
244                 }\r
245 \r
246                 //! get the current hardware mapping hint\r
247                 E_HARDWARE_MAPPING getHardwareMappingHint_Index() const override\r
248                 {\r
249                         return MappingHint_Index;\r
250                 }\r
251 \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
254                 {\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
259                 }\r
260 \r
261                 //! Describe what kind of primitive geometry is used by the meshbuffer\r
262                 void setPrimitiveType(E_PRIMITIVE_TYPE type) override\r
263                 {\r
264                         PrimitiveType = type;\r
265                 }\r
266 \r
267                 //! Get the kind of primitive geometry which is used by the meshbuffer\r
268                 E_PRIMITIVE_TYPE getPrimitiveType() const override\r
269                 {\r
270                         return PrimitiveType;\r
271                 }\r
272 \r
273                 //! flags the mesh as changed, reloads hardware buffers\r
274                 void setDirty(E_BUFFER_TYPE Buffer=EBT_VERTEX_AND_INDEX) override\r
275                 {\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
279                                 ++ChangedID_Index;\r
280                 }\r
281 \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
285 \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
289 \r
290                 void setHWBuffer(void *ptr) const override {\r
291                         HWBuffer = ptr;\r
292                 }\r
293 \r
294                 void *getHWBuffer() const override {\r
295                         return HWBuffer;\r
296                 }\r
297 \r
298 \r
299                 u32 ChangedID_Vertex;\r
300                 u32 ChangedID_Index;\r
301 \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
306 \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
317         };\r
318 \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
327 \r
328 #endif\r
329 \r
330 \r