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