]> git.lizzy.rs Git - minetest.git/blob - src/mesh.cpp
Change debug optimization level back to -O1
[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->copyTo(img2);
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 }