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