]> git.lizzy.rs Git - minetest.git/blob - src/content_mapblock.cpp
Improve the look of fences
[minetest.git] / src / content_mapblock.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "content_mapblock.h"
21
22 #include "main.h" // For g_settings
23 #include "mapblock_mesh.h" // For MapBlock_LightColor() and MeshCollector
24 #include "settings.h"
25 #include "nodedef.h"
26 #include "tile.h"
27 #include "gamedef.h"
28
29 // Create a cuboid.
30 //  collector - the MeshCollector for the resulting polygons
31 //  box       - the position and size of the box
32 //  tiles     - the tiles (materials) to use (for all 6 faces)
33 //  tilecount - number of entries in tiles, 1<=tilecount<=6
34 //  c         - vertex colour - used for all
35 //  txc       - texture coordinates - this is a list of texture coordinates
36 //              for the opposite corners of each face - therefore, there
37 //              should be (2+2)*6=24 values in the list. Alternatively, pass
38 //              NULL to use the entire texture for each face. The order of
39 //              the faces in the list is up-down-right-left-back-front
40 //              (compatible with ContentFeatures). If you specified 0,0,1,1
41 //              for each face, that would be the same as passing NULL.
42 void makeCuboid(MeshCollector *collector, const aabb3f &box,
43         const TileSpec *tiles, int tilecount,
44         video::SColor &c, const f32* txc)
45 {
46         assert(tilecount >= 1 && tilecount <= 6);
47
48         v3f min = box.MinEdge;
49         v3f max = box.MaxEdge;
50
51         if(txc == NULL)
52         {
53                 static const f32 txc_default[24] = {
54                         0,0,1,1,
55                         0,0,1,1,
56                         0,0,1,1,
57                         0,0,1,1,
58                         0,0,1,1,
59                         0,0,1,1
60                 };
61                 txc = txc_default;
62         }
63
64         video::S3DVertex vertices[24] =
65         {
66                 // up
67                 video::S3DVertex(min.X,max.Y,max.Z, 0,1,0, c, txc[0],txc[1]),
68                 video::S3DVertex(max.X,max.Y,max.Z, 0,1,0, c, txc[2],txc[1]),
69                 video::S3DVertex(max.X,max.Y,min.Z, 0,1,0, c, txc[2],txc[3]),
70                 video::S3DVertex(min.X,max.Y,min.Z, 0,1,0, c, txc[0],txc[3]),
71                 // down
72                 video::S3DVertex(min.X,min.Y,min.Z, 0,-1,0, c, txc[4],txc[5]),
73                 video::S3DVertex(max.X,min.Y,min.Z, 0,-1,0, c, txc[6],txc[5]),
74                 video::S3DVertex(max.X,min.Y,max.Z, 0,-1,0, c, txc[6],txc[7]),
75                 video::S3DVertex(min.X,min.Y,max.Z, 0,-1,0, c, txc[4],txc[7]),
76                 // right
77                 video::S3DVertex(max.X,max.Y,min.Z, 1,0,0, c, txc[ 8],txc[9]),
78                 video::S3DVertex(max.X,max.Y,max.Z, 1,0,0, c, txc[10],txc[9]),
79                 video::S3DVertex(max.X,min.Y,max.Z, 1,0,0, c, txc[10],txc[11]),
80                 video::S3DVertex(max.X,min.Y,min.Z, 1,0,0, c, txc[ 8],txc[11]),
81                 // left
82                 video::S3DVertex(min.X,max.Y,max.Z, -1,0,0, c, txc[12],txc[13]),
83                 video::S3DVertex(min.X,max.Y,min.Z, -1,0,0, c, txc[14],txc[13]),
84                 video::S3DVertex(min.X,min.Y,min.Z, -1,0,0, c, txc[14],txc[15]),
85                 video::S3DVertex(min.X,min.Y,max.Z, -1,0,0, c, txc[12],txc[15]),
86                 // back
87                 video::S3DVertex(max.X,max.Y,max.Z, 0,0,1, c, txc[16],txc[17]),
88                 video::S3DVertex(min.X,max.Y,max.Z, 0,0,1, c, txc[18],txc[17]),
89                 video::S3DVertex(min.X,min.Y,max.Z, 0,0,1, c, txc[18],txc[19]),
90                 video::S3DVertex(max.X,min.Y,max.Z, 0,0,1, c, txc[16],txc[19]),
91                 // front
92                 video::S3DVertex(min.X,max.Y,min.Z, 0,0,-1, c, txc[20],txc[21]),
93                 video::S3DVertex(max.X,max.Y,min.Z, 0,0,-1, c, txc[22],txc[21]),
94                 video::S3DVertex(max.X,min.Y,min.Z, 0,0,-1, c, txc[22],txc[23]),
95                 video::S3DVertex(min.X,min.Y,min.Z, 0,0,-1, c, txc[20],txc[23]),
96         };
97
98         for(s32 j=0; j<24; j++)
99         {
100                 int tileindex = MYMIN(j/4, tilecount-1);
101                 vertices[j].TCoords *= tiles[tileindex].texture.size;
102                 vertices[j].TCoords += tiles[tileindex].texture.pos;
103         }
104
105         u16 indices[] = {0,1,2,2,3,0};
106
107         // Add to mesh collector
108         for(s32 j=0; j<24; j+=4)
109         {
110                 int tileindex = MYMIN(j/4, tilecount-1);
111                 collector->append(tiles[tileindex],
112                                 vertices+j, 4, indices, 6);
113         }
114 }
115
116 void mapblock_mesh_generate_special(MeshMakeData *data,
117                 MeshCollector &collector)
118 {
119         INodeDefManager *nodedef = data->m_gamedef->ndef();
120
121         // 0ms
122         //TimeTaker timer("mapblock_mesh_generate_special()");
123
124         /*
125                 Some settings
126         */
127         bool new_style_water = g_settings->getBool("new_style_water");
128         
129         float node_liquid_level = 1.0;
130         if(new_style_water)
131                 node_liquid_level = 0.85;
132         
133         v3s16 blockpos_nodes = data->m_blockpos*MAP_BLOCKSIZE;
134
135         for(s16 z=0; z<MAP_BLOCKSIZE; z++)
136         for(s16 y=0; y<MAP_BLOCKSIZE; y++)
137         for(s16 x=0; x<MAP_BLOCKSIZE; x++)
138         {
139                 v3s16 p(x,y,z);
140
141                 MapNode n = data->m_vmanip.getNodeNoEx(blockpos_nodes+p);
142                 const ContentFeatures &f = nodedef->get(n);
143
144                 // Only solidness=0 stuff is drawn here
145                 if(f.solidness != 0)
146                         continue;
147                 
148                 switch(f.drawtype){
149                 default:
150                         infostream<<"Got "<<f.drawtype<<std::endl;
151                         assert(0);
152                         break;
153                 case NDT_AIRLIKE:
154                         break;
155                 case NDT_LIQUID:
156                 {
157                         /*
158                                 Add water sources to mesh if using new style
159                         */
160                         TileSpec tile_liquid = f.special_tiles[0];
161                         AtlasPointer &pa_liquid = tile_liquid.texture;
162
163                         bool top_is_air = false;
164                         MapNode n = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y+1,z));
165                         if(n.getContent() == CONTENT_AIR)
166                                 top_is_air = true;
167                         
168                         if(top_is_air == false)
169                                 continue;
170
171                         u16 l = getInteriorLight(n, 0, data);
172                         video::SColor c = MapBlock_LightColor(f.alpha, l);
173                         
174                         video::S3DVertex vertices[4] =
175                         {
176                                 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
177                                                 pa_liquid.x0(), pa_liquid.y1()),
178                                 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
179                                                 pa_liquid.x1(), pa_liquid.y1()),
180                                 video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c,
181                                                 pa_liquid.x1(), pa_liquid.y0()),
182                                 video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c,
183                                                 pa_liquid.x0(), pa_liquid.y0()),
184                         };
185
186                         v3f offset(p.X, p.Y + (-0.5+node_liquid_level)*BS, p.Z);
187                         for(s32 i=0; i<4; i++)
188                         {
189                                 vertices[i].Pos += offset;
190                         }
191
192                         u16 indices[] = {0,1,2,2,3,0};
193                         // Add to mesh collector
194                         collector.append(tile_liquid, vertices, 4, indices, 6);
195                 break;}
196                 case NDT_FLOWINGLIQUID:
197                 {
198                         /*
199                                 Add flowing liquid to mesh
200                         */
201                         TileSpec tile_liquid = f.special_tiles[0];
202                         TileSpec tile_liquid_bfculled = f.special_tiles[1];
203                         AtlasPointer &pa_liquid = tile_liquid.texture;
204
205                         bool top_is_same_liquid = false;
206                         MapNode ntop = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y+1,z));
207                         content_t c_flowing = nodedef->getId(f.liquid_alternative_flowing);
208                         content_t c_source = nodedef->getId(f.liquid_alternative_source);
209                         if(ntop.getContent() == c_flowing || ntop.getContent() == c_source)
210                                 top_is_same_liquid = true;
211                         
212                         u16 l = 0;
213                         // Use the light of the node on top if possible
214                         if(nodedef->get(ntop).param_type == CPT_LIGHT)
215                                 l = getInteriorLight(ntop, 0, data);
216                         // Otherwise use the light of this node (the liquid)
217                         else
218                                 l = getInteriorLight(n, 0, data);
219                         video::SColor c = MapBlock_LightColor(f.alpha, l);
220                         
221                         // Neighbor liquid levels (key = relative position)
222                         // Includes current node
223                         core::map<v3s16, f32> neighbor_levels;
224                         core::map<v3s16, content_t> neighbor_contents;
225                         core::map<v3s16, u8> neighbor_flags;
226                         const u8 neighborflag_top_is_same_liquid = 0x01;
227                         v3s16 neighbor_dirs[9] = {
228                                 v3s16(0,0,0),
229                                 v3s16(0,0,1),
230                                 v3s16(0,0,-1),
231                                 v3s16(1,0,0),
232                                 v3s16(-1,0,0),
233                                 v3s16(1,0,1),
234                                 v3s16(-1,0,-1),
235                                 v3s16(1,0,-1),
236                                 v3s16(-1,0,1),
237                         };
238                         for(u32 i=0; i<9; i++)
239                         {
240                                 content_t content = CONTENT_AIR;
241                                 float level = -0.5 * BS;
242                                 u8 flags = 0;
243                                 // Check neighbor
244                                 v3s16 p2 = p + neighbor_dirs[i];
245                                 MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
246                                 if(n2.getContent() != CONTENT_IGNORE)
247                                 {
248                                         content = n2.getContent();
249
250                                         if(n2.getContent() == c_source)
251                                                 level = (-0.5+node_liquid_level) * BS;
252                                         else if(n2.getContent() == c_flowing)
253                                                 level = (-0.5 + ((float)(n2.param2&LIQUID_LEVEL_MASK)
254                                                                 + 0.5) / 8.0 * node_liquid_level) * BS;
255
256                                         // Check node above neighbor.
257                                         // NOTE: This doesn't get executed if neighbor
258                                         //       doesn't exist
259                                         p2.Y += 1;
260                                         n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
261                                         if(n2.getContent() == c_source ||
262                                                         n2.getContent() == c_flowing)
263                                                 flags |= neighborflag_top_is_same_liquid;
264                                 }
265                                 
266                                 neighbor_levels.insert(neighbor_dirs[i], level);
267                                 neighbor_contents.insert(neighbor_dirs[i], content);
268                                 neighbor_flags.insert(neighbor_dirs[i], flags);
269                         }
270
271                         // Corner heights (average between four liquids)
272                         f32 corner_levels[4];
273                         
274                         v3s16 halfdirs[4] = {
275                                 v3s16(0,0,0),
276                                 v3s16(1,0,0),
277                                 v3s16(1,0,1),
278                                 v3s16(0,0,1),
279                         };
280                         for(u32 i=0; i<4; i++)
281                         {
282                                 v3s16 cornerdir = halfdirs[i];
283                                 float cornerlevel = 0;
284                                 u32 valid_count = 0;
285                                 u32 air_count = 0;
286                                 for(u32 j=0; j<4; j++)
287                                 {
288                                         v3s16 neighbordir = cornerdir - halfdirs[j];
289                                         content_t content = neighbor_contents[neighbordir];
290                                         // If top is liquid, draw starting from top of node
291                                         if(neighbor_flags[neighbordir] &
292                                                         neighborflag_top_is_same_liquid)
293                                         {
294                                                 cornerlevel = 0.5*BS;
295                                                 valid_count = 1;
296                                                 break;
297                                         }
298                                         // Source is always the same height
299                                         else if(content == c_source)
300                                         {
301                                                 cornerlevel = (-0.5+node_liquid_level)*BS;
302                                                 valid_count = 1;
303                                                 break;
304                                         }
305                                         // Flowing liquid has level information
306                                         else if(content == c_flowing)
307                                         {
308                                                 cornerlevel += neighbor_levels[neighbordir];
309                                                 valid_count++;
310                                         }
311                                         else if(content == CONTENT_AIR)
312                                         {
313                                                 air_count++;
314                                         }
315                                 }
316                                 if(air_count >= 2)
317                                         cornerlevel = -0.5*BS;
318                                 else if(valid_count > 0)
319                                         cornerlevel /= valid_count;
320                                 corner_levels[i] = cornerlevel;
321                         }
322
323                         /*
324                                 Generate sides
325                         */
326
327                         v3s16 side_dirs[4] = {
328                                 v3s16(1,0,0),
329                                 v3s16(-1,0,0),
330                                 v3s16(0,0,1),
331                                 v3s16(0,0,-1),
332                         };
333                         s16 side_corners[4][2] = {
334                                 {1, 2},
335                                 {3, 0},
336                                 {2, 3},
337                                 {0, 1},
338                         };
339                         for(u32 i=0; i<4; i++)
340                         {
341                                 v3s16 dir = side_dirs[i];
342
343                                 /*
344                                         If our topside is liquid and neighbor's topside
345                                         is liquid, don't draw side face
346                                 */
347                                 if(top_is_same_liquid &&
348                                                 neighbor_flags[dir] & neighborflag_top_is_same_liquid)
349                                         continue;
350
351                                 content_t neighbor_content = neighbor_contents[dir];
352                                 const ContentFeatures &n_feat = nodedef->get(neighbor_content);
353                                 
354                                 // Don't draw face if neighbor is blocking the view
355                                 if(n_feat.solidness == 2)
356                                         continue;
357                                 
358                                 bool neighbor_is_same_liquid = (neighbor_content == c_source
359                                                 || neighbor_content == c_flowing);
360                                 
361                                 // Don't draw any faces if neighbor same is liquid and top is
362                                 // same liquid
363                                 if(neighbor_is_same_liquid == true
364                                                 && top_is_same_liquid == false)
365                                         continue;
366
367                                 // Use backface culled material if neighbor doesn't have a
368                                 // solidness of 0
369                                 const TileSpec *current_tile = &tile_liquid;
370                                 if(n_feat.solidness != 0 || n_feat.visual_solidness != 0)
371                                         current_tile = &tile_liquid_bfculled;
372                                 
373                                 video::S3DVertex vertices[4] =
374                                 {
375                                         video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
376                                                         pa_liquid.x0(), pa_liquid.y1()),
377                                         video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
378                                                         pa_liquid.x1(), pa_liquid.y1()),
379                                         video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
380                                                         pa_liquid.x1(), pa_liquid.y0()),
381                                         video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
382                                                         pa_liquid.x0(), pa_liquid.y0()),
383                                 };
384                                 
385                                 /*
386                                         If our topside is liquid, set upper border of face
387                                         at upper border of node
388                                 */
389                                 if(top_is_same_liquid)
390                                 {
391                                         vertices[2].Pos.Y = 0.5*BS;
392                                         vertices[3].Pos.Y = 0.5*BS;
393                                 }
394                                 /*
395                                         Otherwise upper position of face is corner levels
396                                 */
397                                 else
398                                 {
399                                         vertices[2].Pos.Y = corner_levels[side_corners[i][0]];
400                                         vertices[3].Pos.Y = corner_levels[side_corners[i][1]];
401                                 }
402                                 
403                                 /*
404                                         If neighbor is liquid, lower border of face is corner
405                                         liquid levels
406                                 */
407                                 if(neighbor_is_same_liquid)
408                                 {
409                                         vertices[0].Pos.Y = corner_levels[side_corners[i][1]];
410                                         vertices[1].Pos.Y = corner_levels[side_corners[i][0]];
411                                 }
412                                 /*
413                                         If neighbor is not liquid, lower border of face is
414                                         lower border of node
415                                 */
416                                 else
417                                 {
418                                         vertices[0].Pos.Y = -0.5*BS;
419                                         vertices[1].Pos.Y = -0.5*BS;
420                                 }
421                                 
422                                 for(s32 j=0; j<4; j++)
423                                 {
424                                         if(dir == v3s16(0,0,1))
425                                                 vertices[j].Pos.rotateXZBy(0);
426                                         if(dir == v3s16(0,0,-1))
427                                                 vertices[j].Pos.rotateXZBy(180);
428                                         if(dir == v3s16(-1,0,0))
429                                                 vertices[j].Pos.rotateXZBy(90);
430                                         if(dir == v3s16(1,0,-0))
431                                                 vertices[j].Pos.rotateXZBy(-90);
432                                                 
433                                         // Do this to not cause glitches when two liquids are
434                                         // side-by-side
435                                         /*if(neighbor_is_same_liquid == false){
436                                                 vertices[j].Pos.X *= 0.98;
437                                                 vertices[j].Pos.Z *= 0.98;
438                                         }*/
439
440                                         vertices[j].Pos += intToFloat(p, BS);
441                                 }
442
443                                 u16 indices[] = {0,1,2,2,3,0};
444                                 // Add to mesh collector
445                                 collector.append(*current_tile, vertices, 4, indices, 6);
446                         }
447                         
448                         /*
449                                 Generate top side, if appropriate
450                         */
451                         
452                         if(top_is_same_liquid == false)
453                         {
454                                 video::S3DVertex vertices[4] =
455                                 {
456                                         video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
457                                                         pa_liquid.x0(), pa_liquid.y1()),
458                                         video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
459                                                         pa_liquid.x1(), pa_liquid.y1()),
460                                         video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c,
461                                                         pa_liquid.x1(), pa_liquid.y0()),
462                                         video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c,
463                                                         pa_liquid.x0(), pa_liquid.y0()),
464                                 };
465                                 
466                                 // This fixes a strange bug
467                                 s32 corner_resolve[4] = {3,2,1,0};
468
469                                 for(s32 i=0; i<4; i++)
470                                 {
471                                         //vertices[i].Pos.Y += liquid_level;
472                                         //vertices[i].Pos.Y += neighbor_levels[v3s16(0,0,0)];
473                                         s32 j = corner_resolve[i];
474                                         vertices[i].Pos.Y += corner_levels[j];
475                                         vertices[i].Pos += intToFloat(p, BS);
476                                 }
477
478                                 u16 indices[] = {0,1,2,2,3,0};
479                                 // Add to mesh collector
480                                 collector.append(tile_liquid, vertices, 4, indices, 6);
481                         }
482                 break;}
483                 case NDT_GLASSLIKE:
484                 {
485                         TileSpec tile = getNodeTile(n, p, v3s16(0,0,0), data);
486                         AtlasPointer ap = tile.texture;
487
488                         u16 l = getInteriorLight(n, 1, data);
489                         video::SColor c = MapBlock_LightColor(255, l);
490
491                         for(u32 j=0; j<6; j++)
492                         {
493                                 // Check this neighbor
494                                 v3s16 n2p = blockpos_nodes + p + g_6dirs[j];
495                                 MapNode n2 = data->m_vmanip.getNodeNoEx(n2p);
496                                 // Don't make face if neighbor is of same type
497                                 if(n2.getContent() == n.getContent())
498                                         continue;
499
500                                 // The face at Z+
501                                 video::S3DVertex vertices[4] =
502                                 {
503                                         video::S3DVertex(-BS/2,-BS/2,BS/2, 0,0,0, c,
504                                                 ap.x0(), ap.y1()),
505                                         video::S3DVertex(BS/2,-BS/2,BS/2, 0,0,0, c,
506                                                 ap.x1(), ap.y1()),
507                                         video::S3DVertex(BS/2,BS/2,BS/2, 0,0,0, c,
508                                                 ap.x1(), ap.y0()),
509                                         video::S3DVertex(-BS/2,BS/2,BS/2, 0,0,0, c,
510                                                 ap.x0(), ap.y0()),
511                                 };
512                                 
513                                 // Rotations in the g_6dirs format
514                                 if(j == 0) // Z+
515                                         for(u16 i=0; i<4; i++)
516                                                 vertices[i].Pos.rotateXZBy(0);
517                                 else if(j == 1) // Y+
518                                         for(u16 i=0; i<4; i++)
519                                                 vertices[i].Pos.rotateYZBy(-90);
520                                 else if(j == 2) // X+
521                                         for(u16 i=0; i<4; i++)
522                                                 vertices[i].Pos.rotateXZBy(-90);
523                                 else if(j == 3) // Z-
524                                         for(u16 i=0; i<4; i++)
525                                                 vertices[i].Pos.rotateXZBy(180);
526                                 else if(j == 4) // Y-
527                                         for(u16 i=0; i<4; i++)
528                                                 vertices[i].Pos.rotateYZBy(90);
529                                 else if(j == 5) // X-
530                                         for(u16 i=0; i<4; i++)
531                                                 vertices[i].Pos.rotateXZBy(90);
532
533                                 for(u16 i=0; i<4; i++){
534                                         vertices[i].Pos += intToFloat(p, BS);
535                                 }
536
537                                 u16 indices[] = {0,1,2,2,3,0};
538                                 // Add to mesh collector
539                                 collector.append(tile, vertices, 4, indices, 6);
540                         }
541                 break;}
542                 case NDT_ALLFACES:
543                 {
544                         TileSpec tile_leaves = getNodeTile(n, p,
545                                         v3s16(0,0,0), data);
546                         AtlasPointer pa_leaves = tile_leaves.texture;
547
548                         u16 l = getInteriorLight(n, 1, data);
549                         video::SColor c = MapBlock_LightColor(255, l);
550
551                         v3f pos = intToFloat(p, BS);
552                         aabb3f box(-BS/2,-BS/2,-BS/2,BS/2,BS/2,BS/2);
553                         box.MinEdge += pos;
554                         box.MaxEdge += pos;
555                         makeCuboid(&collector, box, &tile_leaves, 1, c, NULL);
556                 break;}
557                 case NDT_ALLFACES_OPTIONAL:
558                         // This is always pre-converted to something else
559                         assert(0);
560                         break;
561                 case NDT_TORCHLIKE:
562                 {
563                         v3s16 dir = n.getWallMountedDir(nodedef);
564                         
565                         u8 tileindex = 0;
566                         if(dir == v3s16(0,-1,0)){
567                                 tileindex = 0; // floor
568                         } else if(dir == v3s16(0,1,0)){
569                                 tileindex = 1; // ceiling
570                         // For backwards compatibility
571                         } else if(dir == v3s16(0,0,0)){
572                                 tileindex = 0; // floor
573                         } else {
574                                 tileindex = 2; // side
575                         }
576
577                         TileSpec tile = getNodeTileN(n, p, tileindex, data);
578                         tile.material_flags &= ~MATERIAL_FLAG_BACKFACE_CULLING;
579                         tile.material_flags |= MATERIAL_FLAG_CRACK_OVERLAY;
580
581                         AtlasPointer ap = tile.texture;
582
583                         video::SColor c(255,255,255,255);
584
585                         // Wall at X+ of node
586                         video::S3DVertex vertices[4] =
587                         {
588                                 video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c,
589                                                 ap.x0(), ap.y1()),
590                                 video::S3DVertex(BS/2,-BS/2,0, 0,0,0, c,
591                                                 ap.x1(), ap.y1()),
592                                 video::S3DVertex(BS/2,BS/2,0, 0,0,0, c,
593                                                 ap.x1(), ap.y0()),
594                                 video::S3DVertex(-BS/2,BS/2,0, 0,0,0, c,
595                                                 ap.x0(), ap.y0()),
596                         };
597
598                         for(s32 i=0; i<4; i++)
599                         {
600                                 if(dir == v3s16(1,0,0))
601                                         vertices[i].Pos.rotateXZBy(0);
602                                 if(dir == v3s16(-1,0,0))
603                                         vertices[i].Pos.rotateXZBy(180);
604                                 if(dir == v3s16(0,0,1))
605                                         vertices[i].Pos.rotateXZBy(90);
606                                 if(dir == v3s16(0,0,-1))
607                                         vertices[i].Pos.rotateXZBy(-90);
608                                 if(dir == v3s16(0,-1,0))
609                                         vertices[i].Pos.rotateXZBy(45);
610                                 if(dir == v3s16(0,1,0))
611                                         vertices[i].Pos.rotateXZBy(-45);
612
613                                 vertices[i].Pos += intToFloat(p, BS);
614                         }
615
616                         u16 indices[] = {0,1,2,2,3,0};
617                         // Add to mesh collector
618                         collector.append(tile, vertices, 4, indices, 6);
619                 break;}
620                 case NDT_SIGNLIKE:
621                 {
622                         TileSpec tile = getNodeTileN(n, p, 0, data);
623                         tile.material_flags &= ~MATERIAL_FLAG_BACKFACE_CULLING;
624                         tile.material_flags |= MATERIAL_FLAG_CRACK_OVERLAY;
625                         AtlasPointer ap = tile.texture;
626
627                         u16 l = getInteriorLight(n, 0, data);
628                         video::SColor c = MapBlock_LightColor(255, l);
629                                 
630                         float d = (float)BS/16;
631                         // Wall at X+ of node
632                         video::S3DVertex vertices[4] =
633                         {
634                                 video::S3DVertex(BS/2-d,BS/2,BS/2, 0,0,0, c,
635                                                 ap.x0(), ap.y0()),
636                                 video::S3DVertex(BS/2-d,BS/2,-BS/2, 0,0,0, c,
637                                                 ap.x1(), ap.y0()),
638                                 video::S3DVertex(BS/2-d,-BS/2,-BS/2, 0,0,0, c,
639                                                 ap.x1(), ap.y1()),
640                                 video::S3DVertex(BS/2-d,-BS/2,BS/2, 0,0,0, c,
641                                                 ap.x0(), ap.y1()),
642                         };
643
644                         v3s16 dir = n.getWallMountedDir(nodedef);
645
646                         for(s32 i=0; i<4; i++)
647                         {
648                                 if(dir == v3s16(1,0,0))
649                                         vertices[i].Pos.rotateXZBy(0);
650                                 if(dir == v3s16(-1,0,0))
651                                         vertices[i].Pos.rotateXZBy(180);
652                                 if(dir == v3s16(0,0,1))
653                                         vertices[i].Pos.rotateXZBy(90);
654                                 if(dir == v3s16(0,0,-1))
655                                         vertices[i].Pos.rotateXZBy(-90);
656                                 if(dir == v3s16(0,-1,0))
657                                         vertices[i].Pos.rotateXYBy(-90);
658                                 if(dir == v3s16(0,1,0))
659                                         vertices[i].Pos.rotateXYBy(90);
660
661                                 vertices[i].Pos += intToFloat(p, BS);
662                         }
663
664                         u16 indices[] = {0,1,2,2,3,0};
665                         // Add to mesh collector
666                         collector.append(tile, vertices, 4, indices, 6);
667                 break;}
668                 case NDT_PLANTLIKE:
669                 {
670                         TileSpec tile = getNodeTileN(n, p, 0, data);
671                         tile.material_flags |= MATERIAL_FLAG_CRACK_OVERLAY;
672                         AtlasPointer ap = tile.texture;
673                         
674                         u16 l = getInteriorLight(n, 1, data);
675                         video::SColor c = MapBlock_LightColor(255, l);
676
677                         for(u32 j=0; j<4; j++)
678                         {
679                                 video::S3DVertex vertices[4] =
680                                 {
681                                         video::S3DVertex(-BS/2*f.visual_scale,-BS/2,0, 0,0,0, c,
682                                                 ap.x0(), ap.y1()),
683                                         video::S3DVertex( BS/2*f.visual_scale,-BS/2,0, 0,0,0, c,
684                                                 ap.x1(), ap.y1()),
685                                         video::S3DVertex( BS/2*f.visual_scale,
686                                                 -BS/2 + f.visual_scale*BS,0, 0,0,0, c,
687                                                 ap.x1(), ap.y0()),
688                                         video::S3DVertex(-BS/2*f.visual_scale,
689                                                 -BS/2 + f.visual_scale*BS,0, 0,0,0, c,
690                                                 ap.x0(), ap.y0()),
691                                 };
692
693                                 if(j == 0)
694                                 {
695                                         for(u16 i=0; i<4; i++)
696                                                 vertices[i].Pos.rotateXZBy(45);
697                                 }
698                                 else if(j == 1)
699                                 {
700                                         for(u16 i=0; i<4; i++)
701                                                 vertices[i].Pos.rotateXZBy(-45);
702                                 }
703                                 else if(j == 2)
704                                 {
705                                         for(u16 i=0; i<4; i++)
706                                                 vertices[i].Pos.rotateXZBy(135);
707                                 }
708                                 else if(j == 3)
709                                 {
710                                         for(u16 i=0; i<4; i++)
711                                                 vertices[i].Pos.rotateXZBy(-135);
712                                 }
713
714                                 for(u16 i=0; i<4; i++)
715                                 {
716                                         vertices[i].Pos *= f.visual_scale;
717                                         vertices[i].Pos += intToFloat(p, BS);
718                                 }
719
720                                 u16 indices[] = {0,1,2,2,3,0};
721                                 // Add to mesh collector
722                                 collector.append(tile, vertices, 4, indices, 6);
723                         }
724                 break;}
725                 case NDT_FENCELIKE:
726                 {
727                         TileSpec tile = getNodeTile(n, p, v3s16(0,0,0), data);
728                         TileSpec tile_nocrack = tile;
729                         tile_nocrack.material_flags &= ~MATERIAL_FLAG_CRACK;
730                         
731                         // A hack to put wood the right way around in the posts
732                         ITextureSource *tsrc = data->m_gamedef->tsrc();
733                         TileSpec tile_rot = tile;
734                         tile_rot.texture = tsrc->getTexture(tsrc->getTextureName(
735                                         tile.texture.id) + "^[transformR90");
736                                         
737                         u16 l = getInteriorLight(n, 1, data);
738                         video::SColor c = MapBlock_LightColor(255, l);
739
740                         const f32 post_rad=(f32)BS/8;
741                         const f32 bar_rad=(f32)BS/16;
742                         const f32 bar_len=(f32)(BS/2)-post_rad;
743
744                         v3f pos = intToFloat(p, BS);
745
746                         // The post - always present
747                         aabb3f post(-post_rad,-BS/2,-post_rad,post_rad,BS/2,post_rad);
748                         post.MinEdge += pos;
749                         post.MaxEdge += pos;
750                         f32 postuv[24]={
751                                         6/16.,6/16.,10/16.,10/16.,
752                                         6/16.,6/16.,10/16.,10/16.,
753                                         0/16.,0,4/16.,1,
754                                         4/16.,0,8/16.,1,
755                                         8/16.,0,12/16.,1,
756                                         12/16.,0,16/16.,1};
757                         makeCuboid(&collector, post, &tile_rot, 1, c, postuv);
758
759                         // Now a section of fence, +X, if there's a post there
760                         v3s16 p2 = p;
761                         p2.X++;
762                         MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
763                         const ContentFeatures *f2 = &nodedef->get(n2);
764                         if(f2->drawtype == NDT_FENCELIKE)
765                         {
766                                 aabb3f bar(-bar_len+BS/2,-bar_rad+BS/4,-bar_rad,
767                                                 bar_len+BS/2,bar_rad+BS/4,bar_rad);
768                                 bar.MinEdge += pos;
769                                 bar.MaxEdge += pos;
770                                 f32 xrailuv[24]={
771                                         0/16.,2/16.,16/16.,4/16.,
772                                         0/16.,4/16.,16/16.,6/16.,
773                                         6/16.,6/16.,8/16.,8/16.,
774                                         10/16.,10/16.,12/16.,12/16.,
775                                         0/16.,8/16.,16/16.,10/16.,
776                                         0/16.,14/16.,16/16.,16/16.};
777                                 makeCuboid(&collector, bar, &tile_nocrack, 1,
778                                                 c, xrailuv);
779                                 bar.MinEdge.Y -= BS/2;
780                                 bar.MaxEdge.Y -= BS/2;
781                                 makeCuboid(&collector, bar, &tile_nocrack, 1,
782                                                 c, xrailuv);
783                         }
784
785                         // Now a section of fence, +Z, if there's a post there
786                         p2 = p;
787                         p2.Z++;
788                         n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
789                         f2 = &nodedef->get(n2);
790                         if(f2->drawtype == NDT_FENCELIKE)
791                         {
792                                 aabb3f bar(-bar_rad,-bar_rad+BS/4,-bar_len+BS/2,
793                                                 bar_rad,bar_rad+BS/4,bar_len+BS/2);
794                                 bar.MinEdge += pos;
795                                 bar.MaxEdge += pos;
796                                 f32 zrailuv[24]={
797                                         3/16.,1/16.,5/16.,5/16., // cannot rotate; stretch
798                                         4/16.,1/16.,6/16.,5/16., // for wood texture instead
799                                         0/16.,9/16.,16/16.,11/16.,
800                                         0/16.,6/16.,16/16.,8/16.,
801                                         6/16.,6/16.,8/16.,8/16.,
802                                         10/16.,10/16.,12/16.,12/16.};
803                                 makeCuboid(&collector, bar, &tile_nocrack, 1,
804                                                 c, zrailuv);
805                                 bar.MinEdge.Y -= BS/2;
806                                 bar.MaxEdge.Y -= BS/2;
807                                 makeCuboid(&collector, bar, &tile_nocrack, 1,
808                                                 c, zrailuv);
809                         }
810                 break;}
811                 case NDT_RAILLIKE:
812                 {
813                         bool is_rail_x [] = { false, false };  /* x-1, x+1 */
814                         bool is_rail_z [] = { false, false };  /* z-1, z+1 */
815
816                         bool is_rail_z_minus_y [] = { false, false };  /* z-1, z+1; y-1 */
817                         bool is_rail_x_minus_y [] = { false, false };  /* x-1, z+1; y-1 */
818                         bool is_rail_z_plus_y [] = { false, false };  /* z-1, z+1; y+1 */
819                         bool is_rail_x_plus_y [] = { false, false };  /* x-1, x+1; y+1 */
820
821                         MapNode n_minus_x = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x-1,y,z));
822                         MapNode n_plus_x = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x+1,y,z));
823                         MapNode n_minus_z = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y,z-1));
824                         MapNode n_plus_z = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y,z+1));
825                         MapNode n_plus_x_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x+1, y+1, z));
826                         MapNode n_plus_x_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x+1, y-1, z));
827                         MapNode n_minus_x_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x-1, y+1, z));
828                         MapNode n_minus_x_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x-1, y-1, z));
829                         MapNode n_plus_z_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y+1, z+1));
830                         MapNode n_minus_z_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y+1, z-1));
831                         MapNode n_plus_z_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y-1, z+1));
832                         MapNode n_minus_z_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y-1, z-1));
833                         
834                         content_t thiscontent = n.getContent();
835                         if(n_minus_x.getContent() == thiscontent)
836                                 is_rail_x[0] = true;
837                         if (n_minus_x_minus_y.getContent() == thiscontent)
838                                 is_rail_x_minus_y[0] = true;
839                         if(n_minus_x_plus_y.getContent() == thiscontent)
840                                 is_rail_x_plus_y[0] = true;
841
842                         if(n_plus_x.getContent() == thiscontent)
843                                 is_rail_x[1] = true;
844                         if (n_plus_x_minus_y.getContent() == thiscontent)
845                                 is_rail_x_minus_y[1] = true;
846                         if(n_plus_x_plus_y.getContent() == thiscontent)
847                                 is_rail_x_plus_y[1] = true;
848
849                         if(n_minus_z.getContent() == thiscontent)
850                                 is_rail_z[0] = true;
851                         if (n_minus_z_minus_y.getContent() == thiscontent)
852                                 is_rail_z_minus_y[0] = true;
853                         if(n_minus_z_plus_y.getContent() == thiscontent)
854                                 is_rail_z_plus_y[0] = true;
855
856                         if(n_plus_z.getContent() == thiscontent)
857                                 is_rail_z[1] = true;
858                         if (n_plus_z_minus_y.getContent() == thiscontent)
859                                 is_rail_z_minus_y[1] = true;
860                         if(n_plus_z_plus_y.getContent() == thiscontent)
861                                 is_rail_z_plus_y[1] = true;
862
863
864                         bool is_rail_x_all[] = {false, false};
865                         bool is_rail_z_all[] = {false, false};
866                         is_rail_x_all[0]=is_rail_x[0] || is_rail_x_minus_y[0] || is_rail_x_plus_y[0];
867                         is_rail_x_all[1]=is_rail_x[1] || is_rail_x_minus_y[1] || is_rail_x_plus_y[1];
868                         is_rail_z_all[0]=is_rail_z[0] || is_rail_z_minus_y[0] || is_rail_z_plus_y[0];
869                         is_rail_z_all[1]=is_rail_z[1] || is_rail_z_minus_y[1] || is_rail_z_plus_y[1];
870
871                         bool is_straight = (is_rail_x_all[0] && is_rail_x_all[1]) || (is_rail_z_all[0] && is_rail_z_all[1]);//is really straight, rails on both sides
872                         int adjacencies = is_rail_x_all[0] + is_rail_x_all[1] + is_rail_z_all[0] + is_rail_z_all[1];
873
874                         if (is_rail_x_plus_y[0] || is_rail_x_plus_y[1] || is_rail_z_plus_y[0] || is_rail_z_plus_y[1]) //is straight because sloped
875                         {
876                                 adjacencies = 5; //5 means sloped
877                                 is_straight = true;
878                         }
879
880                         // Assign textures
881                         u8 tileindex = 0; // straight
882                         if(adjacencies < 2)
883                                 tileindex = 0; // straight
884                         else if(adjacencies == 2)
885                         {
886                                 if(is_straight)
887                                         tileindex = 0; // straight
888                                 else
889                                         tileindex = 1; // curved
890                         }
891                         else if(adjacencies == 3)
892                                 tileindex = 2; // t-junction
893                         else if(adjacencies == 4)
894                                 tileindex = 3; // crossing
895
896                         TileSpec tile = getNodeTileN(n, p, tileindex, data);
897                         tile.material_flags &= ~MATERIAL_FLAG_BACKFACE_CULLING;
898                         tile.material_flags |= MATERIAL_FLAG_CRACK_OVERLAY;
899
900                         AtlasPointer ap = tile.texture;
901                         
902                         u16 l = getInteriorLight(n, 0, data);
903                         video::SColor c = MapBlock_LightColor(255, l);
904
905                         float d = (float)BS/64;
906                         
907                         char g=-1;
908                         if (is_rail_x_plus_y[0] || is_rail_x_plus_y[1] || is_rail_z_plus_y[0] || is_rail_z_plus_y[1])
909                                 g=1; //Object is at a slope
910
911                         video::S3DVertex vertices[4] =
912                         {
913                                         video::S3DVertex(-BS/2,-BS/2+d,-BS/2, 0,0,0, c,
914                                                         ap.x0(), ap.y1()),
915                                         video::S3DVertex(BS/2,-BS/2+d,-BS/2, 0,0,0, c,
916                                                         ap.x1(), ap.y1()),
917                                         video::S3DVertex(BS/2,g*BS/2+d,BS/2, 0,0,0, c,
918                                                         ap.x1(), ap.y0()),
919                                         video::S3DVertex(-BS/2,g*BS/2+d,BS/2, 0,0,0, c,
920                                                         ap.x0(), ap.y0()),
921                         };
922
923
924                         // Rotate textures
925                         int angle = 0;
926
927                         if(adjacencies == 1)
928                         {
929                                 if(is_rail_x_all[0] || is_rail_x_all[1])
930                                         angle = 90;
931                         }
932                         if(adjacencies == 2)
933                         {
934                                 if(is_rail_x_all[0] && is_rail_x_all[1])
935                                 {
936                                         angle = 90;
937                                 }
938                                 if(is_rail_z_all[0] && is_rail_z_all[1])
939                                 {
940                                         if (n_minus_z_plus_y.getContent() == thiscontent) angle = 180;
941                                 }
942                                 else if(is_rail_x_all[0] && is_rail_z_all[0])
943                                         angle = 270;
944                                 else if(is_rail_x_all[0] && is_rail_z_all[1])
945                                         angle = 180;
946                                 else if(is_rail_x_all[1] && is_rail_z_all[1])
947                                         angle = 90;
948                         }
949                         if(adjacencies == 3)
950                         {
951                                 if(!is_rail_x_all[0])
952                                         angle=0;
953                                 if(!is_rail_x_all[1])
954                                         angle=180;
955                                 if(!is_rail_z_all[0])
956                                         angle=90;
957                                 if(!is_rail_z_all[1])
958                                         angle=270;
959                         }
960                         //adjacencies 4: Crossing
961                         if(adjacencies == 5) //sloped
962                         {
963                                 if(is_rail_z_plus_y[0])
964                                         angle = 180;
965                                 if(is_rail_x_plus_y[0])
966                                         angle = 90;
967                                 if(is_rail_x_plus_y[1])
968                                         angle = -90;
969                         }
970
971                         if(angle != 0) {
972                                 for(u16 i=0; i<4; i++)
973                                         vertices[i].Pos.rotateXZBy(angle);
974                         }
975
976                         for(s32 i=0; i<4; i++)
977                         {
978                                 vertices[i].Pos += intToFloat(p, BS);
979                         }
980
981                         u16 indices[] = {0,1,2,2,3,0};
982                         collector.append(tile, vertices, 4, indices, 6);
983                 break;}
984                 }
985         }
986 }
987