3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
20 #include "content_mapblock.h"
21 #include "content_mapnode.h"
22 #include "main.h" // For g_settings and g_texturesource
24 #include "mapblock_mesh.h" // For MapBlock_LightColor()
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)
52 video::S3DVertex v[4] =
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)
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;
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;
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;
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;
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;
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;
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];
111 for(u16 i=0; i<4; i++)
113 u16 indices[] = {0,1,2,2,3,0};
114 collector->append(material, v, 4, indices, 6);
122 void mapblock_mesh_generate_special(MeshMakeData *data,
123 MeshCollector &collector)
126 //TimeTaker timer("mapblock_mesh_generate_special()");
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");
136 float node_liquid_level = 1.0;
138 node_liquid_level = 0.85;
140 v3s16 blockpos_nodes = data->m_blockpos*MAP_BLOCKSIZE;
142 // New-style leaves material
143 video::SMaterial material_leaves1;
144 material_leaves1.setFlag(video::EMF_LIGHTING, false);
145 //material_leaves1.setFlag(video::EMF_BACK_FACE_CULLING, false);
146 material_leaves1.setFlag(video::EMF_BILINEAR_FILTER, false);
147 material_leaves1.setFlag(video::EMF_FOG_ENABLE, true);
148 material_leaves1.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
149 AtlasPointer pa_leaves1 = g_texturesource->getTexture(
150 g_texturesource->getTextureId("leaves.png"));
151 material_leaves1.setTexture(0, pa_leaves1.atlas);
154 video::SMaterial material_glass;
155 material_glass.setFlag(video::EMF_LIGHTING, false);
156 material_glass.setFlag(video::EMF_BILINEAR_FILTER, false);
157 material_glass.setFlag(video::EMF_FOG_ENABLE, true);
158 material_glass.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
159 AtlasPointer pa_glass = g_texturesource->getTexture(
160 g_texturesource->getTextureId("glass.png"));
161 material_glass.setTexture(0, pa_glass.atlas);
164 video::SMaterial material_wood;
165 material_wood.setFlag(video::EMF_LIGHTING, false);
166 material_wood.setFlag(video::EMF_BILINEAR_FILTER, false);
167 material_wood.setFlag(video::EMF_FOG_ENABLE, true);
168 material_wood.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
169 AtlasPointer pa_wood = g_texturesource->getTexture(
170 g_texturesource->getTextureId("wood.png"));
171 material_wood.setTexture(0, pa_wood.atlas);
173 // General ground material for special output
174 // Texture is modified just before usage
175 video::SMaterial material_general;
176 material_general.setFlag(video::EMF_LIGHTING, false);
177 material_general.setFlag(video::EMF_BILINEAR_FILTER, false);
178 material_general.setFlag(video::EMF_FOG_ENABLE, true);
179 material_general.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
183 video::SMaterial material_papyrus;
184 material_papyrus.setFlag(video::EMF_LIGHTING, false);
185 material_papyrus.setFlag(video::EMF_BILINEAR_FILTER, false);
186 material_papyrus.setFlag(video::EMF_FOG_ENABLE, true);
187 material_papyrus.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
188 AtlasPointer pa_papyrus = g_texturesource->getTexture(
189 g_texturesource->getTextureId("papyrus.png"));
190 material_papyrus.setTexture(0, pa_papyrus.atlas);
193 video::SMaterial material_apple;
194 material_apple.setFlag(video::EMF_LIGHTING, false);
195 material_apple.setFlag(video::EMF_BILINEAR_FILTER, false);
196 material_apple.setFlag(video::EMF_FOG_ENABLE, true);
197 material_apple.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
198 AtlasPointer pa_apple = g_texturesource->getTexture(
199 g_texturesource->getTextureId("apple.png"));
200 material_apple.setTexture(0, pa_apple.atlas);
204 video::SMaterial material_sapling;
205 material_sapling.setFlag(video::EMF_LIGHTING, false);
206 material_sapling.setFlag(video::EMF_BILINEAR_FILTER, false);
207 material_sapling.setFlag(video::EMF_FOG_ENABLE, true);
208 material_sapling.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
209 AtlasPointer pa_sapling = g_texturesource->getTexture(
210 g_texturesource->getTextureId("sapling.png"));
211 material_sapling.setTexture(0, pa_sapling.atlas);
214 // junglegrass material
215 video::SMaterial material_junglegrass;
216 material_junglegrass.setFlag(video::EMF_LIGHTING, false);
217 material_junglegrass.setFlag(video::EMF_BILINEAR_FILTER, false);
218 material_junglegrass.setFlag(video::EMF_FOG_ENABLE, true);
219 material_junglegrass.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
220 AtlasPointer pa_junglegrass = g_texturesource->getTexture(
221 g_texturesource->getTextureId("junglegrass.png"));
222 material_junglegrass.setTexture(0, pa_junglegrass.atlas);
224 for(s16 z=0; z<MAP_BLOCKSIZE; z++)
225 for(s16 y=0; y<MAP_BLOCKSIZE; y++)
226 for(s16 x=0; x<MAP_BLOCKSIZE; x++)
230 MapNode n = data->m_vmanip.getNodeNoEx(blockpos_nodes+p);
235 if(n.getContent() == CONTENT_TORCH)
237 video::SColor c(255,255,255,255);
239 // Wall at X+ of node
240 video::S3DVertex vertices[4] =
242 video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c, 0,1),
243 video::S3DVertex(BS/2,-BS/2,0, 0,0,0, c, 1,1),
244 video::S3DVertex(BS/2,BS/2,0, 0,0,0, c, 1,0),
245 video::S3DVertex(-BS/2,BS/2,0, 0,0,0, c, 0,0),
248 v3s16 dir = unpackDir(n.param2);
250 for(s32 i=0; i<4; i++)
252 if(dir == v3s16(1,0,0))
253 vertices[i].Pos.rotateXZBy(0);
254 if(dir == v3s16(-1,0,0))
255 vertices[i].Pos.rotateXZBy(180);
256 if(dir == v3s16(0,0,1))
257 vertices[i].Pos.rotateXZBy(90);
258 if(dir == v3s16(0,0,-1))
259 vertices[i].Pos.rotateXZBy(-90);
260 if(dir == v3s16(0,-1,0))
261 vertices[i].Pos.rotateXZBy(45);
262 if(dir == v3s16(0,1,0))
263 vertices[i].Pos.rotateXZBy(-45);
265 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
269 video::SMaterial material;
270 material.setFlag(video::EMF_LIGHTING, false);
271 material.setFlag(video::EMF_BACK_FACE_CULLING, false);
272 material.setFlag(video::EMF_BILINEAR_FILTER, false);
273 //material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
274 material.MaterialType
275 = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
277 if(dir == v3s16(0,-1,0))
278 material.setTexture(0,
279 g_texturesource->getTextureRaw("torch_on_floor.png"));
280 else if(dir == v3s16(0,1,0))
281 material.setTexture(0,
282 g_texturesource->getTextureRaw("torch_on_ceiling.png"));
283 // For backwards compatibility
284 else if(dir == v3s16(0,0,0))
285 material.setTexture(0,
286 g_texturesource->getTextureRaw("torch_on_floor.png"));
288 material.setTexture(0,
289 g_texturesource->getTextureRaw("torch.png"));
291 u16 indices[] = {0,1,2,2,3,0};
292 // Add to mesh collector
293 collector.append(material, vertices, 4, indices, 6);
298 else if(n.getContent() == CONTENT_SIGN_WALL)
300 u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio));
301 video::SColor c = MapBlock_LightColor(255, l);
303 float d = (float)BS/16;
304 // Wall at X+ of node
305 video::S3DVertex vertices[4] =
307 video::S3DVertex(BS/2-d,-BS/2,-BS/2, 0,0,0, c, 0,1),
308 video::S3DVertex(BS/2-d,-BS/2,BS/2, 0,0,0, c, 1,1),
309 video::S3DVertex(BS/2-d,BS/2,BS/2, 0,0,0, c, 1,0),
310 video::S3DVertex(BS/2-d,BS/2,-BS/2, 0,0,0, c, 0,0),
313 v3s16 dir = unpackDir(n.param2);
315 for(s32 i=0; i<4; i++)
317 if(dir == v3s16(1,0,0))
318 vertices[i].Pos.rotateXZBy(0);
319 if(dir == v3s16(-1,0,0))
320 vertices[i].Pos.rotateXZBy(180);
321 if(dir == v3s16(0,0,1))
322 vertices[i].Pos.rotateXZBy(90);
323 if(dir == v3s16(0,0,-1))
324 vertices[i].Pos.rotateXZBy(-90);
325 if(dir == v3s16(0,-1,0))
326 vertices[i].Pos.rotateXYBy(-90);
327 if(dir == v3s16(0,1,0))
328 vertices[i].Pos.rotateXYBy(90);
330 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
334 video::SMaterial material;
335 material.setFlag(video::EMF_LIGHTING, false);
336 material.setFlag(video::EMF_BACK_FACE_CULLING, false);
337 material.setFlag(video::EMF_BILINEAR_FILTER, false);
338 material.setFlag(video::EMF_FOG_ENABLE, true);
339 //material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
340 material.MaterialType
341 = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
343 material.setTexture(0,
344 g_texturesource->getTextureRaw("sign_wall.png"));
346 u16 indices[] = {0,1,2,2,3,0};
347 // Add to mesh collector
348 collector.append(material, vertices, 4, indices, 6);
351 Add flowing liquid to mesh
353 else if(content_features(n).liquid_type == LIQUID_FLOWING)
355 assert(content_features(n).special_material);
356 video::SMaterial &liquid_material =
357 *content_features(n).special_material;
358 assert(content_features(n).special_atlas);
359 AtlasPointer &pa_liquid1 =
360 *content_features(n).special_atlas;
362 bool top_is_same_liquid = false;
363 MapNode ntop = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y+1,z));
364 content_t c_flowing = content_features(n).liquid_alternative_flowing;
365 content_t c_source = content_features(n).liquid_alternative_source;
366 if(ntop.getContent() == c_flowing || ntop.getContent() == c_source)
367 top_is_same_liquid = true;
370 // Use the light of the node on top if possible
371 if(content_features(ntop).param_type == CPT_LIGHT)
372 l = decode_light(ntop.getLightBlend(data->m_daynight_ratio));
373 // Otherwise use the light of this node (the liquid)
375 l = decode_light(n.getLightBlend(data->m_daynight_ratio));
376 video::SColor c = MapBlock_LightColor(
377 content_features(n).vertex_alpha, l);
379 // Neighbor liquid levels (key = relative position)
380 // Includes current node
381 core::map<v3s16, f32> neighbor_levels;
382 core::map<v3s16, content_t> neighbor_contents;
383 core::map<v3s16, u8> neighbor_flags;
384 const u8 neighborflag_top_is_same_liquid = 0x01;
385 v3s16 neighbor_dirs[9] = {
396 for(u32 i=0; i<9; i++)
398 content_t content = CONTENT_AIR;
399 float level = -0.5 * BS;
402 v3s16 p2 = p + neighbor_dirs[i];
403 MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
404 if(n2.getContent() != CONTENT_IGNORE)
406 content = n2.getContent();
408 if(n2.getContent() == c_source)
409 level = (-0.5+node_liquid_level) * BS;
410 else if(n2.getContent() == c_flowing)
411 level = (-0.5 + ((float)(n2.param2&LIQUID_LEVEL_MASK)
412 + 0.5) / 8.0 * node_liquid_level) * BS;
414 // Check node above neighbor.
415 // NOTE: This doesn't get executed if neighbor
418 n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
419 if(n2.getContent() == c_source ||
420 n2.getContent() == c_flowing)
421 flags |= neighborflag_top_is_same_liquid;
424 neighbor_levels.insert(neighbor_dirs[i], level);
425 neighbor_contents.insert(neighbor_dirs[i], content);
426 neighbor_flags.insert(neighbor_dirs[i], flags);
429 // Corner heights (average between four liquids)
430 f32 corner_levels[4];
432 v3s16 halfdirs[4] = {
438 for(u32 i=0; i<4; i++)
440 v3s16 cornerdir = halfdirs[i];
441 float cornerlevel = 0;
444 for(u32 j=0; j<4; j++)
446 v3s16 neighbordir = cornerdir - halfdirs[j];
447 content_t content = neighbor_contents[neighbordir];
448 // If top is liquid, draw starting from top of node
449 if(neighbor_flags[neighbordir] &
450 neighborflag_top_is_same_liquid)
452 cornerlevel = 0.5*BS;
456 // Source is always the same height
457 else if(content == c_source)
459 cornerlevel = (-0.5+node_liquid_level)*BS;
463 // Flowing liquid has level information
464 else if(content == c_flowing)
466 cornerlevel += neighbor_levels[neighbordir];
469 else if(content == CONTENT_AIR)
475 cornerlevel = -0.5*BS;
476 else if(valid_count > 0)
477 cornerlevel /= valid_count;
478 corner_levels[i] = cornerlevel;
485 v3s16 side_dirs[4] = {
491 s16 side_corners[4][2] = {
497 for(u32 i=0; i<4; i++)
499 v3s16 dir = side_dirs[i];
502 If our topside is liquid and neighbor's topside
503 is liquid, don't draw side face
505 if(top_is_same_liquid &&
506 neighbor_flags[dir] & neighborflag_top_is_same_liquid)
509 content_t neighbor_content = neighbor_contents[dir];
511 // Don't draw face if neighbor is not air or liquid
512 if(neighbor_content != CONTENT_AIR
513 && content_liquid(neighbor_content) == false)
516 bool neighbor_is_same_liquid = (neighbor_content == c_source
517 || neighbor_content == c_flowing);
519 // Don't draw any faces if neighbor same is liquid and top is
521 if(neighbor_is_same_liquid == true
522 && top_is_same_liquid == false)
525 video::S3DVertex vertices[4] =
527 /*video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c, 0,1),
528 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c, 1,1),
529 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c, 1,0),
530 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c, 0,0),*/
531 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
532 pa_liquid1.x0(), pa_liquid1.y1()),
533 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
534 pa_liquid1.x1(), pa_liquid1.y1()),
535 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
536 pa_liquid1.x1(), pa_liquid1.y0()),
537 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
538 pa_liquid1.x0(), pa_liquid1.y0()),
542 If our topside is liquid, set upper border of face
543 at upper border of node
545 if(top_is_same_liquid)
547 vertices[2].Pos.Y = 0.5*BS;
548 vertices[3].Pos.Y = 0.5*BS;
551 Otherwise upper position of face is corner levels
555 vertices[2].Pos.Y = corner_levels[side_corners[i][0]];
556 vertices[3].Pos.Y = corner_levels[side_corners[i][1]];
560 If neighbor is liquid, lower border of face is corner
563 if(neighbor_is_same_liquid)
565 vertices[0].Pos.Y = corner_levels[side_corners[i][1]];
566 vertices[1].Pos.Y = corner_levels[side_corners[i][0]];
569 If neighbor is not liquid, lower border of face is
574 vertices[0].Pos.Y = -0.5*BS;
575 vertices[1].Pos.Y = -0.5*BS;
578 for(s32 j=0; j<4; j++)
580 if(dir == v3s16(0,0,1))
581 vertices[j].Pos.rotateXZBy(0);
582 if(dir == v3s16(0,0,-1))
583 vertices[j].Pos.rotateXZBy(180);
584 if(dir == v3s16(-1,0,0))
585 vertices[j].Pos.rotateXZBy(90);
586 if(dir == v3s16(1,0,-0))
587 vertices[j].Pos.rotateXZBy(-90);
589 // Do this to not cause glitches when two liquids are
591 if(neighbor_is_same_liquid == false){
592 vertices[j].Pos.X *= 0.98;
593 vertices[j].Pos.Z *= 0.98;
596 vertices[j].Pos += intToFloat(p + blockpos_nodes, BS);
599 u16 indices[] = {0,1,2,2,3,0};
600 // Add to mesh collector
601 collector.append(liquid_material, vertices, 4, indices, 6);
605 Generate top side, if appropriate
608 if(top_is_same_liquid == false)
610 video::S3DVertex vertices[4] =
612 /*video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c, 0,1),
613 video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c, 1,1),
614 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c, 1,0),
615 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c, 0,0),*/
616 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
617 pa_liquid1.x0(), pa_liquid1.y1()),
618 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
619 pa_liquid1.x1(), pa_liquid1.y1()),
620 video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c,
621 pa_liquid1.x1(), pa_liquid1.y0()),
622 video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c,
623 pa_liquid1.x0(), pa_liquid1.y0()),
626 // This fixes a strange bug
627 s32 corner_resolve[4] = {3,2,1,0};
629 for(s32 i=0; i<4; i++)
631 //vertices[i].Pos.Y += liquid_level;
632 //vertices[i].Pos.Y += neighbor_levels[v3s16(0,0,0)];
633 s32 j = corner_resolve[i];
634 vertices[i].Pos.Y += corner_levels[j];
635 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
638 u16 indices[] = {0,1,2,2,3,0};
639 // Add to mesh collector
640 collector.append(liquid_material, vertices, 4, indices, 6);
644 Add water sources to mesh if using new style
646 else if(content_features(n).liquid_type == LIQUID_SOURCE
649 assert(content_features(n).special_material);
650 video::SMaterial &liquid_material =
651 *content_features(n).special_material;
652 assert(content_features(n).special_atlas);
653 AtlasPointer &pa_liquid1 =
654 *content_features(n).special_atlas;
656 bool top_is_air = false;
657 MapNode n = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y+1,z));
658 if(n.getContent() == CONTENT_AIR)
661 if(top_is_air == false)
664 u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio));
665 video::SColor c = MapBlock_LightColor(
666 content_features(n).vertex_alpha, l);
668 video::S3DVertex vertices[4] =
670 /*video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c, 0,1),
671 video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c, 1,1),
672 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c, 1,0),
673 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c, 0,0),*/
674 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
675 pa_liquid1.x0(), pa_liquid1.y1()),
676 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
677 pa_liquid1.x1(), pa_liquid1.y1()),
678 video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c,
679 pa_liquid1.x1(), pa_liquid1.y0()),
680 video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c,
681 pa_liquid1.x0(), pa_liquid1.y0()),
684 for(s32 i=0; i<4; i++)
686 vertices[i].Pos.Y += (-0.5+node_liquid_level)*BS;
687 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
690 u16 indices[] = {0,1,2,2,3,0};
691 // Add to mesh collector
692 collector.append(liquid_material, vertices, 4, indices, 6);
695 Add leaves if using new style
697 else if(n.getContent() == CONTENT_LEAVES && new_style_leaves)
699 /*u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio));*/
700 u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio)));
701 video::SColor c = MapBlock_LightColor(255, l);
703 for(u32 j=0; j<6; j++)
705 video::S3DVertex vertices[4] =
707 /*video::S3DVertex(-BS/2,-BS/2,BS/2, 0,0,0, c, 0,1),
708 video::S3DVertex(BS/2,-BS/2,BS/2, 0,0,0, c, 1,1),
709 video::S3DVertex(BS/2,BS/2,BS/2, 0,0,0, c, 1,0),
710 video::S3DVertex(-BS/2,BS/2,BS/2, 0,0,0, c, 0,0),*/
711 video::S3DVertex(-BS/2,-BS/2,BS/2, 0,0,0, c,
712 pa_leaves1.x0(), pa_leaves1.y1()),
713 video::S3DVertex(BS/2,-BS/2,BS/2, 0,0,0, c,
714 pa_leaves1.x1(), pa_leaves1.y1()),
715 video::S3DVertex(BS/2,BS/2,BS/2, 0,0,0, c,
716 pa_leaves1.x1(), pa_leaves1.y0()),
717 video::S3DVertex(-BS/2,BS/2,BS/2, 0,0,0, c,
718 pa_leaves1.x0(), pa_leaves1.y0()),
723 for(u16 i=0; i<4; i++)
724 vertices[i].Pos.rotateXZBy(0);
728 for(u16 i=0; i<4; i++)
729 vertices[i].Pos.rotateXZBy(180);
733 for(u16 i=0; i<4; i++)
734 vertices[i].Pos.rotateXZBy(-90);
738 for(u16 i=0; i<4; i++)
739 vertices[i].Pos.rotateXZBy(90);
743 for(u16 i=0; i<4; i++)
744 vertices[i].Pos.rotateYZBy(-90);
748 for(u16 i=0; i<4; i++)
749 vertices[i].Pos.rotateYZBy(90);
752 for(u16 i=0; i<4; i++)
754 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
757 u16 indices[] = {0,1,2,2,3,0};
758 // Add to mesh collector
759 collector.append(material_leaves1, vertices, 4, indices, 6);
765 else if(n.getContent() == CONTENT_GLASS)
767 u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio)));
768 video::SColor c = MapBlock_LightColor(255, l);
770 for(u32 j=0; j<6; j++)
772 video::S3DVertex vertices[4] =
774 video::S3DVertex(-BS/2,-BS/2,BS/2, 0,0,0, c,
775 pa_glass.x0(), pa_glass.y1()),
776 video::S3DVertex(BS/2,-BS/2,BS/2, 0,0,0, c,
777 pa_glass.x1(), pa_glass.y1()),
778 video::S3DVertex(BS/2,BS/2,BS/2, 0,0,0, c,
779 pa_glass.x1(), pa_glass.y0()),
780 video::S3DVertex(-BS/2,BS/2,BS/2, 0,0,0, c,
781 pa_glass.x0(), pa_glass.y0()),
786 for(u16 i=0; i<4; i++)
787 vertices[i].Pos.rotateXZBy(0);
791 for(u16 i=0; i<4; i++)
792 vertices[i].Pos.rotateXZBy(180);
796 for(u16 i=0; i<4; i++)
797 vertices[i].Pos.rotateXZBy(-90);
801 for(u16 i=0; i<4; i++)
802 vertices[i].Pos.rotateXZBy(90);
806 for(u16 i=0; i<4; i++)
807 vertices[i].Pos.rotateYZBy(-90);
811 for(u16 i=0; i<4; i++)
812 vertices[i].Pos.rotateYZBy(90);
815 for(u16 i=0; i<4; i++)
817 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
820 u16 indices[] = {0,1,2,2,3,0};
821 // Add to mesh collector
822 collector.append(material_glass, vertices, 4, indices, 6);
828 else if(n.getContent() == CONTENT_FENCE)
830 u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio)));
831 video::SColor c = MapBlock_LightColor(255, l);
833 const f32 post_rad=(f32)BS/10;
834 const f32 bar_rad=(f32)BS/20;
835 const f32 bar_len=(f32)(BS/2)-post_rad;
837 // The post - always present
838 v3f pos = intToFloat(p+blockpos_nodes, BS);
846 makeCuboid(material_wood, &collector,
848 post_rad,BS/2,post_rad, postuv);
850 // Now a section of fence, +X, if there's a post there
853 MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
854 if(n2.getContent() == CONTENT_FENCE)
856 pos = intToFloat(p+blockpos_nodes, BS);
866 makeCuboid(material_wood, &collector,
868 bar_len,bar_rad,bar_rad, xrailuv);
871 makeCuboid(material_wood, &collector,
873 bar_len,bar_rad,bar_rad, xrailuv);
876 // Now a section of fence, +Z, if there's a post there
879 n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
880 if(n2.getContent() == CONTENT_FENCE)
882 pos = intToFloat(p+blockpos_nodes, BS);
892 makeCuboid(material_wood, &collector,
894 bar_rad,bar_rad,bar_len, zrailuv);
896 makeCuboid(material_wood, &collector,
898 bar_rad,bar_rad,bar_len, zrailuv);
905 Add stones with minerals if stone is invisible
907 else if(n.getContent() == CONTENT_STONE && invisible_stone && n.getMineral() != MINERAL_NONE)
909 for(u32 j=0; j<6; j++)
911 // NOTE: Hopefully g_6dirs[j] is the right direction...
912 v3s16 dir = g_6dirs[j];
914 MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + dir);
915 if(content_features(n2).param_type == CPT_LIGHT)
916 l = decode_light(n2.getLightBlend(data->m_daynight_ratio));
920 video::SColor c = MapBlock_LightColor(255, l);
922 // Get the right texture
923 TileSpec ts = n.getTile(dir);
924 AtlasPointer ap = ts.texture;
925 material_general.setTexture(0, ap.atlas);
927 video::S3DVertex vertices[4] =
929 /*video::S3DVertex(-BS/2,-BS/2,BS/2, 0,0,0, c, 0,1),
930 video::S3DVertex(BS/2,-BS/2,BS/2, 0,0,0, c, 1,1),
931 video::S3DVertex(BS/2,BS/2,BS/2, 0,0,0, c, 1,0),
932 video::S3DVertex(-BS/2,BS/2,BS/2, 0,0,0, c, 0,0),*/
933 video::S3DVertex(-BS/2,-BS/2,BS/2, 0,0,0, c,
935 video::S3DVertex(BS/2,-BS/2,BS/2, 0,0,0, c,
937 video::S3DVertex(BS/2,BS/2,BS/2, 0,0,0, c,
939 video::S3DVertex(-BS/2,BS/2,BS/2, 0,0,0, c,
945 for(u16 i=0; i<4; i++)
946 vertices[i].Pos.rotateXZBy(0);
950 for(u16 i=0; i<4; i++)
951 vertices[i].Pos.rotateXZBy(180);
955 for(u16 i=0; i<4; i++)
956 vertices[i].Pos.rotateXZBy(-90);
960 for(u16 i=0; i<4; i++)
961 vertices[i].Pos.rotateXZBy(90);
965 for(u16 i=0; i<4; i++)
967 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
970 u16 indices[] = {0,1,2,2,3,0};
971 // Add to mesh collector
972 collector.append(material_general, vertices, 4, indices, 6);
976 else if(n.getContent() == CONTENT_PAPYRUS)
978 u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio)));
979 video::SColor c = MapBlock_LightColor(255, l);
981 for(u32 j=0; j<4; j++)
983 video::S3DVertex vertices[4] =
985 video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c,
986 pa_papyrus.x0(), pa_papyrus.y1()),
987 video::S3DVertex(BS/2,-BS/2,0, 0,0,0, c,
988 pa_papyrus.x1(), pa_papyrus.y1()),
989 video::S3DVertex(BS/2,BS/2,0, 0,0,0, c,
990 pa_papyrus.x1(), pa_papyrus.y0()),
991 video::S3DVertex(-BS/2,BS/2,0, 0,0,0, c,
992 pa_papyrus.x0(), pa_papyrus.y0()),
997 for(u16 i=0; i<4; i++)
998 vertices[i].Pos.rotateXZBy(45);
1002 for(u16 i=0; i<4; i++)
1003 vertices[i].Pos.rotateXZBy(-45);
1007 for(u16 i=0; i<4; i++)
1008 vertices[i].Pos.rotateXZBy(135);
1012 for(u16 i=0; i<4; i++)
1013 vertices[i].Pos.rotateXZBy(-135);
1016 for(u16 i=0; i<4; i++)
1018 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
1021 u16 indices[] = {0,1,2,2,3,0};
1022 // Add to mesh collector
1023 collector.append(material_papyrus, vertices, 4, indices, 6);
1026 else if(n.getContent() == CONTENT_JUNGLEGRASS)
1028 u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio)));
1029 video::SColor c = MapBlock_LightColor(255, l);
1031 for(u32 j=0; j<4; j++)
1033 video::S3DVertex vertices[4] =
1035 video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c,
1036 pa_papyrus.x0(), pa_papyrus.y1()),
1037 video::S3DVertex(BS/2,-BS/2,0, 0,0,0, c,
1038 pa_papyrus.x1(), pa_papyrus.y1()),
1039 video::S3DVertex(BS/2,BS/1,0, 0,0,0, c,
1040 pa_papyrus.x1(), pa_papyrus.y0()),
1041 video::S3DVertex(-BS/2,BS/1,0, 0,0,0, c,
1042 pa_papyrus.x0(), pa_papyrus.y0()),
1047 for(u16 i=0; i<4; i++)
1048 vertices[i].Pos.rotateXZBy(45);
1052 for(u16 i=0; i<4; i++)
1053 vertices[i].Pos.rotateXZBy(-45);
1057 for(u16 i=0; i<4; i++)
1058 vertices[i].Pos.rotateXZBy(135);
1062 for(u16 i=0; i<4; i++)
1063 vertices[i].Pos.rotateXZBy(-135);
1066 for(u16 i=0; i<4; i++)
1068 vertices[i].Pos *= 1.3;
1069 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
1072 u16 indices[] = {0,1,2,2,3,0};
1073 // Add to mesh collector
1074 collector.append(material_junglegrass, vertices, 4, indices, 6);
1077 else if(n.getContent() == CONTENT_RAIL)
1079 u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio));
1080 video::SColor c = MapBlock_LightColor(255, l);
1082 bool is_rail_x [] = { false, false }; /* x-1, x+1 */
1083 bool is_rail_z [] = { false, false }; /* z-1, z+1 */
1085 MapNode n_minus_x = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x-1,y,z));
1086 MapNode n_plus_x = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x+1,y,z));
1087 MapNode n_minus_z = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y,z-1));
1088 MapNode n_plus_z = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y,z+1));
1090 if(n_minus_x.getContent() == CONTENT_RAIL)
1091 is_rail_x[0] = true;
1092 if(n_plus_x.getContent() == CONTENT_RAIL)
1093 is_rail_x[1] = true;
1094 if(n_minus_z.getContent() == CONTENT_RAIL)
1095 is_rail_z[0] = true;
1096 if(n_plus_z.getContent() == CONTENT_RAIL)
1097 is_rail_z[1] = true;
1099 float d = (float)BS/16;
1100 video::S3DVertex vertices[4] =
1102 video::S3DVertex(-BS/2,-BS/2+d,-BS/2, 0,0,0, c,
1104 video::S3DVertex(BS/2,-BS/2+d,-BS/2, 0,0,0, c,
1106 video::S3DVertex(BS/2,-BS/2+d,BS/2, 0,0,0, c,
1108 video::S3DVertex(-BS/2,-BS/2+d,BS/2, 0,0,0, c,
1112 video::SMaterial material_rail;
1113 material_rail.setFlag(video::EMF_LIGHTING, false);
1114 material_rail.setFlag(video::EMF_BACK_FACE_CULLING, false);
1115 material_rail.setFlag(video::EMF_BILINEAR_FILTER, false);
1116 material_rail.setFlag(video::EMF_FOG_ENABLE, true);
1117 material_rail.MaterialType
1118 = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
1120 int adjacencies = is_rail_x[0] + is_rail_x[1] + is_rail_z[0] + is_rail_z[1];
1124 material_rail.setTexture(0, g_texturesource->getTextureRaw("rail.png"));
1125 else if(adjacencies == 2)
1127 if((is_rail_x[0] && is_rail_x[1]) || (is_rail_z[0] && is_rail_z[1]))
1128 material_rail.setTexture(0, g_texturesource->getTextureRaw("rail.png"));
1130 material_rail.setTexture(0, g_texturesource->getTextureRaw("rail_curved.png"));
1132 else if(adjacencies == 3)
1133 material_rail.setTexture(0, g_texturesource->getTextureRaw("rail_t_junction.png"));
1134 else if(adjacencies == 4)
1135 material_rail.setTexture(0, g_texturesource->getTextureRaw("rail_crossing.png"));
1140 if(adjacencies == 1)
1142 if(is_rail_x[0] || is_rail_x[1])
1145 else if(adjacencies == 2)
1147 if(is_rail_x[0] && is_rail_x[1])
1149 else if(is_rail_x[0] && is_rail_z[0])
1151 else if(is_rail_x[0] && is_rail_z[1])
1153 else if(is_rail_x[1] && is_rail_z[1])
1156 else if(adjacencies == 3)
1169 for(u16 i=0; i<4; i++)
1170 vertices[i].Pos.rotateXZBy(angle);
1173 for(s32 i=0; i<4; i++)
1175 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
1178 u16 indices[] = {0,1,2,2,3,0};
1179 collector.append(material_rail, vertices, 4, indices, 6);
1181 else if (n.getContent() == CONTENT_LADDER) {
1182 u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio));
1183 video::SColor c(255,l,l,l);
1185 float d = (float)BS/16;
1187 // Assume wall is at X+
1188 video::S3DVertex vertices[4] =
1190 video::S3DVertex(BS/2-d,-BS/2,-BS/2, 0,0,0, c, 0,1),
1191 video::S3DVertex(BS/2-d,-BS/2,BS/2, 0,0,0, c, 1,1),
1192 video::S3DVertex(BS/2-d,BS/2,BS/2, 0,0,0, c, 1,0),
1193 video::S3DVertex(BS/2-d,BS/2,-BS/2, 0,0,0, c, 0,0),
1196 v3s16 dir = unpackDir(n.param2);
1198 for(s32 i=0; i<4; i++)
1200 if(dir == v3s16(1,0,0))
1201 vertices[i].Pos.rotateXZBy(0);
1202 if(dir == v3s16(-1,0,0))
1203 vertices[i].Pos.rotateXZBy(180);
1204 if(dir == v3s16(0,0,1))
1205 vertices[i].Pos.rotateXZBy(90);
1206 if(dir == v3s16(0,0,-1))
1207 vertices[i].Pos.rotateXZBy(-90);
1208 if(dir == v3s16(0,-1,0))
1209 vertices[i].Pos.rotateXYBy(-90);
1210 if(dir == v3s16(0,1,0))
1211 vertices[i].Pos.rotateXYBy(90);
1213 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
1216 video::SMaterial material_ladder;
1217 material_ladder.setFlag(video::EMF_LIGHTING, false);
1218 material_ladder.setFlag(video::EMF_BACK_FACE_CULLING, false);
1219 material_ladder.setFlag(video::EMF_BILINEAR_FILTER, false);
1220 material_ladder.setFlag(video::EMF_FOG_ENABLE, true);
1221 material_ladder.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
1222 material_ladder.setTexture(0, g_texturesource->getTextureRaw("ladder.png"));
1224 u16 indices[] = {0,1,2,2,3,0};
1225 // Add to mesh collector
1226 collector.append(material_ladder, vertices, 4, indices, 6);
1228 else if(n.getContent() == CONTENT_APPLE)
1230 u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio)));
1231 video::SColor c = MapBlock_LightColor(255, l);
1233 for(u32 j=0; j<4; j++)
1235 video::S3DVertex vertices[4] =
1237 video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c,
1238 pa_apple.x0(), pa_apple.y1()),
1239 video::S3DVertex(BS/2,-BS/2,0, 0,0,0, c,
1240 pa_apple.x1(), pa_apple.y1()),
1241 video::S3DVertex(BS/2,BS/2,0, 0,0,0, c,
1242 pa_apple.x1(), pa_apple.y0()),
1243 video::S3DVertex(-BS/2,BS/2,0, 0,0,0, c,
1244 pa_apple.x0(), pa_apple.y0()),
1249 for(u16 i=0; i<4; i++)
1250 vertices[i].Pos.rotateXZBy(45);
1254 for(u16 i=0; i<4; i++)
1255 vertices[i].Pos.rotateXZBy(-45);
1259 for(u16 i=0; i<4; i++)
1260 vertices[i].Pos.rotateXZBy(135);
1264 for(u16 i=0; i<4; i++)
1265 vertices[i].Pos.rotateXZBy(-135);
1268 for(u16 i=0; i<4; i++)
1270 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
1273 u16 indices[] = {0,1,2,2,3,0};
1274 // Add to mesh collector
1275 collector.append(material_apple, vertices, 4, indices, 6);
1278 else if(n.getContent() == CONTENT_SAPLING) {
1279 u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio)));
1280 video::SColor c = MapBlock_LightColor(255, l);
1282 for(u32 j=0; j<4; j++)
1284 video::S3DVertex vertices[4] =
1286 video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c,
1287 pa_sapling.x0(), pa_sapling.y1()),
1288 video::S3DVertex(BS/2,-BS/2,0, 0,0,0, c,
1289 pa_sapling.x1(), pa_sapling.y1()),
1290 video::S3DVertex(BS/2,BS/1,0, 0,0,0, c,
1291 pa_sapling.x1(), pa_sapling.y0()),
1292 video::S3DVertex(-BS/2,BS/1,0, 0,0,0, c,
1293 pa_sapling.x0(), pa_sapling.y0()),
1298 for(u16 i=0; i<4; i++)
1299 vertices[i].Pos.rotateXZBy(45);
1303 for(u16 i=0; i<4; i++)
1304 vertices[i].Pos.rotateXZBy(-45);
1308 for(u16 i=0; i<4; i++)
1309 vertices[i].Pos.rotateXZBy(135);
1313 for(u16 i=0; i<4; i++)
1314 vertices[i].Pos.rotateXZBy(-135);
1317 for(u16 i=0; i<4; i++)
1319 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
1322 u16 indices[] = {0,1,2,2,3,0};
1323 // Add to mesh collector
1324 collector.append(material_sapling, vertices, 4, indices, 6);