]> git.lizzy.rs Git - dragonfireclient.git/blob - src/content_mapblock.cpp
9af3be7891ce7a2fd560b4fc902d6ade92578a3d
[dragonfireclient.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 dir = g_6dirs[j];
760                                 v3s16 n2p = blockpos_nodes + p + dir;
761                                 MapNode n2 = data->m_vmanip.getNodeNoEx(n2p);
762                                 // Don't make face if neighbor is of same type
763                                 if(n2.getContent() == n.getContent())
764                                         continue;
765
766                                 // The face at Z+
767                                 video::S3DVertex vertices[4] = {
768                                         video::S3DVertex(-BS/2,-BS/2,BS/2, dir.X,dir.Y,dir.Z, c, 1,1),
769                                         video::S3DVertex(BS/2,-BS/2,BS/2, dir.X,dir.Y,dir.Z, c, 0,1),
770                                         video::S3DVertex(BS/2,BS/2,BS/2, dir.X,dir.Y,dir.Z, c, 0,0),
771                                         video::S3DVertex(-BS/2,BS/2,BS/2, dir.X,dir.Y,dir.Z, c, 1,0),
772                                 };
773                                 
774                                 // Rotations in the g_6dirs format
775                                 if(j == 0) // Z+
776                                         for(u16 i=0; i<4; i++)
777                                                 vertices[i].Pos.rotateXZBy(0);
778                                 else if(j == 1) // Y+
779                                         for(u16 i=0; i<4; i++)
780                                                 vertices[i].Pos.rotateYZBy(-90);
781                                 else if(j == 2) // X+
782                                         for(u16 i=0; i<4; i++)
783                                                 vertices[i].Pos.rotateXZBy(-90);
784                                 else if(j == 3) // Z-
785                                         for(u16 i=0; i<4; i++)
786                                                 vertices[i].Pos.rotateXZBy(180);
787                                 else if(j == 4) // Y-
788                                         for(u16 i=0; i<4; i++)
789                                                 vertices[i].Pos.rotateYZBy(90);
790                                 else if(j == 5) // X-
791                                         for(u16 i=0; i<4; i++)
792                                                 vertices[i].Pos.rotateXZBy(90);
793
794                                 for(u16 i=0; i<4; i++){
795                                         vertices[i].Pos += intToFloat(p, BS);
796                                 }
797
798                                 u16 indices[] = {0,1,2,2,3,0};
799                                 // Add to mesh collector
800                                 collector.append(tile, vertices, 4, indices, 6);
801                         }
802                 break;}
803                 case NDT_GLASSLIKE_FRAMED_OPTIONAL:
804                         // This is always pre-converted to something else
805                         assert(0);
806                         break;
807                 case NDT_GLASSLIKE_FRAMED:
808                 {
809                         static const v3s16 dirs[6] = {
810                                 v3s16( 0, 1, 0),
811                                 v3s16( 0,-1, 0),
812                                 v3s16( 1, 0, 0),
813                                 v3s16(-1, 0, 0),
814                                 v3s16( 0, 0, 1),
815                                 v3s16( 0, 0,-1)
816                         };
817
818                         u8 i;
819                         TileSpec tiles[6];
820                         for (i = 0; i < 6; i++)
821                                 tiles[i] = getNodeTile(n, p, dirs[i], data);
822                         
823                         TileSpec glass_tiles[6];
824                         if (tiles[1].texture && tiles[2].texture && tiles[3].texture) {
825                                 glass_tiles[0] = tiles[2];
826                                 glass_tiles[1] = tiles[3];
827                                 glass_tiles[2] = tiles[1];
828                                 glass_tiles[3] = tiles[1];
829                                 glass_tiles[4] = tiles[1];
830                                 glass_tiles[5] = tiles[1];
831                         } else {
832                                 for (i = 0; i < 6; i++)
833                                         glass_tiles[i] = tiles[1];      
834                         }
835                         
836                         u8 param2 = n.getParam2();
837                         bool H_merge = ! bool(param2 & 128);
838                         bool V_merge = ! bool(param2 & 64);
839                         param2  = param2 & 63;
840                         
841                         u16 l = getInteriorLight(n, 1, nodedef);
842                         video::SColor c = MapBlock_LightColor(255, l, f.light_source);
843                         v3f pos = intToFloat(p, BS);
844                         static const float a = BS / 2;
845                         static const float g = a - 0.003;
846                         static const float b = .876 * ( BS / 2 );
847                         
848                         static const aabb3f frame_edges[12] = {
849                                 aabb3f( b, b,-a, a, a, a), // y+
850                                 aabb3f(-a, b,-a,-b, a, a), // y+
851                                 aabb3f( b,-a,-a, a,-b, a), // y-
852                                 aabb3f(-a,-a,-a,-b,-b, a), // y-
853                                 aabb3f( b,-a, b, a, a, a), // x+
854                                 aabb3f( b,-a,-a, a, a,-b), // x+
855                                 aabb3f(-a,-a, b,-b, a, a), // x-
856                                 aabb3f(-a,-a,-a,-b, a,-b), // x-
857                                 aabb3f(-a, b, b, a, a, a), // z+
858                                 aabb3f(-a,-a, b, a,-b, a), // z+
859                                 aabb3f(-a,-a,-a, a,-b,-b), // z-
860                                 aabb3f(-a, b,-a, a, a,-b)  // z-
861                         };
862                         static const aabb3f glass_faces[6] = {
863                                 aabb3f(-g, g,-g, g, g, g), // y+
864                                 aabb3f(-g,-g,-g, g,-g, g), // y-
865                                 aabb3f( g,-g,-g, g, g, g), // x+
866                                 aabb3f(-g,-g,-g,-g, g, g), // x-
867                                 aabb3f(-g,-g, g, g, g, g), // z+
868                                 aabb3f(-g,-g,-g, g, g,-g)  // z-
869                         };
870                         
871                         // table of node visible faces, 0 = invisible
872                         int visible_faces[6] = {0,0,0,0,0,0};
873                         
874                         // table of neighbours, 1 = same type, checked with g_26dirs
875                         int nb[18] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
876                         
877                         // g_26dirs to check when only horizontal merge is allowed
878                         int nb_H_dirs[8] = {0,2,3,5,10,11,12,13};
879                         
880                         content_t current = n.getContent();
881                         content_t n2c;
882                         MapNode n2;
883                         v3s16 n2p;
884
885                         // neighbours checks for frames visibility
886
887                         if (!H_merge && V_merge) {
888                                 n2p = blockpos_nodes + p + g_26dirs[1];
889                                 n2 = data->m_vmanip.getNodeNoEx(n2p);
890                                 n2c = n2.getContent();
891                                 if (n2c == current || n2c == CONTENT_IGNORE)
892                                         nb[1] = 1;
893                                 n2p = blockpos_nodes + p + g_26dirs[4];
894                                 n2 = data->m_vmanip.getNodeNoEx(n2p);
895                                 n2c = n2.getContent();
896                                 if (n2c == current || n2c == CONTENT_IGNORE)
897                                         nb[4] = 1;      
898                         } else if (H_merge && !V_merge) {
899                                 for(i = 0; i < 8; i++) {
900                                         n2p = blockpos_nodes + p + g_26dirs[nb_H_dirs[i]];
901                                         n2 = data->m_vmanip.getNodeNoEx(n2p);
902                                         n2c = n2.getContent();
903                                         if (n2c == current || n2c == CONTENT_IGNORE)
904                                                 nb[nb_H_dirs[i]] = 1;           
905                                 }
906                         } else if (H_merge && V_merge) {
907                                 for(i = 0; i < 18; i++) {
908                                         n2p = blockpos_nodes + p + g_26dirs[i];
909                                         n2 = data->m_vmanip.getNodeNoEx(n2p);
910                                         n2c = n2.getContent();
911                                         if (n2c == current || n2c == CONTENT_IGNORE)
912                                                 nb[i] = 1;
913                                 }
914                         }
915
916                         // faces visibility checks
917
918                         if (!V_merge) {
919                                 visible_faces[0] = 1;
920                                 visible_faces[1] = 1;
921                         } else {
922                                 for(i = 0; i < 2; i++) {
923                                         n2p = blockpos_nodes + p + dirs[i];
924                                         n2 = data->m_vmanip.getNodeNoEx(n2p);
925                                         n2c = n2.getContent();
926                                         if (n2c != current)
927                                                 visible_faces[i] = 1;
928                                 }
929                         }
930                                 
931                         if (!H_merge) {
932                                 visible_faces[2] = 1;
933                                 visible_faces[3] = 1;
934                                 visible_faces[4] = 1;
935                                 visible_faces[5] = 1;
936                         } else {
937                                 for(i = 2; i < 6; i++) {
938                                         n2p = blockpos_nodes + p + dirs[i];
939                                         n2 = data->m_vmanip.getNodeNoEx(n2p);
940                                         n2c = n2.getContent();
941                                         if (n2c != current)
942                                                 visible_faces[i] = 1;
943                                 }
944                         }
945         
946                         static const u8 nb_triplet[12*3] = {
947                                 1,2, 7,  1,5, 6,  4,2,15,  4,5,14,
948                                 2,0,11,  2,3,13,  5,0,10,  5,3,12,
949                                 0,1, 8,  0,4,16,  3,4,17,  3,1, 9
950                         };
951
952                         f32 tx1, ty1, tz1, tx2, ty2, tz2;
953                         aabb3f box;
954
955                         for(i = 0; i < 12; i++)
956                         {
957                                 int edge_invisible;
958                                 if (nb[nb_triplet[i*3+2]])
959                                         edge_invisible = nb[nb_triplet[i*3]] & nb[nb_triplet[i*3+1]];
960                                 else
961                                         edge_invisible = nb[nb_triplet[i*3]] ^ nb[nb_triplet[i*3+1]];
962                                 if (edge_invisible)
963                                         continue;
964                                 box = frame_edges[i];
965                                 box.MinEdge += pos;
966                                 box.MaxEdge += pos;
967                                 tx1 = (box.MinEdge.X / BS) + 0.5;
968                                 ty1 = (box.MinEdge.Y / BS) + 0.5;
969                                 tz1 = (box.MinEdge.Z / BS) + 0.5;
970                                 tx2 = (box.MaxEdge.X / BS) + 0.5;
971                                 ty2 = (box.MaxEdge.Y / BS) + 0.5;
972                                 tz2 = (box.MaxEdge.Z / BS) + 0.5;
973                                 f32 txc1[24] = {
974                                         tx1,   1-tz2,   tx2, 1-tz1,
975                                         tx1,     tz1,   tx2,   tz2,
976                                         tz1,   1-ty2,   tz2, 1-ty1,
977                                         1-tz2, 1-ty2, 1-tz1, 1-ty1,
978                                         1-tx2, 1-ty2, 1-tx1, 1-ty1,
979                                         tx1,   1-ty2,   tx2, 1-ty1,
980                                 };
981                                 makeCuboid(&collector, box, &tiles[0], 1, c, txc1);
982                         }
983
984                         for(i = 0; i < 6; i++)
985                         {
986                                 if (!visible_faces[i])
987                                         continue;
988                                 box = glass_faces[i];
989                                 box.MinEdge += pos;
990                                 box.MaxEdge += pos;
991                                 tx1 = (box.MinEdge.X / BS) + 0.5;
992                                 ty1 = (box.MinEdge.Y / BS) + 0.5;
993                                 tz1 = (box.MinEdge.Z / BS) + 0.5;
994                                 tx2 = (box.MaxEdge.X / BS) + 0.5;
995                                 ty2 = (box.MaxEdge.Y / BS) + 0.5;
996                                 tz2 = (box.MaxEdge.Z / BS) + 0.5;
997                                 f32 txc2[24] = {
998                                         tx1,   1-tz2,   tx2, 1-tz1,
999                                         tx1,     tz1,   tx2,   tz2,
1000                                         tz1,   1-ty2,   tz2, 1-ty1,
1001                                         1-tz2, 1-ty2, 1-tz1, 1-ty1,
1002                                         1-tx2, 1-ty2, 1-tx1, 1-ty1,
1003                                         tx1,   1-ty2,   tx2, 1-ty1,
1004                                 };
1005                                 makeCuboid(&collector, box, &glass_tiles[i], 1, c, txc2);
1006                         }
1007
1008                         if (param2 > 0 && f.special_tiles[0].texture) {
1009                                 // Interior volume level is in range 0 .. 63,
1010                                 // convert it to -0.5 .. 0.5
1011                                 float vlev = (((float)param2 / 63.0 ) * 2.0 - 1.0);
1012                                 TileSpec interior_tiles[6];
1013                                 for (i = 0; i < 6; i++)
1014                                         interior_tiles[i] = f.special_tiles[0];
1015                                 float offset = 0.003;
1016                                 box = aabb3f(visible_faces[3] ? -b : -a + offset,
1017                                                          visible_faces[1] ? -b : -a + offset,
1018                                                          visible_faces[5] ? -b : -a + offset,
1019                                                          visible_faces[2] ? b : a - offset,
1020                                                          visible_faces[0] ? b * vlev : a * vlev - offset,
1021                                                          visible_faces[4] ? b : a - offset);
1022                                 box.MinEdge += pos;
1023                                 box.MaxEdge += pos;
1024                                 tx1 = (box.MinEdge.X / BS) + 0.5;
1025                                 ty1 = (box.MinEdge.Y / BS) + 0.5;
1026                                 tz1 = (box.MinEdge.Z / BS) + 0.5;
1027                                 tx2 = (box.MaxEdge.X / BS) + 0.5;
1028                                 ty2 = (box.MaxEdge.Y / BS) + 0.5;
1029                                 tz2 = (box.MaxEdge.Z / BS) + 0.5;
1030                                 f32 txc3[24] = {
1031                                         tx1,   1-tz2,   tx2, 1-tz1,
1032                                         tx1,     tz1,   tx2,   tz2,
1033                                         tz1,   1-ty2,   tz2, 1-ty1,
1034                                         1-tz2, 1-ty2, 1-tz1, 1-ty1,
1035                                         1-tx2, 1-ty2, 1-tx1, 1-ty1,
1036                                         tx1,   1-ty2,   tx2, 1-ty1,
1037                                 };
1038                                 makeCuboid(&collector, box, interior_tiles, 6, c,  txc3);
1039                         }
1040                 break;}
1041                 case NDT_ALLFACES:
1042                 {
1043                         TileSpec tile_leaves = getNodeTile(n, p,
1044                                         v3s16(0,0,0), data);
1045
1046                         u16 l = getInteriorLight(n, 1, nodedef);
1047                         video::SColor c = MapBlock_LightColor(255, l, f.light_source);
1048
1049                         v3f pos = intToFloat(p, BS);
1050                         aabb3f box(-BS/2,-BS/2,-BS/2,BS/2,BS/2,BS/2);
1051                         box.MinEdge += pos;
1052                         box.MaxEdge += pos;
1053                         makeCuboid(&collector, box, &tile_leaves, 1, c, NULL);
1054                 break;}
1055                 case NDT_ALLFACES_OPTIONAL:
1056                         // This is always pre-converted to something else
1057                         assert(0);
1058                         break;
1059                 case NDT_TORCHLIKE:
1060                 {
1061                         v3s16 dir = n.getWallMountedDir(nodedef);
1062                         
1063                         u8 tileindex = 0;
1064                         if(dir == v3s16(0,-1,0)){
1065                                 tileindex = 0; // floor
1066                         } else if(dir == v3s16(0,1,0)){
1067                                 tileindex = 1; // ceiling
1068                         // For backwards compatibility
1069                         } else if(dir == v3s16(0,0,0)){
1070                                 tileindex = 0; // floor
1071                         } else {
1072                                 tileindex = 2; // side
1073                         }
1074
1075                         TileSpec tile = getNodeTileN(n, p, tileindex, data);
1076                         tile.material_flags &= ~MATERIAL_FLAG_BACKFACE_CULLING;
1077                         tile.material_flags |= MATERIAL_FLAG_CRACK_OVERLAY;
1078
1079                         u16 l = getInteriorLight(n, 1, nodedef);
1080                         video::SColor c = MapBlock_LightColor(255, l, f.light_source);
1081
1082                         float s = BS/2*f.visual_scale;
1083                         // Wall at X+ of node
1084                         video::S3DVertex vertices[4] =
1085                         {
1086                                 video::S3DVertex(-s,-s,0, 0,0,0, c, 0,1),
1087                                 video::S3DVertex( s,-s,0, 0,0,0, c, 1,1),
1088                                 video::S3DVertex( s, s,0, 0,0,0, c, 1,0),
1089                                 video::S3DVertex(-s, s,0, 0,0,0, c, 0,0),
1090                         };
1091
1092                         for(s32 i=0; i<4; i++)
1093                         {
1094                                 if(dir == v3s16(1,0,0))
1095                                         vertices[i].Pos.rotateXZBy(0);
1096                                 if(dir == v3s16(-1,0,0))
1097                                         vertices[i].Pos.rotateXZBy(180);
1098                                 if(dir == v3s16(0,0,1))
1099                                         vertices[i].Pos.rotateXZBy(90);
1100                                 if(dir == v3s16(0,0,-1))
1101                                         vertices[i].Pos.rotateXZBy(-90);
1102                                 if(dir == v3s16(0,-1,0))
1103                                         vertices[i].Pos.rotateXZBy(45);
1104                                 if(dir == v3s16(0,1,0))
1105                                         vertices[i].Pos.rotateXZBy(-45);
1106
1107                                 vertices[i].Pos += intToFloat(p, BS);
1108                         }
1109
1110                         u16 indices[] = {0,1,2,2,3,0};
1111                         // Add to mesh collector
1112                         collector.append(tile, vertices, 4, indices, 6);
1113                 break;}
1114                 case NDT_SIGNLIKE:
1115                 {
1116                         TileSpec tile = getNodeTileN(n, p, 0, data);
1117                         tile.material_flags &= ~MATERIAL_FLAG_BACKFACE_CULLING;
1118                         tile.material_flags |= MATERIAL_FLAG_CRACK_OVERLAY;
1119
1120                         u16 l = getInteriorLight(n, 0, nodedef);
1121                         video::SColor c = MapBlock_LightColor(255, l, f.light_source);
1122                                 
1123                         float d = (float)BS/16;
1124                         float s = BS/2*f.visual_scale;
1125                         // Wall at X+ of node
1126                         video::S3DVertex vertices[4] =
1127                         {
1128                                 video::S3DVertex(BS/2-d,  s,  s, 0,0,0, c, 0,0),
1129                                 video::S3DVertex(BS/2-d,  s, -s, 0,0,0, c, 1,0),
1130                                 video::S3DVertex(BS/2-d, -s, -s, 0,0,0, c, 1,1),
1131                                 video::S3DVertex(BS/2-d, -s,  s, 0,0,0, c, 0,1),
1132                         };
1133
1134                         v3s16 dir = n.getWallMountedDir(nodedef);
1135
1136                         for(s32 i=0; i<4; i++)
1137                         {
1138                                 if(dir == v3s16(1,0,0))
1139                                         vertices[i].Pos.rotateXZBy(0);
1140                                 if(dir == v3s16(-1,0,0))
1141                                         vertices[i].Pos.rotateXZBy(180);
1142                                 if(dir == v3s16(0,0,1))
1143                                         vertices[i].Pos.rotateXZBy(90);
1144                                 if(dir == v3s16(0,0,-1))
1145                                         vertices[i].Pos.rotateXZBy(-90);
1146                                 if(dir == v3s16(0,-1,0))
1147                                         vertices[i].Pos.rotateXYBy(-90);
1148                                 if(dir == v3s16(0,1,0))
1149                                         vertices[i].Pos.rotateXYBy(90);
1150
1151                                 vertices[i].Pos += intToFloat(p, BS);
1152                         }
1153
1154                         u16 indices[] = {0,1,2,2,3,0};
1155                         // Add to mesh collector
1156                         collector.append(tile, vertices, 4, indices, 6);
1157                 break;}
1158                 case NDT_PLANTLIKE:
1159                 {
1160                         TileSpec tile = getNodeTileN(n, p, 0, data);
1161                         tile.material_flags |= MATERIAL_FLAG_CRACK_OVERLAY;
1162
1163                         u16 l = getInteriorLight(n, 1, nodedef);
1164                         video::SColor c = MapBlock_LightColor(255, l, f.light_source);
1165                         
1166                         float s = BS / 2 * f.visual_scale;
1167
1168                         for (int j = 0; j < 2; j++)
1169                         {
1170                                 video::S3DVertex vertices[4] =
1171                                 {
1172                                         video::S3DVertex(-s,-BS/2, 0, 0,0,0, c, 0,1),
1173                                         video::S3DVertex( s,-BS/2, 0, 0,0,0, c, 1,1),
1174                                         video::S3DVertex( s,-BS/2 + s*2,0, 0,0,0, c, 1,0),
1175                                         video::S3DVertex(-s,-BS/2 + s*2,0, 0,0,0, c, 0,0),
1176                                 };
1177
1178                                 if(j == 0)
1179                                 {
1180                                         for(u16 i = 0; i < 4; i++)
1181                                                 vertices[i].Pos.rotateXZBy(46 + n.param2 * 2);
1182                                 }
1183                                 else if(j == 1)
1184                                 {
1185                                         for(u16 i = 0; i < 4; i++)
1186                                                 vertices[i].Pos.rotateXZBy(-44 + n.param2 * 2);
1187                                 }
1188
1189                                 for (int i = 0; i < 4; i++)
1190                                 {
1191                                         vertices[i].Pos *= f.visual_scale;
1192                                         vertices[i].Pos.Y += BS/2 * (f.visual_scale - 1);
1193                                         vertices[i].Pos += intToFloat(p, BS);
1194                                 }
1195
1196                                 u16 indices[] = {0, 1, 2, 2, 3, 0};
1197                                 // Add to mesh collector
1198                                 collector.append(tile, vertices, 4, indices, 6);
1199                         }
1200                 break;}
1201                 case NDT_FIRELIKE:
1202                 {
1203                         TileSpec tile = getNodeTileN(n, p, 0, data);
1204                         tile.material_flags |= MATERIAL_FLAG_CRACK_OVERLAY;
1205
1206                         u16 l = getInteriorLight(n, 1, nodedef);
1207                         video::SColor c = MapBlock_LightColor(255, l, f.light_source);
1208
1209                         float s = BS/2*f.visual_scale;
1210
1211                         content_t current = n.getContent();
1212                         content_t n2c;
1213                         MapNode n2;
1214                         v3s16 n2p;
1215
1216                         static const v3s16 dirs[6] = {
1217                                 v3s16( 0, 1, 0),
1218                                 v3s16( 0,-1, 0),
1219                                 v3s16( 1, 0, 0),
1220                                 v3s16(-1, 0, 0),
1221                                 v3s16( 0, 0, 1),
1222                                 v3s16( 0, 0,-1)
1223                         };
1224
1225                         int doDraw[6] = {0,0,0,0,0,0};
1226
1227                         bool drawAllFaces = true;
1228
1229                         bool drawBottomFacesOnly = false; // Currently unused
1230
1231                         // Check for adjacent nodes
1232                         for(int i = 0; i < 6; i++)
1233                         {
1234                                 n2p = blockpos_nodes + p + dirs[i];
1235                                 n2 = data->m_vmanip.getNodeNoEx(n2p);
1236                                 n2c = n2.getContent();
1237                                 if (n2c != CONTENT_IGNORE && n2c != CONTENT_AIR && n2c != current) {
1238                                         doDraw[i] = 1;
1239                                         if(drawAllFaces)
1240                                                 drawAllFaces = false;
1241
1242                                 }
1243                         }
1244
1245                         for(int j = 0; j < 6; j++)
1246                         {
1247                                 int vOffset = 0; // Vertical offset of faces after rotation
1248                                 int hOffset = 4; // Horizontal offset of faces to reach the edge
1249
1250                                 video::S3DVertex vertices[4] =
1251                                 {
1252                                         video::S3DVertex(-s,-BS/2,      0, 0,0,0, c, 0,1),
1253                                         video::S3DVertex( s,-BS/2,      0, 0,0,0, c, 1,1),
1254                                         video::S3DVertex( s,-BS/2 + s*2,0, 0,0,0, c, 1,0),
1255                                         video::S3DVertex(-s,-BS/2 + s*2,0, 0,0,0, c, 0,0),
1256                                 };
1257
1258                                 // Calculate which faces should be drawn, (top or sides)
1259                                 if(j == 0 && (drawAllFaces || (doDraw[3] == 1 || doDraw[1] == 1)))
1260                                 {
1261                                         for(int i = 0; i < 4; i++) {
1262                                                 vertices[i].Pos.rotateXZBy(90 + n.param2 * 2);
1263                                                 vertices[i].Pos.rotateXYBy(-10);
1264                                                 vertices[i].Pos.Y -= vOffset;
1265                                                 vertices[i].Pos.X -= hOffset;
1266                                         }
1267                                 }
1268                                 else if(j == 1 && (drawAllFaces || (doDraw[5] == 1 || doDraw[1] == 1)))
1269                                 {
1270                                         for(int i = 0; i < 4; i++) {
1271                                                 vertices[i].Pos.rotateXZBy(180 + n.param2 * 2);
1272                                                 vertices[i].Pos.rotateYZBy(10);
1273                                                 vertices[i].Pos.Y -= vOffset;
1274                                                 vertices[i].Pos.Z -= hOffset;
1275                                         }
1276                                 }
1277                                 else if(j == 2 && (drawAllFaces || (doDraw[2] == 1 || doDraw[1] == 1)))
1278                                 {
1279                                         for(int i = 0; i < 4; i++) {
1280                                                 vertices[i].Pos.rotateXZBy(270 + n.param2 * 2);
1281                                                 vertices[i].Pos.rotateXYBy(10);
1282                                                 vertices[i].Pos.Y -= vOffset;
1283                                                 vertices[i].Pos.X += hOffset;
1284                                         }
1285                                 }
1286                                 else if(j == 3 && (drawAllFaces || (doDraw[4] == 1 || doDraw[1] == 1)))
1287                                 {
1288                                         for(int i = 0; i < 4; i++) {
1289                                                 vertices[i].Pos.rotateYZBy(-10);
1290                                                 vertices[i].Pos.Y -= vOffset;
1291                                                 vertices[i].Pos.Z += hOffset;
1292                                         }
1293                                 }
1294
1295                                 // Center cross-flames
1296                                 else if(j == 4 && (drawAllFaces || doDraw[1] == 1))
1297                                 {
1298                                         for(int i=0; i<4; i++) {
1299                                                 vertices[i].Pos.rotateXZBy(45 + n.param2 * 2);
1300                                                 vertices[i].Pos.Y -= vOffset;
1301                                         }
1302                                 }
1303                                 else if(j == 5 && (drawAllFaces || doDraw[1] == 1))
1304                                 {
1305                                         for(int i=0; i<4; i++) {
1306                                                 vertices[i].Pos.rotateXZBy(-45 + n.param2 * 2);
1307                                                 vertices[i].Pos.Y -= vOffset;
1308                                         }
1309                                 }
1310
1311                                 // Render flames on bottom
1312                                 else if(j == 0 && (drawBottomFacesOnly || (doDraw[0] == 1 && doDraw[1] == 0)))
1313                                 {
1314                                         for(int i = 0; i < 4; i++) {
1315                                                 vertices[i].Pos.rotateYZBy(70);
1316                                                 vertices[i].Pos.rotateXZBy(90 + n.param2 * 2);
1317                                                 vertices[i].Pos.Y += 4.84;
1318                                                 vertices[i].Pos.X -= hOffset+0.7;
1319                                         }
1320                                 }
1321                                 else if(j == 1 && (drawBottomFacesOnly || (doDraw[0] == 1 && doDraw[1] == 0)))
1322                                 {
1323                                         for(int i = 0; i < 4; i++) {
1324                                                 vertices[i].Pos.rotateYZBy(70);
1325                                                 vertices[i].Pos.rotateXZBy(180 + n.param2 * 2);
1326                                                 vertices[i].Pos.Y += 4.84;
1327                                                 vertices[i].Pos.Z -= hOffset+0.7;
1328                                         }
1329                                 }
1330                                 else if(j == 2 && (drawBottomFacesOnly || (doDraw[0] == 1 && doDraw[1] == 0)))
1331                                 {
1332                                         for(int i = 0; i < 4; i++) {
1333                                                 vertices[i].Pos.rotateYZBy(70);
1334                                                 vertices[i].Pos.rotateXZBy(270 + n.param2 * 2);
1335                                                 vertices[i].Pos.Y += 4.84;
1336                                                 vertices[i].Pos.X += hOffset+0.7;
1337                                         }
1338                                 }
1339                                 else if(j == 3 && (drawBottomFacesOnly || (doDraw[0] == 1 && doDraw[1] == 0)))
1340                                 {
1341                                         for(int i = 0; i < 4; i++) {
1342                                                 vertices[i].Pos.rotateYZBy(70);
1343                                                 vertices[i].Pos.Y += 4.84;
1344                                                 vertices[i].Pos.Z += hOffset+0.7;
1345                                         }
1346                                 }
1347                                 else {
1348                                         // Skip faces that aren't adjacent to a node
1349                                         continue;
1350                                 }
1351
1352                                 for(int i=0; i<4; i++)
1353                                 {
1354                                         vertices[i].Pos *= f.visual_scale;
1355                                         vertices[i].Pos += intToFloat(p, BS);
1356                                 }
1357
1358                                 u16 indices[] = {0,1,2,2,3,0};
1359                                 // Add to mesh collector
1360                                 collector.append(tile, vertices, 4, indices, 6);
1361                         }
1362                 break;}
1363                 case NDT_FENCELIKE:
1364                 {
1365                         TileSpec tile = getNodeTile(n, p, v3s16(0,0,0), data);
1366                         TileSpec tile_nocrack = tile;
1367                         tile_nocrack.material_flags &= ~MATERIAL_FLAG_CRACK;
1368
1369                         // Put wood the right way around in the posts
1370                         TileSpec tile_rot = tile;
1371                         tile_rot.rotation = 1;
1372
1373                         u16 l = getInteriorLight(n, 1, nodedef);
1374                         video::SColor c = MapBlock_LightColor(255, l, f.light_source);
1375
1376                         const f32 post_rad=(f32)BS/8;
1377                         const f32 bar_rad=(f32)BS/16;
1378                         const f32 bar_len=(f32)(BS/2)-post_rad;
1379
1380                         v3f pos = intToFloat(p, BS);
1381
1382                         // The post - always present
1383                         aabb3f post(-post_rad,-BS/2,-post_rad,post_rad,BS/2,post_rad);
1384                         post.MinEdge += pos;
1385                         post.MaxEdge += pos;
1386                         f32 postuv[24]={
1387                                         6/16.,6/16.,10/16.,10/16.,
1388                                         6/16.,6/16.,10/16.,10/16.,
1389                                         0/16.,0,4/16.,1,
1390                                         4/16.,0,8/16.,1,
1391                                         8/16.,0,12/16.,1,
1392                                         12/16.,0,16/16.,1};
1393                         makeCuboid(&collector, post, &tile_rot, 1, c, postuv);
1394
1395                         // Now a section of fence, +X, if there's a post there
1396                         v3s16 p2 = p;
1397                         p2.X++;
1398                         MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
1399                         const ContentFeatures *f2 = &nodedef->get(n2);
1400                         if(f2->drawtype == NDT_FENCELIKE)
1401                         {
1402                                 aabb3f bar(-bar_len+BS/2,-bar_rad+BS/4,-bar_rad,
1403                                                 bar_len+BS/2,bar_rad+BS/4,bar_rad);
1404                                 bar.MinEdge += pos;
1405                                 bar.MaxEdge += pos;
1406                                 f32 xrailuv[24]={
1407                                         0/16.,2/16.,16/16.,4/16.,
1408                                         0/16.,4/16.,16/16.,6/16.,
1409                                         6/16.,6/16.,8/16.,8/16.,
1410                                         10/16.,10/16.,12/16.,12/16.,
1411                                         0/16.,8/16.,16/16.,10/16.,
1412                                         0/16.,14/16.,16/16.,16/16.};
1413                                 makeCuboid(&collector, bar, &tile_nocrack, 1,
1414                                                 c, xrailuv);
1415                                 bar.MinEdge.Y -= BS/2;
1416                                 bar.MaxEdge.Y -= BS/2;
1417                                 makeCuboid(&collector, bar, &tile_nocrack, 1,
1418                                                 c, xrailuv);
1419                         }
1420
1421                         // Now a section of fence, +Z, if there's a post there
1422                         p2 = p;
1423                         p2.Z++;
1424                         n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
1425                         f2 = &nodedef->get(n2);
1426                         if(f2->drawtype == NDT_FENCELIKE)
1427                         {
1428                                 aabb3f bar(-bar_rad,-bar_rad+BS/4,-bar_len+BS/2,
1429                                                 bar_rad,bar_rad+BS/4,bar_len+BS/2);
1430                                 bar.MinEdge += pos;
1431                                 bar.MaxEdge += pos;
1432                                 f32 zrailuv[24]={
1433                                         3/16.,1/16.,5/16.,5/16., // cannot rotate; stretch
1434                                         4/16.,1/16.,6/16.,5/16., // for wood texture instead
1435                                         0/16.,9/16.,16/16.,11/16.,
1436                                         0/16.,6/16.,16/16.,8/16.,
1437                                         6/16.,6/16.,8/16.,8/16.,
1438                                         10/16.,10/16.,12/16.,12/16.};
1439                                 makeCuboid(&collector, bar, &tile_nocrack, 1,
1440                                                 c, zrailuv);
1441                                 bar.MinEdge.Y -= BS/2;
1442                                 bar.MaxEdge.Y -= BS/2;
1443                                 makeCuboid(&collector, bar, &tile_nocrack, 1,
1444                                                 c, zrailuv);
1445                         }
1446                 break;}
1447                 case NDT_RAILLIKE:
1448                 {
1449                         bool is_rail_x [] = { false, false };  /* x-1, x+1 */
1450                         bool is_rail_z [] = { false, false };  /* z-1, z+1 */
1451
1452                         bool is_rail_z_minus_y [] = { false, false };  /* z-1, z+1; y-1 */
1453                         bool is_rail_x_minus_y [] = { false, false };  /* x-1, z+1; y-1 */
1454                         bool is_rail_z_plus_y [] = { false, false };  /* z-1, z+1; y+1 */
1455                         bool is_rail_x_plus_y [] = { false, false };  /* x-1, x+1; y+1 */
1456
1457                         MapNode n_minus_x = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x-1,y,z));
1458                         MapNode n_plus_x = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x+1,y,z));
1459                         MapNode n_minus_z = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y,z-1));
1460                         MapNode n_plus_z = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y,z+1));
1461                         MapNode n_plus_x_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x+1, y+1, z));
1462                         MapNode n_plus_x_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x+1, y-1, z));
1463                         MapNode n_minus_x_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x-1, y+1, z));
1464                         MapNode n_minus_x_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x-1, y-1, z));
1465                         MapNode n_plus_z_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y+1, z+1));
1466                         MapNode n_minus_z_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y+1, z-1));
1467                         MapNode n_plus_z_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y-1, z+1));
1468                         MapNode n_minus_z_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y-1, z-1));
1469
1470                         content_t thiscontent = n.getContent();
1471                         std::string groupname = "connect_to_raillike"; // name of the group that enables connecting to raillike nodes of different kind
1472                         bool self_connect_to_raillike = ((ItemGroupList) nodedef->get(n).groups)[groupname] != 0;
1473
1474                         if ((nodedef->get(n_minus_x).drawtype == NDT_RAILLIKE
1475                                         && ((ItemGroupList) nodedef->get(n_minus_x).groups)[groupname] != 0
1476                                         && self_connect_to_raillike)
1477                                         || n_minus_x.getContent() == thiscontent)
1478                                 is_rail_x[0] = true;
1479
1480                         if ((nodedef->get(n_minus_x_minus_y).drawtype == NDT_RAILLIKE
1481                                         && ((ItemGroupList) nodedef->get(n_minus_x_minus_y).groups)[groupname] != 0
1482                                         && self_connect_to_raillike)
1483                                         || n_minus_x_minus_y.getContent() == thiscontent)
1484                                 is_rail_x_minus_y[0] = true;
1485
1486                         if ((nodedef->get(n_minus_x_plus_y).drawtype == NDT_RAILLIKE
1487                                         && ((ItemGroupList) nodedef->get(n_minus_x_plus_y).groups)[groupname] != 0
1488                                         && self_connect_to_raillike)
1489                                         || n_minus_x_plus_y.getContent() == thiscontent)
1490                                 is_rail_x_plus_y[0] = true;
1491
1492                         if ((nodedef->get(n_plus_x).drawtype == NDT_RAILLIKE
1493                                         && ((ItemGroupList) nodedef->get(n_plus_x).groups)[groupname] != 0
1494                                         && self_connect_to_raillike)
1495                                         || n_plus_x.getContent() == thiscontent)
1496                                 is_rail_x[1] = true;
1497
1498                         if ((nodedef->get(n_plus_x_minus_y).drawtype == NDT_RAILLIKE
1499                                         && ((ItemGroupList) nodedef->get(n_plus_x_minus_y).groups)[groupname] != 0
1500                                         && self_connect_to_raillike)
1501                                         || n_plus_x_minus_y.getContent() == thiscontent)
1502                                 is_rail_x_minus_y[1] = true;
1503
1504                         if ((nodedef->get(n_plus_x_plus_y).drawtype == NDT_RAILLIKE
1505                                         && ((ItemGroupList) nodedef->get(n_plus_x_plus_y).groups)[groupname] != 0
1506                                         && self_connect_to_raillike)
1507                                         || n_plus_x_plus_y.getContent() == thiscontent)
1508                                 is_rail_x_plus_y[1] = true;
1509
1510                         if ((nodedef->get(n_minus_z).drawtype == NDT_RAILLIKE
1511                                         && ((ItemGroupList) nodedef->get(n_minus_z).groups)[groupname] != 0
1512                                         && self_connect_to_raillike)
1513                                         || n_minus_z.getContent() == thiscontent)
1514                                 is_rail_z[0] = true;
1515
1516                         if ((nodedef->get(n_minus_z_minus_y).drawtype == NDT_RAILLIKE
1517                                         && ((ItemGroupList) nodedef->get(n_minus_z_minus_y).groups)[groupname] != 0
1518                                         && self_connect_to_raillike)
1519                                         || n_minus_z_minus_y.getContent() == thiscontent)
1520                                 is_rail_z_minus_y[0] = true;
1521
1522                         if ((nodedef->get(n_minus_z_plus_y).drawtype == NDT_RAILLIKE
1523                                         && ((ItemGroupList) nodedef->get(n_minus_z_plus_y).groups)[groupname] != 0
1524                                         && self_connect_to_raillike)
1525                                         || n_minus_z_plus_y.getContent() == thiscontent)
1526                                 is_rail_z_plus_y[0] = true;
1527
1528                         if ((nodedef->get(n_plus_z).drawtype == NDT_RAILLIKE
1529                                         && ((ItemGroupList) nodedef->get(n_plus_z).groups)[groupname] != 0
1530                                         && self_connect_to_raillike)
1531                                         || n_plus_z.getContent() == thiscontent)
1532                                 is_rail_z[1] = true;
1533
1534                         if ((nodedef->get(n_plus_z_minus_y).drawtype == NDT_RAILLIKE
1535                                         && ((ItemGroupList) nodedef->get(n_plus_z_minus_y).groups)[groupname] != 0
1536                                         && self_connect_to_raillike)
1537                                         || n_plus_z_minus_y.getContent() == thiscontent)
1538                                 is_rail_z_minus_y[1] = true;
1539
1540                         if ((nodedef->get(n_plus_z_plus_y).drawtype == NDT_RAILLIKE
1541                                         && ((ItemGroupList) nodedef->get(n_plus_z_plus_y).groups)[groupname] != 0
1542                                         && self_connect_to_raillike)
1543                                         || n_plus_z_plus_y.getContent() == thiscontent)
1544                                 is_rail_z_plus_y[1] = true;
1545
1546                         bool is_rail_x_all[] = {false, false};
1547                         bool is_rail_z_all[] = {false, false};
1548                         is_rail_x_all[0]=is_rail_x[0] || is_rail_x_minus_y[0] || is_rail_x_plus_y[0];
1549                         is_rail_x_all[1]=is_rail_x[1] || is_rail_x_minus_y[1] || is_rail_x_plus_y[1];
1550                         is_rail_z_all[0]=is_rail_z[0] || is_rail_z_minus_y[0] || is_rail_z_plus_y[0];
1551                         is_rail_z_all[1]=is_rail_z[1] || is_rail_z_minus_y[1] || is_rail_z_plus_y[1];
1552
1553                         // reasonable default, flat straight unrotated rail
1554                         bool is_straight = true;
1555                         int adjacencies = 0;
1556                         int angle = 0;
1557                         u8 tileindex = 0;
1558
1559                         // check for sloped rail
1560                         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])
1561                         {
1562                                 adjacencies = 5; //5 means sloped
1563                                 is_straight = true; // sloped is always straight
1564                         }
1565                         else
1566                         {
1567                                 // is really straight, rails on both sides
1568                                 is_straight = (is_rail_x_all[0] && is_rail_x_all[1]) || (is_rail_z_all[0] && is_rail_z_all[1]);
1569                                 adjacencies = is_rail_x_all[0] + is_rail_x_all[1] + is_rail_z_all[0] + is_rail_z_all[1];
1570                         }
1571
1572                         switch (adjacencies) {
1573                         case 1:
1574                                 if(is_rail_x_all[0] || is_rail_x_all[1])
1575                                         angle = 90;
1576                                 break;
1577                         case 2:
1578                                 if(!is_straight)
1579                                         tileindex = 1; // curved
1580                                 if(is_rail_x_all[0] && is_rail_x_all[1])
1581                                         angle = 90;
1582                                 if(is_rail_z_all[0] && is_rail_z_all[1]){
1583                                         if (is_rail_z_plus_y[0])
1584                                                 angle = 180;
1585                                 }
1586                                 else if(is_rail_x_all[0] && is_rail_z_all[0])
1587                                         angle = 270;
1588                                 else if(is_rail_x_all[0] && is_rail_z_all[1])
1589                                         angle = 180;
1590                                 else if(is_rail_x_all[1] && is_rail_z_all[1])
1591                                         angle = 90;
1592                                 break;
1593                         case 3:
1594                                 // here is where the potential to 'switch' a junction is, but not implemented at present
1595                                 tileindex = 2; // t-junction
1596                                 if(!is_rail_x_all[1])
1597                                         angle=180;
1598                                 if(!is_rail_z_all[0])
1599                                         angle=90;
1600                                 if(!is_rail_z_all[1])
1601                                         angle=270;
1602                                 break;
1603                         case 4:
1604                                 tileindex = 3; // crossing
1605                                 break;
1606                         case 5: //sloped
1607                                 if(is_rail_z_plus_y[0])
1608                                         angle = 180;
1609                                 if(is_rail_x_plus_y[0])
1610                                         angle = 90;
1611                                 if(is_rail_x_plus_y[1])
1612                                         angle = -90;
1613                                 break;
1614                         default:
1615                                 break;
1616                         }
1617
1618                         TileSpec tile = getNodeTileN(n, p, tileindex, data);
1619                         tile.material_flags &= ~MATERIAL_FLAG_BACKFACE_CULLING;
1620                         tile.material_flags |= MATERIAL_FLAG_CRACK_OVERLAY;
1621
1622                         u16 l = getInteriorLight(n, 0, nodedef);
1623                         video::SColor c = MapBlock_LightColor(255, l, f.light_source);
1624
1625                         float d = (float)BS/64;
1626                         float s = BS/2;
1627
1628                         short g = -1;
1629                         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])
1630                                 g = 1; //Object is at a slope
1631
1632                         video::S3DVertex vertices[4] =
1633                         {
1634                                         video::S3DVertex(-s,  -s+d,-s,  0,0,0,  c,0,1),
1635                                         video::S3DVertex( s,  -s+d,-s,  0,0,0,  c,1,1),
1636                                         video::S3DVertex( s, g*s+d, s,  0,0,0,  c,1,0),
1637                                         video::S3DVertex(-s, g*s+d, s,  0,0,0,  c,0,0),
1638                         };
1639
1640                         for(s32 i=0; i<4; i++)
1641                         {
1642                                 if(angle != 0)
1643                                         vertices[i].Pos.rotateXZBy(angle);
1644                                 vertices[i].Pos += intToFloat(p, BS);
1645                         }
1646
1647                         u16 indices[] = {0,1,2,2,3,0};
1648                         collector.append(tile, vertices, 4, indices, 6);
1649                 break;}
1650                 case NDT_NODEBOX:
1651                 {
1652                         static const v3s16 tile_dirs[6] = {
1653                                 v3s16(0, 1, 0),
1654                                 v3s16(0, -1, 0),
1655                                 v3s16(1, 0, 0),
1656                                 v3s16(-1, 0, 0),
1657                                 v3s16(0, 0, 1),
1658                                 v3s16(0, 0, -1)
1659                         };
1660                         TileSpec tiles[6];
1661                         
1662                         u16 l = getInteriorLight(n, 1, nodedef);
1663                         video::SColor c = MapBlock_LightColor(255, l, f.light_source);
1664
1665                         v3f pos = intToFloat(p, BS);
1666
1667                         std::vector<aabb3f> boxes = n.getNodeBoxes(nodedef);
1668                         for(std::vector<aabb3f>::iterator
1669                                         i = boxes.begin();
1670                                         i != boxes.end(); i++)
1671                         {
1672                         for(int j = 0; j < 6; j++)
1673                                 {
1674                                 // Handles facedir rotation for textures
1675                                 tiles[j] = getNodeTile(n, p, tile_dirs[j], data);
1676                                 }
1677                                 aabb3f box = *i;
1678                                 box.MinEdge += pos;
1679                                 box.MaxEdge += pos;
1680                                 
1681                                 f32 temp;
1682                                 if (box.MinEdge.X > box.MaxEdge.X)
1683                                 {
1684                                         temp=box.MinEdge.X;
1685                                         box.MinEdge.X=box.MaxEdge.X;
1686                                         box.MaxEdge.X=temp;
1687                                 }
1688                                 if (box.MinEdge.Y > box.MaxEdge.Y)
1689                                 {
1690                                         temp=box.MinEdge.Y;
1691                                         box.MinEdge.Y=box.MaxEdge.Y;
1692                                         box.MaxEdge.Y=temp;
1693                                 }
1694                                 if (box.MinEdge.Z > box.MaxEdge.Z)
1695                                 {
1696                                         temp=box.MinEdge.Z;
1697                                         box.MinEdge.Z=box.MaxEdge.Z;
1698                                         box.MaxEdge.Z=temp;
1699                                 }
1700
1701                                 //
1702                                 // Compute texture coords
1703                                 f32 tx1 = (box.MinEdge.X/BS)+0.5;
1704                                 f32 ty1 = (box.MinEdge.Y/BS)+0.5;
1705                                 f32 tz1 = (box.MinEdge.Z/BS)+0.5;
1706                                 f32 tx2 = (box.MaxEdge.X/BS)+0.5;
1707                                 f32 ty2 = (box.MaxEdge.Y/BS)+0.5;
1708                                 f32 tz2 = (box.MaxEdge.Z/BS)+0.5;
1709                                 f32 txc[24] = {
1710                                         // up
1711                                         tx1, 1-tz2, tx2, 1-tz1,
1712                                         // down
1713                                         tx1, tz1, tx2, tz2,
1714                                         // right
1715                                         tz1, 1-ty2, tz2, 1-ty1,
1716                                         // left
1717                                         1-tz2, 1-ty2, 1-tz1, 1-ty1,
1718                                         // back
1719                                         1-tx2, 1-ty2, 1-tx1, 1-ty1,
1720                                         // front
1721                                         tx1, 1-ty2, tx2, 1-ty1,
1722                                 };
1723                                 makeCuboid(&collector, box, tiles, 6, c, txc);
1724                         }
1725                 break;}
1726                 case NDT_MESH:
1727                 {
1728                         v3f pos = intToFloat(p, BS);
1729                         video::SColor c = MapBlock_LightColor(255, getInteriorLight(n, 1, nodedef), f.light_source);
1730
1731                         u8 facedir = 0;
1732                         if (f.param_type_2 == CPT2_FACEDIR) {
1733                                 facedir = n.getFaceDir(nodedef);
1734                         } else if (f.param_type_2 == CPT2_WALLMOUNTED) {
1735                                 //convert wallmounted to 6dfacedir.
1736                                 //when cache enabled, it is already converted
1737                                 facedir = n.getWallMounted(nodedef);
1738                                 if (!enable_mesh_cache) {
1739                                         static const u8 wm_to_6d[6] = {20, 0, 16+1, 12+3, 8, 4+2};
1740                                         facedir = wm_to_6d[facedir];
1741                                 }
1742                         }
1743
1744                         if (f.mesh_ptr[facedir]) {
1745                                 // use cached meshes
1746                                 for(u16 j = 0; j < f.mesh_ptr[0]->getMeshBufferCount(); j++) {
1747                                         scene::IMeshBuffer *buf = f.mesh_ptr[facedir]->getMeshBuffer(j);
1748                                         collector.append(getNodeTileN(n, p, j, data),
1749                                                 (video::S3DVertex *)buf->getVertices(), buf->getVertexCount(),
1750                                                 buf->getIndices(), buf->getIndexCount(), pos, c);
1751                                 }
1752                         } else if (f.mesh_ptr[0]) {
1753                                 // no cache, clone and rotate mesh
1754                                 scene::IMesh* mesh = cloneMesh(f.mesh_ptr[0]);
1755                                 rotateMeshBy6dFacedir(mesh, facedir);
1756                                 recalculateBoundingBox(mesh);
1757                                 meshmanip->recalculateNormals(mesh, true, false);
1758                                 for(u16 j = 0; j < mesh->getMeshBufferCount(); j++) {
1759                                         scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
1760                                         collector.append(getNodeTileN(n, p, j, data),
1761                                                 (video::S3DVertex *)buf->getVertices(), buf->getVertexCount(),
1762                                                 buf->getIndices(), buf->getIndexCount(), pos, c);
1763                                 }
1764                                 mesh->drop();
1765                         }
1766                 break;}
1767                 }
1768         }
1769 }
1770