3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <IAnimatedMesh.h>
26 #include <SAnimatedMesh.h>
28 // In Irrlicht 1.8 the signature of ITexture::lock was changed from
29 // (bool, u32) to (E_TEXTURE_LOCK_MODE, u32).
30 #if IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR <= 7
31 #define MY_ETLM_READ_ONLY true
33 #define MY_ETLM_READ_ONLY video::ETLM_READ_ONLY
36 inline static void applyShadeFactor(video::SColor& color, float factor)
38 color.setRed(core::clamp(core::round32(color.getRed()*factor), 0, 255));
39 color.setGreen(core::clamp(core::round32(color.getGreen()*factor), 0, 255));
40 color.setBlue(core::clamp(core::round32(color.getBlue()*factor), 0, 255));
43 void applyFacesShading(video::SColor &color, const v3f &normal)
46 Some drawtypes have normals set to (0, 0, 0), this must result in
47 maximum brightness: shade factor 1.0.
48 Shade factors for aligned cube faces are:
51 +-X 0.670820 sqrt(0.45)
52 +-Z 0.836660 sqrt(0.7)
54 float x2 = normal.X * normal.X;
55 float y2 = normal.Y * normal.Y;
56 float z2 = normal.Z * normal.Z;
58 applyShadeFactor(color, 0.670820f * x2 + 0.447213f * y2 + 0.836660f * z2);
59 else if ((x2 > 1e-3) || (z2 > 1e-3))
60 applyShadeFactor(color, 0.670820f * x2 + 1.000000f * y2 + 0.836660f * z2);
63 scene::IAnimatedMesh* createCubeMesh(v3f scale)
65 video::SColor c(255,255,255,255);
66 video::S3DVertex vertices[24] =
69 video::S3DVertex(-0.5,+0.5,-0.5, 0,1,0, c, 0,1),
70 video::S3DVertex(-0.5,+0.5,+0.5, 0,1,0, c, 0,0),
71 video::S3DVertex(+0.5,+0.5,+0.5, 0,1,0, c, 1,0),
72 video::S3DVertex(+0.5,+0.5,-0.5, 0,1,0, c, 1,1),
74 video::S3DVertex(-0.5,-0.5,-0.5, 0,-1,0, c, 0,0),
75 video::S3DVertex(+0.5,-0.5,-0.5, 0,-1,0, c, 1,0),
76 video::S3DVertex(+0.5,-0.5,+0.5, 0,-1,0, c, 1,1),
77 video::S3DVertex(-0.5,-0.5,+0.5, 0,-1,0, c, 0,1),
79 video::S3DVertex(+0.5,-0.5,-0.5, 1,0,0, c, 0,1),
80 video::S3DVertex(+0.5,+0.5,-0.5, 1,0,0, c, 0,0),
81 video::S3DVertex(+0.5,+0.5,+0.5, 1,0,0, c, 1,0),
82 video::S3DVertex(+0.5,-0.5,+0.5, 1,0,0, c, 1,1),
84 video::S3DVertex(-0.5,-0.5,-0.5, -1,0,0, c, 1,1),
85 video::S3DVertex(-0.5,-0.5,+0.5, -1,0,0, c, 0,1),
86 video::S3DVertex(-0.5,+0.5,+0.5, -1,0,0, c, 0,0),
87 video::S3DVertex(-0.5,+0.5,-0.5, -1,0,0, c, 1,0),
89 video::S3DVertex(-0.5,-0.5,+0.5, 0,0,1, c, 1,1),
90 video::S3DVertex(+0.5,-0.5,+0.5, 0,0,1, c, 0,1),
91 video::S3DVertex(+0.5,+0.5,+0.5, 0,0,1, c, 0,0),
92 video::S3DVertex(-0.5,+0.5,+0.5, 0,0,1, c, 1,0),
94 video::S3DVertex(-0.5,-0.5,-0.5, 0,0,-1, c, 0,1),
95 video::S3DVertex(-0.5,+0.5,-0.5, 0,0,-1, c, 0,0),
96 video::S3DVertex(+0.5,+0.5,-0.5, 0,0,-1, c, 1,0),
97 video::S3DVertex(+0.5,-0.5,-0.5, 0,0,-1, c, 1,1),
100 u16 indices[6] = {0,1,2,2,3,0};
102 scene::SMesh *mesh = new scene::SMesh();
103 for (u32 i=0; i<6; ++i)
105 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
106 buf->append(vertices + 4 * i, 4, indices, 6);
107 // Set default material
108 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
109 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
110 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
111 // Add mesh buffer to mesh
112 mesh->addMeshBuffer(buf);
116 scene::SAnimatedMesh *anim_mesh = new scene::SAnimatedMesh(mesh);
118 scaleMesh(anim_mesh, scale); // also recalculates bounding box
122 void scaleMesh(scene::IMesh *mesh, v3f scale)
130 u32 mc = mesh->getMeshBufferCount();
131 for (u32 j = 0; j < mc; j++) {
132 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
133 const u32 stride = getVertexPitchFromType(buf->getVertexType());
134 u32 vertex_count = buf->getVertexCount();
135 u8 *vertices = (u8 *)buf->getVertices();
136 for (u32 i = 0; i < vertex_count; i++)
137 ((video::S3DVertex *)(vertices + i * stride))->Pos *= scale;
139 buf->recalculateBoundingBox();
141 // calculate total bounding box
143 bbox = buf->getBoundingBox();
145 bbox.addInternalBox(buf->getBoundingBox());
147 mesh->setBoundingBox(bbox);
150 void translateMesh(scene::IMesh *mesh, v3f vec)
158 u32 mc = mesh->getMeshBufferCount();
159 for (u32 j = 0; j < mc; j++) {
160 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
161 const u32 stride = getVertexPitchFromType(buf->getVertexType());
162 u32 vertex_count = buf->getVertexCount();
163 u8 *vertices = (u8 *)buf->getVertices();
164 for (u32 i = 0; i < vertex_count; i++)
165 ((video::S3DVertex *)(vertices + i * stride))->Pos += vec;
167 buf->recalculateBoundingBox();
169 // calculate total bounding box
171 bbox = buf->getBoundingBox();
173 bbox.addInternalBox(buf->getBoundingBox());
175 mesh->setBoundingBox(bbox);
179 void setMeshColor(scene::IMesh *mesh, const video::SColor &color)
184 u32 mc = mesh->getMeshBufferCount();
185 for (u32 j = 0; j < mc; j++) {
186 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
187 const u32 stride = getVertexPitchFromType(buf->getVertexType());
188 u32 vertex_count = buf->getVertexCount();
189 u8 *vertices = (u8 *)buf->getVertices();
190 for (u32 i = 0; i < vertex_count; i++)
191 ((video::S3DVertex *)(vertices + i * stride))->Color = color;
195 void colorizeMeshBuffer(scene::IMeshBuffer *buf, const video::SColor *buffercolor)
197 const u32 stride = getVertexPitchFromType(buf->getVertexType());
198 u32 vertex_count = buf->getVertexCount();
199 u8 *vertices = (u8 *) buf->getVertices();
200 for (u32 i = 0; i < vertex_count; i++) {
201 video::S3DVertex *vertex = (video::S3DVertex *) (vertices + i * stride);
202 video::SColor *vc = &(vertex->Color);
206 applyFacesShading(*vc, vertex->Normal);
210 void setMeshColorByNormalXYZ(scene::IMesh *mesh,
211 const video::SColor &colorX,
212 const video::SColor &colorY,
213 const video::SColor &colorZ)
218 u16 mc = mesh->getMeshBufferCount();
219 for (u16 j = 0; j < mc; j++) {
220 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
221 const u32 stride = getVertexPitchFromType(buf->getVertexType());
222 u32 vertex_count = buf->getVertexCount();
223 u8 *vertices = (u8 *)buf->getVertices();
224 for (u32 i = 0; i < vertex_count; i++) {
225 video::S3DVertex *vertex = (video::S3DVertex *)(vertices + i * stride);
226 f32 x = fabs(vertex->Normal.X);
227 f32 y = fabs(vertex->Normal.Y);
228 f32 z = fabs(vertex->Normal.Z);
229 if (x >= y && x >= z)
230 vertex->Color = colorX;
232 vertex->Color = colorY;
234 vertex->Color = colorZ;
239 void setMeshColorByNormal(scene::IMesh *mesh, const v3f &normal,
240 const video::SColor &color)
245 u16 mc = mesh->getMeshBufferCount();
246 for (u16 j = 0; j < mc; j++) {
247 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
248 const u32 stride = getVertexPitchFromType(buf->getVertexType());
249 u32 vertex_count = buf->getVertexCount();
250 u8 *vertices = (u8 *)buf->getVertices();
251 for (u32 i = 0; i < vertex_count; i++) {
252 video::S3DVertex *vertex = (video::S3DVertex *)(vertices + i * stride);
253 if (normal == vertex->Normal) {
254 vertex->Color = color;
260 void rotateMeshXYby(scene::IMesh *mesh, f64 degrees)
262 u16 mc = mesh->getMeshBufferCount();
263 for (u16 j = 0; j < mc; j++) {
264 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
265 const u32 stride = getVertexPitchFromType(buf->getVertexType());
266 u32 vertex_count = buf->getVertexCount();
267 u8 *vertices = (u8 *)buf->getVertices();
268 for (u32 i = 0; i < vertex_count; i++)
269 ((video::S3DVertex *)(vertices + i * stride))->Pos.rotateXYBy(degrees);
273 void rotateMeshXZby(scene::IMesh *mesh, f64 degrees)
275 u16 mc = mesh->getMeshBufferCount();
276 for (u16 j = 0; j < mc; j++) {
277 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
278 const u32 stride = getVertexPitchFromType(buf->getVertexType());
279 u32 vertex_count = buf->getVertexCount();
280 u8 *vertices = (u8 *)buf->getVertices();
281 for (u32 i = 0; i < vertex_count; i++)
282 ((video::S3DVertex *)(vertices + i * stride))->Pos.rotateXZBy(degrees);
286 void rotateMeshYZby(scene::IMesh *mesh, f64 degrees)
288 u16 mc = mesh->getMeshBufferCount();
289 for (u16 j = 0; j < mc; j++) {
290 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
291 const u32 stride = getVertexPitchFromType(buf->getVertexType());
292 u32 vertex_count = buf->getVertexCount();
293 u8 *vertices = (u8 *)buf->getVertices();
294 for (u32 i = 0; i < vertex_count; i++)
295 ((video::S3DVertex *)(vertices + i * stride))->Pos.rotateYZBy(degrees);
299 void rotateMeshBy6dFacedir(scene::IMesh *mesh, int facedir)
301 int axisdir = facedir >> 2;
304 u16 mc = mesh->getMeshBufferCount();
305 for (u16 j = 0; j < mc; j++) {
306 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
307 const u32 stride = getVertexPitchFromType(buf->getVertexType());
308 u32 vertex_count = buf->getVertexCount();
309 u8 *vertices = (u8 *)buf->getVertices();
310 for (u32 i = 0; i < vertex_count; i++) {
311 video::S3DVertex *vertex = (video::S3DVertex *)(vertices + i * stride);
315 vertex->Pos.rotateXZBy(-90);
316 else if (facedir == 2)
317 vertex->Pos.rotateXZBy(180);
318 else if (facedir == 3)
319 vertex->Pos.rotateXZBy(90);
322 vertex->Pos.rotateYZBy(90);
324 vertex->Pos.rotateXYBy(90);
325 else if (facedir == 2)
326 vertex->Pos.rotateXYBy(180);
327 else if (facedir == 3)
328 vertex->Pos.rotateXYBy(-90);
331 vertex->Pos.rotateYZBy(-90);
333 vertex->Pos.rotateXYBy(-90);
334 else if (facedir == 2)
335 vertex->Pos.rotateXYBy(180);
336 else if (facedir == 3)
337 vertex->Pos.rotateXYBy(90);
340 vertex->Pos.rotateXYBy(-90);
342 vertex->Pos.rotateYZBy(90);
343 else if (facedir == 2)
344 vertex->Pos.rotateYZBy(180);
345 else if (facedir == 3)
346 vertex->Pos.rotateYZBy(-90);
349 vertex->Pos.rotateXYBy(90);
351 vertex->Pos.rotateYZBy(-90);
352 else if (facedir == 2)
353 vertex->Pos.rotateYZBy(180);
354 else if (facedir == 3)
355 vertex->Pos.rotateYZBy(90);
358 vertex->Pos.rotateXYBy(-180);
360 vertex->Pos.rotateXZBy(90);
361 else if (facedir == 2)
362 vertex->Pos.rotateXZBy(180);
363 else if (facedir == 3)
364 vertex->Pos.rotateXZBy(-90);
373 void recalculateBoundingBox(scene::IMesh *src_mesh)
377 for (u16 j = 0; j < src_mesh->getMeshBufferCount(); j++) {
378 scene::IMeshBuffer *buf = src_mesh->getMeshBuffer(j);
379 buf->recalculateBoundingBox();
381 bbox = buf->getBoundingBox();
383 bbox.addInternalBox(buf->getBoundingBox());
385 src_mesh->setBoundingBox(bbox);
388 scene::IMeshBuffer* cloneMeshBuffer(scene::IMeshBuffer *mesh_buffer)
390 scene::IMeshBuffer *clone = NULL;
391 switch (mesh_buffer->getVertexType()) {
392 case video::EVT_STANDARD: {
393 video::S3DVertex *v = (video::S3DVertex *) mesh_buffer->getVertices();
394 u16 *indices = (u16*) mesh_buffer->getIndices();
395 scene::SMeshBuffer *temp_buf = new scene::SMeshBuffer();
396 temp_buf->append(v, mesh_buffer->getVertexCount(), indices,
397 mesh_buffer->getIndexCount());
401 case video::EVT_2TCOORDS: {
402 video::S3DVertex2TCoords *v =
403 (video::S3DVertex2TCoords *) mesh_buffer->getVertices();
404 u16 *indices = (u16*) mesh_buffer->getIndices();
405 scene::SMeshBufferTangents *temp_buf = new scene::SMeshBufferTangents();
406 temp_buf->append(v, mesh_buffer->getVertexCount(), indices,
407 mesh_buffer->getIndexCount());
410 case video::EVT_TANGENTS: {
411 video::S3DVertexTangents *v =
412 (video::S3DVertexTangents *) mesh_buffer->getVertices();
413 u16 *indices = (u16*) mesh_buffer->getIndices();
414 scene::SMeshBufferTangents *temp_buf = new scene::SMeshBufferTangents();
415 temp_buf->append(v, mesh_buffer->getVertexCount(), indices,
416 mesh_buffer->getIndexCount());
423 scene::SMesh* cloneMesh(scene::IMesh *src_mesh)
425 scene::SMesh* dst_mesh = new scene::SMesh();
426 for (u16 j = 0; j < src_mesh->getMeshBufferCount(); j++) {
427 scene::IMeshBuffer *temp_buf = cloneMeshBuffer(
428 src_mesh->getMeshBuffer(j));
429 dst_mesh->addMeshBuffer(temp_buf);
436 scene::IMesh* convertNodeboxesToMesh(const std::vector<aabb3f> &boxes,
437 const f32 *uv_coords, float expand)
439 scene::SMesh* dst_mesh = new scene::SMesh();
441 for (u16 j = 0; j < 6; j++)
443 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
444 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
445 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
446 dst_mesh->addMeshBuffer(buf);
450 video::SColor c(255,255,255,255);
452 for (std::vector<aabb3f>::const_iterator
454 i != boxes.end(); ++i)
459 box.MinEdge.X -= expand;
460 box.MinEdge.Y -= expand;
461 box.MinEdge.Z -= expand;
462 box.MaxEdge.X += expand;
463 box.MaxEdge.Y += expand;
464 box.MaxEdge.Z += expand;
466 // Compute texture UV coords
467 f32 tx1 = (box.MinEdge.X / BS) + 0.5;
468 f32 ty1 = (box.MinEdge.Y / BS) + 0.5;
469 f32 tz1 = (box.MinEdge.Z / BS) + 0.5;
470 f32 tx2 = (box.MaxEdge.X / BS) + 0.5;
471 f32 ty2 = (box.MaxEdge.Y / BS) + 0.5;
472 f32 tz2 = (box.MaxEdge.Z / BS) + 0.5;
474 f32 txc_default[24] = {
476 tx1, 1 - tz2, tx2, 1 - tz1,
480 tz1, 1 - ty2, tz2, 1 - ty1,
482 1 - tz2, 1 - ty2, 1 - tz1, 1 - ty1,
484 1 - tx2, 1 - ty2, 1 - tx1, 1 - ty1,
486 tx1, 1 - ty2, tx2, 1 - ty1,
489 // use default texture UV mapping if not provided
490 const f32 *txc = uv_coords ? uv_coords : txc_default;
492 v3f min = box.MinEdge;
493 v3f max = box.MaxEdge;
495 video::S3DVertex vertices[24] =
498 video::S3DVertex(min.X,max.Y,max.Z, 0,1,0, c, txc[0],txc[1]),
499 video::S3DVertex(max.X,max.Y,max.Z, 0,1,0, c, txc[2],txc[1]),
500 video::S3DVertex(max.X,max.Y,min.Z, 0,1,0, c, txc[2],txc[3]),
501 video::S3DVertex(min.X,max.Y,min.Z, 0,1,0, c, txc[0],txc[3]),
503 video::S3DVertex(min.X,min.Y,min.Z, 0,-1,0, c, txc[4],txc[5]),
504 video::S3DVertex(max.X,min.Y,min.Z, 0,-1,0, c, txc[6],txc[5]),
505 video::S3DVertex(max.X,min.Y,max.Z, 0,-1,0, c, txc[6],txc[7]),
506 video::S3DVertex(min.X,min.Y,max.Z, 0,-1,0, c, txc[4],txc[7]),
508 video::S3DVertex(max.X,max.Y,min.Z, 1,0,0, c, txc[ 8],txc[9]),
509 video::S3DVertex(max.X,max.Y,max.Z, 1,0,0, c, txc[10],txc[9]),
510 video::S3DVertex(max.X,min.Y,max.Z, 1,0,0, c, txc[10],txc[11]),
511 video::S3DVertex(max.X,min.Y,min.Z, 1,0,0, c, txc[ 8],txc[11]),
513 video::S3DVertex(min.X,max.Y,max.Z, -1,0,0, c, txc[12],txc[13]),
514 video::S3DVertex(min.X,max.Y,min.Z, -1,0,0, c, txc[14],txc[13]),
515 video::S3DVertex(min.X,min.Y,min.Z, -1,0,0, c, txc[14],txc[15]),
516 video::S3DVertex(min.X,min.Y,max.Z, -1,0,0, c, txc[12],txc[15]),
518 video::S3DVertex(max.X,max.Y,max.Z, 0,0,1, c, txc[16],txc[17]),
519 video::S3DVertex(min.X,max.Y,max.Z, 0,0,1, c, txc[18],txc[17]),
520 video::S3DVertex(min.X,min.Y,max.Z, 0,0,1, c, txc[18],txc[19]),
521 video::S3DVertex(max.X,min.Y,max.Z, 0,0,1, c, txc[16],txc[19]),
523 video::S3DVertex(min.X,max.Y,min.Z, 0,0,-1, c, txc[20],txc[21]),
524 video::S3DVertex(max.X,max.Y,min.Z, 0,0,-1, c, txc[22],txc[21]),
525 video::S3DVertex(max.X,min.Y,min.Z, 0,0,-1, c, txc[22],txc[23]),
526 video::S3DVertex(min.X,min.Y,min.Z, 0,0,-1, c, txc[20],txc[23]),
529 u16 indices[] = {0,1,2,2,3,0};
531 for(u16 j = 0; j < 24; j += 4)
533 scene::IMeshBuffer *buf = dst_mesh->getMeshBuffer(j / 4);
534 buf->append(vertices + j, 4, indices, 6);
542 core::array<u32> tris;
555 const u16 cachesize = 32;
557 float FindVertexScore(vcache *v)
559 const float CacheDecayPower = 1.5f;
560 const float LastTriScore = 0.75f;
561 const float ValenceBoostScale = 2.0f;
562 const float ValenceBoostPower = 0.5f;
563 const float MaxSizeVertexCache = 32.0f;
565 if (v->NumActiveTris == 0)
567 // No tri needs this vertex!
572 int CachePosition = v->cachepos;
573 if (CachePosition < 0)
575 // Vertex is not in FIFO cache - no score.
579 if (CachePosition < 3)
581 // This vertex was used in the last triangle,
582 // so it has a fixed score.
583 Score = LastTriScore;
587 // Points for being high in the cache.
588 const float Scaler = 1.0f / (MaxSizeVertexCache - 3);
589 Score = 1.0f - (CachePosition - 3) * Scaler;
590 Score = powf(Score, CacheDecayPower);
594 // Bonus points for having a low number of tris still to
595 // use the vert, so we get rid of lone verts quickly.
596 float ValenceBoost = powf(v->NumActiveTris,
598 Score += ValenceBoostScale * ValenceBoost;
604 A specialized LRU cache for the Forsyth algorithm.
611 f_lru(vcache *v, tcache *t): vc(v), tc(t)
613 for (u16 i = 0; i < cachesize; i++)
619 // Adds this vertex index and returns the highest-scoring triangle index
620 u32 add(u16 vert, bool updatetris = false)
624 // Mark existing pos as empty
625 for (u16 i = 0; i < cachesize; i++)
627 if (cache[i] == vert)
629 // Move everything down
630 for (u16 j = i; j; j--)
632 cache[j] = cache[j - 1];
642 if (cache[cachesize-1] != -1)
643 vc[cache[cachesize-1]].cachepos = -1;
645 // Move everything down
646 for (u16 i = cachesize - 1; i; i--)
648 cache[i] = cache[i - 1];
659 // Update cache positions
660 for (u16 i = 0; i < cachesize; i++)
665 vc[cache[i]].cachepos = i;
666 vc[cache[i]].score = FindVertexScore(&vc[cache[i]]);
669 // Update triangle scores
670 for (u16 i = 0; i < cachesize; i++)
675 const u16 trisize = vc[cache[i]].tris.size();
676 for (u16 t = 0; t < trisize; t++)
678 tcache *tri = &tc[vc[cache[i]].tris[t]];
681 vc[tri->ind[0]].score +
682 vc[tri->ind[1]].score +
683 vc[tri->ind[2]].score;
685 if (tri->score > hiscore)
687 hiscore = tri->score;
688 highest = vc[cache[i]].tris[t];
698 s32 cache[cachesize];
704 Vertex cache optimization according to the Forsyth paper:
705 http://home.comcast.net/~tom_forsyth/papers/fast_vert_cache_opt.html
707 The function is thread-safe (read: you can optimize several meshes in different threads)
709 \param mesh Source mesh for the operation. */
710 scene::IMesh* createForsythOptimizedMesh(const scene::IMesh *mesh)
715 scene::SMesh *newmesh = new scene::SMesh();
716 newmesh->BoundingBox = mesh->getBoundingBox();
718 const u32 mbcount = mesh->getMeshBufferCount();
720 for (u32 b = 0; b < mbcount; ++b)
722 const scene::IMeshBuffer *mb = mesh->getMeshBuffer(b);
724 if (mb->getIndexType() != video::EIT_16BIT)
726 //os::Printer::log("Cannot optimize a mesh with 32bit indices", ELL_ERROR);
731 const u32 icount = mb->getIndexCount();
732 const u32 tcount = icount / 3;
733 const u32 vcount = mb->getVertexCount();
734 const u16 *ind = mb->getIndices();
736 vcache *vc = new vcache[vcount];
737 tcache *tc = new tcache[tcount];
742 for (u16 i = 0; i < vcount; i++)
746 vc[i].NumActiveTris = 0;
749 // First pass: count how many times a vert is used
750 for (u32 i = 0; i < icount; i += 3)
752 vc[ind[i]].NumActiveTris++;
753 vc[ind[i + 1]].NumActiveTris++;
754 vc[ind[i + 2]].NumActiveTris++;
756 const u32 tri_ind = i/3;
757 tc[tri_ind].ind[0] = ind[i];
758 tc[tri_ind].ind[1] = ind[i + 1];
759 tc[tri_ind].ind[2] = ind[i + 2];
762 // Second pass: list of each triangle
763 for (u32 i = 0; i < tcount; i++)
765 vc[tc[i].ind[0]].tris.push_back(i);
766 vc[tc[i].ind[1]].tris.push_back(i);
767 vc[tc[i].ind[2]].tris.push_back(i);
772 // Give initial scores
773 for (u16 i = 0; i < vcount; i++)
775 vc[i].score = FindVertexScore(&vc[i]);
777 for (u32 i = 0; i < tcount; i++)
780 vc[tc[i].ind[0]].score +
781 vc[tc[i].ind[1]].score +
782 vc[tc[i].ind[2]].score;
785 switch(mb->getVertexType())
787 case video::EVT_STANDARD:
789 video::S3DVertex *v = (video::S3DVertex *) mb->getVertices();
791 scene::SMeshBuffer *buf = new scene::SMeshBuffer();
792 buf->Material = mb->getMaterial();
794 buf->Vertices.reallocate(vcount);
795 buf->Indices.reallocate(icount);
797 core::map<const video::S3DVertex, const u16> sind; // search index for fast operation
798 typedef core::map<const video::S3DVertex, const u16>::Node snode;
805 if (tc[highest].drawn)
809 for (u32 t = 0; t < tcount; t++)
813 if (tc[t].score > hiscore)
816 hiscore = tc[t].score;
825 // Output the best triangle
826 u16 newind = buf->Vertices.size();
828 snode *s = sind.find(v[tc[highest].ind[0]]);
832 buf->Vertices.push_back(v[tc[highest].ind[0]]);
833 buf->Indices.push_back(newind);
834 sind.insert(v[tc[highest].ind[0]], newind);
839 buf->Indices.push_back(s->getValue());
842 s = sind.find(v[tc[highest].ind[1]]);
846 buf->Vertices.push_back(v[tc[highest].ind[1]]);
847 buf->Indices.push_back(newind);
848 sind.insert(v[tc[highest].ind[1]], newind);
853 buf->Indices.push_back(s->getValue());
856 s = sind.find(v[tc[highest].ind[2]]);
860 buf->Vertices.push_back(v[tc[highest].ind[2]]);
861 buf->Indices.push_back(newind);
862 sind.insert(v[tc[highest].ind[2]], newind);
866 buf->Indices.push_back(s->getValue());
869 vc[tc[highest].ind[0]].NumActiveTris--;
870 vc[tc[highest].ind[1]].NumActiveTris--;
871 vc[tc[highest].ind[2]].NumActiveTris--;
873 tc[highest].drawn = true;
875 for (u16 j = 0; j < 3; j++)
877 vcache *vert = &vc[tc[highest].ind[j]];
878 for (u16 t = 0; t < vert->tris.size(); t++)
880 if (highest == vert->tris[t])
888 lru.add(tc[highest].ind[0]);
889 lru.add(tc[highest].ind[1]);
890 highest = lru.add(tc[highest].ind[2], true);
894 buf->setBoundingBox(mb->getBoundingBox());
895 newmesh->addMeshBuffer(buf);
899 case video::EVT_2TCOORDS:
901 video::S3DVertex2TCoords *v = (video::S3DVertex2TCoords *) mb->getVertices();
903 scene::SMeshBufferLightMap *buf = new scene::SMeshBufferLightMap();
904 buf->Material = mb->getMaterial();
906 buf->Vertices.reallocate(vcount);
907 buf->Indices.reallocate(icount);
909 core::map<const video::S3DVertex2TCoords, const u16> sind; // search index for fast operation
910 typedef core::map<const video::S3DVertex2TCoords, const u16>::Node snode;
917 if (tc[highest].drawn)
921 for (u32 t = 0; t < tcount; t++)
925 if (tc[t].score > hiscore)
928 hiscore = tc[t].score;
937 // Output the best triangle
938 u16 newind = buf->Vertices.size();
940 snode *s = sind.find(v[tc[highest].ind[0]]);
944 buf->Vertices.push_back(v[tc[highest].ind[0]]);
945 buf->Indices.push_back(newind);
946 sind.insert(v[tc[highest].ind[0]], newind);
951 buf->Indices.push_back(s->getValue());
954 s = sind.find(v[tc[highest].ind[1]]);
958 buf->Vertices.push_back(v[tc[highest].ind[1]]);
959 buf->Indices.push_back(newind);
960 sind.insert(v[tc[highest].ind[1]], newind);
965 buf->Indices.push_back(s->getValue());
968 s = sind.find(v[tc[highest].ind[2]]);
972 buf->Vertices.push_back(v[tc[highest].ind[2]]);
973 buf->Indices.push_back(newind);
974 sind.insert(v[tc[highest].ind[2]], newind);
978 buf->Indices.push_back(s->getValue());
981 vc[tc[highest].ind[0]].NumActiveTris--;
982 vc[tc[highest].ind[1]].NumActiveTris--;
983 vc[tc[highest].ind[2]].NumActiveTris--;
985 tc[highest].drawn = true;
987 for (u16 j = 0; j < 3; j++)
989 vcache *vert = &vc[tc[highest].ind[j]];
990 for (u16 t = 0; t < vert->tris.size(); t++)
992 if (highest == vert->tris[t])
1000 lru.add(tc[highest].ind[0]);
1001 lru.add(tc[highest].ind[1]);
1002 highest = lru.add(tc[highest].ind[2]);
1006 buf->setBoundingBox(mb->getBoundingBox());
1007 newmesh->addMeshBuffer(buf);
1012 case video::EVT_TANGENTS:
1014 video::S3DVertexTangents *v = (video::S3DVertexTangents *) mb->getVertices();
1016 scene::SMeshBufferTangents *buf = new scene::SMeshBufferTangents();
1017 buf->Material = mb->getMaterial();
1019 buf->Vertices.reallocate(vcount);
1020 buf->Indices.reallocate(icount);
1022 core::map<const video::S3DVertexTangents, const u16> sind; // search index for fast operation
1023 typedef core::map<const video::S3DVertexTangents, const u16>::Node snode;
1030 if (tc[highest].drawn)
1034 for (u32 t = 0; t < tcount; t++)
1038 if (tc[t].score > hiscore)
1041 hiscore = tc[t].score;
1050 // Output the best triangle
1051 u16 newind = buf->Vertices.size();
1053 snode *s = sind.find(v[tc[highest].ind[0]]);
1057 buf->Vertices.push_back(v[tc[highest].ind[0]]);
1058 buf->Indices.push_back(newind);
1059 sind.insert(v[tc[highest].ind[0]], newind);
1064 buf->Indices.push_back(s->getValue());
1067 s = sind.find(v[tc[highest].ind[1]]);
1071 buf->Vertices.push_back(v[tc[highest].ind[1]]);
1072 buf->Indices.push_back(newind);
1073 sind.insert(v[tc[highest].ind[1]], newind);
1078 buf->Indices.push_back(s->getValue());
1081 s = sind.find(v[tc[highest].ind[2]]);
1085 buf->Vertices.push_back(v[tc[highest].ind[2]]);
1086 buf->Indices.push_back(newind);
1087 sind.insert(v[tc[highest].ind[2]], newind);
1091 buf->Indices.push_back(s->getValue());
1094 vc[tc[highest].ind[0]].NumActiveTris--;
1095 vc[tc[highest].ind[1]].NumActiveTris--;
1096 vc[tc[highest].ind[2]].NumActiveTris--;
1098 tc[highest].drawn = true;
1100 for (u16 j = 0; j < 3; j++)
1102 vcache *vert = &vc[tc[highest].ind[j]];
1103 for (u16 t = 0; t < vert->tris.size(); t++)
1105 if (highest == vert->tris[t])
1107 vert->tris.erase(t);
1113 lru.add(tc[highest].ind[0]);
1114 lru.add(tc[highest].ind[1]);
1115 highest = lru.add(tc[highest].ind[2]);
1119 buf->setBoundingBox(mb->getBoundingBox());
1120 newmesh->addMeshBuffer(buf);
1129 } // for each meshbuffer