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