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 const 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;
55 static const f32 txc_default[24] = {
66 video::S3DVertex vertices[24] =
69 video::S3DVertex(min.X,max.Y,max.Z, 0,1,0, c, txc[0],txc[1]),
70 video::S3DVertex(max.X,max.Y,max.Z, 0,1,0, c, txc[2],txc[1]),
71 video::S3DVertex(max.X,max.Y,min.Z, 0,1,0, c, txc[2],txc[3]),
72 video::S3DVertex(min.X,max.Y,min.Z, 0,1,0, c, txc[0],txc[3]),
74 video::S3DVertex(min.X,min.Y,min.Z, 0,-1,0, c, txc[4],txc[5]),
75 video::S3DVertex(max.X,min.Y,min.Z, 0,-1,0, c, txc[6],txc[5]),
76 video::S3DVertex(max.X,min.Y,max.Z, 0,-1,0, c, txc[6],txc[7]),
77 video::S3DVertex(min.X,min.Y,max.Z, 0,-1,0, c, txc[4],txc[7]),
79 video::S3DVertex(max.X,max.Y,min.Z, 1,0,0, c, txc[ 8],txc[9]),
80 video::S3DVertex(max.X,max.Y,max.Z, 1,0,0, c, txc[10],txc[9]),
81 video::S3DVertex(max.X,min.Y,max.Z, 1,0,0, c, txc[10],txc[11]),
82 video::S3DVertex(max.X,min.Y,min.Z, 1,0,0, c, txc[ 8],txc[11]),
84 video::S3DVertex(min.X,max.Y,max.Z, -1,0,0, c, txc[12],txc[13]),
85 video::S3DVertex(min.X,max.Y,min.Z, -1,0,0, c, txc[14],txc[13]),
86 video::S3DVertex(min.X,min.Y,min.Z, -1,0,0, c, txc[14],txc[15]),
87 video::S3DVertex(min.X,min.Y,max.Z, -1,0,0, c, txc[12],txc[15]),
89 video::S3DVertex(max.X,max.Y,max.Z, 0,0,1, c, txc[16],txc[17]),
90 video::S3DVertex(min.X,max.Y,max.Z, 0,0,1, c, txc[18],txc[17]),
91 video::S3DVertex(min.X,min.Y,max.Z, 0,0,1, c, txc[18],txc[19]),
92 video::S3DVertex(max.X,min.Y,max.Z, 0,0,1, c, txc[16],txc[19]),
94 video::S3DVertex(min.X,max.Y,min.Z, 0,0,-1, c, txc[20],txc[21]),
95 video::S3DVertex(max.X,max.Y,min.Z, 0,0,-1, c, txc[22],txc[21]),
96 video::S3DVertex(max.X,min.Y,min.Z, 0,0,-1, c, txc[22],txc[23]),
97 video::S3DVertex(min.X,min.Y,min.Z, 0,0,-1, c, txc[20],txc[23]),
100 for(s32 j=0; j<24; j++)
102 int tileindex = MYMIN(j/4, tilecount-1);
103 vertices[j].TCoords *= tiles[tileindex].texture.size;
104 vertices[j].TCoords += tiles[tileindex].texture.pos;
107 u16 indices[] = {0,1,2,2,3,0};
109 // Add to mesh collector
110 for(s32 j=0; j<24; j+=4)
112 int tileindex = MYMIN(j/4, tilecount-1);
113 collector->append(tiles[tileindex],
114 vertices+j, 4, indices, 6);
118 void mapblock_mesh_generate_special(MeshMakeData *data,
119 MeshCollector &collector)
121 INodeDefManager *nodedef = data->m_gamedef->ndef();
124 //TimeTaker timer("mapblock_mesh_generate_special()");
129 bool new_style_water = g_settings->getBool("new_style_water");
131 float node_liquid_level = 1.0;
133 node_liquid_level = 0.85;
135 v3s16 blockpos_nodes = data->m_blockpos*MAP_BLOCKSIZE;
137 for(s16 z=0; z<MAP_BLOCKSIZE; z++)
138 for(s16 y=0; y<MAP_BLOCKSIZE; y++)
139 for(s16 x=0; x<MAP_BLOCKSIZE; x++)
143 MapNode n = data->m_vmanip.getNodeNoEx(blockpos_nodes+p);
144 const ContentFeatures &f = nodedef->get(n);
146 // Only solidness=0 stuff is drawn here
152 infostream<<"Got "<<f.drawtype<<std::endl;
160 Add water sources to mesh if using new style
162 TileSpec tile_liquid = f.special_tiles[0];
163 TileSpec tile_liquid_bfculled = getNodeTile(n, p, v3s16(0,0,0), data);
164 AtlasPointer &pa_liquid = tile_liquid.texture;
166 bool top_is_same_liquid = false;
167 MapNode ntop = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y+1,z));
168 content_t c_flowing = nodedef->getId(f.liquid_alternative_flowing);
169 content_t c_source = nodedef->getId(f.liquid_alternative_source);
170 if(ntop.getContent() == c_flowing || ntop.getContent() == c_source)
171 top_is_same_liquid = true;
173 u16 l = getInteriorLight(n, 0, data);
174 video::SColor c = MapBlock_LightColor(f.alpha, l, decode_light(f.light_source));
179 v3s16 side_dirs[4] = {
185 for(u32 i=0; i<4; i++)
187 v3s16 dir = side_dirs[i];
189 MapNode neighbor = data->m_vmanip.getNodeNoEx(blockpos_nodes + p + dir);
190 content_t neighbor_content = neighbor.getContent();
191 const ContentFeatures &n_feat = nodedef->get(neighbor_content);
192 MapNode n_top = data->m_vmanip.getNodeNoEx(blockpos_nodes + p + dir+ v3s16(0,1,0));
193 content_t n_top_c = n_top.getContent();
195 if(neighbor_content == CONTENT_IGNORE)
199 If our topside is liquid and neighbor's topside
200 is liquid, don't draw side face
202 if(top_is_same_liquid && (n_top_c == c_flowing ||
203 n_top_c == c_source || n_top_c == CONTENT_IGNORE))
206 // Don't draw face if neighbor is blocking the view
207 if(n_feat.solidness == 2)
210 bool neighbor_is_same_liquid = (neighbor_content == c_source
211 || neighbor_content == c_flowing);
213 // Don't draw any faces if neighbor same is liquid and top is
215 if(neighbor_is_same_liquid && !top_is_same_liquid)
218 // Use backface culled material if neighbor doesn't have a
220 const TileSpec *current_tile = &tile_liquid;
221 if(n_feat.solidness != 0 || n_feat.visual_solidness != 0)
222 current_tile = &tile_liquid_bfculled;
224 video::S3DVertex vertices[4] =
226 video::S3DVertex(-BS/2,0,BS/2,0,0,0, c,
227 pa_liquid.x0(), pa_liquid.y1()),
228 video::S3DVertex(BS/2,0,BS/2,0,0,0, c,
229 pa_liquid.x1(), pa_liquid.y1()),
230 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
231 pa_liquid.x1(), pa_liquid.y0()),
232 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
233 pa_liquid.x0(), pa_liquid.y0()),
237 If our topside is liquid, set upper border of face
238 at upper border of node
240 if(top_is_same_liquid)
242 vertices[2].Pos.Y = 0.5*BS;
243 vertices[3].Pos.Y = 0.5*BS;
246 Otherwise upper position of face is liquid level
250 vertices[2].Pos.Y = (node_liquid_level-0.5)*BS;
251 vertices[3].Pos.Y = (node_liquid_level-0.5)*BS;
254 If neighbor is liquid, lower border of face is liquid level
256 if(neighbor_is_same_liquid)
258 vertices[0].Pos.Y = (node_liquid_level-0.5)*BS;
259 vertices[1].Pos.Y = (node_liquid_level-0.5)*BS;
262 If neighbor is not liquid, lower border of face is
267 vertices[0].Pos.Y = -0.5*BS;
268 vertices[1].Pos.Y = -0.5*BS;
271 for(s32 j=0; j<4; j++)
273 if(dir == v3s16(0,0,1))
274 vertices[j].Pos.rotateXZBy(0);
275 if(dir == v3s16(0,0,-1))
276 vertices[j].Pos.rotateXZBy(180);
277 if(dir == v3s16(-1,0,0))
278 vertices[j].Pos.rotateXZBy(90);
279 if(dir == v3s16(1,0,-0))
280 vertices[j].Pos.rotateXZBy(-90);
282 // Do this to not cause glitches when two liquids are
284 /*if(neighbor_is_same_liquid == false){
285 vertices[j].Pos.X *= 0.98;
286 vertices[j].Pos.Z *= 0.98;
289 vertices[j].Pos += intToFloat(p, BS);
292 u16 indices[] = {0,1,2,2,3,0};
293 // Add to mesh collector
294 collector.append(*current_tile, vertices, 4, indices, 6);
300 if(top_is_same_liquid)
303 video::S3DVertex vertices[4] =
305 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
306 pa_liquid.x0(), pa_liquid.y1()),
307 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
308 pa_liquid.x1(), pa_liquid.y1()),
309 video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c,
310 pa_liquid.x1(), pa_liquid.y0()),
311 video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c,
312 pa_liquid.x0(), pa_liquid.y0()),
315 v3f offset(p.X*BS, p.Y*BS + (-0.5+node_liquid_level)*BS, p.Z*BS);
316 for(s32 i=0; i<4; i++)
318 vertices[i].Pos += offset;
321 u16 indices[] = {0,1,2,2,3,0};
322 // Add to mesh collector
323 collector.append(tile_liquid, vertices, 4, indices, 6);
325 case NDT_FLOWINGLIQUID:
328 Add flowing liquid to mesh
330 TileSpec tile_liquid = f.special_tiles[0];
331 TileSpec tile_liquid_bfculled = f.special_tiles[1];
332 AtlasPointer &pa_liquid = tile_liquid.texture;
334 bool top_is_same_liquid = false;
335 MapNode ntop = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y+1,z));
336 content_t c_flowing = nodedef->getId(f.liquid_alternative_flowing);
337 content_t c_source = nodedef->getId(f.liquid_alternative_source);
338 if(ntop.getContent() == c_flowing || ntop.getContent() == c_source)
339 top_is_same_liquid = true;
342 // If this liquid emits light and doesn't contain light, draw
343 // it at what it emits, for an increased effect
344 u8 light_source = nodedef->get(n).light_source;
345 if(light_source != 0){
346 //l = decode_light(undiminish_light(light_source));
347 l = decode_light(light_source);
350 // Use the light of the node on top if possible
351 else if(nodedef->get(ntop).param_type == CPT_LIGHT)
352 l = getInteriorLight(ntop, 0, data);
353 // Otherwise use the light of this node (the liquid)
355 l = getInteriorLight(n, 0, data);
356 video::SColor c = MapBlock_LightColor(f.alpha, l, decode_light(f.light_source));
358 // Neighbor liquid levels (key = relative position)
359 // Includes current node
360 std::map<v3s16, f32> neighbor_levels;
361 std::map<v3s16, content_t> neighbor_contents;
362 std::map<v3s16, u8> neighbor_flags;
363 const u8 neighborflag_top_is_same_liquid = 0x01;
364 v3s16 neighbor_dirs[9] = {
375 for(u32 i=0; i<9; i++)
377 content_t content = CONTENT_AIR;
378 float level = -0.5 * BS;
381 v3s16 p2 = p + neighbor_dirs[i];
382 MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
383 if(n2.getContent() != CONTENT_IGNORE)
385 content = n2.getContent();
387 if(n2.getContent() == c_source)
388 level = (-0.5+node_liquid_level) * BS;
389 else if(n2.getContent() == c_flowing)
390 level = (-0.5 + ((float)(n2.param2&LIQUID_LEVEL_MASK)
391 + 0.5) / (float)LIQUID_LEVEL_SOURCE * node_liquid_level) * BS;
393 // Check node above neighbor.
394 // NOTE: This doesn't get executed if neighbor
397 n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
398 if(n2.getContent() == c_source ||
399 n2.getContent() == c_flowing)
400 flags |= neighborflag_top_is_same_liquid;
403 neighbor_levels[neighbor_dirs[i]] = level;
404 neighbor_contents[neighbor_dirs[i]] = content;
405 neighbor_flags[neighbor_dirs[i]] = flags;
408 // Corner heights (average between four liquids)
409 f32 corner_levels[4];
411 v3s16 halfdirs[4] = {
417 for(u32 i=0; i<4; i++)
419 v3s16 cornerdir = halfdirs[i];
420 float cornerlevel = 0;
423 for(u32 j=0; j<4; j++)
425 v3s16 neighbordir = cornerdir - halfdirs[j];
426 content_t content = neighbor_contents[neighbordir];
427 // If top is liquid, draw starting from top of node
428 if(neighbor_flags[neighbordir] &
429 neighborflag_top_is_same_liquid)
431 cornerlevel = 0.5*BS;
435 // Source is always the same height
436 else if(content == c_source)
438 cornerlevel = (-0.5+node_liquid_level)*BS;
442 // Flowing liquid has level information
443 else if(content == c_flowing)
445 cornerlevel += neighbor_levels[neighbordir];
448 else if(content == CONTENT_AIR)
454 cornerlevel = -0.5*BS+0.2;
455 else if(valid_count > 0)
456 cornerlevel /= valid_count;
457 corner_levels[i] = cornerlevel;
464 v3s16 side_dirs[4] = {
470 s16 side_corners[4][2] = {
476 for(u32 i=0; i<4; i++)
478 v3s16 dir = side_dirs[i];
481 If our topside is liquid and neighbor's topside
482 is liquid, don't draw side face
484 if(top_is_same_liquid &&
485 neighbor_flags[dir] & neighborflag_top_is_same_liquid)
488 content_t neighbor_content = neighbor_contents[dir];
489 const ContentFeatures &n_feat = nodedef->get(neighbor_content);
491 // Don't draw face if neighbor is blocking the view
492 if(n_feat.solidness == 2)
495 bool neighbor_is_same_liquid = (neighbor_content == c_source
496 || neighbor_content == c_flowing);
498 // Don't draw any faces if neighbor same is liquid and top is
500 if(neighbor_is_same_liquid == true
501 && top_is_same_liquid == false)
504 // Use backface culled material if neighbor doesn't have a
506 const TileSpec *current_tile = &tile_liquid;
507 if(n_feat.solidness != 0 || n_feat.visual_solidness != 0)
508 current_tile = &tile_liquid_bfculled;
510 video::S3DVertex vertices[4] =
512 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
513 pa_liquid.x0(), pa_liquid.y1()),
514 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
515 pa_liquid.x1(), pa_liquid.y1()),
516 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
517 pa_liquid.x1(), pa_liquid.y0()),
518 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
519 pa_liquid.x0(), pa_liquid.y0()),
523 If our topside is liquid, set upper border of face
524 at upper border of node
526 if(top_is_same_liquid)
528 vertices[2].Pos.Y = 0.5*BS;
529 vertices[3].Pos.Y = 0.5*BS;
532 Otherwise upper position of face is corner levels
536 vertices[2].Pos.Y = corner_levels[side_corners[i][0]];
537 vertices[3].Pos.Y = corner_levels[side_corners[i][1]];
541 If neighbor is liquid, lower border of face is corner
544 if(neighbor_is_same_liquid)
546 vertices[0].Pos.Y = corner_levels[side_corners[i][1]];
547 vertices[1].Pos.Y = corner_levels[side_corners[i][0]];
550 If neighbor is not liquid, lower border of face is
555 vertices[0].Pos.Y = -0.5*BS;
556 vertices[1].Pos.Y = -0.5*BS;
559 for(s32 j=0; j<4; j++)
561 if(dir == v3s16(0,0,1))
562 vertices[j].Pos.rotateXZBy(0);
563 if(dir == v3s16(0,0,-1))
564 vertices[j].Pos.rotateXZBy(180);
565 if(dir == v3s16(-1,0,0))
566 vertices[j].Pos.rotateXZBy(90);
567 if(dir == v3s16(1,0,-0))
568 vertices[j].Pos.rotateXZBy(-90);
570 // Do this to not cause glitches when two liquids are
572 /*if(neighbor_is_same_liquid == false){
573 vertices[j].Pos.X *= 0.98;
574 vertices[j].Pos.Z *= 0.98;
577 vertices[j].Pos += intToFloat(p, BS);
580 u16 indices[] = {0,1,2,2,3,0};
581 // Add to mesh collector
582 collector.append(*current_tile, vertices, 4, indices, 6);
586 Generate top side, if appropriate
589 if(top_is_same_liquid == false)
591 video::S3DVertex vertices[4] =
593 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
594 pa_liquid.x0(), pa_liquid.y1()),
595 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
596 pa_liquid.x1(), pa_liquid.y1()),
597 video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c,
598 pa_liquid.x1(), pa_liquid.y0()),
599 video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c,
600 pa_liquid.x0(), pa_liquid.y0()),
603 // To get backface culling right, the vertices need to go
604 // clockwise around the front of the face. And we happened to
605 // calculate corner levels in exact reverse order.
606 s32 corner_resolve[4] = {3,2,1,0};
608 for(s32 i=0; i<4; i++)
610 //vertices[i].Pos.Y += liquid_level;
611 //vertices[i].Pos.Y += neighbor_levels[v3s16(0,0,0)];
612 s32 j = corner_resolve[i];
613 vertices[i].Pos.Y += corner_levels[j];
614 vertices[i].Pos += intToFloat(p, BS);
617 // Default downwards-flowing texture animation goes from
618 // -Z towards +Z, thus the direction is +Z.
619 // Rotate texture to make animation go in flow direction
620 // Positive if liquid moves towards +Z
621 int dz = (corner_levels[side_corners[3][0]] +
622 corner_levels[side_corners[3][1]]) -
623 (corner_levels[side_corners[2][0]] +
624 corner_levels[side_corners[2][1]]);
625 // Positive if liquid moves towards +X
626 int dx = (corner_levels[side_corners[1][0]] +
627 corner_levels[side_corners[1][1]]) -
628 (corner_levels[side_corners[0][0]] +
629 corner_levels[side_corners[0][1]]);
633 v2f t = vertices[0].TCoords;
634 vertices[0].TCoords = vertices[1].TCoords;
635 vertices[1].TCoords = vertices[2].TCoords;
636 vertices[2].TCoords = vertices[3].TCoords;
637 vertices[3].TCoords = t;
642 v2f t = vertices[0].TCoords;
643 vertices[0].TCoords = vertices[3].TCoords;
644 vertices[3].TCoords = vertices[2].TCoords;
645 vertices[2].TCoords = vertices[1].TCoords;
646 vertices[1].TCoords = t;
651 v2f t = vertices[0].TCoords;
652 vertices[0].TCoords = vertices[3].TCoords;
653 vertices[3].TCoords = vertices[2].TCoords;
654 vertices[2].TCoords = vertices[1].TCoords;
655 vertices[1].TCoords = t;
656 t = vertices[0].TCoords;
657 vertices[0].TCoords = vertices[3].TCoords;
658 vertices[3].TCoords = vertices[2].TCoords;
659 vertices[2].TCoords = vertices[1].TCoords;
660 vertices[1].TCoords = t;
663 u16 indices[] = {0,1,2,2,3,0};
664 // Add to mesh collector
665 collector.append(tile_liquid, vertices, 4, indices, 6);
670 TileSpec tile = getNodeTile(n, p, v3s16(0,0,0), data);
671 AtlasPointer ap = tile.texture;
673 u16 l = getInteriorLight(n, 1, data);
674 video::SColor c = MapBlock_LightColor(255, l, decode_light(f.light_source));
676 for(u32 j=0; j<6; j++)
678 // Check this neighbor
679 v3s16 n2p = blockpos_nodes + p + g_6dirs[j];
680 MapNode n2 = data->m_vmanip.getNodeNoEx(n2p);
681 // Don't make face if neighbor is of same type
682 if(n2.getContent() == n.getContent())
686 video::S3DVertex vertices[4] =
688 video::S3DVertex(-BS/2,-BS/2,BS/2, 0,0,0, c,
690 video::S3DVertex(BS/2,-BS/2,BS/2, 0,0,0, c,
692 video::S3DVertex(BS/2,BS/2,BS/2, 0,0,0, c,
694 video::S3DVertex(-BS/2,BS/2,BS/2, 0,0,0, c,
698 // Rotations in the g_6dirs format
700 for(u16 i=0; i<4; i++)
701 vertices[i].Pos.rotateXZBy(0);
702 else if(j == 1) // Y+
703 for(u16 i=0; i<4; i++)
704 vertices[i].Pos.rotateYZBy(-90);
705 else if(j == 2) // X+
706 for(u16 i=0; i<4; i++)
707 vertices[i].Pos.rotateXZBy(-90);
708 else if(j == 3) // Z-
709 for(u16 i=0; i<4; i++)
710 vertices[i].Pos.rotateXZBy(180);
711 else if(j == 4) // Y-
712 for(u16 i=0; i<4; i++)
713 vertices[i].Pos.rotateYZBy(90);
714 else if(j == 5) // X-
715 for(u16 i=0; i<4; i++)
716 vertices[i].Pos.rotateXZBy(90);
718 for(u16 i=0; i<4; i++){
719 vertices[i].Pos += intToFloat(p, BS);
722 u16 indices[] = {0,1,2,2,3,0};
723 // Add to mesh collector
724 collector.append(tile, vertices, 4, indices, 6);
729 TileSpec tile_leaves = getNodeTile(n, p,
731 AtlasPointer pa_leaves = tile_leaves.texture;
733 u16 l = getInteriorLight(n, 1, data);
734 video::SColor c = MapBlock_LightColor(255, l, decode_light(f.light_source));
736 v3f pos = intToFloat(p, BS);
737 aabb3f box(-BS/2,-BS/2,-BS/2,BS/2,BS/2,BS/2);
740 makeCuboid(&collector, box, &tile_leaves, 1, c, NULL);
742 case NDT_ALLFACES_OPTIONAL:
743 // This is always pre-converted to something else
748 v3s16 dir = n.getWallMountedDir(nodedef);
751 if(dir == v3s16(0,-1,0)){
752 tileindex = 0; // floor
753 } else if(dir == v3s16(0,1,0)){
754 tileindex = 1; // ceiling
755 // For backwards compatibility
756 } else if(dir == v3s16(0,0,0)){
757 tileindex = 0; // floor
759 tileindex = 2; // side
762 TileSpec tile = getNodeTileN(n, p, tileindex, data);
763 tile.material_flags &= ~MATERIAL_FLAG_BACKFACE_CULLING;
764 tile.material_flags |= MATERIAL_FLAG_CRACK_OVERLAY;
766 AtlasPointer ap = tile.texture;
768 u16 l = getInteriorLight(n, 1, data);
769 video::SColor c = MapBlock_LightColor(255, l, decode_light(f.light_source));
771 // Wall at X+ of node
772 video::S3DVertex vertices[4] =
774 video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c,
776 video::S3DVertex(BS/2,-BS/2,0, 0,0,0, c,
778 video::S3DVertex(BS/2,BS/2,0, 0,0,0, c,
780 video::S3DVertex(-BS/2,BS/2,0, 0,0,0, c,
784 for(s32 i=0; i<4; i++)
786 if(dir == v3s16(1,0,0))
787 vertices[i].Pos.rotateXZBy(0);
788 if(dir == v3s16(-1,0,0))
789 vertices[i].Pos.rotateXZBy(180);
790 if(dir == v3s16(0,0,1))
791 vertices[i].Pos.rotateXZBy(90);
792 if(dir == v3s16(0,0,-1))
793 vertices[i].Pos.rotateXZBy(-90);
794 if(dir == v3s16(0,-1,0))
795 vertices[i].Pos.rotateXZBy(45);
796 if(dir == v3s16(0,1,0))
797 vertices[i].Pos.rotateXZBy(-45);
799 vertices[i].Pos += intToFloat(p, BS);
802 u16 indices[] = {0,1,2,2,3,0};
803 // Add to mesh collector
804 collector.append(tile, vertices, 4, indices, 6);
808 TileSpec tile = getNodeTileN(n, p, 0, data);
809 tile.material_flags &= ~MATERIAL_FLAG_BACKFACE_CULLING;
810 tile.material_flags |= MATERIAL_FLAG_CRACK_OVERLAY;
811 AtlasPointer ap = tile.texture;
813 u16 l = getInteriorLight(n, 0, data);
814 video::SColor c = MapBlock_LightColor(255, l, decode_light(f.light_source));
816 float d = (float)BS/16;
817 // Wall at X+ of node
818 video::S3DVertex vertices[4] =
820 video::S3DVertex(BS/2-d,BS/2,BS/2, 0,0,0, c,
822 video::S3DVertex(BS/2-d,BS/2,-BS/2, 0,0,0, c,
824 video::S3DVertex(BS/2-d,-BS/2,-BS/2, 0,0,0, c,
826 video::S3DVertex(BS/2-d,-BS/2,BS/2, 0,0,0, c,
830 v3s16 dir = n.getWallMountedDir(nodedef);
832 for(s32 i=0; i<4; i++)
834 if(dir == v3s16(1,0,0))
835 vertices[i].Pos.rotateXZBy(0);
836 if(dir == v3s16(-1,0,0))
837 vertices[i].Pos.rotateXZBy(180);
838 if(dir == v3s16(0,0,1))
839 vertices[i].Pos.rotateXZBy(90);
840 if(dir == v3s16(0,0,-1))
841 vertices[i].Pos.rotateXZBy(-90);
842 if(dir == v3s16(0,-1,0))
843 vertices[i].Pos.rotateXYBy(-90);
844 if(dir == v3s16(0,1,0))
845 vertices[i].Pos.rotateXYBy(90);
847 vertices[i].Pos += intToFloat(p, BS);
850 u16 indices[] = {0,1,2,2,3,0};
851 // Add to mesh collector
852 collector.append(tile, vertices, 4, indices, 6);
856 TileSpec tile = getNodeTileN(n, p, 0, data);
857 tile.material_flags |= MATERIAL_FLAG_CRACK_OVERLAY;
858 AtlasPointer ap = tile.texture;
860 u16 l = getInteriorLight(n, 1, data);
861 video::SColor c = MapBlock_LightColor(255, l, decode_light(f.light_source));
863 for(u32 j=0; j<2; j++)
865 video::S3DVertex vertices[4] =
867 video::S3DVertex(-BS/2*f.visual_scale,-BS/2,0, 0,0,0, c,
869 video::S3DVertex( BS/2*f.visual_scale,-BS/2,0, 0,0,0, c,
871 video::S3DVertex( BS/2*f.visual_scale,
872 -BS/2 + f.visual_scale*BS,0, 0,0,0, c,
874 video::S3DVertex(-BS/2*f.visual_scale,
875 -BS/2 + f.visual_scale*BS,0, 0,0,0, c,
881 for(u16 i=0; i<4; i++)
882 vertices[i].Pos.rotateXZBy(45);
886 for(u16 i=0; i<4; i++)
887 vertices[i].Pos.rotateXZBy(-45);
890 for(u16 i=0; i<4; i++)
892 vertices[i].Pos *= f.visual_scale;
893 vertices[i].Pos += intToFloat(p, BS);
896 u16 indices[] = {0,1,2,2,3,0};
897 // Add to mesh collector
898 collector.append(tile, vertices, 4, indices, 6);
903 TileSpec tile = getNodeTile(n, p, v3s16(0,0,0), data);
904 TileSpec tile_nocrack = tile;
905 tile_nocrack.material_flags &= ~MATERIAL_FLAG_CRACK;
907 // A hack to put wood the right way around in the posts
908 ITextureSource *tsrc = data->m_gamedef->tsrc();
909 TileSpec tile_rot = tile;
910 tile_rot.texture = tsrc->getTexture(tsrc->getTextureName(
911 tile.texture.id) + "^[transformR90");
913 u16 l = getInteriorLight(n, 1, data);
914 video::SColor c = MapBlock_LightColor(255, l, decode_light(f.light_source));
916 const f32 post_rad=(f32)BS/8;
917 const f32 bar_rad=(f32)BS/16;
918 const f32 bar_len=(f32)(BS/2)-post_rad;
920 v3f pos = intToFloat(p, BS);
922 // The post - always present
923 aabb3f post(-post_rad,-BS/2,-post_rad,post_rad,BS/2,post_rad);
927 6/16.,6/16.,10/16.,10/16.,
928 6/16.,6/16.,10/16.,10/16.,
933 makeCuboid(&collector, post, &tile_rot, 1, c, postuv);
935 // Now a section of fence, +X, if there's a post there
938 MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
939 const ContentFeatures *f2 = &nodedef->get(n2);
940 if(f2->drawtype == NDT_FENCELIKE)
942 aabb3f bar(-bar_len+BS/2,-bar_rad+BS/4,-bar_rad,
943 bar_len+BS/2,bar_rad+BS/4,bar_rad);
947 0/16.,2/16.,16/16.,4/16.,
948 0/16.,4/16.,16/16.,6/16.,
949 6/16.,6/16.,8/16.,8/16.,
950 10/16.,10/16.,12/16.,12/16.,
951 0/16.,8/16.,16/16.,10/16.,
952 0/16.,14/16.,16/16.,16/16.};
953 makeCuboid(&collector, bar, &tile_nocrack, 1,
955 bar.MinEdge.Y -= BS/2;
956 bar.MaxEdge.Y -= BS/2;
957 makeCuboid(&collector, bar, &tile_nocrack, 1,
961 // Now a section of fence, +Z, if there's a post there
964 n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
965 f2 = &nodedef->get(n2);
966 if(f2->drawtype == NDT_FENCELIKE)
968 aabb3f bar(-bar_rad,-bar_rad+BS/4,-bar_len+BS/2,
969 bar_rad,bar_rad+BS/4,bar_len+BS/2);
973 3/16.,1/16.,5/16.,5/16., // cannot rotate; stretch
974 4/16.,1/16.,6/16.,5/16., // for wood texture instead
975 0/16.,9/16.,16/16.,11/16.,
976 0/16.,6/16.,16/16.,8/16.,
977 6/16.,6/16.,8/16.,8/16.,
978 10/16.,10/16.,12/16.,12/16.};
979 makeCuboid(&collector, bar, &tile_nocrack, 1,
981 bar.MinEdge.Y -= BS/2;
982 bar.MaxEdge.Y -= BS/2;
983 makeCuboid(&collector, bar, &tile_nocrack, 1,
989 bool is_rail_x [] = { false, false }; /* x-1, x+1 */
990 bool is_rail_z [] = { false, false }; /* z-1, z+1 */
992 bool is_rail_z_minus_y [] = { false, false }; /* z-1, z+1; y-1 */
993 bool is_rail_x_minus_y [] = { false, false }; /* x-1, z+1; y-1 */
994 bool is_rail_z_plus_y [] = { false, false }; /* z-1, z+1; y+1 */
995 bool is_rail_x_plus_y [] = { false, false }; /* x-1, x+1; y+1 */
997 MapNode n_minus_x = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x-1,y,z));
998 MapNode n_plus_x = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x+1,y,z));
999 MapNode n_minus_z = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y,z-1));
1000 MapNode n_plus_z = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y,z+1));
1001 MapNode n_plus_x_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x+1, y+1, z));
1002 MapNode n_plus_x_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x+1, y-1, z));
1003 MapNode n_minus_x_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x-1, y+1, z));
1004 MapNode n_minus_x_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x-1, y-1, z));
1005 MapNode n_plus_z_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y+1, z+1));
1006 MapNode n_minus_z_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y+1, z-1));
1007 MapNode n_plus_z_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y-1, z+1));
1008 MapNode n_minus_z_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y-1, z-1));
1010 content_t thiscontent = n.getContent();
1011 if(n_minus_x.getContent() == thiscontent)
1012 is_rail_x[0] = true;
1013 if (n_minus_x_minus_y.getContent() == thiscontent)
1014 is_rail_x_minus_y[0] = true;
1015 if(n_minus_x_plus_y.getContent() == thiscontent)
1016 is_rail_x_plus_y[0] = true;
1018 if(n_plus_x.getContent() == thiscontent)
1019 is_rail_x[1] = true;
1020 if (n_plus_x_minus_y.getContent() == thiscontent)
1021 is_rail_x_minus_y[1] = true;
1022 if(n_plus_x_plus_y.getContent() == thiscontent)
1023 is_rail_x_plus_y[1] = true;
1025 if(n_minus_z.getContent() == thiscontent)
1026 is_rail_z[0] = true;
1027 if (n_minus_z_minus_y.getContent() == thiscontent)
1028 is_rail_z_minus_y[0] = true;
1029 if(n_minus_z_plus_y.getContent() == thiscontent)
1030 is_rail_z_plus_y[0] = true;
1032 if(n_plus_z.getContent() == thiscontent)
1033 is_rail_z[1] = true;
1034 if (n_plus_z_minus_y.getContent() == thiscontent)
1035 is_rail_z_minus_y[1] = true;
1036 if(n_plus_z_plus_y.getContent() == thiscontent)
1037 is_rail_z_plus_y[1] = true;
1039 bool is_rail_x_all[] = {false, false};
1040 bool is_rail_z_all[] = {false, false};
1041 is_rail_x_all[0]=is_rail_x[0] || is_rail_x_minus_y[0] || is_rail_x_plus_y[0];
1042 is_rail_x_all[1]=is_rail_x[1] || is_rail_x_minus_y[1] || is_rail_x_plus_y[1];
1043 is_rail_z_all[0]=is_rail_z[0] || is_rail_z_minus_y[0] || is_rail_z_plus_y[0];
1044 is_rail_z_all[1]=is_rail_z[1] || is_rail_z_minus_y[1] || is_rail_z_plus_y[1];
1046 // reasonable default, flat straight unrotated rail
1047 bool is_straight = true;
1048 int adjacencies = 0;
1052 // check for sloped rail
1053 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])
1055 adjacencies = 5; //5 means sloped
1056 is_straight = true; // sloped is always straight
1060 // is really straight, rails on both sides
1061 is_straight = (is_rail_x_all[0] && is_rail_x_all[1]) || (is_rail_z_all[0] && is_rail_z_all[1]);
1062 adjacencies = is_rail_x_all[0] + is_rail_x_all[1] + is_rail_z_all[0] + is_rail_z_all[1];
1065 switch (adjacencies) {
1067 if(is_rail_x_all[0] || is_rail_x_all[1])
1072 tileindex = 1; // curved
1073 if(is_rail_x_all[0] && is_rail_x_all[1])
1075 if(is_rail_z_all[0] && is_rail_z_all[1]){
1076 if (n_minus_z_plus_y.getContent() == thiscontent) angle = 180;
1078 else if(is_rail_x_all[0] && is_rail_z_all[0])
1080 else if(is_rail_x_all[0] && is_rail_z_all[1])
1082 else if(is_rail_x_all[1] && is_rail_z_all[1])
1086 // here is where the potential to 'switch' a junction is, but not implemented at present
1087 tileindex = 2; // t-junction
1088 if(!is_rail_x_all[1])
1090 if(!is_rail_z_all[0])
1092 if(!is_rail_z_all[1])
1096 tileindex = 3; // crossing
1099 if(is_rail_z_plus_y[0])
1101 if(is_rail_x_plus_y[0])
1103 if(is_rail_x_plus_y[1])
1110 TileSpec tile = getNodeTileN(n, p, tileindex, data);
1111 tile.material_flags &= ~MATERIAL_FLAG_BACKFACE_CULLING;
1112 tile.material_flags |= MATERIAL_FLAG_CRACK_OVERLAY;
1114 AtlasPointer ap = tile.texture;
1116 u16 l = getInteriorLight(n, 0, data);
1117 video::SColor c = MapBlock_LightColor(255, l, decode_light(f.light_source));
1119 float d = (float)BS/64;
1122 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])
1123 g=1; //Object is at a slope
1125 video::S3DVertex vertices[4] =
1127 video::S3DVertex(-BS/2,-BS/2+d,-BS/2, 0,0,0, c,
1129 video::S3DVertex(BS/2,-BS/2+d,-BS/2, 0,0,0, c,
1131 video::S3DVertex(BS/2,g*BS/2+d,BS/2, 0,0,0, c,
1133 video::S3DVertex(-BS/2,g*BS/2+d,BS/2, 0,0,0, c,
1137 for(s32 i=0; i<4; i++)
1140 vertices[i].Pos.rotateXZBy(angle);
1141 vertices[i].Pos += intToFloat(p, BS);
1144 u16 indices[] = {0,1,2,2,3,0};
1145 collector.append(tile, vertices, 4, indices, 6);
1149 static const v3s16 tile_dirs[6] = {
1159 for(int i = 0; i < 6; i++)
1161 // Handles facedir rotation for textures
1162 tiles[i] = getNodeTile(n, p, tile_dirs[i], data);
1165 u16 l = getInteriorLight(n, 0, data);
1166 video::SColor c = MapBlock_LightColor(255, l, decode_light(f.light_source));
1168 v3f pos = intToFloat(p, BS);
1170 std::vector<aabb3f> boxes = n.getNodeBoxes(nodedef);
1171 for(std::vector<aabb3f>::iterator
1173 i != boxes.end(); i++)
1179 // Compute texture coords
1180 f32 tx1 = (i->MinEdge.X/BS)+0.5;
1181 f32 ty1 = (i->MinEdge.Y/BS)+0.5;
1182 f32 tz1 = (i->MinEdge.Z/BS)+0.5;
1183 f32 tx2 = (i->MaxEdge.X/BS)+0.5;
1184 f32 ty2 = (i->MaxEdge.Y/BS)+0.5;
1185 f32 tz2 = (i->MaxEdge.Z/BS)+0.5;
1188 tx1, 1-tz2, tx2, 1-tz1,
1192 tz1, 1-ty2, tz2, 1-ty1,
1194 1-tz2, 1-ty2, 1-tz1, 1-ty1,
1196 1-tx2, 1-ty2, 1-tx1, 1-ty1,
1198 tx1, 1-ty2, tx2, 1-ty1,
1201 makeCuboid(&collector, box, tiles, 6, c, txc);