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)
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)
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 // Many special drawtypes have normals set to 0,0,0 and this
190 // must result in maximum brightness (no face shadng).
191 if (vertex->Normal.Y < -0.5f)
192 applyFacesShading (vc, 0.447213f);
193 else if (vertex->Normal.X > 0.5f || vertex->Normal.X < -0.5f)
194 applyFacesShading (vc, 0.670820f);
195 else if (vertex->Normal.Z > 0.5f || vertex->Normal.Z < -0.5f)
196 applyFacesShading (vc, 0.836660f);
201 void setMeshColorByNormalXYZ(scene::IMesh *mesh,
202 const video::SColor &colorX,
203 const video::SColor &colorY,
204 const video::SColor &colorZ)
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 u8 *vertices = (u8 *)buf->getVertices();
215 for (u32 i = 0; i < vertex_count; i++) {
216 video::S3DVertex *vertex = (video::S3DVertex *)(vertices + i * stride);
217 f32 x = fabs(vertex->Normal.X);
218 f32 y = fabs(vertex->Normal.Y);
219 f32 z = fabs(vertex->Normal.Z);
220 if (x >= y && x >= z)
221 vertex->Color = colorX;
223 vertex->Color = colorY;
225 vertex->Color = colorZ;
230 void setMeshColorByNormal(scene::IMesh *mesh, const v3f &normal,
231 const video::SColor &color)
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 *vertex = (video::S3DVertex *)(vertices + i * stride);
244 if (normal == vertex->Normal) {
245 vertex->Color = color;
251 void rotateMeshXYby(scene::IMesh *mesh, f64 degrees)
253 u16 mc = mesh->getMeshBufferCount();
254 for (u16 j = 0; j < mc; j++) {
255 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
256 const u32 stride = getVertexPitchFromType(buf->getVertexType());
257 u32 vertex_count = buf->getVertexCount();
258 u8 *vertices = (u8 *)buf->getVertices();
259 for (u32 i = 0; i < vertex_count; i++)
260 ((video::S3DVertex *)(vertices + i * stride))->Pos.rotateXYBy(degrees);
264 void rotateMeshXZby(scene::IMesh *mesh, f64 degrees)
266 u16 mc = mesh->getMeshBufferCount();
267 for (u16 j = 0; j < mc; j++) {
268 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
269 const u32 stride = getVertexPitchFromType(buf->getVertexType());
270 u32 vertex_count = buf->getVertexCount();
271 u8 *vertices = (u8 *)buf->getVertices();
272 for (u32 i = 0; i < vertex_count; i++)
273 ((video::S3DVertex *)(vertices + i * stride))->Pos.rotateXZBy(degrees);
277 void rotateMeshYZby(scene::IMesh *mesh, f64 degrees)
279 u16 mc = mesh->getMeshBufferCount();
280 for (u16 j = 0; j < mc; j++) {
281 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
282 const u32 stride = getVertexPitchFromType(buf->getVertexType());
283 u32 vertex_count = buf->getVertexCount();
284 u8 *vertices = (u8 *)buf->getVertices();
285 for (u32 i = 0; i < vertex_count; i++)
286 ((video::S3DVertex *)(vertices + i * stride))->Pos.rotateYZBy(degrees);
290 void rotateMeshBy6dFacedir(scene::IMesh *mesh, int facedir)
292 int axisdir = facedir >> 2;
295 u16 mc = mesh->getMeshBufferCount();
296 for (u16 j = 0; j < mc; j++) {
297 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
298 const u32 stride = getVertexPitchFromType(buf->getVertexType());
299 u32 vertex_count = buf->getVertexCount();
300 u8 *vertices = (u8 *)buf->getVertices();
301 for (u32 i = 0; i < vertex_count; i++) {
302 video::S3DVertex *vertex = (video::S3DVertex *)(vertices + i * stride);
306 vertex->Pos.rotateXZBy(-90);
307 else if (facedir == 2)
308 vertex->Pos.rotateXZBy(180);
309 else if (facedir == 3)
310 vertex->Pos.rotateXZBy(90);
313 vertex->Pos.rotateYZBy(90);
315 vertex->Pos.rotateXYBy(90);
316 else if (facedir == 2)
317 vertex->Pos.rotateXYBy(180);
318 else if (facedir == 3)
319 vertex->Pos.rotateXYBy(-90);
322 vertex->Pos.rotateYZBy(-90);
324 vertex->Pos.rotateXYBy(-90);
325 else if (facedir == 2)
326 vertex->Pos.rotateXYBy(180);
327 else if (facedir == 3)
328 vertex->Pos.rotateXYBy(90);
331 vertex->Pos.rotateXYBy(-90);
333 vertex->Pos.rotateYZBy(90);
334 else if (facedir == 2)
335 vertex->Pos.rotateYZBy(180);
336 else if (facedir == 3)
337 vertex->Pos.rotateYZBy(-90);
340 vertex->Pos.rotateXYBy(90);
342 vertex->Pos.rotateYZBy(-90);
343 else if (facedir == 2)
344 vertex->Pos.rotateYZBy(180);
345 else if (facedir == 3)
346 vertex->Pos.rotateYZBy(90);
349 vertex->Pos.rotateXYBy(-180);
351 vertex->Pos.rotateXZBy(90);
352 else if (facedir == 2)
353 vertex->Pos.rotateXZBy(180);
354 else if (facedir == 3)
355 vertex->Pos.rotateXZBy(-90);
364 void recalculateBoundingBox(scene::IMesh *src_mesh)
368 for (u16 j = 0; j < src_mesh->getMeshBufferCount(); j++) {
369 scene::IMeshBuffer *buf = src_mesh->getMeshBuffer(j);
370 buf->recalculateBoundingBox();
372 bbox = buf->getBoundingBox();
374 bbox.addInternalBox(buf->getBoundingBox());
376 src_mesh->setBoundingBox(bbox);
379 scene::IMesh* cloneMesh(scene::IMesh *src_mesh)
381 scene::SMesh* dst_mesh = new scene::SMesh();
382 for (u16 j = 0; j < src_mesh->getMeshBufferCount(); j++) {
383 scene::IMeshBuffer *buf = src_mesh->getMeshBuffer(j);
384 switch (buf->getVertexType()) {
385 case video::EVT_STANDARD: {
386 video::S3DVertex *v =
387 (video::S3DVertex *) buf->getVertices();
388 u16 *indices = (u16*)buf->getIndices();
389 scene::SMeshBuffer *temp_buf = new scene::SMeshBuffer();
390 temp_buf->append(v, buf->getVertexCount(),
391 indices, buf->getIndexCount());
392 dst_mesh->addMeshBuffer(temp_buf);
396 case video::EVT_2TCOORDS: {
397 video::S3DVertex2TCoords *v =
398 (video::S3DVertex2TCoords *) buf->getVertices();
399 u16 *indices = (u16*)buf->getIndices();
400 scene::SMeshBufferTangents *temp_buf =
401 new scene::SMeshBufferTangents();
402 temp_buf->append(v, buf->getVertexCount(),
403 indices, buf->getIndexCount());
404 dst_mesh->addMeshBuffer(temp_buf);
408 case video::EVT_TANGENTS: {
409 video::S3DVertexTangents *v =
410 (video::S3DVertexTangents *) buf->getVertices();
411 u16 *indices = (u16*)buf->getIndices();
412 scene::SMeshBufferTangents *temp_buf =
413 new scene::SMeshBufferTangents();
414 temp_buf->append(v, buf->getVertexCount(),
415 indices, buf->getIndexCount());
416 dst_mesh->addMeshBuffer(temp_buf);
425 scene::IMesh* convertNodeboxesToMesh(const std::vector<aabb3f> &boxes,
426 const f32 *uv_coords, float expand)
428 scene::SMesh* dst_mesh = new scene::SMesh();
430 for (u16 j = 0; j < 6; j++)
432 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
433 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
434 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
435 dst_mesh->addMeshBuffer(buf);
439 video::SColor c(255,255,255,255);
441 for (std::vector<aabb3f>::const_iterator
443 i != boxes.end(); ++i)
448 box.MinEdge.X -= expand;
449 box.MinEdge.Y -= expand;
450 box.MinEdge.Z -= expand;
451 box.MaxEdge.X += expand;
452 box.MaxEdge.Y += expand;
453 box.MaxEdge.Z += expand;
455 // Compute texture UV coords
456 f32 tx1 = (box.MinEdge.X / BS) + 0.5;
457 f32 ty1 = (box.MinEdge.Y / BS) + 0.5;
458 f32 tz1 = (box.MinEdge.Z / BS) + 0.5;
459 f32 tx2 = (box.MaxEdge.X / BS) + 0.5;
460 f32 ty2 = (box.MaxEdge.Y / BS) + 0.5;
461 f32 tz2 = (box.MaxEdge.Z / BS) + 0.5;
463 f32 txc_default[24] = {
465 tx1, 1 - tz2, tx2, 1 - tz1,
469 tz1, 1 - ty2, tz2, 1 - ty1,
471 1 - tz2, 1 - ty2, 1 - tz1, 1 - ty1,
473 1 - tx2, 1 - ty2, 1 - tx1, 1 - ty1,
475 tx1, 1 - ty2, tx2, 1 - ty1,
478 // use default texture UV mapping if not provided
479 const f32 *txc = uv_coords ? uv_coords : txc_default;
481 v3f min = box.MinEdge;
482 v3f max = box.MaxEdge;
484 video::S3DVertex vertices[24] =
487 video::S3DVertex(min.X,max.Y,max.Z, 0,1,0, c, txc[0],txc[1]),
488 video::S3DVertex(max.X,max.Y,max.Z, 0,1,0, c, txc[2],txc[1]),
489 video::S3DVertex(max.X,max.Y,min.Z, 0,1,0, c, txc[2],txc[3]),
490 video::S3DVertex(min.X,max.Y,min.Z, 0,1,0, c, txc[0],txc[3]),
492 video::S3DVertex(min.X,min.Y,min.Z, 0,-1,0, c, txc[4],txc[5]),
493 video::S3DVertex(max.X,min.Y,min.Z, 0,-1,0, c, txc[6],txc[5]),
494 video::S3DVertex(max.X,min.Y,max.Z, 0,-1,0, c, txc[6],txc[7]),
495 video::S3DVertex(min.X,min.Y,max.Z, 0,-1,0, c, txc[4],txc[7]),
497 video::S3DVertex(max.X,max.Y,min.Z, 1,0,0, c, txc[ 8],txc[9]),
498 video::S3DVertex(max.X,max.Y,max.Z, 1,0,0, c, txc[10],txc[9]),
499 video::S3DVertex(max.X,min.Y,max.Z, 1,0,0, c, txc[10],txc[11]),
500 video::S3DVertex(max.X,min.Y,min.Z, 1,0,0, c, txc[ 8],txc[11]),
502 video::S3DVertex(min.X,max.Y,max.Z, -1,0,0, c, txc[12],txc[13]),
503 video::S3DVertex(min.X,max.Y,min.Z, -1,0,0, c, txc[14],txc[13]),
504 video::S3DVertex(min.X,min.Y,min.Z, -1,0,0, c, txc[14],txc[15]),
505 video::S3DVertex(min.X,min.Y,max.Z, -1,0,0, c, txc[12],txc[15]),
507 video::S3DVertex(max.X,max.Y,max.Z, 0,0,1, c, txc[16],txc[17]),
508 video::S3DVertex(min.X,max.Y,max.Z, 0,0,1, c, txc[18],txc[17]),
509 video::S3DVertex(min.X,min.Y,max.Z, 0,0,1, c, txc[18],txc[19]),
510 video::S3DVertex(max.X,min.Y,max.Z, 0,0,1, c, txc[16],txc[19]),
512 video::S3DVertex(min.X,max.Y,min.Z, 0,0,-1, c, txc[20],txc[21]),
513 video::S3DVertex(max.X,max.Y,min.Z, 0,0,-1, c, txc[22],txc[21]),
514 video::S3DVertex(max.X,min.Y,min.Z, 0,0,-1, c, txc[22],txc[23]),
515 video::S3DVertex(min.X,min.Y,min.Z, 0,0,-1, c, txc[20],txc[23]),
518 u16 indices[] = {0,1,2,2,3,0};
520 for(u16 j = 0; j < 24; j += 4)
522 scene::IMeshBuffer *buf = dst_mesh->getMeshBuffer(j / 4);
523 buf->append(vertices + j, 4, indices, 6);
531 core::array<u32> tris;
544 const u16 cachesize = 32;
546 float FindVertexScore(vcache *v)
548 const float CacheDecayPower = 1.5f;
549 const float LastTriScore = 0.75f;
550 const float ValenceBoostScale = 2.0f;
551 const float ValenceBoostPower = 0.5f;
552 const float MaxSizeVertexCache = 32.0f;
554 if (v->NumActiveTris == 0)
556 // No tri needs this vertex!
561 int CachePosition = v->cachepos;
562 if (CachePosition < 0)
564 // Vertex is not in FIFO cache - no score.
568 if (CachePosition < 3)
570 // This vertex was used in the last triangle,
571 // so it has a fixed score.
572 Score = LastTriScore;
576 // Points for being high in the cache.
577 const float Scaler = 1.0f / (MaxSizeVertexCache - 3);
578 Score = 1.0f - (CachePosition - 3) * Scaler;
579 Score = powf(Score, CacheDecayPower);
583 // Bonus points for having a low number of tris still to
584 // use the vert, so we get rid of lone verts quickly.
585 float ValenceBoost = powf(v->NumActiveTris,
587 Score += ValenceBoostScale * ValenceBoost;
593 A specialized LRU cache for the Forsyth algorithm.
600 f_lru(vcache *v, tcache *t): vc(v), tc(t)
602 for (u16 i = 0; i < cachesize; i++)
608 // Adds this vertex index and returns the highest-scoring triangle index
609 u32 add(u16 vert, bool updatetris = false)
613 // Mark existing pos as empty
614 for (u16 i = 0; i < cachesize; i++)
616 if (cache[i] == vert)
618 // Move everything down
619 for (u16 j = i; j; j--)
621 cache[j] = cache[j - 1];
631 if (cache[cachesize-1] != -1)
632 vc[cache[cachesize-1]].cachepos = -1;
634 // Move everything down
635 for (u16 i = cachesize - 1; i; i--)
637 cache[i] = cache[i - 1];
648 // Update cache positions
649 for (u16 i = 0; i < cachesize; i++)
654 vc[cache[i]].cachepos = i;
655 vc[cache[i]].score = FindVertexScore(&vc[cache[i]]);
658 // Update triangle scores
659 for (u16 i = 0; i < cachesize; i++)
664 const u16 trisize = vc[cache[i]].tris.size();
665 for (u16 t = 0; t < trisize; t++)
667 tcache *tri = &tc[vc[cache[i]].tris[t]];
670 vc[tri->ind[0]].score +
671 vc[tri->ind[1]].score +
672 vc[tri->ind[2]].score;
674 if (tri->score > hiscore)
676 hiscore = tri->score;
677 highest = vc[cache[i]].tris[t];
687 s32 cache[cachesize];
693 Vertex cache optimization according to the Forsyth paper:
694 http://home.comcast.net/~tom_forsyth/papers/fast_vert_cache_opt.html
696 The function is thread-safe (read: you can optimize several meshes in different threads)
698 \param mesh Source mesh for the operation. */
699 scene::IMesh* createForsythOptimizedMesh(const scene::IMesh *mesh)
704 scene::SMesh *newmesh = new scene::SMesh();
705 newmesh->BoundingBox = mesh->getBoundingBox();
707 const u32 mbcount = mesh->getMeshBufferCount();
709 for (u32 b = 0; b < mbcount; ++b)
711 const scene::IMeshBuffer *mb = mesh->getMeshBuffer(b);
713 if (mb->getIndexType() != video::EIT_16BIT)
715 //os::Printer::log("Cannot optimize a mesh with 32bit indices", ELL_ERROR);
720 const u32 icount = mb->getIndexCount();
721 const u32 tcount = icount / 3;
722 const u32 vcount = mb->getVertexCount();
723 const u16 *ind = mb->getIndices();
725 vcache *vc = new vcache[vcount];
726 tcache *tc = new tcache[tcount];
731 for (u16 i = 0; i < vcount; i++)
735 vc[i].NumActiveTris = 0;
738 // First pass: count how many times a vert is used
739 for (u32 i = 0; i < icount; i += 3)
741 vc[ind[i]].NumActiveTris++;
742 vc[ind[i + 1]].NumActiveTris++;
743 vc[ind[i + 2]].NumActiveTris++;
745 const u32 tri_ind = i/3;
746 tc[tri_ind].ind[0] = ind[i];
747 tc[tri_ind].ind[1] = ind[i + 1];
748 tc[tri_ind].ind[2] = ind[i + 2];
751 // Second pass: list of each triangle
752 for (u32 i = 0; i < tcount; i++)
754 vc[tc[i].ind[0]].tris.push_back(i);
755 vc[tc[i].ind[1]].tris.push_back(i);
756 vc[tc[i].ind[2]].tris.push_back(i);
761 // Give initial scores
762 for (u16 i = 0; i < vcount; i++)
764 vc[i].score = FindVertexScore(&vc[i]);
766 for (u32 i = 0; i < tcount; i++)
769 vc[tc[i].ind[0]].score +
770 vc[tc[i].ind[1]].score +
771 vc[tc[i].ind[2]].score;
774 switch(mb->getVertexType())
776 case video::EVT_STANDARD:
778 video::S3DVertex *v = (video::S3DVertex *) mb->getVertices();
780 scene::SMeshBuffer *buf = new scene::SMeshBuffer();
781 buf->Material = mb->getMaterial();
783 buf->Vertices.reallocate(vcount);
784 buf->Indices.reallocate(icount);
786 core::map<const video::S3DVertex, const u16> sind; // search index for fast operation
787 typedef core::map<const video::S3DVertex, const u16>::Node snode;
794 if (tc[highest].drawn)
798 for (u32 t = 0; t < tcount; t++)
802 if (tc[t].score > hiscore)
805 hiscore = tc[t].score;
814 // Output the best triangle
815 u16 newind = buf->Vertices.size();
817 snode *s = sind.find(v[tc[highest].ind[0]]);
821 buf->Vertices.push_back(v[tc[highest].ind[0]]);
822 buf->Indices.push_back(newind);
823 sind.insert(v[tc[highest].ind[0]], newind);
828 buf->Indices.push_back(s->getValue());
831 s = sind.find(v[tc[highest].ind[1]]);
835 buf->Vertices.push_back(v[tc[highest].ind[1]]);
836 buf->Indices.push_back(newind);
837 sind.insert(v[tc[highest].ind[1]], newind);
842 buf->Indices.push_back(s->getValue());
845 s = sind.find(v[tc[highest].ind[2]]);
849 buf->Vertices.push_back(v[tc[highest].ind[2]]);
850 buf->Indices.push_back(newind);
851 sind.insert(v[tc[highest].ind[2]], newind);
855 buf->Indices.push_back(s->getValue());
858 vc[tc[highest].ind[0]].NumActiveTris--;
859 vc[tc[highest].ind[1]].NumActiveTris--;
860 vc[tc[highest].ind[2]].NumActiveTris--;
862 tc[highest].drawn = true;
864 for (u16 j = 0; j < 3; j++)
866 vcache *vert = &vc[tc[highest].ind[j]];
867 for (u16 t = 0; t < vert->tris.size(); t++)
869 if (highest == vert->tris[t])
877 lru.add(tc[highest].ind[0]);
878 lru.add(tc[highest].ind[1]);
879 highest = lru.add(tc[highest].ind[2], true);
883 buf->setBoundingBox(mb->getBoundingBox());
884 newmesh->addMeshBuffer(buf);
888 case video::EVT_2TCOORDS:
890 video::S3DVertex2TCoords *v = (video::S3DVertex2TCoords *) mb->getVertices();
892 scene::SMeshBufferLightMap *buf = new scene::SMeshBufferLightMap();
893 buf->Material = mb->getMaterial();
895 buf->Vertices.reallocate(vcount);
896 buf->Indices.reallocate(icount);
898 core::map<const video::S3DVertex2TCoords, const u16> sind; // search index for fast operation
899 typedef core::map<const video::S3DVertex2TCoords, const u16>::Node snode;
906 if (tc[highest].drawn)
910 for (u32 t = 0; t < tcount; t++)
914 if (tc[t].score > hiscore)
917 hiscore = tc[t].score;
926 // Output the best triangle
927 u16 newind = buf->Vertices.size();
929 snode *s = sind.find(v[tc[highest].ind[0]]);
933 buf->Vertices.push_back(v[tc[highest].ind[0]]);
934 buf->Indices.push_back(newind);
935 sind.insert(v[tc[highest].ind[0]], newind);
940 buf->Indices.push_back(s->getValue());
943 s = sind.find(v[tc[highest].ind[1]]);
947 buf->Vertices.push_back(v[tc[highest].ind[1]]);
948 buf->Indices.push_back(newind);
949 sind.insert(v[tc[highest].ind[1]], newind);
954 buf->Indices.push_back(s->getValue());
957 s = sind.find(v[tc[highest].ind[2]]);
961 buf->Vertices.push_back(v[tc[highest].ind[2]]);
962 buf->Indices.push_back(newind);
963 sind.insert(v[tc[highest].ind[2]], newind);
967 buf->Indices.push_back(s->getValue());
970 vc[tc[highest].ind[0]].NumActiveTris--;
971 vc[tc[highest].ind[1]].NumActiveTris--;
972 vc[tc[highest].ind[2]].NumActiveTris--;
974 tc[highest].drawn = true;
976 for (u16 j = 0; j < 3; j++)
978 vcache *vert = &vc[tc[highest].ind[j]];
979 for (u16 t = 0; t < vert->tris.size(); t++)
981 if (highest == vert->tris[t])
989 lru.add(tc[highest].ind[0]);
990 lru.add(tc[highest].ind[1]);
991 highest = lru.add(tc[highest].ind[2]);
995 buf->setBoundingBox(mb->getBoundingBox());
996 newmesh->addMeshBuffer(buf);
1001 case video::EVT_TANGENTS:
1003 video::S3DVertexTangents *v = (video::S3DVertexTangents *) mb->getVertices();
1005 scene::SMeshBufferTangents *buf = new scene::SMeshBufferTangents();
1006 buf->Material = mb->getMaterial();
1008 buf->Vertices.reallocate(vcount);
1009 buf->Indices.reallocate(icount);
1011 core::map<const video::S3DVertexTangents, const u16> sind; // search index for fast operation
1012 typedef core::map<const video::S3DVertexTangents, const u16>::Node snode;
1019 if (tc[highest].drawn)
1023 for (u32 t = 0; t < tcount; t++)
1027 if (tc[t].score > hiscore)
1030 hiscore = tc[t].score;
1039 // Output the best triangle
1040 u16 newind = buf->Vertices.size();
1042 snode *s = sind.find(v[tc[highest].ind[0]]);
1046 buf->Vertices.push_back(v[tc[highest].ind[0]]);
1047 buf->Indices.push_back(newind);
1048 sind.insert(v[tc[highest].ind[0]], newind);
1053 buf->Indices.push_back(s->getValue());
1056 s = sind.find(v[tc[highest].ind[1]]);
1060 buf->Vertices.push_back(v[tc[highest].ind[1]]);
1061 buf->Indices.push_back(newind);
1062 sind.insert(v[tc[highest].ind[1]], newind);
1067 buf->Indices.push_back(s->getValue());
1070 s = sind.find(v[tc[highest].ind[2]]);
1074 buf->Vertices.push_back(v[tc[highest].ind[2]]);
1075 buf->Indices.push_back(newind);
1076 sind.insert(v[tc[highest].ind[2]], newind);
1080 buf->Indices.push_back(s->getValue());
1083 vc[tc[highest].ind[0]].NumActiveTris--;
1084 vc[tc[highest].ind[1]].NumActiveTris--;
1085 vc[tc[highest].ind[2]].NumActiveTris--;
1087 tc[highest].drawn = true;
1089 for (u16 j = 0; j < 3; j++)
1091 vcache *vert = &vc[tc[highest].ind[j]];
1092 for (u16 t = 0; t < vert->tris.size(); t++)
1094 if (highest == vert->tris[t])
1096 vert->tris.erase(t);
1102 lru.add(tc[highest].ind[0]);
1103 lru.add(tc[highest].ind[1]);
1104 highest = lru.add(tc[highest].ind[2]);
1108 buf->setBoundingBox(mb->getBoundingBox());
1109 newmesh->addMeshBuffer(buf);
1118 } // for each meshbuffer