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