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