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