Re-add jungles, apple trees
[oweals/minetest.git] / src / mesh.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "mesh.h"
21 #include "log.h"
22 #include <cassert>
23 #include <iostream>
24 #include <IAnimatedMesh.h>
25 #include <SAnimatedMesh.h>
26 #include <ICameraSceneNode.h>
27
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
32 #else
33 #define MY_ETLM_READ_ONLY video::ETLM_READ_ONLY
34 #endif
35
36 scene::IAnimatedMesh* createCubeMesh(v3f scale)
37 {
38         video::SColor c(255,255,255,255);
39         video::S3DVertex vertices[24] =
40         {
41                 // Up
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),
46                 // Down
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),
51                 // Right
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),
56                 // Left
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),
61                 // Back
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),
66                 // Front
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),
71         };
72
73         u16 indices[6] = {0,1,2,2,3,0};
74
75         scene::SMesh *mesh = new scene::SMesh();
76         for (u32 i=0; i<6; ++i)
77         {
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);
86                 buf->drop();
87         }
88
89         scene::SAnimatedMesh *anim_mesh = new scene::SAnimatedMesh(mesh);
90         mesh->drop();
91         scaleMesh(anim_mesh, scale);  // also recalculates bounding box
92         return anim_mesh;
93 }
94
95 static scene::IAnimatedMesh* extrudeARGB(u32 twidth, u32 theight, u8 *data)
96 {
97         const s32 argb_wstep = 4 * twidth;
98         const s32 alpha_threshold = 1;
99
100         scene::IMeshBuffer *buf = new scene::SMeshBuffer();
101         video::SColor c(255,255,255,255);
102
103         // Front and back
104         {
105                 video::S3DVertex vertices[8] =
106                 {
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),
115                 };
116                 u16 indices[12] = {0,1,2,2,3,0,4,5,6,6,7,4};
117                 buf->append(vertices, 8, indices, 12);
118         }
119
120         // "Interior"
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)
125         {
126                 u8 *scanline = solidity + y * wstep;
127                 if (y == 0 || y == theight + 1)
128                 {
129                         for (u32 x = 0; x < twidth + 2; ++x)
130                                 scanline[x] = 0;
131                 }
132                 else
133                 {
134                         scanline[0] = 0;
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;
139                 }
140         }
141
142         // without this, there would be occasional "holes" in the mesh
143         f32 eps = 0.01;
144
145         for (u32 y = 0; y <= theight; ++y)
146         {
147                 u8 *scanline = solidity + y * wstep + 1;
148                 for (u32 x = 0; x <= twidth; ++x)
149                 {
150                         if (scanline[x] && !scanline[x + wstep])
151                         {
152                                 u32 xx = x + 1;
153                                 while (scanline[xx] && !scanline[xx + wstep])
154                                         ++xx;
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] =
162                                 {
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),
167                                 };
168                                 u16 indices[6] = {0,1,2,2,3,0};
169                                 buf->append(vertices, 4, indices, 6);
170                                 x = xx - 1;
171                         }
172                         if (!scanline[x] && scanline[x + wstep])
173                         {
174                                 u32 xx = x + 1;
175                                 while (!scanline[xx] && scanline[xx + wstep])
176                                         ++xx;
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] =
184                                 {
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),
189                                 };
190                                 u16 indices[6] = {0,1,2,2,3,0};
191                                 buf->append(vertices, 4, indices, 6);
192                                 x = xx - 1;
193                         }
194                 }
195         }
196
197         for (u32 x = 0; x <= twidth; ++x)
198         {
199                 u8 *scancol = solidity + x + wstep;
200                 for (u32 y = 0; y <= theight; ++y)
201                 {
202                         if (scancol[y * wstep] && !scancol[y * wstep + 1])
203                         {
204                                 u32 yy = y + 1;
205                                 while (scancol[yy * wstep] && !scancol[yy * wstep + 1])
206                                         ++yy;
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] =
214                                 {
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),
219                                 };
220                                 u16 indices[6] = {0,1,2,2,3,0};
221                                 buf->append(vertices, 4, indices, 6);
222                                 y = yy - 1;
223                         }
224                         if (!scancol[y * wstep] && scancol[y * wstep + 1])
225                         {
226                                 u32 yy = y + 1;
227                                 while (!scancol[yy * wstep] && scancol[yy * wstep + 1])
228                                         ++yy;
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] =
236                                 {
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),
241                                 };
242                                 u16 indices[6] = {0,1,2,2,3,0};
243                                 buf->append(vertices, 4, indices, 6);
244                                 y = yy - 1;
245                         }
246                 }
247         }
248
249         // Add to mesh
250         scene::SMesh *mesh = new scene::SMesh();
251         mesh->addMeshBuffer(buf);
252         buf->drop();
253         scene::SAnimatedMesh *anim_mesh = new scene::SAnimatedMesh(mesh);
254         mesh->drop();
255         return anim_mesh;
256 }
257
258 scene::IAnimatedMesh* createExtrudedMesh(video::ITexture *texture,
259                 video::IVideoDriver *driver, v3f scale)
260 {
261         scene::IAnimatedMesh *mesh = NULL;
262         core::dimension2d<u32> size = texture->getSize();
263         video::ECOLOR_FORMAT format = texture->getColorFormat();
264         if (format == video::ECF_A8R8G8B8)
265         {
266                 // Texture is in the correct color format, we can pass it
267                 // to extrudeARGB right away.
268                 void *data = texture->lock(MY_ETLM_READ_ONLY);
269                 if (data == NULL)
270                         return NULL;
271                 mesh = extrudeARGB(size.Width, size.Height, (u8*) data);
272                 texture->unlock();
273         }
274         else
275         {
276                 video::IImage *img1 = driver->createImageFromData(format, size, texture->lock(MY_ETLM_READ_ONLY));
277                 if (img1 == NULL)
278                         return NULL;
279
280                 // img1 is in the texture's color format, convert to 8-bit ARGB
281                 video::IImage *img2 = driver->createImage(video::ECF_A8R8G8B8, size);
282                 if (img2 != NULL)
283                 {
284                         img1->copyTo(img2);
285                         img1->drop();
286
287                         mesh = extrudeARGB(size.Width, size.Height, (u8*) img2->lock());
288                         img2->unlock();
289                         img2->drop();
290                 }
291                 img1->drop();
292         }
293
294         // Set default material
295         mesh->getMeshBuffer(0)->getMaterial().setTexture(0, texture);
296         mesh->getMeshBuffer(0)->getMaterial().setFlag(video::EMF_LIGHTING, false);
297         mesh->getMeshBuffer(0)->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
298         mesh->getMeshBuffer(0)->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
299
300         scaleMesh(mesh, scale);  // also recalculates bounding box
301         return mesh;
302 }
303
304 void scaleMesh(scene::IMesh *mesh, v3f scale)
305 {
306         if(mesh == NULL)
307                 return;
308
309         core::aabbox3d<f32> bbox;
310         bbox.reset(0,0,0);
311
312         u16 mc = mesh->getMeshBufferCount();
313         for(u16 j=0; j<mc; j++)
314         {
315                 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
316                 video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
317                 u16 vc = buf->getVertexCount();
318                 for(u16 i=0; i<vc; i++)
319                 {
320                         vertices[i].Pos *= scale;
321                 }
322                 buf->recalculateBoundingBox();
323
324                 // calculate total bounding box
325                 if(j == 0)
326                         bbox = buf->getBoundingBox();
327                 else
328                         bbox.addInternalBox(buf->getBoundingBox());
329         }
330         mesh->setBoundingBox(bbox);
331 }
332
333 void translateMesh(scene::IMesh *mesh, v3f vec)
334 {
335         if(mesh == NULL)
336                 return;
337
338         core::aabbox3d<f32> bbox;
339         bbox.reset(0,0,0);
340
341         u16 mc = mesh->getMeshBufferCount();
342         for(u16 j=0; j<mc; j++)
343         {
344                 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
345                 video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
346                 u16 vc = buf->getVertexCount();
347                 for(u16 i=0; i<vc; i++)
348                 {
349                         vertices[i].Pos += vec;
350                 }
351                 buf->recalculateBoundingBox();
352
353                 // calculate total bounding box
354                 if(j == 0)
355                         bbox = buf->getBoundingBox();
356                 else
357                         bbox.addInternalBox(buf->getBoundingBox());
358         }
359         mesh->setBoundingBox(bbox);
360 }
361
362 void setMeshColor(scene::IMesh *mesh, const video::SColor &color)
363 {
364         if(mesh == NULL)
365                 return;
366         
367         u16 mc = mesh->getMeshBufferCount();
368         for(u16 j=0; j<mc; j++)
369         {
370                 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
371                 video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
372                 u16 vc = buf->getVertexCount();
373                 for(u16 i=0; i<vc; i++)
374                 {
375                         vertices[i].Color = color;
376                 }
377         }
378 }
379
380 void setMeshColorByNormalXYZ(scene::IMesh *mesh,
381                 const video::SColor &colorX,
382                 const video::SColor &colorY,
383                 const video::SColor &colorZ)
384 {
385         if(mesh == NULL)
386                 return;
387         
388         u16 mc = mesh->getMeshBufferCount();
389         for(u16 j=0; j<mc; j++)
390         {
391                 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
392                 video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
393                 u16 vc = buf->getVertexCount();
394                 for(u16 i=0; i<vc; i++)
395                 {
396                         f32 x = fabs(vertices[i].Normal.X);
397                         f32 y = fabs(vertices[i].Normal.Y);
398                         f32 z = fabs(vertices[i].Normal.Z);
399                         if(x >= y && x >= z)
400                                 vertices[i].Color = colorX;
401                         else if(y >= z)
402                                 vertices[i].Color = colorY;
403                         else
404                                 vertices[i].Color = colorZ;
405
406                 }
407         }
408 }
409
410 video::ITexture *generateTextureFromMesh(scene::IMesh *mesh,
411                 IrrlichtDevice *device,
412                 core::dimension2d<u32> dim,
413                 std::string texture_name,
414                 v3f camera_position,
415                 v3f camera_lookat,
416                 core::CMatrix4<f32> camera_projection_matrix,
417                 video::SColorf ambient_light,
418                 v3f light_position,
419                 video::SColorf light_color,
420                 f32 light_radius)
421 {
422         video::IVideoDriver *driver = device->getVideoDriver();
423         if(driver->queryFeature(video::EVDF_RENDER_TO_TARGET) == false)
424         {
425                 static bool warned = false;
426                 if(!warned)
427                 {
428                         errorstream<<"generateTextureFromMesh(): EVDF_RENDER_TO_TARGET"
429                                         " not supported."<<std::endl;
430                         warned = true;
431                 }
432                 return NULL;
433         }
434
435         // Create render target texture
436         video::ITexture *rtt = driver->addRenderTargetTexture(
437                         dim, texture_name.c_str(), video::ECF_A8R8G8B8);
438         if(rtt == NULL)
439         {
440                 errorstream<<"generateTextureFromMesh(): addRenderTargetTexture"
441                                 " returned NULL."<<std::endl;
442                 return NULL;
443         }
444
445         // Set render target
446         driver->setRenderTarget(rtt, false, true, video::SColor(0,0,0,0));
447
448         // Get a scene manager
449         scene::ISceneManager *smgr_main = device->getSceneManager();
450         assert(smgr_main);
451         scene::ISceneManager *smgr = smgr_main->createNewSceneManager();
452         assert(smgr);
453
454         scene::IMeshSceneNode* meshnode = smgr->addMeshSceneNode(mesh, NULL, -1, v3f(0,0,0), v3f(0,0,0), v3f(1,1,1), true);
455         meshnode->setMaterialFlag(video::EMF_LIGHTING, true);
456         meshnode->setMaterialFlag(video::EMF_ANTI_ALIASING, true);
457         meshnode->setMaterialFlag(video::EMF_BILINEAR_FILTER, true);
458
459         scene::ICameraSceneNode* camera = smgr->addCameraSceneNode(0,
460                         camera_position, camera_lookat);
461         // second parameter of setProjectionMatrix (isOrthogonal) is ignored
462         camera->setProjectionMatrix(camera_projection_matrix, false);
463
464         smgr->setAmbientLight(ambient_light);
465         smgr->addLightSceneNode(0, light_position, light_color, light_radius);
466
467         // Render scene
468         driver->beginScene(true, true, video::SColor(0,0,0,0));
469         smgr->drawAll();
470         driver->endScene();
471
472         // NOTE: The scene nodes should not be dropped, otherwise
473         //       smgr->drop() segfaults
474         /*cube->drop();
475         camera->drop();
476         light->drop();*/
477         // Drop scene manager
478         smgr->drop();
479
480         // Unset render target
481         driver->setRenderTarget(0, false, true, 0);
482
483         return rtt;
484 }