]> git.lizzy.rs Git - dragonfireclient.git/blob - src/content_mapblock.cpp
Nodebox: Allow nodeboxes to "connect"
[dragonfireclient.git] / src / content_mapblock.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 "content_mapblock.h"
21 #include "util/numeric.h"
22 #include "util/directiontables.h"
23 #include "mapblock_mesh.h" // For MapBlock_LightColor() and MeshCollector
24 #include "settings.h"
25 #include "nodedef.h"
26 #include "client/tile.h"
27 #include "mesh.h"
28 #include <IMeshManipulator.h>
29 #include "gamedef.h"
30 #include "log.h"
31
32
33 // Create a cuboid.
34 //  collector - the MeshCollector for the resulting polygons
35 //  box       - the position and size of the box
36 //  tiles     - the tiles (materials) to use (for all 6 faces)
37 //  tilecount - number of entries in tiles, 1<=tilecount<=6
38 //  c         - vertex colour - used for all
39 //  txc       - texture coordinates - this is a list of texture coordinates
40 //              for the opposite corners of each face - therefore, there
41 //              should be (2+2)*6=24 values in the list. Alternatively, pass
42 //              NULL to use the entire texture for each face. The order of
43 //              the faces in the list is up-down-right-left-back-front
44 //              (compatible with ContentFeatures). If you specified 0,0,1,1
45 //              for each face, that would be the same as passing NULL.
46 void makeCuboid(MeshCollector *collector, const aabb3f &box,
47         TileSpec *tiles, int tilecount, video::SColor &c, const f32* txc)
48 {
49         assert(tilecount >= 1 && tilecount <= 6); // pre-condition
50
51         v3f min = box.MinEdge;
52         v3f max = box.MaxEdge;
53
54
55
56         if(txc == NULL) {
57                 static const f32 txc_default[24] = {
58                         0,0,1,1,
59                         0,0,1,1,
60                         0,0,1,1,
61                         0,0,1,1,
62                         0,0,1,1,
63                         0,0,1,1
64                 };
65                 txc = txc_default;
66         }
67
68         video::S3DVertex vertices[24] =
69         {
70                 // up
71                 video::S3DVertex(min.X,max.Y,max.Z, 0,1,0, c, txc[0],txc[1]),
72                 video::S3DVertex(max.X,max.Y,max.Z, 0,1,0, c, txc[2],txc[1]),
73                 video::S3DVertex(max.X,max.Y,min.Z, 0,1,0, c, txc[2],txc[3]),
74                 video::S3DVertex(min.X,max.Y,min.Z, 0,1,0, c, txc[0],txc[3]),
75                 // down
76                 video::S3DVertex(min.X,min.Y,min.Z, 0,-1,0, c, txc[4],txc[5]),
77                 video::S3DVertex(max.X,min.Y,min.Z, 0,-1,0, c, txc[6],txc[5]),
78                 video::S3DVertex(max.X,min.Y,max.Z, 0,-1,0, c, txc[6],txc[7]),
79                 video::S3DVertex(min.X,min.Y,max.Z, 0,-1,0, c, txc[4],txc[7]),
80                 // right
81                 video::S3DVertex(max.X,max.Y,min.Z, 1,0,0, c, txc[ 8],txc[9]),
82                 video::S3DVertex(max.X,max.Y,max.Z, 1,0,0, c, txc[10],txc[9]),
83                 video::S3DVertex(max.X,min.Y,max.Z, 1,0,0, c, txc[10],txc[11]),
84                 video::S3DVertex(max.X,min.Y,min.Z, 1,0,0, c, txc[ 8],txc[11]),
85                 // left
86                 video::S3DVertex(min.X,max.Y,max.Z, -1,0,0, c, txc[12],txc[13]),
87                 video::S3DVertex(min.X,max.Y,min.Z, -1,0,0, c, txc[14],txc[13]),
88                 video::S3DVertex(min.X,min.Y,min.Z, -1,0,0, c, txc[14],txc[15]),
89                 video::S3DVertex(min.X,min.Y,max.Z, -1,0,0, c, txc[12],txc[15]),
90                 // back
91                 video::S3DVertex(max.X,max.Y,max.Z, 0,0,1, c, txc[16],txc[17]),
92                 video::S3DVertex(min.X,max.Y,max.Z, 0,0,1, c, txc[18],txc[17]),
93                 video::S3DVertex(min.X,min.Y,max.Z, 0,0,1, c, txc[18],txc[19]),
94                 video::S3DVertex(max.X,min.Y,max.Z, 0,0,1, c, txc[16],txc[19]),
95                 // front
96                 video::S3DVertex(min.X,max.Y,min.Z, 0,0,-1, c, txc[20],txc[21]),
97                 video::S3DVertex(max.X,max.Y,min.Z, 0,0,-1, c, txc[22],txc[21]),
98                 video::S3DVertex(max.X,min.Y,min.Z, 0,0,-1, c, txc[22],txc[23]),
99                 video::S3DVertex(min.X,min.Y,min.Z, 0,0,-1, c, txc[20],txc[23]),
100         };
101
102         for(int i = 0; i < 6; i++)
103                                 {
104                                 switch (tiles[MYMIN(i, tilecount-1)].rotation)
105                                 {
106                                 case 0:
107                                         break;
108                                 case 1: //R90
109                                         for (int x = 0; x < 4; x++)
110                                                 vertices[i*4+x].TCoords.rotateBy(90,irr::core::vector2df(0, 0));
111                                         break;
112                                 case 2: //R180
113                                         for (int x = 0; x < 4; x++)
114                                                 vertices[i*4+x].TCoords.rotateBy(180,irr::core::vector2df(0, 0));
115                                         break;
116                                 case 3: //R270
117                                         for (int x = 0; x < 4; x++)
118                                                 vertices[i*4+x].TCoords.rotateBy(270,irr::core::vector2df(0, 0));
119                                         break;
120                                 case 4: //FXR90
121                                         for (int x = 0; x < 4; x++){
122                                                 vertices[i*4+x].TCoords.X = 1.0 - vertices[i*4+x].TCoords.X;
123                                                 vertices[i*4+x].TCoords.rotateBy(90,irr::core::vector2df(0, 0));
124                                         }
125                                         break;
126                                 case 5: //FXR270
127                                         for (int x = 0; x < 4; x++){
128                                                 vertices[i*4+x].TCoords.X = 1.0 - vertices[i*4+x].TCoords.X;
129                                                 vertices[i*4+x].TCoords.rotateBy(270,irr::core::vector2df(0, 0));
130                                         }
131                                         break;
132                                 case 6: //FYR90
133                                         for (int x = 0; x < 4; x++){
134                                                 vertices[i*4+x].TCoords.Y = 1.0 - vertices[i*4+x].TCoords.Y;
135                                                 vertices[i*4+x].TCoords.rotateBy(90,irr::core::vector2df(0, 0));
136                                         }
137                                         break;
138                                 case 7: //FYR270
139                                         for (int x = 0; x < 4; x++){
140                                                 vertices[i*4+x].TCoords.Y = 1.0 - vertices[i*4+x].TCoords.Y;
141                                                 vertices[i*4+x].TCoords.rotateBy(270,irr::core::vector2df(0, 0));
142                                         }
143                                         break;
144                                 case 8: //FX
145                                         for (int x = 0; x < 4; x++){
146                                                 vertices[i*4+x].TCoords.X = 1.0 - vertices[i*4+x].TCoords.X;
147                                         }
148                                         break;
149                                 case 9: //FY
150                                         for (int x = 0; x < 4; x++){
151                                                 vertices[i*4+x].TCoords.Y = 1.0 - vertices[i*4+x].TCoords.Y;
152                                         }
153                                         break;
154                                 default:
155                                         break;
156                                 }
157                         }
158         u16 indices[] = {0,1,2,2,3,0};
159         // Add to mesh collector
160         for (s32 j = 0; j < 24; j += 4) {
161                 int tileindex = MYMIN(j / 4, tilecount - 1);
162                 collector->append(tiles[tileindex], vertices + j, 4, indices, 6);
163         }
164 }
165
166 static inline void getNeighborConnectingFace(v3s16 p, INodeDefManager *nodedef,
167                 MeshMakeData *data, MapNode n, int v, int *neighbors)
168 {
169         MapNode n2 = data->m_vmanip.getNodeNoEx(p);
170         if (nodedef->nodeboxConnects(n, n2))
171                 *neighbors |= v;
172 }
173
174 /*
175         TODO: Fix alpha blending for special nodes
176         Currently only the last element rendered is blended correct
177 */
178 void mapblock_mesh_generate_special(MeshMakeData *data,
179                 MeshCollector &collector)
180 {
181         INodeDefManager *nodedef = data->m_gamedef->ndef();
182         scene::ISceneManager* smgr = data->m_gamedef->getSceneManager();
183         scene::IMeshManipulator* meshmanip = smgr->getMeshManipulator();
184
185         // 0ms
186         //TimeTaker timer("mapblock_mesh_generate_special()");
187
188         /*
189                 Some settings
190         */
191         bool enable_mesh_cache  = g_settings->getBool("enable_mesh_cache");
192
193         v3s16 blockpos_nodes = data->m_blockpos*MAP_BLOCKSIZE;
194
195         for(s16 z = 0; z < MAP_BLOCKSIZE; z++)
196         for(s16 y = 0; y < MAP_BLOCKSIZE; y++)
197         for(s16 x = 0; x < MAP_BLOCKSIZE; x++)
198         {
199                 v3s16 p(x,y,z);
200
201                 MapNode n = data->m_vmanip.getNodeNoEx(blockpos_nodes + p);
202                 const ContentFeatures &f = nodedef->get(n);
203
204                 // Only solidness=0 stuff is drawn here
205                 if(f.solidness != 0)
206                         continue;
207
208                 switch(f.drawtype){
209                 default:
210                         infostream << "Got " << f.drawtype << std::endl;
211                         FATAL_ERROR("Unknown drawtype");
212                         break;
213                 case NDT_AIRLIKE:
214                         break;
215                 case NDT_LIQUID:
216                 {
217                         /*
218                                 Add water sources to mesh if using new style
219                         */
220                         TileSpec tile_liquid = f.special_tiles[0];
221                         TileSpec tile_liquid_bfculled = getNodeTile(n, p, v3s16(0,0,0), data);
222
223                         bool top_is_same_liquid = false;
224                         MapNode ntop = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y+1,z));
225                         content_t c_flowing = nodedef->getId(f.liquid_alternative_flowing);
226                         content_t c_source = nodedef->getId(f.liquid_alternative_source);
227                         if(ntop.getContent() == c_flowing || ntop.getContent() == c_source)
228                                 top_is_same_liquid = true;
229
230                         u16 l = getInteriorLight(n, 0, nodedef);
231                         video::SColor c = MapBlock_LightColor(f.alpha, l, f.light_source);
232
233                         /*
234                                 Generate sides
235                          */
236                         v3s16 side_dirs[4] = {
237                                 v3s16(1,0,0),
238                                 v3s16(-1,0,0),
239                                 v3s16(0,0,1),
240                                 v3s16(0,0,-1),
241                         };
242                         for(u32 i=0; i<4; i++)
243                         {
244                                 v3s16 dir = side_dirs[i];
245
246                                 MapNode neighbor = data->m_vmanip.getNodeNoEx(blockpos_nodes + p + dir);
247                                 content_t neighbor_content = neighbor.getContent();
248                                 const ContentFeatures &n_feat = nodedef->get(neighbor_content);
249                                 MapNode n_top = data->m_vmanip.getNodeNoEx(blockpos_nodes + p + dir+ v3s16(0,1,0));
250                                 content_t n_top_c = n_top.getContent();
251
252                                 if(neighbor_content == CONTENT_IGNORE)
253                                         continue;
254
255                                 /*
256                                         If our topside is liquid and neighbor's topside
257                                         is liquid, don't draw side face
258                                 */
259                                 if(top_is_same_liquid && (n_top_c == c_flowing ||
260                                                 n_top_c == c_source || n_top_c == CONTENT_IGNORE))
261                                         continue;
262
263                                 // Don't draw face if neighbor is blocking the view
264                                 if(n_feat.solidness == 2)
265                                         continue;
266
267                                 bool neighbor_is_same_liquid = (neighbor_content == c_source
268                                                 || neighbor_content == c_flowing);
269
270                                 // Don't draw any faces if neighbor same is liquid and top is
271                                 // same liquid
272                                 if(neighbor_is_same_liquid && !top_is_same_liquid)
273                                         continue;
274
275                                 // Use backface culled material if neighbor doesn't have a
276                                 // solidness of 0
277                                 const TileSpec *current_tile = &tile_liquid;
278                                 if(n_feat.solidness != 0 || n_feat.visual_solidness != 0)
279                                         current_tile = &tile_liquid_bfculled;
280
281                                 video::S3DVertex vertices[4] =
282                                 {
283                                         video::S3DVertex(-BS/2,0,BS/2,0,0,0, c, 0,1),
284                                         video::S3DVertex(BS/2,0,BS/2,0,0,0, c, 1,1),
285                                         video::S3DVertex(BS/2,0,BS/2, 0,0,0, c, 1,0),
286                                         video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c, 0,0),
287                                 };
288
289                                 /*
290                                         If our topside is liquid, set upper border of face
291                                         at upper border of node
292                                 */
293                                 if (top_is_same_liquid) {
294                                         vertices[2].Pos.Y = 0.5 * BS;
295                                         vertices[3].Pos.Y = 0.5 * BS;
296                                 } else {
297                                 /*
298                                         Otherwise upper position of face is liquid level
299                                 */
300                                         vertices[2].Pos.Y = 0.5 * BS;
301                                         vertices[3].Pos.Y = 0.5 * BS;
302                                 }
303                                 /*
304                                         If neighbor is liquid, lower border of face is liquid level
305                                 */
306                                 if (neighbor_is_same_liquid) {
307                                         vertices[0].Pos.Y = 0.5 * BS;
308                                         vertices[1].Pos.Y = 0.5 * BS;
309                                 } else {
310                                 /*
311                                         If neighbor is not liquid, lower border of face is
312                                         lower border of node
313                                 */
314                                         vertices[0].Pos.Y = -0.5 * BS;
315                                         vertices[1].Pos.Y = -0.5 * BS;
316                                 }
317
318                                 for(s32 j=0; j<4; j++)
319                                 {
320                                         if(dir == v3s16(0,0,1))
321                                                 vertices[j].Pos.rotateXZBy(0);
322                                         if(dir == v3s16(0,0,-1))
323                                                 vertices[j].Pos.rotateXZBy(180);
324                                         if(dir == v3s16(-1,0,0))
325                                                 vertices[j].Pos.rotateXZBy(90);
326                                         if(dir == v3s16(1,0,-0))
327                                                 vertices[j].Pos.rotateXZBy(-90);
328
329                                         // Do this to not cause glitches when two liquids are
330                                         // side-by-side
331                                         /*if(neighbor_is_same_liquid == false){
332                                                 vertices[j].Pos.X *= 0.98;
333                                                 vertices[j].Pos.Z *= 0.98;
334                                         }*/
335
336                                         vertices[j].Pos += intToFloat(p, BS);
337                                 }
338
339                                 u16 indices[] = {0,1,2,2,3,0};
340                                 // Add to mesh collector
341                                 collector.append(*current_tile, vertices, 4, indices, 6);
342                         }
343
344                         /*
345                                 Generate top
346                          */
347                         if(top_is_same_liquid)
348                                 continue;
349
350                         video::S3DVertex vertices[4] =
351                         {
352                                 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c, 0,1),
353                                 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c, 1,1),
354                                 video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c, 1,0),
355                                 video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c, 0,0),
356                         };
357
358                         v3f offset(p.X * BS, (p.Y + 0.5) * BS, p.Z * BS);
359                         for(s32 i=0; i<4; i++)
360                         {
361                                 vertices[i].Pos += offset;
362                         }
363
364                         u16 indices[] = {0,1,2,2,3,0};
365                         // Add to mesh collector
366                         collector.append(tile_liquid, vertices, 4, indices, 6);
367                 break;}
368                 case NDT_FLOWINGLIQUID:
369                 {
370                         /*
371                                 Add flowing liquid to mesh
372                         */
373                         TileSpec tile_liquid = f.special_tiles[0];
374                         TileSpec tile_liquid_bfculled = f.special_tiles[1];
375
376                         bool top_is_same_liquid = false;
377                         MapNode ntop = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y+1,z));
378                         content_t c_flowing = nodedef->getId(f.liquid_alternative_flowing);
379                         content_t c_source = nodedef->getId(f.liquid_alternative_source);
380                         if(ntop.getContent() == c_flowing || ntop.getContent() == c_source)
381                                 top_is_same_liquid = true;
382
383                         u16 l = 0;
384                         // If this liquid emits light and doesn't contain light, draw
385                         // it at what it emits, for an increased effect
386                         u8 light_source = nodedef->get(n).light_source;
387                         if(light_source != 0){
388                                 l = decode_light(light_source);
389                                 l = l | (l<<8);
390                         }
391                         // Use the light of the node on top if possible
392                         else if(nodedef->get(ntop).param_type == CPT_LIGHT)
393                                 l = getInteriorLight(ntop, 0, nodedef);
394                         // Otherwise use the light of this node (the liquid)
395                         else
396                                 l = getInteriorLight(n, 0, nodedef);
397                         video::SColor c = MapBlock_LightColor(f.alpha, l, f.light_source);
398
399                         u8 range = rangelim(nodedef->get(c_flowing).liquid_range, 1, 8);
400
401                         // Neighbor liquid levels (key = relative position)
402                         // Includes current node
403                         std::map<v3s16, f32> neighbor_levels;
404                         std::map<v3s16, content_t> neighbor_contents;
405                         std::map<v3s16, u8> neighbor_flags;
406                         const u8 neighborflag_top_is_same_liquid = 0x01;
407                         v3s16 neighbor_dirs[9] = {
408                                 v3s16(0,0,0),
409                                 v3s16(0,0,1),
410                                 v3s16(0,0,-1),
411                                 v3s16(1,0,0),
412                                 v3s16(-1,0,0),
413                                 v3s16(1,0,1),
414                                 v3s16(-1,0,-1),
415                                 v3s16(1,0,-1),
416                                 v3s16(-1,0,1),
417                         };
418                         for(u32 i=0; i<9; i++)
419                         {
420                                 content_t content = CONTENT_AIR;
421                                 float level = -0.5 * BS;
422                                 u8 flags = 0;
423                                 // Check neighbor
424                                 v3s16 p2 = p + neighbor_dirs[i];
425                                 MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
426                                 if(n2.getContent() != CONTENT_IGNORE)
427                                 {
428                                         content = n2.getContent();
429
430                                         if(n2.getContent() == c_source)
431                                                 level = 0.5 * BS;
432                                         else if(n2.getContent() == c_flowing){
433                                                 u8 liquid_level = (n2.param2&LIQUID_LEVEL_MASK);
434                                                 if (liquid_level <= LIQUID_LEVEL_MAX+1-range)
435                                                         liquid_level = 0;
436                                                 else
437                                                         liquid_level -= (LIQUID_LEVEL_MAX+1-range);
438                                                 level = (-0.5 + ((float)liquid_level + 0.5) / (float)range) * BS;
439                                         }
440
441                                         // Check node above neighbor.
442                                         // NOTE: This doesn't get executed if neighbor
443                                         //       doesn't exist
444                                         p2.Y += 1;
445                                         n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
446                                         if(n2.getContent() == c_source ||
447                                                         n2.getContent() == c_flowing)
448                                                 flags |= neighborflag_top_is_same_liquid;
449                                 }
450
451                                 neighbor_levels[neighbor_dirs[i]] = level;
452                                 neighbor_contents[neighbor_dirs[i]] = content;
453                                 neighbor_flags[neighbor_dirs[i]] = flags;
454                         }
455
456                         // Corner heights (average between four liquids)
457                         f32 corner_levels[4];
458
459                         v3s16 halfdirs[4] = {
460                                 v3s16(0,0,0),
461                                 v3s16(1,0,0),
462                                 v3s16(1,0,1),
463                                 v3s16(0,0,1),
464                         };
465                         for(u32 i=0; i<4; i++)
466                         {
467                                 v3s16 cornerdir = halfdirs[i];
468                                 float cornerlevel = 0;
469                                 u32 valid_count = 0;
470                                 u32 air_count = 0;
471                                 for(u32 j=0; j<4; j++)
472                                 {
473                                         v3s16 neighbordir = cornerdir - halfdirs[j];
474                                         content_t content = neighbor_contents[neighbordir];
475                                         // If top is liquid, draw starting from top of node
476                                         if(neighbor_flags[neighbordir] &
477                                                         neighborflag_top_is_same_liquid)
478                                         {
479                                                 cornerlevel = 0.5*BS;
480                                                 valid_count = 1;
481                                                 break;
482                                         }
483                                         // Source is always the same height
484                                         else if(content == c_source)
485                                         {
486                                                 cornerlevel = 0.5 * BS;
487                                                 valid_count = 1;
488                                                 break;
489                                         }
490                                         // Flowing liquid has level information
491                                         else if(content == c_flowing)
492                                         {
493                                                 cornerlevel += neighbor_levels[neighbordir];
494                                                 valid_count++;
495                                         }
496                                         else if(content == CONTENT_AIR)
497                                         {
498                                                 air_count++;
499                                         }
500                                 }
501                                 if(air_count >= 2)
502                                         cornerlevel = -0.5*BS+0.2;
503                                 else if(valid_count > 0)
504                                         cornerlevel /= valid_count;
505                                 corner_levels[i] = cornerlevel;
506                         }
507
508                         /*
509                                 Generate sides
510                         */
511
512                         v3s16 side_dirs[4] = {
513                                 v3s16(1,0,0),
514                                 v3s16(-1,0,0),
515                                 v3s16(0,0,1),
516                                 v3s16(0,0,-1),
517                         };
518                         s16 side_corners[4][2] = {
519                                 {1, 2},
520                                 {3, 0},
521                                 {2, 3},
522                                 {0, 1},
523                         };
524                         for(u32 i=0; i<4; i++)
525                         {
526                                 v3s16 dir = side_dirs[i];
527
528                                 /*
529                                         If our topside is liquid and neighbor's topside
530                                         is liquid, don't draw side face
531                                 */
532                                 if(top_is_same_liquid &&
533                                                 neighbor_flags[dir] & neighborflag_top_is_same_liquid)
534                                         continue;
535
536                                 content_t neighbor_content = neighbor_contents[dir];
537                                 const ContentFeatures &n_feat = nodedef->get(neighbor_content);
538
539                                 // Don't draw face if neighbor is blocking the view
540                                 if(n_feat.solidness == 2)
541                                         continue;
542
543                                 bool neighbor_is_same_liquid = (neighbor_content == c_source
544                                                 || neighbor_content == c_flowing);
545
546                                 // Don't draw any faces if neighbor same is liquid and top is
547                                 // same liquid
548                                 if(neighbor_is_same_liquid == true
549                                                 && top_is_same_liquid == false)
550                                         continue;
551
552                                 // Use backface culled material if neighbor doesn't have a
553                                 // solidness of 0
554                                 const TileSpec *current_tile = &tile_liquid;
555                                 if(n_feat.solidness != 0 || n_feat.visual_solidness != 0)
556                                         current_tile = &tile_liquid_bfculled;
557
558                                 video::S3DVertex vertices[4] =
559                                 {
560                                         video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c, 0,1),
561                                         video::S3DVertex(BS/2,0,BS/2, 0,0,0, c, 1,1),
562                                         video::S3DVertex(BS/2,0,BS/2, 0,0,0, c, 1,0),
563                                         video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c, 0,0),
564                                 };
565
566                                 /*
567                                         If our topside is liquid, set upper border of face
568                                         at upper border of node
569                                 */
570                                 if(top_is_same_liquid)
571                                 {
572                                         vertices[2].Pos.Y = 0.5*BS;
573                                         vertices[3].Pos.Y = 0.5*BS;
574                                 }
575                                 /*
576                                         Otherwise upper position of face is corner levels
577                                 */
578                                 else
579                                 {
580                                         vertices[2].Pos.Y = corner_levels[side_corners[i][0]];
581                                         vertices[3].Pos.Y = corner_levels[side_corners[i][1]];
582                                 }
583
584                                 /*
585                                         If neighbor is liquid, lower border of face is corner
586                                         liquid levels
587                                 */
588                                 if(neighbor_is_same_liquid)
589                                 {
590                                         vertices[0].Pos.Y = corner_levels[side_corners[i][1]];
591                                         vertices[1].Pos.Y = corner_levels[side_corners[i][0]];
592                                 }
593                                 /*
594                                         If neighbor is not liquid, lower border of face is
595                                         lower border of node
596                                 */
597                                 else
598                                 {
599                                         vertices[0].Pos.Y = -0.5*BS;
600                                         vertices[1].Pos.Y = -0.5*BS;
601                                 }
602
603                                 for(s32 j=0; j<4; j++)
604                                 {
605                                         if(dir == v3s16(0,0,1))
606                                                 vertices[j].Pos.rotateXZBy(0);
607                                         if(dir == v3s16(0,0,-1))
608                                                 vertices[j].Pos.rotateXZBy(180);
609                                         if(dir == v3s16(-1,0,0))
610                                                 vertices[j].Pos.rotateXZBy(90);
611                                         if(dir == v3s16(1,0,-0))
612                                                 vertices[j].Pos.rotateXZBy(-90);
613
614                                         // Do this to not cause glitches when two liquids are
615                                         // side-by-side
616                                         /*if(neighbor_is_same_liquid == false){
617                                                 vertices[j].Pos.X *= 0.98;
618                                                 vertices[j].Pos.Z *= 0.98;
619                                         }*/
620
621                                         vertices[j].Pos += intToFloat(p, BS);
622                                 }
623
624                                 u16 indices[] = {0,1,2,2,3,0};
625                                 // Add to mesh collector
626                                 collector.append(*current_tile, vertices, 4, indices, 6);
627                         }
628
629                         /*
630                                 Generate top side, if appropriate
631                         */
632
633                         if(top_is_same_liquid == false)
634                         {
635                                 video::S3DVertex vertices[4] =
636                                 {
637                                         video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c, 0,1),
638                                         video::S3DVertex(BS/2,0,BS/2, 0,0,0, c, 1,1),
639                                         video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c, 1,0),
640                                         video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c, 0,0),
641                                 };
642
643                                 // To get backface culling right, the vertices need to go
644                                 // clockwise around the front of the face. And we happened to
645                                 // calculate corner levels in exact reverse order.
646                                 s32 corner_resolve[4] = {3,2,1,0};
647
648                                 for(s32 i=0; i<4; i++)
649                                 {
650                                         //vertices[i].Pos.Y += liquid_level;
651                                         //vertices[i].Pos.Y += neighbor_levels[v3s16(0,0,0)];
652                                         s32 j = corner_resolve[i];
653                                         vertices[i].Pos.Y += corner_levels[j];
654                                         vertices[i].Pos += intToFloat(p, BS);
655                                 }
656
657                                 // Default downwards-flowing texture animation goes from
658                                 // -Z towards +Z, thus the direction is +Z.
659                                 // Rotate texture to make animation go in flow direction
660                                 // Positive if liquid moves towards +Z
661                                 f32 dz = (corner_levels[side_corners[3][0]] +
662                                                 corner_levels[side_corners[3][1]]) -
663                                                 (corner_levels[side_corners[2][0]] +
664                                                 corner_levels[side_corners[2][1]]);
665                                 // Positive if liquid moves towards +X
666                                 f32 dx = (corner_levels[side_corners[1][0]] +
667                                                 corner_levels[side_corners[1][1]]) -
668                                                 (corner_levels[side_corners[0][0]] +
669                                                 corner_levels[side_corners[0][1]]);
670                                 f32 tcoord_angle = atan2(dz, dx) * core::RADTODEG ;
671                                 v2f tcoord_center(0.5, 0.5);
672                                 v2f tcoord_translate(
673                                                 blockpos_nodes.Z + z,
674                                                 blockpos_nodes.X + x);
675                                 tcoord_translate.rotateBy(tcoord_angle);
676                                 tcoord_translate.X -= floor(tcoord_translate.X);
677                                 tcoord_translate.Y -= floor(tcoord_translate.Y);
678
679                                 for(s32 i=0; i<4; i++)
680                                 {
681                                         vertices[i].TCoords.rotateBy(
682                                                         tcoord_angle,
683                                                         tcoord_center);
684                                         vertices[i].TCoords += tcoord_translate;
685                                 }
686
687                                 v2f t = vertices[0].TCoords;
688                                 vertices[0].TCoords = vertices[2].TCoords;
689                                 vertices[2].TCoords = t;
690
691                                 u16 indices[] = {0,1,2,2,3,0};
692                                 // Add to mesh collector
693                                 collector.append(tile_liquid, vertices, 4, indices, 6);
694                         }
695                 break;}
696                 case NDT_GLASSLIKE:
697                 {
698                         TileSpec tile = getNodeTile(n, p, v3s16(0,0,0), data);
699
700                         u16 l = getInteriorLight(n, 1, nodedef);
701                         video::SColor c = MapBlock_LightColor(255, l, f.light_source);
702
703                         for(u32 j=0; j<6; j++)
704                         {
705                                 // Check this neighbor
706                                 v3s16 dir = g_6dirs[j];
707                                 v3s16 n2p = blockpos_nodes + p + dir;
708                                 MapNode n2 = data->m_vmanip.getNodeNoEx(n2p);
709                                 // Don't make face if neighbor is of same type
710                                 if(n2.getContent() == n.getContent())
711                                         continue;
712
713                                 // The face at Z+
714                                 video::S3DVertex vertices[4] = {
715                                         video::S3DVertex(-BS/2,-BS/2,BS/2, dir.X,dir.Y,dir.Z, c, 1,1),
716                                         video::S3DVertex(BS/2,-BS/2,BS/2, dir.X,dir.Y,dir.Z, c, 0,1),
717                                         video::S3DVertex(BS/2,BS/2,BS/2, dir.X,dir.Y,dir.Z, c, 0,0),
718                                         video::S3DVertex(-BS/2,BS/2,BS/2, dir.X,dir.Y,dir.Z, c, 1,0),
719                                 };
720
721                                 // Rotations in the g_6dirs format
722                                 if(j == 0) // Z+
723                                         for(u16 i=0; i<4; i++)
724                                                 vertices[i].Pos.rotateXZBy(0);
725                                 else if(j == 1) // Y+
726                                         for(u16 i=0; i<4; i++)
727                                                 vertices[i].Pos.rotateYZBy(-90);
728                                 else if(j == 2) // X+
729                                         for(u16 i=0; i<4; i++)
730                                                 vertices[i].Pos.rotateXZBy(-90);
731                                 else if(j == 3) // Z-
732                                         for(u16 i=0; i<4; i++)
733                                                 vertices[i].Pos.rotateXZBy(180);
734                                 else if(j == 4) // Y-
735                                         for(u16 i=0; i<4; i++)
736                                                 vertices[i].Pos.rotateYZBy(90);
737                                 else if(j == 5) // X-
738                                         for(u16 i=0; i<4; i++)
739                                                 vertices[i].Pos.rotateXZBy(90);
740
741                                 for(u16 i=0; i<4; i++){
742                                         vertices[i].Pos += intToFloat(p, BS);
743                                 }
744
745                                 u16 indices[] = {0,1,2,2,3,0};
746                                 // Add to mesh collector
747                                 collector.append(tile, vertices, 4, indices, 6);
748                         }
749                 break;}
750                 case NDT_GLASSLIKE_FRAMED_OPTIONAL:
751                         // This is always pre-converted to something else
752                         FATAL_ERROR("NDT_GLASSLIKE_FRAMED_OPTIONAL not pre-converted as expected");
753                         break;
754                 case NDT_GLASSLIKE_FRAMED:
755                 {
756                         static const v3s16 dirs[6] = {
757                                 v3s16( 0, 1, 0),
758                                 v3s16( 0,-1, 0),
759                                 v3s16( 1, 0, 0),
760                                 v3s16(-1, 0, 0),
761                                 v3s16( 0, 0, 1),
762                                 v3s16( 0, 0,-1)
763                         };
764
765                         u8 i;
766                         TileSpec tiles[6];
767                         for (i = 0; i < 6; i++)
768                                 tiles[i] = getNodeTile(n, p, dirs[i], data);
769
770                         TileSpec glass_tiles[6];
771                         if (tiles[1].texture && tiles[2].texture && tiles[3].texture) {
772                                 glass_tiles[0] = tiles[2];
773                                 glass_tiles[1] = tiles[3];
774                                 glass_tiles[2] = tiles[1];
775                                 glass_tiles[3] = tiles[1];
776                                 glass_tiles[4] = tiles[1];
777                                 glass_tiles[5] = tiles[1];
778                         } else {
779                                 for (i = 0; i < 6; i++)
780                                         glass_tiles[i] = tiles[1];
781                         }
782
783                         u8 param2 = n.getParam2();
784                         bool H_merge = ! bool(param2 & 128);
785                         bool V_merge = ! bool(param2 & 64);
786                         param2  = param2 & 63;
787
788                         u16 l = getInteriorLight(n, 1, nodedef);
789                         video::SColor c = MapBlock_LightColor(255, l, f.light_source);
790                         v3f pos = intToFloat(p, BS);
791                         static const float a = BS / 2;
792                         static const float g = a - 0.003;
793                         static const float b = .876 * ( BS / 2 );
794
795                         static const aabb3f frame_edges[12] = {
796                                 aabb3f( b, b,-a, a, a, a), // y+
797                                 aabb3f(-a, b,-a,-b, a, a), // y+
798                                 aabb3f( b,-a,-a, a,-b, a), // y-
799                                 aabb3f(-a,-a,-a,-b,-b, a), // y-
800                                 aabb3f( b,-a, b, a, a, a), // x+
801                                 aabb3f( b,-a,-a, a, a,-b), // x+
802                                 aabb3f(-a,-a, b,-b, a, a), // x-
803                                 aabb3f(-a,-a,-a,-b, a,-b), // x-
804                                 aabb3f(-a, b, b, a, a, a), // z+
805                                 aabb3f(-a,-a, b, a,-b, a), // z+
806                                 aabb3f(-a,-a,-a, a,-b,-b), // z-
807                                 aabb3f(-a, b,-a, a, a,-b)  // z-
808                         };
809                         static const aabb3f glass_faces[6] = {
810                                 aabb3f(-g, g,-g, g, g, g), // y+
811                                 aabb3f(-g,-g,-g, g,-g, g), // y-
812                                 aabb3f( g,-g,-g, g, g, g), // x+
813                                 aabb3f(-g,-g,-g,-g, g, g), // x-
814                                 aabb3f(-g,-g, g, g, g, g), // z+
815                                 aabb3f(-g,-g,-g, g, g,-g)  // z-
816                         };
817
818                         // table of node visible faces, 0 = invisible
819                         int visible_faces[6] = {0,0,0,0,0,0};
820
821                         // table of neighbours, 1 = same type, checked with g_26dirs
822                         int nb[18] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
823
824                         // g_26dirs to check when only horizontal merge is allowed
825                         int nb_H_dirs[8] = {0,2,3,5,10,11,12,13};
826
827                         content_t current = n.getContent();
828                         content_t n2c;
829                         MapNode n2;
830                         v3s16 n2p;
831
832                         // neighbours checks for frames visibility
833
834                         if (!H_merge && V_merge) {
835                                 n2p = blockpos_nodes + p + g_26dirs[1];
836                                 n2 = data->m_vmanip.getNodeNoEx(n2p);
837                                 n2c = n2.getContent();
838                                 if (n2c == current || n2c == CONTENT_IGNORE)
839                                         nb[1] = 1;
840                                 n2p = blockpos_nodes + p + g_26dirs[4];
841                                 n2 = data->m_vmanip.getNodeNoEx(n2p);
842                                 n2c = n2.getContent();
843                                 if (n2c == current || n2c == CONTENT_IGNORE)
844                                         nb[4] = 1;
845                         } else if (H_merge && !V_merge) {
846                                 for(i = 0; i < 8; i++) {
847                                         n2p = blockpos_nodes + p + g_26dirs[nb_H_dirs[i]];
848                                         n2 = data->m_vmanip.getNodeNoEx(n2p);
849                                         n2c = n2.getContent();
850                                         if (n2c == current || n2c == CONTENT_IGNORE)
851                                                 nb[nb_H_dirs[i]] = 1;
852                                 }
853                         } else if (H_merge && V_merge) {
854                                 for(i = 0; i < 18; i++) {
855                                         n2p = blockpos_nodes + p + g_26dirs[i];
856                                         n2 = data->m_vmanip.getNodeNoEx(n2p);
857                                         n2c = n2.getContent();
858                                         if (n2c == current || n2c == CONTENT_IGNORE)
859                                                 nb[i] = 1;
860                                 }
861                         }
862
863                         // faces visibility checks
864
865                         if (!V_merge) {
866                                 visible_faces[0] = 1;
867                                 visible_faces[1] = 1;
868                         } else {
869                                 for(i = 0; i < 2; i++) {
870                                         n2p = blockpos_nodes + p + dirs[i];
871                                         n2 = data->m_vmanip.getNodeNoEx(n2p);
872                                         n2c = n2.getContent();
873                                         if (n2c != current)
874                                                 visible_faces[i] = 1;
875                                 }
876                         }
877
878                         if (!H_merge) {
879                                 visible_faces[2] = 1;
880                                 visible_faces[3] = 1;
881                                 visible_faces[4] = 1;
882                                 visible_faces[5] = 1;
883                         } else {
884                                 for(i = 2; i < 6; i++) {
885                                         n2p = blockpos_nodes + p + dirs[i];
886                                         n2 = data->m_vmanip.getNodeNoEx(n2p);
887                                         n2c = n2.getContent();
888                                         if (n2c != current)
889                                                 visible_faces[i] = 1;
890                                 }
891                         }
892
893                         static const u8 nb_triplet[12*3] = {
894                                 1,2, 7,  1,5, 6,  4,2,15,  4,5,14,
895                                 2,0,11,  2,3,13,  5,0,10,  5,3,12,
896                                 0,1, 8,  0,4,16,  3,4,17,  3,1, 9
897                         };
898
899                         f32 tx1, ty1, tz1, tx2, ty2, tz2;
900                         aabb3f box;
901
902                         for(i = 0; i < 12; i++)
903                         {
904                                 int edge_invisible;
905                                 if (nb[nb_triplet[i*3+2]])
906                                         edge_invisible = nb[nb_triplet[i*3]] & nb[nb_triplet[i*3+1]];
907                                 else
908                                         edge_invisible = nb[nb_triplet[i*3]] ^ nb[nb_triplet[i*3+1]];
909                                 if (edge_invisible)
910                                         continue;
911                                 box = frame_edges[i];
912                                 box.MinEdge += pos;
913                                 box.MaxEdge += pos;
914                                 tx1 = (box.MinEdge.X / BS) + 0.5;
915                                 ty1 = (box.MinEdge.Y / BS) + 0.5;
916                                 tz1 = (box.MinEdge.Z / BS) + 0.5;
917                                 tx2 = (box.MaxEdge.X / BS) + 0.5;
918                                 ty2 = (box.MaxEdge.Y / BS) + 0.5;
919                                 tz2 = (box.MaxEdge.Z / BS) + 0.5;
920                                 f32 txc1[24] = {
921                                         tx1,   1-tz2,   tx2, 1-tz1,
922                                         tx1,     tz1,   tx2,   tz2,
923                                         tz1,   1-ty2,   tz2, 1-ty1,
924                                         1-tz2, 1-ty2, 1-tz1, 1-ty1,
925                                         1-tx2, 1-ty2, 1-tx1, 1-ty1,
926                                         tx1,   1-ty2,   tx2, 1-ty1,
927                                 };
928                                 makeCuboid(&collector, box, &tiles[0], 1, c, txc1);
929                         }
930
931                         for(i = 0; i < 6; i++)
932                         {
933                                 if (!visible_faces[i])
934                                         continue;
935                                 box = glass_faces[i];
936                                 box.MinEdge += pos;
937                                 box.MaxEdge += pos;
938                                 tx1 = (box.MinEdge.X / BS) + 0.5;
939                                 ty1 = (box.MinEdge.Y / BS) + 0.5;
940                                 tz1 = (box.MinEdge.Z / BS) + 0.5;
941                                 tx2 = (box.MaxEdge.X / BS) + 0.5;
942                                 ty2 = (box.MaxEdge.Y / BS) + 0.5;
943                                 tz2 = (box.MaxEdge.Z / BS) + 0.5;
944                                 f32 txc2[24] = {
945                                         tx1,   1-tz2,   tx2, 1-tz1,
946                                         tx1,     tz1,   tx2,   tz2,
947                                         tz1,   1-ty2,   tz2, 1-ty1,
948                                         1-tz2, 1-ty2, 1-tz1, 1-ty1,
949                                         1-tx2, 1-ty2, 1-tx1, 1-ty1,
950                                         tx1,   1-ty2,   tx2, 1-ty1,
951                                 };
952                                 makeCuboid(&collector, box, &glass_tiles[i], 1, c, txc2);
953                         }
954
955                         if (param2 > 0 && f.special_tiles[0].texture) {
956                                 // Interior volume level is in range 0 .. 63,
957                                 // convert it to -0.5 .. 0.5
958                                 float vlev = (((float)param2 / 63.0 ) * 2.0 - 1.0);
959                                 TileSpec interior_tiles[6];
960                                 for (i = 0; i < 6; i++)
961                                         interior_tiles[i] = f.special_tiles[0];
962                                 float offset = 0.003;
963                                 box = aabb3f(visible_faces[3] ? -b : -a + offset,
964                                                          visible_faces[1] ? -b : -a + offset,
965                                                          visible_faces[5] ? -b : -a + offset,
966                                                          visible_faces[2] ? b : a - offset,
967                                                          visible_faces[0] ? b * vlev : a * vlev - offset,
968                                                          visible_faces[4] ? b : a - offset);
969                                 box.MinEdge += pos;
970                                 box.MaxEdge += pos;
971                                 tx1 = (box.MinEdge.X / BS) + 0.5;
972                                 ty1 = (box.MinEdge.Y / BS) + 0.5;
973                                 tz1 = (box.MinEdge.Z / BS) + 0.5;
974                                 tx2 = (box.MaxEdge.X / BS) + 0.5;
975                                 ty2 = (box.MaxEdge.Y / BS) + 0.5;
976                                 tz2 = (box.MaxEdge.Z / BS) + 0.5;
977                                 f32 txc3[24] = {
978                                         tx1,   1-tz2,   tx2, 1-tz1,
979                                         tx1,     tz1,   tx2,   tz2,
980                                         tz1,   1-ty2,   tz2, 1-ty1,
981                                         1-tz2, 1-ty2, 1-tz1, 1-ty1,
982                                         1-tx2, 1-ty2, 1-tx1, 1-ty1,
983                                         tx1,   1-ty2,   tx2, 1-ty1,
984                                 };
985                                 makeCuboid(&collector, box, interior_tiles, 6, c,  txc3);
986                         }
987                 break;}
988                 case NDT_ALLFACES:
989                 {
990                         TileSpec tile_leaves = getNodeTile(n, p,
991                                         v3s16(0,0,0), data);
992
993                         u16 l = getInteriorLight(n, 1, nodedef);
994                         video::SColor c = MapBlock_LightColor(255, l, f.light_source);
995
996                         v3f pos = intToFloat(p, BS);
997                         aabb3f box(-BS/2,-BS/2,-BS/2,BS/2,BS/2,BS/2);
998                         box.MinEdge += pos;
999                         box.MaxEdge += pos;
1000                         makeCuboid(&collector, box, &tile_leaves, 1, c, NULL);
1001                 break;}
1002                 case NDT_ALLFACES_OPTIONAL:
1003                         // This is always pre-converted to something else
1004                         FATAL_ERROR("NDT_ALLFACES_OPTIONAL not pre-converted");
1005                         break;
1006                 case NDT_TORCHLIKE:
1007                 {
1008                         v3s16 dir = n.getWallMountedDir(nodedef);
1009
1010                         u8 tileindex = 0;
1011                         if(dir == v3s16(0,-1,0)){
1012                                 tileindex = 0; // floor
1013                         } else if(dir == v3s16(0,1,0)){
1014                                 tileindex = 1; // ceiling
1015                         // For backwards compatibility
1016                         } else if(dir == v3s16(0,0,0)){
1017                                 tileindex = 0; // floor
1018                         } else {
1019                                 tileindex = 2; // side
1020                         }
1021
1022                         TileSpec tile = getNodeTileN(n, p, tileindex, data);
1023                         tile.material_flags &= ~MATERIAL_FLAG_BACKFACE_CULLING;
1024                         tile.material_flags |= MATERIAL_FLAG_CRACK_OVERLAY;
1025
1026                         u16 l = getInteriorLight(n, 1, nodedef);
1027                         video::SColor c = MapBlock_LightColor(255, l, f.light_source);
1028
1029                         float s = BS/2*f.visual_scale;
1030                         // Wall at X+ of node
1031                         video::S3DVertex vertices[4] =
1032                         {
1033                                 video::S3DVertex(-s,-s,0, 0,0,0, c, 0,1),
1034                                 video::S3DVertex( s,-s,0, 0,0,0, c, 1,1),
1035                                 video::S3DVertex( s, s,0, 0,0,0, c, 1,0),
1036                                 video::S3DVertex(-s, s,0, 0,0,0, c, 0,0),
1037                         };
1038
1039                         for(s32 i=0; i<4; i++)
1040                         {
1041                                 if(dir == v3s16(1,0,0))
1042                                         vertices[i].Pos.rotateXZBy(0);
1043                                 if(dir == v3s16(-1,0,0))
1044                                         vertices[i].Pos.rotateXZBy(180);
1045                                 if(dir == v3s16(0,0,1))
1046                                         vertices[i].Pos.rotateXZBy(90);
1047                                 if(dir == v3s16(0,0,-1))
1048                                         vertices[i].Pos.rotateXZBy(-90);
1049                                 if(dir == v3s16(0,-1,0))
1050                                         vertices[i].Pos.rotateXZBy(45);
1051                                 if(dir == v3s16(0,1,0))
1052                                         vertices[i].Pos.rotateXZBy(-45);
1053
1054                                 vertices[i].Pos += intToFloat(p, BS);
1055                         }
1056
1057                         u16 indices[] = {0,1,2,2,3,0};
1058                         // Add to mesh collector
1059                         collector.append(tile, vertices, 4, indices, 6);
1060                 break;}
1061                 case NDT_SIGNLIKE:
1062                 {
1063                         TileSpec tile = getNodeTileN(n, p, 0, data);
1064                         tile.material_flags &= ~MATERIAL_FLAG_BACKFACE_CULLING;
1065                         tile.material_flags |= MATERIAL_FLAG_CRACK_OVERLAY;
1066
1067                         u16 l = getInteriorLight(n, 0, nodedef);
1068                         video::SColor c = MapBlock_LightColor(255, l, f.light_source);
1069
1070                         float d = (float)BS/16;
1071                         float s = BS/2*f.visual_scale;
1072                         // Wall at X+ of node
1073                         video::S3DVertex vertices[4] =
1074                         {
1075                                 video::S3DVertex(BS/2-d,  s,  s, 0,0,0, c, 0,0),
1076                                 video::S3DVertex(BS/2-d,  s, -s, 0,0,0, c, 1,0),
1077                                 video::S3DVertex(BS/2-d, -s, -s, 0,0,0, c, 1,1),
1078                                 video::S3DVertex(BS/2-d, -s,  s, 0,0,0, c, 0,1),
1079                         };
1080
1081                         v3s16 dir = n.getWallMountedDir(nodedef);
1082
1083                         for(s32 i=0; i<4; i++)
1084                         {
1085                                 if(dir == v3s16(1,0,0))
1086                                         vertices[i].Pos.rotateXZBy(0);
1087                                 if(dir == v3s16(-1,0,0))
1088                                         vertices[i].Pos.rotateXZBy(180);
1089                                 if(dir == v3s16(0,0,1))
1090                                         vertices[i].Pos.rotateXZBy(90);
1091                                 if(dir == v3s16(0,0,-1))
1092                                         vertices[i].Pos.rotateXZBy(-90);
1093                                 if(dir == v3s16(0,-1,0))
1094                                         vertices[i].Pos.rotateXYBy(-90);
1095                                 if(dir == v3s16(0,1,0))
1096                                         vertices[i].Pos.rotateXYBy(90);
1097
1098                                 vertices[i].Pos += intToFloat(p, BS);
1099                         }
1100
1101                         u16 indices[] = {0,1,2,2,3,0};
1102                         // Add to mesh collector
1103                         collector.append(tile, vertices, 4, indices, 6);
1104                 break;}
1105                 case NDT_PLANTLIKE:
1106                 {
1107                         TileSpec tile = getNodeTileN(n, p, 0, data);
1108                         tile.material_flags |= MATERIAL_FLAG_CRACK_OVERLAY;
1109
1110                         u16 l = getInteriorLight(n, 1, nodedef);
1111                         video::SColor c = MapBlock_LightColor(255, l, f.light_source);
1112
1113                         float s = BS / 2 * f.visual_scale;
1114
1115                         for (int j = 0; j < 2; j++)
1116                         {
1117                                 video::S3DVertex vertices[4] =
1118                                 {
1119                                         video::S3DVertex(-s,-BS/2, 0, 0,0,0, c, 0,1),
1120                                         video::S3DVertex( s,-BS/2, 0, 0,0,0, c, 1,1),
1121                                         video::S3DVertex( s,-BS/2 + s*2,0, 0,0,0, c, 1,0),
1122                                         video::S3DVertex(-s,-BS/2 + s*2,0, 0,0,0, c, 0,0),
1123                                 };
1124                                 float rotate_degree = 0;
1125                                 if (f.param_type_2 == CPT2_DEGROTATE)
1126                                         rotate_degree = n.param2 * 2;
1127
1128                                 if (j == 0) {
1129                                         for(u16 i = 0; i < 4; i++)
1130                                                 vertices[i].Pos.rotateXZBy(46 + rotate_degree);
1131                                 } else if (j == 1) {
1132                                         for(u16 i = 0; i < 4; i++)
1133                                                 vertices[i].Pos.rotateXZBy(-44 + rotate_degree);
1134                                 }
1135
1136                                 for (int i = 0; i < 4; i++)
1137                                 {
1138                                         vertices[i].Pos *= f.visual_scale;
1139                                         vertices[i].Pos.Y += BS/2 * (f.visual_scale - 1);
1140                                         vertices[i].Pos += intToFloat(p, BS);
1141                                 }
1142
1143                                 u16 indices[] = {0, 1, 2, 2, 3, 0};
1144                                 // Add to mesh collector
1145                                 collector.append(tile, vertices, 4, indices, 6);
1146                         }
1147                 break;}
1148                 case NDT_FIRELIKE:
1149                 {
1150                         TileSpec tile = getNodeTileN(n, p, 0, data);
1151                         tile.material_flags |= MATERIAL_FLAG_CRACK_OVERLAY;
1152
1153                         u16 l = getInteriorLight(n, 1, nodedef);
1154                         video::SColor c = MapBlock_LightColor(255, l, f.light_source);
1155
1156                         float s = BS / 2 * f.visual_scale;
1157
1158                         content_t current = n.getContent();
1159                         content_t n2c;
1160                         MapNode n2;
1161                         v3s16 n2p;
1162
1163                         static const v3s16 dirs[6] = {
1164                                 v3s16( 0,  1,  0),
1165                                 v3s16( 0, -1,  0),
1166                                 v3s16( 1,  0,  0),
1167                                 v3s16(-1,  0,  0),
1168                                 v3s16( 0,  0,  1),
1169                                 v3s16( 0,  0, -1)
1170                         };
1171
1172                         int doDraw[6] = {0, 0, 0, 0, 0, 0};
1173
1174                         bool drawAllFaces = true;
1175
1176                         // Check for adjacent nodes
1177                         for (int i = 0; i < 6; i++) {
1178                                 n2p = blockpos_nodes + p + dirs[i];
1179                                 n2 = data->m_vmanip.getNodeNoEx(n2p);
1180                                 n2c = n2.getContent();
1181                                 if (n2c != CONTENT_IGNORE && n2c != CONTENT_AIR && n2c != current) {
1182                                         doDraw[i] = 1;
1183                                         if (drawAllFaces)
1184                                                 drawAllFaces = false;
1185
1186                                 }
1187                         }
1188
1189                         for (int j = 0; j < 6; j++) {
1190
1191                                 video::S3DVertex vertices[4] = {
1192                                         video::S3DVertex(-s, -BS / 2,         0, 0, 0, 0, c, 0, 1),
1193                                         video::S3DVertex( s, -BS / 2,         0, 0, 0, 0, c, 1, 1),
1194                                         video::S3DVertex( s, -BS / 2 + s * 2, 0, 0, 0, 0, c, 1, 0),
1195                                         video::S3DVertex(-s, -BS / 2 + s * 2, 0, 0, 0, 0, c, 0, 0),
1196                                 };
1197
1198                                 // Calculate which faces should be drawn, (top or sides)
1199                                 if (j == 0 && (drawAllFaces ||
1200                                                 (doDraw[3] == 1 || doDraw[1] == 1))) {
1201                                         for (int i = 0; i < 4; i++) {
1202                                                 vertices[i].Pos.rotateXZBy(90);
1203                                                 vertices[i].Pos.rotateXYBy(-10);
1204                                                 vertices[i].Pos.X -= 4.0;
1205                                         }
1206                                 } else if (j == 1 && (drawAllFaces ||
1207                                                 (doDraw[5] == 1 || doDraw[1] == 1))) {
1208                                         for (int i = 0; i < 4; i++) {
1209                                                 vertices[i].Pos.rotateXZBy(180);
1210                                                 vertices[i].Pos.rotateYZBy(10);
1211                                                 vertices[i].Pos.Z -= 4.0;
1212                                         }
1213                                 } else if (j == 2 && (drawAllFaces ||
1214                                                 (doDraw[2] == 1 || doDraw[1] == 1))) {
1215                                         for (int i = 0; i < 4; i++) {
1216                                                 vertices[i].Pos.rotateXZBy(270);
1217                                                 vertices[i].Pos.rotateXYBy(10);
1218                                                 vertices[i].Pos.X += 4.0;
1219                                         }
1220                                 } else if (j == 3 && (drawAllFaces ||
1221                                                 (doDraw[4] == 1 || doDraw[1] == 1))) {
1222                                         for (int i = 0; i < 4; i++) {
1223                                                 vertices[i].Pos.rotateYZBy(-10);
1224                                                 vertices[i].Pos.Z += 4.0;
1225                                         }
1226                                 // Center cross-flames
1227                                 } else if (j == 4 && (drawAllFaces || doDraw[1] == 1)) {
1228                                         for (int i = 0; i < 4; i++) {
1229                                                 vertices[i].Pos.rotateXZBy(45);
1230                                         }
1231                                 } else if (j == 5 && (drawAllFaces || doDraw[1] == 1)) {
1232                                         for (int i = 0; i < 4; i++) {
1233                                                 vertices[i].Pos.rotateXZBy(-45);
1234                                         }
1235                                 // Render flames on bottom of node above
1236                                 } else if (j == 0 && doDraw[0] == 1 && doDraw[1] == 0) {
1237                                         for (int i = 0; i < 4; i++) {
1238                                                 vertices[i].Pos.rotateYZBy(70);
1239                                                 vertices[i].Pos.rotateXZBy(90);
1240                                                 vertices[i].Pos.Y += 4.84;
1241                                                 vertices[i].Pos.X -= 4.7;
1242                                         }
1243                                 } else if (j == 1 && doDraw[0] == 1 && doDraw[1] == 0) {
1244                                         for (int i = 0; i < 4; i++) {
1245                                                 vertices[i].Pos.rotateYZBy(70);
1246                                                 vertices[i].Pos.rotateXZBy(180);
1247                                                 vertices[i].Pos.Y += 4.84;
1248                                                 vertices[i].Pos.Z -= 4.7;
1249                                         }
1250                                 } else if (j == 2 && doDraw[0] == 1 && doDraw[1] == 0) {
1251                                         for (int i = 0; i < 4; i++) {
1252                                                 vertices[i].Pos.rotateYZBy(70);
1253                                                 vertices[i].Pos.rotateXZBy(270);
1254                                                 vertices[i].Pos.Y += 4.84;
1255                                                 vertices[i].Pos.X += 4.7;
1256                                         }
1257                                 } else if (j == 3 && doDraw[0] == 1 && doDraw[1] == 0) {
1258                                         for (int i = 0; i < 4; i++) {
1259                                                 vertices[i].Pos.rotateYZBy(70);
1260                                                 vertices[i].Pos.Y += 4.84;
1261                                                 vertices[i].Pos.Z += 4.7;
1262                                         }
1263                                 } else {
1264                                         // Skip faces that aren't adjacent to a node
1265                                         continue;
1266                                 }
1267
1268                                 for (int i = 0; i < 4; i++) {
1269                                         vertices[i].Pos *= f.visual_scale;
1270                                         vertices[i].Pos += intToFloat(p, BS);
1271                                 }
1272
1273                                 u16 indices[] = {0, 1, 2, 2, 3, 0};
1274                                 // Add to mesh collector
1275                                 collector.append(tile, vertices, 4, indices, 6);
1276                         }
1277                 break;}
1278                 case NDT_FENCELIKE:
1279                 {
1280                         TileSpec tile = getNodeTile(n, p, v3s16(0,0,0), data);
1281                         TileSpec tile_nocrack = tile;
1282                         tile_nocrack.material_flags &= ~MATERIAL_FLAG_CRACK;
1283
1284                         // Put wood the right way around in the posts
1285                         TileSpec tile_rot = tile;
1286                         tile_rot.rotation = 1;
1287
1288                         u16 l = getInteriorLight(n, 1, nodedef);
1289                         video::SColor c = MapBlock_LightColor(255, l, f.light_source);
1290
1291                         const f32 post_rad=(f32)BS/8;
1292                         const f32 bar_rad=(f32)BS/16;
1293                         const f32 bar_len=(f32)(BS/2)-post_rad;
1294
1295                         v3f pos = intToFloat(p, BS);
1296
1297                         // The post - always present
1298                         aabb3f post(-post_rad,-BS/2,-post_rad,post_rad,BS/2,post_rad);
1299                         post.MinEdge += pos;
1300                         post.MaxEdge += pos;
1301                         f32 postuv[24]={
1302                                         6/16.,6/16.,10/16.,10/16.,
1303                                         6/16.,6/16.,10/16.,10/16.,
1304                                         0/16.,0,4/16.,1,
1305                                         4/16.,0,8/16.,1,
1306                                         8/16.,0,12/16.,1,
1307                                         12/16.,0,16/16.,1};
1308                         makeCuboid(&collector, post, &tile_rot, 1, c, postuv);
1309
1310                         // Now a section of fence, +X, if there's a post there
1311                         v3s16 p2 = p;
1312                         p2.X++;
1313                         MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
1314                         const ContentFeatures *f2 = &nodedef->get(n2);
1315                         if(f2->drawtype == NDT_FENCELIKE)
1316                         {
1317                                 aabb3f bar(-bar_len+BS/2,-bar_rad+BS/4,-bar_rad,
1318                                                 bar_len+BS/2,bar_rad+BS/4,bar_rad);
1319                                 bar.MinEdge += pos;
1320                                 bar.MaxEdge += pos;
1321                                 f32 xrailuv[24]={
1322                                         0/16.,2/16.,16/16.,4/16.,
1323                                         0/16.,4/16.,16/16.,6/16.,
1324                                         6/16.,6/16.,8/16.,8/16.,
1325                                         10/16.,10/16.,12/16.,12/16.,
1326                                         0/16.,8/16.,16/16.,10/16.,
1327                                         0/16.,14/16.,16/16.,16/16.};
1328                                 makeCuboid(&collector, bar, &tile_nocrack, 1,
1329                                                 c, xrailuv);
1330                                 bar.MinEdge.Y -= BS/2;
1331                                 bar.MaxEdge.Y -= BS/2;
1332                                 makeCuboid(&collector, bar, &tile_nocrack, 1,
1333                                                 c, xrailuv);
1334                         }
1335
1336                         // Now a section of fence, +Z, if there's a post there
1337                         p2 = p;
1338                         p2.Z++;
1339                         n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
1340                         f2 = &nodedef->get(n2);
1341                         if(f2->drawtype == NDT_FENCELIKE)
1342                         {
1343                                 aabb3f bar(-bar_rad,-bar_rad+BS/4,-bar_len+BS/2,
1344                                                 bar_rad,bar_rad+BS/4,bar_len+BS/2);
1345                                 bar.MinEdge += pos;
1346                                 bar.MaxEdge += pos;
1347                                 f32 zrailuv[24]={
1348                                         3/16.,1/16.,5/16.,5/16., // cannot rotate; stretch
1349                                         4/16.,1/16.,6/16.,5/16., // for wood texture instead
1350                                         0/16.,9/16.,16/16.,11/16.,
1351                                         0/16.,6/16.,16/16.,8/16.,
1352                                         6/16.,6/16.,8/16.,8/16.,
1353                                         10/16.,10/16.,12/16.,12/16.};
1354                                 makeCuboid(&collector, bar, &tile_nocrack, 1,
1355                                                 c, zrailuv);
1356                                 bar.MinEdge.Y -= BS/2;
1357                                 bar.MaxEdge.Y -= BS/2;
1358                                 makeCuboid(&collector, bar, &tile_nocrack, 1,
1359                                                 c, zrailuv);
1360                         }
1361                 break;}
1362                 case NDT_RAILLIKE:
1363                 {
1364                         bool is_rail_x[6]; /* (-1,-1,0) X (1,-1,0) (-1,0,0) X (1,0,0) (-1,1,0) X (1,1,0) */
1365                         bool is_rail_z[6];
1366
1367                         content_t thiscontent = n.getContent();
1368                         std::string groupname = "connect_to_raillike"; // name of the group that enables connecting to raillike nodes of different kind
1369                         int self_group = ((ItemGroupList) nodedef->get(n).groups)[groupname];
1370
1371                         u8 index = 0;
1372                         for (s8 y0 = -1; y0 <= 1; y0++) {
1373                                 // Prevent from indexing never used coordinates
1374                                 for (s8 xz = -1; xz <= 1; xz++) {
1375                                         if (xz == 0)
1376                                                 continue;
1377                                         MapNode n_xy = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x + xz, y + y0, z));
1378                                         MapNode n_zy = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y + y0, z + xz));
1379                                         ContentFeatures def_xy = nodedef->get(n_xy);
1380                                         ContentFeatures def_zy = nodedef->get(n_zy);
1381
1382                                         // Check if current node would connect with the rail
1383                                         is_rail_x[index] = ((def_xy.drawtype == NDT_RAILLIKE
1384                                                         && ((ItemGroupList) def_xy.groups)[groupname] == self_group)
1385                                                         || n_xy.getContent() == thiscontent);
1386
1387                                         is_rail_z[index] = ((def_zy.drawtype == NDT_RAILLIKE
1388                                                         && ((ItemGroupList) def_zy.groups)[groupname] == self_group)
1389                                                         || n_zy.getContent() == thiscontent);
1390                                         index++;
1391                                 }
1392                         }
1393
1394                         bool is_rail_x_all[2]; // [0] = negative x, [1] = positive x coordinate from the current node position
1395                         bool is_rail_z_all[2];
1396                         is_rail_x_all[0] = is_rail_x[0] || is_rail_x[2] || is_rail_x[4];
1397                         is_rail_x_all[1] = is_rail_x[1] || is_rail_x[3] || is_rail_x[5];
1398                         is_rail_z_all[0] = is_rail_z[0] || is_rail_z[2] || is_rail_z[4];
1399                         is_rail_z_all[1] = is_rail_z[1] || is_rail_z[3] || is_rail_z[5];
1400
1401                         // reasonable default, flat straight unrotated rail
1402                         bool is_straight = true;
1403                         int adjacencies = 0;
1404                         int angle = 0;
1405                         u8 tileindex = 0;
1406
1407                         // check for sloped rail
1408                         if (is_rail_x[4] || is_rail_x[5] || is_rail_z[4] || is_rail_z[5]) {
1409                                 adjacencies = 5; // 5 means sloped
1410                                 is_straight = true; // sloped is always straight
1411                         } else {
1412                                 // is really straight, rails on both sides
1413                                 is_straight = (is_rail_x_all[0] && is_rail_x_all[1]) || (is_rail_z_all[0] && is_rail_z_all[1]);
1414                                 adjacencies = is_rail_x_all[0] + is_rail_x_all[1] + is_rail_z_all[0] + is_rail_z_all[1];
1415                         }
1416
1417                         switch (adjacencies) {
1418                         case 1:
1419                                 if (is_rail_x_all[0] || is_rail_x_all[1])
1420                                         angle = 90;
1421                                 break;
1422                         case 2:
1423                                 if (!is_straight)
1424                                         tileindex = 1; // curved
1425                                 if (is_rail_x_all[0] && is_rail_x_all[1])
1426                                         angle = 90;
1427                                 if (is_rail_z_all[0] && is_rail_z_all[1]) {
1428                                         if (is_rail_z[4])
1429                                                 angle = 180;
1430                                 }
1431                                 else if (is_rail_x_all[0] && is_rail_z_all[0])
1432                                         angle = 270;
1433                                 else if (is_rail_x_all[0] && is_rail_z_all[1])
1434                                         angle = 180;
1435                                 else if (is_rail_x_all[1] && is_rail_z_all[1])
1436                                         angle = 90;
1437                                 break;
1438                         case 3:
1439                                 // here is where the potential to 'switch' a junction is, but not implemented at present
1440                                 tileindex = 2; // t-junction
1441                                 if(!is_rail_x_all[1])
1442                                         angle = 180;
1443                                 if(!is_rail_z_all[0])
1444                                         angle = 90;
1445                                 if(!is_rail_z_all[1])
1446                                         angle = 270;
1447                                 break;
1448                         case 4:
1449                                 tileindex = 3; // crossing
1450                                 break;
1451                         case 5: //sloped
1452                                 if (is_rail_z[4])
1453                                         angle = 180;
1454                                 if (is_rail_x[4])
1455                                         angle = 90;
1456                                 if (is_rail_x[5])
1457                                         angle = -90;
1458                                 break;
1459                         default:
1460                                 break;
1461                         }
1462
1463                         TileSpec tile = getNodeTileN(n, p, tileindex, data);
1464                         tile.material_flags &= ~MATERIAL_FLAG_BACKFACE_CULLING;
1465                         tile.material_flags |= MATERIAL_FLAG_CRACK_OVERLAY;
1466
1467                         u16 l = getInteriorLight(n, 0, nodedef);
1468                         video::SColor c = MapBlock_LightColor(255, l, f.light_source);
1469
1470                         float d = (float)BS/64;
1471                         float s = BS/2;
1472
1473                         short g = -1;
1474                         if (is_rail_x[4] || is_rail_x[5] || is_rail_z[4] || is_rail_z[5])
1475                                 g = 1; //Object is at a slope
1476
1477                         video::S3DVertex vertices[4] =
1478                         {
1479                                         video::S3DVertex(-s,  -s+d,-s,  0,0,0,  c,0,1),
1480                                         video::S3DVertex( s,  -s+d,-s,  0,0,0,  c,1,1),
1481                                         video::S3DVertex( s, g*s+d, s,  0,0,0,  c,1,0),
1482                                         video::S3DVertex(-s, g*s+d, s,  0,0,0,  c,0,0),
1483                         };
1484
1485                         for(s32 i=0; i<4; i++)
1486                         {
1487                                 if(angle != 0)
1488                                         vertices[i].Pos.rotateXZBy(angle);
1489                                 vertices[i].Pos += intToFloat(p, BS);
1490                         }
1491
1492                         u16 indices[] = {0,1,2,2,3,0};
1493                         collector.append(tile, vertices, 4, indices, 6);
1494                 break;}
1495                 case NDT_NODEBOX:
1496                 {
1497                         static const v3s16 tile_dirs[6] = {
1498                                 v3s16(0, 1, 0),
1499                                 v3s16(0, -1, 0),
1500                                 v3s16(1, 0, 0),
1501                                 v3s16(-1, 0, 0),
1502                                 v3s16(0, 0, 1),
1503                                 v3s16(0, 0, -1)
1504                         };
1505                         TileSpec tiles[6];
1506
1507                         u16 l = getInteriorLight(n, 1, nodedef);
1508                         video::SColor c = MapBlock_LightColor(255, l, f.light_source);
1509
1510                         v3f pos = intToFloat(p, BS);
1511
1512                         int neighbors = 0;
1513
1514                         // locate possible neighboring nodes to connect to
1515                         if (f.node_box.type == NODEBOX_CONNECTED) {
1516                                 v3s16 p2 = p;
1517
1518                                 p2.Y++;
1519                                 getNeighborConnectingFace(blockpos_nodes + p2, nodedef, data, n, 1, &neighbors);
1520
1521                                 p2 = p;
1522                                 p2.Y--;
1523                                 getNeighborConnectingFace(blockpos_nodes + p2, nodedef, data, n, 2, &neighbors);
1524
1525                                 p2 = p;
1526                                 p2.Z--;
1527                                 getNeighborConnectingFace(blockpos_nodes + p2, nodedef, data, n, 4, &neighbors);
1528
1529                                 p2 = p;
1530                                 p2.X--;
1531                                 getNeighborConnectingFace(blockpos_nodes + p2, nodedef, data, n, 8, &neighbors);
1532
1533                                 p2 = p;
1534                                 p2.Z++;
1535                                 getNeighborConnectingFace(blockpos_nodes + p2, nodedef, data, n, 16, &neighbors);
1536
1537                                 p2 = p;
1538                                 p2.X++;
1539                                 getNeighborConnectingFace(blockpos_nodes + p2, nodedef, data, n, 32, &neighbors);
1540                         }
1541
1542                         std::vector<aabb3f> boxes;
1543                         n.getNodeBoxes(nodedef, &boxes, neighbors);
1544                         for(std::vector<aabb3f>::iterator
1545                                         i = boxes.begin();
1546                                         i != boxes.end(); ++i)
1547                         {
1548                         for(int j = 0; j < 6; j++)
1549                                 {
1550                                 // Handles facedir rotation for textures
1551                                 tiles[j] = getNodeTile(n, p, tile_dirs[j], data);
1552                                 }
1553                                 aabb3f box = *i;
1554                                 box.MinEdge += pos;
1555                                 box.MaxEdge += pos;
1556
1557                                 f32 temp;
1558                                 if (box.MinEdge.X > box.MaxEdge.X)
1559                                 {
1560                                         temp=box.MinEdge.X;
1561                                         box.MinEdge.X=box.MaxEdge.X;
1562                                         box.MaxEdge.X=temp;
1563                                 }
1564                                 if (box.MinEdge.Y > box.MaxEdge.Y)
1565                                 {
1566                                         temp=box.MinEdge.Y;
1567                                         box.MinEdge.Y=box.MaxEdge.Y;
1568                                         box.MaxEdge.Y=temp;
1569                                 }
1570                                 if (box.MinEdge.Z > box.MaxEdge.Z)
1571                                 {
1572                                         temp=box.MinEdge.Z;
1573                                         box.MinEdge.Z=box.MaxEdge.Z;
1574                                         box.MaxEdge.Z=temp;
1575                                 }
1576
1577                                 //
1578                                 // Compute texture coords
1579                                 f32 tx1 = (box.MinEdge.X/BS)+0.5;
1580                                 f32 ty1 = (box.MinEdge.Y/BS)+0.5;
1581                                 f32 tz1 = (box.MinEdge.Z/BS)+0.5;
1582                                 f32 tx2 = (box.MaxEdge.X/BS)+0.5;
1583                                 f32 ty2 = (box.MaxEdge.Y/BS)+0.5;
1584                                 f32 tz2 = (box.MaxEdge.Z/BS)+0.5;
1585                                 f32 txc[24] = {
1586                                         // up
1587                                         tx1, 1-tz2, tx2, 1-tz1,
1588                                         // down
1589                                         tx1, tz1, tx2, tz2,
1590                                         // right
1591                                         tz1, 1-ty2, tz2, 1-ty1,
1592                                         // left
1593                                         1-tz2, 1-ty2, 1-tz1, 1-ty1,
1594                                         // back
1595                                         1-tx2, 1-ty2, 1-tx1, 1-ty1,
1596                                         // front
1597                                         tx1, 1-ty2, tx2, 1-ty1,
1598                                 };
1599                                 makeCuboid(&collector, box, tiles, 6, c, txc);
1600                         }
1601                 break;}
1602                 case NDT_MESH:
1603                 {
1604                         v3f pos = intToFloat(p, BS);
1605                         video::SColor c = MapBlock_LightColor(255, getInteriorLight(n, 1, nodedef), f.light_source);
1606
1607                         u8 facedir = 0;
1608                         if (f.param_type_2 == CPT2_FACEDIR) {
1609                                 facedir = n.getFaceDir(nodedef);
1610                         } else if (f.param_type_2 == CPT2_WALLMOUNTED) {
1611                                 //convert wallmounted to 6dfacedir.
1612                                 //when cache enabled, it is already converted
1613                                 facedir = n.getWallMounted(nodedef);
1614                                 if (!enable_mesh_cache) {
1615                                         static const u8 wm_to_6d[6] = {20, 0, 16+1, 12+3, 8, 4+2};
1616                                         facedir = wm_to_6d[facedir];
1617                                 }
1618                         }
1619
1620                         if (f.mesh_ptr[facedir]) {
1621                                 // use cached meshes
1622                                 for(u16 j = 0; j < f.mesh_ptr[0]->getMeshBufferCount(); j++) {
1623                                         scene::IMeshBuffer *buf = f.mesh_ptr[facedir]->getMeshBuffer(j);
1624                                         collector.append(getNodeTileN(n, p, j, data),
1625                                                 (video::S3DVertex *)buf->getVertices(), buf->getVertexCount(),
1626                                                 buf->getIndices(), buf->getIndexCount(), pos, c);
1627                                 }
1628                         } else if (f.mesh_ptr[0]) {
1629                                 // no cache, clone and rotate mesh
1630                                 scene::IMesh* mesh = cloneMesh(f.mesh_ptr[0]);
1631                                 rotateMeshBy6dFacedir(mesh, facedir);
1632                                 recalculateBoundingBox(mesh);
1633                                 meshmanip->recalculateNormals(mesh, true, false);
1634                                 for(u16 j = 0; j < mesh->getMeshBufferCount(); j++) {
1635                                         scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
1636                                         collector.append(getNodeTileN(n, p, j, data),
1637                                                 (video::S3DVertex *)buf->getVertices(), buf->getVertexCount(),
1638                                                 buf->getIndices(), buf->getIndexCount(), pos, c);
1639                                 }
1640                                 mesh->drop();
1641                         }
1642                 break;}
1643                 }
1644         }
1645 }
1646