Send KEY_END when (re)creating a text input
[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_water_level = 1.0;
137         if(new_style_water)
138                 node_water_level = 0.85;
139         
140         v3s16 blockpos_nodes = data->m_blockpos*MAP_BLOCKSIZE;
141
142         // Flowing water material
143         video::SMaterial material_water1;
144         material_water1.setFlag(video::EMF_LIGHTING, false);
145         material_water1.setFlag(video::EMF_BACK_FACE_CULLING, false);
146         material_water1.setFlag(video::EMF_BILINEAR_FILTER, false);
147         material_water1.setFlag(video::EMF_FOG_ENABLE, true);
148         material_water1.MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
149         AtlasPointer pa_water1 = g_texturesource->getTexture(
150                         g_texturesource->getTextureId("water.png"));
151         material_water1.setTexture(0, pa_water1.atlas);
152
153         // New-style leaves material
154         video::SMaterial material_leaves1;
155         material_leaves1.setFlag(video::EMF_LIGHTING, false);
156         //material_leaves1.setFlag(video::EMF_BACK_FACE_CULLING, false);
157         material_leaves1.setFlag(video::EMF_BILINEAR_FILTER, false);
158         material_leaves1.setFlag(video::EMF_FOG_ENABLE, true);
159         material_leaves1.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
160         AtlasPointer pa_leaves1 = g_texturesource->getTexture(
161                         g_texturesource->getTextureId("leaves.png"));
162         material_leaves1.setTexture(0, pa_leaves1.atlas);
163
164         // Glass material
165         video::SMaterial material_glass;
166         material_glass.setFlag(video::EMF_LIGHTING, false);
167         material_glass.setFlag(video::EMF_BILINEAR_FILTER, false);
168         material_glass.setFlag(video::EMF_FOG_ENABLE, true);
169         material_glass.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
170         AtlasPointer pa_glass = g_texturesource->getTexture(
171                         g_texturesource->getTextureId("glass.png"));
172         material_glass.setTexture(0, pa_glass.atlas);
173
174         // Wood material
175         video::SMaterial material_wood;
176         material_wood.setFlag(video::EMF_LIGHTING, false);
177         material_wood.setFlag(video::EMF_BILINEAR_FILTER, false);
178         material_wood.setFlag(video::EMF_FOG_ENABLE, true);
179         material_wood.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
180         AtlasPointer pa_wood = g_texturesource->getTexture(
181                         g_texturesource->getTextureId("wood.png"));
182         material_wood.setTexture(0, pa_wood.atlas);
183
184         // General ground material for special output
185         // Texture is modified just before usage
186         video::SMaterial material_general;
187         material_general.setFlag(video::EMF_LIGHTING, false);
188         material_general.setFlag(video::EMF_BILINEAR_FILTER, false);
189         material_general.setFlag(video::EMF_FOG_ENABLE, true);
190         material_general.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
191
192
193         // Papyrus material
194         video::SMaterial material_papyrus;
195         material_papyrus.setFlag(video::EMF_LIGHTING, false);
196         material_papyrus.setFlag(video::EMF_BILINEAR_FILTER, false);
197         material_papyrus.setFlag(video::EMF_FOG_ENABLE, true);
198         material_papyrus.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
199         AtlasPointer pa_papyrus = g_texturesource->getTexture(
200                         g_texturesource->getTextureId("papyrus.png"));
201         material_papyrus.setTexture(0, pa_papyrus.atlas);
202
203         // junglegrass material
204         video::SMaterial material_junglegrass;
205         material_junglegrass.setFlag(video::EMF_LIGHTING, false);
206         material_junglegrass.setFlag(video::EMF_BILINEAR_FILTER, false);
207         material_junglegrass.setFlag(video::EMF_FOG_ENABLE, true);
208         material_junglegrass.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
209         AtlasPointer pa_junglegrass = g_texturesource->getTexture(
210                         g_texturesource->getTextureId("junglegrass.png"));
211         material_junglegrass.setTexture(0, pa_junglegrass.atlas);
212
213         for(s16 z=0; z<MAP_BLOCKSIZE; z++)
214         for(s16 y=0; y<MAP_BLOCKSIZE; y++)
215         for(s16 x=0; x<MAP_BLOCKSIZE; x++)
216         {
217                 v3s16 p(x,y,z);
218
219                 MapNode n = data->m_vmanip.getNodeNoEx(blockpos_nodes+p);
220                 
221                 /*
222                         Add torches to mesh
223                 */
224                 if(n.getContent() == CONTENT_TORCH)
225                 {
226                         video::SColor c(255,255,255,255);
227
228                         // Wall at X+ of node
229                         video::S3DVertex vertices[4] =
230                         {
231                                 video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c, 0,1),
232                                 video::S3DVertex(BS/2,-BS/2,0, 0,0,0, c, 1,1),
233                                 video::S3DVertex(BS/2,BS/2,0, 0,0,0, c, 1,0),
234                                 video::S3DVertex(-BS/2,BS/2,0, 0,0,0, c, 0,0),
235                         };
236
237                         v3s16 dir = unpackDir(n.param2);
238
239                         for(s32 i=0; i<4; i++)
240                         {
241                                 if(dir == v3s16(1,0,0))
242                                         vertices[i].Pos.rotateXZBy(0);
243                                 if(dir == v3s16(-1,0,0))
244                                         vertices[i].Pos.rotateXZBy(180);
245                                 if(dir == v3s16(0,0,1))
246                                         vertices[i].Pos.rotateXZBy(90);
247                                 if(dir == v3s16(0,0,-1))
248                                         vertices[i].Pos.rotateXZBy(-90);
249                                 if(dir == v3s16(0,-1,0))
250                                         vertices[i].Pos.rotateXZBy(45);
251                                 if(dir == v3s16(0,1,0))
252                                         vertices[i].Pos.rotateXZBy(-45);
253
254                                 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
255                         }
256
257                         // Set material
258                         video::SMaterial material;
259                         material.setFlag(video::EMF_LIGHTING, false);
260                         material.setFlag(video::EMF_BACK_FACE_CULLING, false);
261                         material.setFlag(video::EMF_BILINEAR_FILTER, false);
262                         //material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
263                         material.MaterialType
264                                         = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
265
266                         if(dir == v3s16(0,-1,0))
267                                 material.setTexture(0,
268                                                 g_texturesource->getTextureRaw("torch_on_floor.png"));
269                         else if(dir == v3s16(0,1,0))
270                                 material.setTexture(0,
271                                                 g_texturesource->getTextureRaw("torch_on_ceiling.png"));
272                         // For backwards compatibility
273                         else if(dir == v3s16(0,0,0))
274                                 material.setTexture(0,
275                                                 g_texturesource->getTextureRaw("torch_on_floor.png"));
276                         else
277                                 material.setTexture(0, 
278                                                 g_texturesource->getTextureRaw("torch.png"));
279
280                         u16 indices[] = {0,1,2,2,3,0};
281                         // Add to mesh collector
282                         collector.append(material, vertices, 4, indices, 6);
283                 }
284                 /*
285                         Signs on walls
286                 */
287                 else if(n.getContent() == CONTENT_SIGN_WALL)
288                 {
289                         u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio));
290                         video::SColor c = MapBlock_LightColor(255, l);
291                                 
292                         float d = (float)BS/16;
293                         // Wall at X+ of node
294                         video::S3DVertex vertices[4] =
295                         {
296                                 video::S3DVertex(BS/2-d,-BS/2,-BS/2, 0,0,0, c, 0,1),
297                                 video::S3DVertex(BS/2-d,-BS/2,BS/2, 0,0,0, c, 1,1),
298                                 video::S3DVertex(BS/2-d,BS/2,BS/2, 0,0,0, c, 1,0),
299                                 video::S3DVertex(BS/2-d,BS/2,-BS/2, 0,0,0, c, 0,0),
300                         };
301
302                         v3s16 dir = unpackDir(n.param2);
303
304                         for(s32 i=0; i<4; i++)
305                         {
306                                 if(dir == v3s16(1,0,0))
307                                         vertices[i].Pos.rotateXZBy(0);
308                                 if(dir == v3s16(-1,0,0))
309                                         vertices[i].Pos.rotateXZBy(180);
310                                 if(dir == v3s16(0,0,1))
311                                         vertices[i].Pos.rotateXZBy(90);
312                                 if(dir == v3s16(0,0,-1))
313                                         vertices[i].Pos.rotateXZBy(-90);
314                                 if(dir == v3s16(0,-1,0))
315                                         vertices[i].Pos.rotateXYBy(-90);
316                                 if(dir == v3s16(0,1,0))
317                                         vertices[i].Pos.rotateXYBy(90);
318
319                                 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
320                         }
321
322                         // Set material
323                         video::SMaterial material;
324                         material.setFlag(video::EMF_LIGHTING, false);
325                         material.setFlag(video::EMF_BACK_FACE_CULLING, false);
326                         material.setFlag(video::EMF_BILINEAR_FILTER, false);
327                         material.setFlag(video::EMF_FOG_ENABLE, true);
328                         //material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
329                         material.MaterialType
330                                         = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
331
332                         material.setTexture(0, 
333                                         g_texturesource->getTextureRaw("sign_wall.png"));
334
335                         u16 indices[] = {0,1,2,2,3,0};
336                         // Add to mesh collector
337                         collector.append(material, vertices, 4, indices, 6);
338                 }
339                 /*
340                         Add flowing water to mesh
341                 */
342                 else if(n.getContent() == CONTENT_WATER)
343                 {
344                         bool top_is_water = false;
345                         MapNode ntop = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y+1,z));
346                         if(ntop.getContent() == CONTENT_WATER || ntop.getContent() == CONTENT_WATERSOURCE)
347                                 top_is_water = true;
348                         
349                         u8 l = 0;
350                         // Use the light of the node on top if possible
351                         if(content_features(ntop).param_type == CPT_LIGHT)
352                                 l = decode_light(ntop.getLightBlend(data->m_daynight_ratio));
353                         // Otherwise use the light of this node (the water)
354                         else
355                                 l = decode_light(n.getLightBlend(data->m_daynight_ratio));
356                         video::SColor c = MapBlock_LightColor(WATER_ALPHA, l);
357                         
358                         // Neighbor water levels (key = relative position)
359                         // Includes current node
360                         core::map<v3s16, f32> neighbor_levels;
361                         core::map<v3s16, content_t> neighbor_contents;
362                         core::map<v3s16, u8> neighbor_flags;
363                         const u8 neighborflag_top_is_water = 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                                 u8 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() == CONTENT_WATERSOURCE)
388                                                 level = (-0.5+node_water_level) * BS;
389                                         else if(n2.getContent() == CONTENT_WATER)
390                                                 level = (-0.5 + ((float)n2.param2 + 0.5) / 8.0
391                                                                 * node_water_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() == CONTENT_WATERSOURCE || n2.getContent() == CONTENT_WATER)
399                                                 flags |= neighborflag_top_is_water;
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 water_level = (-0.5 + ((float)n.param2 + 0.5) / 8.0) * BS;
408                         //float water_level = neighbor_levels[v3s16(0,0,0)];
409
410                         // Corner heights (average between four waters)
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 == CONTENT_WATERSOURCE)
430                                         {
431                                                 cornerlevel = (-0.5+node_water_level)*BS;
432                                                 valid_count = 1;
433                                                 break;
434                                         }
435                                         else if(content == CONTENT_WATER)
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 water and neighbor's topside
473                                         is water, don't draw side face
474                                 */
475                                 if(top_is_water &&
476                                                 neighbor_flags[dir] & neighborflag_top_is_water)
477                                         continue;
478
479                                 u8 neighbor_content = neighbor_contents[dir];
480                                 
481                                 // Don't draw face if neighbor is not air or water
482                                 if(neighbor_content != CONTENT_AIR
483                                                 && neighbor_content != CONTENT_WATER)
484                                         continue;
485                                 
486                                 bool neighbor_is_water = (neighbor_content == CONTENT_WATER);
487                                 
488                                 // Don't draw any faces if neighbor is water and top is water
489                                 if(neighbor_is_water == true && top_is_water == 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_water1.x0(), pa_water1.y1()),
500                                         video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
501                                                         pa_water1.x1(), pa_water1.y1()),
502                                         video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
503                                                         pa_water1.x1(), pa_water1.y0()),
504                                         video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
505                                                         pa_water1.x0(), pa_water1.y0()),
506                                 };
507                                 
508                                 /*
509                                         If our topside is water, set upper border of face
510                                         at upper border of node
511                                 */
512                                 if(top_is_water)
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 water, lower border of face is corner
528                                         water levels
529                                 */
530                                 if(neighbor_is_water)
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 water, 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(material_water1, vertices, 4, indices, 6);
562                         }
563                         
564                         /*
565                                 Generate top side, if appropriate
566                         */
567                         
568                         if(top_is_water == 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_water1.x0(), pa_water1.y1()),
578                                         video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
579                                                         pa_water1.x1(), pa_water1.y1()),
580                                         video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c,
581                                                         pa_water1.x1(), pa_water1.y0()),
582                                         video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c,
583                                                         pa_water1.x0(), pa_water1.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 += water_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(material_water1, vertices, 4, indices, 6);
601                         }
602                 }
603                 /*
604                         Add water sources to mesh if using new style
605                 */
606                 else if(n.getContent() == CONTENT_WATERSOURCE && new_style_water)
607                 {
608                         //bool top_is_water = false;
609                         bool top_is_air = false;
610                         MapNode n = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y+1,z));
611                         /*if(n.getContent() == CONTENT_WATER || n.getContent() == CONTENT_WATERSOURCE)
612                                 top_is_water = true;*/
613                         if(n.getContent() == CONTENT_AIR)
614                                 top_is_air = true;
615                         
616                         /*if(top_is_water == true)
617                                 continue;*/
618                         if(top_is_air == false)
619                                 continue;
620
621                         u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio));
622                         video::SColor c = MapBlock_LightColor(WATER_ALPHA, l);
623                         
624                         video::S3DVertex vertices[4] =
625                         {
626                                 /*video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c, 0,1),
627                                 video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c, 1,1),
628                                 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c, 1,0),
629                                 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c, 0,0),*/
630                                 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
631                                                 pa_water1.x0(), pa_water1.y1()),
632                                 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
633                                                 pa_water1.x1(), pa_water1.y1()),
634                                 video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c,
635                                                 pa_water1.x1(), pa_water1.y0()),
636                                 video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c,
637                                                 pa_water1.x0(), pa_water1.y0()),
638                         };
639
640                         for(s32 i=0; i<4; i++)
641                         {
642                                 vertices[i].Pos.Y += (-0.5+node_water_level)*BS;
643                                 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
644                         }
645
646                         u16 indices[] = {0,1,2,2,3,0};
647                         // Add to mesh collector
648                         collector.append(material_water1, vertices, 4, indices, 6);
649                 }
650                 /*
651                         Add leaves if using new style
652                 */
653                 else if(n.getContent() == CONTENT_LEAVES && new_style_leaves)
654                 {
655                         /*u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio));*/
656                         u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio)));
657                         video::SColor c = MapBlock_LightColor(255, l);
658
659                         for(u32 j=0; j<6; j++)
660                         {
661                                 video::S3DVertex vertices[4] =
662                                 {
663                                         /*video::S3DVertex(-BS/2,-BS/2,BS/2, 0,0,0, c, 0,1),
664                                         video::S3DVertex(BS/2,-BS/2,BS/2, 0,0,0, c, 1,1),
665                                         video::S3DVertex(BS/2,BS/2,BS/2, 0,0,0, c, 1,0),
666                                         video::S3DVertex(-BS/2,BS/2,BS/2, 0,0,0, c, 0,0),*/
667                                         video::S3DVertex(-BS/2,-BS/2,BS/2, 0,0,0, c,
668                                                 pa_leaves1.x0(), pa_leaves1.y1()),
669                                         video::S3DVertex(BS/2,-BS/2,BS/2, 0,0,0, c,
670                                                 pa_leaves1.x1(), pa_leaves1.y1()),
671                                         video::S3DVertex(BS/2,BS/2,BS/2, 0,0,0, c,
672                                                 pa_leaves1.x1(), pa_leaves1.y0()),
673                                         video::S3DVertex(-BS/2,BS/2,BS/2, 0,0,0, c,
674                                                 pa_leaves1.x0(), pa_leaves1.y0()),
675                                 };
676
677                                 if(j == 0)
678                                 {
679                                         for(u16 i=0; i<4; i++)
680                                                 vertices[i].Pos.rotateXZBy(0);
681                                 }
682                                 else if(j == 1)
683                                 {
684                                         for(u16 i=0; i<4; i++)
685                                                 vertices[i].Pos.rotateXZBy(180);
686                                 }
687                                 else if(j == 2)
688                                 {
689                                         for(u16 i=0; i<4; i++)
690                                                 vertices[i].Pos.rotateXZBy(-90);
691                                 }
692                                 else if(j == 3)
693                                 {
694                                         for(u16 i=0; i<4; i++)
695                                                 vertices[i].Pos.rotateXZBy(90);
696                                 }
697                                 else if(j == 4)
698                                 {
699                                         for(u16 i=0; i<4; i++)
700                                                 vertices[i].Pos.rotateYZBy(-90);
701                                 }
702                                 else if(j == 5)
703                                 {
704                                         for(u16 i=0; i<4; i++)
705                                                 vertices[i].Pos.rotateYZBy(90);
706                                 }
707
708                                 for(u16 i=0; i<4; i++)
709                                 {
710                                         vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
711                                 }
712
713                                 u16 indices[] = {0,1,2,2,3,0};
714                                 // Add to mesh collector
715                                 collector.append(material_leaves1, vertices, 4, indices, 6);
716                         }
717                 }
718                 /*
719                         Add glass
720                 */
721                 else if(n.getContent() == CONTENT_GLASS)
722                 {
723                         u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio)));
724                         video::SColor c = MapBlock_LightColor(255, l);
725
726                         for(u32 j=0; j<6; j++)
727                         {
728                                 video::S3DVertex vertices[4] =
729                                 {
730                                         video::S3DVertex(-BS/2,-BS/2,BS/2, 0,0,0, c,
731                                                 pa_glass.x0(), pa_glass.y1()),
732                                         video::S3DVertex(BS/2,-BS/2,BS/2, 0,0,0, c,
733                                                 pa_glass.x1(), pa_glass.y1()),
734                                         video::S3DVertex(BS/2,BS/2,BS/2, 0,0,0, c,
735                                                 pa_glass.x1(), pa_glass.y0()),
736                                         video::S3DVertex(-BS/2,BS/2,BS/2, 0,0,0, c,
737                                                 pa_glass.x0(), pa_glass.y0()),
738                                 };
739
740                                 if(j == 0)
741                                 {
742                                         for(u16 i=0; i<4; i++)
743                                                 vertices[i].Pos.rotateXZBy(0);
744                                 }
745                                 else if(j == 1)
746                                 {
747                                         for(u16 i=0; i<4; i++)
748                                                 vertices[i].Pos.rotateXZBy(180);
749                                 }
750                                 else if(j == 2)
751                                 {
752                                         for(u16 i=0; i<4; i++)
753                                                 vertices[i].Pos.rotateXZBy(-90);
754                                 }
755                                 else if(j == 3)
756                                 {
757                                         for(u16 i=0; i<4; i++)
758                                                 vertices[i].Pos.rotateXZBy(90);
759                                 }
760                                 else if(j == 4)
761                                 {
762                                         for(u16 i=0; i<4; i++)
763                                                 vertices[i].Pos.rotateYZBy(-90);
764                                 }
765                                 else if(j == 5)
766                                 {
767                                         for(u16 i=0; i<4; i++)
768                                                 vertices[i].Pos.rotateYZBy(90);
769                                 }
770
771                                 for(u16 i=0; i<4; i++)
772                                 {
773                                         vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
774                                 }
775
776                                 u16 indices[] = {0,1,2,2,3,0};
777                                 // Add to mesh collector
778                                 collector.append(material_glass, vertices, 4, indices, 6);
779                         }
780                 }
781                 /*
782                         Add fence
783                 */
784                 else if(n.getContent() == CONTENT_FENCE)
785                 {
786                         u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio)));
787                         video::SColor c = MapBlock_LightColor(255, l);
788
789                         const f32 post_rad=(f32)BS/10;
790                         const f32 bar_rad=(f32)BS/20;
791                         const f32 bar_len=(f32)(BS/2)-post_rad;
792
793                         // The post - always present
794                         v3f pos = intToFloat(p+blockpos_nodes, BS);
795                         f32 postuv[24]={
796                                         0.4,0.4,0.6,0.6,
797                                         0.35,0,0.65,1,
798                                         0.35,0,0.65,1,
799                                         0.35,0,0.65,1,
800                                         0.35,0,0.65,1,
801                                         0.4,0.4,0.6,0.6};
802                         makeCuboid(material_wood, &collector,
803                                 &pa_wood, c, pos,
804                                 post_rad,BS/2,post_rad, postuv);
805
806                         // Now a section of fence, +X, if there's a post there
807                         v3s16 p2 = p;
808                         p2.X++;
809                         MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
810                         if(n2.getContent() == CONTENT_FENCE)
811                         {
812                                 pos = intToFloat(p+blockpos_nodes, BS);
813                                 pos.X += BS/2;
814                                 pos.Y += BS/4;
815                                 f32 xrailuv[24]={
816                                         0,0.4,1,0.6,
817                                         0,0.4,1,0.6,
818                                         0,0.4,1,0.6,
819                                         0,0.4,1,0.6,
820                                         0,0.4,1,0.6,
821                                         0,0.4,1,0.6};
822                                 makeCuboid(material_wood, &collector,
823                                         &pa_wood, c, pos,
824                                         bar_len,bar_rad,bar_rad, xrailuv);
825
826                                 pos.Y -= BS/2;
827                                 makeCuboid(material_wood, &collector,
828                                         &pa_wood, c, pos,
829                                         bar_len,bar_rad,bar_rad, xrailuv);
830                         }
831
832                         // Now a section of fence, +Z, if there's a post there
833                         p2 = p;
834                         p2.Z++;
835                         n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
836                         if(n2.getContent() == CONTENT_FENCE)
837                         {
838                                 pos = intToFloat(p+blockpos_nodes, BS);
839                                 pos.Z += BS/2;
840                                 pos.Y += BS/4;
841                                 f32 zrailuv[24]={
842                                         0,0.4,1,0.6,
843                                         0,0.4,1,0.6,
844                                         0,0.4,1,0.6,
845                                         0,0.4,1,0.6,
846                                         0,0.4,1,0.6,
847                                         0,0.4,1,0.6};
848                                 makeCuboid(material_wood, &collector,
849                                         &pa_wood, c, pos,
850                                         bar_rad,bar_rad,bar_len, zrailuv);
851                                 pos.Y -= BS/2;
852                                 makeCuboid(material_wood, &collector,
853                                         &pa_wood, c, pos,
854                                         bar_rad,bar_rad,bar_len, zrailuv);
855
856                         }
857
858                 }
859 #if 1
860                 /*
861                         Add stones with minerals if stone is invisible
862                 */
863                 else if(n.getContent() == CONTENT_STONE && invisible_stone && n.getMineral() != MINERAL_NONE)
864                 {
865                         for(u32 j=0; j<6; j++)
866                         {
867                                 // NOTE: Hopefully g_6dirs[j] is the right direction...
868                                 v3s16 dir = g_6dirs[j];
869                                 /*u8 l = 0;
870                                 MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + dir);
871                                 if(content_features(n2).param_type == CPT_LIGHT)
872                                         l = decode_light(n2.getLightBlend(data->m_daynight_ratio));
873                                 else
874                                         l = 255;*/
875                                 u8 l = 255;
876                                 video::SColor c = MapBlock_LightColor(255, l);
877                                 
878                                 // Get the right texture
879                                 TileSpec ts = n.getTile(dir);
880                                 AtlasPointer ap = ts.texture;
881                                 material_general.setTexture(0, ap.atlas);
882
883                                 video::S3DVertex vertices[4] =
884                                 {
885                                         /*video::S3DVertex(-BS/2,-BS/2,BS/2, 0,0,0, c, 0,1),
886                                         video::S3DVertex(BS/2,-BS/2,BS/2, 0,0,0, c, 1,1),
887                                         video::S3DVertex(BS/2,BS/2,BS/2, 0,0,0, c, 1,0),
888                                         video::S3DVertex(-BS/2,BS/2,BS/2, 0,0,0, c, 0,0),*/
889                                         video::S3DVertex(-BS/2,-BS/2,BS/2, 0,0,0, c,
890                                                 ap.x0(), ap.y1()),
891                                         video::S3DVertex(BS/2,-BS/2,BS/2, 0,0,0, c,
892                                                 ap.x1(), ap.y1()),
893                                         video::S3DVertex(BS/2,BS/2,BS/2, 0,0,0, c,
894                                                 ap.x1(), ap.y0()),
895                                         video::S3DVertex(-BS/2,BS/2,BS/2, 0,0,0, c,
896                                                 ap.x0(), ap.y0()),
897                                 };
898
899                                 if(j == 0)
900                                 {
901                                         for(u16 i=0; i<4; i++)
902                                                 vertices[i].Pos.rotateXZBy(0);
903                                 }
904                                 else if(j == 1)
905                                 {
906                                         for(u16 i=0; i<4; i++)
907                                                 vertices[i].Pos.rotateXZBy(180);
908                                 }
909                                 else if(j == 2)
910                                 {
911                                         for(u16 i=0; i<4; i++)
912                                                 vertices[i].Pos.rotateXZBy(-90);
913                                 }
914                                 else if(j == 3)
915                                 {
916                                         for(u16 i=0; i<4; i++)
917                                                 vertices[i].Pos.rotateXZBy(90);
918                                 }
919                                 else if(j == 4)
920
921                                 for(u16 i=0; i<4; i++)
922                                 {
923                                         vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
924                                 }
925
926                                 u16 indices[] = {0,1,2,2,3,0};
927                                 // Add to mesh collector
928                                 collector.append(material_general, vertices, 4, indices, 6);
929                         }
930                 }
931 #endif
932                 else if(n.getContent() == CONTENT_PAPYRUS)
933                 {
934                         u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio)));
935                         video::SColor c = MapBlock_LightColor(255, l);
936
937                         for(u32 j=0; j<4; j++)
938                         {
939                                 video::S3DVertex vertices[4] =
940                                 {
941                                         video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c,
942                                                 pa_papyrus.x0(), pa_papyrus.y1()),
943                                         video::S3DVertex(BS/2,-BS/2,0, 0,0,0, c,
944                                                 pa_papyrus.x1(), pa_papyrus.y1()),
945                                         video::S3DVertex(BS/2,BS/2,0, 0,0,0, c,
946                                                 pa_papyrus.x1(), pa_papyrus.y0()),
947                                         video::S3DVertex(-BS/2,BS/2,0, 0,0,0, c,
948                                                 pa_papyrus.x0(), pa_papyrus.y0()),
949                                 };
950
951                                 if(j == 0)
952                                 {
953                                         for(u16 i=0; i<4; i++)
954                                                 vertices[i].Pos.rotateXZBy(45);
955                                 }
956                                 else if(j == 1)
957                                 {
958                                         for(u16 i=0; i<4; i++)
959                                                 vertices[i].Pos.rotateXZBy(-45);
960                                 }
961                                 else if(j == 2)
962                                 {
963                                         for(u16 i=0; i<4; i++)
964                                                 vertices[i].Pos.rotateXZBy(135);
965                                 }
966                                 else if(j == 3)
967                                 {
968                                         for(u16 i=0; i<4; i++)
969                                                 vertices[i].Pos.rotateXZBy(-135);
970                                 }
971
972                                 for(u16 i=0; i<4; i++)
973                                 {
974                                         vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
975                                 }
976
977                                 u16 indices[] = {0,1,2,2,3,0};
978                                 // Add to mesh collector
979                                 collector.append(material_papyrus, vertices, 4, indices, 6);
980                         }
981                 }
982                 else if(n.getContent() == CONTENT_JUNGLEGRASS)
983                 {
984                         u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio)));
985                         video::SColor c = MapBlock_LightColor(255, l);
986
987                         for(u32 j=0; j<4; j++)
988                         {
989                                 video::S3DVertex vertices[4] =
990                                 {
991                                         video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c,
992                                                 pa_papyrus.x0(), pa_papyrus.y1()),
993                                         video::S3DVertex(BS/2,-BS/2,0, 0,0,0, c,
994                                                 pa_papyrus.x1(), pa_papyrus.y1()),
995                                         video::S3DVertex(BS/2,BS/1,0, 0,0,0, c,
996                                                 pa_papyrus.x1(), pa_papyrus.y0()),
997                                         video::S3DVertex(-BS/2,BS/1,0, 0,0,0, c,
998                                                 pa_papyrus.x0(), pa_papyrus.y0()),
999                                 };
1000
1001                                 if(j == 0)
1002                                 {
1003                                         for(u16 i=0; i<4; i++)
1004                                                 vertices[i].Pos.rotateXZBy(45);
1005                                 }
1006                                 else if(j == 1)
1007                                 {
1008                                         for(u16 i=0; i<4; i++)
1009                                                 vertices[i].Pos.rotateXZBy(-45);
1010                                 }
1011                                 else if(j == 2)
1012                                 {
1013                                         for(u16 i=0; i<4; i++)
1014                                                 vertices[i].Pos.rotateXZBy(135);
1015                                 }
1016                                 else if(j == 3)
1017                                 {
1018                                         for(u16 i=0; i<4; i++)
1019                                                 vertices[i].Pos.rotateXZBy(-135);
1020                                 }
1021
1022                                 for(u16 i=0; i<4; i++)
1023                                 {
1024                                         vertices[i].Pos *= 1.3;
1025                                         vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
1026                                 }
1027
1028                                 u16 indices[] = {0,1,2,2,3,0};
1029                                 // Add to mesh collector
1030                                 collector.append(material_junglegrass, vertices, 4, indices, 6);
1031                         }
1032                 }
1033                 else if(n.getContent() == CONTENT_RAIL)
1034                 {
1035                         u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio));
1036                         video::SColor c = MapBlock_LightColor(255, l);
1037
1038                         bool is_rail_x [] = { false, false };  /* x-1, x+1 */
1039                         bool is_rail_z [] = { false, false };  /* z-1, z+1 */
1040
1041                         MapNode n_minus_x = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x-1,y,z));
1042                         MapNode n_plus_x = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x+1,y,z));
1043                         MapNode n_minus_z = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y,z-1));
1044                         MapNode n_plus_z = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y,z+1));
1045
1046                         if(n_minus_x.getContent() == CONTENT_RAIL)
1047                                 is_rail_x[0] = true;
1048                         if(n_plus_x.getContent() == CONTENT_RAIL)
1049                                 is_rail_x[1] = true;
1050                         if(n_minus_z.getContent() == CONTENT_RAIL)
1051                                 is_rail_z[0] = true;
1052                         if(n_plus_z.getContent() == CONTENT_RAIL)
1053                                 is_rail_z[1] = true;
1054
1055                         float d = (float)BS/16;
1056                         video::S3DVertex vertices[4] =
1057                         {
1058                                 video::S3DVertex(-BS/2,-BS/2+d,-BS/2, 0,0,0, c,
1059                                         0, 1),
1060                                 video::S3DVertex(BS/2,-BS/2+d,-BS/2, 0,0,0, c,
1061                                         1, 1),
1062                                 video::S3DVertex(BS/2,-BS/2+d,BS/2, 0,0,0, c,
1063                                         1, 0),
1064                                 video::S3DVertex(-BS/2,-BS/2+d,BS/2, 0,0,0, c,
1065                                         0, 0),
1066                         };
1067
1068                         video::SMaterial material_rail;
1069                         material_rail.setFlag(video::EMF_LIGHTING, false);
1070                         material_rail.setFlag(video::EMF_BACK_FACE_CULLING, false);
1071                         material_rail.setFlag(video::EMF_BILINEAR_FILTER, false);
1072                         material_rail.setFlag(video::EMF_FOG_ENABLE, true);
1073                         material_rail.MaterialType
1074                                         = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
1075
1076                         int adjacencies = is_rail_x[0] + is_rail_x[1] + is_rail_z[0] + is_rail_z[1];
1077
1078                         // Assign textures
1079                         if(adjacencies < 2)
1080                                 material_rail.setTexture(0, g_texturesource->getTextureRaw("rail.png"));
1081                         else if(adjacencies == 2)
1082                         {
1083                                 if((is_rail_x[0] && is_rail_x[1]) || (is_rail_z[0] && is_rail_z[1]))
1084                                         material_rail.setTexture(0, g_texturesource->getTextureRaw("rail.png"));
1085                                 else
1086                                         material_rail.setTexture(0, g_texturesource->getTextureRaw("rail_curved.png"));
1087                         }
1088                         else if(adjacencies == 3)
1089                                 material_rail.setTexture(0, g_texturesource->getTextureRaw("rail_t_junction.png"));
1090                         else if(adjacencies == 4)
1091                                 material_rail.setTexture(0, g_texturesource->getTextureRaw("rail_crossing.png"));
1092
1093                         // Rotate textures
1094                         int angle = 0;
1095
1096                         if(adjacencies == 1)
1097                         {
1098                                 if(is_rail_x[0] || is_rail_x[1])
1099                                         angle = 90;
1100                         }
1101                         else if(adjacencies == 2)
1102                         {
1103                                 if(is_rail_x[0] && is_rail_x[1])
1104                                         angle = 90;
1105                                 else if(is_rail_x[0] && is_rail_z[0])
1106                                         angle = 270;
1107                                 else if(is_rail_x[0] && is_rail_z[1])
1108                                         angle = 180;
1109                                 else if(is_rail_x[1] && is_rail_z[1])
1110                                         angle = 90;
1111                         }
1112                         else if(adjacencies == 3)
1113                         {
1114                                 if(!is_rail_x[0])
1115                                         angle=0;
1116                                 if(!is_rail_x[1])
1117                                         angle=180;
1118                                 if(!is_rail_z[0])
1119                                         angle=90;
1120                                 if(!is_rail_z[1])
1121                                         angle=270;
1122                         }
1123
1124                         if(angle != 0) {
1125                                 for(u16 i=0; i<4; i++)
1126                                         vertices[i].Pos.rotateXZBy(angle);
1127                         }
1128
1129                         for(s32 i=0; i<4; i++)
1130                         {
1131                                 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
1132                         }
1133
1134                         u16 indices[] = {0,1,2,2,3,0};
1135                         collector.append(material_rail, vertices, 4, indices, 6);
1136                 }
1137                 else if (n.getContent() == CONTENT_LADDER) {
1138                         u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio));
1139                         video::SColor c(255,l,l,l);
1140
1141                         float d = (float)BS/16;
1142
1143                         // Assume wall is at X+
1144                         video::S3DVertex vertices[4] =
1145                         {
1146                                 video::S3DVertex(BS/2-d,-BS/2,-BS/2, 0,0,0, c, 0,1),
1147                                 video::S3DVertex(BS/2-d,-BS/2,BS/2, 0,0,0, c, 1,1),
1148                                 video::S3DVertex(BS/2-d,BS/2,BS/2, 0,0,0, c, 1,0),
1149                                 video::S3DVertex(BS/2-d,BS/2,-BS/2, 0,0,0, c, 0,0),
1150                         };
1151
1152                         v3s16 dir = unpackDir(n.param2);
1153
1154                         for(s32 i=0; i<4; i++)
1155                         {
1156                                 if(dir == v3s16(1,0,0))
1157                                         vertices[i].Pos.rotateXZBy(0);
1158                                 if(dir == v3s16(-1,0,0))
1159                                         vertices[i].Pos.rotateXZBy(180);
1160                                 if(dir == v3s16(0,0,1))
1161                                         vertices[i].Pos.rotateXZBy(90);
1162                                 if(dir == v3s16(0,0,-1))
1163                                         vertices[i].Pos.rotateXZBy(-90);
1164                                 if(dir == v3s16(0,-1,0))
1165                                         vertices[i].Pos.rotateXYBy(-90);
1166                                 if(dir == v3s16(0,1,0))
1167                                         vertices[i].Pos.rotateXYBy(90);
1168
1169                                 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
1170                         }
1171
1172                         video::SMaterial material_ladder;
1173                         material_ladder.setFlag(video::EMF_LIGHTING, false);
1174                         material_ladder.setFlag(video::EMF_BACK_FACE_CULLING, false);
1175                         material_ladder.setFlag(video::EMF_BILINEAR_FILTER, false);
1176                         material_ladder.setFlag(video::EMF_FOG_ENABLE, true);
1177                         material_ladder.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
1178                         material_ladder.setTexture(0, g_texturesource->getTextureRaw("ladder.png"));
1179
1180                         u16 indices[] = {0,1,2,2,3,0};
1181                         // Add to mesh collector
1182                         collector.append(material_ladder, vertices, 4, indices, 6);
1183                 }
1184         }
1185 }
1186 #endif
1187