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 scene::IAnimatedMesh* createCubeMesh(v3f scale)
38 video::SColor c(255,255,255,255);
39 video::S3DVertex vertices[24] =
42 video::S3DVertex(-0.5,+0.5,-0.5, 0,1,0, c, 0,1),
43 video::S3DVertex(-0.5,+0.5,+0.5, 0,1,0, c, 0,0),
44 video::S3DVertex(+0.5,+0.5,+0.5, 0,1,0, c, 1,0),
45 video::S3DVertex(+0.5,+0.5,-0.5, 0,1,0, c, 1,1),
47 video::S3DVertex(-0.5,-0.5,-0.5, 0,-1,0, c, 0,0),
48 video::S3DVertex(+0.5,-0.5,-0.5, 0,-1,0, c, 1,0),
49 video::S3DVertex(+0.5,-0.5,+0.5, 0,-1,0, c, 1,1),
50 video::S3DVertex(-0.5,-0.5,+0.5, 0,-1,0, c, 0,1),
52 video::S3DVertex(+0.5,-0.5,-0.5, 1,0,0, c, 0,1),
53 video::S3DVertex(+0.5,+0.5,-0.5, 1,0,0, c, 0,0),
54 video::S3DVertex(+0.5,+0.5,+0.5, 1,0,0, c, 1,0),
55 video::S3DVertex(+0.5,-0.5,+0.5, 1,0,0, c, 1,1),
57 video::S3DVertex(-0.5,-0.5,-0.5, -1,0,0, c, 1,1),
58 video::S3DVertex(-0.5,-0.5,+0.5, -1,0,0, c, 0,1),
59 video::S3DVertex(-0.5,+0.5,+0.5, -1,0,0, c, 0,0),
60 video::S3DVertex(-0.5,+0.5,-0.5, -1,0,0, c, 1,0),
62 video::S3DVertex(-0.5,-0.5,+0.5, 0,0,1, c, 1,1),
63 video::S3DVertex(+0.5,-0.5,+0.5, 0,0,1, c, 0,1),
64 video::S3DVertex(+0.5,+0.5,+0.5, 0,0,1, c, 0,0),
65 video::S3DVertex(-0.5,+0.5,+0.5, 0,0,1, c, 1,0),
67 video::S3DVertex(-0.5,-0.5,-0.5, 0,0,-1, c, 0,1),
68 video::S3DVertex(-0.5,+0.5,-0.5, 0,0,-1, c, 0,0),
69 video::S3DVertex(+0.5,+0.5,-0.5, 0,0,-1, c, 1,0),
70 video::S3DVertex(+0.5,-0.5,-0.5, 0,0,-1, c, 1,1),
73 u16 indices[6] = {0,1,2,2,3,0};
75 scene::SMesh *mesh = new scene::SMesh();
76 for (u32 i=0; i<6; ++i)
78 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
79 buf->append(vertices + 4 * i, 4, indices, 6);
80 // Set default material
81 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
82 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
83 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
84 // Add mesh buffer to mesh
85 mesh->addMeshBuffer(buf);
89 scene::SAnimatedMesh *anim_mesh = new scene::SAnimatedMesh(mesh);
91 scaleMesh(anim_mesh, scale); // also recalculates bounding box
95 void scaleMesh(scene::IMesh *mesh, v3f scale)
100 core::aabbox3d<f32> bbox;
103 u16 mc = mesh->getMeshBufferCount();
104 for(u16 j=0; j<mc; j++)
106 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
107 video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
108 u16 vc = buf->getVertexCount();
109 for(u16 i=0; i<vc; i++)
111 vertices[i].Pos *= scale;
113 buf->recalculateBoundingBox();
115 // calculate total bounding box
117 bbox = buf->getBoundingBox();
119 bbox.addInternalBox(buf->getBoundingBox());
121 mesh->setBoundingBox(bbox);
124 void translateMesh(scene::IMesh *mesh, v3f vec)
129 core::aabbox3d<f32> bbox;
132 u16 mc = mesh->getMeshBufferCount();
133 for(u16 j=0; j<mc; j++)
135 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
136 video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
137 u16 vc = buf->getVertexCount();
138 for(u16 i=0; i<vc; i++)
140 vertices[i].Pos += vec;
142 buf->recalculateBoundingBox();
144 // calculate total bounding box
146 bbox = buf->getBoundingBox();
148 bbox.addInternalBox(buf->getBoundingBox());
150 mesh->setBoundingBox(bbox);
153 void setMeshColor(scene::IMesh *mesh, const video::SColor &color)
158 u16 mc = mesh->getMeshBufferCount();
159 for(u16 j=0; j<mc; j++)
161 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
162 video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
163 u16 vc = buf->getVertexCount();
164 for(u16 i=0; i<vc; i++)
166 vertices[i].Color = color;
171 void setMeshColorByNormalXYZ(scene::IMesh *mesh,
172 const video::SColor &colorX,
173 const video::SColor &colorY,
174 const video::SColor &colorZ)
179 u16 mc = mesh->getMeshBufferCount();
180 for(u16 j=0; j<mc; j++)
182 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
183 video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
184 u16 vc = buf->getVertexCount();
185 for(u16 i=0; i<vc; i++)
187 f32 x = fabs(vertices[i].Normal.X);
188 f32 y = fabs(vertices[i].Normal.Y);
189 f32 z = fabs(vertices[i].Normal.Z);
191 vertices[i].Color = colorX;
193 vertices[i].Color = colorY;
195 vertices[i].Color = colorZ;
201 void rotateMeshXYby (scene::IMesh *mesh, f64 degrees)
203 u16 mc = mesh->getMeshBufferCount();
204 for(u16 j = 0; j < mc; j++)
206 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
207 video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
208 u16 vc = buf->getVertexCount();
209 for(u16 i = 0; i < vc; i++)
211 vertices[i].Pos.rotateXYBy(degrees);
216 void rotateMeshXZby (scene::IMesh *mesh, f64 degrees)
218 u16 mc = mesh->getMeshBufferCount();
219 for(u16 j = 0; j < mc; j++)
221 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
222 video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
223 u16 vc = buf->getVertexCount();
224 for(u16 i = 0; i < vc; i++)
226 vertices[i].Pos.rotateXZBy(degrees);
231 void rotateMeshYZby (scene::IMesh *mesh, f64 degrees)
233 u16 mc = mesh->getMeshBufferCount();
234 for(u16 j = 0; j < mc; j++)
236 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
237 video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
238 u16 vc = buf->getVertexCount();
239 for(u16 i = 0; i < vc; i++)
241 vertices[i].Pos.rotateYZBy(degrees);
246 void rotateMeshBy6dFacedir(scene::IMesh *mesh, int facedir)
248 int axisdir = facedir>>2;
251 u16 mc = mesh->getMeshBufferCount();
252 for(u16 j = 0; j < mc; j++)
254 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
255 video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
256 u16 vc = buf->getVertexCount();
257 for(u16 i=0; i<vc; i++)
263 vertices[i].Pos.rotateXZBy(-90);
264 else if(facedir == 2)
265 vertices[i].Pos.rotateXZBy(180);
266 else if(facedir == 3)
267 vertices[i].Pos.rotateXZBy(90);
270 vertices[i].Pos.rotateYZBy(90);
272 vertices[i].Pos.rotateXYBy(90);
273 else if(facedir == 2)
274 vertices[i].Pos.rotateXYBy(180);
275 else if(facedir == 3)
276 vertices[i].Pos.rotateXYBy(-90);
279 vertices[i].Pos.rotateYZBy(-90);
281 vertices[i].Pos.rotateXYBy(-90);
282 else if(facedir == 2)
283 vertices[i].Pos.rotateXYBy(180);
284 else if(facedir == 3)
285 vertices[i].Pos.rotateXYBy(90);
288 vertices[i].Pos.rotateXYBy(-90);
290 vertices[i].Pos.rotateYZBy(90);
291 else if(facedir == 2)
292 vertices[i].Pos.rotateYZBy(180);
293 else if(facedir == 3)
294 vertices[i].Pos.rotateYZBy(-90);
297 vertices[i].Pos.rotateXYBy(90);
299 vertices[i].Pos.rotateYZBy(-90);
300 else if(facedir == 2)
301 vertices[i].Pos.rotateYZBy(180);
302 else if(facedir == 3)
303 vertices[i].Pos.rotateYZBy(90);
306 vertices[i].Pos.rotateXYBy(-180);
308 vertices[i].Pos.rotateXZBy(90);
309 else if(facedir == 2)
310 vertices[i].Pos.rotateXZBy(180);
311 else if(facedir == 3)
312 vertices[i].Pos.rotateXZBy(-90);
321 void recalculateBoundingBox(scene::IMesh *src_mesh)
323 core::aabbox3d<f32> bbox;
325 for(u16 j = 0; j < src_mesh->getMeshBufferCount(); j++)
327 scene::IMeshBuffer *buf = src_mesh->getMeshBuffer(j);
328 buf->recalculateBoundingBox();
330 bbox = buf->getBoundingBox();
332 bbox.addInternalBox(buf->getBoundingBox());
334 src_mesh->setBoundingBox(bbox);
337 scene::IMesh* cloneMesh(scene::IMesh *src_mesh)
339 scene::SMesh* dst_mesh = new scene::SMesh();
340 for(u16 j = 0; j < src_mesh->getMeshBufferCount(); j++)
342 scene::IMeshBuffer *buf = src_mesh->getMeshBuffer(j);
343 video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
344 u16 *indices = (u16*)buf->getIndices();
345 scene::SMeshBuffer *temp_buf = new scene::SMeshBuffer();
346 temp_buf->append(vertices, buf->getVertexCount(),
347 indices, buf->getIndexCount());
348 dst_mesh->addMeshBuffer(temp_buf);
354 scene::IMesh* convertNodeboxNodeToMesh(ContentFeatures *f)
356 scene::SMesh* dst_mesh = new scene::SMesh();
357 for (u16 j = 0; j < 6; j++)
359 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
360 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
361 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
362 dst_mesh->addMeshBuffer(buf);
366 video::SColor c(255,255,255,255);
368 std::vector<aabb3f> boxes = f->node_box.fixed;
370 for(std::vector<aabb3f>::iterator
372 i != boxes.end(); i++)
377 if (box.MinEdge.X > box.MaxEdge.X)
380 box.MinEdge.X=box.MaxEdge.X;
383 if (box.MinEdge.Y > box.MaxEdge.Y)
386 box.MinEdge.Y=box.MaxEdge.Y;
389 if (box.MinEdge.Z > box.MaxEdge.Z)
392 box.MinEdge.Z=box.MaxEdge.Z;
395 // Compute texture coords
396 f32 tx1 = (box.MinEdge.X/BS)+0.5;
397 f32 ty1 = (box.MinEdge.Y/BS)+0.5;
398 f32 tz1 = (box.MinEdge.Z/BS)+0.5;
399 f32 tx2 = (box.MaxEdge.X/BS)+0.5;
400 f32 ty2 = (box.MaxEdge.Y/BS)+0.5;
401 f32 tz2 = (box.MaxEdge.Z/BS)+0.5;
404 tx1, 1-tz2, tx2, 1-tz1,
408 tz1, 1-ty2, tz2, 1-ty1,
410 1-tz2, 1-ty2, 1-tz1, 1-ty1,
412 1-tx2, 1-ty2, 1-tx1, 1-ty1,
414 tx1, 1-ty2, tx2, 1-ty1,
416 v3f min = box.MinEdge;
417 v3f max = box.MaxEdge;
419 video::S3DVertex vertices[24] =
422 video::S3DVertex(min.X,max.Y,max.Z, 0,1,0, c, txc[0],txc[1]),
423 video::S3DVertex(max.X,max.Y,max.Z, 0,1,0, c, txc[2],txc[1]),
424 video::S3DVertex(max.X,max.Y,min.Z, 0,1,0, c, txc[2],txc[3]),
425 video::S3DVertex(min.X,max.Y,min.Z, 0,1,0, c, txc[0],txc[3]),
427 video::S3DVertex(min.X,min.Y,min.Z, 0,-1,0, c, txc[4],txc[5]),
428 video::S3DVertex(max.X,min.Y,min.Z, 0,-1,0, c, txc[6],txc[5]),
429 video::S3DVertex(max.X,min.Y,max.Z, 0,-1,0, c, txc[6],txc[7]),
430 video::S3DVertex(min.X,min.Y,max.Z, 0,-1,0, c, txc[4],txc[7]),
432 video::S3DVertex(max.X,max.Y,min.Z, 1,0,0, c, txc[ 8],txc[9]),
433 video::S3DVertex(max.X,max.Y,max.Z, 1,0,0, c, txc[10],txc[9]),
434 video::S3DVertex(max.X,min.Y,max.Z, 1,0,0, c, txc[10],txc[11]),
435 video::S3DVertex(max.X,min.Y,min.Z, 1,0,0, c, txc[ 8],txc[11]),
437 video::S3DVertex(min.X,max.Y,max.Z, -1,0,0, c, txc[12],txc[13]),
438 video::S3DVertex(min.X,max.Y,min.Z, -1,0,0, c, txc[14],txc[13]),
439 video::S3DVertex(min.X,min.Y,min.Z, -1,0,0, c, txc[14],txc[15]),
440 video::S3DVertex(min.X,min.Y,max.Z, -1,0,0, c, txc[12],txc[15]),
442 video::S3DVertex(max.X,max.Y,max.Z, 0,0,1, c, txc[16],txc[17]),
443 video::S3DVertex(min.X,max.Y,max.Z, 0,0,1, c, txc[18],txc[17]),
444 video::S3DVertex(min.X,min.Y,max.Z, 0,0,1, c, txc[18],txc[19]),
445 video::S3DVertex(max.X,min.Y,max.Z, 0,0,1, c, txc[16],txc[19]),
447 video::S3DVertex(min.X,max.Y,min.Z, 0,0,-1, c, txc[20],txc[21]),
448 video::S3DVertex(max.X,max.Y,min.Z, 0,0,-1, c, txc[22],txc[21]),
449 video::S3DVertex(max.X,min.Y,min.Z, 0,0,-1, c, txc[22],txc[23]),
450 video::S3DVertex(min.X,min.Y,min.Z, 0,0,-1, c, txc[20],txc[23]),
453 u16 indices[] = {0,1,2,2,3,0};
455 for(u16 j = 0; j < 24; j += 4)
457 scene::IMeshBuffer *buf = dst_mesh->getMeshBuffer(j / 4);
458 buf->append(vertices + j, 4, indices, 6);
466 core::array<u32> tris;
479 const u16 cachesize = 32;
481 float FindVertexScore(vcache *v)
483 const float CacheDecayPower = 1.5f;
484 const float LastTriScore = 0.75f;
485 const float ValenceBoostScale = 2.0f;
486 const float ValenceBoostPower = 0.5f;
487 const float MaxSizeVertexCache = 32.0f;
489 if (v->NumActiveTris == 0)
491 // No tri needs this vertex!
496 int CachePosition = v->cachepos;
497 if (CachePosition < 0)
499 // Vertex is not in FIFO cache - no score.
503 if (CachePosition < 3)
505 // This vertex was used in the last triangle,
506 // so it has a fixed score.
507 Score = LastTriScore;
511 // Points for being high in the cache.
512 const float Scaler = 1.0f / (MaxSizeVertexCache - 3);
513 Score = 1.0f - (CachePosition - 3) * Scaler;
514 Score = powf(Score, CacheDecayPower);
518 // Bonus points for having a low number of tris still to
519 // use the vert, so we get rid of lone verts quickly.
520 float ValenceBoost = powf(v->NumActiveTris,
522 Score += ValenceBoostScale * ValenceBoost;
528 A specialized LRU cache for the Forsyth algorithm.
535 f_lru(vcache *v, tcache *t): vc(v), tc(t)
537 for (u16 i = 0; i < cachesize; i++)
543 // Adds this vertex index and returns the highest-scoring triangle index
544 u32 add(u16 vert, bool updatetris = false)
548 // Mark existing pos as empty
549 for (u16 i = 0; i < cachesize; i++)
551 if (cache[i] == vert)
553 // Move everything down
554 for (u16 j = i; j; j--)
556 cache[j] = cache[j - 1];
566 if (cache[cachesize-1] != -1)
567 vc[cache[cachesize-1]].cachepos = -1;
569 // Move everything down
570 for (u16 i = cachesize - 1; i; i--)
572 cache[i] = cache[i - 1];
583 // Update cache positions
584 for (u16 i = 0; i < cachesize; i++)
589 vc[cache[i]].cachepos = i;
590 vc[cache[i]].score = FindVertexScore(&vc[cache[i]]);
593 // Update triangle scores
594 for (u16 i = 0; i < cachesize; i++)
599 const u16 trisize = vc[cache[i]].tris.size();
600 for (u16 t = 0; t < trisize; t++)
602 tcache *tri = &tc[vc[cache[i]].tris[t]];
605 vc[tri->ind[0]].score +
606 vc[tri->ind[1]].score +
607 vc[tri->ind[2]].score;
609 if (tri->score > hiscore)
611 hiscore = tri->score;
612 highest = vc[cache[i]].tris[t];
622 s32 cache[cachesize];
628 Vertex cache optimization according to the Forsyth paper:
629 http://home.comcast.net/~tom_forsyth/papers/fast_vert_cache_opt.html
631 The function is thread-safe (read: you can optimize several meshes in different threads)
633 \param mesh Source mesh for the operation. */
634 scene::IMesh* createForsythOptimizedMesh(const scene::IMesh *mesh)
639 scene::SMesh *newmesh = new scene::SMesh();
640 newmesh->BoundingBox = mesh->getBoundingBox();
642 const u32 mbcount = mesh->getMeshBufferCount();
644 for (u32 b = 0; b < mbcount; ++b)
646 const scene::IMeshBuffer *mb = mesh->getMeshBuffer(b);
648 if (mb->getIndexType() != video::EIT_16BIT)
650 //os::Printer::log("Cannot optimize a mesh with 32bit indices", ELL_ERROR);
655 const u32 icount = mb->getIndexCount();
656 const u32 tcount = icount / 3;
657 const u32 vcount = mb->getVertexCount();
658 const u16 *ind = mb->getIndices();
660 vcache *vc = new vcache[vcount];
661 tcache *tc = new tcache[tcount];
666 for (u16 i = 0; i < vcount; i++)
670 vc[i].NumActiveTris = 0;
673 // First pass: count how many times a vert is used
674 for (u32 i = 0; i < icount; i += 3)
676 vc[ind[i]].NumActiveTris++;
677 vc[ind[i + 1]].NumActiveTris++;
678 vc[ind[i + 2]].NumActiveTris++;
680 const u32 tri_ind = i/3;
681 tc[tri_ind].ind[0] = ind[i];
682 tc[tri_ind].ind[1] = ind[i + 1];
683 tc[tri_ind].ind[2] = ind[i + 2];
686 // Second pass: list of each triangle
687 for (u32 i = 0; i < tcount; i++)
689 vc[tc[i].ind[0]].tris.push_back(i);
690 vc[tc[i].ind[1]].tris.push_back(i);
691 vc[tc[i].ind[2]].tris.push_back(i);
696 // Give initial scores
697 for (u16 i = 0; i < vcount; i++)
699 vc[i].score = FindVertexScore(&vc[i]);
701 for (u32 i = 0; i < tcount; i++)
704 vc[tc[i].ind[0]].score +
705 vc[tc[i].ind[1]].score +
706 vc[tc[i].ind[2]].score;
709 switch(mb->getVertexType())
711 case video::EVT_STANDARD:
713 video::S3DVertex *v = (video::S3DVertex *) mb->getVertices();
715 scene::SMeshBuffer *buf = new scene::SMeshBuffer();
716 buf->Material = mb->getMaterial();
718 buf->Vertices.reallocate(vcount);
719 buf->Indices.reallocate(icount);
721 core::map<const video::S3DVertex, const u16> sind; // search index for fast operation
722 typedef core::map<const video::S3DVertex, const u16>::Node snode;
729 if (tc[highest].drawn)
733 for (u32 t = 0; t < tcount; t++)
737 if (tc[t].score > hiscore)
740 hiscore = tc[t].score;
749 // Output the best triangle
750 u16 newind = buf->Vertices.size();
752 snode *s = sind.find(v[tc[highest].ind[0]]);
756 buf->Vertices.push_back(v[tc[highest].ind[0]]);
757 buf->Indices.push_back(newind);
758 sind.insert(v[tc[highest].ind[0]], newind);
763 buf->Indices.push_back(s->getValue());
766 s = sind.find(v[tc[highest].ind[1]]);
770 buf->Vertices.push_back(v[tc[highest].ind[1]]);
771 buf->Indices.push_back(newind);
772 sind.insert(v[tc[highest].ind[1]], newind);
777 buf->Indices.push_back(s->getValue());
780 s = sind.find(v[tc[highest].ind[2]]);
784 buf->Vertices.push_back(v[tc[highest].ind[2]]);
785 buf->Indices.push_back(newind);
786 sind.insert(v[tc[highest].ind[2]], newind);
790 buf->Indices.push_back(s->getValue());
793 vc[tc[highest].ind[0]].NumActiveTris--;
794 vc[tc[highest].ind[1]].NumActiveTris--;
795 vc[tc[highest].ind[2]].NumActiveTris--;
797 tc[highest].drawn = true;
799 for (u16 j = 0; j < 3; j++)
801 vcache *vert = &vc[tc[highest].ind[j]];
802 for (u16 t = 0; t < vert->tris.size(); t++)
804 if (highest == vert->tris[t])
812 lru.add(tc[highest].ind[0]);
813 lru.add(tc[highest].ind[1]);
814 highest = lru.add(tc[highest].ind[2], true);
818 buf->setBoundingBox(mb->getBoundingBox());
819 newmesh->addMeshBuffer(buf);
823 case video::EVT_2TCOORDS:
825 video::S3DVertex2TCoords *v = (video::S3DVertex2TCoords *) mb->getVertices();
827 scene::SMeshBufferLightMap *buf = new scene::SMeshBufferLightMap();
828 buf->Material = mb->getMaterial();
830 buf->Vertices.reallocate(vcount);
831 buf->Indices.reallocate(icount);
833 core::map<const video::S3DVertex2TCoords, const u16> sind; // search index for fast operation
834 typedef core::map<const video::S3DVertex2TCoords, const u16>::Node snode;
841 if (tc[highest].drawn)
845 for (u32 t = 0; t < tcount; t++)
849 if (tc[t].score > hiscore)
852 hiscore = tc[t].score;
861 // Output the best triangle
862 u16 newind = buf->Vertices.size();
864 snode *s = sind.find(v[tc[highest].ind[0]]);
868 buf->Vertices.push_back(v[tc[highest].ind[0]]);
869 buf->Indices.push_back(newind);
870 sind.insert(v[tc[highest].ind[0]], newind);
875 buf->Indices.push_back(s->getValue());
878 s = sind.find(v[tc[highest].ind[1]]);
882 buf->Vertices.push_back(v[tc[highest].ind[1]]);
883 buf->Indices.push_back(newind);
884 sind.insert(v[tc[highest].ind[1]], newind);
889 buf->Indices.push_back(s->getValue());
892 s = sind.find(v[tc[highest].ind[2]]);
896 buf->Vertices.push_back(v[tc[highest].ind[2]]);
897 buf->Indices.push_back(newind);
898 sind.insert(v[tc[highest].ind[2]], newind);
902 buf->Indices.push_back(s->getValue());
905 vc[tc[highest].ind[0]].NumActiveTris--;
906 vc[tc[highest].ind[1]].NumActiveTris--;
907 vc[tc[highest].ind[2]].NumActiveTris--;
909 tc[highest].drawn = true;
911 for (u16 j = 0; j < 3; j++)
913 vcache *vert = &vc[tc[highest].ind[j]];
914 for (u16 t = 0; t < vert->tris.size(); t++)
916 if (highest == vert->tris[t])
924 lru.add(tc[highest].ind[0]);
925 lru.add(tc[highest].ind[1]);
926 highest = lru.add(tc[highest].ind[2]);
930 buf->setBoundingBox(mb->getBoundingBox());
931 newmesh->addMeshBuffer(buf);
936 case video::EVT_TANGENTS:
938 video::S3DVertexTangents *v = (video::S3DVertexTangents *) mb->getVertices();
940 scene::SMeshBufferTangents *buf = new scene::SMeshBufferTangents();
941 buf->Material = mb->getMaterial();
943 buf->Vertices.reallocate(vcount);
944 buf->Indices.reallocate(icount);
946 core::map<const video::S3DVertexTangents, const u16> sind; // search index for fast operation
947 typedef core::map<const video::S3DVertexTangents, const u16>::Node snode;
954 if (tc[highest].drawn)
958 for (u32 t = 0; t < tcount; t++)
962 if (tc[t].score > hiscore)
965 hiscore = tc[t].score;
974 // Output the best triangle
975 u16 newind = buf->Vertices.size();
977 snode *s = sind.find(v[tc[highest].ind[0]]);
981 buf->Vertices.push_back(v[tc[highest].ind[0]]);
982 buf->Indices.push_back(newind);
983 sind.insert(v[tc[highest].ind[0]], newind);
988 buf->Indices.push_back(s->getValue());
991 s = sind.find(v[tc[highest].ind[1]]);
995 buf->Vertices.push_back(v[tc[highest].ind[1]]);
996 buf->Indices.push_back(newind);
997 sind.insert(v[tc[highest].ind[1]], newind);
1002 buf->Indices.push_back(s->getValue());
1005 s = sind.find(v[tc[highest].ind[2]]);
1009 buf->Vertices.push_back(v[tc[highest].ind[2]]);
1010 buf->Indices.push_back(newind);
1011 sind.insert(v[tc[highest].ind[2]], newind);
1015 buf->Indices.push_back(s->getValue());
1018 vc[tc[highest].ind[0]].NumActiveTris--;
1019 vc[tc[highest].ind[1]].NumActiveTris--;
1020 vc[tc[highest].ind[2]].NumActiveTris--;
1022 tc[highest].drawn = true;
1024 for (u16 j = 0; j < 3; j++)
1026 vcache *vert = &vc[tc[highest].ind[j]];
1027 for (u16 t = 0; t < vert->tris.size(); t++)
1029 if (highest == vert->tris[t])
1031 vert->tris.erase(t);
1037 lru.add(tc[highest].ind[0]);
1038 lru.add(tc[highest].ind[1]);
1039 highest = lru.add(tc[highest].ind[2]);
1043 buf->setBoundingBox(mb->getBoundingBox());
1044 newmesh->addMeshBuffer(buf);
1053 } // for each meshbuffer