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