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.
26 #include <IAnimatedMesh.h>
27 #include <SAnimatedMesh.h>
28 #include <IAnimatedMeshSceneNode.h>
30 // In Irrlicht 1.8 the signature of ITexture::lock was changed from
31 // (bool, u32) to (E_TEXTURE_LOCK_MODE, u32).
32 #if IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR <= 7
33 #define MY_ETLM_READ_ONLY true
35 #define MY_ETLM_READ_ONLY video::ETLM_READ_ONLY
38 inline static void applyShadeFactor(video::SColor& color, float factor)
40 color.setRed(core::clamp(core::round32(color.getRed()*factor), 0, 255));
41 color.setGreen(core::clamp(core::round32(color.getGreen()*factor), 0, 255));
42 color.setBlue(core::clamp(core::round32(color.getBlue()*factor), 0, 255));
45 void applyFacesShading(video::SColor &color, const v3f &normal)
48 Some drawtypes have normals set to (0, 0, 0), this must result in
49 maximum brightness: shade factor 1.0.
50 Shade factors for aligned cube faces are:
53 +-X 0.670820 sqrt(0.45)
54 +-Z 0.836660 sqrt(0.7)
56 float x2 = normal.X * normal.X;
57 float y2 = normal.Y * normal.Y;
58 float z2 = normal.Z * normal.Z;
60 applyShadeFactor(color, 0.670820f * x2 + 0.447213f * y2 + 0.836660f * z2);
61 else if ((x2 > 1e-3) || (z2 > 1e-3))
62 applyShadeFactor(color, 0.670820f * x2 + 1.000000f * y2 + 0.836660f * z2);
65 scene::IAnimatedMesh* createCubeMesh(v3f scale)
67 video::SColor c(255,255,255,255);
68 video::S3DVertex vertices[24] =
71 video::S3DVertex(-0.5,+0.5,-0.5, 0,1,0, c, 0,1),
72 video::S3DVertex(-0.5,+0.5,+0.5, 0,1,0, c, 0,0),
73 video::S3DVertex(+0.5,+0.5,+0.5, 0,1,0, c, 1,0),
74 video::S3DVertex(+0.5,+0.5,-0.5, 0,1,0, c, 1,1),
76 video::S3DVertex(-0.5,-0.5,-0.5, 0,-1,0, c, 0,0),
77 video::S3DVertex(+0.5,-0.5,-0.5, 0,-1,0, c, 1,0),
78 video::S3DVertex(+0.5,-0.5,+0.5, 0,-1,0, c, 1,1),
79 video::S3DVertex(-0.5,-0.5,+0.5, 0,-1,0, c, 0,1),
81 video::S3DVertex(+0.5,-0.5,-0.5, 1,0,0, c, 0,1),
82 video::S3DVertex(+0.5,+0.5,-0.5, 1,0,0, c, 0,0),
83 video::S3DVertex(+0.5,+0.5,+0.5, 1,0,0, c, 1,0),
84 video::S3DVertex(+0.5,-0.5,+0.5, 1,0,0, c, 1,1),
86 video::S3DVertex(-0.5,-0.5,-0.5, -1,0,0, c, 1,1),
87 video::S3DVertex(-0.5,-0.5,+0.5, -1,0,0, c, 0,1),
88 video::S3DVertex(-0.5,+0.5,+0.5, -1,0,0, c, 0,0),
89 video::S3DVertex(-0.5,+0.5,-0.5, -1,0,0, c, 1,0),
91 video::S3DVertex(-0.5,-0.5,+0.5, 0,0,1, c, 1,1),
92 video::S3DVertex(+0.5,-0.5,+0.5, 0,0,1, c, 0,1),
93 video::S3DVertex(+0.5,+0.5,+0.5, 0,0,1, c, 0,0),
94 video::S3DVertex(-0.5,+0.5,+0.5, 0,0,1, c, 1,0),
96 video::S3DVertex(-0.5,-0.5,-0.5, 0,0,-1, c, 0,1),
97 video::S3DVertex(-0.5,+0.5,-0.5, 0,0,-1, c, 0,0),
98 video::S3DVertex(+0.5,+0.5,-0.5, 0,0,-1, c, 1,0),
99 video::S3DVertex(+0.5,-0.5,-0.5, 0,0,-1, c, 1,1),
102 u16 indices[6] = {0,1,2,2,3,0};
104 scene::SMesh *mesh = new scene::SMesh();
105 for (u32 i=0; i<6; ++i)
107 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
108 buf->append(vertices + 4 * i, 4, indices, 6);
109 // Set default material
110 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
111 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
112 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
113 // Add mesh buffer to mesh
114 mesh->addMeshBuffer(buf);
118 scene::SAnimatedMesh *anim_mesh = new scene::SAnimatedMesh(mesh);
120 scaleMesh(anim_mesh, scale); // also recalculates bounding box
124 void scaleMesh(scene::IMesh *mesh, v3f scale)
132 u32 mc = mesh->getMeshBufferCount();
133 for (u32 j = 0; j < mc; j++) {
134 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
135 const u32 stride = getVertexPitchFromType(buf->getVertexType());
136 u32 vertex_count = buf->getVertexCount();
137 u8 *vertices = (u8 *)buf->getVertices();
138 for (u32 i = 0; i < vertex_count; i++)
139 ((video::S3DVertex *)(vertices + i * stride))->Pos *= scale;
141 buf->recalculateBoundingBox();
143 // calculate total bounding box
145 bbox = buf->getBoundingBox();
147 bbox.addInternalBox(buf->getBoundingBox());
149 mesh->setBoundingBox(bbox);
152 void translateMesh(scene::IMesh *mesh, v3f vec)
160 u32 mc = mesh->getMeshBufferCount();
161 for (u32 j = 0; j < mc; j++) {
162 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
163 const u32 stride = getVertexPitchFromType(buf->getVertexType());
164 u32 vertex_count = buf->getVertexCount();
165 u8 *vertices = (u8 *)buf->getVertices();
166 for (u32 i = 0; i < vertex_count; i++)
167 ((video::S3DVertex *)(vertices + i * stride))->Pos += vec;
169 buf->recalculateBoundingBox();
171 // calculate total bounding box
173 bbox = buf->getBoundingBox();
175 bbox.addInternalBox(buf->getBoundingBox());
177 mesh->setBoundingBox(bbox);
180 void setMeshBufferColor(scene::IMeshBuffer *buf, const video::SColor &color)
182 const u32 stride = getVertexPitchFromType(buf->getVertexType());
183 u32 vertex_count = buf->getVertexCount();
184 u8 *vertices = (u8 *) buf->getVertices();
185 for (u32 i = 0; i < vertex_count; i++)
186 ((video::S3DVertex *) (vertices + i * stride))->Color = color;
189 void setAnimatedMeshColor(scene::IAnimatedMeshSceneNode *node, const video::SColor &color)
191 for (u32 i = 0; i < node->getMaterialCount(); ++i) {
192 node->getMaterial(i).EmissiveColor = color;
196 void setMeshColor(scene::IMesh *mesh, const video::SColor &color)
201 u32 mc = mesh->getMeshBufferCount();
202 for (u32 j = 0; j < mc; j++)
203 setMeshBufferColor(mesh->getMeshBuffer(j), color);
206 void setMeshBufferTextureCoords(scene::IMeshBuffer *buf, const v2f *uv, u32 count)
208 const u32 stride = getVertexPitchFromType(buf->getVertexType());
209 assert(buf->getVertexCount() >= count);
210 u8 *vertices = (u8 *) buf->getVertices();
211 for (u32 i = 0; i < count; i++)
212 ((video::S3DVertex*) (vertices + i * stride))->TCoords = uv[i];
215 template <typename F>
216 static void applyToMesh(scene::IMesh *mesh, const F &fn)
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 char *vertices = reinterpret_cast<char *>(buf->getVertices());
224 for (u32 i = 0; i < vertex_count; i++)
225 fn(reinterpret_cast<video::S3DVertex *>(vertices + i * stride));
229 void colorizeMeshBuffer(scene::IMeshBuffer *buf, const video::SColor *buffercolor)
231 const u32 stride = getVertexPitchFromType(buf->getVertexType());
232 u32 vertex_count = buf->getVertexCount();
233 u8 *vertices = (u8 *) buf->getVertices();
234 for (u32 i = 0; i < vertex_count; i++) {
235 video::S3DVertex *vertex = (video::S3DVertex *) (vertices + i * stride);
236 video::SColor *vc = &(vertex->Color);
240 applyFacesShading(*vc, vertex->Normal);
244 void setMeshColorByNormalXYZ(scene::IMesh *mesh,
245 const video::SColor &colorX,
246 const video::SColor &colorY,
247 const video::SColor &colorZ)
251 auto colorizator = [=] (video::S3DVertex *vertex) {
252 f32 x = fabs(vertex->Normal.X);
253 f32 y = fabs(vertex->Normal.Y);
254 f32 z = fabs(vertex->Normal.Z);
255 if (x >= y && x >= z)
256 vertex->Color = colorX;
258 vertex->Color = colorY;
260 vertex->Color = colorZ;
262 applyToMesh(mesh, colorizator);
265 void setMeshColorByNormal(scene::IMesh *mesh, const v3f &normal,
266 const video::SColor &color)
270 auto colorizator = [normal, color] (video::S3DVertex *vertex) {
271 if (vertex->Normal == normal)
272 vertex->Color = color;
274 applyToMesh(mesh, colorizator);
277 template <float v3f::*U, float v3f::*V>
278 static void rotateMesh(scene::IMesh *mesh, float degrees)
280 degrees *= M_PI / 180.0f;
281 float c = std::cos(degrees);
282 float s = std::sin(degrees);
283 auto rotator = [c, s] (video::S3DVertex *vertex) {
284 float u = vertex->Pos.*U;
285 float v = vertex->Pos.*V;
286 vertex->Pos.*U = c * u - s * v;
287 vertex->Pos.*V = s * u + c * v;
289 applyToMesh(mesh, rotator);
292 void rotateMeshXYby(scene::IMesh *mesh, f64 degrees)
294 rotateMesh<&v3f::X, &v3f::Y>(mesh, degrees);
297 void rotateMeshXZby(scene::IMesh *mesh, f64 degrees)
299 rotateMesh<&v3f::X, &v3f::Z>(mesh, degrees);
302 void rotateMeshYZby(scene::IMesh *mesh, f64 degrees)
304 rotateMesh<&v3f::Y, &v3f::Z>(mesh, degrees);
307 void rotateMeshBy6dFacedir(scene::IMesh *mesh, int facedir)
309 int axisdir = facedir >> 2;
312 case 1: rotateMeshXZby(mesh, -90); break;
313 case 2: rotateMeshXZby(mesh, 180); break;
314 case 3: rotateMeshXZby(mesh, 90); break;
317 case 1: rotateMeshYZby(mesh, 90); break; // z+
318 case 2: rotateMeshYZby(mesh, -90); break; // z-
319 case 3: rotateMeshXYby(mesh, -90); break; // x+
320 case 4: rotateMeshXYby(mesh, 90); break; // x-
321 case 5: rotateMeshXYby(mesh, -180); break;
325 void recalculateBoundingBox(scene::IMesh *src_mesh)
329 for (u16 j = 0; j < src_mesh->getMeshBufferCount(); j++) {
330 scene::IMeshBuffer *buf = src_mesh->getMeshBuffer(j);
331 buf->recalculateBoundingBox();
333 bbox = buf->getBoundingBox();
335 bbox.addInternalBox(buf->getBoundingBox());
337 src_mesh->setBoundingBox(bbox);
340 bool checkMeshNormals(scene::IMesh *mesh)
342 u32 buffer_count = mesh->getMeshBufferCount();
344 for (u32 i = 0; i < buffer_count; i++) {
345 scene::IMeshBuffer *buffer = mesh->getMeshBuffer(i);
347 // Here we intentionally check only first normal, assuming that if buffer
348 // has it valid, then most likely all other ones are fine too. We can
349 // check all of the normals to have length, but it seems like an overkill
350 // hurting the performance and covering only really weird broken models.
351 f32 length = buffer->getNormal(0).getLength();
353 if (!std::isfinite(length) || length < 1e-10f)
360 scene::IMeshBuffer* cloneMeshBuffer(scene::IMeshBuffer *mesh_buffer)
362 switch (mesh_buffer->getVertexType()) {
363 case video::EVT_STANDARD: {
364 video::S3DVertex *v = (video::S3DVertex *) mesh_buffer->getVertices();
365 u16 *indices = mesh_buffer->getIndices();
366 scene::SMeshBuffer *cloned_buffer = new scene::SMeshBuffer();
367 cloned_buffer->append(v, mesh_buffer->getVertexCount(), indices,
368 mesh_buffer->getIndexCount());
369 return cloned_buffer;
371 case video::EVT_2TCOORDS: {
372 video::S3DVertex2TCoords *v =
373 (video::S3DVertex2TCoords *) mesh_buffer->getVertices();
374 u16 *indices = mesh_buffer->getIndices();
375 scene::SMeshBufferLightMap *cloned_buffer =
376 new scene::SMeshBufferLightMap();
377 cloned_buffer->append(v, mesh_buffer->getVertexCount(), indices,
378 mesh_buffer->getIndexCount());
379 return cloned_buffer;
381 case video::EVT_TANGENTS: {
382 video::S3DVertexTangents *v =
383 (video::S3DVertexTangents *) mesh_buffer->getVertices();
384 u16 *indices = mesh_buffer->getIndices();
385 scene::SMeshBufferTangents *cloned_buffer =
386 new scene::SMeshBufferTangents();
387 cloned_buffer->append(v, mesh_buffer->getVertexCount(), indices,
388 mesh_buffer->getIndexCount());
389 return cloned_buffer;
392 // This should not happen.
397 scene::SMesh* cloneMesh(scene::IMesh *src_mesh)
399 scene::SMesh* dst_mesh = new scene::SMesh();
400 for (u16 j = 0; j < src_mesh->getMeshBufferCount(); j++) {
401 scene::IMeshBuffer *temp_buf = cloneMeshBuffer(
402 src_mesh->getMeshBuffer(j));
403 dst_mesh->addMeshBuffer(temp_buf);
410 scene::IMesh* convertNodeboxesToMesh(const std::vector<aabb3f> &boxes,
411 const f32 *uv_coords, float expand)
413 scene::SMesh* dst_mesh = new scene::SMesh();
415 for (u16 j = 0; j < 6; j++)
417 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
418 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
419 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
420 dst_mesh->addMeshBuffer(buf);
424 video::SColor c(255,255,255,255);
426 for (aabb3f box : boxes) {
429 box.MinEdge.X -= expand;
430 box.MinEdge.Y -= expand;
431 box.MinEdge.Z -= expand;
432 box.MaxEdge.X += expand;
433 box.MaxEdge.Y += expand;
434 box.MaxEdge.Z += expand;
436 // Compute texture UV coords
437 f32 tx1 = (box.MinEdge.X / BS) + 0.5;
438 f32 ty1 = (box.MinEdge.Y / BS) + 0.5;
439 f32 tz1 = (box.MinEdge.Z / BS) + 0.5;
440 f32 tx2 = (box.MaxEdge.X / BS) + 0.5;
441 f32 ty2 = (box.MaxEdge.Y / BS) + 0.5;
442 f32 tz2 = (box.MaxEdge.Z / BS) + 0.5;
444 f32 txc_default[24] = {
446 tx1, 1 - tz2, tx2, 1 - tz1,
450 tz1, 1 - ty2, tz2, 1 - ty1,
452 1 - tz2, 1 - ty2, 1 - tz1, 1 - ty1,
454 1 - tx2, 1 - ty2, 1 - tx1, 1 - ty1,
456 tx1, 1 - ty2, tx2, 1 - ty1,
459 // use default texture UV mapping if not provided
460 const f32 *txc = uv_coords ? uv_coords : txc_default;
462 v3f min = box.MinEdge;
463 v3f max = box.MaxEdge;
465 video::S3DVertex vertices[24] =
468 video::S3DVertex(min.X,max.Y,max.Z, 0,1,0, c, txc[0],txc[1]),
469 video::S3DVertex(max.X,max.Y,max.Z, 0,1,0, c, txc[2],txc[1]),
470 video::S3DVertex(max.X,max.Y,min.Z, 0,1,0, c, txc[2],txc[3]),
471 video::S3DVertex(min.X,max.Y,min.Z, 0,1,0, c, txc[0],txc[3]),
473 video::S3DVertex(min.X,min.Y,min.Z, 0,-1,0, c, txc[4],txc[5]),
474 video::S3DVertex(max.X,min.Y,min.Z, 0,-1,0, c, txc[6],txc[5]),
475 video::S3DVertex(max.X,min.Y,max.Z, 0,-1,0, c, txc[6],txc[7]),
476 video::S3DVertex(min.X,min.Y,max.Z, 0,-1,0, c, txc[4],txc[7]),
478 video::S3DVertex(max.X,max.Y,min.Z, 1,0,0, c, txc[ 8],txc[9]),
479 video::S3DVertex(max.X,max.Y,max.Z, 1,0,0, c, txc[10],txc[9]),
480 video::S3DVertex(max.X,min.Y,max.Z, 1,0,0, c, txc[10],txc[11]),
481 video::S3DVertex(max.X,min.Y,min.Z, 1,0,0, c, txc[ 8],txc[11]),
483 video::S3DVertex(min.X,max.Y,max.Z, -1,0,0, c, txc[12],txc[13]),
484 video::S3DVertex(min.X,max.Y,min.Z, -1,0,0, c, txc[14],txc[13]),
485 video::S3DVertex(min.X,min.Y,min.Z, -1,0,0, c, txc[14],txc[15]),
486 video::S3DVertex(min.X,min.Y,max.Z, -1,0,0, c, txc[12],txc[15]),
488 video::S3DVertex(max.X,max.Y,max.Z, 0,0,1, c, txc[16],txc[17]),
489 video::S3DVertex(min.X,max.Y,max.Z, 0,0,1, c, txc[18],txc[17]),
490 video::S3DVertex(min.X,min.Y,max.Z, 0,0,1, c, txc[18],txc[19]),
491 video::S3DVertex(max.X,min.Y,max.Z, 0,0,1, c, txc[16],txc[19]),
493 video::S3DVertex(min.X,max.Y,min.Z, 0,0,-1, c, txc[20],txc[21]),
494 video::S3DVertex(max.X,max.Y,min.Z, 0,0,-1, c, txc[22],txc[21]),
495 video::S3DVertex(max.X,min.Y,min.Z, 0,0,-1, c, txc[22],txc[23]),
496 video::S3DVertex(min.X,min.Y,min.Z, 0,0,-1, c, txc[20],txc[23]),
499 u16 indices[] = {0,1,2,2,3,0};
501 for(u16 j = 0; j < 24; j += 4)
503 scene::IMeshBuffer *buf = dst_mesh->getMeshBuffer(j / 4);
504 buf->append(vertices + j, 4, indices, 6);
512 core::array<u32> tris;
525 const u16 cachesize = 32;
527 float FindVertexScore(vcache *v)
529 const float CacheDecayPower = 1.5f;
530 const float LastTriScore = 0.75f;
531 const float ValenceBoostScale = 2.0f;
532 const float ValenceBoostPower = 0.5f;
533 const float MaxSizeVertexCache = 32.0f;
535 if (v->NumActiveTris == 0)
537 // No tri needs this vertex!
542 int CachePosition = v->cachepos;
543 if (CachePosition < 0)
545 // Vertex is not in FIFO cache - no score.
549 if (CachePosition < 3)
551 // This vertex was used in the last triangle,
552 // so it has a fixed score.
553 Score = LastTriScore;
557 // Points for being high in the cache.
558 const float Scaler = 1.0f / (MaxSizeVertexCache - 3);
559 Score = 1.0f - (CachePosition - 3) * Scaler;
560 Score = powf(Score, CacheDecayPower);
564 // Bonus points for having a low number of tris still to
565 // use the vert, so we get rid of lone verts quickly.
566 float ValenceBoost = powf(v->NumActiveTris,
568 Score += ValenceBoostScale * ValenceBoost;
574 A specialized LRU cache for the Forsyth algorithm.
581 f_lru(vcache *v, tcache *t): vc(v), tc(t)
583 for (int &i : cache) {
588 // Adds this vertex index and returns the highest-scoring triangle index
589 u32 add(u16 vert, bool updatetris = false)
593 // Mark existing pos as empty
594 for (u16 i = 0; i < cachesize; i++)
596 if (cache[i] == vert)
598 // Move everything down
599 for (u16 j = i; j; j--)
601 cache[j] = cache[j - 1];
611 if (cache[cachesize-1] != -1)
612 vc[cache[cachesize-1]].cachepos = -1;
614 // Move everything down
615 for (u16 i = cachesize - 1; i; i--)
617 cache[i] = cache[i - 1];
628 // Update cache positions
629 for (u16 i = 0; i < cachesize; i++)
634 vc[cache[i]].cachepos = i;
635 vc[cache[i]].score = FindVertexScore(&vc[cache[i]]);
638 // Update triangle scores
639 for (int i : cache) {
643 const u16 trisize = vc[i].tris.size();
644 for (u16 t = 0; t < trisize; t++)
646 tcache *tri = &tc[vc[i].tris[t]];
649 vc[tri->ind[0]].score +
650 vc[tri->ind[1]].score +
651 vc[tri->ind[2]].score;
653 if (tri->score > hiscore)
655 hiscore = tri->score;
656 highest = vc[i].tris[t];
666 s32 cache[cachesize];
672 Vertex cache optimization according to the Forsyth paper:
673 http://home.comcast.net/~tom_forsyth/papers/fast_vert_cache_opt.html
675 The function is thread-safe (read: you can optimize several meshes in different threads)
677 \param mesh Source mesh for the operation. */
678 scene::IMesh* createForsythOptimizedMesh(const scene::IMesh *mesh)
683 scene::SMesh *newmesh = new scene::SMesh();
684 newmesh->BoundingBox = mesh->getBoundingBox();
686 const u32 mbcount = mesh->getMeshBufferCount();
688 for (u32 b = 0; b < mbcount; ++b)
690 const scene::IMeshBuffer *mb = mesh->getMeshBuffer(b);
692 if (mb->getIndexType() != video::EIT_16BIT)
694 //os::Printer::log("Cannot optimize a mesh with 32bit indices", ELL_ERROR);
699 const u32 icount = mb->getIndexCount();
700 const u32 tcount = icount / 3;
701 const u32 vcount = mb->getVertexCount();
702 const u16 *ind = mb->getIndices();
704 vcache *vc = new vcache[vcount];
705 tcache *tc = new tcache[tcount];
710 for (u16 i = 0; i < vcount; i++)
714 vc[i].NumActiveTris = 0;
717 // First pass: count how many times a vert is used
718 for (u32 i = 0; i < icount; i += 3)
720 vc[ind[i]].NumActiveTris++;
721 vc[ind[i + 1]].NumActiveTris++;
722 vc[ind[i + 2]].NumActiveTris++;
724 const u32 tri_ind = i/3;
725 tc[tri_ind].ind[0] = ind[i];
726 tc[tri_ind].ind[1] = ind[i + 1];
727 tc[tri_ind].ind[2] = ind[i + 2];
730 // Second pass: list of each triangle
731 for (u32 i = 0; i < tcount; i++)
733 vc[tc[i].ind[0]].tris.push_back(i);
734 vc[tc[i].ind[1]].tris.push_back(i);
735 vc[tc[i].ind[2]].tris.push_back(i);
740 // Give initial scores
741 for (u16 i = 0; i < vcount; i++)
743 vc[i].score = FindVertexScore(&vc[i]);
745 for (u32 i = 0; i < tcount; i++)
748 vc[tc[i].ind[0]].score +
749 vc[tc[i].ind[1]].score +
750 vc[tc[i].ind[2]].score;
753 switch(mb->getVertexType())
755 case video::EVT_STANDARD:
757 video::S3DVertex *v = (video::S3DVertex *) mb->getVertices();
759 scene::SMeshBuffer *buf = new scene::SMeshBuffer();
760 buf->Material = mb->getMaterial();
762 buf->Vertices.reallocate(vcount);
763 buf->Indices.reallocate(icount);
765 core::map<const video::S3DVertex, const u16> sind; // search index for fast operation
766 typedef core::map<const video::S3DVertex, const u16>::Node snode;
773 if (tc[highest].drawn)
777 for (u32 t = 0; t < tcount; t++)
781 if (tc[t].score > hiscore)
784 hiscore = tc[t].score;
793 // Output the best triangle
794 u16 newind = buf->Vertices.size();
796 snode *s = sind.find(v[tc[highest].ind[0]]);
800 buf->Vertices.push_back(v[tc[highest].ind[0]]);
801 buf->Indices.push_back(newind);
802 sind.insert(v[tc[highest].ind[0]], newind);
807 buf->Indices.push_back(s->getValue());
810 s = sind.find(v[tc[highest].ind[1]]);
814 buf->Vertices.push_back(v[tc[highest].ind[1]]);
815 buf->Indices.push_back(newind);
816 sind.insert(v[tc[highest].ind[1]], newind);
821 buf->Indices.push_back(s->getValue());
824 s = sind.find(v[tc[highest].ind[2]]);
828 buf->Vertices.push_back(v[tc[highest].ind[2]]);
829 buf->Indices.push_back(newind);
830 sind.insert(v[tc[highest].ind[2]], newind);
834 buf->Indices.push_back(s->getValue());
837 vc[tc[highest].ind[0]].NumActiveTris--;
838 vc[tc[highest].ind[1]].NumActiveTris--;
839 vc[tc[highest].ind[2]].NumActiveTris--;
841 tc[highest].drawn = true;
843 for (u16 j : tc[highest].ind) {
844 vcache *vert = &vc[j];
845 for (u16 t = 0; t < vert->tris.size(); t++)
847 if (highest == vert->tris[t])
855 lru.add(tc[highest].ind[0]);
856 lru.add(tc[highest].ind[1]);
857 highest = lru.add(tc[highest].ind[2], true);
861 buf->setBoundingBox(mb->getBoundingBox());
862 newmesh->addMeshBuffer(buf);
866 case video::EVT_2TCOORDS:
868 video::S3DVertex2TCoords *v = (video::S3DVertex2TCoords *) mb->getVertices();
870 scene::SMeshBufferLightMap *buf = new scene::SMeshBufferLightMap();
871 buf->Material = mb->getMaterial();
873 buf->Vertices.reallocate(vcount);
874 buf->Indices.reallocate(icount);
876 core::map<const video::S3DVertex2TCoords, const u16> sind; // search index for fast operation
877 typedef core::map<const video::S3DVertex2TCoords, const u16>::Node snode;
884 if (tc[highest].drawn)
888 for (u32 t = 0; t < tcount; t++)
892 if (tc[t].score > hiscore)
895 hiscore = tc[t].score;
904 // Output the best triangle
905 u16 newind = buf->Vertices.size();
907 snode *s = sind.find(v[tc[highest].ind[0]]);
911 buf->Vertices.push_back(v[tc[highest].ind[0]]);
912 buf->Indices.push_back(newind);
913 sind.insert(v[tc[highest].ind[0]], newind);
918 buf->Indices.push_back(s->getValue());
921 s = sind.find(v[tc[highest].ind[1]]);
925 buf->Vertices.push_back(v[tc[highest].ind[1]]);
926 buf->Indices.push_back(newind);
927 sind.insert(v[tc[highest].ind[1]], newind);
932 buf->Indices.push_back(s->getValue());
935 s = sind.find(v[tc[highest].ind[2]]);
939 buf->Vertices.push_back(v[tc[highest].ind[2]]);
940 buf->Indices.push_back(newind);
941 sind.insert(v[tc[highest].ind[2]], newind);
945 buf->Indices.push_back(s->getValue());
948 vc[tc[highest].ind[0]].NumActiveTris--;
949 vc[tc[highest].ind[1]].NumActiveTris--;
950 vc[tc[highest].ind[2]].NumActiveTris--;
952 tc[highest].drawn = true;
954 for (u16 j : tc[highest].ind) {
955 vcache *vert = &vc[j];
956 for (u16 t = 0; t < vert->tris.size(); t++)
958 if (highest == vert->tris[t])
966 lru.add(tc[highest].ind[0]);
967 lru.add(tc[highest].ind[1]);
968 highest = lru.add(tc[highest].ind[2]);
972 buf->setBoundingBox(mb->getBoundingBox());
973 newmesh->addMeshBuffer(buf);
978 case video::EVT_TANGENTS:
980 video::S3DVertexTangents *v = (video::S3DVertexTangents *) mb->getVertices();
982 scene::SMeshBufferTangents *buf = new scene::SMeshBufferTangents();
983 buf->Material = mb->getMaterial();
985 buf->Vertices.reallocate(vcount);
986 buf->Indices.reallocate(icount);
988 core::map<const video::S3DVertexTangents, const u16> sind; // search index for fast operation
989 typedef core::map<const video::S3DVertexTangents, const u16>::Node snode;
996 if (tc[highest].drawn)
1000 for (u32 t = 0; t < tcount; t++)
1004 if (tc[t].score > hiscore)
1007 hiscore = tc[t].score;
1016 // Output the best triangle
1017 u16 newind = buf->Vertices.size();
1019 snode *s = sind.find(v[tc[highest].ind[0]]);
1023 buf->Vertices.push_back(v[tc[highest].ind[0]]);
1024 buf->Indices.push_back(newind);
1025 sind.insert(v[tc[highest].ind[0]], newind);
1030 buf->Indices.push_back(s->getValue());
1033 s = sind.find(v[tc[highest].ind[1]]);
1037 buf->Vertices.push_back(v[tc[highest].ind[1]]);
1038 buf->Indices.push_back(newind);
1039 sind.insert(v[tc[highest].ind[1]], newind);
1044 buf->Indices.push_back(s->getValue());
1047 s = sind.find(v[tc[highest].ind[2]]);
1051 buf->Vertices.push_back(v[tc[highest].ind[2]]);
1052 buf->Indices.push_back(newind);
1053 sind.insert(v[tc[highest].ind[2]], newind);
1057 buf->Indices.push_back(s->getValue());
1060 vc[tc[highest].ind[0]].NumActiveTris--;
1061 vc[tc[highest].ind[1]].NumActiveTris--;
1062 vc[tc[highest].ind[2]].NumActiveTris--;
1064 tc[highest].drawn = true;
1066 for (u16 j : tc[highest].ind) {
1067 vcache *vert = &vc[j];
1068 for (u16 t = 0; t < vert->tris.size(); t++)
1070 if (highest == vert->tris[t])
1072 vert->tris.erase(t);
1078 lru.add(tc[highest].ind[0]);
1079 lru.add(tc[highest].ind[1]);
1080 highest = lru.add(tc[highest].ind[2]);
1084 buf->setBoundingBox(mb->getBoundingBox());
1085 newmesh->addMeshBuffer(buf);
1094 } // for each meshbuffer