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.
24 #include <IAnimatedMesh.h>
25 #include <SAnimatedMesh.h>
27 // In Irrlicht 1.8 the signature of ITexture::lock was changed from
28 // (bool, u32) to (E_TEXTURE_LOCK_MODE, u32).
29 #if IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR <= 7
30 #define MY_ETLM_READ_ONLY true
32 #define MY_ETLM_READ_ONLY video::ETLM_READ_ONLY
35 scene::IAnimatedMesh* createCubeMesh(v3f scale)
37 video::SColor c(255,255,255,255);
38 video::S3DVertex vertices[24] =
41 video::S3DVertex(-0.5,+0.5,-0.5, 0,1,0, c, 0,1),
42 video::S3DVertex(-0.5,+0.5,+0.5, 0,1,0, c, 0,0),
43 video::S3DVertex(+0.5,+0.5,+0.5, 0,1,0, c, 1,0),
44 video::S3DVertex(+0.5,+0.5,-0.5, 0,1,0, c, 1,1),
46 video::S3DVertex(-0.5,-0.5,-0.5, 0,-1,0, c, 0,0),
47 video::S3DVertex(+0.5,-0.5,-0.5, 0,-1,0, c, 1,0),
48 video::S3DVertex(+0.5,-0.5,+0.5, 0,-1,0, c, 1,1),
49 video::S3DVertex(-0.5,-0.5,+0.5, 0,-1,0, c, 0,1),
51 video::S3DVertex(+0.5,-0.5,-0.5, 1,0,0, c, 0,1),
52 video::S3DVertex(+0.5,+0.5,-0.5, 1,0,0, c, 0,0),
53 video::S3DVertex(+0.5,+0.5,+0.5, 1,0,0, c, 1,0),
54 video::S3DVertex(+0.5,-0.5,+0.5, 1,0,0, c, 1,1),
56 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, 0,1),
58 video::S3DVertex(-0.5,+0.5,+0.5, -1,0,0, c, 0,0),
59 video::S3DVertex(-0.5,+0.5,-0.5, -1,0,0, c, 1,0),
61 video::S3DVertex(-0.5,-0.5,+0.5, 0,0,1, c, 1,1),
62 video::S3DVertex(+0.5,-0.5,+0.5, 0,0,1, c, 0,1),
63 video::S3DVertex(+0.5,+0.5,+0.5, 0,0,1, c, 0,0),
64 video::S3DVertex(-0.5,+0.5,+0.5, 0,0,1, c, 1,0),
66 video::S3DVertex(-0.5,-0.5,-0.5, 0,0,-1, c, 0,1),
67 video::S3DVertex(-0.5,+0.5,-0.5, 0,0,-1, c, 0,0),
68 video::S3DVertex(+0.5,+0.5,-0.5, 0,0,-1, c, 1,0),
69 video::S3DVertex(+0.5,-0.5,-0.5, 0,0,-1, c, 1,1),
72 u16 indices[6] = {0,1,2,2,3,0};
74 scene::SMesh *mesh = new scene::SMesh();
75 for (u32 i=0; i<6; ++i)
77 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
78 buf->append(vertices + 4 * i, 4, indices, 6);
79 // Set default material
80 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
81 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
82 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
83 // Add mesh buffer to mesh
84 mesh->addMeshBuffer(buf);
88 scene::SAnimatedMesh *anim_mesh = new scene::SAnimatedMesh(mesh);
90 scaleMesh(anim_mesh, scale); // also recalculates bounding box
94 static scene::IAnimatedMesh* extrudeARGB(u32 twidth, u32 theight, u8 *data)
96 const s32 argb_wstep = 4 * twidth;
97 const s32 alpha_threshold = 1;
99 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
100 video::SColor c(255,255,255,255);
104 video::S3DVertex vertices[8] =
106 video::S3DVertex(-0.5,-0.5,-0.5, 0,0,-1, c, 0,1),
107 video::S3DVertex(-0.5,+0.5,-0.5, 0,0,-1, c, 0,0),
108 video::S3DVertex(+0.5,+0.5,-0.5, 0,0,-1, c, 1,0),
109 video::S3DVertex(+0.5,-0.5,-0.5, 0,0,-1, c, 1,1),
110 video::S3DVertex(+0.5,-0.5,+0.5, 0,0,+1, c, 1,1),
111 video::S3DVertex(+0.5,+0.5,+0.5, 0,0,+1, c, 1,0),
112 video::S3DVertex(-0.5,+0.5,+0.5, 0,0,+1, c, 0,0),
113 video::S3DVertex(-0.5,-0.5,+0.5, 0,0,+1, c, 0,1),
115 u16 indices[12] = {0,1,2,2,3,0,4,5,6,6,7,4};
116 buf->append(vertices, 8, indices, 12);
120 // (add faces where a solid pixel is next to a transparent one)
121 u8 *solidity = new u8[(twidth+2) * (theight+2)];
122 u32 wstep = twidth + 2;
123 for (u32 y = 0; y < theight + 2; ++y)
125 u8 *scanline = solidity + y * wstep;
126 if (y == 0 || y == theight + 1)
128 for (u32 x = 0; x < twidth + 2; ++x)
134 u8 *argb_scanline = data + (y - 1) * argb_wstep;
135 for (u32 x = 0; x < twidth; ++x)
136 scanline[x+1] = (argb_scanline[x*4+3] >= alpha_threshold);
137 scanline[twidth + 1] = 0;
141 // without this, there would be occasional "holes" in the mesh
144 for (u32 y = 0; y <= theight; ++y)
146 u8 *scanline = solidity + y * wstep + 1;
147 for (u32 x = 0; x <= twidth; ++x)
149 if (scanline[x] && !scanline[x + wstep])
152 while (scanline[xx] && !scanline[xx + wstep])
154 f32 vx1 = (x - eps) / (f32) twidth - 0.5;
155 f32 vx2 = (xx + eps) / (f32) twidth - 0.5;
156 f32 vy = 0.5 - (y - eps) / (f32) theight;
157 f32 tx1 = x / (f32) twidth;
158 f32 tx2 = xx / (f32) twidth;
159 f32 ty = (y - 0.5) / (f32) theight;
160 video::S3DVertex vertices[8] =
162 video::S3DVertex(vx1,vy,-0.5, 0,-1,0, c, tx1,ty),
163 video::S3DVertex(vx2,vy,-0.5, 0,-1,0, c, tx2,ty),
164 video::S3DVertex(vx2,vy,+0.5, 0,-1,0, c, tx2,ty),
165 video::S3DVertex(vx1,vy,+0.5, 0,-1,0, c, tx1,ty),
167 u16 indices[6] = {0,1,2,2,3,0};
168 buf->append(vertices, 4, indices, 6);
171 if (!scanline[x] && scanline[x + wstep])
174 while (!scanline[xx] && scanline[xx + wstep])
176 f32 vx1 = (x - eps) / (f32) twidth - 0.5;
177 f32 vx2 = (xx + eps) / (f32) twidth - 0.5;
178 f32 vy = 0.5 - (y + eps) / (f32) theight;
179 f32 tx1 = x / (f32) twidth;
180 f32 tx2 = xx / (f32) twidth;
181 f32 ty = (y + 0.5) / (f32) theight;
182 video::S3DVertex vertices[8] =
184 video::S3DVertex(vx1,vy,-0.5, 0,1,0, c, tx1,ty),
185 video::S3DVertex(vx1,vy,+0.5, 0,1,0, c, tx1,ty),
186 video::S3DVertex(vx2,vy,+0.5, 0,1,0, c, tx2,ty),
187 video::S3DVertex(vx2,vy,-0.5, 0,1,0, c, tx2,ty),
189 u16 indices[6] = {0,1,2,2,3,0};
190 buf->append(vertices, 4, indices, 6);
196 for (u32 x = 0; x <= twidth; ++x)
198 u8 *scancol = solidity + x + wstep;
199 for (u32 y = 0; y <= theight; ++y)
201 if (scancol[y * wstep] && !scancol[y * wstep + 1])
204 while (scancol[yy * wstep] && !scancol[yy * wstep + 1])
206 f32 vx = (x - eps) / (f32) twidth - 0.5;
207 f32 vy1 = 0.5 - (y - eps) / (f32) theight;
208 f32 vy2 = 0.5 - (yy + eps) / (f32) theight;
209 f32 tx = (x - 0.5) / (f32) twidth;
210 f32 ty1 = y / (f32) theight;
211 f32 ty2 = yy / (f32) theight;
212 video::S3DVertex vertices[8] =
214 video::S3DVertex(vx,vy1,-0.5, 1,0,0, c, tx,ty1),
215 video::S3DVertex(vx,vy1,+0.5, 1,0,0, c, tx,ty1),
216 video::S3DVertex(vx,vy2,+0.5, 1,0,0, c, tx,ty2),
217 video::S3DVertex(vx,vy2,-0.5, 1,0,0, c, tx,ty2),
219 u16 indices[6] = {0,1,2,2,3,0};
220 buf->append(vertices, 4, indices, 6);
223 if (!scancol[y * wstep] && scancol[y * wstep + 1])
226 while (!scancol[yy * wstep] && scancol[yy * wstep + 1])
228 f32 vx = (x + eps) / (f32) twidth - 0.5;
229 f32 vy1 = 0.5 - (y - eps) / (f32) theight;
230 f32 vy2 = 0.5 - (yy + eps) / (f32) theight;
231 f32 tx = (x + 0.5) / (f32) twidth;
232 f32 ty1 = y / (f32) theight;
233 f32 ty2 = yy / (f32) theight;
234 video::S3DVertex vertices[8] =
236 video::S3DVertex(vx,vy1,-0.5, -1,0,0, c, tx,ty1),
237 video::S3DVertex(vx,vy2,-0.5, -1,0,0, c, tx,ty2),
238 video::S3DVertex(vx,vy2,+0.5, -1,0,0, c, tx,ty2),
239 video::S3DVertex(vx,vy1,+0.5, -1,0,0, c, tx,ty1),
241 u16 indices[6] = {0,1,2,2,3,0};
242 buf->append(vertices, 4, indices, 6);
251 scene::SMesh *mesh = new scene::SMesh();
252 mesh->addMeshBuffer(buf);
254 scene::SAnimatedMesh *anim_mesh = new scene::SAnimatedMesh(mesh);
259 scene::IAnimatedMesh* createExtrudedMesh(video::ITexture *texture,
260 video::IVideoDriver *driver, v3f scale)
262 scene::IAnimatedMesh *mesh = NULL;
263 core::dimension2d<u32> size = texture->getOriginalSize();
264 video::ECOLOR_FORMAT format = texture->getColorFormat();
265 if (format == video::ECF_A8R8G8B8)
267 // Texture is in the correct color format, we can pass it
268 // to extrudeARGB right away.
269 void *data = texture->lock(MY_ETLM_READ_ONLY);
272 mesh = extrudeARGB(size.Width, size.Height, (u8*) data);
277 video::IImage *img1 = driver->createImageFromData(format, size, texture->lock(MY_ETLM_READ_ONLY));
281 // img1 is in the texture's color format, convert to 8-bit ARGB
282 video::IImage *img2 = driver->createImage(video::ECF_A8R8G8B8, size);
291 mesh = extrudeARGB(size.Width, size.Height, (u8*) img2->lock());
296 // Set default material
297 mesh->getMeshBuffer(0)->getMaterial().setTexture(0, texture);
298 mesh->getMeshBuffer(0)->getMaterial().setFlag(video::EMF_LIGHTING, false);
299 mesh->getMeshBuffer(0)->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
300 mesh->getMeshBuffer(0)->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
302 scaleMesh(mesh, scale); // also recalculates bounding box
306 void scaleMesh(scene::IMesh *mesh, v3f scale)
311 core::aabbox3d<f32> bbox;
314 u16 mc = mesh->getMeshBufferCount();
315 for(u16 j=0; j<mc; j++)
317 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
318 video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
319 u16 vc = buf->getVertexCount();
320 for(u16 i=0; i<vc; i++)
322 vertices[i].Pos *= scale;
324 buf->recalculateBoundingBox();
326 // calculate total bounding box
328 bbox = buf->getBoundingBox();
330 bbox.addInternalBox(buf->getBoundingBox());
332 mesh->setBoundingBox(bbox);
335 void translateMesh(scene::IMesh *mesh, v3f vec)
340 core::aabbox3d<f32> bbox;
343 u16 mc = mesh->getMeshBufferCount();
344 for(u16 j=0; j<mc; j++)
346 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
347 video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
348 u16 vc = buf->getVertexCount();
349 for(u16 i=0; i<vc; i++)
351 vertices[i].Pos += vec;
353 buf->recalculateBoundingBox();
355 // calculate total bounding box
357 bbox = buf->getBoundingBox();
359 bbox.addInternalBox(buf->getBoundingBox());
361 mesh->setBoundingBox(bbox);
364 void setMeshColor(scene::IMesh *mesh, const video::SColor &color)
369 u16 mc = mesh->getMeshBufferCount();
370 for(u16 j=0; j<mc; j++)
372 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
373 video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
374 u16 vc = buf->getVertexCount();
375 for(u16 i=0; i<vc; i++)
377 vertices[i].Color = color;
382 void setMeshColorByNormalXYZ(scene::IMesh *mesh,
383 const video::SColor &colorX,
384 const video::SColor &colorY,
385 const video::SColor &colorZ)
390 u16 mc = mesh->getMeshBufferCount();
391 for(u16 j=0; j<mc; j++)
393 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
394 video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
395 u16 vc = buf->getVertexCount();
396 for(u16 i=0; i<vc; i++)
398 f32 x = fabs(vertices[i].Normal.X);
399 f32 y = fabs(vertices[i].Normal.Y);
400 f32 z = fabs(vertices[i].Normal.Z);
402 vertices[i].Color = colorX;
404 vertices[i].Color = colorY;
406 vertices[i].Color = colorZ;
412 void rotateMeshBy6dFacedir(scene::IMesh *mesh, int facedir)
414 int axisdir = facedir>>2;
417 u16 mc = mesh->getMeshBufferCount();
418 for(u16 j = 0; j < mc; j++)
420 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
421 video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
422 u16 vc = buf->getVertexCount();
423 for(u16 i=0; i<vc; i++)
429 vertices[i].Pos.rotateXZBy(-90);
430 else if(facedir == 2)
431 vertices[i].Pos.rotateXZBy(180);
432 else if(facedir == 3)
433 vertices[i].Pos.rotateXZBy(90);
436 vertices[i].Pos.rotateYZBy(90);
438 vertices[i].Pos.rotateXYBy(90);
439 else if(facedir == 2)
440 vertices[i].Pos.rotateXYBy(180);
441 else if(facedir == 3)
442 vertices[i].Pos.rotateXYBy(-90);
445 vertices[i].Pos.rotateYZBy(-90);
447 vertices[i].Pos.rotateXYBy(-90);
448 else if(facedir == 2)
449 vertices[i].Pos.rotateXYBy(180);
450 else if(facedir == 3)
451 vertices[i].Pos.rotateXYBy(90);
454 vertices[i].Pos.rotateXYBy(-90);
456 vertices[i].Pos.rotateYZBy(90);
457 else if(facedir == 2)
458 vertices[i].Pos.rotateYZBy(180);
459 else if(facedir == 3)
460 vertices[i].Pos.rotateYZBy(-90);
463 vertices[i].Pos.rotateXYBy(90);
465 vertices[i].Pos.rotateYZBy(-90);
466 else if(facedir == 2)
467 vertices[i].Pos.rotateYZBy(180);
468 else if(facedir == 3)
469 vertices[i].Pos.rotateYZBy(90);
472 vertices[i].Pos.rotateXYBy(-180);
474 vertices[i].Pos.rotateXZBy(90);
475 else if(facedir == 2)
476 vertices[i].Pos.rotateXZBy(180);
477 else if(facedir == 3)
478 vertices[i].Pos.rotateXZBy(-90);
487 void recalculateBoundingBox(scene::IMesh *src_mesh)
489 core::aabbox3d<f32> bbox;
491 for(u16 j = 0; j < src_mesh->getMeshBufferCount(); j++)
493 scene::IMeshBuffer *buf = src_mesh->getMeshBuffer(j);
494 buf->recalculateBoundingBox();
496 bbox = buf->getBoundingBox();
498 bbox.addInternalBox(buf->getBoundingBox());
500 src_mesh->setBoundingBox(bbox);
503 scene::IMesh* cloneMesh(scene::IMesh *src_mesh)
505 scene::SMesh* dst_mesh = new scene::SMesh();
506 for(u16 j = 0; j < src_mesh->getMeshBufferCount(); j++)
508 scene::IMeshBuffer *buf = src_mesh->getMeshBuffer(j);
509 video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
510 u16 *indices = (u16*)buf->getIndices();
511 scene::SMeshBuffer *temp_buf = new scene::SMeshBuffer();
512 temp_buf->append(vertices, buf->getVertexCount(),
513 indices, buf->getIndexCount());
514 dst_mesh->addMeshBuffer(temp_buf);
520 scene::IMesh* convertNodeboxNodeToMesh(ContentFeatures *f)
522 scene::SMesh* dst_mesh = new scene::SMesh();
523 for (u16 j = 0; j < 6; j++)
525 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
526 dst_mesh->addMeshBuffer(buf);
530 video::SColor c(255,255,255,255);
532 std::vector<aabb3f> boxes = f->node_box.fixed;
534 for(std::vector<aabb3f>::iterator
536 i != boxes.end(); i++)
541 if (box.MinEdge.X > box.MaxEdge.X)
544 box.MinEdge.X=box.MaxEdge.X;
547 if (box.MinEdge.Y > box.MaxEdge.Y)
550 box.MinEdge.Y=box.MaxEdge.Y;
553 if (box.MinEdge.Z > box.MaxEdge.Z)
556 box.MinEdge.Z=box.MaxEdge.Z;
559 // Compute texture coords
560 f32 tx1 = (box.MinEdge.X/BS)+0.5;
561 f32 ty1 = (box.MinEdge.Y/BS)+0.5;
562 f32 tz1 = (box.MinEdge.Z/BS)+0.5;
563 f32 tx2 = (box.MaxEdge.X/BS)+0.5;
564 f32 ty2 = (box.MaxEdge.Y/BS)+0.5;
565 f32 tz2 = (box.MaxEdge.Z/BS)+0.5;
568 tx1, 1-tz2, tx2, 1-tz1,
572 tz1, 1-ty2, tz2, 1-ty1,
574 1-tz2, 1-ty2, 1-tz1, 1-ty1,
576 1-tx2, 1-ty2, 1-tx1, 1-ty1,
578 tx1, 1-ty2, tx2, 1-ty1,
580 v3f min = box.MinEdge;
581 v3f max = box.MaxEdge;
583 video::S3DVertex vertices[24] =
586 video::S3DVertex(min.X,max.Y,max.Z, 0,1,0, c, txc[0],txc[1]),
587 video::S3DVertex(max.X,max.Y,max.Z, 0,1,0, c, txc[2],txc[1]),
588 video::S3DVertex(max.X,max.Y,min.Z, 0,1,0, c, txc[2],txc[3]),
589 video::S3DVertex(min.X,max.Y,min.Z, 0,1,0, c, txc[0],txc[3]),
591 video::S3DVertex(min.X,min.Y,min.Z, 0,-1,0, c, txc[4],txc[5]),
592 video::S3DVertex(max.X,min.Y,min.Z, 0,-1,0, c, txc[6],txc[5]),
593 video::S3DVertex(max.X,min.Y,max.Z, 0,-1,0, c, txc[6],txc[7]),
594 video::S3DVertex(min.X,min.Y,max.Z, 0,-1,0, c, txc[4],txc[7]),
596 video::S3DVertex(max.X,max.Y,min.Z, 1,0,0, c, txc[ 8],txc[9]),
597 video::S3DVertex(max.X,max.Y,max.Z, 1,0,0, c, txc[10],txc[9]),
598 video::S3DVertex(max.X,min.Y,max.Z, 1,0,0, c, txc[10],txc[11]),
599 video::S3DVertex(max.X,min.Y,min.Z, 1,0,0, c, txc[ 8],txc[11]),
601 video::S3DVertex(min.X,max.Y,max.Z, -1,0,0, c, txc[12],txc[13]),
602 video::S3DVertex(min.X,max.Y,min.Z, -1,0,0, c, txc[14],txc[13]),
603 video::S3DVertex(min.X,min.Y,min.Z, -1,0,0, c, txc[14],txc[15]),
604 video::S3DVertex(min.X,min.Y,max.Z, -1,0,0, c, txc[12],txc[15]),
606 video::S3DVertex(max.X,max.Y,max.Z, 0,0,1, c, txc[16],txc[17]),
607 video::S3DVertex(min.X,max.Y,max.Z, 0,0,1, c, txc[18],txc[17]),
608 video::S3DVertex(min.X,min.Y,max.Z, 0,0,1, c, txc[18],txc[19]),
609 video::S3DVertex(max.X,min.Y,max.Z, 0,0,1, c, txc[16],txc[19]),
611 video::S3DVertex(min.X,max.Y,min.Z, 0,0,-1, c, txc[20],txc[21]),
612 video::S3DVertex(max.X,max.Y,min.Z, 0,0,-1, c, txc[22],txc[21]),
613 video::S3DVertex(max.X,min.Y,min.Z, 0,0,-1, c, txc[22],txc[23]),
614 video::S3DVertex(min.X,min.Y,min.Z, 0,0,-1, c, txc[20],txc[23]),
617 u16 indices[] = {0,1,2,2,3,0};
619 for(u16 j = 0; j < 24; j += 4)
621 scene::IMeshBuffer *buf = dst_mesh->getMeshBuffer(j / 4);
622 buf->append(vertices + j, 4, indices, 6);