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 static void applyFacesShading(video::SColor& color, float factor)
38 color.setRed(core::clamp(core::round32(color.getRed()*factor), 0, 255));
39 color.setGreen(core::clamp(core::round32(color.getGreen()*factor), 0, 255));
40 color.setBlue(core::clamp(core::round32(color.getBlue()*factor), 0, 255));
43 scene::IAnimatedMesh* createCubeMesh(v3f scale)
45 video::SColor c(255,255,255,255);
46 video::S3DVertex vertices[24] =
49 video::S3DVertex(-0.5,+0.5,-0.5, 0,1,0, c, 0,1),
50 video::S3DVertex(-0.5,+0.5,+0.5, 0,1,0, c, 0,0),
51 video::S3DVertex(+0.5,+0.5,+0.5, 0,1,0, c, 1,0),
52 video::S3DVertex(+0.5,+0.5,-0.5, 0,1,0, c, 1,1),
54 video::S3DVertex(-0.5,-0.5,-0.5, 0,-1,0, c, 0,0),
55 video::S3DVertex(+0.5,-0.5,-0.5, 0,-1,0, c, 1,0),
56 video::S3DVertex(+0.5,-0.5,+0.5, 0,-1,0, c, 1,1),
57 video::S3DVertex(-0.5,-0.5,+0.5, 0,-1,0, c, 0,1),
59 video::S3DVertex(+0.5,-0.5,-0.5, 1,0,0, c, 0,1),
60 video::S3DVertex(+0.5,+0.5,-0.5, 1,0,0, c, 0,0),
61 video::S3DVertex(+0.5,+0.5,+0.5, 1,0,0, c, 1,0),
62 video::S3DVertex(+0.5,-0.5,+0.5, 1,0,0, c, 1,1),
64 video::S3DVertex(-0.5,-0.5,-0.5, -1,0,0, c, 1,1),
65 video::S3DVertex(-0.5,-0.5,+0.5, -1,0,0, c, 0,1),
66 video::S3DVertex(-0.5,+0.5,+0.5, -1,0,0, c, 0,0),
67 video::S3DVertex(-0.5,+0.5,-0.5, -1,0,0, c, 1,0),
69 video::S3DVertex(-0.5,-0.5,+0.5, 0,0,1, c, 1,1),
70 video::S3DVertex(+0.5,-0.5,+0.5, 0,0,1, c, 0,1),
71 video::S3DVertex(+0.5,+0.5,+0.5, 0,0,1, c, 0,0),
72 video::S3DVertex(-0.5,+0.5,+0.5, 0,0,1, c, 1,0),
74 video::S3DVertex(-0.5,-0.5,-0.5, 0,0,-1, c, 0,1),
75 video::S3DVertex(-0.5,+0.5,-0.5, 0,0,-1, c, 0,0),
76 video::S3DVertex(+0.5,+0.5,-0.5, 0,0,-1, c, 1,0),
77 video::S3DVertex(+0.5,-0.5,-0.5, 0,0,-1, c, 1,1),
80 u16 indices[6] = {0,1,2,2,3,0};
82 scene::SMesh *mesh = new scene::SMesh();
83 for (u32 i=0; i<6; ++i)
85 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
86 buf->append(vertices + 4 * i, 4, indices, 6);
87 // Set default material
88 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
89 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
90 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
91 // Add mesh buffer to mesh
92 mesh->addMeshBuffer(buf);
96 scene::SAnimatedMesh *anim_mesh = new scene::SAnimatedMesh(mesh);
98 scaleMesh(anim_mesh, scale); // also recalculates bounding box
102 void scaleMesh(scene::IMesh *mesh, v3f scale)
107 core::aabbox3d<f32> bbox;
110 u32 mc = mesh->getMeshBufferCount();
111 for (u32 j = 0; j < mc; j++) {
112 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
113 const u32 stride = getVertexPitchFromType(buf->getVertexType());
114 u32 vertex_count = buf->getVertexCount();
115 u8 *vertices = (u8 *)buf->getVertices();
116 for (u32 i = 0; i < vertex_count; i++)
117 ((video::S3DVertex *)(vertices + i * stride))->Pos *= scale;
119 buf->recalculateBoundingBox();
121 // calculate total bounding box
123 bbox = buf->getBoundingBox();
125 bbox.addInternalBox(buf->getBoundingBox());
127 mesh->setBoundingBox(bbox);
130 void translateMesh(scene::IMesh *mesh, v3f vec)
135 core::aabbox3d<f32> bbox;
138 u32 mc = mesh->getMeshBufferCount();
139 for (u32 j = 0; j < mc; j++) {
140 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
141 const u32 stride = getVertexPitchFromType(buf->getVertexType());
142 u32 vertex_count = buf->getVertexCount();
143 u8 *vertices = (u8 *)buf->getVertices();
144 for (u32 i = 0; i < vertex_count; i++)
145 ((video::S3DVertex *)(vertices + i * stride))->Pos += vec;
147 buf->recalculateBoundingBox();
149 // calculate total bounding box
151 bbox = buf->getBoundingBox();
153 bbox.addInternalBox(buf->getBoundingBox());
155 mesh->setBoundingBox(bbox);
159 void setMeshColor(scene::IMesh *mesh, const video::SColor &color)
164 u32 mc = mesh->getMeshBufferCount();
165 for (u32 j = 0; j < mc; j++) {
166 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
167 const u32 stride = getVertexPitchFromType(buf->getVertexType());
168 u32 vertex_count = buf->getVertexCount();
169 u8 *vertices = (u8 *)buf->getVertices();
170 for (u32 i = 0; i < vertex_count; i++)
171 ((video::S3DVertex *)(vertices + i * stride))->Color = color;
175 void shadeMeshFaces(scene::IMesh *mesh)
180 u32 mc = mesh->getMeshBufferCount();
181 for (u32 j = 0; j < mc; j++) {
182 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
183 const u32 stride = getVertexPitchFromType(buf->getVertexType());
184 u32 vertex_count = buf->getVertexCount();
185 u8 *vertices = (u8 *)buf->getVertices();
186 for (u32 i = 0; i < vertex_count; i++) {
187 video::S3DVertex *vertex = (video::S3DVertex *)(vertices + i * stride);
188 video::SColor &vc = vertex->Color;
189 if (vertex->Normal.Y < -0.5) {
190 applyFacesShading (vc, 0.447213);
191 } else if (vertex->Normal.Z > 0.5) {
192 applyFacesShading (vc, 0.670820);
193 } else if (vertex->Normal.Z < -0.5) {
194 applyFacesShading (vc, 0.670820);
195 } else if (vertex->Normal.X > 0.5) {
196 applyFacesShading (vc, 0.836660);
197 } else if (vertex->Normal.X < -0.5) {
198 applyFacesShading (vc, 0.836660);
204 void setMeshColorByNormalXYZ(scene::IMesh *mesh,
205 const video::SColor &colorX,
206 const video::SColor &colorY,
207 const video::SColor &colorZ)
212 u16 mc = mesh->getMeshBufferCount();
213 for (u16 j = 0; j < mc; j++) {
214 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
215 const u32 stride = getVertexPitchFromType(buf->getVertexType());
216 u32 vertex_count = buf->getVertexCount();
217 u8 *vertices = (u8 *)buf->getVertices();
218 for (u32 i = 0; i < vertex_count; i++) {
219 video::S3DVertex *vertex = (video::S3DVertex *)(vertices + i * stride);
220 f32 x = fabs(vertex->Normal.X);
221 f32 y = fabs(vertex->Normal.Y);
222 f32 z = fabs(vertex->Normal.Z);
223 if (x >= y && x >= z)
224 vertex->Color = colorX;
226 vertex->Color = colorY;
228 vertex->Color = colorZ;
234 void rotateMeshXYby(scene::IMesh *mesh, f64 degrees)
236 u16 mc = mesh->getMeshBufferCount();
237 for (u16 j = 0; j < mc; j++) {
238 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
239 const u32 stride = getVertexPitchFromType(buf->getVertexType());
240 u32 vertex_count = buf->getVertexCount();
241 u8 *vertices = (u8 *)buf->getVertices();
242 for (u32 i = 0; i < vertex_count; i++)
243 ((video::S3DVertex *)(vertices + i * stride))->Pos.rotateXYBy(degrees);
247 void rotateMeshXZby(scene::IMesh *mesh, f64 degrees)
249 u16 mc = mesh->getMeshBufferCount();
250 for (u16 j = 0; j < mc; j++) {
251 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
252 const u32 stride = getVertexPitchFromType(buf->getVertexType());
253 u32 vertex_count = buf->getVertexCount();
254 u8 *vertices = (u8 *)buf->getVertices();
255 for (u32 i = 0; i < vertex_count; i++)
256 ((video::S3DVertex *)(vertices + i * stride))->Pos.rotateXZBy(degrees);
260 void rotateMeshYZby(scene::IMesh *mesh, f64 degrees)
262 u16 mc = mesh->getMeshBufferCount();
263 for (u16 j = 0; j < mc; j++) {
264 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
265 const u32 stride = getVertexPitchFromType(buf->getVertexType());
266 u32 vertex_count = buf->getVertexCount();
267 u8 *vertices = (u8 *)buf->getVertices();
268 for (u32 i = 0; i < vertex_count; i++)
269 ((video::S3DVertex *)(vertices + i * stride))->Pos.rotateYZBy(degrees);
273 void rotateMeshBy6dFacedir(scene::IMesh *mesh, int facedir)
275 int axisdir = facedir >> 2;
278 u16 mc = mesh->getMeshBufferCount();
279 for (u16 j = 0; j < mc; j++) {
280 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
281 const u32 stride = getVertexPitchFromType(buf->getVertexType());
282 u32 vertex_count = buf->getVertexCount();
283 u8 *vertices = (u8 *)buf->getVertices();
284 for (u32 i = 0; i < vertex_count; i++) {
285 video::S3DVertex *vertex = (video::S3DVertex *)(vertices + i * stride);
289 vertex->Pos.rotateXZBy(-90);
290 else if (facedir == 2)
291 vertex->Pos.rotateXZBy(180);
292 else if (facedir == 3)
293 vertex->Pos.rotateXZBy(90);
296 vertex->Pos.rotateYZBy(90);
298 vertex->Pos.rotateXYBy(90);
299 else if (facedir == 2)
300 vertex->Pos.rotateXYBy(180);
301 else if (facedir == 3)
302 vertex->Pos.rotateXYBy(-90);
305 vertex->Pos.rotateYZBy(-90);
307 vertex->Pos.rotateXYBy(-90);
308 else if (facedir == 2)
309 vertex->Pos.rotateXYBy(180);
310 else if (facedir == 3)
311 vertex->Pos.rotateXYBy(90);
314 vertex->Pos.rotateXYBy(-90);
316 vertex->Pos.rotateYZBy(90);
317 else if (facedir == 2)
318 vertex->Pos.rotateYZBy(180);
319 else if (facedir == 3)
320 vertex->Pos.rotateYZBy(-90);
323 vertex->Pos.rotateXYBy(90);
325 vertex->Pos.rotateYZBy(-90);
326 else if (facedir == 2)
327 vertex->Pos.rotateYZBy(180);
328 else if (facedir == 3)
329 vertex->Pos.rotateYZBy(90);
332 vertex->Pos.rotateXYBy(-180);
334 vertex->Pos.rotateXZBy(90);
335 else if (facedir == 2)
336 vertex->Pos.rotateXZBy(180);
337 else if (facedir == 3)
338 vertex->Pos.rotateXZBy(-90);
347 void recalculateBoundingBox(scene::IMesh *src_mesh)
349 core::aabbox3d<f32> bbox;
351 for (u16 j = 0; j < src_mesh->getMeshBufferCount(); j++) {
352 scene::IMeshBuffer *buf = src_mesh->getMeshBuffer(j);
353 buf->recalculateBoundingBox();
355 bbox = buf->getBoundingBox();
357 bbox.addInternalBox(buf->getBoundingBox());
359 src_mesh->setBoundingBox(bbox);
362 scene::IMesh* cloneMesh(scene::IMesh *src_mesh)
364 scene::SMesh* dst_mesh = new scene::SMesh();
365 for (u16 j = 0; j < src_mesh->getMeshBufferCount(); j++) {
366 scene::IMeshBuffer *buf = src_mesh->getMeshBuffer(j);
367 switch (buf->getVertexType()) {
368 case video::EVT_STANDARD: {
369 video::S3DVertex *v =
370 (video::S3DVertex *) buf->getVertices();
371 u16 *indices = (u16*)buf->getIndices();
372 scene::SMeshBuffer *temp_buf = new scene::SMeshBuffer();
373 temp_buf->append(v, buf->getVertexCount(),
374 indices, buf->getIndexCount());
375 dst_mesh->addMeshBuffer(temp_buf);
379 case video::EVT_2TCOORDS: {
380 video::S3DVertex2TCoords *v =
381 (video::S3DVertex2TCoords *) buf->getVertices();
382 u16 *indices = (u16*)buf->getIndices();
383 scene::SMeshBufferTangents *temp_buf =
384 new scene::SMeshBufferTangents();
385 temp_buf->append(v, buf->getVertexCount(),
386 indices, buf->getIndexCount());
387 dst_mesh->addMeshBuffer(temp_buf);
391 case video::EVT_TANGENTS: {
392 video::S3DVertexTangents *v =
393 (video::S3DVertexTangents *) buf->getVertices();
394 u16 *indices = (u16*)buf->getIndices();
395 scene::SMeshBufferTangents *temp_buf =
396 new scene::SMeshBufferTangents();
397 temp_buf->append(v, buf->getVertexCount(),
398 indices, buf->getIndexCount());
399 dst_mesh->addMeshBuffer(temp_buf);
408 scene::IMesh* convertNodeboxesToMesh(const std::vector<aabb3f> &boxes,
409 const f32 *uv_coords)
411 scene::SMesh* dst_mesh = new scene::SMesh();
413 for (u16 j = 0; j < 6; j++)
415 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
416 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
417 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
418 dst_mesh->addMeshBuffer(buf);
422 video::SColor c(255,255,255,255);
424 for(std::vector<aabb3f>::const_iterator
426 i != boxes.end(); ++i)
431 if (box.MinEdge.X > box.MaxEdge.X)
434 box.MinEdge.X=box.MaxEdge.X;
437 if (box.MinEdge.Y > box.MaxEdge.Y)
440 box.MinEdge.Y=box.MaxEdge.Y;
443 if (box.MinEdge.Z > box.MaxEdge.Z)
446 box.MinEdge.Z=box.MaxEdge.Z;
450 // Compute texture UV coords
451 f32 tx1 = (box.MinEdge.X / BS) + 0.5;
452 f32 ty1 = (box.MinEdge.Y / BS) + 0.5;
453 f32 tz1 = (box.MinEdge.Z / BS) + 0.5;
454 f32 tx2 = (box.MaxEdge.X / BS) + 0.5;
455 f32 ty2 = (box.MaxEdge.Y / BS) + 0.5;
456 f32 tz2 = (box.MaxEdge.Z / BS) + 0.5;
458 f32 txc_default[24] = {
460 tx1, 1 - tz2, tx2, 1 - tz1,
464 tz1, 1 - ty2, tz2, 1 - ty1,
466 1 - tz2, 1 - ty2, 1 - tz1, 1 - ty1,
468 1 - tx2, 1 - ty2, 1 - tx1, 1 - ty1,
470 tx1, 1 - ty2, tx2, 1 - ty1,
473 // use default texture UV mapping if not provided
474 const f32 *txc = uv_coords ? uv_coords : txc_default;
476 v3f min = box.MinEdge;
477 v3f max = box.MaxEdge;
479 video::S3DVertex vertices[24] =
482 video::S3DVertex(min.X,max.Y,max.Z, 0,1,0, c, txc[0],txc[1]),
483 video::S3DVertex(max.X,max.Y,max.Z, 0,1,0, c, txc[2],txc[1]),
484 video::S3DVertex(max.X,max.Y,min.Z, 0,1,0, c, txc[2],txc[3]),
485 video::S3DVertex(min.X,max.Y,min.Z, 0,1,0, c, txc[0],txc[3]),
487 video::S3DVertex(min.X,min.Y,min.Z, 0,-1,0, c, txc[4],txc[5]),
488 video::S3DVertex(max.X,min.Y,min.Z, 0,-1,0, c, txc[6],txc[5]),
489 video::S3DVertex(max.X,min.Y,max.Z, 0,-1,0, c, txc[6],txc[7]),
490 video::S3DVertex(min.X,min.Y,max.Z, 0,-1,0, c, txc[4],txc[7]),
492 video::S3DVertex(max.X,max.Y,min.Z, 1,0,0, c, txc[ 8],txc[9]),
493 video::S3DVertex(max.X,max.Y,max.Z, 1,0,0, c, txc[10],txc[9]),
494 video::S3DVertex(max.X,min.Y,max.Z, 1,0,0, c, txc[10],txc[11]),
495 video::S3DVertex(max.X,min.Y,min.Z, 1,0,0, c, txc[ 8],txc[11]),
497 video::S3DVertex(min.X,max.Y,max.Z, -1,0,0, c, txc[12],txc[13]),
498 video::S3DVertex(min.X,max.Y,min.Z, -1,0,0, c, txc[14],txc[13]),
499 video::S3DVertex(min.X,min.Y,min.Z, -1,0,0, c, txc[14],txc[15]),
500 video::S3DVertex(min.X,min.Y,max.Z, -1,0,0, c, txc[12],txc[15]),
502 video::S3DVertex(max.X,max.Y,max.Z, 0,0,1, c, txc[16],txc[17]),
503 video::S3DVertex(min.X,max.Y,max.Z, 0,0,1, c, txc[18],txc[17]),
504 video::S3DVertex(min.X,min.Y,max.Z, 0,0,1, c, txc[18],txc[19]),
505 video::S3DVertex(max.X,min.Y,max.Z, 0,0,1, c, txc[16],txc[19]),
507 video::S3DVertex(min.X,max.Y,min.Z, 0,0,-1, c, txc[20],txc[21]),
508 video::S3DVertex(max.X,max.Y,min.Z, 0,0,-1, c, txc[22],txc[21]),
509 video::S3DVertex(max.X,min.Y,min.Z, 0,0,-1, c, txc[22],txc[23]),
510 video::S3DVertex(min.X,min.Y,min.Z, 0,0,-1, c, txc[20],txc[23]),
513 u16 indices[] = {0,1,2,2,3,0};
515 for(u16 j = 0; j < 24; j += 4)
517 scene::IMeshBuffer *buf = dst_mesh->getMeshBuffer(j / 4);
518 buf->append(vertices + j, 4, indices, 6);
526 core::array<u32> tris;
539 const u16 cachesize = 32;
541 float FindVertexScore(vcache *v)
543 const float CacheDecayPower = 1.5f;
544 const float LastTriScore = 0.75f;
545 const float ValenceBoostScale = 2.0f;
546 const float ValenceBoostPower = 0.5f;
547 const float MaxSizeVertexCache = 32.0f;
549 if (v->NumActiveTris == 0)
551 // No tri needs this vertex!
556 int CachePosition = v->cachepos;
557 if (CachePosition < 0)
559 // Vertex is not in FIFO cache - no score.
563 if (CachePosition < 3)
565 // This vertex was used in the last triangle,
566 // so it has a fixed score.
567 Score = LastTriScore;
571 // Points for being high in the cache.
572 const float Scaler = 1.0f / (MaxSizeVertexCache - 3);
573 Score = 1.0f - (CachePosition - 3) * Scaler;
574 Score = powf(Score, CacheDecayPower);
578 // Bonus points for having a low number of tris still to
579 // use the vert, so we get rid of lone verts quickly.
580 float ValenceBoost = powf(v->NumActiveTris,
582 Score += ValenceBoostScale * ValenceBoost;
588 A specialized LRU cache for the Forsyth algorithm.
595 f_lru(vcache *v, tcache *t): vc(v), tc(t)
597 for (u16 i = 0; i < cachesize; i++)
603 // Adds this vertex index and returns the highest-scoring triangle index
604 u32 add(u16 vert, bool updatetris = false)
608 // Mark existing pos as empty
609 for (u16 i = 0; i < cachesize; i++)
611 if (cache[i] == vert)
613 // Move everything down
614 for (u16 j = i; j; j--)
616 cache[j] = cache[j - 1];
626 if (cache[cachesize-1] != -1)
627 vc[cache[cachesize-1]].cachepos = -1;
629 // Move everything down
630 for (u16 i = cachesize - 1; i; i--)
632 cache[i] = cache[i - 1];
643 // Update cache positions
644 for (u16 i = 0; i < cachesize; i++)
649 vc[cache[i]].cachepos = i;
650 vc[cache[i]].score = FindVertexScore(&vc[cache[i]]);
653 // Update triangle scores
654 for (u16 i = 0; i < cachesize; i++)
659 const u16 trisize = vc[cache[i]].tris.size();
660 for (u16 t = 0; t < trisize; t++)
662 tcache *tri = &tc[vc[cache[i]].tris[t]];
665 vc[tri->ind[0]].score +
666 vc[tri->ind[1]].score +
667 vc[tri->ind[2]].score;
669 if (tri->score > hiscore)
671 hiscore = tri->score;
672 highest = vc[cache[i]].tris[t];
682 s32 cache[cachesize];
688 Vertex cache optimization according to the Forsyth paper:
689 http://home.comcast.net/~tom_forsyth/papers/fast_vert_cache_opt.html
691 The function is thread-safe (read: you can optimize several meshes in different threads)
693 \param mesh Source mesh for the operation. */
694 scene::IMesh* createForsythOptimizedMesh(const scene::IMesh *mesh)
699 scene::SMesh *newmesh = new scene::SMesh();
700 newmesh->BoundingBox = mesh->getBoundingBox();
702 const u32 mbcount = mesh->getMeshBufferCount();
704 for (u32 b = 0; b < mbcount; ++b)
706 const scene::IMeshBuffer *mb = mesh->getMeshBuffer(b);
708 if (mb->getIndexType() != video::EIT_16BIT)
710 //os::Printer::log("Cannot optimize a mesh with 32bit indices", ELL_ERROR);
715 const u32 icount = mb->getIndexCount();
716 const u32 tcount = icount / 3;
717 const u32 vcount = mb->getVertexCount();
718 const u16 *ind = mb->getIndices();
720 vcache *vc = new vcache[vcount];
721 tcache *tc = new tcache[tcount];
726 for (u16 i = 0; i < vcount; i++)
730 vc[i].NumActiveTris = 0;
733 // First pass: count how many times a vert is used
734 for (u32 i = 0; i < icount; i += 3)
736 vc[ind[i]].NumActiveTris++;
737 vc[ind[i + 1]].NumActiveTris++;
738 vc[ind[i + 2]].NumActiveTris++;
740 const u32 tri_ind = i/3;
741 tc[tri_ind].ind[0] = ind[i];
742 tc[tri_ind].ind[1] = ind[i + 1];
743 tc[tri_ind].ind[2] = ind[i + 2];
746 // Second pass: list of each triangle
747 for (u32 i = 0; i < tcount; i++)
749 vc[tc[i].ind[0]].tris.push_back(i);
750 vc[tc[i].ind[1]].tris.push_back(i);
751 vc[tc[i].ind[2]].tris.push_back(i);
756 // Give initial scores
757 for (u16 i = 0; i < vcount; i++)
759 vc[i].score = FindVertexScore(&vc[i]);
761 for (u32 i = 0; i < tcount; i++)
764 vc[tc[i].ind[0]].score +
765 vc[tc[i].ind[1]].score +
766 vc[tc[i].ind[2]].score;
769 switch(mb->getVertexType())
771 case video::EVT_STANDARD:
773 video::S3DVertex *v = (video::S3DVertex *) mb->getVertices();
775 scene::SMeshBuffer *buf = new scene::SMeshBuffer();
776 buf->Material = mb->getMaterial();
778 buf->Vertices.reallocate(vcount);
779 buf->Indices.reallocate(icount);
781 core::map<const video::S3DVertex, const u16> sind; // search index for fast operation
782 typedef core::map<const video::S3DVertex, const u16>::Node snode;
789 if (tc[highest].drawn)
793 for (u32 t = 0; t < tcount; t++)
797 if (tc[t].score > hiscore)
800 hiscore = tc[t].score;
809 // Output the best triangle
810 u16 newind = buf->Vertices.size();
812 snode *s = sind.find(v[tc[highest].ind[0]]);
816 buf->Vertices.push_back(v[tc[highest].ind[0]]);
817 buf->Indices.push_back(newind);
818 sind.insert(v[tc[highest].ind[0]], newind);
823 buf->Indices.push_back(s->getValue());
826 s = sind.find(v[tc[highest].ind[1]]);
830 buf->Vertices.push_back(v[tc[highest].ind[1]]);
831 buf->Indices.push_back(newind);
832 sind.insert(v[tc[highest].ind[1]], newind);
837 buf->Indices.push_back(s->getValue());
840 s = sind.find(v[tc[highest].ind[2]]);
844 buf->Vertices.push_back(v[tc[highest].ind[2]]);
845 buf->Indices.push_back(newind);
846 sind.insert(v[tc[highest].ind[2]], newind);
850 buf->Indices.push_back(s->getValue());
853 vc[tc[highest].ind[0]].NumActiveTris--;
854 vc[tc[highest].ind[1]].NumActiveTris--;
855 vc[tc[highest].ind[2]].NumActiveTris--;
857 tc[highest].drawn = true;
859 for (u16 j = 0; j < 3; j++)
861 vcache *vert = &vc[tc[highest].ind[j]];
862 for (u16 t = 0; t < vert->tris.size(); t++)
864 if (highest == vert->tris[t])
872 lru.add(tc[highest].ind[0]);
873 lru.add(tc[highest].ind[1]);
874 highest = lru.add(tc[highest].ind[2], true);
878 buf->setBoundingBox(mb->getBoundingBox());
879 newmesh->addMeshBuffer(buf);
883 case video::EVT_2TCOORDS:
885 video::S3DVertex2TCoords *v = (video::S3DVertex2TCoords *) mb->getVertices();
887 scene::SMeshBufferLightMap *buf = new scene::SMeshBufferLightMap();
888 buf->Material = mb->getMaterial();
890 buf->Vertices.reallocate(vcount);
891 buf->Indices.reallocate(icount);
893 core::map<const video::S3DVertex2TCoords, const u16> sind; // search index for fast operation
894 typedef core::map<const video::S3DVertex2TCoords, const u16>::Node snode;
901 if (tc[highest].drawn)
905 for (u32 t = 0; t < tcount; t++)
909 if (tc[t].score > hiscore)
912 hiscore = tc[t].score;
921 // Output the best triangle
922 u16 newind = buf->Vertices.size();
924 snode *s = sind.find(v[tc[highest].ind[0]]);
928 buf->Vertices.push_back(v[tc[highest].ind[0]]);
929 buf->Indices.push_back(newind);
930 sind.insert(v[tc[highest].ind[0]], newind);
935 buf->Indices.push_back(s->getValue());
938 s = sind.find(v[tc[highest].ind[1]]);
942 buf->Vertices.push_back(v[tc[highest].ind[1]]);
943 buf->Indices.push_back(newind);
944 sind.insert(v[tc[highest].ind[1]], newind);
949 buf->Indices.push_back(s->getValue());
952 s = sind.find(v[tc[highest].ind[2]]);
956 buf->Vertices.push_back(v[tc[highest].ind[2]]);
957 buf->Indices.push_back(newind);
958 sind.insert(v[tc[highest].ind[2]], newind);
962 buf->Indices.push_back(s->getValue());
965 vc[tc[highest].ind[0]].NumActiveTris--;
966 vc[tc[highest].ind[1]].NumActiveTris--;
967 vc[tc[highest].ind[2]].NumActiveTris--;
969 tc[highest].drawn = true;
971 for (u16 j = 0; j < 3; j++)
973 vcache *vert = &vc[tc[highest].ind[j]];
974 for (u16 t = 0; t < vert->tris.size(); t++)
976 if (highest == vert->tris[t])
984 lru.add(tc[highest].ind[0]);
985 lru.add(tc[highest].ind[1]);
986 highest = lru.add(tc[highest].ind[2]);
990 buf->setBoundingBox(mb->getBoundingBox());
991 newmesh->addMeshBuffer(buf);
996 case video::EVT_TANGENTS:
998 video::S3DVertexTangents *v = (video::S3DVertexTangents *) mb->getVertices();
1000 scene::SMeshBufferTangents *buf = new scene::SMeshBufferTangents();
1001 buf->Material = mb->getMaterial();
1003 buf->Vertices.reallocate(vcount);
1004 buf->Indices.reallocate(icount);
1006 core::map<const video::S3DVertexTangents, const u16> sind; // search index for fast operation
1007 typedef core::map<const video::S3DVertexTangents, const u16>::Node snode;
1014 if (tc[highest].drawn)
1018 for (u32 t = 0; t < tcount; t++)
1022 if (tc[t].score > hiscore)
1025 hiscore = tc[t].score;
1034 // Output the best triangle
1035 u16 newind = buf->Vertices.size();
1037 snode *s = sind.find(v[tc[highest].ind[0]]);
1041 buf->Vertices.push_back(v[tc[highest].ind[0]]);
1042 buf->Indices.push_back(newind);
1043 sind.insert(v[tc[highest].ind[0]], newind);
1048 buf->Indices.push_back(s->getValue());
1051 s = sind.find(v[tc[highest].ind[1]]);
1055 buf->Vertices.push_back(v[tc[highest].ind[1]]);
1056 buf->Indices.push_back(newind);
1057 sind.insert(v[tc[highest].ind[1]], newind);
1062 buf->Indices.push_back(s->getValue());
1065 s = sind.find(v[tc[highest].ind[2]]);
1069 buf->Vertices.push_back(v[tc[highest].ind[2]]);
1070 buf->Indices.push_back(newind);
1071 sind.insert(v[tc[highest].ind[2]], newind);
1075 buf->Indices.push_back(s->getValue());
1078 vc[tc[highest].ind[0]].NumActiveTris--;
1079 vc[tc[highest].ind[1]].NumActiveTris--;
1080 vc[tc[highest].ind[2]].NumActiveTris--;
1082 tc[highest].drawn = true;
1084 for (u16 j = 0; j < 3; j++)
1086 vcache *vert = &vc[tc[highest].ind[j]];
1087 for (u16 t = 0; t < vert->tris.size(); t++)
1089 if (highest == vert->tris[t])
1091 vert->tris.erase(t);
1097 lru.add(tc[highest].ind[0]);
1098 lru.add(tc[highest].ind[1]);
1099 highest = lru.add(tc[highest].ind[2]);
1103 buf->setBoundingBox(mb->getBoundingBox());
1104 newmesh->addMeshBuffer(buf);
1113 } // for each meshbuffer