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 inline static void applyShadeFactor(video::SColor& color, float factor)
32 color.setRed(core::clamp(core::round32(color.getRed()*factor), 0, 255));
33 color.setGreen(core::clamp(core::round32(color.getGreen()*factor), 0, 255));
34 color.setBlue(core::clamp(core::round32(color.getBlue()*factor), 0, 255));
37 void applyFacesShading(video::SColor &color, const v3f &normal)
40 Some drawtypes have normals set to (0, 0, 0), this must result in
41 maximum brightness: shade factor 1.0.
42 Shade factors for aligned cube faces are:
45 +-X 0.670820 sqrt(0.45)
46 +-Z 0.836660 sqrt(0.7)
48 float x2 = normal.X * normal.X;
49 float y2 = normal.Y * normal.Y;
50 float z2 = normal.Z * normal.Z;
52 applyShadeFactor(color, 0.670820f * x2 + 0.447213f * y2 + 0.836660f * z2);
53 else if ((x2 > 1e-3) || (z2 > 1e-3))
54 applyShadeFactor(color, 0.670820f * x2 + 1.000000f * y2 + 0.836660f * z2);
57 scene::IAnimatedMesh* createCubeMesh(v3f scale)
59 video::SColor c(255,255,255,255);
60 video::S3DVertex vertices[24] =
63 video::S3DVertex(-0.5,+0.5,-0.5, 0,1,0, c, 0,1),
64 video::S3DVertex(-0.5,+0.5,+0.5, 0,1,0, c, 0,0),
65 video::S3DVertex(+0.5,+0.5,+0.5, 0,1,0, c, 1,0),
66 video::S3DVertex(+0.5,+0.5,-0.5, 0,1,0, c, 1,1),
68 video::S3DVertex(-0.5,-0.5,-0.5, 0,-1,0, c, 0,0),
69 video::S3DVertex(+0.5,-0.5,-0.5, 0,-1,0, c, 1,0),
70 video::S3DVertex(+0.5,-0.5,+0.5, 0,-1,0, c, 1,1),
71 video::S3DVertex(-0.5,-0.5,+0.5, 0,-1,0, c, 0,1),
73 video::S3DVertex(+0.5,-0.5,-0.5, 1,0,0, c, 0,1),
74 video::S3DVertex(+0.5,+0.5,-0.5, 1,0,0, c, 0,0),
75 video::S3DVertex(+0.5,+0.5,+0.5, 1,0,0, c, 1,0),
76 video::S3DVertex(+0.5,-0.5,+0.5, 1,0,0, c, 1,1),
78 video::S3DVertex(-0.5,-0.5,-0.5, -1,0,0, c, 1,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),
83 video::S3DVertex(-0.5,-0.5,+0.5, 0,0,1, c, 1,1),
84 video::S3DVertex(+0.5,-0.5,+0.5, 0,0,1, c, 0,1),
85 video::S3DVertex(+0.5,+0.5,+0.5, 0,0,1, c, 0,0),
86 video::S3DVertex(-0.5,+0.5,+0.5, 0,0,1, c, 1,0),
88 video::S3DVertex(-0.5,-0.5,-0.5, 0,0,-1, c, 0,1),
89 video::S3DVertex(-0.5,+0.5,-0.5, 0,0,-1, c, 0,0),
90 video::S3DVertex(+0.5,+0.5,-0.5, 0,0,-1, c, 1,0),
91 video::S3DVertex(+0.5,-0.5,-0.5, 0,0,-1, c, 1,1),
94 u16 indices[6] = {0,1,2,2,3,0};
96 scene::SMesh *mesh = new scene::SMesh();
97 for (u32 i=0; i<6; ++i)
99 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
100 buf->append(vertices + 4 * i, 4, indices, 6);
101 // Set default material
102 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
103 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
104 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
105 // Add mesh buffer to mesh
106 mesh->addMeshBuffer(buf);
110 scene::SAnimatedMesh *anim_mesh = new scene::SAnimatedMesh(mesh);
112 scaleMesh(anim_mesh, scale); // also recalculates bounding box
116 void scaleMesh(scene::IMesh *mesh, v3f scale)
124 u32 mc = mesh->getMeshBufferCount();
125 for (u32 j = 0; j < mc; j++) {
126 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
127 const u32 stride = getVertexPitchFromType(buf->getVertexType());
128 u32 vertex_count = buf->getVertexCount();
129 u8 *vertices = (u8 *)buf->getVertices();
130 for (u32 i = 0; i < vertex_count; i++)
131 ((video::S3DVertex *)(vertices + i * stride))->Pos *= scale;
133 buf->recalculateBoundingBox();
135 // calculate total bounding box
137 bbox = buf->getBoundingBox();
139 bbox.addInternalBox(buf->getBoundingBox());
141 mesh->setBoundingBox(bbox);
144 void translateMesh(scene::IMesh *mesh, v3f vec)
152 u32 mc = mesh->getMeshBufferCount();
153 for (u32 j = 0; j < mc; j++) {
154 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
155 const u32 stride = getVertexPitchFromType(buf->getVertexType());
156 u32 vertex_count = buf->getVertexCount();
157 u8 *vertices = (u8 *)buf->getVertices();
158 for (u32 i = 0; i < vertex_count; i++)
159 ((video::S3DVertex *)(vertices + i * stride))->Pos += vec;
161 buf->recalculateBoundingBox();
163 // calculate total bounding box
165 bbox = buf->getBoundingBox();
167 bbox.addInternalBox(buf->getBoundingBox());
169 mesh->setBoundingBox(bbox);
172 void setMeshBufferColor(scene::IMeshBuffer *buf, const video::SColor &color)
174 const u32 stride = getVertexPitchFromType(buf->getVertexType());
175 u32 vertex_count = buf->getVertexCount();
176 u8 *vertices = (u8 *) buf->getVertices();
177 for (u32 i = 0; i < vertex_count; i++)
178 ((video::S3DVertex *) (vertices + i * stride))->Color = color;
181 void setAnimatedMeshColor(scene::IAnimatedMeshSceneNode *node, const video::SColor &color)
183 for (u32 i = 0; i < node->getMaterialCount(); ++i) {
184 node->getMaterial(i).EmissiveColor = color;
188 void setMeshColor(scene::IMesh *mesh, const video::SColor &color)
193 u32 mc = mesh->getMeshBufferCount();
194 for (u32 j = 0; j < mc; j++)
195 setMeshBufferColor(mesh->getMeshBuffer(j), color);
198 void setMeshBufferTextureCoords(scene::IMeshBuffer *buf, const v2f *uv, u32 count)
200 const u32 stride = getVertexPitchFromType(buf->getVertexType());
201 assert(buf->getVertexCount() >= count);
202 u8 *vertices = (u8 *) buf->getVertices();
203 for (u32 i = 0; i < count; i++)
204 ((video::S3DVertex*) (vertices + i * stride))->TCoords = uv[i];
207 template <typename F>
208 static void applyToMesh(scene::IMesh *mesh, const F &fn)
210 u16 mc = mesh->getMeshBufferCount();
211 for (u16 j = 0; j < mc; j++) {
212 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
213 const u32 stride = getVertexPitchFromType(buf->getVertexType());
214 u32 vertex_count = buf->getVertexCount();
215 char *vertices = reinterpret_cast<char *>(buf->getVertices());
216 for (u32 i = 0; i < vertex_count; i++)
217 fn(reinterpret_cast<video::S3DVertex *>(vertices + i * stride));
221 void colorizeMeshBuffer(scene::IMeshBuffer *buf, const video::SColor *buffercolor)
223 const u32 stride = getVertexPitchFromType(buf->getVertexType());
224 u32 vertex_count = buf->getVertexCount();
225 u8 *vertices = (u8 *) buf->getVertices();
226 for (u32 i = 0; i < vertex_count; i++) {
227 video::S3DVertex *vertex = (video::S3DVertex *) (vertices + i * stride);
228 video::SColor *vc = &(vertex->Color);
232 applyFacesShading(*vc, vertex->Normal);
236 void setMeshColorByNormalXYZ(scene::IMesh *mesh,
237 const video::SColor &colorX,
238 const video::SColor &colorY,
239 const video::SColor &colorZ)
243 auto colorizator = [=] (video::S3DVertex *vertex) {
244 f32 x = fabs(vertex->Normal.X);
245 f32 y = fabs(vertex->Normal.Y);
246 f32 z = fabs(vertex->Normal.Z);
247 if (x >= y && x >= z)
248 vertex->Color = colorX;
250 vertex->Color = colorY;
252 vertex->Color = colorZ;
254 applyToMesh(mesh, colorizator);
257 void setMeshColorByNormal(scene::IMesh *mesh, const v3f &normal,
258 const video::SColor &color)
262 auto colorizator = [normal, color] (video::S3DVertex *vertex) {
263 if (vertex->Normal == normal)
264 vertex->Color = color;
266 applyToMesh(mesh, colorizator);
269 template <float v3f::*U, float v3f::*V>
270 static void rotateMesh(scene::IMesh *mesh, float degrees)
272 degrees *= M_PI / 180.0f;
273 float c = std::cos(degrees);
274 float s = std::sin(degrees);
275 auto rotator = [c, s] (video::S3DVertex *vertex) {
276 float u = vertex->Pos.*U;
277 float v = vertex->Pos.*V;
278 vertex->Pos.*U = c * u - s * v;
279 vertex->Pos.*V = s * u + c * v;
281 applyToMesh(mesh, rotator);
284 void rotateMeshXYby(scene::IMesh *mesh, f64 degrees)
286 rotateMesh<&v3f::X, &v3f::Y>(mesh, degrees);
289 void rotateMeshXZby(scene::IMesh *mesh, f64 degrees)
291 rotateMesh<&v3f::X, &v3f::Z>(mesh, degrees);
294 void rotateMeshYZby(scene::IMesh *mesh, f64 degrees)
296 rotateMesh<&v3f::Y, &v3f::Z>(mesh, degrees);
299 void rotateMeshBy6dFacedir(scene::IMesh *mesh, int facedir)
301 int axisdir = facedir >> 2;
304 case 1: rotateMeshXZby(mesh, -90); break;
305 case 2: rotateMeshXZby(mesh, 180); break;
306 case 3: rotateMeshXZby(mesh, 90); break;
309 case 1: rotateMeshYZby(mesh, 90); break; // z+
310 case 2: rotateMeshYZby(mesh, -90); break; // z-
311 case 3: rotateMeshXYby(mesh, -90); break; // x+
312 case 4: rotateMeshXYby(mesh, 90); break; // x-
313 case 5: rotateMeshXYby(mesh, -180); break;
317 void recalculateBoundingBox(scene::IMesh *src_mesh)
321 for (u16 j = 0; j < src_mesh->getMeshBufferCount(); j++) {
322 scene::IMeshBuffer *buf = src_mesh->getMeshBuffer(j);
323 buf->recalculateBoundingBox();
325 bbox = buf->getBoundingBox();
327 bbox.addInternalBox(buf->getBoundingBox());
329 src_mesh->setBoundingBox(bbox);
332 bool checkMeshNormals(scene::IMesh *mesh)
334 u32 buffer_count = mesh->getMeshBufferCount();
336 for (u32 i = 0; i < buffer_count; i++) {
337 scene::IMeshBuffer *buffer = mesh->getMeshBuffer(i);
339 // Here we intentionally check only first normal, assuming that if buffer
340 // has it valid, then most likely all other ones are fine too. We can
341 // check all of the normals to have length, but it seems like an overkill
342 // hurting the performance and covering only really weird broken models.
343 f32 length = buffer->getNormal(0).getLength();
345 if (!std::isfinite(length) || length < 1e-10f)
352 scene::IMeshBuffer* cloneMeshBuffer(scene::IMeshBuffer *mesh_buffer)
354 switch (mesh_buffer->getVertexType()) {
355 case video::EVT_STANDARD: {
356 video::S3DVertex *v = (video::S3DVertex *) mesh_buffer->getVertices();
357 u16 *indices = mesh_buffer->getIndices();
358 scene::SMeshBuffer *cloned_buffer = new scene::SMeshBuffer();
359 cloned_buffer->append(v, mesh_buffer->getVertexCount(), indices,
360 mesh_buffer->getIndexCount());
361 return cloned_buffer;
363 case video::EVT_2TCOORDS: {
364 video::S3DVertex2TCoords *v =
365 (video::S3DVertex2TCoords *) mesh_buffer->getVertices();
366 u16 *indices = mesh_buffer->getIndices();
367 scene::SMeshBufferLightMap *cloned_buffer =
368 new scene::SMeshBufferLightMap();
369 cloned_buffer->append(v, mesh_buffer->getVertexCount(), indices,
370 mesh_buffer->getIndexCount());
371 return cloned_buffer;
373 case video::EVT_TANGENTS: {
374 video::S3DVertexTangents *v =
375 (video::S3DVertexTangents *) mesh_buffer->getVertices();
376 u16 *indices = mesh_buffer->getIndices();
377 scene::SMeshBufferTangents *cloned_buffer =
378 new scene::SMeshBufferTangents();
379 cloned_buffer->append(v, mesh_buffer->getVertexCount(), indices,
380 mesh_buffer->getIndexCount());
381 return cloned_buffer;
384 // This should not happen.
389 scene::SMesh* cloneMesh(scene::IMesh *src_mesh)
391 scene::SMesh* dst_mesh = new scene::SMesh();
392 for (u16 j = 0; j < src_mesh->getMeshBufferCount(); j++) {
393 scene::IMeshBuffer *temp_buf = cloneMeshBuffer(
394 src_mesh->getMeshBuffer(j));
395 dst_mesh->addMeshBuffer(temp_buf);
402 scene::IMesh* convertNodeboxesToMesh(const std::vector<aabb3f> &boxes,
403 const f32 *uv_coords, float expand)
405 scene::SMesh* dst_mesh = new scene::SMesh();
407 for (u16 j = 0; j < 6; j++)
409 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
410 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
411 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
412 dst_mesh->addMeshBuffer(buf);
416 video::SColor c(255,255,255,255);
418 for (aabb3f box : boxes) {
421 box.MinEdge.X -= expand;
422 box.MinEdge.Y -= expand;
423 box.MinEdge.Z -= expand;
424 box.MaxEdge.X += expand;
425 box.MaxEdge.Y += expand;
426 box.MaxEdge.Z += expand;
428 // Compute texture UV coords
429 f32 tx1 = (box.MinEdge.X / BS) + 0.5;
430 f32 ty1 = (box.MinEdge.Y / BS) + 0.5;
431 f32 tz1 = (box.MinEdge.Z / BS) + 0.5;
432 f32 tx2 = (box.MaxEdge.X / BS) + 0.5;
433 f32 ty2 = (box.MaxEdge.Y / BS) + 0.5;
434 f32 tz2 = (box.MaxEdge.Z / BS) + 0.5;
436 f32 txc_default[24] = {
438 tx1, 1 - tz2, tx2, 1 - tz1,
442 tz1, 1 - ty2, tz2, 1 - ty1,
444 1 - tz2, 1 - ty2, 1 - tz1, 1 - ty1,
446 1 - tx2, 1 - ty2, 1 - tx1, 1 - ty1,
448 tx1, 1 - ty2, tx2, 1 - ty1,
451 // use default texture UV mapping if not provided
452 const f32 *txc = uv_coords ? uv_coords : txc_default;
454 v3f min = box.MinEdge;
455 v3f max = box.MaxEdge;
457 video::S3DVertex vertices[24] =
460 video::S3DVertex(min.X,max.Y,max.Z, 0,1,0, c, txc[0],txc[1]),
461 video::S3DVertex(max.X,max.Y,max.Z, 0,1,0, c, txc[2],txc[1]),
462 video::S3DVertex(max.X,max.Y,min.Z, 0,1,0, c, txc[2],txc[3]),
463 video::S3DVertex(min.X,max.Y,min.Z, 0,1,0, c, txc[0],txc[3]),
465 video::S3DVertex(min.X,min.Y,min.Z, 0,-1,0, c, txc[4],txc[5]),
466 video::S3DVertex(max.X,min.Y,min.Z, 0,-1,0, c, txc[6],txc[5]),
467 video::S3DVertex(max.X,min.Y,max.Z, 0,-1,0, c, txc[6],txc[7]),
468 video::S3DVertex(min.X,min.Y,max.Z, 0,-1,0, c, txc[4],txc[7]),
470 video::S3DVertex(max.X,max.Y,min.Z, 1,0,0, c, txc[ 8],txc[9]),
471 video::S3DVertex(max.X,max.Y,max.Z, 1,0,0, c, txc[10],txc[9]),
472 video::S3DVertex(max.X,min.Y,max.Z, 1,0,0, c, txc[10],txc[11]),
473 video::S3DVertex(max.X,min.Y,min.Z, 1,0,0, c, txc[ 8],txc[11]),
475 video::S3DVertex(min.X,max.Y,max.Z, -1,0,0, c, txc[12],txc[13]),
476 video::S3DVertex(min.X,max.Y,min.Z, -1,0,0, c, txc[14],txc[13]),
477 video::S3DVertex(min.X,min.Y,min.Z, -1,0,0, c, txc[14],txc[15]),
478 video::S3DVertex(min.X,min.Y,max.Z, -1,0,0, c, txc[12],txc[15]),
480 video::S3DVertex(max.X,max.Y,max.Z, 0,0,1, c, txc[16],txc[17]),
481 video::S3DVertex(min.X,max.Y,max.Z, 0,0,1, c, txc[18],txc[17]),
482 video::S3DVertex(min.X,min.Y,max.Z, 0,0,1, c, txc[18],txc[19]),
483 video::S3DVertex(max.X,min.Y,max.Z, 0,0,1, c, txc[16],txc[19]),
485 video::S3DVertex(min.X,max.Y,min.Z, 0,0,-1, c, txc[20],txc[21]),
486 video::S3DVertex(max.X,max.Y,min.Z, 0,0,-1, c, txc[22],txc[21]),
487 video::S3DVertex(max.X,min.Y,min.Z, 0,0,-1, c, txc[22],txc[23]),
488 video::S3DVertex(min.X,min.Y,min.Z, 0,0,-1, c, txc[20],txc[23]),
491 u16 indices[] = {0,1,2,2,3,0};
493 for(u16 j = 0; j < 24; j += 4)
495 scene::IMeshBuffer *buf = dst_mesh->getMeshBuffer(j / 4);
496 buf->append(vertices + j, 4, indices, 6);
504 core::array<u32> tris;
517 const u16 cachesize = 32;
519 float FindVertexScore(vcache *v)
521 const float CacheDecayPower = 1.5f;
522 const float LastTriScore = 0.75f;
523 const float ValenceBoostScale = 2.0f;
524 const float ValenceBoostPower = 0.5f;
525 const float MaxSizeVertexCache = 32.0f;
527 if (v->NumActiveTris == 0)
529 // No tri needs this vertex!
534 int CachePosition = v->cachepos;
535 if (CachePosition < 0)
537 // Vertex is not in FIFO cache - no score.
541 if (CachePosition < 3)
543 // This vertex was used in the last triangle,
544 // so it has a fixed score.
545 Score = LastTriScore;
549 // Points for being high in the cache.
550 const float Scaler = 1.0f / (MaxSizeVertexCache - 3);
551 Score = 1.0f - (CachePosition - 3) * Scaler;
552 Score = powf(Score, CacheDecayPower);
556 // Bonus points for having a low number of tris still to
557 // use the vert, so we get rid of lone verts quickly.
558 float ValenceBoost = powf(v->NumActiveTris,
560 Score += ValenceBoostScale * ValenceBoost;
566 A specialized LRU cache for the Forsyth algorithm.
573 f_lru(vcache *v, tcache *t): vc(v), tc(t)
575 for (int &i : cache) {
580 // Adds this vertex index and returns the highest-scoring triangle index
581 u32 add(u16 vert, bool updatetris = false)
585 // Mark existing pos as empty
586 for (u16 i = 0; i < cachesize; i++)
588 if (cache[i] == vert)
590 // Move everything down
591 for (u16 j = i; j; j--)
593 cache[j] = cache[j - 1];
603 if (cache[cachesize-1] != -1)
604 vc[cache[cachesize-1]].cachepos = -1;
606 // Move everything down
607 for (u16 i = cachesize - 1; i; i--)
609 cache[i] = cache[i - 1];
620 // Update cache positions
621 for (u16 i = 0; i < cachesize; i++)
626 vc[cache[i]].cachepos = i;
627 vc[cache[i]].score = FindVertexScore(&vc[cache[i]]);
630 // Update triangle scores
631 for (int i : cache) {
635 const u16 trisize = vc[i].tris.size();
636 for (u16 t = 0; t < trisize; t++)
638 tcache *tri = &tc[vc[i].tris[t]];
641 vc[tri->ind[0]].score +
642 vc[tri->ind[1]].score +
643 vc[tri->ind[2]].score;
645 if (tri->score > hiscore)
647 hiscore = tri->score;
648 highest = vc[i].tris[t];
658 s32 cache[cachesize];
664 Vertex cache optimization according to the Forsyth paper:
665 http://home.comcast.net/~tom_forsyth/papers/fast_vert_cache_opt.html
667 The function is thread-safe (read: you can optimize several meshes in different threads)
669 \param mesh Source mesh for the operation. */
670 scene::IMesh* createForsythOptimizedMesh(const scene::IMesh *mesh)
675 scene::SMesh *newmesh = new scene::SMesh();
676 newmesh->BoundingBox = mesh->getBoundingBox();
678 const u32 mbcount = mesh->getMeshBufferCount();
680 for (u32 b = 0; b < mbcount; ++b)
682 const scene::IMeshBuffer *mb = mesh->getMeshBuffer(b);
684 if (mb->getIndexType() != video::EIT_16BIT)
686 //os::Printer::log("Cannot optimize a mesh with 32bit indices", ELL_ERROR);
691 const u32 icount = mb->getIndexCount();
692 const u32 tcount = icount / 3;
693 const u32 vcount = mb->getVertexCount();
694 const u16 *ind = mb->getIndices();
696 vcache *vc = new vcache[vcount];
697 tcache *tc = new tcache[tcount];
702 for (u16 i = 0; i < vcount; i++)
706 vc[i].NumActiveTris = 0;
709 // First pass: count how many times a vert is used
710 for (u32 i = 0; i < icount; i += 3)
712 vc[ind[i]].NumActiveTris++;
713 vc[ind[i + 1]].NumActiveTris++;
714 vc[ind[i + 2]].NumActiveTris++;
716 const u32 tri_ind = i/3;
717 tc[tri_ind].ind[0] = ind[i];
718 tc[tri_ind].ind[1] = ind[i + 1];
719 tc[tri_ind].ind[2] = ind[i + 2];
722 // Second pass: list of each triangle
723 for (u32 i = 0; i < tcount; i++)
725 vc[tc[i].ind[0]].tris.push_back(i);
726 vc[tc[i].ind[1]].tris.push_back(i);
727 vc[tc[i].ind[2]].tris.push_back(i);
732 // Give initial scores
733 for (u16 i = 0; i < vcount; i++)
735 vc[i].score = FindVertexScore(&vc[i]);
737 for (u32 i = 0; i < tcount; i++)
740 vc[tc[i].ind[0]].score +
741 vc[tc[i].ind[1]].score +
742 vc[tc[i].ind[2]].score;
745 switch(mb->getVertexType())
747 case video::EVT_STANDARD:
749 video::S3DVertex *v = (video::S3DVertex *) mb->getVertices();
751 scene::SMeshBuffer *buf = new scene::SMeshBuffer();
752 buf->Material = mb->getMaterial();
754 buf->Vertices.reallocate(vcount);
755 buf->Indices.reallocate(icount);
757 core::map<const video::S3DVertex, const u16> sind; // search index for fast operation
758 typedef core::map<const video::S3DVertex, const u16>::Node snode;
765 if (tc[highest].drawn)
769 for (u32 t = 0; t < tcount; t++)
773 if (tc[t].score > hiscore)
776 hiscore = tc[t].score;
785 // Output the best triangle
786 u16 newind = buf->Vertices.size();
788 snode *s = sind.find(v[tc[highest].ind[0]]);
792 buf->Vertices.push_back(v[tc[highest].ind[0]]);
793 buf->Indices.push_back(newind);
794 sind.insert(v[tc[highest].ind[0]], newind);
799 buf->Indices.push_back(s->getValue());
802 s = sind.find(v[tc[highest].ind[1]]);
806 buf->Vertices.push_back(v[tc[highest].ind[1]]);
807 buf->Indices.push_back(newind);
808 sind.insert(v[tc[highest].ind[1]], newind);
813 buf->Indices.push_back(s->getValue());
816 s = sind.find(v[tc[highest].ind[2]]);
820 buf->Vertices.push_back(v[tc[highest].ind[2]]);
821 buf->Indices.push_back(newind);
822 sind.insert(v[tc[highest].ind[2]], newind);
826 buf->Indices.push_back(s->getValue());
829 vc[tc[highest].ind[0]].NumActiveTris--;
830 vc[tc[highest].ind[1]].NumActiveTris--;
831 vc[tc[highest].ind[2]].NumActiveTris--;
833 tc[highest].drawn = true;
835 for (u16 j : tc[highest].ind) {
836 vcache *vert = &vc[j];
837 for (u16 t = 0; t < vert->tris.size(); t++)
839 if (highest == vert->tris[t])
847 lru.add(tc[highest].ind[0]);
848 lru.add(tc[highest].ind[1]);
849 highest = lru.add(tc[highest].ind[2], true);
853 buf->setBoundingBox(mb->getBoundingBox());
854 newmesh->addMeshBuffer(buf);
858 case video::EVT_2TCOORDS:
860 video::S3DVertex2TCoords *v = (video::S3DVertex2TCoords *) mb->getVertices();
862 scene::SMeshBufferLightMap *buf = new scene::SMeshBufferLightMap();
863 buf->Material = mb->getMaterial();
865 buf->Vertices.reallocate(vcount);
866 buf->Indices.reallocate(icount);
868 core::map<const video::S3DVertex2TCoords, const u16> sind; // search index for fast operation
869 typedef core::map<const video::S3DVertex2TCoords, const u16>::Node snode;
876 if (tc[highest].drawn)
880 for (u32 t = 0; t < tcount; t++)
884 if (tc[t].score > hiscore)
887 hiscore = tc[t].score;
896 // Output the best triangle
897 u16 newind = buf->Vertices.size();
899 snode *s = sind.find(v[tc[highest].ind[0]]);
903 buf->Vertices.push_back(v[tc[highest].ind[0]]);
904 buf->Indices.push_back(newind);
905 sind.insert(v[tc[highest].ind[0]], newind);
910 buf->Indices.push_back(s->getValue());
913 s = sind.find(v[tc[highest].ind[1]]);
917 buf->Vertices.push_back(v[tc[highest].ind[1]]);
918 buf->Indices.push_back(newind);
919 sind.insert(v[tc[highest].ind[1]], newind);
924 buf->Indices.push_back(s->getValue());
927 s = sind.find(v[tc[highest].ind[2]]);
931 buf->Vertices.push_back(v[tc[highest].ind[2]]);
932 buf->Indices.push_back(newind);
933 sind.insert(v[tc[highest].ind[2]], newind);
937 buf->Indices.push_back(s->getValue());
940 vc[tc[highest].ind[0]].NumActiveTris--;
941 vc[tc[highest].ind[1]].NumActiveTris--;
942 vc[tc[highest].ind[2]].NumActiveTris--;
944 tc[highest].drawn = true;
946 for (u16 j : tc[highest].ind) {
947 vcache *vert = &vc[j];
948 for (u16 t = 0; t < vert->tris.size(); t++)
950 if (highest == vert->tris[t])
958 lru.add(tc[highest].ind[0]);
959 lru.add(tc[highest].ind[1]);
960 highest = lru.add(tc[highest].ind[2]);
964 buf->setBoundingBox(mb->getBoundingBox());
965 newmesh->addMeshBuffer(buf);
970 case video::EVT_TANGENTS:
972 video::S3DVertexTangents *v = (video::S3DVertexTangents *) mb->getVertices();
974 scene::SMeshBufferTangents *buf = new scene::SMeshBufferTangents();
975 buf->Material = mb->getMaterial();
977 buf->Vertices.reallocate(vcount);
978 buf->Indices.reallocate(icount);
980 core::map<const video::S3DVertexTangents, const u16> sind; // search index for fast operation
981 typedef core::map<const video::S3DVertexTangents, const u16>::Node snode;
988 if (tc[highest].drawn)
992 for (u32 t = 0; t < tcount; t++)
996 if (tc[t].score > hiscore)
999 hiscore = tc[t].score;
1008 // Output the best triangle
1009 u16 newind = buf->Vertices.size();
1011 snode *s = sind.find(v[tc[highest].ind[0]]);
1015 buf->Vertices.push_back(v[tc[highest].ind[0]]);
1016 buf->Indices.push_back(newind);
1017 sind.insert(v[tc[highest].ind[0]], newind);
1022 buf->Indices.push_back(s->getValue());
1025 s = sind.find(v[tc[highest].ind[1]]);
1029 buf->Vertices.push_back(v[tc[highest].ind[1]]);
1030 buf->Indices.push_back(newind);
1031 sind.insert(v[tc[highest].ind[1]], newind);
1036 buf->Indices.push_back(s->getValue());
1039 s = sind.find(v[tc[highest].ind[2]]);
1043 buf->Vertices.push_back(v[tc[highest].ind[2]]);
1044 buf->Indices.push_back(newind);
1045 sind.insert(v[tc[highest].ind[2]], newind);
1049 buf->Indices.push_back(s->getValue());
1052 vc[tc[highest].ind[0]].NumActiveTris--;
1053 vc[tc[highest].ind[1]].NumActiveTris--;
1054 vc[tc[highest].ind[2]].NumActiveTris--;
1056 tc[highest].drawn = true;
1058 for (u16 j : tc[highest].ind) {
1059 vcache *vert = &vc[j];
1060 for (u16 t = 0; t < vert->tris.size(); t++)
1062 if (highest == vert->tris[t])
1064 vert->tris.erase(t);
1070 lru.add(tc[highest].ind[0]);
1071 lru.add(tc[highest].ind[1]);
1072 highest = lru.add(tc[highest].ind[2]);
1076 buf->setBoundingBox(mb->getBoundingBox());
1077 newmesh->addMeshBuffer(buf);
1086 } // for each meshbuffer