]> git.lizzy.rs Git - minetest.git/blob - src/content_mapblock.cpp
Remove texture atlas / AtlasPointer, rename getTextureRaw to getTexture
[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, data);
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, data);
393                         // Otherwise use the light of this node (the liquid)
394                         else
395                                 l = getInteriorLight(n, 0, data);
396                         video::SColor c = MapBlock_LightColor(f.alpha, l, decode_light(f.light_source));
397                         
398                         // Neighbor liquid levels (key = relative position)
399                         // Includes current node
400                         std::map<v3s16, f32> neighbor_levels;
401                         std::map<v3s16, content_t> neighbor_contents;
402                         std::map<v3s16, u8> neighbor_flags;
403                         const u8 neighborflag_top_is_same_liquid = 0x01;
404                         v3s16 neighbor_dirs[9] = {
405                                 v3s16(0,0,0),
406                                 v3s16(0,0,1),
407                                 v3s16(0,0,-1),
408                                 v3s16(1,0,0),
409                                 v3s16(-1,0,0),
410                                 v3s16(1,0,1),
411                                 v3s16(-1,0,-1),
412                                 v3s16(1,0,-1),
413                                 v3s16(-1,0,1),
414                         };
415                         for(u32 i=0; i<9; i++)
416                         {
417                                 content_t content = CONTENT_AIR;
418                                 float level = -0.5 * BS;
419                                 u8 flags = 0;
420                                 // Check neighbor
421                                 v3s16 p2 = p + neighbor_dirs[i];
422                                 MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
423                                 if(n2.getContent() != CONTENT_IGNORE)
424                                 {
425                                         content = n2.getContent();
426
427                                         if(n2.getContent() == c_source)
428                                                 level = (-0.5+node_liquid_level) * BS;
429                                         else if(n2.getContent() == c_flowing)
430                                                 level = (-0.5 + ((float)(n2.param2&LIQUID_LEVEL_MASK)
431                                                                 + 0.5) / (float)LIQUID_LEVEL_SOURCE * node_liquid_level) * BS;
432
433                                         // Check node above neighbor.
434                                         // NOTE: This doesn't get executed if neighbor
435                                         //       doesn't exist
436                                         p2.Y += 1;
437                                         n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
438                                         if(n2.getContent() == c_source ||
439                                                         n2.getContent() == c_flowing)
440                                                 flags |= neighborflag_top_is_same_liquid;
441                                 }
442                                 
443                                 neighbor_levels[neighbor_dirs[i]] = level;
444                                 neighbor_contents[neighbor_dirs[i]] = content;
445                                 neighbor_flags[neighbor_dirs[i]] = flags;
446                         }
447
448                         // Corner heights (average between four liquids)
449                         f32 corner_levels[4];
450                         
451                         v3s16 halfdirs[4] = {
452                                 v3s16(0,0,0),
453                                 v3s16(1,0,0),
454                                 v3s16(1,0,1),
455                                 v3s16(0,0,1),
456                         };
457                         for(u32 i=0; i<4; i++)
458                         {
459                                 v3s16 cornerdir = halfdirs[i];
460                                 float cornerlevel = 0;
461                                 u32 valid_count = 0;
462                                 u32 air_count = 0;
463                                 for(u32 j=0; j<4; j++)
464                                 {
465                                         v3s16 neighbordir = cornerdir - halfdirs[j];
466                                         content_t content = neighbor_contents[neighbordir];
467                                         // If top is liquid, draw starting from top of node
468                                         if(neighbor_flags[neighbordir] &
469                                                         neighborflag_top_is_same_liquid)
470                                         {
471                                                 cornerlevel = 0.5*BS;
472                                                 valid_count = 1;
473                                                 break;
474                                         }
475                                         // Source is always the same height
476                                         else if(content == c_source)
477                                         {
478                                                 cornerlevel = (-0.5+node_liquid_level)*BS;
479                                                 valid_count = 1;
480                                                 break;
481                                         }
482                                         // Flowing liquid has level information
483                                         else if(content == c_flowing)
484                                         {
485                                                 cornerlevel += neighbor_levels[neighbordir];
486                                                 valid_count++;
487                                         }
488                                         else if(content == CONTENT_AIR)
489                                         {
490                                                 air_count++;
491                                         }
492                                 }
493                                 if(air_count >= 2)
494                                         cornerlevel = -0.5*BS+0.2;
495                                 else if(valid_count > 0)
496                                         cornerlevel /= valid_count;
497                                 corner_levels[i] = cornerlevel;
498                         }
499
500                         /*
501                                 Generate sides
502                         */
503
504                         v3s16 side_dirs[4] = {
505                                 v3s16(1,0,0),
506                                 v3s16(-1,0,0),
507                                 v3s16(0,0,1),
508                                 v3s16(0,0,-1),
509                         };
510                         s16 side_corners[4][2] = {
511                                 {1, 2},
512                                 {3, 0},
513                                 {2, 3},
514                                 {0, 1},
515                         };
516                         for(u32 i=0; i<4; i++)
517                         {
518                                 v3s16 dir = side_dirs[i];
519
520                                 /*
521                                         If our topside is liquid and neighbor's topside
522                                         is liquid, don't draw side face
523                                 */
524                                 if(top_is_same_liquid &&
525                                                 neighbor_flags[dir] & neighborflag_top_is_same_liquid)
526                                         continue;
527
528                                 content_t neighbor_content = neighbor_contents[dir];
529                                 const ContentFeatures &n_feat = nodedef->get(neighbor_content);
530                                 
531                                 // Don't draw face if neighbor is blocking the view
532                                 if(n_feat.solidness == 2)
533                                         continue;
534                                 
535                                 bool neighbor_is_same_liquid = (neighbor_content == c_source
536                                                 || neighbor_content == c_flowing);
537                                 
538                                 // Don't draw any faces if neighbor same is liquid and top is
539                                 // same liquid
540                                 if(neighbor_is_same_liquid == true
541                                                 && top_is_same_liquid == false)
542                                         continue;
543
544                                 // Use backface culled material if neighbor doesn't have a
545                                 // solidness of 0
546                                 const TileSpec *current_tile = &tile_liquid;
547                                 if(n_feat.solidness != 0 || n_feat.visual_solidness != 0)
548                                         current_tile = &tile_liquid_bfculled;
549                                 
550                                 video::S3DVertex vertices[4] =
551                                 {
552                                         video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c, 0,1),
553                                         video::S3DVertex(BS/2,0,BS/2, 0,0,0, c, 1,1),
554                                         video::S3DVertex(BS/2,0,BS/2, 0,0,0, c, 1,0),
555                                         video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c, 0,0),
556                                 };
557                                 
558                                 /*
559                                         If our topside is liquid, set upper border of face
560                                         at upper border of node
561                                 */
562                                 if(top_is_same_liquid)
563                                 {
564                                         vertices[2].Pos.Y = 0.5*BS;
565                                         vertices[3].Pos.Y = 0.5*BS;
566                                 }
567                                 /*
568                                         Otherwise upper position of face is corner levels
569                                 */
570                                 else
571                                 {
572                                         vertices[2].Pos.Y = corner_levels[side_corners[i][0]];
573                                         vertices[3].Pos.Y = corner_levels[side_corners[i][1]];
574                                 }
575                                 
576                                 /*
577                                         If neighbor is liquid, lower border of face is corner
578                                         liquid levels
579                                 */
580                                 if(neighbor_is_same_liquid)
581                                 {
582                                         vertices[0].Pos.Y = corner_levels[side_corners[i][1]];
583                                         vertices[1].Pos.Y = corner_levels[side_corners[i][0]];
584                                 }
585                                 /*
586                                         If neighbor is not liquid, lower border of face is
587                                         lower border of node
588                                 */
589                                 else
590                                 {
591                                         vertices[0].Pos.Y = -0.5*BS;
592                                         vertices[1].Pos.Y = -0.5*BS;
593                                 }
594                                 
595                                 for(s32 j=0; j<4; j++)
596                                 {
597                                         if(dir == v3s16(0,0,1))
598                                                 vertices[j].Pos.rotateXZBy(0);
599                                         if(dir == v3s16(0,0,-1))
600                                                 vertices[j].Pos.rotateXZBy(180);
601                                         if(dir == v3s16(-1,0,0))
602                                                 vertices[j].Pos.rotateXZBy(90);
603                                         if(dir == v3s16(1,0,-0))
604                                                 vertices[j].Pos.rotateXZBy(-90);
605                                                 
606                                         // Do this to not cause glitches when two liquids are
607                                         // side-by-side
608                                         /*if(neighbor_is_same_liquid == false){
609                                                 vertices[j].Pos.X *= 0.98;
610                                                 vertices[j].Pos.Z *= 0.98;
611                                         }*/
612
613                                         vertices[j].Pos += intToFloat(p, BS);
614                                 }
615
616                                 u16 indices[] = {0,1,2,2,3,0};
617                                 // Add to mesh collector
618                                 collector.append(*current_tile, vertices, 4, indices, 6);
619                         }
620                         
621                         /*
622                                 Generate top side, if appropriate
623                         */
624                         
625                         if(top_is_same_liquid == false)
626                         {
627                                 video::S3DVertex vertices[4] =
628                                 {
629                                         video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c, 0,1),
630                                         video::S3DVertex(BS/2,0,BS/2, 0,0,0, c, 1,1),
631                                         video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c, 1,0),
632                                         video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c, 0,0),
633                                 };
634                                 
635                                 // To get backface culling right, the vertices need to go
636                                 // clockwise around the front of the face. And we happened to
637                                 // calculate corner levels in exact reverse order.
638                                 s32 corner_resolve[4] = {3,2,1,0};
639
640                                 for(s32 i=0; i<4; i++)
641                                 {
642                                         //vertices[i].Pos.Y += liquid_level;
643                                         //vertices[i].Pos.Y += neighbor_levels[v3s16(0,0,0)];
644                                         s32 j = corner_resolve[i];
645                                         vertices[i].Pos.Y += corner_levels[j];
646                                         vertices[i].Pos += intToFloat(p, BS);
647                                 }
648                                 
649                                 // Default downwards-flowing texture animation goes from 
650                                 // -Z towards +Z, thus the direction is +Z.
651                                 // Rotate texture to make animation go in flow direction
652                                 // Positive if liquid moves towards +Z
653                                 int dz = (corner_levels[side_corners[3][0]] +
654                                                 corner_levels[side_corners[3][1]]) -
655                                                 (corner_levels[side_corners[2][0]] +
656                                                 corner_levels[side_corners[2][1]]);
657                                 // Positive if liquid moves towards +X
658                                 int dx = (corner_levels[side_corners[1][0]] +
659                                                 corner_levels[side_corners[1][1]]) -
660                                                 (corner_levels[side_corners[0][0]] +
661                                                 corner_levels[side_corners[0][1]]);
662                                 // -X
663                                 if(-dx >= abs(dz))
664                                 {
665                                         v2f t = vertices[0].TCoords;
666                                         vertices[0].TCoords = vertices[1].TCoords;
667                                         vertices[1].TCoords = vertices[2].TCoords;
668                                         vertices[2].TCoords = vertices[3].TCoords;
669                                         vertices[3].TCoords = t;
670                                 }
671                                 // +X
672                                 if(dx >= abs(dz))
673                                 {
674                                         v2f t = vertices[0].TCoords;
675                                         vertices[0].TCoords = vertices[3].TCoords;
676                                         vertices[3].TCoords = vertices[2].TCoords;
677                                         vertices[2].TCoords = vertices[1].TCoords;
678                                         vertices[1].TCoords = t;
679                                 }
680                                 // -Z
681                                 if(-dz >= abs(dx))
682                                 {
683                                         v2f t = vertices[0].TCoords;
684                                         vertices[0].TCoords = vertices[3].TCoords;
685                                         vertices[3].TCoords = vertices[2].TCoords;
686                                         vertices[2].TCoords = vertices[1].TCoords;
687                                         vertices[1].TCoords = t;
688                                         t = vertices[0].TCoords;
689                                         vertices[0].TCoords = vertices[3].TCoords;
690                                         vertices[3].TCoords = vertices[2].TCoords;
691                                         vertices[2].TCoords = vertices[1].TCoords;
692                                         vertices[1].TCoords = t;
693                                 }
694
695                                 u16 indices[] = {0,1,2,2,3,0};
696                                 // Add to mesh collector
697                                 collector.append(tile_liquid, vertices, 4, indices, 6);
698                         }
699                 break;}
700                 case NDT_GLASSLIKE:
701                 {
702                         TileSpec tile = getNodeTile(n, p, v3s16(0,0,0), data);
703
704                         u16 l = getInteriorLight(n, 1, data);
705                         video::SColor c = MapBlock_LightColor(255, l, decode_light(f.light_source));
706
707                         for(u32 j=0; j<6; j++)
708                         {
709                                 // Check this neighbor
710                                 v3s16 n2p = blockpos_nodes + p + g_6dirs[j];
711                                 MapNode n2 = data->m_vmanip.getNodeNoEx(n2p);
712                                 // Don't make face if neighbor is of same type
713                                 if(n2.getContent() == n.getContent())
714                                         continue;
715
716                                 // The face at Z+
717                                 video::S3DVertex vertices[4] = {
718                                         video::S3DVertex(-BS/2,-BS/2,BS/2, 0,0,0, c, 0,1),
719                                         video::S3DVertex(BS/2,-BS/2,BS/2, 0,0,0, c, 1,1),
720                                         video::S3DVertex(BS/2,BS/2,BS/2, 0,0,0, c, 1,0),
721                                         video::S3DVertex(-BS/2,BS/2,BS/2, 0,0,0, c, 0,0),
722                                 };
723                                 
724                                 // Rotations in the g_6dirs format
725                                 if(j == 0) // Z+
726                                         for(u16 i=0; i<4; i++)
727                                                 vertices[i].Pos.rotateXZBy(0);
728                                 else if(j == 1) // Y+
729                                         for(u16 i=0; i<4; i++)
730                                                 vertices[i].Pos.rotateYZBy(-90);
731                                 else if(j == 2) // X+
732                                         for(u16 i=0; i<4; i++)
733                                                 vertices[i].Pos.rotateXZBy(-90);
734                                 else if(j == 3) // Z-
735                                         for(u16 i=0; i<4; i++)
736                                                 vertices[i].Pos.rotateXZBy(180);
737                                 else if(j == 4) // Y-
738                                         for(u16 i=0; i<4; i++)
739                                                 vertices[i].Pos.rotateYZBy(90);
740                                 else if(j == 5) // X-
741                                         for(u16 i=0; i<4; i++)
742                                                 vertices[i].Pos.rotateXZBy(90);
743
744                                 for(u16 i=0; i<4; i++){
745                                         vertices[i].Pos += intToFloat(p, BS);
746                                 }
747
748                                 u16 indices[] = {0,1,2,2,3,0};
749                                 // Add to mesh collector
750                                 collector.append(tile, vertices, 4, indices, 6);
751                         }
752                 break;}
753                 case NDT_GLASSLIKE_FRAMED:
754                 {
755                         static const v3s16 dirs[6] = {
756                                 v3s16( 0, 1, 0),
757                                 v3s16( 0,-1, 0),
758                                 v3s16( 1, 0, 0),
759                                 v3s16(-1, 0, 0),
760                                 v3s16( 0, 0, 1),
761                                 v3s16( 0, 0,-1)
762                         };
763                         TileSpec tiles[2];
764                         tiles[0] = getNodeTile(n, p, dirs[0], data);
765                         tiles[1] = getNodeTile(n, p, dirs[1], data);
766                         u16 l = getInteriorLight(n, 1, data);
767                         video::SColor c = MapBlock_LightColor(255, l, decode_light(f.light_source));
768                         v3f pos = intToFloat(p, BS);
769                         static const float a=BS/2;
770                         static const float b=.876*(BS/2);
771                         static const aabb3f frame_edges[12] = {
772                                 aabb3f( b, b,-a, a, a, a), // y+
773                                 aabb3f(-a, b,-a,-b, a, a), // y+
774                                 aabb3f( b,-a,-a, a,-b, a), // y-
775                                 aabb3f(-a,-a,-a,-b,-b, a), // y-
776                                 aabb3f( b,-a, b, a, a, a), // x+
777                                 aabb3f( b,-a,-a, a, a,-b), // x+
778                                 aabb3f(-a,-a, b,-b, a, a), // x-
779                                 aabb3f(-a,-a,-a,-b, a,-b), // x-
780                                 aabb3f(-a, b, b, a, a, a), // z+
781                                 aabb3f(-a,-a, b, a,-b, a), // z+
782                                 aabb3f(-a,-a,-a, a,-b,-b), // z-
783                                 aabb3f(-a, b,-a, a, a,-b)  // z-
784                         };
785                         aabb3f glass_faces[6] = {
786                                 aabb3f(-a, a,-a, a, a, a), // y+
787                                 aabb3f(-a,-a,-a, a,-a, a), // y-
788                                 aabb3f( a,-a,-a, a, a, a), // x+
789                                 aabb3f(-a,-a,-a,-a, a, a), // x-
790                                 aabb3f(-a,-a, a, a, a, a), // z+
791                                 aabb3f(-a,-a,-a, a, a,-a)  // z-
792                         };
793                         
794                         int visible_faces[6] = {0,0,0,0,0,0};
795                         int nb[18] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
796                         u8 i;
797                         content_t current = n.getContent();
798                         content_t content;
799                         MapNode n2;
800                         v3s16 n2p;
801                         for(i=0; i<18; i++)
802                         {
803                                 n2p = blockpos_nodes + p + g_26dirs[i];
804                                 n2 = data->m_vmanip.getNodeNoEx(n2p);
805                                 content_t n2c = n2.getContent();
806                                 //TODO: remove CONTENT_IGNORE check when getNodeNoEx is fixed
807                                 if (n2c == current || n2c == CONTENT_IGNORE)
808                                         nb[i]=1;
809                         }
810                         for(i=0; i<6; i++)
811                         {
812                                 n2p = blockpos_nodes + p + dirs[i];
813                                 n2 = data->m_vmanip.getNodeNoEx(n2p);
814                                 content = n2.getContent();
815                                 const ContentFeatures &f2 = nodedef->get(content);
816                                 if (content == CONTENT_AIR || f2.isLiquid())
817                                         visible_faces[i]=1;
818                         }
819                         static const u8 nb_triplet[12*3] = {
820                                 1,2, 7,  1,5, 6,  4,2,15,  4,5,14,
821                                 2,0,11,  2,3,13,  5,0,10,  5,3,12,
822                                 0,1, 8,  0,4,16,  3,4,17,  3,1, 9
823                         };
824
825                         f32 tx1,ty1,tz1,tx2,ty2,tz2;
826                         aabb3f box;
827                         for(i=0; i<12; i++)
828                         {
829                                 int edge_invisible;
830                                 if (nb[nb_triplet[i*3+2]]==1)
831                                         edge_invisible=nb[nb_triplet[i*3]] & nb[nb_triplet[i*3+1]];
832                                 else
833                                         edge_invisible=nb[nb_triplet[i*3]] ^ nb[nb_triplet[i*3+1]];
834                                 if (edge_invisible)
835                                         continue;
836                                 box=frame_edges[i];
837                                 box.MinEdge += pos;
838                                 box.MaxEdge += pos;
839                                 tx1 = (box.MinEdge.X/BS)+0.5;
840                                 ty1 = (box.MinEdge.Y/BS)+0.5;
841                                 tz1 = (box.MinEdge.Z/BS)+0.5;
842                                 tx2 = (box.MaxEdge.X/BS)+0.5;
843                                 ty2 = (box.MaxEdge.Y/BS)+0.5;
844                                 tz2 = (box.MaxEdge.Z/BS)+0.5;
845                                 f32 txc1[24] = {
846                                         tx1,   1-tz2,   tx2, 1-tz1,
847                                         tx1,     tz1,   tx2,   tz2,
848                                         tz1,   1-ty2,   tz2, 1-ty1,
849                                         1-tz2, 1-ty2, 1-tz1, 1-ty1,
850                                         1-tx2, 1-ty2, 1-tx1, 1-ty1,
851                                         tx1,   1-ty2,   tx2, 1-ty1,
852                                 };
853                                 makeCuboid(&collector, box, &tiles[0], 1, c, txc1);
854                         }
855                         for(i=0; i<6; i++)
856                         {
857                                 if (visible_faces[i]==0)
858                                         continue;
859                                 box=glass_faces[i];
860                                 box.MinEdge += pos;
861                                 box.MaxEdge += pos;
862                                 tx1 = (box.MinEdge.X/BS)+0.5;
863                                 ty1 = (box.MinEdge.Y/BS)+0.5;
864                                 tz1 = (box.MinEdge.Z/BS)+0.5;
865                                 tx2 = (box.MaxEdge.X/BS)+0.5;
866                                 ty2 = (box.MaxEdge.Y/BS)+0.5;
867                                 tz2 = (box.MaxEdge.Z/BS)+0.5;
868                                 f32 txc2[24] = {
869                                         tx1,   1-tz2,   tx2, 1-tz1,
870                                         tx1,     tz1,   tx2,   tz2,
871                                         tz1,   1-ty2,   tz2, 1-ty1,
872                                         1-tz2, 1-ty2, 1-tz1, 1-ty1,
873                                         1-tx2, 1-ty2, 1-tx1, 1-ty1,
874                                         tx1,   1-ty2,   tx2, 1-ty1,
875                                 };
876                                 makeCuboid(&collector, box, &tiles[1], 1, c, txc2);
877                         }
878                 break;}
879                 case NDT_ALLFACES:
880                 {
881                         TileSpec tile_leaves = getNodeTile(n, p,
882                                         v3s16(0,0,0), data);
883
884                         u16 l = getInteriorLight(n, 1, data);
885                         video::SColor c = MapBlock_LightColor(255, l, decode_light(f.light_source));
886
887                         v3f pos = intToFloat(p, BS);
888                         aabb3f box(-BS/2,-BS/2,-BS/2,BS/2,BS/2,BS/2);
889                         box.MinEdge += pos;
890                         box.MaxEdge += pos;
891                         makeCuboid(&collector, box, &tile_leaves, 1, c, NULL);
892                 break;}
893                 case NDT_ALLFACES_OPTIONAL:
894                         // This is always pre-converted to something else
895                         assert(0);
896                         break;
897                 case NDT_TORCHLIKE:
898                 {
899                         v3s16 dir = n.getWallMountedDir(nodedef);
900                         
901                         u8 tileindex = 0;
902                         if(dir == v3s16(0,-1,0)){
903                                 tileindex = 0; // floor
904                         } else if(dir == v3s16(0,1,0)){
905                                 tileindex = 1; // ceiling
906                         // For backwards compatibility
907                         } else if(dir == v3s16(0,0,0)){
908                                 tileindex = 0; // floor
909                         } else {
910                                 tileindex = 2; // side
911                         }
912
913                         TileSpec tile = getNodeTileN(n, p, tileindex, data);
914                         tile.material_flags &= ~MATERIAL_FLAG_BACKFACE_CULLING;
915                         tile.material_flags |= MATERIAL_FLAG_CRACK_OVERLAY;
916
917                         u16 l = getInteriorLight(n, 1, data);
918                         video::SColor c = MapBlock_LightColor(255, l, decode_light(f.light_source));
919
920                         // Wall at X+ of node
921                         video::S3DVertex vertices[4] =
922                         {
923                                 video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c, 0,1),
924                                 video::S3DVertex(BS/2,-BS/2,0, 0,0,0, c, 1,1),
925                                 video::S3DVertex(BS/2,BS/2,0, 0,0,0, c, 1,0),
926                                 video::S3DVertex(-BS/2,BS/2,0, 0,0,0, c, 0,0),
927                         };
928
929                         for(s32 i=0; i<4; i++)
930                         {
931                                 if(dir == v3s16(1,0,0))
932                                         vertices[i].Pos.rotateXZBy(0);
933                                 if(dir == v3s16(-1,0,0))
934                                         vertices[i].Pos.rotateXZBy(180);
935                                 if(dir == v3s16(0,0,1))
936                                         vertices[i].Pos.rotateXZBy(90);
937                                 if(dir == v3s16(0,0,-1))
938                                         vertices[i].Pos.rotateXZBy(-90);
939                                 if(dir == v3s16(0,-1,0))
940                                         vertices[i].Pos.rotateXZBy(45);
941                                 if(dir == v3s16(0,1,0))
942                                         vertices[i].Pos.rotateXZBy(-45);
943
944                                 vertices[i].Pos += intToFloat(p, BS);
945                         }
946
947                         u16 indices[] = {0,1,2,2,3,0};
948                         // Add to mesh collector
949                         collector.append(tile, vertices, 4, indices, 6);
950                 break;}
951                 case NDT_SIGNLIKE:
952                 {
953                         TileSpec tile = getNodeTileN(n, p, 0, data);
954                         tile.material_flags &= ~MATERIAL_FLAG_BACKFACE_CULLING;
955                         tile.material_flags |= MATERIAL_FLAG_CRACK_OVERLAY;
956
957                         u16 l = getInteriorLight(n, 0, data);
958                         video::SColor c = MapBlock_LightColor(255, l, decode_light(f.light_source));
959                                 
960                         float d = (float)BS/16;
961                         // Wall at X+ of node
962                         video::S3DVertex vertices[4] =
963                         {
964                                 video::S3DVertex(BS/2-d,BS/2,BS/2, 0,0,0, c, 0,0),
965                                 video::S3DVertex(BS/2-d,BS/2,-BS/2, 0,0,0, c, 1,0),
966                                 video::S3DVertex(BS/2-d,-BS/2,-BS/2, 0,0,0, c, 1,1),
967                                 video::S3DVertex(BS/2-d,-BS/2,BS/2, 0,0,0, c, 0,1),
968                         };
969
970                         v3s16 dir = n.getWallMountedDir(nodedef);
971
972                         for(s32 i=0; i<4; i++)
973                         {
974                                 if(dir == v3s16(1,0,0))
975                                         vertices[i].Pos.rotateXZBy(0);
976                                 if(dir == v3s16(-1,0,0))
977                                         vertices[i].Pos.rotateXZBy(180);
978                                 if(dir == v3s16(0,0,1))
979                                         vertices[i].Pos.rotateXZBy(90);
980                                 if(dir == v3s16(0,0,-1))
981                                         vertices[i].Pos.rotateXZBy(-90);
982                                 if(dir == v3s16(0,-1,0))
983                                         vertices[i].Pos.rotateXYBy(-90);
984                                 if(dir == v3s16(0,1,0))
985                                         vertices[i].Pos.rotateXYBy(90);
986
987                                 vertices[i].Pos += intToFloat(p, BS);
988                         }
989
990                         u16 indices[] = {0,1,2,2,3,0};
991                         // Add to mesh collector
992                         collector.append(tile, vertices, 4, indices, 6);
993                 break;}
994                 case NDT_PLANTLIKE:
995                 {
996                         TileSpec tile = getNodeTileN(n, p, 0, data);
997                         tile.material_flags |= MATERIAL_FLAG_CRACK_OVERLAY;
998                         
999                         u16 l = getInteriorLight(n, 1, data);
1000                         video::SColor c = MapBlock_LightColor(255, l, decode_light(f.light_source));
1001
1002                         for(u32 j=0; j<2; j++)
1003                         {
1004                                 video::S3DVertex vertices[4] =
1005                                 {
1006                                         video::S3DVertex(-BS/2*f.visual_scale,-BS/2,0, 0,0,0, c, 0,1),
1007                                         video::S3DVertex( BS/2*f.visual_scale,-BS/2,0, 0,0,0, c, 1,1),
1008                                         video::S3DVertex( BS/2*f.visual_scale,
1009                                                 -BS/2 + f.visual_scale*BS,0, 0,0,0, c, 1,0),
1010                                         video::S3DVertex(-BS/2*f.visual_scale,
1011                                                 -BS/2 + f.visual_scale*BS,0, 0,0,0, c, 0,0),
1012                                 };
1013
1014                                 if(j == 0)
1015                                 {
1016                                         for(u16 i=0; i<4; i++)
1017                                                 vertices[i].Pos.rotateXZBy(45);
1018                                 }
1019                                 else if(j == 1)
1020                                 {
1021                                         for(u16 i=0; i<4; i++)
1022                                                 vertices[i].Pos.rotateXZBy(-45);
1023                                 }
1024
1025                                 for(u16 i=0; i<4; i++)
1026                                 {
1027                                         vertices[i].Pos *= f.visual_scale;
1028                                         vertices[i].Pos += intToFloat(p, BS);
1029                                 }
1030
1031                                 u16 indices[] = {0,1,2,2,3,0};
1032                                 // Add to mesh collector
1033                                 collector.append(tile, vertices, 4, indices, 6);
1034                         }
1035                 break;}
1036                 case NDT_FENCELIKE:
1037                 {
1038                         TileSpec tile = getNodeTile(n, p, v3s16(0,0,0), data);
1039                         TileSpec tile_nocrack = tile;
1040                         tile_nocrack.material_flags &= ~MATERIAL_FLAG_CRACK;
1041                         
1042                         // A hack to put wood the right way around in the posts
1043                         ITextureSource *tsrc = data->m_gamedef->tsrc();
1044                         std::string texturestring_rot = tsrc->getTextureName(
1045                                         tile.texture_id) + "^[transformR90";
1046                         TileSpec tile_rot = tile;
1047                         tile_rot.texture = tsrc->getTexture(
1048                                         texturestring_rot,
1049                                         &tile_rot.texture_id);
1050                         
1051                         u16 l = getInteriorLight(n, 1, data);
1052                         video::SColor c = MapBlock_LightColor(255, l, decode_light(f.light_source));
1053
1054                         const f32 post_rad=(f32)BS/8;
1055                         const f32 bar_rad=(f32)BS/16;
1056                         const f32 bar_len=(f32)(BS/2)-post_rad;
1057
1058                         v3f pos = intToFloat(p, BS);
1059
1060                         // The post - always present
1061                         aabb3f post(-post_rad,-BS/2,-post_rad,post_rad,BS/2,post_rad);
1062                         post.MinEdge += pos;
1063                         post.MaxEdge += pos;
1064                         f32 postuv[24]={
1065                                         6/16.,6/16.,10/16.,10/16.,
1066                                         6/16.,6/16.,10/16.,10/16.,
1067                                         0/16.,0,4/16.,1,
1068                                         4/16.,0,8/16.,1,
1069                                         8/16.,0,12/16.,1,
1070                                         12/16.,0,16/16.,1};
1071                         makeCuboid(&collector, post, &tile_rot, 1, c, postuv);
1072
1073                         // Now a section of fence, +X, if there's a post there
1074                         v3s16 p2 = p;
1075                         p2.X++;
1076                         MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
1077                         const ContentFeatures *f2 = &nodedef->get(n2);
1078                         if(f2->drawtype == NDT_FENCELIKE)
1079                         {
1080                                 aabb3f bar(-bar_len+BS/2,-bar_rad+BS/4,-bar_rad,
1081                                                 bar_len+BS/2,bar_rad+BS/4,bar_rad);
1082                                 bar.MinEdge += pos;
1083                                 bar.MaxEdge += pos;
1084                                 f32 xrailuv[24]={
1085                                         0/16.,2/16.,16/16.,4/16.,
1086                                         0/16.,4/16.,16/16.,6/16.,
1087                                         6/16.,6/16.,8/16.,8/16.,
1088                                         10/16.,10/16.,12/16.,12/16.,
1089                                         0/16.,8/16.,16/16.,10/16.,
1090                                         0/16.,14/16.,16/16.,16/16.};
1091                                 makeCuboid(&collector, bar, &tile_nocrack, 1,
1092                                                 c, xrailuv);
1093                                 bar.MinEdge.Y -= BS/2;
1094                                 bar.MaxEdge.Y -= BS/2;
1095                                 makeCuboid(&collector, bar, &tile_nocrack, 1,
1096                                                 c, xrailuv);
1097                         }
1098
1099                         // Now a section of fence, +Z, if there's a post there
1100                         p2 = p;
1101                         p2.Z++;
1102                         n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
1103                         f2 = &nodedef->get(n2);
1104                         if(f2->drawtype == NDT_FENCELIKE)
1105                         {
1106                                 aabb3f bar(-bar_rad,-bar_rad+BS/4,-bar_len+BS/2,
1107                                                 bar_rad,bar_rad+BS/4,bar_len+BS/2);
1108                                 bar.MinEdge += pos;
1109                                 bar.MaxEdge += pos;
1110                                 f32 zrailuv[24]={
1111                                         3/16.,1/16.,5/16.,5/16., // cannot rotate; stretch
1112                                         4/16.,1/16.,6/16.,5/16., // for wood texture instead
1113                                         0/16.,9/16.,16/16.,11/16.,
1114                                         0/16.,6/16.,16/16.,8/16.,
1115                                         6/16.,6/16.,8/16.,8/16.,
1116                                         10/16.,10/16.,12/16.,12/16.};
1117                                 makeCuboid(&collector, bar, &tile_nocrack, 1,
1118                                                 c, zrailuv);
1119                                 bar.MinEdge.Y -= BS/2;
1120                                 bar.MaxEdge.Y -= BS/2;
1121                                 makeCuboid(&collector, bar, &tile_nocrack, 1,
1122                                                 c, zrailuv);
1123                         }
1124                 break;}
1125                 case NDT_RAILLIKE:
1126                 {
1127                         bool is_rail_x [] = { false, false };  /* x-1, x+1 */
1128                         bool is_rail_z [] = { false, false };  /* z-1, z+1 */
1129
1130                         bool is_rail_z_minus_y [] = { false, false };  /* z-1, z+1; y-1 */
1131                         bool is_rail_x_minus_y [] = { false, false };  /* x-1, z+1; y-1 */
1132                         bool is_rail_z_plus_y [] = { false, false };  /* z-1, z+1; y+1 */
1133                         bool is_rail_x_plus_y [] = { false, false };  /* x-1, x+1; y+1 */
1134
1135                         MapNode n_minus_x = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x-1,y,z));
1136                         MapNode n_plus_x = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x+1,y,z));
1137                         MapNode n_minus_z = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y,z-1));
1138                         MapNode n_plus_z = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y,z+1));
1139                         MapNode n_plus_x_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x+1, y+1, z));
1140                         MapNode n_plus_x_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x+1, y-1, z));
1141                         MapNode n_minus_x_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x-1, y+1, z));
1142                         MapNode n_minus_x_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x-1, y-1, z));
1143                         MapNode n_plus_z_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y+1, z+1));
1144                         MapNode n_minus_z_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y+1, z-1));
1145                         MapNode n_plus_z_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y-1, z+1));
1146                         MapNode n_minus_z_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y-1, z-1));
1147
1148                         content_t thiscontent = n.getContent();
1149                         std::string groupname = "connect_to_raillike"; // name of the group that enables connecting to raillike nodes of different kind
1150                         bool self_connect_to_raillike = ((ItemGroupList) nodedef->get(n).groups)[groupname] != 0;
1151
1152                         if ((nodedef->get(n_minus_x).drawtype == NDT_RAILLIKE
1153                                         && ((ItemGroupList) nodedef->get(n_minus_x).groups)[groupname] != 0
1154                                         && self_connect_to_raillike)
1155                                         || n_minus_x.getContent() == thiscontent)
1156                                 is_rail_x[0] = true;
1157
1158                         if ((nodedef->get(n_minus_x_minus_y).drawtype == NDT_RAILLIKE
1159                                         && ((ItemGroupList) nodedef->get(n_minus_x_minus_y).groups)[groupname] != 0
1160                                         && self_connect_to_raillike)
1161                                         || n_minus_x_minus_y.getContent() == thiscontent)
1162                                 is_rail_x_minus_y[0] = true;
1163
1164                         if ((nodedef->get(n_minus_x_plus_y).drawtype == NDT_RAILLIKE
1165                                         && ((ItemGroupList) nodedef->get(n_minus_x_plus_y).groups)[groupname] != 0
1166                                         && self_connect_to_raillike)
1167                                         || n_minus_x_plus_y.getContent() == thiscontent)
1168                                 is_rail_x_plus_y[0] = true;
1169
1170                         if ((nodedef->get(n_plus_x).drawtype == NDT_RAILLIKE
1171                                         && ((ItemGroupList) nodedef->get(n_plus_x).groups)[groupname] != 0
1172                                         && self_connect_to_raillike)
1173                                         || n_plus_x.getContent() == thiscontent)
1174                                 is_rail_x[1] = true;
1175
1176                         if ((nodedef->get(n_plus_x_minus_y).drawtype == NDT_RAILLIKE
1177                                         && ((ItemGroupList) nodedef->get(n_plus_x_minus_y).groups)[groupname] != 0
1178                                         && self_connect_to_raillike)
1179                                         || n_plus_x_minus_y.getContent() == thiscontent)
1180                                 is_rail_x_minus_y[1] = true;
1181
1182                         if ((nodedef->get(n_plus_x_plus_y).drawtype == NDT_RAILLIKE
1183                                         && ((ItemGroupList) nodedef->get(n_plus_x_plus_y).groups)[groupname] != 0
1184                                         && self_connect_to_raillike)
1185                                         || n_plus_x_plus_y.getContent() == thiscontent)
1186                                 is_rail_x_plus_y[1] = true;
1187
1188                         if ((nodedef->get(n_minus_z).drawtype == NDT_RAILLIKE
1189                                         && ((ItemGroupList) nodedef->get(n_minus_z).groups)[groupname] != 0
1190                                         && self_connect_to_raillike)
1191                                         || n_minus_z.getContent() == thiscontent)
1192                                 is_rail_z[0] = true;
1193
1194                         if ((nodedef->get(n_minus_z_minus_y).drawtype == NDT_RAILLIKE
1195                                         && ((ItemGroupList) nodedef->get(n_minus_z_minus_y).groups)[groupname] != 0
1196                                         && self_connect_to_raillike)
1197                                         || n_minus_z_minus_y.getContent() == thiscontent)
1198                                 is_rail_z_minus_y[0] = true;
1199
1200                         if ((nodedef->get(n_minus_z_plus_y).drawtype == NDT_RAILLIKE
1201                                         && ((ItemGroupList) nodedef->get(n_minus_z_plus_y).groups)[groupname] != 0
1202                                         && self_connect_to_raillike)
1203                                         || n_minus_z_plus_y.getContent() == thiscontent)
1204                                 is_rail_z_plus_y[0] = true;
1205
1206                         if ((nodedef->get(n_plus_z).drawtype == NDT_RAILLIKE
1207                                         && ((ItemGroupList) nodedef->get(n_plus_z).groups)[groupname] != 0
1208                                         && self_connect_to_raillike)
1209                                         || n_plus_z.getContent() == thiscontent)
1210                                 is_rail_z[1] = true;
1211
1212                         if ((nodedef->get(n_plus_z_minus_y).drawtype == NDT_RAILLIKE
1213                                         && ((ItemGroupList) nodedef->get(n_plus_z_minus_y).groups)[groupname] != 0
1214                                         && self_connect_to_raillike)
1215                                         || n_plus_z_minus_y.getContent() == thiscontent)
1216                                 is_rail_z_minus_y[1] = true;
1217
1218                         if ((nodedef->get(n_plus_z_plus_y).drawtype == NDT_RAILLIKE
1219                                         && ((ItemGroupList) nodedef->get(n_plus_z_plus_y).groups)[groupname] != 0
1220                                         && self_connect_to_raillike)
1221                                         || n_plus_z_plus_y.getContent() == thiscontent)
1222                                 is_rail_z_plus_y[1] = true;
1223
1224                         bool is_rail_x_all[] = {false, false};
1225                         bool is_rail_z_all[] = {false, false};
1226                         is_rail_x_all[0]=is_rail_x[0] || is_rail_x_minus_y[0] || is_rail_x_plus_y[0];
1227                         is_rail_x_all[1]=is_rail_x[1] || is_rail_x_minus_y[1] || is_rail_x_plus_y[1];
1228                         is_rail_z_all[0]=is_rail_z[0] || is_rail_z_minus_y[0] || is_rail_z_plus_y[0];
1229                         is_rail_z_all[1]=is_rail_z[1] || is_rail_z_minus_y[1] || is_rail_z_plus_y[1];
1230
1231                         // reasonable default, flat straight unrotated rail
1232                         bool is_straight = true;
1233                         int adjacencies = 0;
1234                         int angle = 0;
1235                         u8 tileindex = 0;
1236
1237                         // check for sloped rail
1238                         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])
1239                         {
1240                                 adjacencies = 5; //5 means sloped
1241                                 is_straight = true; // sloped is always straight
1242                         }
1243                         else
1244                         {
1245                                 // is really straight, rails on both sides
1246                                 is_straight = (is_rail_x_all[0] && is_rail_x_all[1]) || (is_rail_z_all[0] && is_rail_z_all[1]);
1247                                 adjacencies = is_rail_x_all[0] + is_rail_x_all[1] + is_rail_z_all[0] + is_rail_z_all[1];
1248                         }
1249
1250                         switch (adjacencies) {
1251                         case 1:
1252                                 if(is_rail_x_all[0] || is_rail_x_all[1])
1253                                         angle = 90;
1254                                 break;
1255                         case 2:
1256                                 if(!is_straight)
1257                                         tileindex = 1; // curved
1258                                 if(is_rail_x_all[0] && is_rail_x_all[1])
1259                                         angle = 90;
1260                                 if(is_rail_z_all[0] && is_rail_z_all[1]){
1261                                         if (is_rail_z_plus_y[0])
1262                                                 angle = 180;
1263                                 }
1264                                 else if(is_rail_x_all[0] && is_rail_z_all[0])
1265                                         angle = 270;
1266                                 else if(is_rail_x_all[0] && is_rail_z_all[1])
1267                                         angle = 180;
1268                                 else if(is_rail_x_all[1] && is_rail_z_all[1])
1269                                         angle = 90;
1270                                 break;
1271                         case 3:
1272                                 // here is where the potential to 'switch' a junction is, but not implemented at present
1273                                 tileindex = 2; // t-junction
1274                                 if(!is_rail_x_all[1])
1275                                         angle=180;
1276                                 if(!is_rail_z_all[0])
1277                                         angle=90;
1278                                 if(!is_rail_z_all[1])
1279                                         angle=270;
1280                                 break;
1281                         case 4:
1282                                 tileindex = 3; // crossing
1283                                 break;
1284                         case 5: //sloped
1285                                 if(is_rail_z_plus_y[0])
1286                                         angle = 180;
1287                                 if(is_rail_x_plus_y[0])
1288                                         angle = 90;
1289                                 if(is_rail_x_plus_y[1])
1290                                         angle = -90;
1291                                 break;
1292                         default:
1293                                 break;
1294                         }
1295
1296                         TileSpec tile = getNodeTileN(n, p, tileindex, data);
1297                         tile.material_flags &= ~MATERIAL_FLAG_BACKFACE_CULLING;
1298                         tile.material_flags |= MATERIAL_FLAG_CRACK_OVERLAY;
1299
1300                         u16 l = getInteriorLight(n, 0, data);
1301                         video::SColor c = MapBlock_LightColor(255, l, decode_light(f.light_source));
1302
1303                         float d = (float)BS/64;
1304                         
1305                         char g=-1;
1306                         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])
1307                                 g=1; //Object is at a slope
1308
1309                         video::S3DVertex vertices[4] =
1310                         {
1311                                         video::S3DVertex(-BS/2,-BS/2+d,-BS/2, 0,0,0, c, 0,1),
1312                                         video::S3DVertex(BS/2,-BS/2+d,-BS/2, 0,0,0, c, 1,1),
1313                                         video::S3DVertex(BS/2,g*BS/2+d,BS/2, 0,0,0, c, 1,0),
1314                                         video::S3DVertex(-BS/2,g*BS/2+d,BS/2, 0,0,0, c, 0,0),
1315                         };
1316
1317                         for(s32 i=0; i<4; i++)
1318                         {
1319                                 if(angle != 0)
1320                                         vertices[i].Pos.rotateXZBy(angle);
1321                                 vertices[i].Pos += intToFloat(p, BS);
1322                         }
1323
1324                         u16 indices[] = {0,1,2,2,3,0};
1325                         collector.append(tile, vertices, 4, indices, 6);
1326                 break;}
1327                 case NDT_NODEBOX:
1328                 {
1329                         static const v3s16 tile_dirs[6] = {
1330                                 v3s16(0, 1, 0),
1331                                 v3s16(0, -1, 0),
1332                                 v3s16(1, 0, 0),
1333                                 v3s16(-1, 0, 0),
1334                                 v3s16(0, 0, 1),
1335                                 v3s16(0, 0, -1)
1336                         };
1337                         TileSpec tiles[6];
1338                         
1339                         u16 l = getInteriorLight(n, 0, data);
1340                         video::SColor c = MapBlock_LightColor(255, l, decode_light(f.light_source));
1341
1342                         v3f pos = intToFloat(p, BS);
1343
1344                         std::vector<aabb3f> boxes = n.getNodeBoxes(nodedef);
1345                         for(std::vector<aabb3f>::iterator
1346                                         i = boxes.begin();
1347                                         i != boxes.end(); i++)
1348                         {
1349                         for(int j = 0; j < 6; j++)
1350                                 {
1351                                 // Handles facedir rotation for textures
1352                                 tiles[j] = getNodeTile(n, p, tile_dirs[j], data);
1353                                 }
1354                                 aabb3f box = *i;
1355                                 box.MinEdge += pos;
1356                                 box.MaxEdge += pos;
1357                                 
1358                                 f32 temp;
1359                                 if (box.MinEdge.X > box.MaxEdge.X)
1360                                 {
1361                                         temp=box.MinEdge.X;
1362                                         box.MinEdge.X=box.MaxEdge.X;
1363                                         box.MaxEdge.X=temp;
1364                                 }
1365                                 if (box.MinEdge.Y > box.MaxEdge.Y)
1366                                 {
1367                                         temp=box.MinEdge.Y;
1368                                         box.MinEdge.Y=box.MaxEdge.Y;
1369                                         box.MaxEdge.Y=temp;
1370                                 }
1371                                 if (box.MinEdge.Z > box.MaxEdge.Z)
1372                                 {
1373                                         temp=box.MinEdge.Z;
1374                                         box.MinEdge.Z=box.MaxEdge.Z;
1375                                         box.MaxEdge.Z=temp;
1376                                 }
1377
1378                                 //
1379                                 // Compute texture coords
1380                                 f32 tx1 = (box.MinEdge.X/BS)+0.5;
1381                                 f32 ty1 = (box.MinEdge.Y/BS)+0.5;
1382                                 f32 tz1 = (box.MinEdge.Z/BS)+0.5;
1383                                 f32 tx2 = (box.MaxEdge.X/BS)+0.5;
1384                                 f32 ty2 = (box.MaxEdge.Y/BS)+0.5;
1385                                 f32 tz2 = (box.MaxEdge.Z/BS)+0.5;
1386                                 f32 txc[24] = {
1387                                         // up
1388                                         tx1, 1-tz2, tx2, 1-tz1,
1389                                         // down
1390                                         tx1, tz1, tx2, tz2,
1391                                         // right
1392                                         tz1, 1-ty2, tz2, 1-ty1,
1393                                         // left
1394                                         1-tz2, 1-ty2, 1-tz1, 1-ty1,
1395                                         // back
1396                                         1-tx2, 1-ty2, 1-tx1, 1-ty1,
1397                                         // front
1398                                         tx1, 1-ty2, tx2, 1-ty1,
1399                                 };
1400                                 makeCuboid(&collector, box, tiles, 6, c, txc);
1401                         }
1402                 break;}
1403                 }
1404         }
1405 }
1406