1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
|
// Copyright (C) 2002-2012 Nikolaus Gebhardt
// This file is part of the "Irrlicht Engine".
// For conditions of distribution and use, see copyright notice in irrlicht.h
#ifndef __I_MESH_MANIPULATOR_H_INCLUDED__
#define __I_MESH_MANIPULATOR_H_INCLUDED__
#include "IReferenceCounted.h"
#include "vector3d.h"
#include "aabbox3d.h"
#include "matrix4.h"
#include "IAnimatedMesh.h"
#include "IMeshBuffer.h"
#include "SVertexManipulator.h"
namespace irr
{
namespace scene
{
struct SMesh;
//! An interface for easy manipulation of meshes.
/** Scale, set alpha value, flip surfaces, and so on. This exists for
fixing problems with wrong imported or exported meshes quickly after
loading. It is not intended for doing mesh modifications and/or
animations during runtime.
*/
class IMeshManipulator : public virtual IReferenceCounted
{
public:
//! Flips the direction of surfaces.
/** Changes backfacing triangles to frontfacing
triangles and vice versa.
\param mesh Mesh on which the operation is performed. */
virtual void flipSurfaces(IMesh* mesh) const = 0;
//! Sets the alpha vertex color value of the whole mesh to a new value.
/** \param mesh Mesh on which the operation is performed.
\param alpha New alpha value. Must be a value between 0 and 255. */
void setVertexColorAlpha(IMesh* mesh, s32 alpha) const
{
apply(scene::SVertexColorSetAlphaManipulator(alpha), mesh);
}
//! Sets the alpha vertex color value of the whole mesh to a new value.
/** \param buffer Meshbuffer on which the operation is performed.
\param alpha New alpha value. Must be a value between 0 and 255. */
void setVertexColorAlpha(IMeshBuffer* buffer, s32 alpha) const
{
apply(scene::SVertexColorSetAlphaManipulator(alpha), buffer);
}
//! Sets the colors of all vertices to one color
/** \param mesh Mesh on which the operation is performed.
\param color New color. */
void setVertexColors(IMesh* mesh, video::SColor color) const
{
apply(scene::SVertexColorSetManipulator(color), mesh);
}
//! Sets the colors of all vertices to one color
/** \param buffer Meshbuffer on which the operation is performed.
\param color New color. */
void setVertexColors(IMeshBuffer* buffer, video::SColor color) const
{
apply(scene::SVertexColorSetManipulator(color), buffer);
}
//! Recalculates all normals of the mesh.
/** \param mesh: Mesh on which the operation is performed.
\param smooth: If the normals shall be smoothed.
\param angleWeighted: If the normals shall be smoothed in relation to their angles. More expensive, but also higher precision. */
virtual void recalculateNormals(IMesh* mesh, bool smooth = false,
bool angleWeighted = false) const=0;
//! Recalculates all normals of the mesh buffer.
/** \param buffer: Mesh buffer on which the operation is performed.
\param smooth: If the normals shall be smoothed.
\param angleWeighted: If the normals shall be smoothed in relation to their angles. More expensive, but also higher precision. */
virtual void recalculateNormals(IMeshBuffer* buffer,
bool smooth = false, bool angleWeighted = false) const=0;
//! Recalculates tangents, requires a tangent mesh
/** \param mesh Mesh on which the operation is performed.
\param recalculateNormals If the normals shall be recalculated, otherwise original normals of the mesh are used unchanged.
\param smooth If the normals shall be smoothed.
\param angleWeighted If the normals shall be smoothed in relation to their angles. More expensive, but also higher precision.
*/
virtual void recalculateTangents(IMesh* mesh,
bool recalculateNormals=false, bool smooth=false,
bool angleWeighted=false) const=0;
//! Recalculates tangents, requires a tangent mesh buffer
/** \param buffer Meshbuffer on which the operation is performed.
\param recalculateNormals If the normals shall be recalculated, otherwise original normals of the buffer are used unchanged.
\param smooth If the normals shall be smoothed.
\param angleWeighted If the normals shall be smoothed in relation to their angles. More expensive, but also higher precision.
*/
virtual void recalculateTangents(IMeshBuffer* buffer,
bool recalculateNormals=false, bool smooth=false,
bool angleWeighted=false) const=0;
//! Scales the actual mesh, not a scene node.
/** \param mesh Mesh on which the operation is performed.
\param factor Scale factor for each axis. */
void scale(IMesh* mesh, const core::vector3df& factor) const
{
apply(SVertexPositionScaleManipulator(factor), mesh, true);
}
//! Scales the actual meshbuffer, not a scene node.
/** \param buffer Meshbuffer on which the operation is performed.
\param factor Scale factor for each axis. */
void scale(IMeshBuffer* buffer, const core::vector3df& factor) const
{
apply(SVertexPositionScaleManipulator(factor), buffer, true);
}
//! Scales the actual mesh, not a scene node.
/** \deprecated Use scale() instead. This method may be removed by Irrlicht 1.9
\param mesh Mesh on which the operation is performed.
\param factor Scale factor for each axis. */
_IRR_DEPRECATED_ void scaleMesh(IMesh* mesh, const core::vector3df& factor) const {return scale(mesh,factor);}
//! Scale the texture coords of a mesh.
/** \param mesh Mesh on which the operation is performed.
\param factor Vector which defines the scale for each axis.
\param level Number of texture coord, starting from 1. Support for level 2 exists for LightMap buffers. */
void scaleTCoords(scene::IMesh* mesh, const core::vector2df& factor, u32 level=1) const
{
apply(SVertexTCoordsScaleManipulator(factor, level), mesh);
}
//! Scale the texture coords of a meshbuffer.
/** \param buffer Meshbuffer on which the operation is performed.
\param factor Vector which defines the scale for each axis.
\param level Number of texture coord, starting from 1. Support for level 2 exists for LightMap buffers. */
void scaleTCoords(scene::IMeshBuffer* buffer, const core::vector2df& factor, u32 level=1) const
{
apply(SVertexTCoordsScaleManipulator(factor, level), buffer);
}
//! Applies a transformation to a mesh
/** \param mesh Mesh on which the operation is performed.
\param m transformation matrix. */
void transform(IMesh* mesh, const core::matrix4& m) const
{
apply(SVertexPositionTransformManipulator(m), mesh, true);
}
//! Applies a transformation to a meshbuffer
/** \param buffer Meshbuffer on which the operation is performed.
\param m transformation matrix. */
void transform(IMeshBuffer* buffer, const core::matrix4& m) const
{
apply(SVertexPositionTransformManipulator(m), buffer, true);
}
//! Applies a transformation to a mesh
/** \deprecated Use transform() instead. This method may be removed by Irrlicht 1.9
\param mesh Mesh on which the operation is performed.
\param m transformation matrix. */
_IRR_DEPRECATED_ virtual void transformMesh(IMesh* mesh, const core::matrix4& m) const {return transform(mesh,m);}
//! Creates a planar texture mapping on the mesh
/** \param mesh: Mesh on which the operation is performed.
\param resolution: resolution of the planar mapping. This is
the value specifying which is the relation between world space
and texture coordinate space. */
virtual void makePlanarTextureMapping(IMesh* mesh, f32 resolution=0.001f) const=0;
//! Creates a planar texture mapping on the meshbuffer
/** \param meshbuffer: Buffer on which the operation is performed.
\param resolution: resolution of the planar mapping. This is
the value specifying which is the relation between world space
and texture coordinate space. */
virtual void makePlanarTextureMapping(scene::IMeshBuffer* meshbuffer, f32 resolution=0.001f) const=0;
//! Creates a planar texture mapping on the buffer
/** This method is currently implemented towards the LWO planar mapping. A more general biasing might be required.
\param mesh Mesh on which the operation is performed.
\param resolutionS Resolution of the planar mapping in horizontal direction. This is the ratio between object space and texture space.
\param resolutionT Resolution of the planar mapping in vertical direction. This is the ratio between object space and texture space.
\param axis The axis along which the texture is projected. The allowed values are 0 (X), 1(Y), and 2(Z).
\param offset Vector added to the vertex positions (in object coordinates).
*/
virtual void makePlanarTextureMapping(scene::IMesh* mesh,
f32 resolutionS, f32 resolutionT,
u8 axis, const core::vector3df& offset) const=0;
//! Creates a planar texture mapping on the meshbuffer
/** This method is currently implemented towards the LWO planar mapping. A more general biasing might be required.
\param buffer Buffer on which the operation is performed.
\param resolutionS Resolution of the planar mapping in horizontal direction. This is the ratio between object space and texture space.
\param resolutionT Resolution of the planar mapping in vertical direction. This is the ratio between object space and texture space.
\param axis The axis along which the texture is projected. The allowed values are 0 (X), 1(Y), and 2(Z).
\param offset Vector added to the vertex positions (in object coordinates).
*/
virtual void makePlanarTextureMapping(scene::IMeshBuffer* buffer,
f32 resolutionS, f32 resolutionT,
u8 axis, const core::vector3df& offset) const=0;
//! Clones a static IMesh into a modifiable SMesh.
/** All meshbuffers in the returned SMesh
are of type SMeshBuffer or SMeshBufferLightMap.
\param mesh Mesh to copy.
\return Cloned mesh. If you no longer need the
cloned mesh, you should call SMesh::drop(). See
IReferenceCounted::drop() for more information. */
virtual SMesh* createMeshCopy(IMesh* mesh) const = 0;
//! Creates a copy of the mesh, which will only consist of S3DVertexTangents vertices.
/** This is useful if you want to draw tangent space normal
mapped geometry because it calculates the tangent and binormal
data which is needed there.
\param mesh Input mesh
\param recalculateNormals The normals are recalculated if set,
otherwise the original ones are kept. Note that keeping the
normals may introduce inaccurate tangents if the normals are
very different to those calculated from the faces.
\param smooth The normals/tangents are smoothed across the
meshbuffer's faces if this flag is set.
\param angleWeighted Improved smoothing calculation used
\param recalculateTangents Whether are actually calculated, or just the mesh with proper type is created.
\return Mesh consisting only of S3DVertexTangents vertices. If
you no longer need the cloned mesh, you should call
IMesh::drop(). See IReferenceCounted::drop() for more
information. */
virtual IMesh* createMeshWithTangents(IMesh* mesh,
bool recalculateNormals=false, bool smooth=false,
bool angleWeighted=false, bool recalculateTangents=true) const=0;
//! Creates a copy of the mesh, which will only consist of S3DVertex2TCoord vertices.
/** \param mesh Input mesh
\return Mesh consisting only of S3DVertex2TCoord vertices. If
you no longer need the cloned mesh, you should call
IMesh::drop(). See IReferenceCounted::drop() for more
information. */
virtual IMesh* createMeshWith2TCoords(IMesh* mesh) const = 0;
//! Creates a copy of the mesh, which will only consist of S3DVertex vertices.
/** \param mesh Input mesh
\return Mesh consisting only of S3DVertex vertices. If
you no longer need the cloned mesh, you should call
IMesh::drop(). See IReferenceCounted::drop() for more
information. */
virtual IMesh* createMeshWith1TCoords(IMesh* mesh) const = 0;
//! Creates a copy of a mesh with all vertices unwelded
/** \param mesh Input mesh
\return Mesh consisting only of unique faces. All vertices
which were previously shared are now duplicated. If you no
longer need the cloned mesh, you should call IMesh::drop(). See
IReferenceCounted::drop() for more information. */
virtual IMesh* createMeshUniquePrimitives(IMesh* mesh) const = 0;
//! Creates a copy of a mesh with vertices welded
/** \param mesh Input mesh
\param tolerance The threshold for vertex comparisons.
\return Mesh without redundant vertices. If you no longer need
the cloned mesh, you should call IMesh::drop(). See
IReferenceCounted::drop() for more information. */
virtual IMesh* createMeshWelded(IMesh* mesh, f32 tolerance=core::ROUNDING_ERROR_f32) const = 0;
//! Get amount of polygons in mesh.
/** \param mesh Input mesh
\return Number of polygons in mesh. */
virtual s32 getPolyCount(IMesh* mesh) const = 0;
//! Get amount of polygons in mesh.
/** \param mesh Input mesh
\return Number of polygons in mesh. */
virtual s32 getPolyCount(IAnimatedMesh* mesh) const = 0;
//! Create a new AnimatedMesh and adds the mesh to it
/** \param mesh Input mesh
\param type The type of the animated mesh to create.
\return Newly created animated mesh with mesh as its only
content. When you don't need the animated mesh anymore, you
should call IAnimatedMesh::drop(). See
IReferenceCounted::drop() for more information. */
virtual IAnimatedMesh * createAnimatedMesh(IMesh* mesh,
scene::E_ANIMATED_MESH_TYPE type = scene::EAMT_UNKNOWN) const = 0;
//! Vertex cache optimization according to the Forsyth paper
/** More information can be found at
http://home.comcast.net/~tom_forsyth/papers/fast_vert_cache_opt.html
The function is thread-safe (read: you can optimize several
meshes in different threads).
\param mesh Source mesh for the operation.
\return A new mesh optimized for the vertex cache. */
virtual IMesh* createForsythOptimizedMesh(const IMesh *mesh) const = 0;
//! Apply a manipulator on the Meshbuffer
/** \param func A functor defining the mesh manipulation.
\param buffer The Meshbuffer to apply the manipulator to.
\param boundingBoxUpdate Specifies if the bounding box should be updated during manipulation.
\return True if the functor was successfully applied, else false. */
template <typename Functor>
bool apply(const Functor& func, IMeshBuffer* buffer, bool boundingBoxUpdate=false) const
{
return apply_(func, buffer, boundingBoxUpdate, func);
}
//! Apply a manipulator on the Mesh
/** \param func A functor defining the mesh manipulation.
\param mesh The Mesh to apply the manipulator to.
\param boundingBoxUpdate Specifies if the bounding box should be updated during manipulation.
\return True if the functor was successfully applied, else false. */
template <typename Functor>
bool apply(const Functor& func, IMesh* mesh, bool boundingBoxUpdate=false) const
{
if (!mesh)
return true;
bool result = true;
core::aabbox3df bufferbox;
for (u32 i=0; i<mesh->getMeshBufferCount(); ++i)
{
result &= apply(func, mesh->getMeshBuffer(i), boundingBoxUpdate);
if (boundingBoxUpdate)
{
if (0==i)
bufferbox.reset(mesh->getMeshBuffer(i)->getBoundingBox());
else
bufferbox.addInternalBox(mesh->getMeshBuffer(i)->getBoundingBox());
}
}
if (boundingBoxUpdate)
mesh->setBoundingBox(bufferbox);
return result;
}
protected:
//! Apply a manipulator based on the type of the functor
/** \param func A functor defining the mesh manipulation.
\param buffer The Meshbuffer to apply the manipulator to.
\param boundingBoxUpdate Specifies if the bounding box should be updated during manipulation.
\param typeTest Unused parameter, which handles the proper call selection based on the type of the Functor which is passed in two times.
\return True if the functor was successfully applied, else false. */
template <typename Functor>
bool apply_(const Functor& func, IMeshBuffer* buffer, bool boundingBoxUpdate, const IVertexManipulator& typeTest) const
{
if (!buffer)
return true;
core::aabbox3df bufferbox;
for (u32 i=0; i<buffer->getVertexCount(); ++i)
{
switch (buffer->getVertexType())
{
case video::EVT_STANDARD:
{
video::S3DVertex* verts = (video::S3DVertex*)buffer->getVertices();
func(verts[i]);
}
break;
case video::EVT_2TCOORDS:
{
video::S3DVertex2TCoords* verts = (video::S3DVertex2TCoords*)buffer->getVertices();
func(verts[i]);
}
break;
case video::EVT_TANGENTS:
{
video::S3DVertexTangents* verts = (video::S3DVertexTangents*)buffer->getVertices();
func(verts[i]);
}
break;
}
if (boundingBoxUpdate)
{
if (0==i)
bufferbox.reset(buffer->getPosition(0));
else
bufferbox.addInternalPoint(buffer->getPosition(i));
}
}
if (boundingBoxUpdate)
buffer->setBoundingBox(bufferbox);
return true;
}
};
} // end namespace scene
} // end namespace irr
#endif
|