3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU 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/serialize.h"
30 #include "util/directiontables.h"
33 // collector - the MeshCollector for the resulting polygons
34 // box - the position and size of the box
35 // tiles - the tiles (materials) to use (for all 6 faces)
36 // tilecount - number of entries in tiles, 1<=tilecount<=6
37 // c - vertex colour - used for all
38 // txc - texture coordinates - this is a list of texture coordinates
39 // for the opposite corners of each face - therefore, there
40 // should be (2+2)*6=24 values in the list. Alternatively, pass
41 // NULL to use the entire texture for each face. The order of
42 // the faces in the list is up-down-right-left-back-front
43 // (compatible with ContentFeatures). If you specified 0,0,1,1
44 // for each face, that would be the same as passing NULL.
45 void makeCuboid(MeshCollector *collector, const aabb3f &box,
46 const TileSpec *tiles, int tilecount,
47 video::SColor &c, const f32* txc)
49 assert(tilecount >= 1 && tilecount <= 6);
51 v3f min = box.MinEdge;
52 v3f max = box.MaxEdge;
56 static const f32 txc_default[24] = {
67 video::S3DVertex vertices[24] =
70 video::S3DVertex(min.X,max.Y,max.Z, 0,1,0, c, txc[0],txc[1]),
71 video::S3DVertex(max.X,max.Y,max.Z, 0,1,0, c, txc[2],txc[1]),
72 video::S3DVertex(max.X,max.Y,min.Z, 0,1,0, c, txc[2],txc[3]),
73 video::S3DVertex(min.X,max.Y,min.Z, 0,1,0, c, txc[0],txc[3]),
75 video::S3DVertex(min.X,min.Y,min.Z, 0,-1,0, c, txc[4],txc[5]),
76 video::S3DVertex(max.X,min.Y,min.Z, 0,-1,0, c, txc[6],txc[5]),
77 video::S3DVertex(max.X,min.Y,max.Z, 0,-1,0, c, txc[6],txc[7]),
78 video::S3DVertex(min.X,min.Y,max.Z, 0,-1,0, c, txc[4],txc[7]),
80 video::S3DVertex(max.X,max.Y,min.Z, 1,0,0, c, txc[ 8],txc[9]),
81 video::S3DVertex(max.X,max.Y,max.Z, 1,0,0, c, txc[10],txc[9]),
82 video::S3DVertex(max.X,min.Y,max.Z, 1,0,0, c, txc[10],txc[11]),
83 video::S3DVertex(max.X,min.Y,min.Z, 1,0,0, c, txc[ 8],txc[11]),
85 video::S3DVertex(min.X,max.Y,max.Z, -1,0,0, c, txc[12],txc[13]),
86 video::S3DVertex(min.X,max.Y,min.Z, -1,0,0, c, txc[14],txc[13]),
87 video::S3DVertex(min.X,min.Y,min.Z, -1,0,0, c, txc[14],txc[15]),
88 video::S3DVertex(min.X,min.Y,max.Z, -1,0,0, c, txc[12],txc[15]),
90 video::S3DVertex(max.X,max.Y,max.Z, 0,0,1, c, txc[16],txc[17]),
91 video::S3DVertex(min.X,max.Y,max.Z, 0,0,1, c, txc[18],txc[17]),
92 video::S3DVertex(min.X,min.Y,max.Z, 0,0,1, c, txc[18],txc[19]),
93 video::S3DVertex(max.X,min.Y,max.Z, 0,0,1, c, txc[16],txc[19]),
95 video::S3DVertex(min.X,max.Y,min.Z, 0,0,-1, c, txc[20],txc[21]),
96 video::S3DVertex(max.X,max.Y,min.Z, 0,0,-1, c, txc[22],txc[21]),
97 video::S3DVertex(max.X,min.Y,min.Z, 0,0,-1, c, txc[22],txc[23]),
98 video::S3DVertex(min.X,min.Y,min.Z, 0,0,-1, c, txc[20],txc[23]),
101 for(s32 j=0; j<24; j++)
103 int tileindex = MYMIN(j/4, tilecount-1);
104 vertices[j].TCoords *= tiles[tileindex].texture.size;
105 vertices[j].TCoords += tiles[tileindex].texture.pos;
108 u16 indices[] = {0,1,2,2,3,0};
110 // Add to mesh collector
111 for(s32 j=0; j<24; j+=4)
113 int tileindex = MYMIN(j/4, tilecount-1);
114 collector->append(tiles[tileindex],
115 vertices+j, 4, indices, 6);
119 void mapblock_mesh_generate_special(MeshMakeData *data,
120 MeshCollector &collector)
122 INodeDefManager *nodedef = data->m_gamedef->ndef();
125 //TimeTaker timer("mapblock_mesh_generate_special()");
130 bool new_style_water = g_settings->getBool("new_style_water");
132 float node_liquid_level = 1.0;
134 node_liquid_level = 0.85;
136 v3s16 blockpos_nodes = data->m_blockpos*MAP_BLOCKSIZE;
138 for(s16 z=0; z<MAP_BLOCKSIZE; z++)
139 for(s16 y=0; y<MAP_BLOCKSIZE; y++)
140 for(s16 x=0; x<MAP_BLOCKSIZE; x++)
144 MapNode n = data->m_vmanip.getNodeNoEx(blockpos_nodes+p);
145 const ContentFeatures &f = nodedef->get(n);
147 // Only solidness=0 stuff is drawn here
153 infostream<<"Got "<<f.drawtype<<std::endl;
161 Add water sources to mesh if using new style
163 TileSpec tile_liquid = f.special_tiles[0];
164 AtlasPointer &pa_liquid = tile_liquid.texture;
166 bool top_is_air = false;
167 MapNode n = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y+1,z));
168 if(n.getContent() == CONTENT_AIR)
171 if(top_is_air == false)
174 u16 l = getInteriorLight(n, 0, data);
175 video::SColor c = MapBlock_LightColor(f.alpha, l);
177 video::S3DVertex vertices[4] =
179 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
180 pa_liquid.x0(), pa_liquid.y1()),
181 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
182 pa_liquid.x1(), pa_liquid.y1()),
183 video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c,
184 pa_liquid.x1(), pa_liquid.y0()),
185 video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c,
186 pa_liquid.x0(), pa_liquid.y0()),
189 v3f offset(p.X, p.Y + (-0.5+node_liquid_level)*BS, p.Z);
190 for(s32 i=0; i<4; i++)
192 vertices[i].Pos += offset;
195 u16 indices[] = {0,1,2,2,3,0};
196 // Add to mesh collector
197 collector.append(tile_liquid, vertices, 4, indices, 6);
199 case NDT_FLOWINGLIQUID:
202 Add flowing liquid to mesh
204 TileSpec tile_liquid = f.special_tiles[0];
205 TileSpec tile_liquid_bfculled = f.special_tiles[1];
206 AtlasPointer &pa_liquid = tile_liquid.texture;
208 bool top_is_same_liquid = false;
209 MapNode ntop = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y+1,z));
210 content_t c_flowing = nodedef->getId(f.liquid_alternative_flowing);
211 content_t c_source = nodedef->getId(f.liquid_alternative_source);
212 if(ntop.getContent() == c_flowing || ntop.getContent() == c_source)
213 top_is_same_liquid = true;
216 // If this liquid emits light and doesn't contain light, draw
217 // it at what it emits, for an increased effect
218 u8 light_source = nodedef->get(n).light_source;
219 if(light_source != 0){
220 //l = decode_light(undiminish_light(light_source));
221 l = decode_light(light_source);
224 // Use the light of the node on top if possible
225 else if(nodedef->get(ntop).param_type == CPT_LIGHT)
226 l = getInteriorLight(ntop, 0, data);
227 // Otherwise use the light of this node (the liquid)
229 l = getInteriorLight(n, 0, data);
230 video::SColor c = MapBlock_LightColor(f.alpha, l);
232 // Neighbor liquid levels (key = relative position)
233 // Includes current node
234 core::map<v3s16, f32> neighbor_levels;
235 core::map<v3s16, content_t> neighbor_contents;
236 core::map<v3s16, u8> neighbor_flags;
237 const u8 neighborflag_top_is_same_liquid = 0x01;
238 v3s16 neighbor_dirs[9] = {
249 for(u32 i=0; i<9; i++)
251 content_t content = CONTENT_AIR;
252 float level = -0.5 * BS;
255 v3s16 p2 = p + neighbor_dirs[i];
256 MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
257 if(n2.getContent() != CONTENT_IGNORE)
259 content = n2.getContent();
261 if(n2.getContent() == c_source)
262 level = (-0.5+node_liquid_level) * BS;
263 else if(n2.getContent() == c_flowing)
264 level = (-0.5 + ((float)(n2.param2&LIQUID_LEVEL_MASK)
265 + 0.5) / 8.0 * node_liquid_level) * BS;
267 // Check node above neighbor.
268 // NOTE: This doesn't get executed if neighbor
271 n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
272 if(n2.getContent() == c_source ||
273 n2.getContent() == c_flowing)
274 flags |= neighborflag_top_is_same_liquid;
277 neighbor_levels.insert(neighbor_dirs[i], level);
278 neighbor_contents.insert(neighbor_dirs[i], content);
279 neighbor_flags.insert(neighbor_dirs[i], flags);
282 // Corner heights (average between four liquids)
283 f32 corner_levels[4];
285 v3s16 halfdirs[4] = {
291 for(u32 i=0; i<4; i++)
293 v3s16 cornerdir = halfdirs[i];
294 float cornerlevel = 0;
297 for(u32 j=0; j<4; j++)
299 v3s16 neighbordir = cornerdir - halfdirs[j];
300 content_t content = neighbor_contents[neighbordir];
301 // If top is liquid, draw starting from top of node
302 if(neighbor_flags[neighbordir] &
303 neighborflag_top_is_same_liquid)
305 cornerlevel = 0.5*BS;
309 // Source is always the same height
310 else if(content == c_source)
312 cornerlevel = (-0.5+node_liquid_level)*BS;
316 // Flowing liquid has level information
317 else if(content == c_flowing)
319 cornerlevel += neighbor_levels[neighbordir];
322 else if(content == CONTENT_AIR)
328 cornerlevel = -0.5*BS;
329 else if(valid_count > 0)
330 cornerlevel /= valid_count;
331 corner_levels[i] = cornerlevel;
338 v3s16 side_dirs[4] = {
344 s16 side_corners[4][2] = {
350 for(u32 i=0; i<4; i++)
352 v3s16 dir = side_dirs[i];
355 If our topside is liquid and neighbor's topside
356 is liquid, don't draw side face
358 if(top_is_same_liquid &&
359 neighbor_flags[dir] & neighborflag_top_is_same_liquid)
362 content_t neighbor_content = neighbor_contents[dir];
363 const ContentFeatures &n_feat = nodedef->get(neighbor_content);
365 // Don't draw face if neighbor is blocking the view
366 if(n_feat.solidness == 2)
369 bool neighbor_is_same_liquid = (neighbor_content == c_source
370 || neighbor_content == c_flowing);
372 // Don't draw any faces if neighbor same is liquid and top is
374 if(neighbor_is_same_liquid == true
375 && top_is_same_liquid == false)
378 // Use backface culled material if neighbor doesn't have a
380 const TileSpec *current_tile = &tile_liquid;
381 if(n_feat.solidness != 0 || n_feat.visual_solidness != 0)
382 current_tile = &tile_liquid_bfculled;
384 video::S3DVertex vertices[4] =
386 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
387 pa_liquid.x0(), pa_liquid.y1()),
388 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
389 pa_liquid.x1(), pa_liquid.y1()),
390 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
391 pa_liquid.x1(), pa_liquid.y0()),
392 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
393 pa_liquid.x0(), pa_liquid.y0()),
397 If our topside is liquid, set upper border of face
398 at upper border of node
400 if(top_is_same_liquid)
402 vertices[2].Pos.Y = 0.5*BS;
403 vertices[3].Pos.Y = 0.5*BS;
406 Otherwise upper position of face is corner levels
410 vertices[2].Pos.Y = corner_levels[side_corners[i][0]];
411 vertices[3].Pos.Y = corner_levels[side_corners[i][1]];
415 If neighbor is liquid, lower border of face is corner
418 if(neighbor_is_same_liquid)
420 vertices[0].Pos.Y = corner_levels[side_corners[i][1]];
421 vertices[1].Pos.Y = corner_levels[side_corners[i][0]];
424 If neighbor is not liquid, lower border of face is
429 vertices[0].Pos.Y = -0.5*BS;
430 vertices[1].Pos.Y = -0.5*BS;
433 for(s32 j=0; j<4; j++)
435 if(dir == v3s16(0,0,1))
436 vertices[j].Pos.rotateXZBy(0);
437 if(dir == v3s16(0,0,-1))
438 vertices[j].Pos.rotateXZBy(180);
439 if(dir == v3s16(-1,0,0))
440 vertices[j].Pos.rotateXZBy(90);
441 if(dir == v3s16(1,0,-0))
442 vertices[j].Pos.rotateXZBy(-90);
444 // Do this to not cause glitches when two liquids are
446 /*if(neighbor_is_same_liquid == false){
447 vertices[j].Pos.X *= 0.98;
448 vertices[j].Pos.Z *= 0.98;
451 vertices[j].Pos += intToFloat(p, BS);
454 u16 indices[] = {0,1,2,2,3,0};
455 // Add to mesh collector
456 collector.append(*current_tile, vertices, 4, indices, 6);
460 Generate top side, if appropriate
463 if(top_is_same_liquid == false)
465 video::S3DVertex vertices[4] =
467 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
468 pa_liquid.x0(), pa_liquid.y1()),
469 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
470 pa_liquid.x1(), pa_liquid.y1()),
471 video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c,
472 pa_liquid.x1(), pa_liquid.y0()),
473 video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c,
474 pa_liquid.x0(), pa_liquid.y0()),
477 // To get backface culling right, the vertices need to go
478 // clockwise around the front of the face. And we happened to
479 // calculate corner levels in exact reverse order.
480 s32 corner_resolve[4] = {3,2,1,0};
482 for(s32 i=0; i<4; i++)
484 //vertices[i].Pos.Y += liquid_level;
485 //vertices[i].Pos.Y += neighbor_levels[v3s16(0,0,0)];
486 s32 j = corner_resolve[i];
487 vertices[i].Pos.Y += corner_levels[j];
488 vertices[i].Pos += intToFloat(p, BS);
491 // Default downwards-flowing texture animation goes from
492 // -Z towards +Z, thus the direction is +Z.
493 // Rotate texture to make animation go in flow direction
494 // Positive if liquid moves towards +Z
495 int dz = (corner_levels[side_corners[2][0]] +
496 corner_levels[side_corners[2][1]] <
497 corner_levels[side_corners[3][0]] +
498 corner_levels[side_corners[3][1]]);
499 // Positive if liquid moves towards +X
500 int dx = (corner_levels[side_corners[0][0]] +
501 corner_levels[side_corners[0][1]] <
502 corner_levels[side_corners[1][0]] +
503 corner_levels[side_corners[1][1]]);
507 v2f t = vertices[0].TCoords;
508 vertices[0].TCoords = vertices[1].TCoords;
509 vertices[1].TCoords = vertices[2].TCoords;
510 vertices[2].TCoords = vertices[3].TCoords;
511 vertices[3].TCoords = t;
516 v2f t = vertices[0].TCoords;
517 vertices[0].TCoords = vertices[3].TCoords;
518 vertices[3].TCoords = vertices[2].TCoords;
519 vertices[2].TCoords = vertices[1].TCoords;
520 vertices[1].TCoords = t;
525 v2f t = vertices[0].TCoords;
526 vertices[0].TCoords = vertices[3].TCoords;
527 vertices[3].TCoords = vertices[2].TCoords;
528 vertices[2].TCoords = vertices[1].TCoords;
529 vertices[1].TCoords = t;
530 t = vertices[0].TCoords;
531 vertices[0].TCoords = vertices[3].TCoords;
532 vertices[3].TCoords = vertices[2].TCoords;
533 vertices[2].TCoords = vertices[1].TCoords;
534 vertices[1].TCoords = t;
537 u16 indices[] = {0,1,2,2,3,0};
538 // Add to mesh collector
539 collector.append(tile_liquid, vertices, 4, indices, 6);
544 TileSpec tile = getNodeTile(n, p, v3s16(0,0,0), data);
545 AtlasPointer ap = tile.texture;
547 u16 l = getInteriorLight(n, 1, data);
548 video::SColor c = MapBlock_LightColor(255, l);
550 for(u32 j=0; j<6; j++)
552 // Check this neighbor
553 v3s16 n2p = blockpos_nodes + p + g_6dirs[j];
554 MapNode n2 = data->m_vmanip.getNodeNoEx(n2p);
555 // Don't make face if neighbor is of same type
556 if(n2.getContent() == n.getContent())
560 video::S3DVertex vertices[4] =
562 video::S3DVertex(-BS/2,-BS/2,BS/2, 0,0,0, c,
564 video::S3DVertex(BS/2,-BS/2,BS/2, 0,0,0, c,
566 video::S3DVertex(BS/2,BS/2,BS/2, 0,0,0, c,
568 video::S3DVertex(-BS/2,BS/2,BS/2, 0,0,0, c,
572 // Rotations in the g_6dirs format
574 for(u16 i=0; i<4; i++)
575 vertices[i].Pos.rotateXZBy(0);
576 else if(j == 1) // Y+
577 for(u16 i=0; i<4; i++)
578 vertices[i].Pos.rotateYZBy(-90);
579 else if(j == 2) // X+
580 for(u16 i=0; i<4; i++)
581 vertices[i].Pos.rotateXZBy(-90);
582 else if(j == 3) // Z-
583 for(u16 i=0; i<4; i++)
584 vertices[i].Pos.rotateXZBy(180);
585 else if(j == 4) // Y-
586 for(u16 i=0; i<4; i++)
587 vertices[i].Pos.rotateYZBy(90);
588 else if(j == 5) // X-
589 for(u16 i=0; i<4; i++)
590 vertices[i].Pos.rotateXZBy(90);
592 for(u16 i=0; i<4; i++){
593 vertices[i].Pos += intToFloat(p, BS);
596 u16 indices[] = {0,1,2,2,3,0};
597 // Add to mesh collector
598 collector.append(tile, vertices, 4, indices, 6);
603 TileSpec tile_leaves = getNodeTile(n, p,
605 AtlasPointer pa_leaves = tile_leaves.texture;
607 u16 l = getInteriorLight(n, 1, data);
608 video::SColor c = MapBlock_LightColor(255, l);
610 v3f pos = intToFloat(p, BS);
611 aabb3f box(-BS/2,-BS/2,-BS/2,BS/2,BS/2,BS/2);
614 makeCuboid(&collector, box, &tile_leaves, 1, c, NULL);
616 case NDT_ALLFACES_OPTIONAL:
617 // This is always pre-converted to something else
622 v3s16 dir = n.getWallMountedDir(nodedef);
625 if(dir == v3s16(0,-1,0)){
626 tileindex = 0; // floor
627 } else if(dir == v3s16(0,1,0)){
628 tileindex = 1; // ceiling
629 // For backwards compatibility
630 } else if(dir == v3s16(0,0,0)){
631 tileindex = 0; // floor
633 tileindex = 2; // side
636 TileSpec tile = getNodeTileN(n, p, tileindex, data);
637 tile.material_flags &= ~MATERIAL_FLAG_BACKFACE_CULLING;
638 tile.material_flags |= MATERIAL_FLAG_CRACK_OVERLAY;
640 AtlasPointer ap = tile.texture;
642 video::SColor c(255,255,255,255);
644 // Wall at X+ of node
645 video::S3DVertex vertices[4] =
647 video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c,
649 video::S3DVertex(BS/2,-BS/2,0, 0,0,0, c,
651 video::S3DVertex(BS/2,BS/2,0, 0,0,0, c,
653 video::S3DVertex(-BS/2,BS/2,0, 0,0,0, c,
657 for(s32 i=0; i<4; i++)
659 if(dir == v3s16(1,0,0))
660 vertices[i].Pos.rotateXZBy(0);
661 if(dir == v3s16(-1,0,0))
662 vertices[i].Pos.rotateXZBy(180);
663 if(dir == v3s16(0,0,1))
664 vertices[i].Pos.rotateXZBy(90);
665 if(dir == v3s16(0,0,-1))
666 vertices[i].Pos.rotateXZBy(-90);
667 if(dir == v3s16(0,-1,0))
668 vertices[i].Pos.rotateXZBy(45);
669 if(dir == v3s16(0,1,0))
670 vertices[i].Pos.rotateXZBy(-45);
672 vertices[i].Pos += intToFloat(p, BS);
675 u16 indices[] = {0,1,2,2,3,0};
676 // Add to mesh collector
677 collector.append(tile, vertices, 4, indices, 6);
681 TileSpec tile = getNodeTileN(n, p, 0, data);
682 tile.material_flags &= ~MATERIAL_FLAG_BACKFACE_CULLING;
683 tile.material_flags |= MATERIAL_FLAG_CRACK_OVERLAY;
684 AtlasPointer ap = tile.texture;
686 u16 l = getInteriorLight(n, 0, data);
687 video::SColor c = MapBlock_LightColor(255, l);
689 float d = (float)BS/16;
690 // Wall at X+ of node
691 video::S3DVertex vertices[4] =
693 video::S3DVertex(BS/2-d,BS/2,BS/2, 0,0,0, c,
695 video::S3DVertex(BS/2-d,BS/2,-BS/2, 0,0,0, c,
697 video::S3DVertex(BS/2-d,-BS/2,-BS/2, 0,0,0, c,
699 video::S3DVertex(BS/2-d,-BS/2,BS/2, 0,0,0, c,
703 v3s16 dir = n.getWallMountedDir(nodedef);
705 for(s32 i=0; i<4; i++)
707 if(dir == v3s16(1,0,0))
708 vertices[i].Pos.rotateXZBy(0);
709 if(dir == v3s16(-1,0,0))
710 vertices[i].Pos.rotateXZBy(180);
711 if(dir == v3s16(0,0,1))
712 vertices[i].Pos.rotateXZBy(90);
713 if(dir == v3s16(0,0,-1))
714 vertices[i].Pos.rotateXZBy(-90);
715 if(dir == v3s16(0,-1,0))
716 vertices[i].Pos.rotateXYBy(-90);
717 if(dir == v3s16(0,1,0))
718 vertices[i].Pos.rotateXYBy(90);
720 vertices[i].Pos += intToFloat(p, BS);
723 u16 indices[] = {0,1,2,2,3,0};
724 // Add to mesh collector
725 collector.append(tile, vertices, 4, indices, 6);
729 TileSpec tile = getNodeTileN(n, p, 0, data);
730 tile.material_flags |= MATERIAL_FLAG_CRACK_OVERLAY;
731 AtlasPointer ap = tile.texture;
733 u16 l = getInteriorLight(n, 1, data);
734 video::SColor c = MapBlock_LightColor(255, l);
736 for(u32 j=0; j<4; j++)
738 video::S3DVertex vertices[4] =
740 video::S3DVertex(-BS/2*f.visual_scale,-BS/2,0, 0,0,0, c,
742 video::S3DVertex( BS/2*f.visual_scale,-BS/2,0, 0,0,0, c,
744 video::S3DVertex( BS/2*f.visual_scale,
745 -BS/2 + f.visual_scale*BS,0, 0,0,0, c,
747 video::S3DVertex(-BS/2*f.visual_scale,
748 -BS/2 + f.visual_scale*BS,0, 0,0,0, c,
754 for(u16 i=0; i<4; i++)
755 vertices[i].Pos.rotateXZBy(45);
759 for(u16 i=0; i<4; i++)
760 vertices[i].Pos.rotateXZBy(-45);
764 for(u16 i=0; i<4; i++)
765 vertices[i].Pos.rotateXZBy(135);
769 for(u16 i=0; i<4; i++)
770 vertices[i].Pos.rotateXZBy(-135);
773 for(u16 i=0; i<4; i++)
775 vertices[i].Pos *= f.visual_scale;
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 = getNodeTile(n, p, v3s16(0,0,0), data);
787 TileSpec tile_nocrack = tile;
788 tile_nocrack.material_flags &= ~MATERIAL_FLAG_CRACK;
790 // A hack to put wood the right way around in the posts
791 ITextureSource *tsrc = data->m_gamedef->tsrc();
792 TileSpec tile_rot = tile;
793 tile_rot.texture = tsrc->getTexture(tsrc->getTextureName(
794 tile.texture.id) + "^[transformR90");
796 u16 l = getInteriorLight(n, 1, data);
797 video::SColor c = MapBlock_LightColor(255, l);
799 const f32 post_rad=(f32)BS/8;
800 const f32 bar_rad=(f32)BS/16;
801 const f32 bar_len=(f32)(BS/2)-post_rad;
803 v3f pos = intToFloat(p, BS);
805 // The post - always present
806 aabb3f post(-post_rad,-BS/2,-post_rad,post_rad,BS/2,post_rad);
810 6/16.,6/16.,10/16.,10/16.,
811 6/16.,6/16.,10/16.,10/16.,
816 makeCuboid(&collector, post, &tile_rot, 1, c, postuv);
818 // Now a section of fence, +X, if there's a post there
821 MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
822 const ContentFeatures *f2 = &nodedef->get(n2);
823 if(f2->drawtype == NDT_FENCELIKE)
825 aabb3f bar(-bar_len+BS/2,-bar_rad+BS/4,-bar_rad,
826 bar_len+BS/2,bar_rad+BS/4,bar_rad);
830 0/16.,2/16.,16/16.,4/16.,
831 0/16.,4/16.,16/16.,6/16.,
832 6/16.,6/16.,8/16.,8/16.,
833 10/16.,10/16.,12/16.,12/16.,
834 0/16.,8/16.,16/16.,10/16.,
835 0/16.,14/16.,16/16.,16/16.};
836 makeCuboid(&collector, bar, &tile_nocrack, 1,
838 bar.MinEdge.Y -= BS/2;
839 bar.MaxEdge.Y -= BS/2;
840 makeCuboid(&collector, bar, &tile_nocrack, 1,
844 // Now a section of fence, +Z, if there's a post there
847 n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
848 f2 = &nodedef->get(n2);
849 if(f2->drawtype == NDT_FENCELIKE)
851 aabb3f bar(-bar_rad,-bar_rad+BS/4,-bar_len+BS/2,
852 bar_rad,bar_rad+BS/4,bar_len+BS/2);
856 3/16.,1/16.,5/16.,5/16., // cannot rotate; stretch
857 4/16.,1/16.,6/16.,5/16., // for wood texture instead
858 0/16.,9/16.,16/16.,11/16.,
859 0/16.,6/16.,16/16.,8/16.,
860 6/16.,6/16.,8/16.,8/16.,
861 10/16.,10/16.,12/16.,12/16.};
862 makeCuboid(&collector, bar, &tile_nocrack, 1,
864 bar.MinEdge.Y -= BS/2;
865 bar.MaxEdge.Y -= BS/2;
866 makeCuboid(&collector, bar, &tile_nocrack, 1,
872 bool is_rail_x [] = { false, false }; /* x-1, x+1 */
873 bool is_rail_z [] = { false, false }; /* z-1, z+1 */
875 bool is_rail_z_minus_y [] = { false, false }; /* z-1, z+1; y-1 */
876 bool is_rail_x_minus_y [] = { false, false }; /* x-1, z+1; y-1 */
877 bool is_rail_z_plus_y [] = { false, false }; /* z-1, z+1; y+1 */
878 bool is_rail_x_plus_y [] = { false, false }; /* x-1, x+1; y+1 */
880 MapNode n_minus_x = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x-1,y,z));
881 MapNode n_plus_x = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x+1,y,z));
882 MapNode n_minus_z = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y,z-1));
883 MapNode n_plus_z = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y,z+1));
884 MapNode n_plus_x_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x+1, y+1, z));
885 MapNode n_plus_x_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x+1, y-1, z));
886 MapNode n_minus_x_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x-1, y+1, z));
887 MapNode n_minus_x_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x-1, y-1, z));
888 MapNode n_plus_z_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y+1, z+1));
889 MapNode n_minus_z_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y+1, z-1));
890 MapNode n_plus_z_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y-1, z+1));
891 MapNode n_minus_z_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y-1, z-1));
893 content_t thiscontent = n.getContent();
894 if(n_minus_x.getContent() == thiscontent)
896 if (n_minus_x_minus_y.getContent() == thiscontent)
897 is_rail_x_minus_y[0] = true;
898 if(n_minus_x_plus_y.getContent() == thiscontent)
899 is_rail_x_plus_y[0] = true;
901 if(n_plus_x.getContent() == thiscontent)
903 if (n_plus_x_minus_y.getContent() == thiscontent)
904 is_rail_x_minus_y[1] = true;
905 if(n_plus_x_plus_y.getContent() == thiscontent)
906 is_rail_x_plus_y[1] = true;
908 if(n_minus_z.getContent() == thiscontent)
910 if (n_minus_z_minus_y.getContent() == thiscontent)
911 is_rail_z_minus_y[0] = true;
912 if(n_minus_z_plus_y.getContent() == thiscontent)
913 is_rail_z_plus_y[0] = true;
915 if(n_plus_z.getContent() == thiscontent)
917 if (n_plus_z_minus_y.getContent() == thiscontent)
918 is_rail_z_minus_y[1] = true;
919 if(n_plus_z_plus_y.getContent() == thiscontent)
920 is_rail_z_plus_y[1] = true;
922 bool is_rail_x_all[] = {false, false};
923 bool is_rail_z_all[] = {false, false};
924 is_rail_x_all[0]=is_rail_x[0] || is_rail_x_minus_y[0] || is_rail_x_plus_y[0];
925 is_rail_x_all[1]=is_rail_x[1] || is_rail_x_minus_y[1] || is_rail_x_plus_y[1];
926 is_rail_z_all[0]=is_rail_z[0] || is_rail_z_minus_y[0] || is_rail_z_plus_y[0];
927 is_rail_z_all[1]=is_rail_z[1] || is_rail_z_minus_y[1] || is_rail_z_plus_y[1];
929 // reasonable default, flat straight unrotated rail
930 bool is_straight = true;
935 // check for sloped rail
936 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])
938 adjacencies = 5; //5 means sloped
939 is_straight = true; // sloped is always straight
943 // is really straight, rails on both sides
944 is_straight = (is_rail_x_all[0] && is_rail_x_all[1]) || (is_rail_z_all[0] && is_rail_z_all[1]);
945 adjacencies = is_rail_x_all[0] + is_rail_x_all[1] + is_rail_z_all[0] + is_rail_z_all[1];
948 switch (adjacencies) {
950 if(is_rail_x_all[0] || is_rail_x_all[1])
955 tileindex = 1; // curved
956 if(is_rail_x_all[0] && is_rail_x_all[1])
958 if(is_rail_z_all[0] && is_rail_z_all[1]){
959 if (n_minus_z_plus_y.getContent() == thiscontent) angle = 180;
961 else if(is_rail_x_all[0] && is_rail_z_all[0])
963 else if(is_rail_x_all[0] && is_rail_z_all[1])
965 else if(is_rail_x_all[1] && is_rail_z_all[1])
969 // here is where the potential to 'switch' a junction is, but not implemented at present
970 tileindex = 2; // t-junction
971 if(!is_rail_x_all[1])
973 if(!is_rail_z_all[0])
975 if(!is_rail_z_all[1])
979 tileindex = 3; // crossing
982 if(is_rail_z_plus_y[0])
984 if(is_rail_x_plus_y[0])
986 if(is_rail_x_plus_y[1])
993 TileSpec tile = getNodeTileN(n, p, tileindex, data);
994 tile.material_flags &= ~MATERIAL_FLAG_BACKFACE_CULLING;
995 tile.material_flags |= MATERIAL_FLAG_CRACK_OVERLAY;
997 AtlasPointer ap = tile.texture;
999 u16 l = getInteriorLight(n, 0, data);
1000 video::SColor c = MapBlock_LightColor(255, l);
1002 float d = (float)BS/64;
1005 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])
1006 g=1; //Object is at a slope
1008 video::S3DVertex vertices[4] =
1010 video::S3DVertex(-BS/2,-BS/2+d,-BS/2, 0,0,0, c,
1012 video::S3DVertex(BS/2,-BS/2+d,-BS/2, 0,0,0, c,
1014 video::S3DVertex(BS/2,g*BS/2+d,BS/2, 0,0,0, c,
1016 video::S3DVertex(-BS/2,g*BS/2+d,BS/2, 0,0,0, c,
1020 for(s32 i=0; i<4; i++)
1023 vertices[i].Pos.rotateXZBy(angle);
1024 vertices[i].Pos += intToFloat(p, BS);
1027 u16 indices[] = {0,1,2,2,3,0};
1028 collector.append(tile, vertices, 4, indices, 6);