]> git.lizzy.rs Git - minetest.git/blob - src/content_mapblock.cpp
Node texture animation
[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 Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "content_mapblock.h"
21
22 #include "main.h" // For g_settings
23 #include "mapblock_mesh.h" // For MapBlock_LightColor() and MeshCollector
24 #include "settings.h"
25 #include "nodedef.h"
26 #include "tile.h"
27 #include "gamedef.h"
28
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                         // If this liquid emits light and doesn't contain light, draw
214                         // it at what it emits, for an increased effect
215                         u8 light_source = nodedef->get(n).light_source;
216                         if(light_source != 0){
217                                 //l = decode_light(undiminish_light(light_source));
218                                 l = decode_light(light_source);
219                                 l = l | (l<<8);
220                         }
221                         // Use the light of the node on top if possible
222                         else if(nodedef->get(ntop).param_type == CPT_LIGHT)
223                                 l = getInteriorLight(ntop, 0, data);
224                         // Otherwise use the light of this node (the liquid)
225                         else
226                                 l = getInteriorLight(n, 0, data);
227                         video::SColor c = MapBlock_LightColor(f.alpha, l);
228                         
229                         // Neighbor liquid levels (key = relative position)
230                         // Includes current node
231                         core::map<v3s16, f32> neighbor_levels;
232                         core::map<v3s16, content_t> neighbor_contents;
233                         core::map<v3s16, u8> neighbor_flags;
234                         const u8 neighborflag_top_is_same_liquid = 0x01;
235                         v3s16 neighbor_dirs[9] = {
236                                 v3s16(0,0,0),
237                                 v3s16(0,0,1),
238                                 v3s16(0,0,-1),
239                                 v3s16(1,0,0),
240                                 v3s16(-1,0,0),
241                                 v3s16(1,0,1),
242                                 v3s16(-1,0,-1),
243                                 v3s16(1,0,-1),
244                                 v3s16(-1,0,1),
245                         };
246                         for(u32 i=0; i<9; i++)
247                         {
248                                 content_t content = CONTENT_AIR;
249                                 float level = -0.5 * BS;
250                                 u8 flags = 0;
251                                 // Check neighbor
252                                 v3s16 p2 = p + neighbor_dirs[i];
253                                 MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
254                                 if(n2.getContent() != CONTENT_IGNORE)
255                                 {
256                                         content = n2.getContent();
257
258                                         if(n2.getContent() == c_source)
259                                                 level = (-0.5+node_liquid_level) * BS;
260                                         else if(n2.getContent() == c_flowing)
261                                                 level = (-0.5 + ((float)(n2.param2&LIQUID_LEVEL_MASK)
262                                                                 + 0.5) / 8.0 * node_liquid_level) * BS;
263
264                                         // Check node above neighbor.
265                                         // NOTE: This doesn't get executed if neighbor
266                                         //       doesn't exist
267                                         p2.Y += 1;
268                                         n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
269                                         if(n2.getContent() == c_source ||
270                                                         n2.getContent() == c_flowing)
271                                                 flags |= neighborflag_top_is_same_liquid;
272                                 }
273                                 
274                                 neighbor_levels.insert(neighbor_dirs[i], level);
275                                 neighbor_contents.insert(neighbor_dirs[i], content);
276                                 neighbor_flags.insert(neighbor_dirs[i], flags);
277                         }
278
279                         // Corner heights (average between four liquids)
280                         f32 corner_levels[4];
281                         
282                         v3s16 halfdirs[4] = {
283                                 v3s16(0,0,0),
284                                 v3s16(1,0,0),
285                                 v3s16(1,0,1),
286                                 v3s16(0,0,1),
287                         };
288                         for(u32 i=0; i<4; i++)
289                         {
290                                 v3s16 cornerdir = halfdirs[i];
291                                 float cornerlevel = 0;
292                                 u32 valid_count = 0;
293                                 u32 air_count = 0;
294                                 for(u32 j=0; j<4; j++)
295                                 {
296                                         v3s16 neighbordir = cornerdir - halfdirs[j];
297                                         content_t content = neighbor_contents[neighbordir];
298                                         // If top is liquid, draw starting from top of node
299                                         if(neighbor_flags[neighbordir] &
300                                                         neighborflag_top_is_same_liquid)
301                                         {
302                                                 cornerlevel = 0.5*BS;
303                                                 valid_count = 1;
304                                                 break;
305                                         }
306                                         // Source is always the same height
307                                         else if(content == c_source)
308                                         {
309                                                 cornerlevel = (-0.5+node_liquid_level)*BS;
310                                                 valid_count = 1;
311                                                 break;
312                                         }
313                                         // Flowing liquid has level information
314                                         else if(content == c_flowing)
315                                         {
316                                                 cornerlevel += neighbor_levels[neighbordir];
317                                                 valid_count++;
318                                         }
319                                         else if(content == CONTENT_AIR)
320                                         {
321                                                 air_count++;
322                                         }
323                                 }
324                                 if(air_count >= 2)
325                                         cornerlevel = -0.5*BS;
326                                 else if(valid_count > 0)
327                                         cornerlevel /= valid_count;
328                                 corner_levels[i] = cornerlevel;
329                         }
330
331                         /*
332                                 Generate sides
333                         */
334
335                         v3s16 side_dirs[4] = {
336                                 v3s16(1,0,0),
337                                 v3s16(-1,0,0),
338                                 v3s16(0,0,1),
339                                 v3s16(0,0,-1),
340                         };
341                         s16 side_corners[4][2] = {
342                                 {1, 2},
343                                 {3, 0},
344                                 {2, 3},
345                                 {0, 1},
346                         };
347                         for(u32 i=0; i<4; i++)
348                         {
349                                 v3s16 dir = side_dirs[i];
350
351                                 /*
352                                         If our topside is liquid and neighbor's topside
353                                         is liquid, don't draw side face
354                                 */
355                                 if(top_is_same_liquid &&
356                                                 neighbor_flags[dir] & neighborflag_top_is_same_liquid)
357                                         continue;
358
359                                 content_t neighbor_content = neighbor_contents[dir];
360                                 const ContentFeatures &n_feat = nodedef->get(neighbor_content);
361                                 
362                                 // Don't draw face if neighbor is blocking the view
363                                 if(n_feat.solidness == 2)
364                                         continue;
365                                 
366                                 bool neighbor_is_same_liquid = (neighbor_content == c_source
367                                                 || neighbor_content == c_flowing);
368                                 
369                                 // Don't draw any faces if neighbor same is liquid and top is
370                                 // same liquid
371                                 if(neighbor_is_same_liquid == true
372                                                 && top_is_same_liquid == false)
373                                         continue;
374
375                                 // Use backface culled material if neighbor doesn't have a
376                                 // solidness of 0
377                                 const TileSpec *current_tile = &tile_liquid;
378                                 if(n_feat.solidness != 0 || n_feat.visual_solidness != 0)
379                                         current_tile = &tile_liquid_bfculled;
380                                 
381                                 video::S3DVertex vertices[4] =
382                                 {
383                                         video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
384                                                         pa_liquid.x0(), pa_liquid.y1()),
385                                         video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
386                                                         pa_liquid.x1(), pa_liquid.y1()),
387                                         video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
388                                                         pa_liquid.x1(), pa_liquid.y0()),
389                                         video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
390                                                         pa_liquid.x0(), pa_liquid.y0()),
391                                 };
392                                 
393                                 /*
394                                         If our topside is liquid, set upper border of face
395                                         at upper border of node
396                                 */
397                                 if(top_is_same_liquid)
398                                 {
399                                         vertices[2].Pos.Y = 0.5*BS;
400                                         vertices[3].Pos.Y = 0.5*BS;
401                                 }
402                                 /*
403                                         Otherwise upper position of face is corner levels
404                                 */
405                                 else
406                                 {
407                                         vertices[2].Pos.Y = corner_levels[side_corners[i][0]];
408                                         vertices[3].Pos.Y = corner_levels[side_corners[i][1]];
409                                 }
410                                 
411                                 /*
412                                         If neighbor is liquid, lower border of face is corner
413                                         liquid levels
414                                 */
415                                 if(neighbor_is_same_liquid)
416                                 {
417                                         vertices[0].Pos.Y = corner_levels[side_corners[i][1]];
418                                         vertices[1].Pos.Y = corner_levels[side_corners[i][0]];
419                                 }
420                                 /*
421                                         If neighbor is not liquid, lower border of face is
422                                         lower border of node
423                                 */
424                                 else
425                                 {
426                                         vertices[0].Pos.Y = -0.5*BS;
427                                         vertices[1].Pos.Y = -0.5*BS;
428                                 }
429                                 
430                                 for(s32 j=0; j<4; j++)
431                                 {
432                                         if(dir == v3s16(0,0,1))
433                                                 vertices[j].Pos.rotateXZBy(0);
434                                         if(dir == v3s16(0,0,-1))
435                                                 vertices[j].Pos.rotateXZBy(180);
436                                         if(dir == v3s16(-1,0,0))
437                                                 vertices[j].Pos.rotateXZBy(90);
438                                         if(dir == v3s16(1,0,-0))
439                                                 vertices[j].Pos.rotateXZBy(-90);
440                                                 
441                                         // Do this to not cause glitches when two liquids are
442                                         // side-by-side
443                                         /*if(neighbor_is_same_liquid == false){
444                                                 vertices[j].Pos.X *= 0.98;
445                                                 vertices[j].Pos.Z *= 0.98;
446                                         }*/
447
448                                         vertices[j].Pos += intToFloat(p, BS);
449                                 }
450
451                                 u16 indices[] = {0,1,2,2,3,0};
452                                 // Add to mesh collector
453                                 collector.append(*current_tile, vertices, 4, indices, 6);
454                         }
455                         
456                         /*
457                                 Generate top side, if appropriate
458                         */
459                         
460                         if(top_is_same_liquid == false)
461                         {
462                                 video::S3DVertex vertices[4] =
463                                 {
464                                         video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
465                                                         pa_liquid.x0(), pa_liquid.y1()),
466                                         video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
467                                                         pa_liquid.x1(), pa_liquid.y1()),
468                                         video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c,
469                                                         pa_liquid.x1(), pa_liquid.y0()),
470                                         video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c,
471                                                         pa_liquid.x0(), pa_liquid.y0()),
472                                 };
473                                 
474                                 // To get backface culling right, the vertices need to go
475                                 // clockwise around the front of the face. And we happened to
476                                 // calculate corner levels in exact reverse order.
477                                 s32 corner_resolve[4] = {3,2,1,0};
478
479                                 for(s32 i=0; i<4; i++)
480                                 {
481                                         //vertices[i].Pos.Y += liquid_level;
482                                         //vertices[i].Pos.Y += neighbor_levels[v3s16(0,0,0)];
483                                         s32 j = corner_resolve[i];
484                                         vertices[i].Pos.Y += corner_levels[j];
485                                         vertices[i].Pos += intToFloat(p, BS);
486                                 }
487                                 
488                                 // Default downwards-flowing texture animation goes from 
489                                 // -Z towards +Z, thus the direction is +Z.
490                                 // Rotate texture to make animation go in flow direction
491                                 // Positive if liquid moves towards +Z
492                                 int dz = (corner_levels[side_corners[2][0]] +
493                                                 corner_levels[side_corners[2][1]] <
494                                                 corner_levels[side_corners[3][0]] +
495                                                 corner_levels[side_corners[3][1]]);
496                                 // Positive if liquid moves towards +X
497                                 int dx = (corner_levels[side_corners[0][0]] +
498                                                 corner_levels[side_corners[0][1]] <
499                                                 corner_levels[side_corners[1][0]] +
500                                                 corner_levels[side_corners[1][1]]);
501                                 // -X
502                                 if(-dx >= abs(dz))
503                                 {
504                                         v2f t = vertices[0].TCoords;
505                                         vertices[0].TCoords = vertices[1].TCoords;
506                                         vertices[1].TCoords = vertices[2].TCoords;
507                                         vertices[2].TCoords = vertices[3].TCoords;
508                                         vertices[3].TCoords = t;
509                                 }
510                                 // +X
511                                 if(dx >= abs(dz))
512                                 {
513                                         v2f t = vertices[0].TCoords;
514                                         vertices[0].TCoords = vertices[3].TCoords;
515                                         vertices[3].TCoords = vertices[2].TCoords;
516                                         vertices[2].TCoords = vertices[1].TCoords;
517                                         vertices[1].TCoords = t;
518                                 }
519                                 // -Z
520                                 if(-dz >= abs(dx))
521                                 {
522                                         v2f t = vertices[0].TCoords;
523                                         vertices[0].TCoords = vertices[3].TCoords;
524                                         vertices[3].TCoords = vertices[2].TCoords;
525                                         vertices[2].TCoords = vertices[1].TCoords;
526                                         vertices[1].TCoords = t;
527                                         t = vertices[0].TCoords;
528                                         vertices[0].TCoords = vertices[3].TCoords;
529                                         vertices[3].TCoords = vertices[2].TCoords;
530                                         vertices[2].TCoords = vertices[1].TCoords;
531                                         vertices[1].TCoords = t;
532                                 }
533
534                                 u16 indices[] = {0,1,2,2,3,0};
535                                 // Add to mesh collector
536                                 collector.append(tile_liquid, vertices, 4, indices, 6);
537                         }
538                 break;}
539                 case NDT_GLASSLIKE:
540                 {
541                         TileSpec tile = getNodeTile(n, p, v3s16(0,0,0), data);
542                         AtlasPointer ap = tile.texture;
543
544                         u16 l = getInteriorLight(n, 1, data);
545                         video::SColor c = MapBlock_LightColor(255, l);
546
547                         for(u32 j=0; j<6; j++)
548                         {
549                                 // Check this neighbor
550                                 v3s16 n2p = blockpos_nodes + p + g_6dirs[j];
551                                 MapNode n2 = data->m_vmanip.getNodeNoEx(n2p);
552                                 // Don't make face if neighbor is of same type
553                                 if(n2.getContent() == n.getContent())
554                                         continue;
555
556                                 // The face at Z+
557                                 video::S3DVertex vertices[4] =
558                                 {
559                                         video::S3DVertex(-BS/2,-BS/2,BS/2, 0,0,0, c,
560                                                 ap.x0(), ap.y1()),
561                                         video::S3DVertex(BS/2,-BS/2,BS/2, 0,0,0, c,
562                                                 ap.x1(), ap.y1()),
563                                         video::S3DVertex(BS/2,BS/2,BS/2, 0,0,0, c,
564                                                 ap.x1(), ap.y0()),
565                                         video::S3DVertex(-BS/2,BS/2,BS/2, 0,0,0, c,
566                                                 ap.x0(), ap.y0()),
567                                 };
568                                 
569                                 // Rotations in the g_6dirs format
570                                 if(j == 0) // Z+
571                                         for(u16 i=0; i<4; i++)
572                                                 vertices[i].Pos.rotateXZBy(0);
573                                 else if(j == 1) // Y+
574                                         for(u16 i=0; i<4; i++)
575                                                 vertices[i].Pos.rotateYZBy(-90);
576                                 else if(j == 2) // X+
577                                         for(u16 i=0; i<4; i++)
578                                                 vertices[i].Pos.rotateXZBy(-90);
579                                 else if(j == 3) // Z-
580                                         for(u16 i=0; i<4; i++)
581                                                 vertices[i].Pos.rotateXZBy(180);
582                                 else if(j == 4) // Y-
583                                         for(u16 i=0; i<4; i++)
584                                                 vertices[i].Pos.rotateYZBy(90);
585                                 else if(j == 5) // X-
586                                         for(u16 i=0; i<4; i++)
587                                                 vertices[i].Pos.rotateXZBy(90);
588
589                                 for(u16 i=0; i<4; i++){
590                                         vertices[i].Pos += intToFloat(p, BS);
591                                 }
592
593                                 u16 indices[] = {0,1,2,2,3,0};
594                                 // Add to mesh collector
595                                 collector.append(tile, vertices, 4, indices, 6);
596                         }
597                 break;}
598                 case NDT_ALLFACES:
599                 {
600                         TileSpec tile_leaves = getNodeTile(n, p,
601                                         v3s16(0,0,0), data);
602                         AtlasPointer pa_leaves = tile_leaves.texture;
603
604                         u16 l = getInteriorLight(n, 1, data);
605                         video::SColor c = MapBlock_LightColor(255, l);
606
607                         v3f pos = intToFloat(p, BS);
608                         aabb3f box(-BS/2,-BS/2,-BS/2,BS/2,BS/2,BS/2);
609                         box.MinEdge += pos;
610                         box.MaxEdge += pos;
611                         makeCuboid(&collector, box, &tile_leaves, 1, c, NULL);
612                 break;}
613                 case NDT_ALLFACES_OPTIONAL:
614                         // This is always pre-converted to something else
615                         assert(0);
616                         break;
617                 case NDT_TORCHLIKE:
618                 {
619                         v3s16 dir = n.getWallMountedDir(nodedef);
620                         
621                         u8 tileindex = 0;
622                         if(dir == v3s16(0,-1,0)){
623                                 tileindex = 0; // floor
624                         } else if(dir == v3s16(0,1,0)){
625                                 tileindex = 1; // ceiling
626                         // For backwards compatibility
627                         } else if(dir == v3s16(0,0,0)){
628                                 tileindex = 0; // floor
629                         } else {
630                                 tileindex = 2; // side
631                         }
632
633                         TileSpec tile = getNodeTileN(n, p, tileindex, data);
634                         tile.material_flags &= ~MATERIAL_FLAG_BACKFACE_CULLING;
635                         tile.material_flags |= MATERIAL_FLAG_CRACK_OVERLAY;
636
637                         AtlasPointer ap = tile.texture;
638
639                         video::SColor c(255,255,255,255);
640
641                         // Wall at X+ of node
642                         video::S3DVertex vertices[4] =
643                         {
644                                 video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c,
645                                                 ap.x0(), ap.y1()),
646                                 video::S3DVertex(BS/2,-BS/2,0, 0,0,0, c,
647                                                 ap.x1(), ap.y1()),
648                                 video::S3DVertex(BS/2,BS/2,0, 0,0,0, c,
649                                                 ap.x1(), ap.y0()),
650                                 video::S3DVertex(-BS/2,BS/2,0, 0,0,0, c,
651                                                 ap.x0(), ap.y0()),
652                         };
653
654                         for(s32 i=0; i<4; i++)
655                         {
656                                 if(dir == v3s16(1,0,0))
657                                         vertices[i].Pos.rotateXZBy(0);
658                                 if(dir == v3s16(-1,0,0))
659                                         vertices[i].Pos.rotateXZBy(180);
660                                 if(dir == v3s16(0,0,1))
661                                         vertices[i].Pos.rotateXZBy(90);
662                                 if(dir == v3s16(0,0,-1))
663                                         vertices[i].Pos.rotateXZBy(-90);
664                                 if(dir == v3s16(0,-1,0))
665                                         vertices[i].Pos.rotateXZBy(45);
666                                 if(dir == v3s16(0,1,0))
667                                         vertices[i].Pos.rotateXZBy(-45);
668
669                                 vertices[i].Pos += intToFloat(p, BS);
670                         }
671
672                         u16 indices[] = {0,1,2,2,3,0};
673                         // Add to mesh collector
674                         collector.append(tile, vertices, 4, indices, 6);
675                 break;}
676                 case NDT_SIGNLIKE:
677                 {
678                         TileSpec tile = getNodeTileN(n, p, 0, data);
679                         tile.material_flags &= ~MATERIAL_FLAG_BACKFACE_CULLING;
680                         tile.material_flags |= MATERIAL_FLAG_CRACK_OVERLAY;
681                         AtlasPointer ap = tile.texture;
682
683                         u16 l = getInteriorLight(n, 0, data);
684                         video::SColor c = MapBlock_LightColor(255, l);
685                                 
686                         float d = (float)BS/16;
687                         // Wall at X+ of node
688                         video::S3DVertex vertices[4] =
689                         {
690                                 video::S3DVertex(BS/2-d,BS/2,BS/2, 0,0,0, c,
691                                                 ap.x0(), ap.y0()),
692                                 video::S3DVertex(BS/2-d,BS/2,-BS/2, 0,0,0, c,
693                                                 ap.x1(), ap.y0()),
694                                 video::S3DVertex(BS/2-d,-BS/2,-BS/2, 0,0,0, c,
695                                                 ap.x1(), ap.y1()),
696                                 video::S3DVertex(BS/2-d,-BS/2,BS/2, 0,0,0, c,
697                                                 ap.x0(), ap.y1()),
698                         };
699
700                         v3s16 dir = n.getWallMountedDir(nodedef);
701
702                         for(s32 i=0; i<4; i++)
703                         {
704                                 if(dir == v3s16(1,0,0))
705                                         vertices[i].Pos.rotateXZBy(0);
706                                 if(dir == v3s16(-1,0,0))
707                                         vertices[i].Pos.rotateXZBy(180);
708                                 if(dir == v3s16(0,0,1))
709                                         vertices[i].Pos.rotateXZBy(90);
710                                 if(dir == v3s16(0,0,-1))
711                                         vertices[i].Pos.rotateXZBy(-90);
712                                 if(dir == v3s16(0,-1,0))
713                                         vertices[i].Pos.rotateXYBy(-90);
714                                 if(dir == v3s16(0,1,0))
715                                         vertices[i].Pos.rotateXYBy(90);
716
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                 break;}
724                 case NDT_PLANTLIKE:
725                 {
726                         TileSpec tile = getNodeTileN(n, p, 0, data);
727                         tile.material_flags |= MATERIAL_FLAG_CRACK_OVERLAY;
728                         AtlasPointer ap = tile.texture;
729                         
730                         u16 l = getInteriorLight(n, 1, data);
731                         video::SColor c = MapBlock_LightColor(255, l);
732
733                         for(u32 j=0; j<4; j++)
734                         {
735                                 video::S3DVertex vertices[4] =
736                                 {
737                                         video::S3DVertex(-BS/2*f.visual_scale,-BS/2,0, 0,0,0, c,
738                                                 ap.x0(), ap.y1()),
739                                         video::S3DVertex( BS/2*f.visual_scale,-BS/2,0, 0,0,0, c,
740                                                 ap.x1(), ap.y1()),
741                                         video::S3DVertex( BS/2*f.visual_scale,
742                                                 -BS/2 + f.visual_scale*BS,0, 0,0,0, c,
743                                                 ap.x1(), ap.y0()),
744                                         video::S3DVertex(-BS/2*f.visual_scale,
745                                                 -BS/2 + f.visual_scale*BS,0, 0,0,0, c,
746                                                 ap.x0(), ap.y0()),
747                                 };
748
749                                 if(j == 0)
750                                 {
751                                         for(u16 i=0; i<4; i++)
752                                                 vertices[i].Pos.rotateXZBy(45);
753                                 }
754                                 else if(j == 1)
755                                 {
756                                         for(u16 i=0; i<4; i++)
757                                                 vertices[i].Pos.rotateXZBy(-45);
758                                 }
759                                 else if(j == 2)
760                                 {
761                                         for(u16 i=0; i<4; i++)
762                                                 vertices[i].Pos.rotateXZBy(135);
763                                 }
764                                 else if(j == 3)
765                                 {
766                                         for(u16 i=0; i<4; i++)
767                                                 vertices[i].Pos.rotateXZBy(-135);
768                                 }
769
770                                 for(u16 i=0; i<4; i++)
771                                 {
772                                         vertices[i].Pos *= f.visual_scale;
773                                         vertices[i].Pos += intToFloat(p, BS);
774                                 }
775
776                                 u16 indices[] = {0,1,2,2,3,0};
777                                 // Add to mesh collector
778                                 collector.append(tile, vertices, 4, indices, 6);
779                         }
780                 break;}
781                 case NDT_FENCELIKE:
782                 {
783                         TileSpec tile = getNodeTile(n, p, v3s16(0,0,0), data);
784                         TileSpec tile_nocrack = tile;
785                         tile_nocrack.material_flags &= ~MATERIAL_FLAG_CRACK;
786                         
787                         // A hack to put wood the right way around in the posts
788                         ITextureSource *tsrc = data->m_gamedef->tsrc();
789                         TileSpec tile_rot = tile;
790                         tile_rot.texture = tsrc->getTexture(tsrc->getTextureName(
791                                         tile.texture.id) + "^[transformR90");
792                                         
793                         u16 l = getInteriorLight(n, 1, data);
794                         video::SColor c = MapBlock_LightColor(255, l);
795
796                         const f32 post_rad=(f32)BS/8;
797                         const f32 bar_rad=(f32)BS/16;
798                         const f32 bar_len=(f32)(BS/2)-post_rad;
799
800                         v3f pos = intToFloat(p, BS);
801
802                         // The post - always present
803                         aabb3f post(-post_rad,-BS/2,-post_rad,post_rad,BS/2,post_rad);
804                         post.MinEdge += pos;
805                         post.MaxEdge += pos;
806                         f32 postuv[24]={
807                                         6/16.,6/16.,10/16.,10/16.,
808                                         6/16.,6/16.,10/16.,10/16.,
809                                         0/16.,0,4/16.,1,
810                                         4/16.,0,8/16.,1,
811                                         8/16.,0,12/16.,1,
812                                         12/16.,0,16/16.,1};
813                         makeCuboid(&collector, post, &tile_rot, 1, c, postuv);
814
815                         // Now a section of fence, +X, if there's a post there
816                         v3s16 p2 = p;
817                         p2.X++;
818                         MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
819                         const ContentFeatures *f2 = &nodedef->get(n2);
820                         if(f2->drawtype == NDT_FENCELIKE)
821                         {
822                                 aabb3f bar(-bar_len+BS/2,-bar_rad+BS/4,-bar_rad,
823                                                 bar_len+BS/2,bar_rad+BS/4,bar_rad);
824                                 bar.MinEdge += pos;
825                                 bar.MaxEdge += pos;
826                                 f32 xrailuv[24]={
827                                         0/16.,2/16.,16/16.,4/16.,
828                                         0/16.,4/16.,16/16.,6/16.,
829                                         6/16.,6/16.,8/16.,8/16.,
830                                         10/16.,10/16.,12/16.,12/16.,
831                                         0/16.,8/16.,16/16.,10/16.,
832                                         0/16.,14/16.,16/16.,16/16.};
833                                 makeCuboid(&collector, bar, &tile_nocrack, 1,
834                                                 c, xrailuv);
835                                 bar.MinEdge.Y -= BS/2;
836                                 bar.MaxEdge.Y -= BS/2;
837                                 makeCuboid(&collector, bar, &tile_nocrack, 1,
838                                                 c, xrailuv);
839                         }
840
841                         // Now a section of fence, +Z, if there's a post there
842                         p2 = p;
843                         p2.Z++;
844                         n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
845                         f2 = &nodedef->get(n2);
846                         if(f2->drawtype == NDT_FENCELIKE)
847                         {
848                                 aabb3f bar(-bar_rad,-bar_rad+BS/4,-bar_len+BS/2,
849                                                 bar_rad,bar_rad+BS/4,bar_len+BS/2);
850                                 bar.MinEdge += pos;
851                                 bar.MaxEdge += pos;
852                                 f32 zrailuv[24]={
853                                         3/16.,1/16.,5/16.,5/16., // cannot rotate; stretch
854                                         4/16.,1/16.,6/16.,5/16., // for wood texture instead
855                                         0/16.,9/16.,16/16.,11/16.,
856                                         0/16.,6/16.,16/16.,8/16.,
857                                         6/16.,6/16.,8/16.,8/16.,
858                                         10/16.,10/16.,12/16.,12/16.};
859                                 makeCuboid(&collector, bar, &tile_nocrack, 1,
860                                                 c, zrailuv);
861                                 bar.MinEdge.Y -= BS/2;
862                                 bar.MaxEdge.Y -= BS/2;
863                                 makeCuboid(&collector, bar, &tile_nocrack, 1,
864                                                 c, zrailuv);
865                         }
866                 break;}
867                 case NDT_RAILLIKE:
868                 {
869                         bool is_rail_x [] = { false, false };  /* x-1, x+1 */
870                         bool is_rail_z [] = { false, false };  /* z-1, z+1 */
871
872                         bool is_rail_z_minus_y [] = { false, false };  /* z-1, z+1; y-1 */
873                         bool is_rail_x_minus_y [] = { false, false };  /* x-1, z+1; y-1 */
874                         bool is_rail_z_plus_y [] = { false, false };  /* z-1, z+1; y+1 */
875                         bool is_rail_x_plus_y [] = { false, false };  /* x-1, x+1; y+1 */
876
877                         MapNode n_minus_x = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x-1,y,z));
878                         MapNode n_plus_x = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x+1,y,z));
879                         MapNode n_minus_z = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y,z-1));
880                         MapNode n_plus_z = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y,z+1));
881                         MapNode n_plus_x_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x+1, y+1, z));
882                         MapNode n_plus_x_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x+1, y-1, z));
883                         MapNode n_minus_x_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x-1, y+1, z));
884                         MapNode n_minus_x_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x-1, y-1, z));
885                         MapNode n_plus_z_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y+1, z+1));
886                         MapNode n_minus_z_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y+1, z-1));
887                         MapNode n_plus_z_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y-1, z+1));
888                         MapNode n_minus_z_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y-1, z-1));
889                         
890                         content_t thiscontent = n.getContent();
891                         if(n_minus_x.getContent() == thiscontent)
892                                 is_rail_x[0] = true;
893                         if (n_minus_x_minus_y.getContent() == thiscontent)
894                                 is_rail_x_minus_y[0] = true;
895                         if(n_minus_x_plus_y.getContent() == thiscontent)
896                                 is_rail_x_plus_y[0] = true;
897
898                         if(n_plus_x.getContent() == thiscontent)
899                                 is_rail_x[1] = true;
900                         if (n_plus_x_minus_y.getContent() == thiscontent)
901                                 is_rail_x_minus_y[1] = true;
902                         if(n_plus_x_plus_y.getContent() == thiscontent)
903                                 is_rail_x_plus_y[1] = true;
904
905                         if(n_minus_z.getContent() == thiscontent)
906                                 is_rail_z[0] = true;
907                         if (n_minus_z_minus_y.getContent() == thiscontent)
908                                 is_rail_z_minus_y[0] = true;
909                         if(n_minus_z_plus_y.getContent() == thiscontent)
910                                 is_rail_z_plus_y[0] = true;
911
912                         if(n_plus_z.getContent() == thiscontent)
913                                 is_rail_z[1] = true;
914                         if (n_plus_z_minus_y.getContent() == thiscontent)
915                                 is_rail_z_minus_y[1] = true;
916                         if(n_plus_z_plus_y.getContent() == thiscontent)
917                                 is_rail_z_plus_y[1] = true;
918
919                         bool is_rail_x_all[] = {false, false};
920                         bool is_rail_z_all[] = {false, false};
921                         is_rail_x_all[0]=is_rail_x[0] || is_rail_x_minus_y[0] || is_rail_x_plus_y[0];
922                         is_rail_x_all[1]=is_rail_x[1] || is_rail_x_minus_y[1] || is_rail_x_plus_y[1];
923                         is_rail_z_all[0]=is_rail_z[0] || is_rail_z_minus_y[0] || is_rail_z_plus_y[0];
924                         is_rail_z_all[1]=is_rail_z[1] || is_rail_z_minus_y[1] || is_rail_z_plus_y[1];
925
926                         // reasonable default, flat straight unrotated rail
927                         bool is_straight = true;
928                         int adjacencies = 0;
929                         int angle = 0;
930                         u8 tileindex = 0;
931
932                         // check for sloped rail
933                         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])
934                         {
935                                 adjacencies = 5; //5 means sloped
936                                 is_straight = true; // sloped is always straight
937                         }
938                         else
939                         {
940                                 // is really straight, rails on both sides
941                                 is_straight = (is_rail_x_all[0] && is_rail_x_all[1]) || (is_rail_z_all[0] && is_rail_z_all[1]);
942                                 adjacencies = is_rail_x_all[0] + is_rail_x_all[1] + is_rail_z_all[0] + is_rail_z_all[1];
943                         }
944
945                         switch (adjacencies) {
946                         case 1:
947                                 if(is_rail_x_all[0] || is_rail_x_all[1])
948                                         angle = 90;
949                                 break;
950                         case 2:
951                                 if(!is_straight)
952                                         tileindex = 1; // curved
953                                 if(is_rail_x_all[0] && is_rail_x_all[1])
954                                         angle = 90;
955                                 if(is_rail_z_all[0] && is_rail_z_all[1]){
956                                         if (n_minus_z_plus_y.getContent() == thiscontent) angle = 180;
957                                 }
958                                 else if(is_rail_x_all[0] && is_rail_z_all[0])
959                                         angle = 270;
960                                 else if(is_rail_x_all[0] && is_rail_z_all[1])
961                                         angle = 180;
962                                 else if(is_rail_x_all[1] && is_rail_z_all[1])
963                                         angle = 90;
964                                 break;
965                         case 3:
966                                 // here is where the potential to 'switch' a junction is, but not implemented at present
967                                 tileindex = 2; // t-junction
968                                 if(!is_rail_x_all[1])
969                                         angle=180;
970                                 if(!is_rail_z_all[0])
971                                         angle=90;
972                                 if(!is_rail_z_all[1])
973                                         angle=270;
974                                 break;
975                         case 4:
976                                 tileindex = 3; // crossing
977                                 break;
978                         case 5: //sloped
979                                 if(is_rail_z_plus_y[0])
980                                         angle = 180;
981                                 if(is_rail_x_plus_y[0])
982                                         angle = 90;
983                                 if(is_rail_x_plus_y[1])
984                                         angle = -90;
985                                 break;
986                         default:
987                                 break;
988                         }
989
990                         TileSpec tile = getNodeTileN(n, p, tileindex, data);
991                         tile.material_flags &= ~MATERIAL_FLAG_BACKFACE_CULLING;
992                         tile.material_flags |= MATERIAL_FLAG_CRACK_OVERLAY;
993
994                         AtlasPointer ap = tile.texture;
995                         
996                         u16 l = getInteriorLight(n, 0, data);
997                         video::SColor c = MapBlock_LightColor(255, l);
998
999                         float d = (float)BS/64;
1000                         
1001                         char g=-1;
1002                         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])
1003                                 g=1; //Object is at a slope
1004
1005                         video::S3DVertex vertices[4] =
1006                         {
1007                                         video::S3DVertex(-BS/2,-BS/2+d,-BS/2, 0,0,0, c,
1008                                                         ap.x0(), ap.y1()),
1009                                         video::S3DVertex(BS/2,-BS/2+d,-BS/2, 0,0,0, c,
1010                                                         ap.x1(), ap.y1()),
1011                                         video::S3DVertex(BS/2,g*BS/2+d,BS/2, 0,0,0, c,
1012                                                         ap.x1(), ap.y0()),
1013                                         video::S3DVertex(-BS/2,g*BS/2+d,BS/2, 0,0,0, c,
1014                                                         ap.x0(), ap.y0()),
1015                         };
1016
1017                         for(s32 i=0; i<4; i++)
1018                         {
1019                                 if(angle != 0)
1020                                         vertices[i].Pos.rotateXZBy(angle);
1021                                 vertices[i].Pos += intToFloat(p, BS);
1022                         }
1023
1024                         u16 indices[] = {0,1,2,2,3,0};
1025                         collector.append(tile, vertices, 4, indices, 6);
1026                 break;}
1027                 }
1028         }
1029 }
1030