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