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>
26 #include <ICameraSceneNode.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 static scene::IAnimatedMesh* extrudeARGB(u32 twidth, u32 theight, u8 *data)
97 const s32 argb_wstep = 4 * twidth;
98 const s32 alpha_threshold = 1;
100 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
101 video::SColor c(255,255,255,255);
105 video::S3DVertex vertices[8] =
107 video::S3DVertex(-0.5,-0.5,-0.5, 0,0,-1, c, 0,1),
108 video::S3DVertex(-0.5,+0.5,-0.5, 0,0,-1, c, 0,0),
109 video::S3DVertex(+0.5,+0.5,-0.5, 0,0,-1, c, 1,0),
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,1),
112 video::S3DVertex(+0.5,+0.5,+0.5, 0,0,+1, c, 1,0),
113 video::S3DVertex(-0.5,+0.5,+0.5, 0,0,+1, c, 0,0),
114 video::S3DVertex(-0.5,-0.5,+0.5, 0,0,+1, c, 0,1),
116 u16 indices[12] = {0,1,2,2,3,0,4,5,6,6,7,4};
117 buf->append(vertices, 8, indices, 12);
121 // (add faces where a solid pixel is next to a transparent one)
122 u8 *solidity = new u8[(twidth+2) * (theight+2)];
123 u32 wstep = twidth + 2;
124 for (u32 y = 0; y < theight + 2; ++y)
126 u8 *scanline = solidity + y * wstep;
127 if (y == 0 || y == theight + 1)
129 for (u32 x = 0; x < twidth + 2; ++x)
135 u8 *argb_scanline = data + (y - 1) * argb_wstep;
136 for (u32 x = 0; x < twidth; ++x)
137 scanline[x+1] = (argb_scanline[x*4+3] >= alpha_threshold);
138 scanline[twidth + 1] = 0;
142 // without this, there would be occasional "holes" in the mesh
145 for (u32 y = 0; y <= theight; ++y)
147 u8 *scanline = solidity + y * wstep + 1;
148 for (u32 x = 0; x <= twidth; ++x)
150 if (scanline[x] && !scanline[x + wstep])
153 while (scanline[xx] && !scanline[xx + wstep])
155 f32 vx1 = (x - eps) / (f32) twidth - 0.5;
156 f32 vx2 = (xx + eps) / (f32) twidth - 0.5;
157 f32 vy = 0.5 - (y - eps) / (f32) theight;
158 f32 tx1 = x / (f32) twidth;
159 f32 tx2 = xx / (f32) twidth;
160 f32 ty = (y - 0.5) / (f32) theight;
161 video::S3DVertex vertices[8] =
163 video::S3DVertex(vx1,vy,-0.5, 0,-1,0, c, tx1,ty),
164 video::S3DVertex(vx2,vy,-0.5, 0,-1,0, c, tx2,ty),
165 video::S3DVertex(vx2,vy,+0.5, 0,-1,0, c, tx2,ty),
166 video::S3DVertex(vx1,vy,+0.5, 0,-1,0, c, tx1,ty),
168 u16 indices[6] = {0,1,2,2,3,0};
169 buf->append(vertices, 4, indices, 6);
172 if (!scanline[x] && scanline[x + wstep])
175 while (!scanline[xx] && scanline[xx + wstep])
177 f32 vx1 = (x - eps) / (f32) twidth - 0.5;
178 f32 vx2 = (xx + eps) / (f32) twidth - 0.5;
179 f32 vy = 0.5 - (y + eps) / (f32) theight;
180 f32 tx1 = x / (f32) twidth;
181 f32 tx2 = xx / (f32) twidth;
182 f32 ty = (y + 0.5) / (f32) theight;
183 video::S3DVertex vertices[8] =
185 video::S3DVertex(vx1,vy,-0.5, 0,1,0, c, tx1,ty),
186 video::S3DVertex(vx1,vy,+0.5, 0,1,0, c, tx1,ty),
187 video::S3DVertex(vx2,vy,+0.5, 0,1,0, c, tx2,ty),
188 video::S3DVertex(vx2,vy,-0.5, 0,1,0, c, tx2,ty),
190 u16 indices[6] = {0,1,2,2,3,0};
191 buf->append(vertices, 4, indices, 6);
197 for (u32 x = 0; x <= twidth; ++x)
199 u8 *scancol = solidity + x + wstep;
200 for (u32 y = 0; y <= theight; ++y)
202 if (scancol[y * wstep] && !scancol[y * wstep + 1])
205 while (scancol[yy * wstep] && !scancol[yy * wstep + 1])
207 f32 vx = (x - eps) / (f32) twidth - 0.5;
208 f32 vy1 = 0.5 - (y - eps) / (f32) theight;
209 f32 vy2 = 0.5 - (yy + eps) / (f32) theight;
210 f32 tx = (x - 0.5) / (f32) twidth;
211 f32 ty1 = y / (f32) theight;
212 f32 ty2 = yy / (f32) theight;
213 video::S3DVertex vertices[8] =
215 video::S3DVertex(vx,vy1,-0.5, 1,0,0, c, tx,ty1),
216 video::S3DVertex(vx,vy1,+0.5, 1,0,0, c, tx,ty1),
217 video::S3DVertex(vx,vy2,+0.5, 1,0,0, c, tx,ty2),
218 video::S3DVertex(vx,vy2,-0.5, 1,0,0, c, tx,ty2),
220 u16 indices[6] = {0,1,2,2,3,0};
221 buf->append(vertices, 4, indices, 6);
224 if (!scancol[y * wstep] && scancol[y * wstep + 1])
227 while (!scancol[yy * wstep] && scancol[yy * wstep + 1])
229 f32 vx = (x + eps) / (f32) twidth - 0.5;
230 f32 vy1 = 0.5 - (y - eps) / (f32) theight;
231 f32 vy2 = 0.5 - (yy + eps) / (f32) theight;
232 f32 tx = (x + 0.5) / (f32) twidth;
233 f32 ty1 = y / (f32) theight;
234 f32 ty2 = yy / (f32) theight;
235 video::S3DVertex vertices[8] =
237 video::S3DVertex(vx,vy1,-0.5, -1,0,0, c, tx,ty1),
238 video::S3DVertex(vx,vy2,-0.5, -1,0,0, c, tx,ty2),
239 video::S3DVertex(vx,vy2,+0.5, -1,0,0, c, tx,ty2),
240 video::S3DVertex(vx,vy1,+0.5, -1,0,0, c, tx,ty1),
242 u16 indices[6] = {0,1,2,2,3,0};
243 buf->append(vertices, 4, indices, 6);
252 scene::SMesh *mesh = new scene::SMesh();
253 mesh->addMeshBuffer(buf);
255 scene::SAnimatedMesh *anim_mesh = new scene::SAnimatedMesh(mesh);
260 scene::IAnimatedMesh* createExtrudedMesh(video::ITexture *texture,
261 video::IVideoDriver *driver, v3f scale)
263 scene::IAnimatedMesh *mesh = NULL;
264 core::dimension2d<u32> size = texture->getSize();
265 video::ECOLOR_FORMAT format = texture->getColorFormat();
266 if (format == video::ECF_A8R8G8B8)
268 // Texture is in the correct color format, we can pass it
269 // to extrudeARGB right away.
270 void *data = texture->lock(MY_ETLM_READ_ONLY);
273 mesh = extrudeARGB(size.Width, size.Height, (u8*) data);
278 video::IImage *img1 = driver->createImageFromData(format, size, texture->lock(MY_ETLM_READ_ONLY));
282 // img1 is in the texture's color format, convert to 8-bit ARGB
283 video::IImage *img2 = driver->createImage(video::ECF_A8R8G8B8, size);
289 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 video::ITexture *generateTextureFromMesh(scene::IMesh *mesh,
413 IrrlichtDevice *device,
414 core::dimension2d<u32> dim,
415 std::string texture_name,
418 core::CMatrix4<f32> camera_projection_matrix,
419 video::SColorf ambient_light,
421 video::SColorf light_color,
424 video::IVideoDriver *driver = device->getVideoDriver();
425 if(driver->queryFeature(video::EVDF_RENDER_TO_TARGET) == false)
427 static bool warned = false;
430 errorstream<<"generateTextureFromMesh(): EVDF_RENDER_TO_TARGET"
431 " not supported."<<std::endl;
437 // Create render target texture
438 video::ITexture *rtt = driver->addRenderTargetTexture(
439 dim, texture_name.c_str(), video::ECF_A8R8G8B8);
442 errorstream<<"generateTextureFromMesh(): addRenderTargetTexture"
443 " returned NULL."<<std::endl;
448 driver->setRenderTarget(rtt, false, true, video::SColor(0,0,0,0));
450 // Get a scene manager
451 scene::ISceneManager *smgr_main = device->getSceneManager();
453 scene::ISceneManager *smgr = smgr_main->createNewSceneManager();
456 scene::IMeshSceneNode* meshnode = smgr->addMeshSceneNode(mesh, NULL, -1, v3f(0,0,0), v3f(0,0,0), v3f(1,1,1), true);
457 meshnode->setMaterialFlag(video::EMF_LIGHTING, true);
458 meshnode->setMaterialFlag(video::EMF_ANTI_ALIASING, true);
459 meshnode->setMaterialFlag(video::EMF_BILINEAR_FILTER, true);
461 scene::ICameraSceneNode* camera = smgr->addCameraSceneNode(0,
462 camera_position, camera_lookat);
463 // second parameter of setProjectionMatrix (isOrthogonal) is ignored
464 camera->setProjectionMatrix(camera_projection_matrix, false);
466 smgr->setAmbientLight(ambient_light);
467 smgr->addLightSceneNode(0, light_position, light_color, light_radius);
470 driver->beginScene(true, true, video::SColor(0,0,0,0));
474 // NOTE: The scene nodes should not be dropped, otherwise
475 // smgr->drop() segfaults
479 // Drop scene manager
482 // Unset render target
483 driver->setRenderTarget(0, false, true, 0);