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"
22 #include "main.h" // For g_settings
23 #include "mapblock_mesh.h" // For MapBlock_LightColor() and MeshCollector
28 #include "util/numeric.h"
29 #include "util/directiontables.h"
32 // collector - the MeshCollector for the resulting polygons
33 // box - the position and size of the box
34 // tiles - the tiles (materials) to use (for all 6 faces)
35 // tilecount - number of entries in tiles, 1<=tilecount<=6
36 // c - vertex colour - used for all
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 up-down-right-left-back-front
42 // (compatible with ContentFeatures). If you specified 0,0,1,1
43 // for each face, that would be the same as passing NULL.
44 void makeCuboid(MeshCollector *collector, const aabb3f &box,
45 TileSpec *tiles, int tilecount,
46 video::SColor &c, const f32* txc)
48 assert(tilecount >= 1 && tilecount <= 6);
50 v3f min = box.MinEdge;
51 v3f max = box.MaxEdge;
57 static const f32 txc_default[24] = {
68 video::S3DVertex vertices[24] =
71 video::S3DVertex(min.X,max.Y,max.Z, 0,1,0, c, txc[0],txc[1]),
72 video::S3DVertex(max.X,max.Y,max.Z, 0,1,0, c, txc[2],txc[1]),
73 video::S3DVertex(max.X,max.Y,min.Z, 0,1,0, c, txc[2],txc[3]),
74 video::S3DVertex(min.X,max.Y,min.Z, 0,1,0, c, txc[0],txc[3]),
76 video::S3DVertex(min.X,min.Y,min.Z, 0,-1,0, c, txc[4],txc[5]),
77 video::S3DVertex(max.X,min.Y,min.Z, 0,-1,0, c, txc[6],txc[5]),
78 video::S3DVertex(max.X,min.Y,max.Z, 0,-1,0, c, txc[6],txc[7]),
79 video::S3DVertex(min.X,min.Y,max.Z, 0,-1,0, c, txc[4],txc[7]),
81 video::S3DVertex(max.X,max.Y,min.Z, 1,0,0, c, txc[ 8],txc[9]),
82 video::S3DVertex(max.X,max.Y,max.Z, 1,0,0, c, txc[10],txc[9]),
83 video::S3DVertex(max.X,min.Y,max.Z, 1,0,0, c, txc[10],txc[11]),
84 video::S3DVertex(max.X,min.Y,min.Z, 1,0,0, c, txc[ 8],txc[11]),
86 video::S3DVertex(min.X,max.Y,max.Z, -1,0,0, c, txc[12],txc[13]),
87 video::S3DVertex(min.X,max.Y,min.Z, -1,0,0, c, txc[14],txc[13]),
88 video::S3DVertex(min.X,min.Y,min.Z, -1,0,0, c, txc[14],txc[15]),
89 video::S3DVertex(min.X,min.Y,max.Z, -1,0,0, c, txc[12],txc[15]),
91 video::S3DVertex(max.X,max.Y,max.Z, 0,0,1, c, txc[16],txc[17]),
92 video::S3DVertex(min.X,max.Y,max.Z, 0,0,1, c, txc[18],txc[17]),
93 video::S3DVertex(min.X,min.Y,max.Z, 0,0,1, c, txc[18],txc[19]),
94 video::S3DVertex(max.X,min.Y,max.Z, 0,0,1, c, txc[16],txc[19]),
96 video::S3DVertex(min.X,max.Y,min.Z, 0,0,-1, c, txc[20],txc[21]),
97 video::S3DVertex(max.X,max.Y,min.Z, 0,0,-1, c, txc[22],txc[21]),
98 video::S3DVertex(max.X,min.Y,min.Z, 0,0,-1, c, txc[22],txc[23]),
99 video::S3DVertex(min.X,min.Y,min.Z, 0,0,-1, c, txc[20],txc[23]),
103 for(int i = 0; i < tilecount; i++)
105 switch (tiles[i].rotation)
110 for (int x = 0; x < 4; x++)
111 vertices[i*4+x].TCoords.rotateBy(90,irr::core::vector2df(0, 0));
114 for (int x = 0; x < 4; x++)
115 vertices[i*4+x].TCoords.rotateBy(180,irr::core::vector2df(0, 0));
118 for (int x = 0; x < 4; x++)
119 vertices[i*4+x].TCoords.rotateBy(270,irr::core::vector2df(0, 0));
122 for (int x = 0; x < 4; x++)
123 vertices[i*4+x].TCoords.rotateBy(90,irr::core::vector2df(0, 0));
125 tiles[i].texture.pos.Y += tiles[i].texture.size.Y;
126 tiles[i].texture.size.Y *= -1;
129 for (int x = 0; x < 4; x++)
130 vertices[i*4+x].TCoords.rotateBy(270,irr::core::vector2df(0, 0));
131 t=vertices[i*4].TCoords;
132 tiles[i].texture.pos.Y += tiles[i].texture.size.Y;
133 tiles[i].texture.size.Y *= -1;
136 for (int x = 0; x < 4; x++)
137 vertices[i*4+x].TCoords.rotateBy(90,irr::core::vector2df(0, 0));
138 tiles[i].texture.pos.X += tiles[i].texture.size.X;
139 tiles[i].texture.size.X *= -1;
142 for (int x = 0; x < 4; x++)
143 vertices[i*4+x].TCoords.rotateBy(270,irr::core::vector2df(0, 0));
144 tiles[i].texture.pos.X += tiles[i].texture.size.X;
145 tiles[i].texture.size.X *= -1;
148 tiles[i].texture.pos.Y += tiles[i].texture.size.Y;
149 tiles[i].texture.size.Y *= -1;
152 tiles[i].texture.pos.X += tiles[i].texture.size.X;
153 tiles[i].texture.size.X *= -1;
159 for(s32 j=0; j<24; j++)
161 int tileindex = MYMIN(j/4, tilecount-1);
162 vertices[j].TCoords *= tiles[tileindex].texture.size;
163 vertices[j].TCoords += tiles[tileindex].texture.pos;
165 u16 indices[] = {0,1,2,2,3,0};
166 // Add to mesh collector
167 for(s32 j=0; j<24; j+=4)
169 int tileindex = MYMIN(j/4, tilecount-1);
170 collector->append(tiles[tileindex],
171 vertices+j, 4, indices, 6);
175 void mapblock_mesh_generate_special(MeshMakeData *data,
176 MeshCollector &collector)
178 INodeDefManager *nodedef = data->m_gamedef->ndef();
181 //TimeTaker timer("mapblock_mesh_generate_special()");
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 for(s16 z=0; z<MAP_BLOCKSIZE; z++)
195 for(s16 y=0; y<MAP_BLOCKSIZE; y++)
196 for(s16 x=0; x<MAP_BLOCKSIZE; x++)
200 MapNode n = data->m_vmanip.getNodeNoEx(blockpos_nodes+p);
201 const ContentFeatures &f = nodedef->get(n);
203 // Only solidness=0 stuff is drawn here
209 infostream<<"Got "<<f.drawtype<<std::endl;
217 Add water sources to mesh if using new style
219 TileSpec tile_liquid = f.special_tiles[0];
220 TileSpec tile_liquid_bfculled = getNodeTile(n, p, v3s16(0,0,0), data);
221 AtlasPointer &pa_liquid = tile_liquid.texture;
223 bool top_is_same_liquid = false;
224 MapNode ntop = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y+1,z));
225 content_t c_flowing = nodedef->getId(f.liquid_alternative_flowing);
226 content_t c_source = nodedef->getId(f.liquid_alternative_source);
227 if(ntop.getContent() == c_flowing || ntop.getContent() == c_source)
228 top_is_same_liquid = true;
230 u16 l = getInteriorLight(n, 0, data);
231 video::SColor c = MapBlock_LightColor(f.alpha, l, decode_light(f.light_source));
236 v3s16 side_dirs[4] = {
242 for(u32 i=0; i<4; i++)
244 v3s16 dir = side_dirs[i];
246 MapNode neighbor = data->m_vmanip.getNodeNoEx(blockpos_nodes + p + dir);
247 content_t neighbor_content = neighbor.getContent();
248 const ContentFeatures &n_feat = nodedef->get(neighbor_content);
249 MapNode n_top = data->m_vmanip.getNodeNoEx(blockpos_nodes + p + dir+ v3s16(0,1,0));
250 content_t n_top_c = n_top.getContent();
252 if(neighbor_content == CONTENT_IGNORE)
256 If our topside is liquid and neighbor's topside
257 is liquid, don't draw side face
259 if(top_is_same_liquid && (n_top_c == c_flowing ||
260 n_top_c == c_source || n_top_c == CONTENT_IGNORE))
263 // Don't draw face if neighbor is blocking the view
264 if(n_feat.solidness == 2)
267 bool neighbor_is_same_liquid = (neighbor_content == c_source
268 || neighbor_content == c_flowing);
270 // Don't draw any faces if neighbor same is liquid and top is
272 if(neighbor_is_same_liquid && !top_is_same_liquid)
275 // Use backface culled material if neighbor doesn't have a
277 const TileSpec *current_tile = &tile_liquid;
278 if(n_feat.solidness != 0 || n_feat.visual_solidness != 0)
279 current_tile = &tile_liquid_bfculled;
281 video::S3DVertex vertices[4] =
283 video::S3DVertex(-BS/2,0,BS/2,0,0,0, c,
284 pa_liquid.x0(), pa_liquid.y1()),
285 video::S3DVertex(BS/2,0,BS/2,0,0,0, c,
286 pa_liquid.x1(), pa_liquid.y1()),
287 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
288 pa_liquid.x1(), pa_liquid.y0()),
289 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
290 pa_liquid.x0(), pa_liquid.y0()),
294 If our topside is liquid, set upper border of face
295 at upper border of node
297 if(top_is_same_liquid)
299 vertices[2].Pos.Y = 0.5*BS;
300 vertices[3].Pos.Y = 0.5*BS;
303 Otherwise upper position of face is liquid level
307 vertices[2].Pos.Y = (node_liquid_level-0.5)*BS;
308 vertices[3].Pos.Y = (node_liquid_level-0.5)*BS;
311 If neighbor is liquid, lower border of face is liquid level
313 if(neighbor_is_same_liquid)
315 vertices[0].Pos.Y = (node_liquid_level-0.5)*BS;
316 vertices[1].Pos.Y = (node_liquid_level-0.5)*BS;
319 If neighbor is not liquid, lower border of face is
324 vertices[0].Pos.Y = -0.5*BS;
325 vertices[1].Pos.Y = -0.5*BS;
328 for(s32 j=0; j<4; j++)
330 if(dir == v3s16(0,0,1))
331 vertices[j].Pos.rotateXZBy(0);
332 if(dir == v3s16(0,0,-1))
333 vertices[j].Pos.rotateXZBy(180);
334 if(dir == v3s16(-1,0,0))
335 vertices[j].Pos.rotateXZBy(90);
336 if(dir == v3s16(1,0,-0))
337 vertices[j].Pos.rotateXZBy(-90);
339 // Do this to not cause glitches when two liquids are
341 /*if(neighbor_is_same_liquid == false){
342 vertices[j].Pos.X *= 0.98;
343 vertices[j].Pos.Z *= 0.98;
346 vertices[j].Pos += intToFloat(p, BS);
349 u16 indices[] = {0,1,2,2,3,0};
350 // Add to mesh collector
351 collector.append(*current_tile, vertices, 4, indices, 6);
357 if(top_is_same_liquid)
360 video::S3DVertex vertices[4] =
362 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
363 pa_liquid.x0(), pa_liquid.y1()),
364 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
365 pa_liquid.x1(), pa_liquid.y1()),
366 video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c,
367 pa_liquid.x1(), pa_liquid.y0()),
368 video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c,
369 pa_liquid.x0(), pa_liquid.y0()),
372 v3f offset(p.X*BS, p.Y*BS + (-0.5+node_liquid_level)*BS, p.Z*BS);
373 for(s32 i=0; i<4; i++)
375 vertices[i].Pos += offset;
378 u16 indices[] = {0,1,2,2,3,0};
379 // Add to mesh collector
380 collector.append(tile_liquid, vertices, 4, indices, 6);
382 case NDT_FLOWINGLIQUID:
385 Add flowing liquid to mesh
387 TileSpec tile_liquid = f.special_tiles[0];
388 TileSpec tile_liquid_bfculled = f.special_tiles[1];
389 AtlasPointer &pa_liquid = tile_liquid.texture;
391 bool top_is_same_liquid = false;
392 MapNode ntop = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y+1,z));
393 content_t c_flowing = nodedef->getId(f.liquid_alternative_flowing);
394 content_t c_source = nodedef->getId(f.liquid_alternative_source);
395 if(ntop.getContent() == c_flowing || ntop.getContent() == c_source)
396 top_is_same_liquid = true;
399 // If this liquid emits light and doesn't contain light, draw
400 // it at what it emits, for an increased effect
401 u8 light_source = nodedef->get(n).light_source;
402 if(light_source != 0){
403 //l = decode_light(undiminish_light(light_source));
404 l = decode_light(light_source);
407 // Use the light of the node on top if possible
408 else if(nodedef->get(ntop).param_type == CPT_LIGHT)
409 l = getInteriorLight(ntop, 0, data);
410 // Otherwise use the light of this node (the liquid)
412 l = getInteriorLight(n, 0, data);
413 video::SColor c = MapBlock_LightColor(f.alpha, l, decode_light(f.light_source));
415 // Neighbor liquid levels (key = relative position)
416 // Includes current node
417 std::map<v3s16, f32> neighbor_levels;
418 std::map<v3s16, content_t> neighbor_contents;
419 std::map<v3s16, u8> neighbor_flags;
420 const u8 neighborflag_top_is_same_liquid = 0x01;
421 v3s16 neighbor_dirs[9] = {
432 for(u32 i=0; i<9; i++)
434 content_t content = CONTENT_AIR;
435 float level = -0.5 * BS;
438 v3s16 p2 = p + neighbor_dirs[i];
439 MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
440 if(n2.getContent() != CONTENT_IGNORE)
442 content = n2.getContent();
444 if(n2.getContent() == c_source)
445 level = (-0.5+node_liquid_level) * BS;
446 else if(n2.getContent() == c_flowing)
447 level = (-0.5 + ((float)(n2.param2&LIQUID_LEVEL_MASK)
448 + 0.5) / (float)LIQUID_LEVEL_SOURCE * node_liquid_level) * BS;
450 // Check node above neighbor.
451 // NOTE: This doesn't get executed if neighbor
454 n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
455 if(n2.getContent() == c_source ||
456 n2.getContent() == c_flowing)
457 flags |= neighborflag_top_is_same_liquid;
460 neighbor_levels[neighbor_dirs[i]] = level;
461 neighbor_contents[neighbor_dirs[i]] = content;
462 neighbor_flags[neighbor_dirs[i]] = flags;
465 // Corner heights (average between four liquids)
466 f32 corner_levels[4];
468 v3s16 halfdirs[4] = {
474 for(u32 i=0; i<4; i++)
476 v3s16 cornerdir = halfdirs[i];
477 float cornerlevel = 0;
480 for(u32 j=0; j<4; j++)
482 v3s16 neighbordir = cornerdir - halfdirs[j];
483 content_t content = neighbor_contents[neighbordir];
484 // If top is liquid, draw starting from top of node
485 if(neighbor_flags[neighbordir] &
486 neighborflag_top_is_same_liquid)
488 cornerlevel = 0.5*BS;
492 // Source is always the same height
493 else if(content == c_source)
495 cornerlevel = (-0.5+node_liquid_level)*BS;
499 // Flowing liquid has level information
500 else if(content == c_flowing)
502 cornerlevel += neighbor_levels[neighbordir];
505 else if(content == CONTENT_AIR)
511 cornerlevel = -0.5*BS+0.2;
512 else if(valid_count > 0)
513 cornerlevel /= valid_count;
514 corner_levels[i] = cornerlevel;
521 v3s16 side_dirs[4] = {
527 s16 side_corners[4][2] = {
533 for(u32 i=0; i<4; i++)
535 v3s16 dir = side_dirs[i];
538 If our topside is liquid and neighbor's topside
539 is liquid, don't draw side face
541 if(top_is_same_liquid &&
542 neighbor_flags[dir] & neighborflag_top_is_same_liquid)
545 content_t neighbor_content = neighbor_contents[dir];
546 const ContentFeatures &n_feat = nodedef->get(neighbor_content);
548 // Don't draw face if neighbor is blocking the view
549 if(n_feat.solidness == 2)
552 bool neighbor_is_same_liquid = (neighbor_content == c_source
553 || neighbor_content == c_flowing);
555 // Don't draw any faces if neighbor same is liquid and top is
557 if(neighbor_is_same_liquid == true
558 && top_is_same_liquid == false)
561 // Use backface culled material if neighbor doesn't have a
563 const TileSpec *current_tile = &tile_liquid;
564 if(n_feat.solidness != 0 || n_feat.visual_solidness != 0)
565 current_tile = &tile_liquid_bfculled;
567 video::S3DVertex vertices[4] =
569 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
570 pa_liquid.x0(), pa_liquid.y1()),
571 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
572 pa_liquid.x1(), pa_liquid.y1()),
573 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
574 pa_liquid.x1(), pa_liquid.y0()),
575 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
576 pa_liquid.x0(), pa_liquid.y0()),
580 If our topside is liquid, set upper border of face
581 at upper border of node
583 if(top_is_same_liquid)
585 vertices[2].Pos.Y = 0.5*BS;
586 vertices[3].Pos.Y = 0.5*BS;
589 Otherwise upper position of face is corner levels
593 vertices[2].Pos.Y = corner_levels[side_corners[i][0]];
594 vertices[3].Pos.Y = corner_levels[side_corners[i][1]];
598 If neighbor is liquid, lower border of face is corner
601 if(neighbor_is_same_liquid)
603 vertices[0].Pos.Y = corner_levels[side_corners[i][1]];
604 vertices[1].Pos.Y = corner_levels[side_corners[i][0]];
607 If neighbor is not liquid, lower border of face is
612 vertices[0].Pos.Y = -0.5*BS;
613 vertices[1].Pos.Y = -0.5*BS;
616 for(s32 j=0; j<4; j++)
618 if(dir == v3s16(0,0,1))
619 vertices[j].Pos.rotateXZBy(0);
620 if(dir == v3s16(0,0,-1))
621 vertices[j].Pos.rotateXZBy(180);
622 if(dir == v3s16(-1,0,0))
623 vertices[j].Pos.rotateXZBy(90);
624 if(dir == v3s16(1,0,-0))
625 vertices[j].Pos.rotateXZBy(-90);
627 // Do this to not cause glitches when two liquids are
629 /*if(neighbor_is_same_liquid == false){
630 vertices[j].Pos.X *= 0.98;
631 vertices[j].Pos.Z *= 0.98;
634 vertices[j].Pos += intToFloat(p, BS);
637 u16 indices[] = {0,1,2,2,3,0};
638 // Add to mesh collector
639 collector.append(*current_tile, vertices, 4, indices, 6);
643 Generate top side, if appropriate
646 if(top_is_same_liquid == false)
648 video::S3DVertex vertices[4] =
650 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
651 pa_liquid.x0(), pa_liquid.y1()),
652 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
653 pa_liquid.x1(), pa_liquid.y1()),
654 video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c,
655 pa_liquid.x1(), pa_liquid.y0()),
656 video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c,
657 pa_liquid.x0(), pa_liquid.y0()),
660 // To get backface culling right, the vertices need to go
661 // clockwise around the front of the face. And we happened to
662 // calculate corner levels in exact reverse order.
663 s32 corner_resolve[4] = {3,2,1,0};
665 for(s32 i=0; i<4; i++)
667 //vertices[i].Pos.Y += liquid_level;
668 //vertices[i].Pos.Y += neighbor_levels[v3s16(0,0,0)];
669 s32 j = corner_resolve[i];
670 vertices[i].Pos.Y += corner_levels[j];
671 vertices[i].Pos += intToFloat(p, BS);
674 // Default downwards-flowing texture animation goes from
675 // -Z towards +Z, thus the direction is +Z.
676 // Rotate texture to make animation go in flow direction
677 // Positive if liquid moves towards +Z
678 int dz = (corner_levels[side_corners[3][0]] +
679 corner_levels[side_corners[3][1]]) -
680 (corner_levels[side_corners[2][0]] +
681 corner_levels[side_corners[2][1]]);
682 // Positive if liquid moves towards +X
683 int dx = (corner_levels[side_corners[1][0]] +
684 corner_levels[side_corners[1][1]]) -
685 (corner_levels[side_corners[0][0]] +
686 corner_levels[side_corners[0][1]]);
690 v2f t = vertices[0].TCoords;
691 vertices[0].TCoords = vertices[1].TCoords;
692 vertices[1].TCoords = vertices[2].TCoords;
693 vertices[2].TCoords = vertices[3].TCoords;
694 vertices[3].TCoords = t;
699 v2f t = vertices[0].TCoords;
700 vertices[0].TCoords = vertices[3].TCoords;
701 vertices[3].TCoords = vertices[2].TCoords;
702 vertices[2].TCoords = vertices[1].TCoords;
703 vertices[1].TCoords = t;
708 v2f t = vertices[0].TCoords;
709 vertices[0].TCoords = vertices[3].TCoords;
710 vertices[3].TCoords = vertices[2].TCoords;
711 vertices[2].TCoords = vertices[1].TCoords;
712 vertices[1].TCoords = t;
713 t = vertices[0].TCoords;
714 vertices[0].TCoords = vertices[3].TCoords;
715 vertices[3].TCoords = vertices[2].TCoords;
716 vertices[2].TCoords = vertices[1].TCoords;
717 vertices[1].TCoords = t;
720 u16 indices[] = {0,1,2,2,3,0};
721 // Add to mesh collector
722 collector.append(tile_liquid, vertices, 4, indices, 6);
727 TileSpec tile = getNodeTile(n, p, v3s16(0,0,0), data);
728 AtlasPointer ap = tile.texture;
730 u16 l = getInteriorLight(n, 1, data);
731 video::SColor c = MapBlock_LightColor(255, l, decode_light(f.light_source));
733 for(u32 j=0; j<6; j++)
735 // Check this neighbor
736 v3s16 n2p = blockpos_nodes + p + g_6dirs[j];
737 MapNode n2 = data->m_vmanip.getNodeNoEx(n2p);
738 // Don't make face if neighbor is of same type
739 if(n2.getContent() == n.getContent())
743 video::S3DVertex vertices[4] =
745 video::S3DVertex(-BS/2,-BS/2,BS/2, 0,0,0, c,
747 video::S3DVertex(BS/2,-BS/2,BS/2, 0,0,0, c,
749 video::S3DVertex(BS/2,BS/2,BS/2, 0,0,0, c,
751 video::S3DVertex(-BS/2,BS/2,BS/2, 0,0,0, c,
755 // Rotations in the g_6dirs format
757 for(u16 i=0; i<4; i++)
758 vertices[i].Pos.rotateXZBy(0);
759 else if(j == 1) // Y+
760 for(u16 i=0; i<4; i++)
761 vertices[i].Pos.rotateYZBy(-90);
762 else if(j == 2) // X+
763 for(u16 i=0; i<4; i++)
764 vertices[i].Pos.rotateXZBy(-90);
765 else if(j == 3) // Z-
766 for(u16 i=0; i<4; i++)
767 vertices[i].Pos.rotateXZBy(180);
768 else if(j == 4) // Y-
769 for(u16 i=0; i<4; i++)
770 vertices[i].Pos.rotateYZBy(90);
771 else if(j == 5) // X-
772 for(u16 i=0; i<4; i++)
773 vertices[i].Pos.rotateXZBy(90);
775 for(u16 i=0; i<4; i++){
776 vertices[i].Pos += intToFloat(p, BS);
779 u16 indices[] = {0,1,2,2,3,0};
780 // Add to mesh collector
781 collector.append(tile, vertices, 4, indices, 6);
786 TileSpec tile_leaves = getNodeTile(n, p,
788 AtlasPointer pa_leaves = tile_leaves.texture;
790 u16 l = getInteriorLight(n, 1, data);
791 video::SColor c = MapBlock_LightColor(255, l, decode_light(f.light_source));
793 v3f pos = intToFloat(p, BS);
794 aabb3f box(-BS/2,-BS/2,-BS/2,BS/2,BS/2,BS/2);
797 makeCuboid(&collector, box, &tile_leaves, 1, c, NULL);
799 case NDT_ALLFACES_OPTIONAL:
800 // This is always pre-converted to something else
805 v3s16 dir = n.getWallMountedDir(nodedef);
808 if(dir == v3s16(0,-1,0)){
809 tileindex = 0; // floor
810 } else if(dir == v3s16(0,1,0)){
811 tileindex = 1; // ceiling
812 // For backwards compatibility
813 } else if(dir == v3s16(0,0,0)){
814 tileindex = 0; // floor
816 tileindex = 2; // side
819 TileSpec tile = getNodeTileN(n, p, tileindex, data);
820 tile.material_flags &= ~MATERIAL_FLAG_BACKFACE_CULLING;
821 tile.material_flags |= MATERIAL_FLAG_CRACK_OVERLAY;
823 AtlasPointer ap = tile.texture;
825 u16 l = getInteriorLight(n, 1, data);
826 video::SColor c = MapBlock_LightColor(255, l, decode_light(f.light_source));
828 // Wall at X+ of node
829 video::S3DVertex vertices[4] =
831 video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c,
833 video::S3DVertex(BS/2,-BS/2,0, 0,0,0, c,
835 video::S3DVertex(BS/2,BS/2,0, 0,0,0, c,
837 video::S3DVertex(-BS/2,BS/2,0, 0,0,0, c,
841 for(s32 i=0; i<4; i++)
843 if(dir == v3s16(1,0,0))
844 vertices[i].Pos.rotateXZBy(0);
845 if(dir == v3s16(-1,0,0))
846 vertices[i].Pos.rotateXZBy(180);
847 if(dir == v3s16(0,0,1))
848 vertices[i].Pos.rotateXZBy(90);
849 if(dir == v3s16(0,0,-1))
850 vertices[i].Pos.rotateXZBy(-90);
851 if(dir == v3s16(0,-1,0))
852 vertices[i].Pos.rotateXZBy(45);
853 if(dir == v3s16(0,1,0))
854 vertices[i].Pos.rotateXZBy(-45);
856 vertices[i].Pos += intToFloat(p, BS);
859 u16 indices[] = {0,1,2,2,3,0};
860 // Add to mesh collector
861 collector.append(tile, vertices, 4, indices, 6);
865 TileSpec tile = getNodeTileN(n, p, 0, data);
866 tile.material_flags &= ~MATERIAL_FLAG_BACKFACE_CULLING;
867 tile.material_flags |= MATERIAL_FLAG_CRACK_OVERLAY;
868 AtlasPointer ap = tile.texture;
870 u16 l = getInteriorLight(n, 0, data);
871 video::SColor c = MapBlock_LightColor(255, l, decode_light(f.light_source));
873 float d = (float)BS/16;
874 // Wall at X+ of node
875 video::S3DVertex vertices[4] =
877 video::S3DVertex(BS/2-d,BS/2,BS/2, 0,0,0, c,
879 video::S3DVertex(BS/2-d,BS/2,-BS/2, 0,0,0, c,
881 video::S3DVertex(BS/2-d,-BS/2,-BS/2, 0,0,0, c,
883 video::S3DVertex(BS/2-d,-BS/2,BS/2, 0,0,0, c,
887 v3s16 dir = n.getWallMountedDir(nodedef);
889 for(s32 i=0; i<4; i++)
891 if(dir == v3s16(1,0,0))
892 vertices[i].Pos.rotateXZBy(0);
893 if(dir == v3s16(-1,0,0))
894 vertices[i].Pos.rotateXZBy(180);
895 if(dir == v3s16(0,0,1))
896 vertices[i].Pos.rotateXZBy(90);
897 if(dir == v3s16(0,0,-1))
898 vertices[i].Pos.rotateXZBy(-90);
899 if(dir == v3s16(0,-1,0))
900 vertices[i].Pos.rotateXYBy(-90);
901 if(dir == v3s16(0,1,0))
902 vertices[i].Pos.rotateXYBy(90);
904 vertices[i].Pos += intToFloat(p, BS);
907 u16 indices[] = {0,1,2,2,3,0};
908 // Add to mesh collector
909 collector.append(tile, vertices, 4, indices, 6);
913 TileSpec tile = getNodeTileN(n, p, 0, data);
914 tile.material_flags |= MATERIAL_FLAG_CRACK_OVERLAY;
915 AtlasPointer ap = tile.texture;
917 u16 l = getInteriorLight(n, 1, data);
918 video::SColor c = MapBlock_LightColor(255, l, decode_light(f.light_source));
920 for(u32 j=0; j<2; j++)
922 video::S3DVertex vertices[4] =
924 video::S3DVertex(-BS/2*f.visual_scale,-BS/2,0, 0,0,0, c,
926 video::S3DVertex( BS/2*f.visual_scale,-BS/2,0, 0,0,0, c,
928 video::S3DVertex( BS/2*f.visual_scale,
929 -BS/2 + f.visual_scale*BS,0, 0,0,0, c,
931 video::S3DVertex(-BS/2*f.visual_scale,
932 -BS/2 + f.visual_scale*BS,0, 0,0,0, c,
938 for(u16 i=0; i<4; i++)
939 vertices[i].Pos.rotateXZBy(45);
943 for(u16 i=0; i<4; i++)
944 vertices[i].Pos.rotateXZBy(-45);
947 for(u16 i=0; i<4; i++)
949 vertices[i].Pos *= f.visual_scale;
950 vertices[i].Pos += intToFloat(p, BS);
953 u16 indices[] = {0,1,2,2,3,0};
954 // Add to mesh collector
955 collector.append(tile, vertices, 4, indices, 6);
960 TileSpec tile = getNodeTile(n, p, v3s16(0,0,0), data);
961 TileSpec tile_nocrack = tile;
962 tile_nocrack.material_flags &= ~MATERIAL_FLAG_CRACK;
964 // A hack to put wood the right way around in the posts
965 ITextureSource *tsrc = data->m_gamedef->tsrc();
966 TileSpec tile_rot = tile;
967 tile_rot.texture = tsrc->getTexture(tsrc->getTextureName(
968 tile.texture.id) + "^[transformR90");
970 u16 l = getInteriorLight(n, 1, data);
971 video::SColor c = MapBlock_LightColor(255, l, decode_light(f.light_source));
973 const f32 post_rad=(f32)BS/8;
974 const f32 bar_rad=(f32)BS/16;
975 const f32 bar_len=(f32)(BS/2)-post_rad;
977 v3f pos = intToFloat(p, BS);
979 // The post - always present
980 aabb3f post(-post_rad,-BS/2,-post_rad,post_rad,BS/2,post_rad);
984 6/16.,6/16.,10/16.,10/16.,
985 6/16.,6/16.,10/16.,10/16.,
990 makeCuboid(&collector, post, &tile_rot, 1, c, postuv);
992 // Now a section of fence, +X, if there's a post there
995 MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
996 const ContentFeatures *f2 = &nodedef->get(n2);
997 if(f2->drawtype == NDT_FENCELIKE)
999 aabb3f bar(-bar_len+BS/2,-bar_rad+BS/4,-bar_rad,
1000 bar_len+BS/2,bar_rad+BS/4,bar_rad);
1004 0/16.,2/16.,16/16.,4/16.,
1005 0/16.,4/16.,16/16.,6/16.,
1006 6/16.,6/16.,8/16.,8/16.,
1007 10/16.,10/16.,12/16.,12/16.,
1008 0/16.,8/16.,16/16.,10/16.,
1009 0/16.,14/16.,16/16.,16/16.};
1010 makeCuboid(&collector, bar, &tile_nocrack, 1,
1012 bar.MinEdge.Y -= BS/2;
1013 bar.MaxEdge.Y -= BS/2;
1014 makeCuboid(&collector, bar, &tile_nocrack, 1,
1018 // Now a section of fence, +Z, if there's a post there
1021 n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
1022 f2 = &nodedef->get(n2);
1023 if(f2->drawtype == NDT_FENCELIKE)
1025 aabb3f bar(-bar_rad,-bar_rad+BS/4,-bar_len+BS/2,
1026 bar_rad,bar_rad+BS/4,bar_len+BS/2);
1030 3/16.,1/16.,5/16.,5/16., // cannot rotate; stretch
1031 4/16.,1/16.,6/16.,5/16., // for wood texture instead
1032 0/16.,9/16.,16/16.,11/16.,
1033 0/16.,6/16.,16/16.,8/16.,
1034 6/16.,6/16.,8/16.,8/16.,
1035 10/16.,10/16.,12/16.,12/16.};
1036 makeCuboid(&collector, bar, &tile_nocrack, 1,
1038 bar.MinEdge.Y -= BS/2;
1039 bar.MaxEdge.Y -= BS/2;
1040 makeCuboid(&collector, bar, &tile_nocrack, 1,
1046 bool is_rail_x [] = { false, false }; /* x-1, x+1 */
1047 bool is_rail_z [] = { false, false }; /* z-1, z+1 */
1049 bool is_rail_z_minus_y [] = { false, false }; /* z-1, z+1; y-1 */
1050 bool is_rail_x_minus_y [] = { false, false }; /* x-1, z+1; y-1 */
1051 bool is_rail_z_plus_y [] = { false, false }; /* z-1, z+1; y+1 */
1052 bool is_rail_x_plus_y [] = { false, false }; /* x-1, x+1; y+1 */
1054 MapNode n_minus_x = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x-1,y,z));
1055 MapNode n_plus_x = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x+1,y,z));
1056 MapNode n_minus_z = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y,z-1));
1057 MapNode n_plus_z = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y,z+1));
1058 MapNode n_plus_x_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x+1, y+1, z));
1059 MapNode n_plus_x_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x+1, y-1, z));
1060 MapNode n_minus_x_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x-1, y+1, z));
1061 MapNode n_minus_x_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x-1, y-1, z));
1062 MapNode n_plus_z_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y+1, z+1));
1063 MapNode n_minus_z_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y+1, z-1));
1064 MapNode n_plus_z_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y-1, z+1));
1065 MapNode n_minus_z_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y-1, z-1));
1067 content_t thiscontent = n.getContent();
1068 if(n_minus_x.getContent() == thiscontent)
1069 is_rail_x[0] = true;
1070 if (n_minus_x_minus_y.getContent() == thiscontent)
1071 is_rail_x_minus_y[0] = true;
1072 if(n_minus_x_plus_y.getContent() == thiscontent)
1073 is_rail_x_plus_y[0] = true;
1075 if(n_plus_x.getContent() == thiscontent)
1076 is_rail_x[1] = true;
1077 if (n_plus_x_minus_y.getContent() == thiscontent)
1078 is_rail_x_minus_y[1] = true;
1079 if(n_plus_x_plus_y.getContent() == thiscontent)
1080 is_rail_x_plus_y[1] = true;
1082 if(n_minus_z.getContent() == thiscontent)
1083 is_rail_z[0] = true;
1084 if (n_minus_z_minus_y.getContent() == thiscontent)
1085 is_rail_z_minus_y[0] = true;
1086 if(n_minus_z_plus_y.getContent() == thiscontent)
1087 is_rail_z_plus_y[0] = true;
1089 if(n_plus_z.getContent() == thiscontent)
1090 is_rail_z[1] = true;
1091 if (n_plus_z_minus_y.getContent() == thiscontent)
1092 is_rail_z_minus_y[1] = true;
1093 if(n_plus_z_plus_y.getContent() == thiscontent)
1094 is_rail_z_plus_y[1] = true;
1096 bool is_rail_x_all[] = {false, false};
1097 bool is_rail_z_all[] = {false, false};
1098 is_rail_x_all[0]=is_rail_x[0] || is_rail_x_minus_y[0] || is_rail_x_plus_y[0];
1099 is_rail_x_all[1]=is_rail_x[1] || is_rail_x_minus_y[1] || is_rail_x_plus_y[1];
1100 is_rail_z_all[0]=is_rail_z[0] || is_rail_z_minus_y[0] || is_rail_z_plus_y[0];
1101 is_rail_z_all[1]=is_rail_z[1] || is_rail_z_minus_y[1] || is_rail_z_plus_y[1];
1103 // reasonable default, flat straight unrotated rail
1104 bool is_straight = true;
1105 int adjacencies = 0;
1109 // check for sloped rail
1110 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])
1112 adjacencies = 5; //5 means sloped
1113 is_straight = true; // sloped is always straight
1117 // is really straight, rails on both sides
1118 is_straight = (is_rail_x_all[0] && is_rail_x_all[1]) || (is_rail_z_all[0] && is_rail_z_all[1]);
1119 adjacencies = is_rail_x_all[0] + is_rail_x_all[1] + is_rail_z_all[0] + is_rail_z_all[1];
1122 switch (adjacencies) {
1124 if(is_rail_x_all[0] || is_rail_x_all[1])
1129 tileindex = 1; // curved
1130 if(is_rail_x_all[0] && is_rail_x_all[1])
1132 if(is_rail_z_all[0] && is_rail_z_all[1]){
1133 if (n_minus_z_plus_y.getContent() == thiscontent) angle = 180;
1135 else if(is_rail_x_all[0] && is_rail_z_all[0])
1137 else if(is_rail_x_all[0] && is_rail_z_all[1])
1139 else if(is_rail_x_all[1] && is_rail_z_all[1])
1143 // here is where the potential to 'switch' a junction is, but not implemented at present
1144 tileindex = 2; // t-junction
1145 if(!is_rail_x_all[1])
1147 if(!is_rail_z_all[0])
1149 if(!is_rail_z_all[1])
1153 tileindex = 3; // crossing
1156 if(is_rail_z_plus_y[0])
1158 if(is_rail_x_plus_y[0])
1160 if(is_rail_x_plus_y[1])
1167 TileSpec tile = getNodeTileN(n, p, tileindex, data);
1168 tile.material_flags &= ~MATERIAL_FLAG_BACKFACE_CULLING;
1169 tile.material_flags |= MATERIAL_FLAG_CRACK_OVERLAY;
1171 AtlasPointer ap = tile.texture;
1173 u16 l = getInteriorLight(n, 0, data);
1174 video::SColor c = MapBlock_LightColor(255, l, decode_light(f.light_source));
1176 float d = (float)BS/64;
1179 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])
1180 g=1; //Object is at a slope
1182 video::S3DVertex vertices[4] =
1184 video::S3DVertex(-BS/2,-BS/2+d,-BS/2, 0,0,0, c,
1186 video::S3DVertex(BS/2,-BS/2+d,-BS/2, 0,0,0, c,
1188 video::S3DVertex(BS/2,g*BS/2+d,BS/2, 0,0,0, c,
1190 video::S3DVertex(-BS/2,g*BS/2+d,BS/2, 0,0,0, c,
1194 for(s32 i=0; i<4; i++)
1197 vertices[i].Pos.rotateXZBy(angle);
1198 vertices[i].Pos += intToFloat(p, BS);
1201 u16 indices[] = {0,1,2,2,3,0};
1202 collector.append(tile, vertices, 4, indices, 6);
1206 static const v3s16 tile_dirs[6] = {
1216 u16 l = getInteriorLight(n, 0, data);
1217 video::SColor c = MapBlock_LightColor(255, l, decode_light(f.light_source));
1219 v3f pos = intToFloat(p, BS);
1221 std::vector<aabb3f> boxes = n.getNodeBoxes(nodedef);
1222 for(std::vector<aabb3f>::iterator
1224 i != boxes.end(); i++)
1226 for(int j = 0; j < 6; j++)
1228 // Handles facedir rotation for textures
1229 tiles[j] = getNodeTile(n, p, tile_dirs[j], data);
1236 if (box.MinEdge.X > box.MaxEdge.X)
1239 box.MinEdge.X=box.MaxEdge.X;
1242 if (box.MinEdge.Y > box.MaxEdge.Y)
1245 box.MinEdge.Y=box.MaxEdge.Y;
1248 if (box.MinEdge.Z > box.MaxEdge.Z)
1251 box.MinEdge.Z=box.MaxEdge.Z;
1256 // Compute texture coords
1257 f32 tx1 = (box.MinEdge.X/BS)+0.5;
1258 f32 ty1 = (box.MinEdge.Y/BS)+0.5;
1259 f32 tz1 = (box.MinEdge.Z/BS)+0.5;
1260 f32 tx2 = (box.MaxEdge.X/BS)+0.5;
1261 f32 ty2 = (box.MaxEdge.Y/BS)+0.5;
1262 f32 tz2 = (box.MaxEdge.Z/BS)+0.5;
1265 tx1, 1-tz2, tx2, 1-tz1,
1269 tz1, 1-ty2, tz2, 1-ty1,
1271 1-tz2, 1-ty2, 1-tz1, 1-ty1,
1273 1-tx2, 1-ty2, 1-tx1, 1-ty1,
1275 tx1, 1-ty2, tx2, 1-ty1,
1277 makeCuboid(&collector, box, tiles, 6, c, txc);