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
24 #include "mapblock_mesh.h" // For MapBlock_LightColor()
31 // material - the material to use (for all 6 faces)
32 // collector - the MeshCollector for the resulting polygons
33 // pa - texture atlas pointer for the material
34 // c - vertex colour - used for all
35 // pos - the position of the centre of the cuboid
36 // rz,ry,rz - the radius of the cuboid in each dimension
37 // txc - texture coordinates - this is a list of texture coordinates
38 // for the opposite corners of each face - therefore, there
39 // should be (2+2)*6=24 values in the list. Alternatively, pass
40 // NULL to use the entire texture for each face. The order of
41 // the faces in the list is top-backi-right-front-left-bottom
42 // If you specified 0,0,1,1 for each face, that would be the
43 // same as passing NULL.
44 void makeCuboid(video::SMaterial &material, MeshCollector *collector,
45 AtlasPointer* pa, video::SColor &c,
46 v3f &pos, f32 rx, f32 ry, f32 rz, f32* txc)
55 video::S3DVertex v[4] =
57 video::S3DVertex(0,0,0, 0,0,0, c, tu0, tv1),
58 video::S3DVertex(0,0,0, 0,0,0, c, tu1, tv1),
59 video::S3DVertex(0,0,0, 0,0,0, c, tu1, tv0),
60 video::S3DVertex(0,0,0, 0,0,0, c, tu0, tv0)
68 v[0].Pos.X=-rx; v[0].Pos.Y= ry; v[0].Pos.Z=-rz;
69 v[1].Pos.X=-rx; v[1].Pos.Y= ry; v[1].Pos.Z= rz;
70 v[2].Pos.X= rx; v[2].Pos.Y= ry; v[2].Pos.Z= rz;
71 v[3].Pos.X= rx; v[3].Pos.Y= ry, v[3].Pos.Z=-rz;
74 v[0].Pos.X=-rx; v[0].Pos.Y= ry; v[0].Pos.Z=-rz;
75 v[1].Pos.X= rx; v[1].Pos.Y= ry; v[1].Pos.Z=-rz;
76 v[2].Pos.X= rx; v[2].Pos.Y=-ry; v[2].Pos.Z=-rz;
77 v[3].Pos.X=-rx; v[3].Pos.Y=-ry, v[3].Pos.Z=-rz;
80 v[0].Pos.X= rx; v[0].Pos.Y= ry; v[0].Pos.Z=-rz;
81 v[1].Pos.X= rx; v[1].Pos.Y= ry; v[1].Pos.Z= rz;
82 v[2].Pos.X= rx; v[2].Pos.Y=-ry; v[2].Pos.Z= rz;
83 v[3].Pos.X= rx; v[3].Pos.Y=-ry, v[3].Pos.Z=-rz;
86 v[0].Pos.X= rx; v[0].Pos.Y= ry; v[0].Pos.Z= rz;
87 v[1].Pos.X=-rx; v[1].Pos.Y= ry; v[1].Pos.Z= rz;
88 v[2].Pos.X=-rx; v[2].Pos.Y=-ry; v[2].Pos.Z= rz;
89 v[3].Pos.X= rx; v[3].Pos.Y=-ry, v[3].Pos.Z= rz;
92 v[0].Pos.X=-rx; v[0].Pos.Y= ry; v[0].Pos.Z= rz;
93 v[1].Pos.X=-rx; v[1].Pos.Y= ry; v[1].Pos.Z=-rz;
94 v[2].Pos.X=-rx; v[2].Pos.Y=-ry; v[2].Pos.Z=-rz;
95 v[3].Pos.X=-rx; v[3].Pos.Y=-ry, v[3].Pos.Z= rz;
98 v[0].Pos.X= rx; v[0].Pos.Y=-ry; v[0].Pos.Z= rz;
99 v[1].Pos.X=-rx; v[1].Pos.Y=-ry; v[1].Pos.Z= rz;
100 v[2].Pos.X=-rx; v[2].Pos.Y=-ry; v[2].Pos.Z=-rz;
101 v[3].Pos.X= rx; v[3].Pos.Y=-ry, v[3].Pos.Z=-rz;
107 v[0].TCoords.X=tu0+txus*txc[0]; v[0].TCoords.Y=tv0+txvs*txc[3];
108 v[1].TCoords.X=tu0+txus*txc[2]; v[1].TCoords.Y=tv0+txvs*txc[3];
109 v[2].TCoords.X=tu0+txus*txc[2]; v[2].TCoords.Y=tv0+txvs*txc[1];
110 v[3].TCoords.X=tu0+txus*txc[0]; v[3].TCoords.Y=tv0+txvs*txc[1];
114 for(u16 i=0; i<4; i++)
116 u16 indices[] = {0,1,2,2,3,0};
117 collector->append(material, v, 4, indices, 6);
125 void mapblock_mesh_generate_special(MeshMakeData *data,
126 MeshCollector &collector, IGameDef *gamedef)
128 ITextureSource *tsrc = gamedef->tsrc();
129 INodeDefManager *nodedef = gamedef->ndef();
132 //TimeTaker timer("mapblock_mesh_generate_special()");
137 bool new_style_water = g_settings->getBool("new_style_water");
138 bool new_style_leaves = g_settings->getBool("new_style_leaves");
139 //bool smooth_lighting = g_settings->getBool("smooth_lighting");
140 bool invisible_stone = g_settings->getBool("invisible_stone");
142 float node_liquid_level = 1.0;
144 node_liquid_level = 0.85;
146 v3s16 blockpos_nodes = data->m_blockpos*MAP_BLOCKSIZE;
148 // New-style leaves material
149 video::SMaterial material_leaves1;
150 material_leaves1.setFlag(video::EMF_LIGHTING, false);
151 material_leaves1.setFlag(video::EMF_BILINEAR_FILTER, false);
152 material_leaves1.setFlag(video::EMF_FOG_ENABLE, true);
153 material_leaves1.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
154 AtlasPointer pa_leaves1 = tsrc->getTexture(
155 tsrc->getTextureId("leaves.png"));
156 material_leaves1.setTexture(0, pa_leaves1.atlas);
159 video::SMaterial material_glass;
160 material_glass.setFlag(video::EMF_LIGHTING, false);
161 material_glass.setFlag(video::EMF_BILINEAR_FILTER, false);
162 material_glass.setFlag(video::EMF_FOG_ENABLE, true);
163 material_glass.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
164 AtlasPointer pa_glass = tsrc->getTexture(
165 tsrc->getTextureId("glass.png"));
166 material_glass.setTexture(0, pa_glass.atlas);
169 video::SMaterial material_wood;
170 material_wood.setFlag(video::EMF_LIGHTING, false);
171 material_wood.setFlag(video::EMF_BILINEAR_FILTER, false);
172 material_wood.setFlag(video::EMF_FOG_ENABLE, true);
173 material_wood.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
174 AtlasPointer pa_wood = tsrc->getTexture(
175 tsrc->getTextureId("wood.png"));
176 material_wood.setTexture(0, pa_wood.atlas);
178 // General ground material for special output
179 // Texture is modified just before usage
180 video::SMaterial material_general;
181 material_general.setFlag(video::EMF_LIGHTING, false);
182 material_general.setFlag(video::EMF_BILINEAR_FILTER, false);
183 material_general.setFlag(video::EMF_FOG_ENABLE, true);
184 material_general.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
188 video::SMaterial material_papyrus;
189 material_papyrus.setFlag(video::EMF_LIGHTING, false);
190 material_papyrus.setFlag(video::EMF_BILINEAR_FILTER, false);
191 material_papyrus.setFlag(video::EMF_FOG_ENABLE, true);
192 material_papyrus.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
193 AtlasPointer pa_papyrus = tsrc->getTexture(
194 tsrc->getTextureId("papyrus.png"));
195 material_papyrus.setTexture(0, pa_papyrus.atlas);
198 video::SMaterial material_apple;
199 material_apple.setFlag(video::EMF_LIGHTING, false);
200 material_apple.setFlag(video::EMF_BILINEAR_FILTER, false);
201 material_apple.setFlag(video::EMF_FOG_ENABLE, true);
202 material_apple.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
203 AtlasPointer pa_apple = tsrc->getTexture(
204 tsrc->getTextureId("apple.png"));
205 material_apple.setTexture(0, pa_apple.atlas);
209 video::SMaterial material_sapling;
210 material_sapling.setFlag(video::EMF_LIGHTING, false);
211 material_sapling.setFlag(video::EMF_BILINEAR_FILTER, false);
212 material_sapling.setFlag(video::EMF_FOG_ENABLE, true);
213 material_sapling.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
214 AtlasPointer pa_sapling = tsrc->getTexture(
215 tsrc->getTextureId("sapling.png"));
216 material_sapling.setTexture(0, pa_sapling.atlas);
219 // junglegrass material
220 video::SMaterial material_junglegrass;
221 material_junglegrass.setFlag(video::EMF_LIGHTING, false);
222 material_junglegrass.setFlag(video::EMF_BILINEAR_FILTER, false);
223 material_junglegrass.setFlag(video::EMF_FOG_ENABLE, true);
224 material_junglegrass.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
225 AtlasPointer pa_junglegrass = tsrc->getTexture(
226 tsrc->getTextureId("junglegrass.png"));
227 material_junglegrass.setTexture(0, pa_junglegrass.atlas);
229 for(s16 z=0; z<MAP_BLOCKSIZE; z++)
230 for(s16 y=0; y<MAP_BLOCKSIZE; y++)
231 for(s16 x=0; x<MAP_BLOCKSIZE; x++)
235 MapNode n = data->m_vmanip.getNodeNoEx(blockpos_nodes+p);
240 if(n.getContent() == CONTENT_TORCH)
242 v3s16 dir = unpackDir(n.param2);
244 const char *texturename = "torch.png";
245 if(dir == v3s16(0,-1,0)){
246 texturename = "torch_on_floor.png";
247 } else if(dir == v3s16(0,1,0)){
248 texturename = "torch_on_ceiling.png";
249 // For backwards compatibility
250 } else if(dir == v3s16(0,0,0)){
251 texturename = "torch_on_floor.png";
253 texturename = "torch.png";
256 AtlasPointer ap = tsrc->getTexture(texturename);
259 video::SMaterial material;
260 material.setFlag(video::EMF_LIGHTING, false);
261 material.setFlag(video::EMF_BACK_FACE_CULLING, false);
262 material.setFlag(video::EMF_BILINEAR_FILTER, false);
263 material.setFlag(video::EMF_FOG_ENABLE, true);
264 //material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
265 material.MaterialType
266 = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
267 material.setTexture(0, ap.atlas);
269 video::SColor c(255,255,255,255);
271 // Wall at X+ of node
272 video::S3DVertex vertices[4] =
274 video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c,
276 video::S3DVertex(BS/2,-BS/2,0, 0,0,0, c,
278 video::S3DVertex(BS/2,BS/2,0, 0,0,0, c,
280 video::S3DVertex(-BS/2,BS/2,0, 0,0,0, c,
284 for(s32 i=0; i<4; i++)
286 if(dir == v3s16(1,0,0))
287 vertices[i].Pos.rotateXZBy(0);
288 if(dir == v3s16(-1,0,0))
289 vertices[i].Pos.rotateXZBy(180);
290 if(dir == v3s16(0,0,1))
291 vertices[i].Pos.rotateXZBy(90);
292 if(dir == v3s16(0,0,-1))
293 vertices[i].Pos.rotateXZBy(-90);
294 if(dir == v3s16(0,-1,0))
295 vertices[i].Pos.rotateXZBy(45);
296 if(dir == v3s16(0,1,0))
297 vertices[i].Pos.rotateXZBy(-45);
299 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
302 u16 indices[] = {0,1,2,2,3,0};
303 // Add to mesh collector
304 collector.append(material, vertices, 4, indices, 6);
309 else if(n.getContent() == CONTENT_SIGN_WALL)
311 AtlasPointer ap = tsrc->getTexture("sign_wall.png");
314 video::SMaterial material;
315 material.setFlag(video::EMF_LIGHTING, false);
316 material.setFlag(video::EMF_BACK_FACE_CULLING, false);
317 material.setFlag(video::EMF_BILINEAR_FILTER, false);
318 material.setFlag(video::EMF_FOG_ENABLE, true);
319 material.MaterialType
320 = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
321 material.setTexture(0, ap.atlas);
323 u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio, nodedef));
324 video::SColor c = MapBlock_LightColor(255, l);
326 float d = (float)BS/16;
327 // Wall at X+ of node
328 video::S3DVertex vertices[4] =
330 video::S3DVertex(BS/2-d,-BS/2,-BS/2, 0,0,0, c,
332 video::S3DVertex(BS/2-d,-BS/2,BS/2, 0,0,0, c,
334 video::S3DVertex(BS/2-d,BS/2,BS/2, 0,0,0, c,
336 video::S3DVertex(BS/2-d,BS/2,-BS/2, 0,0,0, c,
340 v3s16 dir = unpackDir(n.param2);
342 for(s32 i=0; i<4; i++)
344 if(dir == v3s16(1,0,0))
345 vertices[i].Pos.rotateXZBy(0);
346 if(dir == v3s16(-1,0,0))
347 vertices[i].Pos.rotateXZBy(180);
348 if(dir == v3s16(0,0,1))
349 vertices[i].Pos.rotateXZBy(90);
350 if(dir == v3s16(0,0,-1))
351 vertices[i].Pos.rotateXZBy(-90);
352 if(dir == v3s16(0,-1,0))
353 vertices[i].Pos.rotateXYBy(-90);
354 if(dir == v3s16(0,1,0))
355 vertices[i].Pos.rotateXYBy(90);
357 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
360 u16 indices[] = {0,1,2,2,3,0};
361 // Add to mesh collector
362 collector.append(material, vertices, 4, indices, 6);
365 Add flowing liquid to mesh
367 else if(nodedef->get(n).liquid_type == LIQUID_FLOWING)
369 assert(nodedef->get(n).special_material);
370 video::SMaterial &liquid_material =
371 *nodedef->get(n).special_material;
372 video::SMaterial &liquid_material_bfculled =
373 *nodedef->get(n).special_material2;
375 assert(nodedef->get(n).special_atlas);
376 AtlasPointer &pa_liquid1 =
377 *nodedef->get(n).special_atlas;
379 bool top_is_same_liquid = false;
380 MapNode ntop = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y+1,z));
381 content_t c_flowing = nodedef->get(n).liquid_alternative_flowing;
382 content_t c_source = nodedef->get(n).liquid_alternative_source;
383 if(ntop.getContent() == c_flowing || ntop.getContent() == c_source)
384 top_is_same_liquid = true;
387 // Use the light of the node on top if possible
388 if(nodedef->get(ntop).param_type == CPT_LIGHT)
389 l = decode_light(ntop.getLightBlend(data->m_daynight_ratio, nodedef));
390 // Otherwise use the light of this node (the liquid)
392 l = decode_light(n.getLightBlend(data->m_daynight_ratio, nodedef));
393 video::SColor c = MapBlock_LightColor(
394 nodedef->get(n).vertex_alpha, l);
396 // Neighbor liquid levels (key = relative position)
397 // Includes current node
398 core::map<v3s16, f32> neighbor_levels;
399 core::map<v3s16, content_t> neighbor_contents;
400 core::map<v3s16, u8> neighbor_flags;
401 const u8 neighborflag_top_is_same_liquid = 0x01;
402 v3s16 neighbor_dirs[9] = {
413 for(u32 i=0; i<9; i++)
415 content_t content = CONTENT_AIR;
416 float level = -0.5 * BS;
419 v3s16 p2 = p + neighbor_dirs[i];
420 MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
421 if(n2.getContent() != CONTENT_IGNORE)
423 content = n2.getContent();
425 if(n2.getContent() == c_source)
426 level = (-0.5+node_liquid_level) * BS;
427 else if(n2.getContent() == c_flowing)
428 level = (-0.5 + ((float)(n2.param2&LIQUID_LEVEL_MASK)
429 + 0.5) / 8.0 * node_liquid_level) * BS;
431 // Check node above neighbor.
432 // NOTE: This doesn't get executed if neighbor
435 n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
436 if(n2.getContent() == c_source ||
437 n2.getContent() == c_flowing)
438 flags |= neighborflag_top_is_same_liquid;
441 neighbor_levels.insert(neighbor_dirs[i], level);
442 neighbor_contents.insert(neighbor_dirs[i], content);
443 neighbor_flags.insert(neighbor_dirs[i], flags);
446 // Corner heights (average between four liquids)
447 f32 corner_levels[4];
449 v3s16 halfdirs[4] = {
455 for(u32 i=0; i<4; i++)
457 v3s16 cornerdir = halfdirs[i];
458 float cornerlevel = 0;
461 for(u32 j=0; j<4; j++)
463 v3s16 neighbordir = cornerdir - halfdirs[j];
464 content_t content = neighbor_contents[neighbordir];
465 // If top is liquid, draw starting from top of node
466 if(neighbor_flags[neighbordir] &
467 neighborflag_top_is_same_liquid)
469 cornerlevel = 0.5*BS;
473 // Source is always the same height
474 else if(content == c_source)
476 cornerlevel = (-0.5+node_liquid_level)*BS;
480 // Flowing liquid has level information
481 else if(content == c_flowing)
483 cornerlevel += neighbor_levels[neighbordir];
486 else if(content == CONTENT_AIR)
492 cornerlevel = -0.5*BS;
493 else if(valid_count > 0)
494 cornerlevel /= valid_count;
495 corner_levels[i] = cornerlevel;
502 v3s16 side_dirs[4] = {
508 s16 side_corners[4][2] = {
514 for(u32 i=0; i<4; i++)
516 v3s16 dir = side_dirs[i];
519 If our topside is liquid and neighbor's topside
520 is liquid, don't draw side face
522 if(top_is_same_liquid &&
523 neighbor_flags[dir] & neighborflag_top_is_same_liquid)
526 content_t neighbor_content = neighbor_contents[dir];
527 const ContentFeatures &n_feat = nodedef->get(neighbor_content);
529 // Don't draw face if neighbor is blocking the view
530 if(n_feat.solidness == 2)
533 bool neighbor_is_same_liquid = (neighbor_content == c_source
534 || neighbor_content == c_flowing);
536 // Don't draw any faces if neighbor same is liquid and top is
538 if(neighbor_is_same_liquid == true
539 && top_is_same_liquid == false)
542 // Use backface culled material if neighbor doesn't have a
544 video::SMaterial *current_material = &liquid_material;
545 if(n_feat.solidness != 0 || n_feat.visual_solidness != 0)
546 current_material = &liquid_material_bfculled;
548 video::S3DVertex vertices[4] =
550 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
551 pa_liquid1.x0(), pa_liquid1.y1()),
552 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
553 pa_liquid1.x1(), pa_liquid1.y1()),
554 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
555 pa_liquid1.x1(), pa_liquid1.y0()),
556 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
557 pa_liquid1.x0(), pa_liquid1.y0()),
561 If our topside is liquid, set upper border of face
562 at upper border of node
564 if(top_is_same_liquid)
566 vertices[2].Pos.Y = 0.5*BS;
567 vertices[3].Pos.Y = 0.5*BS;
570 Otherwise upper position of face is corner levels
574 vertices[2].Pos.Y = corner_levels[side_corners[i][0]];
575 vertices[3].Pos.Y = corner_levels[side_corners[i][1]];
579 If neighbor is liquid, lower border of face is corner
582 if(neighbor_is_same_liquid)
584 vertices[0].Pos.Y = corner_levels[side_corners[i][1]];
585 vertices[1].Pos.Y = corner_levels[side_corners[i][0]];
588 If neighbor is not liquid, lower border of face is
593 vertices[0].Pos.Y = -0.5*BS;
594 vertices[1].Pos.Y = -0.5*BS;
597 for(s32 j=0; j<4; j++)
599 if(dir == v3s16(0,0,1))
600 vertices[j].Pos.rotateXZBy(0);
601 if(dir == v3s16(0,0,-1))
602 vertices[j].Pos.rotateXZBy(180);
603 if(dir == v3s16(-1,0,0))
604 vertices[j].Pos.rotateXZBy(90);
605 if(dir == v3s16(1,0,-0))
606 vertices[j].Pos.rotateXZBy(-90);
608 // Do this to not cause glitches when two liquids are
610 /*if(neighbor_is_same_liquid == false){
611 vertices[j].Pos.X *= 0.98;
612 vertices[j].Pos.Z *= 0.98;
615 vertices[j].Pos += intToFloat(p + blockpos_nodes, BS);
618 u16 indices[] = {0,1,2,2,3,0};
619 // Add to mesh collector
620 collector.append(*current_material, vertices, 4, indices, 6);
624 Generate top side, if appropriate
627 if(top_is_same_liquid == false)
629 video::S3DVertex vertices[4] =
631 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
632 pa_liquid1.x0(), pa_liquid1.y1()),
633 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
634 pa_liquid1.x1(), pa_liquid1.y1()),
635 video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c,
636 pa_liquid1.x1(), pa_liquid1.y0()),
637 video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c,
638 pa_liquid1.x0(), pa_liquid1.y0()),
641 // This fixes a strange bug
642 s32 corner_resolve[4] = {3,2,1,0};
644 for(s32 i=0; i<4; i++)
646 //vertices[i].Pos.Y += liquid_level;
647 //vertices[i].Pos.Y += neighbor_levels[v3s16(0,0,0)];
648 s32 j = corner_resolve[i];
649 vertices[i].Pos.Y += corner_levels[j];
650 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
653 u16 indices[] = {0,1,2,2,3,0};
654 // Add to mesh collector
655 collector.append(liquid_material, vertices, 4, indices, 6);
659 Add water sources to mesh if using new style
661 else if(nodedef->get(n).liquid_type == LIQUID_SOURCE
664 assert(nodedef->get(n).special_material);
665 video::SMaterial &liquid_material =
666 *nodedef->get(n).special_material;
667 assert(nodedef->get(n).special_atlas);
668 AtlasPointer &pa_liquid1 =
669 *nodedef->get(n).special_atlas;
671 bool top_is_air = false;
672 MapNode n = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y+1,z));
673 if(n.getContent() == CONTENT_AIR)
676 if(top_is_air == false)
679 u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio, nodedef));
680 video::SColor c = MapBlock_LightColor(
681 nodedef->get(n).vertex_alpha, l);
683 video::S3DVertex vertices[4] =
685 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
686 pa_liquid1.x0(), pa_liquid1.y1()),
687 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
688 pa_liquid1.x1(), pa_liquid1.y1()),
689 video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c,
690 pa_liquid1.x1(), pa_liquid1.y0()),
691 video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c,
692 pa_liquid1.x0(), pa_liquid1.y0()),
695 for(s32 i=0; i<4; i++)
697 vertices[i].Pos.Y += (-0.5+node_liquid_level)*BS;
698 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
701 u16 indices[] = {0,1,2,2,3,0};
702 // Add to mesh collector
703 collector.append(liquid_material, vertices, 4, indices, 6);
706 Add leaves if using new style
708 else if(n.getContent() == CONTENT_LEAVES && new_style_leaves)
710 /*u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio, nodedef));*/
711 u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio, nodedef)));
712 video::SColor c = MapBlock_LightColor(255, l);
714 for(u32 j=0; j<6; j++)
716 video::S3DVertex vertices[4] =
718 video::S3DVertex(-BS/2,-BS/2,BS/2, 0,0,0, c,
719 pa_leaves1.x0(), pa_leaves1.y1()),
720 video::S3DVertex(BS/2,-BS/2,BS/2, 0,0,0, c,
721 pa_leaves1.x1(), pa_leaves1.y1()),
722 video::S3DVertex(BS/2,BS/2,BS/2, 0,0,0, c,
723 pa_leaves1.x1(), pa_leaves1.y0()),
724 video::S3DVertex(-BS/2,BS/2,BS/2, 0,0,0, c,
725 pa_leaves1.x0(), pa_leaves1.y0()),
730 for(u16 i=0; i<4; i++)
731 vertices[i].Pos.rotateXZBy(0);
735 for(u16 i=0; i<4; i++)
736 vertices[i].Pos.rotateXZBy(180);
740 for(u16 i=0; i<4; i++)
741 vertices[i].Pos.rotateXZBy(-90);
745 for(u16 i=0; i<4; i++)
746 vertices[i].Pos.rotateXZBy(90);
750 for(u16 i=0; i<4; i++)
751 vertices[i].Pos.rotateYZBy(-90);
755 for(u16 i=0; i<4; i++)
756 vertices[i].Pos.rotateYZBy(90);
759 for(u16 i=0; i<4; i++)
761 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
764 u16 indices[] = {0,1,2,2,3,0};
765 // Add to mesh collector
766 collector.append(material_leaves1, vertices, 4, indices, 6);
772 else if(n.getContent() == CONTENT_GLASS)
774 u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio, nodedef)));
775 video::SColor c = MapBlock_LightColor(255, l);
777 for(u32 j=0; j<6; j++)
779 video::S3DVertex vertices[4] =
781 video::S3DVertex(-BS/2,-BS/2,BS/2, 0,0,0, c,
782 pa_glass.x0(), pa_glass.y1()),
783 video::S3DVertex(BS/2,-BS/2,BS/2, 0,0,0, c,
784 pa_glass.x1(), pa_glass.y1()),
785 video::S3DVertex(BS/2,BS/2,BS/2, 0,0,0, c,
786 pa_glass.x1(), pa_glass.y0()),
787 video::S3DVertex(-BS/2,BS/2,BS/2, 0,0,0, c,
788 pa_glass.x0(), pa_glass.y0()),
793 for(u16 i=0; i<4; i++)
794 vertices[i].Pos.rotateXZBy(0);
798 for(u16 i=0; i<4; i++)
799 vertices[i].Pos.rotateXZBy(180);
803 for(u16 i=0; i<4; i++)
804 vertices[i].Pos.rotateXZBy(-90);
808 for(u16 i=0; i<4; i++)
809 vertices[i].Pos.rotateXZBy(90);
813 for(u16 i=0; i<4; i++)
814 vertices[i].Pos.rotateYZBy(-90);
818 for(u16 i=0; i<4; i++)
819 vertices[i].Pos.rotateYZBy(90);
822 for(u16 i=0; i<4; i++)
824 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
827 u16 indices[] = {0,1,2,2,3,0};
828 // Add to mesh collector
829 collector.append(material_glass, vertices, 4, indices, 6);
835 else if(n.getContent() == CONTENT_FENCE)
837 u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio, nodedef)));
838 video::SColor c = MapBlock_LightColor(255, l);
840 const f32 post_rad=(f32)BS/10;
841 const f32 bar_rad=(f32)BS/20;
842 const f32 bar_len=(f32)(BS/2)-post_rad;
844 // The post - always present
845 v3f pos = intToFloat(p+blockpos_nodes, BS);
853 makeCuboid(material_wood, &collector,
855 post_rad,BS/2,post_rad, postuv);
857 // Now a section of fence, +X, if there's a post there
860 MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
861 if(n2.getContent() == CONTENT_FENCE)
863 pos = intToFloat(p+blockpos_nodes, BS);
873 makeCuboid(material_wood, &collector,
875 bar_len,bar_rad,bar_rad, xrailuv);
878 makeCuboid(material_wood, &collector,
880 bar_len,bar_rad,bar_rad, xrailuv);
883 // Now a section of fence, +Z, if there's a post there
886 n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
887 if(n2.getContent() == CONTENT_FENCE)
889 pos = intToFloat(p+blockpos_nodes, BS);
899 makeCuboid(material_wood, &collector,
901 bar_rad,bar_rad,bar_len, zrailuv);
903 makeCuboid(material_wood, &collector,
905 bar_rad,bar_rad,bar_len, zrailuv);
912 Add stones with minerals if stone is invisible
914 else if(n.getContent() == CONTENT_STONE && invisible_stone && n.getMineral(nodedef) != MINERAL_NONE)
916 for(u32 j=0; j<6; j++)
918 // NOTE: Hopefully g_6dirs[j] is the right direction...
919 v3s16 dir = g_6dirs[j];
921 MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + dir);
922 if(nodedef->get(n2).param_type == CPT_LIGHT)
923 l = decode_light(n2.getLightBlend(data->m_daynight_ratio, nodedef));
927 video::SColor c = MapBlock_LightColor(255, l);
929 // Get the right texture
930 TileSpec ts = n.getTile(dir, tsrc, nodedef);
931 AtlasPointer ap = ts.texture;
932 material_general.setTexture(0, ap.atlas);
934 video::S3DVertex vertices[4] =
936 video::S3DVertex(-BS/2,-BS/2,BS/2, 0,0,0, c,
938 video::S3DVertex(BS/2,-BS/2,BS/2, 0,0,0, c,
940 video::S3DVertex(BS/2,BS/2,BS/2, 0,0,0, c,
942 video::S3DVertex(-BS/2,BS/2,BS/2, 0,0,0, c,
948 for(u16 i=0; i<4; i++)
949 vertices[i].Pos.rotateXZBy(0);
953 for(u16 i=0; i<4; i++)
954 vertices[i].Pos.rotateXZBy(180);
958 for(u16 i=0; i<4; i++)
959 vertices[i].Pos.rotateXZBy(-90);
963 for(u16 i=0; i<4; i++)
964 vertices[i].Pos.rotateXZBy(90);
968 for(u16 i=0; i<4; i++)
970 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
973 u16 indices[] = {0,1,2,2,3,0};
974 // Add to mesh collector
975 collector.append(material_general, vertices, 4, indices, 6);
979 else if(n.getContent() == CONTENT_PAPYRUS)
981 u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio, nodedef)));
982 video::SColor c = MapBlock_LightColor(255, l);
984 for(u32 j=0; j<4; j++)
986 video::S3DVertex vertices[4] =
988 video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c,
989 pa_papyrus.x0(), pa_papyrus.y1()),
990 video::S3DVertex(BS/2,-BS/2,0, 0,0,0, c,
991 pa_papyrus.x1(), pa_papyrus.y1()),
992 video::S3DVertex(BS/2,BS/2,0, 0,0,0, c,
993 pa_papyrus.x1(), pa_papyrus.y0()),
994 video::S3DVertex(-BS/2,BS/2,0, 0,0,0, c,
995 pa_papyrus.x0(), pa_papyrus.y0()),
1000 for(u16 i=0; i<4; i++)
1001 vertices[i].Pos.rotateXZBy(45);
1005 for(u16 i=0; i<4; i++)
1006 vertices[i].Pos.rotateXZBy(-45);
1010 for(u16 i=0; i<4; i++)
1011 vertices[i].Pos.rotateXZBy(135);
1015 for(u16 i=0; i<4; i++)
1016 vertices[i].Pos.rotateXZBy(-135);
1019 for(u16 i=0; i<4; i++)
1021 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
1024 u16 indices[] = {0,1,2,2,3,0};
1025 // Add to mesh collector
1026 collector.append(material_papyrus, vertices, 4, indices, 6);
1029 else if(n.getContent() == CONTENT_JUNGLEGRASS)
1031 u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio, nodedef)));
1032 video::SColor c = MapBlock_LightColor(255, l);
1034 for(u32 j=0; j<4; j++)
1036 video::S3DVertex vertices[4] =
1038 video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c,
1039 pa_junglegrass.x0(), pa_junglegrass.y1()),
1040 video::S3DVertex(BS/2,-BS/2,0, 0,0,0, c,
1041 pa_junglegrass.x1(), pa_junglegrass.y1()),
1042 video::S3DVertex(BS/2,BS/1,0, 0,0,0, c,
1043 pa_junglegrass.x1(), pa_junglegrass.y0()),
1044 video::S3DVertex(-BS/2,BS/1,0, 0,0,0, c,
1045 pa_junglegrass.x0(), pa_junglegrass.y0()),
1050 for(u16 i=0; i<4; i++)
1051 vertices[i].Pos.rotateXZBy(45);
1055 for(u16 i=0; i<4; i++)
1056 vertices[i].Pos.rotateXZBy(-45);
1060 for(u16 i=0; i<4; i++)
1061 vertices[i].Pos.rotateXZBy(135);
1065 for(u16 i=0; i<4; i++)
1066 vertices[i].Pos.rotateXZBy(-135);
1069 for(u16 i=0; i<4; i++)
1071 vertices[i].Pos *= 1.3;
1072 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
1075 u16 indices[] = {0,1,2,2,3,0};
1076 // Add to mesh collector
1077 collector.append(material_junglegrass, vertices, 4, indices, 6);
1080 else if(n.getContent() == CONTENT_RAIL)
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 int adjacencies = is_rail_x[0] + is_rail_x[1] + is_rail_z[0] + is_rail_z[1];
1102 const char *texturename = "rail.png";
1104 texturename = "rail.png";
1105 else if(adjacencies == 2)
1107 if((is_rail_x[0] && is_rail_x[1]) || (is_rail_z[0] && is_rail_z[1]))
1108 texturename = "rail.png";
1110 texturename = "rail_curved.png";
1112 else if(adjacencies == 3)
1113 texturename = "rail_t_junction.png";
1114 else if(adjacencies == 4)
1115 texturename = "rail_crossing.png";
1117 AtlasPointer ap = tsrc->getTexture(texturename);
1119 video::SMaterial material_rail;
1120 material_rail.setFlag(video::EMF_LIGHTING, false);
1121 material_rail.setFlag(video::EMF_BACK_FACE_CULLING, false);
1122 material_rail.setFlag(video::EMF_BILINEAR_FILTER, false);
1123 material_rail.setFlag(video::EMF_FOG_ENABLE, true);
1124 material_rail.MaterialType
1125 = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
1126 material_rail.setTexture(0, ap.atlas);
1128 u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio, nodedef));
1129 video::SColor c = MapBlock_LightColor(255, l);
1131 float d = (float)BS/16;
1132 video::S3DVertex vertices[4] =
1134 video::S3DVertex(-BS/2,-BS/2+d,-BS/2, 0,0,0, c,
1136 video::S3DVertex(BS/2,-BS/2+d,-BS/2, 0,0,0, c,
1138 video::S3DVertex(BS/2,-BS/2+d,BS/2, 0,0,0, c,
1140 video::S3DVertex(-BS/2,-BS/2+d,BS/2, 0,0,0, c,
1147 if(adjacencies == 1)
1149 if(is_rail_x[0] || is_rail_x[1])
1152 else if(adjacencies == 2)
1154 if(is_rail_x[0] && is_rail_x[1])
1156 else if(is_rail_x[0] && is_rail_z[0])
1158 else if(is_rail_x[0] && is_rail_z[1])
1160 else if(is_rail_x[1] && is_rail_z[1])
1163 else if(adjacencies == 3)
1176 for(u16 i=0; i<4; i++)
1177 vertices[i].Pos.rotateXZBy(angle);
1180 for(s32 i=0; i<4; i++)
1182 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
1185 u16 indices[] = {0,1,2,2,3,0};
1186 collector.append(material_rail, vertices, 4, indices, 6);
1188 else if (n.getContent() == CONTENT_LADDER) {
1189 AtlasPointer ap = tsrc->getTexture("ladder.png");
1192 video::SMaterial material_ladder;
1193 material_ladder.setFlag(video::EMF_LIGHTING, false);
1194 material_ladder.setFlag(video::EMF_BACK_FACE_CULLING, false);
1195 material_ladder.setFlag(video::EMF_BILINEAR_FILTER, false);
1196 material_ladder.setFlag(video::EMF_FOG_ENABLE, true);
1197 material_ladder.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
1198 material_ladder.setTexture(0, ap.atlas);
1200 u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio, nodedef));
1201 video::SColor c(255,l,l,l);
1203 float d = (float)BS/16;
1205 // Assume wall is at X+
1206 video::S3DVertex vertices[4] =
1208 video::S3DVertex(BS/2-d,-BS/2,-BS/2, 0,0,0, c,
1210 video::S3DVertex(BS/2-d,-BS/2,BS/2, 0,0,0, c,
1212 video::S3DVertex(BS/2-d,BS/2,BS/2, 0,0,0, c,
1214 video::S3DVertex(BS/2-d,BS/2,-BS/2, 0,0,0, c,
1218 v3s16 dir = unpackDir(n.param2);
1220 for(s32 i=0; i<4; i++)
1222 if(dir == v3s16(1,0,0))
1223 vertices[i].Pos.rotateXZBy(0);
1224 if(dir == v3s16(-1,0,0))
1225 vertices[i].Pos.rotateXZBy(180);
1226 if(dir == v3s16(0,0,1))
1227 vertices[i].Pos.rotateXZBy(90);
1228 if(dir == v3s16(0,0,-1))
1229 vertices[i].Pos.rotateXZBy(-90);
1230 if(dir == v3s16(0,-1,0))
1231 vertices[i].Pos.rotateXYBy(-90);
1232 if(dir == v3s16(0,1,0))
1233 vertices[i].Pos.rotateXYBy(90);
1235 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
1238 u16 indices[] = {0,1,2,2,3,0};
1239 // Add to mesh collector
1240 collector.append(material_ladder, vertices, 4, indices, 6);
1242 else if(n.getContent() == CONTENT_APPLE)
1244 u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio, nodedef)));
1245 video::SColor c = MapBlock_LightColor(255, l);
1247 for(u32 j=0; j<4; j++)
1249 video::S3DVertex vertices[4] =
1251 video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c,
1252 pa_apple.x0(), pa_apple.y1()),
1253 video::S3DVertex(BS/2,-BS/2,0, 0,0,0, c,
1254 pa_apple.x1(), pa_apple.y1()),
1255 video::S3DVertex(BS/2,BS/2,0, 0,0,0, c,
1256 pa_apple.x1(), pa_apple.y0()),
1257 video::S3DVertex(-BS/2,BS/2,0, 0,0,0, c,
1258 pa_apple.x0(), pa_apple.y0()),
1263 for(u16 i=0; i<4; i++)
1264 vertices[i].Pos.rotateXZBy(45);
1268 for(u16 i=0; i<4; i++)
1269 vertices[i].Pos.rotateXZBy(-45);
1273 for(u16 i=0; i<4; i++)
1274 vertices[i].Pos.rotateXZBy(135);
1278 for(u16 i=0; i<4; i++)
1279 vertices[i].Pos.rotateXZBy(-135);
1282 for(u16 i=0; i<4; i++)
1284 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
1287 u16 indices[] = {0,1,2,2,3,0};
1288 // Add to mesh collector
1289 collector.append(material_apple, vertices, 4, indices, 6);
1292 else if(n.getContent() == CONTENT_SAPLING) {
1293 u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio, nodedef)));
1294 video::SColor c = MapBlock_LightColor(255, l);
1296 for(u32 j=0; j<4; j++)
1298 video::S3DVertex vertices[4] =
1300 video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c,
1301 pa_sapling.x0(), pa_sapling.y1()),
1302 video::S3DVertex(BS/2,-BS/2,0, 0,0,0, c,
1303 pa_sapling.x1(), pa_sapling.y1()),
1304 video::S3DVertex(BS/2,BS/1,0, 0,0,0, c,
1305 pa_sapling.x1(), pa_sapling.y0()),
1306 video::S3DVertex(-BS/2,BS/1,0, 0,0,0, c,
1307 pa_sapling.x0(), pa_sapling.y0()),
1312 for(u16 i=0; i<4; i++)
1313 vertices[i].Pos.rotateXZBy(45);
1317 for(u16 i=0; i<4; i++)
1318 vertices[i].Pos.rotateXZBy(-45);
1322 for(u16 i=0; i<4; i++)
1323 vertices[i].Pos.rotateXZBy(135);
1327 for(u16 i=0; i<4; i++)
1328 vertices[i].Pos.rotateXZBy(-135);
1331 for(u16 i=0; i<4; i++)
1333 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
1336 u16 indices[] = {0,1,2,2,3,0};
1337 // Add to mesh collector
1338 collector.append(material_sapling, vertices, 4, indices, 6);