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