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