3 Copyright (C) 2010-2013 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 Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser 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 "util/numeric.h"
22 #include "util/directiontables.h"
23 #include "main.h" // For g_settings
24 #include "mapblock_mesh.h" // For MapBlock_LightColor() and MeshCollector
29 #include <IMeshManipulator.h>
35 // collector - the MeshCollector for the resulting polygons
36 // box - the position and size of the box
37 // tiles - the tiles (materials) to use (for all 6 faces)
38 // tilecount - number of entries in tiles, 1<=tilecount<=6
39 // c - vertex colour - used for all
40 // txc - texture coordinates - this is a list of texture coordinates
41 // for the opposite corners of each face - therefore, there
42 // should be (2+2)*6=24 values in the list. Alternatively, pass
43 // NULL to use the entire texture for each face. The order of
44 // the faces in the list is up-down-right-left-back-front
45 // (compatible with ContentFeatures). If you specified 0,0,1,1
46 // for each face, that would be the same as passing NULL.
47 void makeCuboid(MeshCollector *collector, const aabb3f &box,
48 TileSpec *tiles, int tilecount,
49 video::SColor &c, const f32* txc)
51 assert(tilecount >= 1 && tilecount <= 6);
53 v3f min = box.MinEdge;
54 v3f max = box.MaxEdge;
60 static const f32 txc_default[24] = {
71 video::S3DVertex vertices[24] =
74 video::S3DVertex(min.X,max.Y,max.Z, 0,1,0, c, txc[0],txc[1]),
75 video::S3DVertex(max.X,max.Y,max.Z, 0,1,0, c, txc[2],txc[1]),
76 video::S3DVertex(max.X,max.Y,min.Z, 0,1,0, c, txc[2],txc[3]),
77 video::S3DVertex(min.X,max.Y,min.Z, 0,1,0, c, txc[0],txc[3]),
79 video::S3DVertex(min.X,min.Y,min.Z, 0,-1,0, c, txc[4],txc[5]),
80 video::S3DVertex(max.X,min.Y,min.Z, 0,-1,0, c, txc[6],txc[5]),
81 video::S3DVertex(max.X,min.Y,max.Z, 0,-1,0, c, txc[6],txc[7]),
82 video::S3DVertex(min.X,min.Y,max.Z, 0,-1,0, c, txc[4],txc[7]),
84 video::S3DVertex(max.X,max.Y,min.Z, 1,0,0, c, txc[ 8],txc[9]),
85 video::S3DVertex(max.X,max.Y,max.Z, 1,0,0, c, txc[10],txc[9]),
86 video::S3DVertex(max.X,min.Y,max.Z, 1,0,0, c, txc[10],txc[11]),
87 video::S3DVertex(max.X,min.Y,min.Z, 1,0,0, c, txc[ 8],txc[11]),
89 video::S3DVertex(min.X,max.Y,max.Z, -1,0,0, c, txc[12],txc[13]),
90 video::S3DVertex(min.X,max.Y,min.Z, -1,0,0, c, txc[14],txc[13]),
91 video::S3DVertex(min.X,min.Y,min.Z, -1,0,0, c, txc[14],txc[15]),
92 video::S3DVertex(min.X,min.Y,max.Z, -1,0,0, c, txc[12],txc[15]),
94 video::S3DVertex(max.X,max.Y,max.Z, 0,0,1, c, txc[16],txc[17]),
95 video::S3DVertex(min.X,max.Y,max.Z, 0,0,1, c, txc[18],txc[17]),
96 video::S3DVertex(min.X,min.Y,max.Z, 0,0,1, c, txc[18],txc[19]),
97 video::S3DVertex(max.X,min.Y,max.Z, 0,0,1, c, txc[16],txc[19]),
99 video::S3DVertex(min.X,max.Y,min.Z, 0,0,-1, c, txc[20],txc[21]),
100 video::S3DVertex(max.X,max.Y,min.Z, 0,0,-1, c, txc[22],txc[21]),
101 video::S3DVertex(max.X,min.Y,min.Z, 0,0,-1, c, txc[22],txc[23]),
102 video::S3DVertex(min.X,min.Y,min.Z, 0,0,-1, c, txc[20],txc[23]),
105 for(int i = 0; i < 6; i++)
107 switch (tiles[MYMIN(i, tilecount-1)].rotation)
112 for (int x = 0; x < 4; x++)
113 vertices[i*4+x].TCoords.rotateBy(90,irr::core::vector2df(0, 0));
116 for (int x = 0; x < 4; x++)
117 vertices[i*4+x].TCoords.rotateBy(180,irr::core::vector2df(0, 0));
120 for (int x = 0; x < 4; x++)
121 vertices[i*4+x].TCoords.rotateBy(270,irr::core::vector2df(0, 0));
124 for (int x = 0; x < 4; x++){
125 vertices[i*4+x].TCoords.X = 1.0 - vertices[i*4+x].TCoords.X;
126 vertices[i*4+x].TCoords.rotateBy(90,irr::core::vector2df(0, 0));
130 for (int x = 0; x < 4; x++){
131 vertices[i*4+x].TCoords.X = 1.0 - vertices[i*4+x].TCoords.X;
132 vertices[i*4+x].TCoords.rotateBy(270,irr::core::vector2df(0, 0));
136 for (int x = 0; x < 4; x++){
137 vertices[i*4+x].TCoords.Y = 1.0 - vertices[i*4+x].TCoords.Y;
138 vertices[i*4+x].TCoords.rotateBy(90,irr::core::vector2df(0, 0));
142 for (int x = 0; x < 4; x++){
143 vertices[i*4+x].TCoords.Y = 1.0 - vertices[i*4+x].TCoords.Y;
144 vertices[i*4+x].TCoords.rotateBy(270,irr::core::vector2df(0, 0));
148 for (int x = 0; x < 4; x++){
149 vertices[i*4+x].TCoords.X = 1.0 - vertices[i*4+x].TCoords.X;
153 for (int x = 0; x < 4; x++){
154 vertices[i*4+x].TCoords.Y = 1.0 - vertices[i*4+x].TCoords.Y;
161 u16 indices[] = {0,1,2,2,3,0};
162 // Add to mesh collector
163 for(s32 j=0; j<24; j+=4)
165 int tileindex = MYMIN(j/4, tilecount-1);
166 collector->append(tiles[tileindex],
167 vertices+j, 4, indices, 6);
171 void mapblock_mesh_generate_special(MeshMakeData *data,
172 MeshCollector &collector)
174 INodeDefManager *nodedef = data->m_gamedef->ndef();
175 ITextureSource *tsrc = data->m_gamedef->tsrc();
176 scene::ISceneManager* smgr = data->m_gamedef->getSceneManager();
177 scene::IMeshManipulator* meshmanip = smgr->getMeshManipulator();
180 //TimeTaker timer("mapblock_mesh_generate_special()");
185 bool enable_mesh_cache = g_settings->getBool("enable_mesh_cache");
186 bool new_style_water = g_settings->getBool("new_style_water");
188 float node_liquid_level = 1.0;
190 node_liquid_level = 0.85;
192 v3s16 blockpos_nodes = data->m_blockpos*MAP_BLOCKSIZE;
194 // Create selection mesh
195 v3s16 p = data->m_highlighted_pos_relative;
196 if (data->m_show_hud &&
197 (p.X >= 0) && (p.X < MAP_BLOCKSIZE) &&
198 (p.Y >= 0) && (p.Y < MAP_BLOCKSIZE) &&
199 (p.Z >= 0) && (p.Z < MAP_BLOCKSIZE)) {
201 MapNode n = data->m_vmanip.getNodeNoEx(blockpos_nodes + p);
202 if(n.getContent() != CONTENT_AIR) {
203 // Get selection mesh light level
204 static const v3s16 dirs[7] = {
216 for (u8 i = 0; i < 7; i++) {
217 MapNode n1 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p + dirs[i]);
218 l1 = getInteriorLight(n1, -4, nodedef);
222 video::SColor c = MapBlock_LightColor(255, l, 0);
223 data->m_highlight_mesh_color = c;
224 std::vector<aabb3f> boxes = n.getSelectionBoxes(nodedef);
226 h_tile.material_flags |= MATERIAL_FLAG_HIGHLIGHTED;
227 h_tile.texture = tsrc->getTexture("halo.png",&h_tile.texture_id);
228 v3f pos = intToFloat(p, BS);
230 for(std::vector<aabb3f>::iterator
232 i != boxes.end(); i++)
235 box.MinEdge += v3f(-d, -d, -d) + pos;
236 box.MaxEdge += v3f(d, d, d) + pos;
237 makeCuboid(&collector, box, &h_tile, 1, c, NULL);
242 for(s16 z = 0; z < MAP_BLOCKSIZE; z++)
243 for(s16 y = 0; y < MAP_BLOCKSIZE; y++)
244 for(s16 x = 0; x < MAP_BLOCKSIZE; x++)
248 MapNode n = data->m_vmanip.getNodeNoEx(blockpos_nodes + p);
249 const ContentFeatures &f = nodedef->get(n);
251 // Only solidness=0 stuff is drawn here
257 infostream<<"Got "<<f.drawtype<<std::endl;
265 Add water sources to mesh if using new style
267 TileSpec tile_liquid = f.special_tiles[0];
268 TileSpec tile_liquid_bfculled = getNodeTile(n, p, v3s16(0,0,0), data);
270 bool top_is_same_liquid = false;
271 MapNode ntop = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y+1,z));
272 content_t c_flowing = nodedef->getId(f.liquid_alternative_flowing);
273 content_t c_source = nodedef->getId(f.liquid_alternative_source);
274 if(ntop.getContent() == c_flowing || ntop.getContent() == c_source)
275 top_is_same_liquid = true;
277 u16 l = getInteriorLight(n, 0, nodedef);
278 video::SColor c = MapBlock_LightColor(f.alpha, l, f.light_source);
283 v3s16 side_dirs[4] = {
289 for(u32 i=0; i<4; i++)
291 v3s16 dir = side_dirs[i];
293 MapNode neighbor = data->m_vmanip.getNodeNoEx(blockpos_nodes + p + dir);
294 content_t neighbor_content = neighbor.getContent();
295 const ContentFeatures &n_feat = nodedef->get(neighbor_content);
296 MapNode n_top = data->m_vmanip.getNodeNoEx(blockpos_nodes + p + dir+ v3s16(0,1,0));
297 content_t n_top_c = n_top.getContent();
299 if(neighbor_content == CONTENT_IGNORE)
303 If our topside is liquid and neighbor's topside
304 is liquid, don't draw side face
306 if(top_is_same_liquid && (n_top_c == c_flowing ||
307 n_top_c == c_source || n_top_c == CONTENT_IGNORE))
310 // Don't draw face if neighbor is blocking the view
311 if(n_feat.solidness == 2)
314 bool neighbor_is_same_liquid = (neighbor_content == c_source
315 || neighbor_content == c_flowing);
317 // Don't draw any faces if neighbor same is liquid and top is
319 if(neighbor_is_same_liquid && !top_is_same_liquid)
322 // Use backface culled material if neighbor doesn't have a
324 const TileSpec *current_tile = &tile_liquid;
325 if(n_feat.solidness != 0 || n_feat.visual_solidness != 0)
326 current_tile = &tile_liquid_bfculled;
328 video::S3DVertex vertices[4] =
330 video::S3DVertex(-BS/2,0,BS/2,0,0,0, c, 0,1),
331 video::S3DVertex(BS/2,0,BS/2,0,0,0, c, 1,1),
332 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c, 1,0),
333 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c, 0,0),
337 If our topside is liquid, set upper border of face
338 at upper border of node
340 if(top_is_same_liquid)
342 vertices[2].Pos.Y = 0.5*BS;
343 vertices[3].Pos.Y = 0.5*BS;
346 Otherwise upper position of face is liquid level
350 vertices[2].Pos.Y = (node_liquid_level-0.5)*BS;
351 vertices[3].Pos.Y = (node_liquid_level-0.5)*BS;
354 If neighbor is liquid, lower border of face is liquid level
356 if(neighbor_is_same_liquid)
358 vertices[0].Pos.Y = (node_liquid_level-0.5)*BS;
359 vertices[1].Pos.Y = (node_liquid_level-0.5)*BS;
362 If neighbor is not liquid, lower border of face is
367 vertices[0].Pos.Y = -0.5*BS;
368 vertices[1].Pos.Y = -0.5*BS;
371 for(s32 j=0; j<4; j++)
373 if(dir == v3s16(0,0,1))
374 vertices[j].Pos.rotateXZBy(0);
375 if(dir == v3s16(0,0,-1))
376 vertices[j].Pos.rotateXZBy(180);
377 if(dir == v3s16(-1,0,0))
378 vertices[j].Pos.rotateXZBy(90);
379 if(dir == v3s16(1,0,-0))
380 vertices[j].Pos.rotateXZBy(-90);
382 // Do this to not cause glitches when two liquids are
384 /*if(neighbor_is_same_liquid == false){
385 vertices[j].Pos.X *= 0.98;
386 vertices[j].Pos.Z *= 0.98;
389 vertices[j].Pos += intToFloat(p, BS);
392 u16 indices[] = {0,1,2,2,3,0};
393 // Add to mesh collector
394 collector.append(*current_tile, vertices, 4, indices, 6);
400 if(top_is_same_liquid)
403 video::S3DVertex vertices[4] =
405 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c, 0,1),
406 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c, 1,1),
407 video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c, 1,0),
408 video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c, 0,0),
411 v3f offset(p.X*BS, p.Y*BS + (-0.5+node_liquid_level)*BS, p.Z*BS);
412 for(s32 i=0; i<4; i++)
414 vertices[i].Pos += offset;
417 u16 indices[] = {0,1,2,2,3,0};
418 // Add to mesh collector
419 collector.append(tile_liquid, vertices, 4, indices, 6);
421 case NDT_FLOWINGLIQUID:
424 Add flowing liquid to mesh
426 TileSpec tile_liquid = f.special_tiles[0];
427 TileSpec tile_liquid_bfculled = f.special_tiles[1];
429 bool top_is_same_liquid = false;
430 MapNode ntop = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y+1,z));
431 content_t c_flowing = nodedef->getId(f.liquid_alternative_flowing);
432 content_t c_source = nodedef->getId(f.liquid_alternative_source);
433 if(ntop.getContent() == c_flowing || ntop.getContent() == c_source)
434 top_is_same_liquid = true;
437 // If this liquid emits light and doesn't contain light, draw
438 // it at what it emits, for an increased effect
439 u8 light_source = nodedef->get(n).light_source;
440 if(light_source != 0){
441 l = decode_light(light_source);
444 // Use the light of the node on top if possible
445 else if(nodedef->get(ntop).param_type == CPT_LIGHT)
446 l = getInteriorLight(ntop, 0, nodedef);
447 // Otherwise use the light of this node (the liquid)
449 l = getInteriorLight(n, 0, nodedef);
450 video::SColor c = MapBlock_LightColor(f.alpha, l, f.light_source);
452 u8 range = rangelim(nodedef->get(c_flowing).liquid_range, 1, 8);
454 // Neighbor liquid levels (key = relative position)
455 // Includes current node
456 std::map<v3s16, f32> neighbor_levels;
457 std::map<v3s16, content_t> neighbor_contents;
458 std::map<v3s16, u8> neighbor_flags;
459 const u8 neighborflag_top_is_same_liquid = 0x01;
460 v3s16 neighbor_dirs[9] = {
471 for(u32 i=0; i<9; i++)
473 content_t content = CONTENT_AIR;
474 float level = -0.5 * BS;
477 v3s16 p2 = p + neighbor_dirs[i];
478 MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
479 if(n2.getContent() != CONTENT_IGNORE)
481 content = n2.getContent();
483 if(n2.getContent() == c_source)
484 level = (-0.5+node_liquid_level) * BS;
485 else if(n2.getContent() == c_flowing){
486 u8 liquid_level = (n2.param2&LIQUID_LEVEL_MASK);
487 if (liquid_level <= LIQUID_LEVEL_MAX+1-range)
490 liquid_level -= (LIQUID_LEVEL_MAX+1-range);
491 level = (-0.5 + ((float)liquid_level+ 0.5) / (float)range * node_liquid_level) * BS;
494 // Check node above neighbor.
495 // NOTE: This doesn't get executed if neighbor
498 n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
499 if(n2.getContent() == c_source ||
500 n2.getContent() == c_flowing)
501 flags |= neighborflag_top_is_same_liquid;
504 neighbor_levels[neighbor_dirs[i]] = level;
505 neighbor_contents[neighbor_dirs[i]] = content;
506 neighbor_flags[neighbor_dirs[i]] = flags;
509 // Corner heights (average between four liquids)
510 f32 corner_levels[4];
512 v3s16 halfdirs[4] = {
518 for(u32 i=0; i<4; i++)
520 v3s16 cornerdir = halfdirs[i];
521 float cornerlevel = 0;
524 for(u32 j=0; j<4; j++)
526 v3s16 neighbordir = cornerdir - halfdirs[j];
527 content_t content = neighbor_contents[neighbordir];
528 // If top is liquid, draw starting from top of node
529 if(neighbor_flags[neighbordir] &
530 neighborflag_top_is_same_liquid)
532 cornerlevel = 0.5*BS;
536 // Source is always the same height
537 else if(content == c_source)
539 cornerlevel = (-0.5+node_liquid_level)*BS;
543 // Flowing liquid has level information
544 else if(content == c_flowing)
546 cornerlevel += neighbor_levels[neighbordir];
549 else if(content == CONTENT_AIR)
555 cornerlevel = -0.5*BS+0.2;
556 else if(valid_count > 0)
557 cornerlevel /= valid_count;
558 corner_levels[i] = cornerlevel;
565 v3s16 side_dirs[4] = {
571 s16 side_corners[4][2] = {
577 for(u32 i=0; i<4; i++)
579 v3s16 dir = side_dirs[i];
582 If our topside is liquid and neighbor's topside
583 is liquid, don't draw side face
585 if(top_is_same_liquid &&
586 neighbor_flags[dir] & neighborflag_top_is_same_liquid)
589 content_t neighbor_content = neighbor_contents[dir];
590 const ContentFeatures &n_feat = nodedef->get(neighbor_content);
592 // Don't draw face if neighbor is blocking the view
593 if(n_feat.solidness == 2)
596 bool neighbor_is_same_liquid = (neighbor_content == c_source
597 || neighbor_content == c_flowing);
599 // Don't draw any faces if neighbor same is liquid and top is
601 if(neighbor_is_same_liquid == true
602 && top_is_same_liquid == false)
605 // Use backface culled material if neighbor doesn't have a
607 const TileSpec *current_tile = &tile_liquid;
608 if(n_feat.solidness != 0 || n_feat.visual_solidness != 0)
609 current_tile = &tile_liquid_bfculled;
611 video::S3DVertex vertices[4] =
613 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c, 0,1),
614 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c, 1,1),
615 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c, 1,0),
616 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c, 0,0),
620 If our topside is liquid, set upper border of face
621 at upper border of node
623 if(top_is_same_liquid)
625 vertices[2].Pos.Y = 0.5*BS;
626 vertices[3].Pos.Y = 0.5*BS;
629 Otherwise upper position of face is corner levels
633 vertices[2].Pos.Y = corner_levels[side_corners[i][0]];
634 vertices[3].Pos.Y = corner_levels[side_corners[i][1]];
638 If neighbor is liquid, lower border of face is corner
641 if(neighbor_is_same_liquid)
643 vertices[0].Pos.Y = corner_levels[side_corners[i][1]];
644 vertices[1].Pos.Y = corner_levels[side_corners[i][0]];
647 If neighbor is not liquid, lower border of face is
652 vertices[0].Pos.Y = -0.5*BS;
653 vertices[1].Pos.Y = -0.5*BS;
656 for(s32 j=0; j<4; j++)
658 if(dir == v3s16(0,0,1))
659 vertices[j].Pos.rotateXZBy(0);
660 if(dir == v3s16(0,0,-1))
661 vertices[j].Pos.rotateXZBy(180);
662 if(dir == v3s16(-1,0,0))
663 vertices[j].Pos.rotateXZBy(90);
664 if(dir == v3s16(1,0,-0))
665 vertices[j].Pos.rotateXZBy(-90);
667 // Do this to not cause glitches when two liquids are
669 /*if(neighbor_is_same_liquid == false){
670 vertices[j].Pos.X *= 0.98;
671 vertices[j].Pos.Z *= 0.98;
674 vertices[j].Pos += intToFloat(p, BS);
677 u16 indices[] = {0,1,2,2,3,0};
678 // Add to mesh collector
679 collector.append(*current_tile, vertices, 4, indices, 6);
683 Generate top side, if appropriate
686 if(top_is_same_liquid == false)
688 video::S3DVertex vertices[4] =
690 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c, 0,1),
691 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c, 1,1),
692 video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c, 1,0),
693 video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c, 0,0),
696 // To get backface culling right, the vertices need to go
697 // clockwise around the front of the face. And we happened to
698 // calculate corner levels in exact reverse order.
699 s32 corner_resolve[4] = {3,2,1,0};
701 for(s32 i=0; i<4; i++)
703 //vertices[i].Pos.Y += liquid_level;
704 //vertices[i].Pos.Y += neighbor_levels[v3s16(0,0,0)];
705 s32 j = corner_resolve[i];
706 vertices[i].Pos.Y += corner_levels[j];
707 vertices[i].Pos += intToFloat(p, BS);
710 // Default downwards-flowing texture animation goes from
711 // -Z towards +Z, thus the direction is +Z.
712 // Rotate texture to make animation go in flow direction
713 // Positive if liquid moves towards +Z
714 f32 dz = (corner_levels[side_corners[3][0]] +
715 corner_levels[side_corners[3][1]]) -
716 (corner_levels[side_corners[2][0]] +
717 corner_levels[side_corners[2][1]]);
718 // Positive if liquid moves towards +X
719 f32 dx = (corner_levels[side_corners[1][0]] +
720 corner_levels[side_corners[1][1]]) -
721 (corner_levels[side_corners[0][0]] +
722 corner_levels[side_corners[0][1]]);
723 f32 tcoord_angle = atan2(dz, dx) * core::RADTODEG ;
724 v2f tcoord_center(0.5, 0.5);
725 v2f tcoord_translate(
726 blockpos_nodes.Z + z,
727 blockpos_nodes.X + x);
728 tcoord_translate.rotateBy(tcoord_angle);
729 tcoord_translate.X -= floor(tcoord_translate.X);
730 tcoord_translate.Y -= floor(tcoord_translate.Y);
732 for(s32 i=0; i<4; i++)
734 vertices[i].TCoords.rotateBy(
737 vertices[i].TCoords += tcoord_translate;
740 v2f t = vertices[0].TCoords;
741 vertices[0].TCoords = vertices[2].TCoords;
742 vertices[2].TCoords = t;
744 u16 indices[] = {0,1,2,2,3,0};
745 // Add to mesh collector
746 collector.append(tile_liquid, vertices, 4, indices, 6);
751 TileSpec tile = getNodeTile(n, p, v3s16(0,0,0), data);
753 u16 l = getInteriorLight(n, 1, nodedef);
754 video::SColor c = MapBlock_LightColor(255, l, f.light_source);
756 for(u32 j=0; j<6; j++)
758 // Check this neighbor
759 v3s16 n2p = blockpos_nodes + p + g_6dirs[j];
760 MapNode n2 = data->m_vmanip.getNodeNoEx(n2p);
761 // Don't make face if neighbor is of same type
762 if(n2.getContent() == n.getContent())
766 video::S3DVertex vertices[4] = {
767 video::S3DVertex(-BS/2,-BS/2,BS/2, 0,0,0, c, 1,1),
768 video::S3DVertex(BS/2,-BS/2,BS/2, 0,0,0, c, 0,1),
769 video::S3DVertex(BS/2,BS/2,BS/2, 0,0,0, c, 0,0),
770 video::S3DVertex(-BS/2,BS/2,BS/2, 0,0,0, c, 1,0),
773 // Rotations in the g_6dirs format
775 for(u16 i=0; i<4; i++)
776 vertices[i].Pos.rotateXZBy(0);
777 else if(j == 1) // Y+
778 for(u16 i=0; i<4; i++)
779 vertices[i].Pos.rotateYZBy(-90);
780 else if(j == 2) // X+
781 for(u16 i=0; i<4; i++)
782 vertices[i].Pos.rotateXZBy(-90);
783 else if(j == 3) // Z-
784 for(u16 i=0; i<4; i++)
785 vertices[i].Pos.rotateXZBy(180);
786 else if(j == 4) // Y-
787 for(u16 i=0; i<4; i++)
788 vertices[i].Pos.rotateYZBy(90);
789 else if(j == 5) // X-
790 for(u16 i=0; i<4; i++)
791 vertices[i].Pos.rotateXZBy(90);
793 for(u16 i=0; i<4; i++){
794 vertices[i].Pos += intToFloat(p, BS);
797 u16 indices[] = {0,1,2,2,3,0};
798 // Add to mesh collector
799 collector.append(tile, vertices, 4, indices, 6);
802 case NDT_GLASSLIKE_FRAMED_OPTIONAL:
803 // This is always pre-converted to something else
806 case NDT_GLASSLIKE_FRAMED:
808 static const v3s16 dirs[6] = {
819 for (i = 0; i < 6; i++)
820 tiles[i] = getNodeTile(n, p, dirs[i], data);
822 TileSpec glass_tiles[6];
823 if (tiles[1].texture && tiles[2].texture && tiles[3].texture) {
824 glass_tiles[0] = tiles[2];
825 glass_tiles[1] = tiles[3];
826 glass_tiles[2] = tiles[1];
827 glass_tiles[3] = tiles[1];
828 glass_tiles[4] = tiles[1];
829 glass_tiles[5] = tiles[1];
831 for (i = 0; i < 6; i++)
832 glass_tiles[i] = tiles[1];
835 u8 param2 = n.getParam2();
836 bool H_merge = ! bool(param2 & 128);
837 bool V_merge = ! bool(param2 & 64);
838 param2 = param2 & 63;
840 u16 l = getInteriorLight(n, 1, nodedef);
841 video::SColor c = MapBlock_LightColor(255, l, f.light_source);
842 v3f pos = intToFloat(p, BS);
843 static const float a = BS / 2;
844 static const float g = a - 0.003;
845 static const float b = .876 * ( BS / 2 );
847 static const aabb3f frame_edges[12] = {
848 aabb3f( b, b,-a, a, a, a), // y+
849 aabb3f(-a, b,-a,-b, a, a), // y+
850 aabb3f( b,-a,-a, a,-b, a), // y-
851 aabb3f(-a,-a,-a,-b,-b, a), // y-
852 aabb3f( b,-a, b, a, a, a), // x+
853 aabb3f( b,-a,-a, a, a,-b), // x+
854 aabb3f(-a,-a, b,-b, a, a), // x-
855 aabb3f(-a,-a,-a,-b, a,-b), // x-
856 aabb3f(-a, b, b, a, a, a), // z+
857 aabb3f(-a,-a, b, a,-b, a), // z+
858 aabb3f(-a,-a,-a, a,-b,-b), // z-
859 aabb3f(-a, b,-a, a, a,-b) // z-
861 static const aabb3f glass_faces[6] = {
862 aabb3f(-g, g,-g, g, g, g), // y+
863 aabb3f(-g,-g,-g, g,-g, g), // y-
864 aabb3f( g,-g,-g, g, g, g), // x+
865 aabb3f(-g,-g,-g,-g, g, g), // x-
866 aabb3f(-g,-g, g, g, g, g), // z+
867 aabb3f(-g,-g,-g, g, g,-g) // z-
870 // table of node visible faces, 0 = invisible
871 int visible_faces[6] = {0,0,0,0,0,0};
873 // table of neighbours, 1 = same type, checked with g_26dirs
874 int nb[18] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
876 // g_26dirs to check when only horizontal merge is allowed
877 int nb_H_dirs[8] = {0,2,3,5,10,11,12,13};
879 content_t current = n.getContent();
884 // neighbours checks for frames visibility
886 if (!H_merge && V_merge) {
887 n2p = blockpos_nodes + p + g_26dirs[1];
888 n2 = data->m_vmanip.getNodeNoEx(n2p);
889 n2c = n2.getContent();
890 if (n2c == current || n2c == CONTENT_IGNORE)
892 n2p = blockpos_nodes + p + g_26dirs[4];
893 n2 = data->m_vmanip.getNodeNoEx(n2p);
894 n2c = n2.getContent();
895 if (n2c == current || n2c == CONTENT_IGNORE)
897 } else if (H_merge && !V_merge) {
898 for(i = 0; i < 8; i++) {
899 n2p = blockpos_nodes + p + g_26dirs[nb_H_dirs[i]];
900 n2 = data->m_vmanip.getNodeNoEx(n2p);
901 n2c = n2.getContent();
902 if (n2c == current || n2c == CONTENT_IGNORE)
903 nb[nb_H_dirs[i]] = 1;
905 } else if (H_merge && V_merge) {
906 for(i = 0; i < 18; i++) {
907 n2p = blockpos_nodes + p + g_26dirs[i];
908 n2 = data->m_vmanip.getNodeNoEx(n2p);
909 n2c = n2.getContent();
910 if (n2c == current || n2c == CONTENT_IGNORE)
915 // faces visibility checks
918 visible_faces[0] = 1;
919 visible_faces[1] = 1;
921 for(i = 0; i < 2; i++) {
922 n2p = blockpos_nodes + p + dirs[i];
923 n2 = data->m_vmanip.getNodeNoEx(n2p);
924 n2c = n2.getContent();
926 visible_faces[i] = 1;
931 visible_faces[2] = 1;
932 visible_faces[3] = 1;
933 visible_faces[4] = 1;
934 visible_faces[5] = 1;
936 for(i = 2; i < 6; i++) {
937 n2p = blockpos_nodes + p + dirs[i];
938 n2 = data->m_vmanip.getNodeNoEx(n2p);
939 n2c = n2.getContent();
941 visible_faces[i] = 1;
945 static const u8 nb_triplet[12*3] = {
946 1,2, 7, 1,5, 6, 4,2,15, 4,5,14,
947 2,0,11, 2,3,13, 5,0,10, 5,3,12,
948 0,1, 8, 0,4,16, 3,4,17, 3,1, 9
951 f32 tx1, ty1, tz1, tx2, ty2, tz2;
954 for(i = 0; i < 12; i++)
957 if (nb[nb_triplet[i*3+2]])
958 edge_invisible = nb[nb_triplet[i*3]] & nb[nb_triplet[i*3+1]];
960 edge_invisible = nb[nb_triplet[i*3]] ^ nb[nb_triplet[i*3+1]];
963 box = frame_edges[i];
966 tx1 = (box.MinEdge.X / BS) + 0.5;
967 ty1 = (box.MinEdge.Y / BS) + 0.5;
968 tz1 = (box.MinEdge.Z / BS) + 0.5;
969 tx2 = (box.MaxEdge.X / BS) + 0.5;
970 ty2 = (box.MaxEdge.Y / BS) + 0.5;
971 tz2 = (box.MaxEdge.Z / BS) + 0.5;
973 tx1, 1-tz2, tx2, 1-tz1,
975 tz1, 1-ty2, tz2, 1-ty1,
976 1-tz2, 1-ty2, 1-tz1, 1-ty1,
977 1-tx2, 1-ty2, 1-tx1, 1-ty1,
978 tx1, 1-ty2, tx2, 1-ty1,
980 makeCuboid(&collector, box, &tiles[0], 1, c, txc1);
983 for(i = 0; i < 6; i++)
985 if (!visible_faces[i])
987 box = glass_faces[i];
990 tx1 = (box.MinEdge.X / BS) + 0.5;
991 ty1 = (box.MinEdge.Y / BS) + 0.5;
992 tz1 = (box.MinEdge.Z / BS) + 0.5;
993 tx2 = (box.MaxEdge.X / BS) + 0.5;
994 ty2 = (box.MaxEdge.Y / BS) + 0.5;
995 tz2 = (box.MaxEdge.Z / BS) + 0.5;
997 tx1, 1-tz2, tx2, 1-tz1,
999 tz1, 1-ty2, tz2, 1-ty1,
1000 1-tz2, 1-ty2, 1-tz1, 1-ty1,
1001 1-tx2, 1-ty2, 1-tx1, 1-ty1,
1002 tx1, 1-ty2, tx2, 1-ty1,
1004 makeCuboid(&collector, box, &glass_tiles[i], 1, c, txc2);
1007 if (param2 > 0 && f.special_tiles[0].texture) {
1008 // Interior volume level is in range 0 .. 63,
1009 // convert it to -0.5 .. 0.5
1010 float vlev = (((float)param2 / 63.0 ) * 2.0 - 1.0);
1011 TileSpec interior_tiles[6];
1012 for (i = 0; i < 6; i++)
1013 interior_tiles[i] = f.special_tiles[0];
1014 float offset = 0.003;
1015 box = aabb3f(visible_faces[3] ? -b : -a + offset,
1016 visible_faces[1] ? -b : -a + offset,
1017 visible_faces[5] ? -b : -a + offset,
1018 visible_faces[2] ? b : a - offset,
1019 visible_faces[0] ? b * vlev : a * vlev - offset,
1020 visible_faces[4] ? b : a - offset);
1023 tx1 = (box.MinEdge.X / BS) + 0.5;
1024 ty1 = (box.MinEdge.Y / BS) + 0.5;
1025 tz1 = (box.MinEdge.Z / BS) + 0.5;
1026 tx2 = (box.MaxEdge.X / BS) + 0.5;
1027 ty2 = (box.MaxEdge.Y / BS) + 0.5;
1028 tz2 = (box.MaxEdge.Z / BS) + 0.5;
1030 tx1, 1-tz2, tx2, 1-tz1,
1032 tz1, 1-ty2, tz2, 1-ty1,
1033 1-tz2, 1-ty2, 1-tz1, 1-ty1,
1034 1-tx2, 1-ty2, 1-tx1, 1-ty1,
1035 tx1, 1-ty2, tx2, 1-ty1,
1037 makeCuboid(&collector, box, interior_tiles, 6, c, txc3);
1042 TileSpec tile_leaves = getNodeTile(n, p,
1043 v3s16(0,0,0), data);
1045 u16 l = getInteriorLight(n, 1, nodedef);
1046 video::SColor c = MapBlock_LightColor(255, l, f.light_source);
1048 v3f pos = intToFloat(p, BS);
1049 aabb3f box(-BS/2,-BS/2,-BS/2,BS/2,BS/2,BS/2);
1052 makeCuboid(&collector, box, &tile_leaves, 1, c, NULL);
1054 case NDT_ALLFACES_OPTIONAL:
1055 // This is always pre-converted to something else
1060 v3s16 dir = n.getWallMountedDir(nodedef);
1063 if(dir == v3s16(0,-1,0)){
1064 tileindex = 0; // floor
1065 } else if(dir == v3s16(0,1,0)){
1066 tileindex = 1; // ceiling
1067 // For backwards compatibility
1068 } else if(dir == v3s16(0,0,0)){
1069 tileindex = 0; // floor
1071 tileindex = 2; // side
1074 TileSpec tile = getNodeTileN(n, p, tileindex, data);
1075 tile.material_flags &= ~MATERIAL_FLAG_BACKFACE_CULLING;
1076 tile.material_flags |= MATERIAL_FLAG_CRACK_OVERLAY;
1078 u16 l = getInteriorLight(n, 1, nodedef);
1079 video::SColor c = MapBlock_LightColor(255, l, f.light_source);
1081 float s = BS/2*f.visual_scale;
1082 // Wall at X+ of node
1083 video::S3DVertex vertices[4] =
1085 video::S3DVertex(-s,-s,0, 0,0,0, c, 0,1),
1086 video::S3DVertex( s,-s,0, 0,0,0, c, 1,1),
1087 video::S3DVertex( s, s,0, 0,0,0, c, 1,0),
1088 video::S3DVertex(-s, s,0, 0,0,0, c, 0,0),
1091 for(s32 i=0; i<4; i++)
1093 if(dir == v3s16(1,0,0))
1094 vertices[i].Pos.rotateXZBy(0);
1095 if(dir == v3s16(-1,0,0))
1096 vertices[i].Pos.rotateXZBy(180);
1097 if(dir == v3s16(0,0,1))
1098 vertices[i].Pos.rotateXZBy(90);
1099 if(dir == v3s16(0,0,-1))
1100 vertices[i].Pos.rotateXZBy(-90);
1101 if(dir == v3s16(0,-1,0))
1102 vertices[i].Pos.rotateXZBy(45);
1103 if(dir == v3s16(0,1,0))
1104 vertices[i].Pos.rotateXZBy(-45);
1106 vertices[i].Pos += intToFloat(p, BS);
1109 u16 indices[] = {0,1,2,2,3,0};
1110 // Add to mesh collector
1111 collector.append(tile, vertices, 4, indices, 6);
1115 TileSpec tile = getNodeTileN(n, p, 0, data);
1116 tile.material_flags &= ~MATERIAL_FLAG_BACKFACE_CULLING;
1117 tile.material_flags |= MATERIAL_FLAG_CRACK_OVERLAY;
1119 u16 l = getInteriorLight(n, 0, nodedef);
1120 video::SColor c = MapBlock_LightColor(255, l, f.light_source);
1122 float d = (float)BS/16;
1123 float s = BS/2*f.visual_scale;
1124 // Wall at X+ of node
1125 video::S3DVertex vertices[4] =
1127 video::S3DVertex(BS/2-d, s, s, 0,0,0, c, 0,0),
1128 video::S3DVertex(BS/2-d, s, -s, 0,0,0, c, 1,0),
1129 video::S3DVertex(BS/2-d, -s, -s, 0,0,0, c, 1,1),
1130 video::S3DVertex(BS/2-d, -s, s, 0,0,0, c, 0,1),
1133 v3s16 dir = n.getWallMountedDir(nodedef);
1135 for(s32 i=0; i<4; i++)
1137 if(dir == v3s16(1,0,0))
1138 vertices[i].Pos.rotateXZBy(0);
1139 if(dir == v3s16(-1,0,0))
1140 vertices[i].Pos.rotateXZBy(180);
1141 if(dir == v3s16(0,0,1))
1142 vertices[i].Pos.rotateXZBy(90);
1143 if(dir == v3s16(0,0,-1))
1144 vertices[i].Pos.rotateXZBy(-90);
1145 if(dir == v3s16(0,-1,0))
1146 vertices[i].Pos.rotateXYBy(-90);
1147 if(dir == v3s16(0,1,0))
1148 vertices[i].Pos.rotateXYBy(90);
1150 vertices[i].Pos += intToFloat(p, BS);
1153 u16 indices[] = {0,1,2,2,3,0};
1154 // Add to mesh collector
1155 collector.append(tile, vertices, 4, indices, 6);
1159 TileSpec tile = getNodeTileN(n, p, 0, data);
1160 tile.material_flags |= MATERIAL_FLAG_CRACK_OVERLAY;
1162 u16 l = getInteriorLight(n, 1, nodedef);
1163 video::SColor c = MapBlock_LightColor(255, l, f.light_source);
1165 float s = BS / 2 * f.visual_scale;
1167 for (int j = 0; j < 2; j++)
1169 video::S3DVertex vertices[4] =
1171 video::S3DVertex(-s,-BS/2, 0, 0,0,0, c, 0,1),
1172 video::S3DVertex( s,-BS/2, 0, 0,0,0, c, 1,1),
1173 video::S3DVertex( s,-BS/2 + s*2,0, 0,0,0, c, 1,0),
1174 video::S3DVertex(-s,-BS/2 + s*2,0, 0,0,0, c, 0,0),
1179 for(u16 i = 0; i < 4; i++)
1180 vertices[i].Pos.rotateXZBy(46 + n.param2 * 2);
1184 for(u16 i = 0; i < 4; i++)
1185 vertices[i].Pos.rotateXZBy(-44 + n.param2 * 2);
1188 for (int i = 0; i < 4; i++)
1190 vertices[i].Pos *= f.visual_scale;
1191 if (f.visual_scale < 1)
1192 vertices[i].Pos.Y -= BS/2 * (1 - f.visual_scale);
1193 vertices[i].Pos += intToFloat(p, BS);
1196 u16 indices[] = {0, 1, 2, 2, 3, 0};
1197 // Add to mesh collector
1198 collector.append(tile, vertices, 4, indices, 6);
1203 TileSpec tile = getNodeTileN(n, p, 0, data);
1204 tile.material_flags |= MATERIAL_FLAG_CRACK_OVERLAY;
1206 u16 l = getInteriorLight(n, 1, nodedef);
1207 video::SColor c = MapBlock_LightColor(255, l, f.light_source);
1209 float s = BS/2*f.visual_scale;
1211 content_t current = n.getContent();
1216 static const v3s16 dirs[6] = {
1225 int doDraw[6] = {0,0,0,0,0,0};
1227 bool drawAllFaces = true;
1229 bool drawBottomFacesOnly = false; // Currently unused
1231 // Check for adjacent nodes
1232 for(int i = 0; i < 6; i++)
1234 n2p = blockpos_nodes + p + dirs[i];
1235 n2 = data->m_vmanip.getNodeNoEx(n2p);
1236 n2c = n2.getContent();
1237 if (n2c != CONTENT_IGNORE && n2c != CONTENT_AIR && n2c != current) {
1240 drawAllFaces = false;
1245 for(int j = 0; j < 6; j++)
1247 int vOffset = 0; // Vertical offset of faces after rotation
1248 int hOffset = 4; // Horizontal offset of faces to reach the edge
1250 video::S3DVertex vertices[4] =
1252 video::S3DVertex(-s,-BS/2, 0, 0,0,0, c, 0,1),
1253 video::S3DVertex( s,-BS/2, 0, 0,0,0, c, 1,1),
1254 video::S3DVertex( s,-BS/2 + s*2,0, 0,0,0, c, 1,0),
1255 video::S3DVertex(-s,-BS/2 + s*2,0, 0,0,0, c, 0,0),
1258 // Calculate which faces should be drawn, (top or sides)
1259 if(j == 0 && (drawAllFaces || (doDraw[3] == 1 || doDraw[1] == 1)))
1261 for(int i = 0; i < 4; i++) {
1262 vertices[i].Pos.rotateXZBy(90 + n.param2 * 2);
1263 vertices[i].Pos.rotateXYBy(-10);
1264 vertices[i].Pos.Y -= vOffset;
1265 vertices[i].Pos.X -= hOffset;
1268 else if(j == 1 && (drawAllFaces || (doDraw[5] == 1 || doDraw[1] == 1)))
1270 for(int i = 0; i < 4; i++) {
1271 vertices[i].Pos.rotateXZBy(180 + n.param2 * 2);
1272 vertices[i].Pos.rotateYZBy(10);
1273 vertices[i].Pos.Y -= vOffset;
1274 vertices[i].Pos.Z -= hOffset;
1277 else if(j == 2 && (drawAllFaces || (doDraw[2] == 1 || doDraw[1] == 1)))
1279 for(int i = 0; i < 4; i++) {
1280 vertices[i].Pos.rotateXZBy(270 + n.param2 * 2);
1281 vertices[i].Pos.rotateXYBy(10);
1282 vertices[i].Pos.Y -= vOffset;
1283 vertices[i].Pos.X += hOffset;
1286 else if(j == 3 && (drawAllFaces || (doDraw[4] == 1 || doDraw[1] == 1)))
1288 for(int i = 0; i < 4; i++) {
1289 vertices[i].Pos.rotateYZBy(-10);
1290 vertices[i].Pos.Y -= vOffset;
1291 vertices[i].Pos.Z += hOffset;
1295 // Center cross-flames
1296 else if(j == 4 && (drawAllFaces || doDraw[1] == 1))
1298 for(int i=0; i<4; i++) {
1299 vertices[i].Pos.rotateXZBy(45 + n.param2 * 2);
1300 vertices[i].Pos.Y -= vOffset;
1303 else if(j == 5 && (drawAllFaces || doDraw[1] == 1))
1305 for(int i=0; i<4; i++) {
1306 vertices[i].Pos.rotateXZBy(-45 + n.param2 * 2);
1307 vertices[i].Pos.Y -= vOffset;
1311 // Render flames on bottom
1312 else if(j == 0 && (drawBottomFacesOnly || (doDraw[0] == 1 && doDraw[1] == 0)))
1314 for(int i = 0; i < 4; i++) {
1315 vertices[i].Pos.rotateYZBy(70);
1316 vertices[i].Pos.rotateXZBy(90 + n.param2 * 2);
1317 vertices[i].Pos.Y += 4.84;
1318 vertices[i].Pos.X -= hOffset+0.7;
1321 else if(j == 1 && (drawBottomFacesOnly || (doDraw[0] == 1 && doDraw[1] == 0)))
1323 for(int i = 0; i < 4; i++) {
1324 vertices[i].Pos.rotateYZBy(70);
1325 vertices[i].Pos.rotateXZBy(180 + n.param2 * 2);
1326 vertices[i].Pos.Y += 4.84;
1327 vertices[i].Pos.Z -= hOffset+0.7;
1330 else if(j == 2 && (drawBottomFacesOnly || (doDraw[0] == 1 && doDraw[1] == 0)))
1332 for(int i = 0; i < 4; i++) {
1333 vertices[i].Pos.rotateYZBy(70);
1334 vertices[i].Pos.rotateXZBy(270 + n.param2 * 2);
1335 vertices[i].Pos.Y += 4.84;
1336 vertices[i].Pos.X += hOffset+0.7;
1339 else if(j == 3 && (drawBottomFacesOnly || (doDraw[0] == 1 && doDraw[1] == 0)))
1341 for(int i = 0; i < 4; i++) {
1342 vertices[i].Pos.rotateYZBy(70);
1343 vertices[i].Pos.Y += 4.84;
1344 vertices[i].Pos.Z += hOffset+0.7;
1348 // Skip faces that aren't adjacent to a node
1352 for(int i=0; i<4; i++)
1354 vertices[i].Pos *= f.visual_scale;
1355 vertices[i].Pos += intToFloat(p, BS);
1358 u16 indices[] = {0,1,2,2,3,0};
1359 // Add to mesh collector
1360 collector.append(tile, vertices, 4, indices, 6);
1365 TileSpec tile = getNodeTile(n, p, v3s16(0,0,0), data);
1366 TileSpec tile_nocrack = tile;
1367 tile_nocrack.material_flags &= ~MATERIAL_FLAG_CRACK;
1369 // Put wood the right way around in the posts
1370 TileSpec tile_rot = tile;
1371 tile_rot.rotation = 1;
1373 u16 l = getInteriorLight(n, 1, nodedef);
1374 video::SColor c = MapBlock_LightColor(255, l, f.light_source);
1376 const f32 post_rad=(f32)BS/8;
1377 const f32 bar_rad=(f32)BS/16;
1378 const f32 bar_len=(f32)(BS/2)-post_rad;
1380 v3f pos = intToFloat(p, BS);
1382 // The post - always present
1383 aabb3f post(-post_rad,-BS/2,-post_rad,post_rad,BS/2,post_rad);
1384 post.MinEdge += pos;
1385 post.MaxEdge += pos;
1387 6/16.,6/16.,10/16.,10/16.,
1388 6/16.,6/16.,10/16.,10/16.,
1393 makeCuboid(&collector, post, &tile_rot, 1, c, postuv);
1395 // Now a section of fence, +X, if there's a post there
1398 MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
1399 const ContentFeatures *f2 = &nodedef->get(n2);
1400 if(f2->drawtype == NDT_FENCELIKE)
1402 aabb3f bar(-bar_len+BS/2,-bar_rad+BS/4,-bar_rad,
1403 bar_len+BS/2,bar_rad+BS/4,bar_rad);
1407 0/16.,2/16.,16/16.,4/16.,
1408 0/16.,4/16.,16/16.,6/16.,
1409 6/16.,6/16.,8/16.,8/16.,
1410 10/16.,10/16.,12/16.,12/16.,
1411 0/16.,8/16.,16/16.,10/16.,
1412 0/16.,14/16.,16/16.,16/16.};
1413 makeCuboid(&collector, bar, &tile_nocrack, 1,
1415 bar.MinEdge.Y -= BS/2;
1416 bar.MaxEdge.Y -= BS/2;
1417 makeCuboid(&collector, bar, &tile_nocrack, 1,
1421 // Now a section of fence, +Z, if there's a post there
1424 n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
1425 f2 = &nodedef->get(n2);
1426 if(f2->drawtype == NDT_FENCELIKE)
1428 aabb3f bar(-bar_rad,-bar_rad+BS/4,-bar_len+BS/2,
1429 bar_rad,bar_rad+BS/4,bar_len+BS/2);
1433 3/16.,1/16.,5/16.,5/16., // cannot rotate; stretch
1434 4/16.,1/16.,6/16.,5/16., // for wood texture instead
1435 0/16.,9/16.,16/16.,11/16.,
1436 0/16.,6/16.,16/16.,8/16.,
1437 6/16.,6/16.,8/16.,8/16.,
1438 10/16.,10/16.,12/16.,12/16.};
1439 makeCuboid(&collector, bar, &tile_nocrack, 1,
1441 bar.MinEdge.Y -= BS/2;
1442 bar.MaxEdge.Y -= BS/2;
1443 makeCuboid(&collector, bar, &tile_nocrack, 1,
1449 bool is_rail_x [] = { false, false }; /* x-1, x+1 */
1450 bool is_rail_z [] = { false, false }; /* z-1, z+1 */
1452 bool is_rail_z_minus_y [] = { false, false }; /* z-1, z+1; y-1 */
1453 bool is_rail_x_minus_y [] = { false, false }; /* x-1, z+1; y-1 */
1454 bool is_rail_z_plus_y [] = { false, false }; /* z-1, z+1; y+1 */
1455 bool is_rail_x_plus_y [] = { false, false }; /* x-1, x+1; y+1 */
1457 MapNode n_minus_x = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x-1,y,z));
1458 MapNode n_plus_x = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x+1,y,z));
1459 MapNode n_minus_z = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y,z-1));
1460 MapNode n_plus_z = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y,z+1));
1461 MapNode n_plus_x_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x+1, y+1, z));
1462 MapNode n_plus_x_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x+1, y-1, z));
1463 MapNode n_minus_x_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x-1, y+1, z));
1464 MapNode n_minus_x_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x-1, y-1, z));
1465 MapNode n_plus_z_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y+1, z+1));
1466 MapNode n_minus_z_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y+1, z-1));
1467 MapNode n_plus_z_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y-1, z+1));
1468 MapNode n_minus_z_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y-1, z-1));
1470 content_t thiscontent = n.getContent();
1471 std::string groupname = "connect_to_raillike"; // name of the group that enables connecting to raillike nodes of different kind
1472 bool self_connect_to_raillike = ((ItemGroupList) nodedef->get(n).groups)[groupname] != 0;
1474 if ((nodedef->get(n_minus_x).drawtype == NDT_RAILLIKE
1475 && ((ItemGroupList) nodedef->get(n_minus_x).groups)[groupname] != 0
1476 && self_connect_to_raillike)
1477 || n_minus_x.getContent() == thiscontent)
1478 is_rail_x[0] = true;
1480 if ((nodedef->get(n_minus_x_minus_y).drawtype == NDT_RAILLIKE
1481 && ((ItemGroupList) nodedef->get(n_minus_x_minus_y).groups)[groupname] != 0
1482 && self_connect_to_raillike)
1483 || n_minus_x_minus_y.getContent() == thiscontent)
1484 is_rail_x_minus_y[0] = true;
1486 if ((nodedef->get(n_minus_x_plus_y).drawtype == NDT_RAILLIKE
1487 && ((ItemGroupList) nodedef->get(n_minus_x_plus_y).groups)[groupname] != 0
1488 && self_connect_to_raillike)
1489 || n_minus_x_plus_y.getContent() == thiscontent)
1490 is_rail_x_plus_y[0] = true;
1492 if ((nodedef->get(n_plus_x).drawtype == NDT_RAILLIKE
1493 && ((ItemGroupList) nodedef->get(n_plus_x).groups)[groupname] != 0
1494 && self_connect_to_raillike)
1495 || n_plus_x.getContent() == thiscontent)
1496 is_rail_x[1] = true;
1498 if ((nodedef->get(n_plus_x_minus_y).drawtype == NDT_RAILLIKE
1499 && ((ItemGroupList) nodedef->get(n_plus_x_minus_y).groups)[groupname] != 0
1500 && self_connect_to_raillike)
1501 || n_plus_x_minus_y.getContent() == thiscontent)
1502 is_rail_x_minus_y[1] = true;
1504 if ((nodedef->get(n_plus_x_plus_y).drawtype == NDT_RAILLIKE
1505 && ((ItemGroupList) nodedef->get(n_plus_x_plus_y).groups)[groupname] != 0
1506 && self_connect_to_raillike)
1507 || n_plus_x_plus_y.getContent() == thiscontent)
1508 is_rail_x_plus_y[1] = true;
1510 if ((nodedef->get(n_minus_z).drawtype == NDT_RAILLIKE
1511 && ((ItemGroupList) nodedef->get(n_minus_z).groups)[groupname] != 0
1512 && self_connect_to_raillike)
1513 || n_minus_z.getContent() == thiscontent)
1514 is_rail_z[0] = true;
1516 if ((nodedef->get(n_minus_z_minus_y).drawtype == NDT_RAILLIKE
1517 && ((ItemGroupList) nodedef->get(n_minus_z_minus_y).groups)[groupname] != 0
1518 && self_connect_to_raillike)
1519 || n_minus_z_minus_y.getContent() == thiscontent)
1520 is_rail_z_minus_y[0] = true;
1522 if ((nodedef->get(n_minus_z_plus_y).drawtype == NDT_RAILLIKE
1523 && ((ItemGroupList) nodedef->get(n_minus_z_plus_y).groups)[groupname] != 0
1524 && self_connect_to_raillike)
1525 || n_minus_z_plus_y.getContent() == thiscontent)
1526 is_rail_z_plus_y[0] = true;
1528 if ((nodedef->get(n_plus_z).drawtype == NDT_RAILLIKE
1529 && ((ItemGroupList) nodedef->get(n_plus_z).groups)[groupname] != 0
1530 && self_connect_to_raillike)
1531 || n_plus_z.getContent() == thiscontent)
1532 is_rail_z[1] = true;
1534 if ((nodedef->get(n_plus_z_minus_y).drawtype == NDT_RAILLIKE
1535 && ((ItemGroupList) nodedef->get(n_plus_z_minus_y).groups)[groupname] != 0
1536 && self_connect_to_raillike)
1537 || n_plus_z_minus_y.getContent() == thiscontent)
1538 is_rail_z_minus_y[1] = true;
1540 if ((nodedef->get(n_plus_z_plus_y).drawtype == NDT_RAILLIKE
1541 && ((ItemGroupList) nodedef->get(n_plus_z_plus_y).groups)[groupname] != 0
1542 && self_connect_to_raillike)
1543 || n_plus_z_plus_y.getContent() == thiscontent)
1544 is_rail_z_plus_y[1] = true;
1546 bool is_rail_x_all[] = {false, false};
1547 bool is_rail_z_all[] = {false, false};
1548 is_rail_x_all[0]=is_rail_x[0] || is_rail_x_minus_y[0] || is_rail_x_plus_y[0];
1549 is_rail_x_all[1]=is_rail_x[1] || is_rail_x_minus_y[1] || is_rail_x_plus_y[1];
1550 is_rail_z_all[0]=is_rail_z[0] || is_rail_z_minus_y[0] || is_rail_z_plus_y[0];
1551 is_rail_z_all[1]=is_rail_z[1] || is_rail_z_minus_y[1] || is_rail_z_plus_y[1];
1553 // reasonable default, flat straight unrotated rail
1554 bool is_straight = true;
1555 int adjacencies = 0;
1559 // check for sloped rail
1560 if (is_rail_x_plus_y[0] || is_rail_x_plus_y[1] || is_rail_z_plus_y[0] || is_rail_z_plus_y[1])
1562 adjacencies = 5; //5 means sloped
1563 is_straight = true; // sloped is always straight
1567 // is really straight, rails on both sides
1568 is_straight = (is_rail_x_all[0] && is_rail_x_all[1]) || (is_rail_z_all[0] && is_rail_z_all[1]);
1569 adjacencies = is_rail_x_all[0] + is_rail_x_all[1] + is_rail_z_all[0] + is_rail_z_all[1];
1572 switch (adjacencies) {
1574 if(is_rail_x_all[0] || is_rail_x_all[1])
1579 tileindex = 1; // curved
1580 if(is_rail_x_all[0] && is_rail_x_all[1])
1582 if(is_rail_z_all[0] && is_rail_z_all[1]){
1583 if (is_rail_z_plus_y[0])
1586 else if(is_rail_x_all[0] && is_rail_z_all[0])
1588 else if(is_rail_x_all[0] && is_rail_z_all[1])
1590 else if(is_rail_x_all[1] && is_rail_z_all[1])
1594 // here is where the potential to 'switch' a junction is, but not implemented at present
1595 tileindex = 2; // t-junction
1596 if(!is_rail_x_all[1])
1598 if(!is_rail_z_all[0])
1600 if(!is_rail_z_all[1])
1604 tileindex = 3; // crossing
1607 if(is_rail_z_plus_y[0])
1609 if(is_rail_x_plus_y[0])
1611 if(is_rail_x_plus_y[1])
1618 TileSpec tile = getNodeTileN(n, p, tileindex, data);
1619 tile.material_flags &= ~MATERIAL_FLAG_BACKFACE_CULLING;
1620 tile.material_flags |= MATERIAL_FLAG_CRACK_OVERLAY;
1622 u16 l = getInteriorLight(n, 0, nodedef);
1623 video::SColor c = MapBlock_LightColor(255, l, f.light_source);
1625 float d = (float)BS/64;
1629 if (is_rail_x_plus_y[0] || is_rail_x_plus_y[1] || is_rail_z_plus_y[0] || is_rail_z_plus_y[1])
1630 g = 1; //Object is at a slope
1632 video::S3DVertex vertices[4] =
1634 video::S3DVertex(-s, -s+d,-s, 0,0,0, c,0,1),
1635 video::S3DVertex( s, -s+d,-s, 0,0,0, c,1,1),
1636 video::S3DVertex( s, g*s+d, s, 0,0,0, c,1,0),
1637 video::S3DVertex(-s, g*s+d, s, 0,0,0, c,0,0),
1640 for(s32 i=0; i<4; i++)
1643 vertices[i].Pos.rotateXZBy(angle);
1644 vertices[i].Pos += intToFloat(p, BS);
1647 u16 indices[] = {0,1,2,2,3,0};
1648 collector.append(tile, vertices, 4, indices, 6);
1652 static const v3s16 tile_dirs[6] = {
1662 u16 l = getInteriorLight(n, 1, nodedef);
1663 video::SColor c = MapBlock_LightColor(255, l, f.light_source);
1665 v3f pos = intToFloat(p, BS);
1667 std::vector<aabb3f> boxes = n.getNodeBoxes(nodedef);
1668 for(std::vector<aabb3f>::iterator
1670 i != boxes.end(); i++)
1672 for(int j = 0; j < 6; j++)
1674 // Handles facedir rotation for textures
1675 tiles[j] = getNodeTile(n, p, tile_dirs[j], data);
1682 if (box.MinEdge.X > box.MaxEdge.X)
1685 box.MinEdge.X=box.MaxEdge.X;
1688 if (box.MinEdge.Y > box.MaxEdge.Y)
1691 box.MinEdge.Y=box.MaxEdge.Y;
1694 if (box.MinEdge.Z > box.MaxEdge.Z)
1697 box.MinEdge.Z=box.MaxEdge.Z;
1702 // Compute texture coords
1703 f32 tx1 = (box.MinEdge.X/BS)+0.5;
1704 f32 ty1 = (box.MinEdge.Y/BS)+0.5;
1705 f32 tz1 = (box.MinEdge.Z/BS)+0.5;
1706 f32 tx2 = (box.MaxEdge.X/BS)+0.5;
1707 f32 ty2 = (box.MaxEdge.Y/BS)+0.5;
1708 f32 tz2 = (box.MaxEdge.Z/BS)+0.5;
1711 tx1, 1-tz2, tx2, 1-tz1,
1715 tz1, 1-ty2, tz2, 1-ty1,
1717 1-tz2, 1-ty2, 1-tz1, 1-ty1,
1719 1-tx2, 1-ty2, 1-tx1, 1-ty1,
1721 tx1, 1-ty2, tx2, 1-ty1,
1723 makeCuboid(&collector, box, tiles, 6, c, txc);
1728 v3f pos = intToFloat(p, BS);
1729 video::SColor c = MapBlock_LightColor(255, getInteriorLight(n, 1, nodedef), f.light_source);
1732 if (f.param_type_2 == CPT2_FACEDIR) {
1733 facedir = n.getFaceDir(nodedef);
1734 } else if (f.param_type_2 == CPT2_WALLMOUNTED) {
1735 //convert wallmounted to 6dfacedir.
1736 //when cache enabled, it is already converted
1737 facedir = n.getWallMounted(nodedef);
1738 if (!enable_mesh_cache) {
1739 static const u8 wm_to_6d[6] = {20, 0, 16+1, 12+3, 8, 4+2};
1740 facedir = wm_to_6d[facedir];
1744 if (f.mesh_ptr[facedir]) {
1745 // use cached meshes
1746 for(u16 j = 0; j < f.mesh_ptr[0]->getMeshBufferCount(); j++) {
1747 scene::IMeshBuffer *buf = f.mesh_ptr[facedir]->getMeshBuffer(j);
1748 collector.append(getNodeTileN(n, p, j, data),
1749 (video::S3DVertex *)buf->getVertices(), buf->getVertexCount(),
1750 buf->getIndices(), buf->getIndexCount(), pos, c);
1752 } else if (f.mesh_ptr[0]) {
1753 // no cache, clone and rotate mesh
1754 scene::IMesh* mesh = cloneMesh(f.mesh_ptr[0]);
1755 rotateMeshBy6dFacedir(mesh, facedir);
1756 recalculateBoundingBox(mesh);
1757 meshmanip->recalculateNormals(mesh, true, false);
1758 for(u16 j = 0; j < mesh->getMeshBufferCount(); j++) {
1759 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
1760 collector.append(getNodeTileN(n, p, j, data),
1761 (video::S3DVertex *)buf->getVertices(), buf->getVertexCount(),
1762 buf->getIndices(), buf->getIndexCount(), pos, c);