]> git.lizzy.rs Git - dragonfireclient.git/blob - src/content_mapblock.cpp
forgot to add mapblock_nodemod.h
[dragonfireclient.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
24 #ifndef SERVER
25 // Create a cuboid.
26 //  material  - the material to use (for all 6 faces)
27 //  collector - the MeshCollector for the resulting polygons
28 //  pa        - texture atlas pointer for the material
29 //  c         - vertex colour - used for all
30 //  pos       - the position of the centre of the cuboid
31 //  rz,ry,rz  - the radius of the cuboid in each dimension
32 //  txc       - texture coordinates - this is a list of texture coordinates
33 //              for the opposite corners of each face - therefore, there
34 //              should be (2+2)*6=24 values in the list. Alternatively, pass
35 //              NULL to use the entire texture for each face. The order of
36 //              the faces in the list is top-backi-right-front-left-bottom
37 //              If you specified 0,0,1,1 for each face, that would be the
38 //              same as passing NULL.
39 void makeCuboid(video::SMaterial &material, MeshCollector *collector,
40         AtlasPointer* pa, video::SColor &c,
41         v3f &pos, f32 rx, f32 ry, f32 rz, f32* txc)
42 {
43         f32 tu0=pa->x0();
44         f32 tu1=pa->x1();
45         f32 tv0=pa->y0();
46         f32 tv1=pa->y1();
47         f32 txus=tu1-tu0;
48         f32 txvs=tv1-tv0;
49
50         video::S3DVertex v[4] =
51         {
52                 video::S3DVertex(0,0,0, 0,0,0, c, tu0, tv1),
53                 video::S3DVertex(0,0,0, 0,0,0, c, tu1, tv1),
54                 video::S3DVertex(0,0,0, 0,0,0, c, tu1, tv0),
55                 video::S3DVertex(0,0,0, 0,0,0, c, tu0, tv0)
56         };
57
58         for(int i=0;i<6;i++)
59         {
60                 switch(i)
61                 {
62                         case 0: // top
63                                 v[0].Pos.X=-rx; v[0].Pos.Y= ry; v[0].Pos.Z=-rz;
64                                 v[1].Pos.X=-rx; v[1].Pos.Y= ry; v[1].Pos.Z= rz;
65                                 v[2].Pos.X= rx; v[2].Pos.Y= ry; v[2].Pos.Z= rz;
66                                 v[3].Pos.X= rx; v[3].Pos.Y= ry, v[3].Pos.Z=-rz;
67                                 break;
68                         case 1: // back
69                                 v[0].Pos.X=-rx; v[0].Pos.Y= ry; v[0].Pos.Z=-rz;
70                                 v[1].Pos.X= rx; v[1].Pos.Y= ry; v[1].Pos.Z=-rz;
71                                 v[2].Pos.X= rx; v[2].Pos.Y=-ry; v[2].Pos.Z=-rz;
72                                 v[3].Pos.X=-rx; v[3].Pos.Y=-ry, v[3].Pos.Z=-rz;
73                                 break;
74                         case 2: //right
75                                 v[0].Pos.X= rx; v[0].Pos.Y= ry; v[0].Pos.Z=-rz;
76                                 v[1].Pos.X= rx; v[1].Pos.Y= ry; v[1].Pos.Z= rz;
77                                 v[2].Pos.X= rx; v[2].Pos.Y=-ry; v[2].Pos.Z= rz;
78                                 v[3].Pos.X= rx; v[3].Pos.Y=-ry, v[3].Pos.Z=-rz;
79                                 break;
80                         case 3: // front
81                                 v[0].Pos.X= rx; v[0].Pos.Y= ry; v[0].Pos.Z= rz;
82                                 v[1].Pos.X=-rx; v[1].Pos.Y= ry; v[1].Pos.Z= rz;
83                                 v[2].Pos.X=-rx; v[2].Pos.Y=-ry; v[2].Pos.Z= rz;
84                                 v[3].Pos.X= rx; v[3].Pos.Y=-ry, v[3].Pos.Z= rz;
85                                 break;
86                         case 4: // left
87                                 v[0].Pos.X=-rx; v[0].Pos.Y= ry; v[0].Pos.Z= rz;
88                                 v[1].Pos.X=-rx; v[1].Pos.Y= ry; v[1].Pos.Z=-rz;
89                                 v[2].Pos.X=-rx; v[2].Pos.Y=-ry; v[2].Pos.Z=-rz;
90                                 v[3].Pos.X=-rx; v[3].Pos.Y=-ry, v[3].Pos.Z= rz;
91                                 break;
92                         case 5: // bottom
93                                 v[0].Pos.X= rx; v[0].Pos.Y=-ry; v[0].Pos.Z= rz;
94                                 v[1].Pos.X=-rx; v[1].Pos.Y=-ry; v[1].Pos.Z= rz;
95                                 v[2].Pos.X=-rx; v[2].Pos.Y=-ry; v[2].Pos.Z=-rz;
96                                 v[3].Pos.X= rx; v[3].Pos.Y=-ry, v[3].Pos.Z=-rz;
97                                 break;
98                 }
99
100                 if(txc!=NULL)
101                 {
102                         v[0].TCoords.X=tu0+txus*txc[0]; v[0].TCoords.Y=tv0+txvs*txc[3];
103                         v[1].TCoords.X=tu0+txus*txc[2]; v[1].TCoords.Y=tv0+txvs*txc[3];
104                         v[2].TCoords.X=tu0+txus*txc[2]; v[2].TCoords.Y=tv0+txvs*txc[1];
105                         v[3].TCoords.X=tu0+txus*txc[0]; v[3].TCoords.Y=tv0+txvs*txc[1];
106                         txc+=4;
107                 }
108
109                 for(u16 i=0; i<4; i++)
110                         v[i].Pos += pos;
111                 u16 indices[] = {0,1,2,2,3,0};
112                 collector->append(material, v, 4, indices, 6);
113
114         }
115
116 }
117 #endif
118
119 #ifndef SERVER
120 void mapblock_mesh_generate_special(MeshMakeData *data,
121                 MeshCollector &collector)
122 {
123         // 0ms
124         //TimeTaker timer("mapblock_mesh_generate_special()");
125
126         /*
127                 Some settings
128         */
129         bool new_style_water = g_settings.getBool("new_style_water");
130         bool new_style_leaves = g_settings.getBool("new_style_leaves");
131         //bool smooth_lighting = g_settings.getBool("smooth_lighting");
132         
133         float node_water_level = 1.0;
134         if(new_style_water)
135                 node_water_level = 0.85;
136         
137         v3s16 blockpos_nodes = data->m_blockpos*MAP_BLOCKSIZE;
138
139         // Flowing water material
140         video::SMaterial material_water1;
141         material_water1.setFlag(video::EMF_LIGHTING, false);
142         material_water1.setFlag(video::EMF_BACK_FACE_CULLING, false);
143         material_water1.setFlag(video::EMF_BILINEAR_FILTER, false);
144         material_water1.setFlag(video::EMF_FOG_ENABLE, true);
145         material_water1.MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
146         AtlasPointer pa_water1 = g_texturesource->getTexture(
147                         g_texturesource->getTextureId("water.png"));
148         material_water1.setTexture(0, pa_water1.atlas);
149
150         // New-style leaves material
151         video::SMaterial material_leaves1;
152         material_leaves1.setFlag(video::EMF_LIGHTING, false);
153         //material_leaves1.setFlag(video::EMF_BACK_FACE_CULLING, false);
154         material_leaves1.setFlag(video::EMF_BILINEAR_FILTER, false);
155         material_leaves1.setFlag(video::EMF_FOG_ENABLE, true);
156         material_leaves1.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
157         AtlasPointer pa_leaves1 = g_texturesource->getTexture(
158                         g_texturesource->getTextureId("leaves.png"));
159         material_leaves1.setTexture(0, pa_leaves1.atlas);
160
161         // Glass material
162         video::SMaterial material_glass;
163         material_glass.setFlag(video::EMF_LIGHTING, false);
164         material_glass.setFlag(video::EMF_BILINEAR_FILTER, false);
165         material_glass.setFlag(video::EMF_FOG_ENABLE, true);
166         material_glass.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
167         AtlasPointer pa_glass = g_texturesource->getTexture(
168                         g_texturesource->getTextureId("glass.png"));
169         material_glass.setTexture(0, pa_glass.atlas);
170
171         // Wood material
172         video::SMaterial material_wood;
173         material_wood.setFlag(video::EMF_LIGHTING, false);
174         material_wood.setFlag(video::EMF_BILINEAR_FILTER, false);
175         material_wood.setFlag(video::EMF_FOG_ENABLE, true);
176         material_wood.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
177         AtlasPointer pa_wood = g_texturesource->getTexture(
178                         g_texturesource->getTextureId("wood.png"));
179         material_wood.setTexture(0, pa_wood.atlas);
180
181         for(s16 z=0; z<MAP_BLOCKSIZE; z++)
182         for(s16 y=0; y<MAP_BLOCKSIZE; y++)
183         for(s16 x=0; x<MAP_BLOCKSIZE; x++)
184         {
185                 v3s16 p(x,y,z);
186
187                 MapNode n = data->m_vmanip.getNodeNoEx(blockpos_nodes+p);
188                 
189                 /*
190                         Add torches to mesh
191                 */
192                 if(n.d == CONTENT_TORCH)
193                 {
194                         video::SColor c(255,255,255,255);
195
196                         // Wall at X+ of node
197                         video::S3DVertex vertices[4] =
198                         {
199                                 video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c, 0,1),
200                                 video::S3DVertex(BS/2,-BS/2,0, 0,0,0, c, 1,1),
201                                 video::S3DVertex(BS/2,BS/2,0, 0,0,0, c, 1,0),
202                                 video::S3DVertex(-BS/2,BS/2,0, 0,0,0, c, 0,0),
203                         };
204
205                         v3s16 dir = unpackDir(n.dir);
206
207                         for(s32 i=0; i<4; i++)
208                         {
209                                 if(dir == v3s16(1,0,0))
210                                         vertices[i].Pos.rotateXZBy(0);
211                                 if(dir == v3s16(-1,0,0))
212                                         vertices[i].Pos.rotateXZBy(180);
213                                 if(dir == v3s16(0,0,1))
214                                         vertices[i].Pos.rotateXZBy(90);
215                                 if(dir == v3s16(0,0,-1))
216                                         vertices[i].Pos.rotateXZBy(-90);
217                                 if(dir == v3s16(0,-1,0))
218                                         vertices[i].Pos.rotateXZBy(45);
219                                 if(dir == v3s16(0,1,0))
220                                         vertices[i].Pos.rotateXZBy(-45);
221
222                                 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
223                         }
224
225                         // Set material
226                         video::SMaterial material;
227                         material.setFlag(video::EMF_LIGHTING, false);
228                         material.setFlag(video::EMF_BACK_FACE_CULLING, false);
229                         material.setFlag(video::EMF_BILINEAR_FILTER, false);
230                         //material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
231                         material.MaterialType
232                                         = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
233
234                         if(dir == v3s16(0,-1,0))
235                                 material.setTexture(0,
236                                                 g_texturesource->getTextureRaw("torch_on_floor.png"));
237                         else if(dir == v3s16(0,1,0))
238                                 material.setTexture(0,
239                                                 g_texturesource->getTextureRaw("torch_on_ceiling.png"));
240                         // For backwards compatibility
241                         else if(dir == v3s16(0,0,0))
242                                 material.setTexture(0,
243                                                 g_texturesource->getTextureRaw("torch_on_floor.png"));
244                         else
245                                 material.setTexture(0, 
246                                                 g_texturesource->getTextureRaw("torch.png"));
247
248                         u16 indices[] = {0,1,2,2,3,0};
249                         // Add to mesh collector
250                         collector.append(material, vertices, 4, indices, 6);
251                 }
252                 /*
253                         Signs on walls
254                 */
255                 if(n.d == CONTENT_SIGN_WALL)
256                 {
257                         u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio));
258                         video::SColor c(255,l,l,l);
259                                 
260                         float d = (float)BS/16;
261                         // Wall at X+ of node
262                         video::S3DVertex vertices[4] =
263                         {
264                                 video::S3DVertex(BS/2-d,-BS/2,-BS/2, 0,0,0, c, 0,1),
265                                 video::S3DVertex(BS/2-d,-BS/2,BS/2, 0,0,0, c, 1,1),
266                                 video::S3DVertex(BS/2-d,BS/2,BS/2, 0,0,0, c, 1,0),
267                                 video::S3DVertex(BS/2-d,BS/2,-BS/2, 0,0,0, c, 0,0),
268                         };
269
270                         v3s16 dir = unpackDir(n.dir);
271
272                         for(s32 i=0; i<4; i++)
273                         {
274                                 if(dir == v3s16(1,0,0))
275                                         vertices[i].Pos.rotateXZBy(0);
276                                 if(dir == v3s16(-1,0,0))
277                                         vertices[i].Pos.rotateXZBy(180);
278                                 if(dir == v3s16(0,0,1))
279                                         vertices[i].Pos.rotateXZBy(90);
280                                 if(dir == v3s16(0,0,-1))
281                                         vertices[i].Pos.rotateXZBy(-90);
282                                 if(dir == v3s16(0,-1,0))
283                                         vertices[i].Pos.rotateXYBy(-90);
284                                 if(dir == v3s16(0,1,0))
285                                         vertices[i].Pos.rotateXYBy(90);
286
287                                 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
288                         }
289
290                         // Set material
291                         video::SMaterial material;
292                         material.setFlag(video::EMF_LIGHTING, false);
293                         material.setFlag(video::EMF_BACK_FACE_CULLING, false);
294                         material.setFlag(video::EMF_BILINEAR_FILTER, false);
295                         material.setFlag(video::EMF_FOG_ENABLE, true);
296                         //material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
297                         material.MaterialType
298                                         = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
299
300                         material.setTexture(0, 
301                                         g_texturesource->getTextureRaw("sign_wall.png"));
302
303                         u16 indices[] = {0,1,2,2,3,0};
304                         // Add to mesh collector
305                         collector.append(material, vertices, 4, indices, 6);
306                 }
307                 /*
308                         Add flowing water to mesh
309                 */
310                 else if(n.d == CONTENT_WATER)
311                 {
312                         bool top_is_water = false;
313                         MapNode ntop = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y+1,z));
314                         if(ntop.d == CONTENT_WATER || ntop.d == CONTENT_WATERSOURCE)
315                                 top_is_water = true;
316                         
317                         u8 l = 0;
318                         // Use the light of the node on top if possible
319                         if(content_features(ntop.d).param_type == CPT_LIGHT)
320                                 l = decode_light(ntop.getLightBlend(data->m_daynight_ratio));
321                         // Otherwise use the light of this node (the water)
322                         else
323                                 l = decode_light(n.getLightBlend(data->m_daynight_ratio));
324                         video::SColor c(WATER_ALPHA,l,l,l);
325                         
326                         // Neighbor water levels (key = relative position)
327                         // Includes current node
328                         core::map<v3s16, f32> neighbor_levels;
329                         core::map<v3s16, u8> neighbor_contents;
330                         core::map<v3s16, u8> neighbor_flags;
331                         const u8 neighborflag_top_is_water = 0x01;
332                         v3s16 neighbor_dirs[9] = {
333                                 v3s16(0,0,0),
334                                 v3s16(0,0,1),
335                                 v3s16(0,0,-1),
336                                 v3s16(1,0,0),
337                                 v3s16(-1,0,0),
338                                 v3s16(1,0,1),
339                                 v3s16(-1,0,-1),
340                                 v3s16(1,0,-1),
341                                 v3s16(-1,0,1),
342                         };
343                         for(u32 i=0; i<9; i++)
344                         {
345                                 u8 content = CONTENT_AIR;
346                                 float level = -0.5 * BS;
347                                 u8 flags = 0;
348                                 // Check neighbor
349                                 v3s16 p2 = p + neighbor_dirs[i];
350                                 MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
351                                 if(n2.d != CONTENT_IGNORE)
352                                 {
353                                         content = n2.d;
354
355                                         if(n2.d == CONTENT_WATERSOURCE)
356                                                 level = (-0.5+node_water_level) * BS;
357                                         else if(n2.d == CONTENT_WATER)
358                                                 level = (-0.5 + ((float)n2.param2 + 0.5) / 8.0
359                                                                 * node_water_level) * BS;
360
361                                         // Check node above neighbor.
362                                         // NOTE: This doesn't get executed if neighbor
363                                         //       doesn't exist
364                                         p2.Y += 1;
365                                         n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
366                                         if(n2.d == CONTENT_WATERSOURCE || n2.d == CONTENT_WATER)
367                                                 flags |= neighborflag_top_is_water;
368                                 }
369                                 
370                                 neighbor_levels.insert(neighbor_dirs[i], level);
371                                 neighbor_contents.insert(neighbor_dirs[i], content);
372                                 neighbor_flags.insert(neighbor_dirs[i], flags);
373                         }
374
375                         //float water_level = (-0.5 + ((float)n.param2 + 0.5) / 8.0) * BS;
376                         //float water_level = neighbor_levels[v3s16(0,0,0)];
377
378                         // Corner heights (average between four waters)
379                         f32 corner_levels[4];
380                         
381                         v3s16 halfdirs[4] = {
382                                 v3s16(0,0,0),
383                                 v3s16(1,0,0),
384                                 v3s16(1,0,1),
385                                 v3s16(0,0,1),
386                         };
387                         for(u32 i=0; i<4; i++)
388                         {
389                                 v3s16 cornerdir = halfdirs[i];
390                                 float cornerlevel = 0;
391                                 u32 valid_count = 0;
392                                 for(u32 j=0; j<4; j++)
393                                 {
394                                         v3s16 neighbordir = cornerdir - halfdirs[j];
395                                         u8 content = neighbor_contents[neighbordir];
396                                         // Special case for source nodes
397                                         if(content == CONTENT_WATERSOURCE)
398                                         {
399                                                 cornerlevel = (-0.5+node_water_level)*BS;
400                                                 valid_count = 1;
401                                                 break;
402                                         }
403                                         else if(content == CONTENT_WATER)
404                                         {
405                                                 cornerlevel += neighbor_levels[neighbordir];
406                                                 valid_count++;
407                                         }
408                                         else if(content == CONTENT_AIR)
409                                         {
410                                                 cornerlevel += -0.5*BS;
411                                                 valid_count++;
412                                         }
413                                 }
414                                 if(valid_count > 0)
415                                         cornerlevel /= valid_count;
416                                 corner_levels[i] = cornerlevel;
417                         }
418
419                         /*
420                                 Generate sides
421                         */
422
423                         v3s16 side_dirs[4] = {
424                                 v3s16(1,0,0),
425                                 v3s16(-1,0,0),
426                                 v3s16(0,0,1),
427                                 v3s16(0,0,-1),
428                         };
429                         s16 side_corners[4][2] = {
430                                 {1, 2},
431                                 {3, 0},
432                                 {2, 3},
433                                 {0, 1},
434                         };
435                         for(u32 i=0; i<4; i++)
436                         {
437                                 v3s16 dir = side_dirs[i];
438
439                                 /*
440                                         If our topside is water and neighbor's topside
441                                         is water, don't draw side face
442                                 */
443                                 if(top_is_water &&
444                                                 neighbor_flags[dir] & neighborflag_top_is_water)
445                                         continue;
446
447                                 u8 neighbor_content = neighbor_contents[dir];
448                                 
449                                 // Don't draw face if neighbor is not air or water
450                                 if(neighbor_content != CONTENT_AIR
451                                                 && neighbor_content != CONTENT_WATER)
452                                         continue;
453                                 
454                                 bool neighbor_is_water = (neighbor_content == CONTENT_WATER);
455                                 
456                                 // Don't draw any faces if neighbor is water and top is water
457                                 if(neighbor_is_water == true && top_is_water == false)
458                                         continue;
459                                 
460                                 video::S3DVertex vertices[4] =
461                                 {
462                                         /*video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c, 0,1),
463                                         video::S3DVertex(BS/2,0,BS/2, 0,0,0, c, 1,1),
464                                         video::S3DVertex(BS/2,0,BS/2, 0,0,0, c, 1,0),
465                                         video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c, 0,0),*/
466                                         video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
467                                                         pa_water1.x0(), pa_water1.y1()),
468                                         video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
469                                                         pa_water1.x1(), pa_water1.y1()),
470                                         video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
471                                                         pa_water1.x1(), pa_water1.y0()),
472                                         video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
473                                                         pa_water1.x0(), pa_water1.y0()),
474                                 };
475                                 
476                                 /*
477                                         If our topside is water, set upper border of face
478                                         at upper border of node
479                                 */
480                                 if(top_is_water)
481                                 {
482                                         vertices[2].Pos.Y = 0.5*BS;
483                                         vertices[3].Pos.Y = 0.5*BS;
484                                 }
485                                 /*
486                                         Otherwise upper position of face is corner levels
487                                 */
488                                 else
489                                 {
490                                         vertices[2].Pos.Y = corner_levels[side_corners[i][0]];
491                                         vertices[3].Pos.Y = corner_levels[side_corners[i][1]];
492                                 }
493                                 
494                                 /*
495                                         If neighbor is water, lower border of face is corner
496                                         water levels
497                                 */
498                                 if(neighbor_is_water)
499                                 {
500                                         vertices[0].Pos.Y = corner_levels[side_corners[i][1]];
501                                         vertices[1].Pos.Y = corner_levels[side_corners[i][0]];
502                                 }
503                                 /*
504                                         If neighbor is not water, lower border of face is
505                                         lower border of node
506                                 */
507                                 else
508                                 {
509                                         vertices[0].Pos.Y = -0.5*BS;
510                                         vertices[1].Pos.Y = -0.5*BS;
511                                 }
512                                 
513                                 for(s32 j=0; j<4; j++)
514                                 {
515                                         if(dir == v3s16(0,0,1))
516                                                 vertices[j].Pos.rotateXZBy(0);
517                                         if(dir == v3s16(0,0,-1))
518                                                 vertices[j].Pos.rotateXZBy(180);
519                                         if(dir == v3s16(-1,0,0))
520                                                 vertices[j].Pos.rotateXZBy(90);
521                                         if(dir == v3s16(1,0,-0))
522                                                 vertices[j].Pos.rotateXZBy(-90);
523
524                                         vertices[j].Pos += intToFloat(p + blockpos_nodes, BS);
525                                 }
526
527                                 u16 indices[] = {0,1,2,2,3,0};
528                                 // Add to mesh collector
529                                 collector.append(material_water1, vertices, 4, indices, 6);
530                         }
531                         
532                         /*
533                                 Generate top side, if appropriate
534                         */
535                         
536                         if(top_is_water == false)
537                         {
538                                 video::S3DVertex vertices[4] =
539                                 {
540                                         /*video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c, 0,1),
541                                         video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c, 1,1),
542                                         video::S3DVertex(BS/2,0,BS/2, 0,0,0, c, 1,0),
543                                         video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c, 0,0),*/
544                                         video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
545                                                         pa_water1.x0(), pa_water1.y1()),
546                                         video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
547                                                         pa_water1.x1(), pa_water1.y1()),
548                                         video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c,
549                                                         pa_water1.x1(), pa_water1.y0()),
550                                         video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c,
551                                                         pa_water1.x0(), pa_water1.y0()),
552                                 };
553                                 
554                                 // This fixes a strange bug
555                                 s32 corner_resolve[4] = {3,2,1,0};
556
557                                 for(s32 i=0; i<4; i++)
558                                 {
559                                         //vertices[i].Pos.Y += water_level;
560                                         //vertices[i].Pos.Y += neighbor_levels[v3s16(0,0,0)];
561                                         s32 j = corner_resolve[i];
562                                         vertices[i].Pos.Y += corner_levels[j];
563                                         vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
564                                 }
565
566                                 u16 indices[] = {0,1,2,2,3,0};
567                                 // Add to mesh collector
568                                 collector.append(material_water1, vertices, 4, indices, 6);
569                         }
570                 }
571                 /*
572                         Add water sources to mesh if using new style
573                 */
574                 else if(n.d == CONTENT_WATERSOURCE && new_style_water)
575                 {
576                         //bool top_is_water = false;
577                         bool top_is_air = false;
578                         MapNode n = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y+1,z));
579                         /*if(n.d == CONTENT_WATER || n.d == CONTENT_WATERSOURCE)
580                                 top_is_water = true;*/
581                         if(n.d == CONTENT_AIR)
582                                 top_is_air = true;
583                         
584                         /*if(top_is_water == true)
585                                 continue;*/
586                         if(top_is_air == false)
587                                 continue;
588
589                         u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio));
590                         video::SColor c(WATER_ALPHA,l,l,l);
591                         
592                         video::S3DVertex vertices[4] =
593                         {
594                                 /*video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c, 0,1),
595                                 video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c, 1,1),
596                                 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c, 1,0),
597                                 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c, 0,0),*/
598                                 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
599                                                 pa_water1.x0(), pa_water1.y1()),
600                                 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
601                                                 pa_water1.x1(), pa_water1.y1()),
602                                 video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c,
603                                                 pa_water1.x1(), pa_water1.y0()),
604                                 video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c,
605                                                 pa_water1.x0(), pa_water1.y0()),
606                         };
607
608                         for(s32 i=0; i<4; i++)
609                         {
610                                 vertices[i].Pos.Y += (-0.5+node_water_level)*BS;
611                                 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
612                         }
613
614                         u16 indices[] = {0,1,2,2,3,0};
615                         // Add to mesh collector
616                         collector.append(material_water1, vertices, 4, indices, 6);
617                 }
618                 /*
619                         Add leaves if using new style
620                 */
621                 else if(n.d == CONTENT_LEAVES && new_style_leaves)
622                 {
623                         /*u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio));*/
624                         u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio)));
625                         video::SColor c(255,l,l,l);
626
627                         for(u32 j=0; j<6; j++)
628                         {
629                                 video::S3DVertex vertices[4] =
630                                 {
631                                         /*video::S3DVertex(-BS/2,-BS/2,BS/2, 0,0,0, c, 0,1),
632                                         video::S3DVertex(BS/2,-BS/2,BS/2, 0,0,0, c, 1,1),
633                                         video::S3DVertex(BS/2,BS/2,BS/2, 0,0,0, c, 1,0),
634                                         video::S3DVertex(-BS/2,BS/2,BS/2, 0,0,0, c, 0,0),*/
635                                         video::S3DVertex(-BS/2,-BS/2,BS/2, 0,0,0, c,
636                                                 pa_leaves1.x0(), pa_leaves1.y1()),
637                                         video::S3DVertex(BS/2,-BS/2,BS/2, 0,0,0, c,
638                                                 pa_leaves1.x1(), pa_leaves1.y1()),
639                                         video::S3DVertex(BS/2,BS/2,BS/2, 0,0,0, c,
640                                                 pa_leaves1.x1(), pa_leaves1.y0()),
641                                         video::S3DVertex(-BS/2,BS/2,BS/2, 0,0,0, c,
642                                                 pa_leaves1.x0(), pa_leaves1.y0()),
643                                 };
644
645                                 if(j == 0)
646                                 {
647                                         for(u16 i=0; i<4; i++)
648                                                 vertices[i].Pos.rotateXZBy(0);
649                                 }
650                                 else if(j == 1)
651                                 {
652                                         for(u16 i=0; i<4; i++)
653                                                 vertices[i].Pos.rotateXZBy(180);
654                                 }
655                                 else if(j == 2)
656                                 {
657                                         for(u16 i=0; i<4; i++)
658                                                 vertices[i].Pos.rotateXZBy(-90);
659                                 }
660                                 else if(j == 3)
661                                 {
662                                         for(u16 i=0; i<4; i++)
663                                                 vertices[i].Pos.rotateXZBy(90);
664                                 }
665                                 else if(j == 4)
666                                 {
667                                         for(u16 i=0; i<4; i++)
668                                                 vertices[i].Pos.rotateYZBy(-90);
669                                 }
670                                 else if(j == 5)
671                                 {
672                                         for(u16 i=0; i<4; i++)
673                                                 vertices[i].Pos.rotateYZBy(90);
674                                 }
675
676                                 for(u16 i=0; i<4; i++)
677                                 {
678                                         vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
679                                 }
680
681                                 u16 indices[] = {0,1,2,2,3,0};
682                                 // Add to mesh collector
683                                 collector.append(material_leaves1, vertices, 4, indices, 6);
684                         }
685                 }
686                 /*
687                         Add glass
688                 */
689                 else if(n.d == CONTENT_GLASS)
690                 {
691                         u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio)));
692                         video::SColor c(255,l,l,l);
693
694                         for(u32 j=0; j<6; j++)
695                         {
696                                 video::S3DVertex vertices[4] =
697                                 {
698                                         video::S3DVertex(-BS/2,-BS/2,BS/2, 0,0,0, c,
699                                                 pa_glass.x0(), pa_glass.y1()),
700                                         video::S3DVertex(BS/2,-BS/2,BS/2, 0,0,0, c,
701                                                 pa_glass.x1(), pa_glass.y1()),
702                                         video::S3DVertex(BS/2,BS/2,BS/2, 0,0,0, c,
703                                                 pa_glass.x1(), pa_glass.y0()),
704                                         video::S3DVertex(-BS/2,BS/2,BS/2, 0,0,0, c,
705                                                 pa_glass.x0(), pa_glass.y0()),
706                                 };
707
708                                 if(j == 0)
709                                 {
710                                         for(u16 i=0; i<4; i++)
711                                                 vertices[i].Pos.rotateXZBy(0);
712                                 }
713                                 else if(j == 1)
714                                 {
715                                         for(u16 i=0; i<4; i++)
716                                                 vertices[i].Pos.rotateXZBy(180);
717                                 }
718                                 else if(j == 2)
719                                 {
720                                         for(u16 i=0; i<4; i++)
721                                                 vertices[i].Pos.rotateXZBy(-90);
722                                 }
723                                 else if(j == 3)
724                                 {
725                                         for(u16 i=0; i<4; i++)
726                                                 vertices[i].Pos.rotateXZBy(90);
727                                 }
728                                 else if(j == 4)
729                                 {
730                                         for(u16 i=0; i<4; i++)
731                                                 vertices[i].Pos.rotateYZBy(-90);
732                                 }
733                                 else if(j == 5)
734                                 {
735                                         for(u16 i=0; i<4; i++)
736                                                 vertices[i].Pos.rotateYZBy(90);
737                                 }
738
739                                 for(u16 i=0; i<4; i++)
740                                 {
741                                         vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
742                                 }
743
744                                 u16 indices[] = {0,1,2,2,3,0};
745                                 // Add to mesh collector
746                                 collector.append(material_glass, vertices, 4, indices, 6);
747                         }
748                 }
749                 /*
750                         Add fence
751                 */
752                 else if(n.d == CONTENT_FENCE)
753                 {
754                         u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio)));
755                         video::SColor c(255,l,l,l);
756
757                         const f32 post_rad=(f32)BS/10;
758                         const f32 bar_rad=(f32)BS/20;
759                         const f32 bar_len=(f32)(BS/2)-post_rad;
760
761                         // The post - always present
762                         v3f pos = intToFloat(p+blockpos_nodes, BS);
763                         f32 postuv[24]={
764                                         0.4,0.4,0.6,0.6,
765                                         0.35,0,0.65,1,
766                                         0.35,0,0.65,1,
767                                         0.35,0,0.65,1,
768                                         0.35,0,0.65,1,
769                                         0.4,0.4,0.6,0.6};
770                         makeCuboid(material_wood, &collector,
771                                 &pa_wood, c, pos,
772                                 post_rad,BS/2,post_rad, postuv);
773
774                         // Now a section of fence, +X, if there's a post there
775                         v3s16 p2 = p;
776                         p2.X++;
777                         MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
778                         if(n2.d == CONTENT_FENCE)
779                         {
780                                 pos = intToFloat(p+blockpos_nodes, BS);
781                                 pos.X += BS/2;
782                                 pos.Y += BS/4;
783                                 f32 xrailuv[24]={
784                                         0,0.4,1,0.6,
785                                         0,0.4,1,0.6,
786                                         0,0.4,1,0.6,
787                                         0,0.4,1,0.6,
788                                         0,0.4,1,0.6,
789                                         0,0.4,1,0.6};
790                                 makeCuboid(material_wood, &collector,
791                                         &pa_wood, c, pos,
792                                         bar_len,bar_rad,bar_rad, xrailuv);
793
794                                 pos.Y -= BS/2;
795                                 makeCuboid(material_wood, &collector,
796                                         &pa_wood, c, pos,
797                                         bar_len,bar_rad,bar_rad, xrailuv);
798                         }
799
800                         // Now a section of fence, +Z, if there's a post there
801                         p2 = p;
802                         p2.Z++;
803                         n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
804                         if(n2.d == CONTENT_FENCE)
805                         {
806                                 pos = intToFloat(p+blockpos_nodes, BS);
807                                 pos.Z += BS/2;
808                                 pos.Y += BS/4;
809                                 f32 zrailuv[24]={
810                                         0,0.4,1,0.6,
811                                         0,0.4,1,0.6,
812                                         0,0.4,1,0.6,
813                                         0,0.4,1,0.6,
814                                         0,0.4,1,0.6,
815                                         0,0.4,1,0.6};
816                                 makeCuboid(material_wood, &collector,
817                                         &pa_wood, c, pos,
818                                         bar_rad,bar_rad,bar_len, zrailuv);
819                                 pos.Y -= BS/2;
820                                 makeCuboid(material_wood, &collector,
821                                         &pa_wood, c, pos,
822                                         bar_rad,bar_rad,bar_len, zrailuv);
823
824                         }
825
826                 }
827
828         }
829 }
830 #endif
831