14a194b8822943c4ccac799672db5cf353ecbc47
[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
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
31 #else
32 #define MY_ETLM_READ_ONLY video::ETLM_READ_ONLY
33 #endif
34
35 scene::IAnimatedMesh* createCubeMesh(v3f scale)
36 {
37         video::SColor c(255,255,255,255);
38         video::S3DVertex vertices[24] =
39         {
40                 // Up
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),
45                 // Down
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),
50                 // Right
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),
55                 // Left
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),
60                 // Back
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),
65                 // Front
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),
70         };
71
72         u16 indices[6] = {0,1,2,2,3,0};
73
74         scene::SMesh *mesh = new scene::SMesh();
75         for (u32 i=0; i<6; ++i)
76         {
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);
85                 buf->drop();
86         }
87
88         scene::SAnimatedMesh *anim_mesh = new scene::SAnimatedMesh(mesh);
89         mesh->drop();
90         scaleMesh(anim_mesh, scale);  // also recalculates bounding box
91         return anim_mesh;
92 }
93
94 static scene::IAnimatedMesh* extrudeARGB(u32 twidth, u32 theight, u8 *data)
95 {
96         const s32 argb_wstep = 4 * twidth;
97         const s32 alpha_threshold = 1;
98
99         scene::IMeshBuffer *buf = new scene::SMeshBuffer();
100         video::SColor c(255,255,255,255);
101
102         // Front and back
103         {
104                 video::S3DVertex vertices[8] =
105                 {
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),
114                 };
115                 u16 indices[12] = {0,1,2,2,3,0,4,5,6,6,7,4};
116                 buf->append(vertices, 8, indices, 12);
117         }
118
119         // "Interior"
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)
124         {
125                 u8 *scanline = solidity + y * wstep;
126                 if (y == 0 || y == theight + 1)
127                 {
128                         for (u32 x = 0; x < twidth + 2; ++x)
129                                 scanline[x] = 0;
130                 }
131                 else
132                 {
133                         scanline[0] = 0;
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;
138                 }
139         }
140
141         // without this, there would be occasional "holes" in the mesh
142         f32 eps = 0.01;
143
144         for (u32 y = 0; y <= theight; ++y)
145         {
146                 u8 *scanline = solidity + y * wstep + 1;
147                 for (u32 x = 0; x <= twidth; ++x)
148                 {
149                         if (scanline[x] && !scanline[x + wstep])
150                         {
151                                 u32 xx = x + 1;
152                                 while (scanline[xx] && !scanline[xx + wstep])
153                                         ++xx;
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] =
161                                 {
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),
166                                 };
167                                 u16 indices[6] = {0,1,2,2,3,0};
168                                 buf->append(vertices, 4, indices, 6);
169                                 x = xx - 1;
170                         }
171                         if (!scanline[x] && scanline[x + wstep])
172                         {
173                                 u32 xx = x + 1;
174                                 while (!scanline[xx] && scanline[xx + wstep])
175                                         ++xx;
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] =
183                                 {
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),
188                                 };
189                                 u16 indices[6] = {0,1,2,2,3,0};
190                                 buf->append(vertices, 4, indices, 6);
191                                 x = xx - 1;
192                         }
193                 }
194         }
195
196         for (u32 x = 0; x <= twidth; ++x)
197         {
198                 u8 *scancol = solidity + x + wstep;
199                 for (u32 y = 0; y <= theight; ++y)
200                 {
201                         if (scancol[y * wstep] && !scancol[y * wstep + 1])
202                         {
203                                 u32 yy = y + 1;
204                                 while (scancol[yy * wstep] && !scancol[yy * wstep + 1])
205                                         ++yy;
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] =
213                                 {
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),
218                                 };
219                                 u16 indices[6] = {0,1,2,2,3,0};
220                                 buf->append(vertices, 4, indices, 6);
221                                 y = yy - 1;
222                         }
223                         if (!scancol[y * wstep] && scancol[y * wstep + 1])
224                         {
225                                 u32 yy = y + 1;
226                                 while (!scancol[yy * wstep] && scancol[yy * wstep + 1])
227                                         ++yy;
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] =
235                                 {
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),
240                                 };
241                                 u16 indices[6] = {0,1,2,2,3,0};
242                                 buf->append(vertices, 4, indices, 6);
243                                 y = yy - 1;
244                         }
245                 }
246         }
247
248         delete[] solidity;
249
250         // Add to mesh
251         scene::SMesh *mesh = new scene::SMesh();
252         mesh->addMeshBuffer(buf);
253         buf->drop();
254         scene::SAnimatedMesh *anim_mesh = new scene::SAnimatedMesh(mesh);
255         mesh->drop();
256         return anim_mesh;
257 }
258
259 scene::IAnimatedMesh* createExtrudedMesh(video::ITexture *texture,
260                 video::IVideoDriver *driver, v3f scale)
261 {
262         scene::IAnimatedMesh *mesh = NULL;
263         core::dimension2d<u32> size = texture->getSize();
264         video::ECOLOR_FORMAT format = texture->getColorFormat();
265         if (format == video::ECF_A8R8G8B8)
266         {
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);
270                 if (data == NULL)
271                         return NULL;
272                 mesh = extrudeARGB(size.Width, size.Height, (u8*) data);
273                 texture->unlock();
274         }
275         else
276         {
277                 video::IImage *img1 = driver->createImageFromData(format, size, texture->lock(MY_ETLM_READ_ONLY));
278                 if (img1 == NULL)
279                         return NULL;
280
281                 // img1 is in the texture's color format, convert to 8-bit ARGB
282                 video::IImage *img2 = driver->createImage(video::ECF_A8R8G8B8, size);
283                 if (img2 == NULL)
284                 {
285                         img1->drop();
286                         return NULL;
287                 }
288
289                 img1->copyTo(img2);
290                 img1->drop();
291                 mesh = extrudeARGB(size.Width, size.Height, (u8*) img2->lock());
292                 img2->unlock();
293                 img2->drop();
294         }
295
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;
301
302         scaleMesh(mesh, scale);  // also recalculates bounding box
303         return mesh;
304 }
305
306 void scaleMesh(scene::IMesh *mesh, v3f scale)
307 {
308         if(mesh == NULL)
309                 return;
310
311         core::aabbox3d<f32> bbox;
312         bbox.reset(0,0,0);
313
314         u16 mc = mesh->getMeshBufferCount();
315         for(u16 j=0; j<mc; j++)
316         {
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++)
321                 {
322                         vertices[i].Pos *= scale;
323                 }
324                 buf->recalculateBoundingBox();
325
326                 // calculate total bounding box
327                 if(j == 0)
328                         bbox = buf->getBoundingBox();
329                 else
330                         bbox.addInternalBox(buf->getBoundingBox());
331         }
332         mesh->setBoundingBox(bbox);
333 }
334
335 void translateMesh(scene::IMesh *mesh, v3f vec)
336 {
337         if(mesh == NULL)
338                 return;
339
340         core::aabbox3d<f32> bbox;
341         bbox.reset(0,0,0);
342
343         u16 mc = mesh->getMeshBufferCount();
344         for(u16 j=0; j<mc; j++)
345         {
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++)
350                 {
351                         vertices[i].Pos += vec;
352                 }
353                 buf->recalculateBoundingBox();
354
355                 // calculate total bounding box
356                 if(j == 0)
357                         bbox = buf->getBoundingBox();
358                 else
359                         bbox.addInternalBox(buf->getBoundingBox());
360         }
361         mesh->setBoundingBox(bbox);
362 }
363
364 void setMeshColor(scene::IMesh *mesh, const video::SColor &color)
365 {
366         if(mesh == NULL)
367                 return;
368         
369         u16 mc = mesh->getMeshBufferCount();
370         for(u16 j=0; j<mc; j++)
371         {
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++)
376                 {
377                         vertices[i].Color = color;
378                 }
379         }
380 }
381
382 void setMeshColorByNormalXYZ(scene::IMesh *mesh,
383                 const video::SColor &colorX,
384                 const video::SColor &colorY,
385                 const video::SColor &colorZ)
386 {
387         if(mesh == NULL)
388                 return;
389         
390         u16 mc = mesh->getMeshBufferCount();
391         for(u16 j=0; j<mc; j++)
392         {
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++)
397                 {
398                         f32 x = fabs(vertices[i].Normal.X);
399                         f32 y = fabs(vertices[i].Normal.Y);
400                         f32 z = fabs(vertices[i].Normal.Z);
401                         if(x >= y && x >= z)
402                                 vertices[i].Color = colorX;
403                         else if(y >= z)
404                                 vertices[i].Color = colorY;
405                         else
406                                 vertices[i].Color = colorZ;
407
408                 }
409         }
410 }