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 template <typename F>
207 static void applyToMesh(scene::IMesh *mesh, const F &fn)
209 u16 mc = mesh->getMeshBufferCount();
210 for (u16 j = 0; j < mc; j++) {
211 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
212 const u32 stride = getVertexPitchFromType(buf->getVertexType());
213 u32 vertex_count = buf->getVertexCount();
214 char *vertices = reinterpret_cast<char *>(buf->getVertices());
215 for (u32 i = 0; i < vertex_count; i++)
216 fn(reinterpret_cast<video::S3DVertex *>(vertices + i * stride));
220 void colorizeMeshBuffer(scene::IMeshBuffer *buf, const video::SColor *buffercolor)
222 const u32 stride = getVertexPitchFromType(buf->getVertexType());
223 u32 vertex_count = buf->getVertexCount();
224 u8 *vertices = (u8 *) buf->getVertices();
225 for (u32 i = 0; i < vertex_count; i++) {
226 video::S3DVertex *vertex = (video::S3DVertex *) (vertices + i * stride);
227 video::SColor *vc = &(vertex->Color);
231 applyFacesShading(*vc, vertex->Normal);
235 void setMeshColorByNormalXYZ(scene::IMesh *mesh,
236 const video::SColor &colorX,
237 const video::SColor &colorY,
238 const video::SColor &colorZ)
242 auto colorizator = [=] (video::S3DVertex *vertex) {
243 f32 x = fabs(vertex->Normal.X);
244 f32 y = fabs(vertex->Normal.Y);
245 f32 z = fabs(vertex->Normal.Z);
246 if (x >= y && x >= z)
247 vertex->Color = colorX;
249 vertex->Color = colorY;
251 vertex->Color = colorZ;
253 applyToMesh(mesh, colorizator);
256 void setMeshColorByNormal(scene::IMesh *mesh, const v3f &normal,
257 const video::SColor &color)
261 auto colorizator = [normal, color] (video::S3DVertex *vertex) {
262 if (vertex->Normal == normal)
263 vertex->Color = color;
265 applyToMesh(mesh, colorizator);
268 template <float v3f::*U, float v3f::*V>
269 static void rotateMesh(scene::IMesh *mesh, float degrees)
271 degrees *= M_PI / 180.0f;
272 float c = std::cos(degrees);
273 float s = std::sin(degrees);
274 auto rotator = [c, s] (video::S3DVertex *vertex) {
275 float u = vertex->Pos.*U;
276 float v = vertex->Pos.*V;
277 vertex->Pos.*U = c * u - s * v;
278 vertex->Pos.*V = s * u + c * v;
280 applyToMesh(mesh, rotator);
283 void rotateMeshXYby(scene::IMesh *mesh, f64 degrees)
285 rotateMesh<&v3f::X, &v3f::Y>(mesh, degrees);
288 void rotateMeshXZby(scene::IMesh *mesh, f64 degrees)
290 rotateMesh<&v3f::X, &v3f::Z>(mesh, degrees);
293 void rotateMeshYZby(scene::IMesh *mesh, f64 degrees)
295 rotateMesh<&v3f::Y, &v3f::Z>(mesh, degrees);
298 void rotateMeshBy6dFacedir(scene::IMesh *mesh, int facedir)
300 int axisdir = facedir >> 2;
303 case 1: rotateMeshXZby(mesh, -90); break;
304 case 2: rotateMeshXZby(mesh, 180); break;
305 case 3: rotateMeshXZby(mesh, 90); break;
308 case 1: rotateMeshYZby(mesh, 90); break; // z+
309 case 2: rotateMeshYZby(mesh, -90); break; // z-
310 case 3: rotateMeshXYby(mesh, -90); break; // x+
311 case 4: rotateMeshXYby(mesh, 90); break; // x-
312 case 5: rotateMeshXYby(mesh, -180); break;
316 void recalculateBoundingBox(scene::IMesh *src_mesh)
320 for (u16 j = 0; j < src_mesh->getMeshBufferCount(); j++) {
321 scene::IMeshBuffer *buf = src_mesh->getMeshBuffer(j);
322 buf->recalculateBoundingBox();
324 bbox = buf->getBoundingBox();
326 bbox.addInternalBox(buf->getBoundingBox());
328 src_mesh->setBoundingBox(bbox);
331 scene::IMeshBuffer* cloneMeshBuffer(scene::IMeshBuffer *mesh_buffer)
333 switch (mesh_buffer->getVertexType()) {
334 case video::EVT_STANDARD: {
335 video::S3DVertex *v = (video::S3DVertex *) mesh_buffer->getVertices();
336 u16 *indices = mesh_buffer->getIndices();
337 scene::SMeshBuffer *cloned_buffer = new scene::SMeshBuffer();
338 cloned_buffer->append(v, mesh_buffer->getVertexCount(), indices,
339 mesh_buffer->getIndexCount());
340 return cloned_buffer;
342 case video::EVT_2TCOORDS: {
343 video::S3DVertex2TCoords *v =
344 (video::S3DVertex2TCoords *) mesh_buffer->getVertices();
345 u16 *indices = mesh_buffer->getIndices();
346 scene::SMeshBufferLightMap *cloned_buffer =
347 new scene::SMeshBufferLightMap();
348 cloned_buffer->append(v, mesh_buffer->getVertexCount(), indices,
349 mesh_buffer->getIndexCount());
350 return cloned_buffer;
352 case video::EVT_TANGENTS: {
353 video::S3DVertexTangents *v =
354 (video::S3DVertexTangents *) mesh_buffer->getVertices();
355 u16 *indices = mesh_buffer->getIndices();
356 scene::SMeshBufferTangents *cloned_buffer =
357 new scene::SMeshBufferTangents();
358 cloned_buffer->append(v, mesh_buffer->getVertexCount(), indices,
359 mesh_buffer->getIndexCount());
360 return cloned_buffer;
363 // This should not happen.
368 scene::SMesh* cloneMesh(scene::IMesh *src_mesh)
370 scene::SMesh* dst_mesh = new scene::SMesh();
371 for (u16 j = 0; j < src_mesh->getMeshBufferCount(); j++) {
372 scene::IMeshBuffer *temp_buf = cloneMeshBuffer(
373 src_mesh->getMeshBuffer(j));
374 dst_mesh->addMeshBuffer(temp_buf);
381 scene::IMesh* convertNodeboxesToMesh(const std::vector<aabb3f> &boxes,
382 const f32 *uv_coords, float expand)
384 scene::SMesh* dst_mesh = new scene::SMesh();
386 for (u16 j = 0; j < 6; j++)
388 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
389 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
390 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
391 dst_mesh->addMeshBuffer(buf);
395 video::SColor c(255,255,255,255);
397 for (aabb3f box : boxes) {
400 box.MinEdge.X -= expand;
401 box.MinEdge.Y -= expand;
402 box.MinEdge.Z -= expand;
403 box.MaxEdge.X += expand;
404 box.MaxEdge.Y += expand;
405 box.MaxEdge.Z += expand;
407 // Compute texture UV coords
408 f32 tx1 = (box.MinEdge.X / BS) + 0.5;
409 f32 ty1 = (box.MinEdge.Y / BS) + 0.5;
410 f32 tz1 = (box.MinEdge.Z / BS) + 0.5;
411 f32 tx2 = (box.MaxEdge.X / BS) + 0.5;
412 f32 ty2 = (box.MaxEdge.Y / BS) + 0.5;
413 f32 tz2 = (box.MaxEdge.Z / BS) + 0.5;
415 f32 txc_default[24] = {
417 tx1, 1 - tz2, tx2, 1 - tz1,
421 tz1, 1 - ty2, tz2, 1 - ty1,
423 1 - tz2, 1 - ty2, 1 - tz1, 1 - ty1,
425 1 - tx2, 1 - ty2, 1 - tx1, 1 - ty1,
427 tx1, 1 - ty2, tx2, 1 - ty1,
430 // use default texture UV mapping if not provided
431 const f32 *txc = uv_coords ? uv_coords : txc_default;
433 v3f min = box.MinEdge;
434 v3f max = box.MaxEdge;
436 video::S3DVertex vertices[24] =
439 video::S3DVertex(min.X,max.Y,max.Z, 0,1,0, c, txc[0],txc[1]),
440 video::S3DVertex(max.X,max.Y,max.Z, 0,1,0, c, txc[2],txc[1]),
441 video::S3DVertex(max.X,max.Y,min.Z, 0,1,0, c, txc[2],txc[3]),
442 video::S3DVertex(min.X,max.Y,min.Z, 0,1,0, c, txc[0],txc[3]),
444 video::S3DVertex(min.X,min.Y,min.Z, 0,-1,0, c, txc[4],txc[5]),
445 video::S3DVertex(max.X,min.Y,min.Z, 0,-1,0, c, txc[6],txc[5]),
446 video::S3DVertex(max.X,min.Y,max.Z, 0,-1,0, c, txc[6],txc[7]),
447 video::S3DVertex(min.X,min.Y,max.Z, 0,-1,0, c, txc[4],txc[7]),
449 video::S3DVertex(max.X,max.Y,min.Z, 1,0,0, c, txc[ 8],txc[9]),
450 video::S3DVertex(max.X,max.Y,max.Z, 1,0,0, c, txc[10],txc[9]),
451 video::S3DVertex(max.X,min.Y,max.Z, 1,0,0, c, txc[10],txc[11]),
452 video::S3DVertex(max.X,min.Y,min.Z, 1,0,0, c, txc[ 8],txc[11]),
454 video::S3DVertex(min.X,max.Y,max.Z, -1,0,0, c, txc[12],txc[13]),
455 video::S3DVertex(min.X,max.Y,min.Z, -1,0,0, c, txc[14],txc[13]),
456 video::S3DVertex(min.X,min.Y,min.Z, -1,0,0, c, txc[14],txc[15]),
457 video::S3DVertex(min.X,min.Y,max.Z, -1,0,0, c, txc[12],txc[15]),
459 video::S3DVertex(max.X,max.Y,max.Z, 0,0,1, c, txc[16],txc[17]),
460 video::S3DVertex(min.X,max.Y,max.Z, 0,0,1, c, txc[18],txc[17]),
461 video::S3DVertex(min.X,min.Y,max.Z, 0,0,1, c, txc[18],txc[19]),
462 video::S3DVertex(max.X,min.Y,max.Z, 0,0,1, c, txc[16],txc[19]),
464 video::S3DVertex(min.X,max.Y,min.Z, 0,0,-1, c, txc[20],txc[21]),
465 video::S3DVertex(max.X,max.Y,min.Z, 0,0,-1, c, txc[22],txc[21]),
466 video::S3DVertex(max.X,min.Y,min.Z, 0,0,-1, c, txc[22],txc[23]),
467 video::S3DVertex(min.X,min.Y,min.Z, 0,0,-1, c, txc[20],txc[23]),
470 u16 indices[] = {0,1,2,2,3,0};
472 for(u16 j = 0; j < 24; j += 4)
474 scene::IMeshBuffer *buf = dst_mesh->getMeshBuffer(j / 4);
475 buf->append(vertices + j, 4, indices, 6);
483 core::array<u32> tris;
496 const u16 cachesize = 32;
498 float FindVertexScore(vcache *v)
500 const float CacheDecayPower = 1.5f;
501 const float LastTriScore = 0.75f;
502 const float ValenceBoostScale = 2.0f;
503 const float ValenceBoostPower = 0.5f;
504 const float MaxSizeVertexCache = 32.0f;
506 if (v->NumActiveTris == 0)
508 // No tri needs this vertex!
513 int CachePosition = v->cachepos;
514 if (CachePosition < 0)
516 // Vertex is not in FIFO cache - no score.
520 if (CachePosition < 3)
522 // This vertex was used in the last triangle,
523 // so it has a fixed score.
524 Score = LastTriScore;
528 // Points for being high in the cache.
529 const float Scaler = 1.0f / (MaxSizeVertexCache - 3);
530 Score = 1.0f - (CachePosition - 3) * Scaler;
531 Score = powf(Score, CacheDecayPower);
535 // Bonus points for having a low number of tris still to
536 // use the vert, so we get rid of lone verts quickly.
537 float ValenceBoost = powf(v->NumActiveTris,
539 Score += ValenceBoostScale * ValenceBoost;
545 A specialized LRU cache for the Forsyth algorithm.
552 f_lru(vcache *v, tcache *t): vc(v), tc(t)
554 for (int &i : cache) {
559 // Adds this vertex index and returns the highest-scoring triangle index
560 u32 add(u16 vert, bool updatetris = false)
564 // Mark existing pos as empty
565 for (u16 i = 0; i < cachesize; i++)
567 if (cache[i] == vert)
569 // Move everything down
570 for (u16 j = i; j; j--)
572 cache[j] = cache[j - 1];
582 if (cache[cachesize-1] != -1)
583 vc[cache[cachesize-1]].cachepos = -1;
585 // Move everything down
586 for (u16 i = cachesize - 1; i; i--)
588 cache[i] = cache[i - 1];
599 // Update cache positions
600 for (u16 i = 0; i < cachesize; i++)
605 vc[cache[i]].cachepos = i;
606 vc[cache[i]].score = FindVertexScore(&vc[cache[i]]);
609 // Update triangle scores
610 for (int i : cache) {
614 const u16 trisize = vc[i].tris.size();
615 for (u16 t = 0; t < trisize; t++)
617 tcache *tri = &tc[vc[i].tris[t]];
620 vc[tri->ind[0]].score +
621 vc[tri->ind[1]].score +
622 vc[tri->ind[2]].score;
624 if (tri->score > hiscore)
626 hiscore = tri->score;
627 highest = vc[i].tris[t];
637 s32 cache[cachesize];
643 Vertex cache optimization according to the Forsyth paper:
644 http://home.comcast.net/~tom_forsyth/papers/fast_vert_cache_opt.html
646 The function is thread-safe (read: you can optimize several meshes in different threads)
648 \param mesh Source mesh for the operation. */
649 scene::IMesh* createForsythOptimizedMesh(const scene::IMesh *mesh)
654 scene::SMesh *newmesh = new scene::SMesh();
655 newmesh->BoundingBox = mesh->getBoundingBox();
657 const u32 mbcount = mesh->getMeshBufferCount();
659 for (u32 b = 0; b < mbcount; ++b)
661 const scene::IMeshBuffer *mb = mesh->getMeshBuffer(b);
663 if (mb->getIndexType() != video::EIT_16BIT)
665 //os::Printer::log("Cannot optimize a mesh with 32bit indices", ELL_ERROR);
670 const u32 icount = mb->getIndexCount();
671 const u32 tcount = icount / 3;
672 const u32 vcount = mb->getVertexCount();
673 const u16 *ind = mb->getIndices();
675 vcache *vc = new vcache[vcount];
676 tcache *tc = new tcache[tcount];
681 for (u16 i = 0; i < vcount; i++)
685 vc[i].NumActiveTris = 0;
688 // First pass: count how many times a vert is used
689 for (u32 i = 0; i < icount; i += 3)
691 vc[ind[i]].NumActiveTris++;
692 vc[ind[i + 1]].NumActiveTris++;
693 vc[ind[i + 2]].NumActiveTris++;
695 const u32 tri_ind = i/3;
696 tc[tri_ind].ind[0] = ind[i];
697 tc[tri_ind].ind[1] = ind[i + 1];
698 tc[tri_ind].ind[2] = ind[i + 2];
701 // Second pass: list of each triangle
702 for (u32 i = 0; i < tcount; i++)
704 vc[tc[i].ind[0]].tris.push_back(i);
705 vc[tc[i].ind[1]].tris.push_back(i);
706 vc[tc[i].ind[2]].tris.push_back(i);
711 // Give initial scores
712 for (u16 i = 0; i < vcount; i++)
714 vc[i].score = FindVertexScore(&vc[i]);
716 for (u32 i = 0; i < tcount; i++)
719 vc[tc[i].ind[0]].score +
720 vc[tc[i].ind[1]].score +
721 vc[tc[i].ind[2]].score;
724 switch(mb->getVertexType())
726 case video::EVT_STANDARD:
728 video::S3DVertex *v = (video::S3DVertex *) mb->getVertices();
730 scene::SMeshBuffer *buf = new scene::SMeshBuffer();
731 buf->Material = mb->getMaterial();
733 buf->Vertices.reallocate(vcount);
734 buf->Indices.reallocate(icount);
736 core::map<const video::S3DVertex, const u16> sind; // search index for fast operation
737 typedef core::map<const video::S3DVertex, const u16>::Node snode;
744 if (tc[highest].drawn)
748 for (u32 t = 0; t < tcount; t++)
752 if (tc[t].score > hiscore)
755 hiscore = tc[t].score;
764 // Output the best triangle
765 u16 newind = buf->Vertices.size();
767 snode *s = sind.find(v[tc[highest].ind[0]]);
771 buf->Vertices.push_back(v[tc[highest].ind[0]]);
772 buf->Indices.push_back(newind);
773 sind.insert(v[tc[highest].ind[0]], newind);
778 buf->Indices.push_back(s->getValue());
781 s = sind.find(v[tc[highest].ind[1]]);
785 buf->Vertices.push_back(v[tc[highest].ind[1]]);
786 buf->Indices.push_back(newind);
787 sind.insert(v[tc[highest].ind[1]], newind);
792 buf->Indices.push_back(s->getValue());
795 s = sind.find(v[tc[highest].ind[2]]);
799 buf->Vertices.push_back(v[tc[highest].ind[2]]);
800 buf->Indices.push_back(newind);
801 sind.insert(v[tc[highest].ind[2]], newind);
805 buf->Indices.push_back(s->getValue());
808 vc[tc[highest].ind[0]].NumActiveTris--;
809 vc[tc[highest].ind[1]].NumActiveTris--;
810 vc[tc[highest].ind[2]].NumActiveTris--;
812 tc[highest].drawn = true;
814 for (u16 j : tc[highest].ind) {
815 vcache *vert = &vc[j];
816 for (u16 t = 0; t < vert->tris.size(); t++)
818 if (highest == vert->tris[t])
826 lru.add(tc[highest].ind[0]);
827 lru.add(tc[highest].ind[1]);
828 highest = lru.add(tc[highest].ind[2], true);
832 buf->setBoundingBox(mb->getBoundingBox());
833 newmesh->addMeshBuffer(buf);
837 case video::EVT_2TCOORDS:
839 video::S3DVertex2TCoords *v = (video::S3DVertex2TCoords *) mb->getVertices();
841 scene::SMeshBufferLightMap *buf = new scene::SMeshBufferLightMap();
842 buf->Material = mb->getMaterial();
844 buf->Vertices.reallocate(vcount);
845 buf->Indices.reallocate(icount);
847 core::map<const video::S3DVertex2TCoords, const u16> sind; // search index for fast operation
848 typedef core::map<const video::S3DVertex2TCoords, const u16>::Node snode;
855 if (tc[highest].drawn)
859 for (u32 t = 0; t < tcount; t++)
863 if (tc[t].score > hiscore)
866 hiscore = tc[t].score;
875 // Output the best triangle
876 u16 newind = buf->Vertices.size();
878 snode *s = sind.find(v[tc[highest].ind[0]]);
882 buf->Vertices.push_back(v[tc[highest].ind[0]]);
883 buf->Indices.push_back(newind);
884 sind.insert(v[tc[highest].ind[0]], newind);
889 buf->Indices.push_back(s->getValue());
892 s = sind.find(v[tc[highest].ind[1]]);
896 buf->Vertices.push_back(v[tc[highest].ind[1]]);
897 buf->Indices.push_back(newind);
898 sind.insert(v[tc[highest].ind[1]], newind);
903 buf->Indices.push_back(s->getValue());
906 s = sind.find(v[tc[highest].ind[2]]);
910 buf->Vertices.push_back(v[tc[highest].ind[2]]);
911 buf->Indices.push_back(newind);
912 sind.insert(v[tc[highest].ind[2]], newind);
916 buf->Indices.push_back(s->getValue());
919 vc[tc[highest].ind[0]].NumActiveTris--;
920 vc[tc[highest].ind[1]].NumActiveTris--;
921 vc[tc[highest].ind[2]].NumActiveTris--;
923 tc[highest].drawn = true;
925 for (u16 j : tc[highest].ind) {
926 vcache *vert = &vc[j];
927 for (u16 t = 0; t < vert->tris.size(); t++)
929 if (highest == vert->tris[t])
937 lru.add(tc[highest].ind[0]);
938 lru.add(tc[highest].ind[1]);
939 highest = lru.add(tc[highest].ind[2]);
943 buf->setBoundingBox(mb->getBoundingBox());
944 newmesh->addMeshBuffer(buf);
949 case video::EVT_TANGENTS:
951 video::S3DVertexTangents *v = (video::S3DVertexTangents *) mb->getVertices();
953 scene::SMeshBufferTangents *buf = new scene::SMeshBufferTangents();
954 buf->Material = mb->getMaterial();
956 buf->Vertices.reallocate(vcount);
957 buf->Indices.reallocate(icount);
959 core::map<const video::S3DVertexTangents, const u16> sind; // search index for fast operation
960 typedef core::map<const video::S3DVertexTangents, const u16>::Node snode;
967 if (tc[highest].drawn)
971 for (u32 t = 0; t < tcount; t++)
975 if (tc[t].score > hiscore)
978 hiscore = tc[t].score;
987 // Output the best triangle
988 u16 newind = buf->Vertices.size();
990 snode *s = sind.find(v[tc[highest].ind[0]]);
994 buf->Vertices.push_back(v[tc[highest].ind[0]]);
995 buf->Indices.push_back(newind);
996 sind.insert(v[tc[highest].ind[0]], newind);
1001 buf->Indices.push_back(s->getValue());
1004 s = sind.find(v[tc[highest].ind[1]]);
1008 buf->Vertices.push_back(v[tc[highest].ind[1]]);
1009 buf->Indices.push_back(newind);
1010 sind.insert(v[tc[highest].ind[1]], newind);
1015 buf->Indices.push_back(s->getValue());
1018 s = sind.find(v[tc[highest].ind[2]]);
1022 buf->Vertices.push_back(v[tc[highest].ind[2]]);
1023 buf->Indices.push_back(newind);
1024 sind.insert(v[tc[highest].ind[2]], newind);
1028 buf->Indices.push_back(s->getValue());
1031 vc[tc[highest].ind[0]].NumActiveTris--;
1032 vc[tc[highest].ind[1]].NumActiveTris--;
1033 vc[tc[highest].ind[2]].NumActiveTris--;
1035 tc[highest].drawn = true;
1037 for (u16 j : tc[highest].ind) {
1038 vcache *vert = &vc[j];
1039 for (u16 t = 0; t < vert->tris.size(); t++)
1041 if (highest == vert->tris[t])
1043 vert->tris.erase(t);
1049 lru.add(tc[highest].ind[0]);
1050 lru.add(tc[highest].ind[1]);
1051 highest = lru.add(tc[highest].ind[2]);
1055 buf->setBoundingBox(mb->getBoundingBox());
1056 newmesh->addMeshBuffer(buf);
1065 } // for each meshbuffer