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