3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "content_mapblock.h"
22 #include "main.h" // For g_settings
23 #include "mapblock_mesh.h" // For MapBlock_LightColor() and MeshCollector
30 // collector - the MeshCollector for the resulting polygons
31 // box - the position and size of the box
32 // materials - the materials to use (for all 6 faces)
33 // pa - texture atlas pointers for the materials
34 // matcount - number of entries in "materials" and "pa", 1<=matcount<=6
35 // c - vertex colour - used for all
36 // txc - texture coordinates - this is a list of texture coordinates
37 // for the opposite corners of each face - therefore, there
38 // should be (2+2)*6=24 values in the list. Alternatively, pass
39 // NULL to use the entire texture for each face. The order of
40 // the faces in the list is up-down-right-left-back-front
41 // (compatible with ContentFeatures). If you specified 0,0,1,1
42 // for each face, that would be the same as passing NULL.
43 void makeCuboid(MeshCollector *collector, const aabb3f &box,
44 const video::SMaterial *materials, const AtlasPointer *pa, int matcount,
45 video::SColor &c, const f32* txc)
47 assert(matcount >= 1);
49 v3f min = box.MinEdge;
50 v3f max = box.MaxEdge;
54 static const f32 txc_default[24] = {
65 video::S3DVertex vertices[24] =
68 video::S3DVertex(min.X,max.Y,max.Z, 0,1,0, c, txc[0],txc[1]),
69 video::S3DVertex(max.X,max.Y,max.Z, 0,1,0, c, txc[2],txc[1]),
70 video::S3DVertex(max.X,max.Y,min.Z, 0,1,0, c, txc[2],txc[3]),
71 video::S3DVertex(min.X,max.Y,min.Z, 0,1,0, c, txc[0],txc[3]),
73 video::S3DVertex(min.X,min.Y,min.Z, 0,-1,0, c, txc[4],txc[5]),
74 video::S3DVertex(max.X,min.Y,min.Z, 0,-1,0, c, txc[6],txc[5]),
75 video::S3DVertex(max.X,min.Y,max.Z, 0,-1,0, c, txc[6],txc[7]),
76 video::S3DVertex(min.X,min.Y,max.Z, 0,-1,0, c, txc[4],txc[7]),
78 video::S3DVertex(max.X,max.Y,min.Z, 1,0,0, c, txc[ 8],txc[9]),
79 video::S3DVertex(max.X,max.Y,max.Z, 1,0,0, c, txc[10],txc[9]),
80 video::S3DVertex(max.X,min.Y,max.Z, 1,0,0, c, txc[10],txc[11]),
81 video::S3DVertex(max.X,min.Y,min.Z, 1,0,0, c, txc[ 8],txc[11]),
83 video::S3DVertex(min.X,max.Y,max.Z, -1,0,0, c, txc[12],txc[13]),
84 video::S3DVertex(min.X,max.Y,min.Z, -1,0,0, c, txc[14],txc[13]),
85 video::S3DVertex(min.X,min.Y,min.Z, -1,0,0, c, txc[14],txc[15]),
86 video::S3DVertex(min.X,min.Y,max.Z, -1,0,0, c, txc[12],txc[15]),
88 video::S3DVertex(min.X,max.Y,min.Z, 0,0,-1, c, txc[16],txc[17]),
89 video::S3DVertex(max.X,max.Y,min.Z, 0,0,-1, c, txc[18],txc[17]),
90 video::S3DVertex(max.X,min.Y,min.Z, 0,0,-1, c, txc[18],txc[19]),
91 video::S3DVertex(min.X,min.Y,min.Z, 0,0,-1, c, txc[16],txc[19]),
93 video::S3DVertex(max.X,max.Y,max.Z, 0,0,1, c, txc[20],txc[21]),
94 video::S3DVertex(min.X,max.Y,max.Z, 0,0,1, c, txc[22],txc[21]),
95 video::S3DVertex(min.X,min.Y,max.Z, 0,0,1, c, txc[22],txc[23]),
96 video::S3DVertex(max.X,min.Y,max.Z, 0,0,1, c, txc[20],txc[23]),
99 for(s32 j=0; j<24; j++)
101 int matindex = MYMIN(j/4, matcount-1);
102 vertices[j].TCoords *= pa[matindex].size;
103 vertices[j].TCoords += pa[matindex].pos;
106 u16 indices[] = {0,1,2,2,3,0};
108 // Add to mesh collector
109 for(s32 j=0; j<24; j+=4)
111 int matindex = MYMIN(j/4, matcount-1);
112 collector->append(materials[matindex],
113 vertices+j, 4, indices, 6);
117 void mapblock_mesh_generate_special(MeshMakeData *data,
118 MeshCollector &collector, IGameDef *gamedef)
120 INodeDefManager *nodedef = gamedef->ndef();
121 ITextureSource *tsrc = gamedef->getTextureSource();
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 /*// General ground material for special output
138 // Texture is modified just before usage
139 video::SMaterial material_general;
140 material_general.setFlag(video::EMF_LIGHTING, false);
141 material_general.setFlag(video::EMF_BILINEAR_FILTER, false);
142 material_general.setFlag(video::EMF_FOG_ENABLE, true);
143 material_general.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;*/
145 for(s16 z=0; z<MAP_BLOCKSIZE; z++)
146 for(s16 y=0; y<MAP_BLOCKSIZE; y++)
147 for(s16 x=0; x<MAP_BLOCKSIZE; x++)
151 MapNode n = data->m_vmanip.getNodeNoEx(blockpos_nodes+p);
152 const ContentFeatures &f = nodedef->get(n);
154 // Only solidness=0 stuff is drawn here
160 infostream<<"Got "<<f.drawtype<<std::endl;
168 Add water sources to mesh if using new style
170 assert(nodedef->get(n).special_materials[0]);
171 //assert(nodedef->get(n).special_materials[1]);
172 assert(nodedef->get(n).special_aps[0]);
174 video::SMaterial &liquid_material =
175 *nodedef->get(n).special_materials[0];
176 /*video::SMaterial &liquid_material_bfculled =
177 *nodedef->get(n).special_materials[1];*/
178 AtlasPointer &pa_liquid1 =
179 *nodedef->get(n).special_aps[0];
181 bool top_is_air = false;
182 MapNode n = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y+1,z));
183 if(n.getContent() == CONTENT_AIR)
186 if(top_is_air == false)
189 u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio, nodedef));
190 video::SColor c = MapBlock_LightColor(
191 nodedef->get(n).alpha, l);
193 video::S3DVertex vertices[4] =
195 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
196 pa_liquid1.x0(), pa_liquid1.y1()),
197 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
198 pa_liquid1.x1(), pa_liquid1.y1()),
199 video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c,
200 pa_liquid1.x1(), pa_liquid1.y0()),
201 video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c,
202 pa_liquid1.x0(), pa_liquid1.y0()),
205 for(s32 i=0; i<4; i++)
207 vertices[i].Pos.Y += (-0.5+node_liquid_level)*BS;
208 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
211 u16 indices[] = {0,1,2,2,3,0};
212 // Add to mesh collector
213 collector.append(liquid_material, vertices, 4, indices, 6);
215 case NDT_FLOWINGLIQUID:
218 Add flowing liquid to mesh
220 assert(nodedef->get(n).special_materials[0]);
221 assert(nodedef->get(n).special_materials[1]);
222 assert(nodedef->get(n).special_aps[0]);
224 video::SMaterial &liquid_material =
225 *nodedef->get(n).special_materials[0];
226 video::SMaterial &liquid_material_bfculled =
227 *nodedef->get(n).special_materials[1];
228 AtlasPointer &pa_liquid1 =
229 *nodedef->get(n).special_aps[0];
231 bool top_is_same_liquid = false;
232 MapNode ntop = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y+1,z));
233 content_t c_flowing = nodedef->getId(nodedef->get(n).liquid_alternative_flowing);
234 content_t c_source = nodedef->getId(nodedef->get(n).liquid_alternative_source);
235 if(ntop.getContent() == c_flowing || ntop.getContent() == c_source)
236 top_is_same_liquid = true;
239 // Use the light of the node on top if possible
240 if(nodedef->get(ntop).param_type == CPT_LIGHT)
241 l = decode_light(ntop.getLightBlend(data->m_daynight_ratio, nodedef));
242 // Otherwise use the light of this node (the liquid)
244 l = decode_light(n.getLightBlend(data->m_daynight_ratio, nodedef));
245 video::SColor c = MapBlock_LightColor(
246 nodedef->get(n).alpha, l);
248 // Neighbor liquid levels (key = relative position)
249 // Includes current node
250 core::map<v3s16, f32> neighbor_levels;
251 core::map<v3s16, content_t> neighbor_contents;
252 core::map<v3s16, u8> neighbor_flags;
253 const u8 neighborflag_top_is_same_liquid = 0x01;
254 v3s16 neighbor_dirs[9] = {
265 for(u32 i=0; i<9; i++)
267 content_t content = CONTENT_AIR;
268 float level = -0.5 * BS;
271 v3s16 p2 = p + neighbor_dirs[i];
272 MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
273 if(n2.getContent() != CONTENT_IGNORE)
275 content = n2.getContent();
277 if(n2.getContent() == c_source)
278 level = (-0.5+node_liquid_level) * BS;
279 else if(n2.getContent() == c_flowing)
280 level = (-0.5 + ((float)(n2.param2&LIQUID_LEVEL_MASK)
281 + 0.5) / 8.0 * node_liquid_level) * BS;
283 // Check node above neighbor.
284 // NOTE: This doesn't get executed if neighbor
287 n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
288 if(n2.getContent() == c_source ||
289 n2.getContent() == c_flowing)
290 flags |= neighborflag_top_is_same_liquid;
293 neighbor_levels.insert(neighbor_dirs[i], level);
294 neighbor_contents.insert(neighbor_dirs[i], content);
295 neighbor_flags.insert(neighbor_dirs[i], flags);
298 // Corner heights (average between four liquids)
299 f32 corner_levels[4];
301 v3s16 halfdirs[4] = {
307 for(u32 i=0; i<4; i++)
309 v3s16 cornerdir = halfdirs[i];
310 float cornerlevel = 0;
313 for(u32 j=0; j<4; j++)
315 v3s16 neighbordir = cornerdir - halfdirs[j];
316 content_t content = neighbor_contents[neighbordir];
317 // If top is liquid, draw starting from top of node
318 if(neighbor_flags[neighbordir] &
319 neighborflag_top_is_same_liquid)
321 cornerlevel = 0.5*BS;
325 // Source is always the same height
326 else if(content == c_source)
328 cornerlevel = (-0.5+node_liquid_level)*BS;
332 // Flowing liquid has level information
333 else if(content == c_flowing)
335 cornerlevel += neighbor_levels[neighbordir];
338 else if(content == CONTENT_AIR)
344 cornerlevel = -0.5*BS;
345 else if(valid_count > 0)
346 cornerlevel /= valid_count;
347 corner_levels[i] = cornerlevel;
354 v3s16 side_dirs[4] = {
360 s16 side_corners[4][2] = {
366 for(u32 i=0; i<4; i++)
368 v3s16 dir = side_dirs[i];
371 If our topside is liquid and neighbor's topside
372 is liquid, don't draw side face
374 if(top_is_same_liquid &&
375 neighbor_flags[dir] & neighborflag_top_is_same_liquid)
378 content_t neighbor_content = neighbor_contents[dir];
379 const ContentFeatures &n_feat = nodedef->get(neighbor_content);
381 // Don't draw face if neighbor is blocking the view
382 if(n_feat.solidness == 2)
385 bool neighbor_is_same_liquid = (neighbor_content == c_source
386 || neighbor_content == c_flowing);
388 // Don't draw any faces if neighbor same is liquid and top is
390 if(neighbor_is_same_liquid == true
391 && top_is_same_liquid == false)
394 // Use backface culled material if neighbor doesn't have a
396 video::SMaterial *current_material = &liquid_material;
397 if(n_feat.solidness != 0 || n_feat.visual_solidness != 0)
398 current_material = &liquid_material_bfculled;
400 video::S3DVertex vertices[4] =
402 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
403 pa_liquid1.x0(), pa_liquid1.y1()),
404 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
405 pa_liquid1.x1(), pa_liquid1.y1()),
406 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
407 pa_liquid1.x1(), pa_liquid1.y0()),
408 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
409 pa_liquid1.x0(), pa_liquid1.y0()),
413 If our topside is liquid, set upper border of face
414 at upper border of node
416 if(top_is_same_liquid)
418 vertices[2].Pos.Y = 0.5*BS;
419 vertices[3].Pos.Y = 0.5*BS;
422 Otherwise upper position of face is corner levels
426 vertices[2].Pos.Y = corner_levels[side_corners[i][0]];
427 vertices[3].Pos.Y = corner_levels[side_corners[i][1]];
431 If neighbor is liquid, lower border of face is corner
434 if(neighbor_is_same_liquid)
436 vertices[0].Pos.Y = corner_levels[side_corners[i][1]];
437 vertices[1].Pos.Y = corner_levels[side_corners[i][0]];
440 If neighbor is not liquid, lower border of face is
445 vertices[0].Pos.Y = -0.5*BS;
446 vertices[1].Pos.Y = -0.5*BS;
449 for(s32 j=0; j<4; j++)
451 if(dir == v3s16(0,0,1))
452 vertices[j].Pos.rotateXZBy(0);
453 if(dir == v3s16(0,0,-1))
454 vertices[j].Pos.rotateXZBy(180);
455 if(dir == v3s16(-1,0,0))
456 vertices[j].Pos.rotateXZBy(90);
457 if(dir == v3s16(1,0,-0))
458 vertices[j].Pos.rotateXZBy(-90);
460 // Do this to not cause glitches when two liquids are
462 /*if(neighbor_is_same_liquid == false){
463 vertices[j].Pos.X *= 0.98;
464 vertices[j].Pos.Z *= 0.98;
467 vertices[j].Pos += intToFloat(p + blockpos_nodes, BS);
470 u16 indices[] = {0,1,2,2,3,0};
471 // Add to mesh collector
472 collector.append(*current_material, vertices, 4, indices, 6);
476 Generate top side, if appropriate
479 if(top_is_same_liquid == false)
481 video::S3DVertex vertices[4] =
483 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
484 pa_liquid1.x0(), pa_liquid1.y1()),
485 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
486 pa_liquid1.x1(), pa_liquid1.y1()),
487 video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c,
488 pa_liquid1.x1(), pa_liquid1.y0()),
489 video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c,
490 pa_liquid1.x0(), pa_liquid1.y0()),
493 // This fixes a strange bug
494 s32 corner_resolve[4] = {3,2,1,0};
496 for(s32 i=0; i<4; i++)
498 //vertices[i].Pos.Y += liquid_level;
499 //vertices[i].Pos.Y += neighbor_levels[v3s16(0,0,0)];
500 s32 j = corner_resolve[i];
501 vertices[i].Pos.Y += corner_levels[j];
502 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
505 u16 indices[] = {0,1,2,2,3,0};
506 // Add to mesh collector
507 collector.append(liquid_material, vertices, 4, indices, 6);
512 video::SMaterial material_glass;
513 material_glass.setFlag(video::EMF_LIGHTING, false);
514 material_glass.setFlag(video::EMF_BILINEAR_FILTER, false);
515 material_glass.setFlag(video::EMF_FOG_ENABLE, true);
516 material_glass.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
517 TileSpec tile_glass = getNodeTile(n, p, v3s16(0,0,0),
518 &data->m_temp_mods, tsrc, nodedef);
519 AtlasPointer pa_glass = tile_glass.texture;
520 material_glass.setTexture(0, pa_glass.atlas);
522 u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio, nodedef)));
523 video::SColor c = MapBlock_LightColor(255, l);
525 for(u32 j=0; j<6; j++)
527 // Check this neighbor
528 v3s16 n2p = blockpos_nodes + p + g_6dirs[j];
529 MapNode n2 = data->m_vmanip.getNodeNoEx(n2p);
530 // Don't make face if neighbor is of same type
531 if(n2.getContent() == n.getContent())
535 video::S3DVertex vertices[4] =
537 video::S3DVertex(-BS/2,-BS/2,BS/2, 0,0,0, c,
538 pa_glass.x0(), pa_glass.y1()),
539 video::S3DVertex(BS/2,-BS/2,BS/2, 0,0,0, c,
540 pa_glass.x1(), pa_glass.y1()),
541 video::S3DVertex(BS/2,BS/2,BS/2, 0,0,0, c,
542 pa_glass.x1(), pa_glass.y0()),
543 video::S3DVertex(-BS/2,BS/2,BS/2, 0,0,0, c,
544 pa_glass.x0(), pa_glass.y0()),
547 // Rotations in the g_6dirs format
549 for(u16 i=0; i<4; i++)
550 vertices[i].Pos.rotateXZBy(0);
551 else if(j == 1) // Y+
552 for(u16 i=0; i<4; i++)
553 vertices[i].Pos.rotateYZBy(-90);
554 else if(j == 2) // X+
555 for(u16 i=0; i<4; i++)
556 vertices[i].Pos.rotateXZBy(-90);
557 else if(j == 3) // Z-
558 for(u16 i=0; i<4; i++)
559 vertices[i].Pos.rotateXZBy(180);
560 else if(j == 4) // Y-
561 for(u16 i=0; i<4; i++)
562 vertices[i].Pos.rotateYZBy(90);
563 else if(j == 5) // X-
564 for(u16 i=0; i<4; i++)
565 vertices[i].Pos.rotateXZBy(90);
567 for(u16 i=0; i<4; i++){
568 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
571 u16 indices[] = {0,1,2,2,3,0};
572 // Add to mesh collector
573 collector.append(material_glass, vertices, 4, indices, 6);
578 video::SMaterial material_leaves1;
579 material_leaves1.setFlag(video::EMF_LIGHTING, false);
580 material_leaves1.setFlag(video::EMF_BILINEAR_FILTER, false);
581 material_leaves1.setFlag(video::EMF_FOG_ENABLE, true);
582 material_leaves1.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
583 TileSpec tile_leaves1 = getNodeTile(n, p, v3s16(0,0,0),
584 &data->m_temp_mods, tsrc, nodedef);
585 AtlasPointer pa_leaves1 = tile_leaves1.texture;
586 material_leaves1.setTexture(0, pa_leaves1.atlas);
588 u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio, nodedef)));
589 video::SColor c = MapBlock_LightColor(255, l);
591 v3f pos = intToFloat(p+blockpos_nodes, BS);
592 aabb3f box(-BS/2,-BS/2,-BS/2,BS/2,BS/2,BS/2);
595 makeCuboid(&collector, box,
596 &material_leaves1, &pa_leaves1, 1,
599 case NDT_ALLFACES_OPTIONAL:
600 // This is always pre-converted to something else
605 v3s16 dir = unpackDir(n.param2);
608 if(dir == v3s16(0,-1,0)){
609 ap = f.tiles[0].texture; // floor
610 } else if(dir == v3s16(0,1,0)){
611 ap = f.tiles[1].texture; // ceiling
612 // For backwards compatibility
613 } else if(dir == v3s16(0,0,0)){
614 ap = f.tiles[0].texture; // floor
616 ap = f.tiles[2].texture; // side
620 video::SMaterial material;
621 material.setFlag(video::EMF_LIGHTING, false);
622 material.setFlag(video::EMF_BACK_FACE_CULLING, false);
623 material.setFlag(video::EMF_BILINEAR_FILTER, false);
624 material.setFlag(video::EMF_FOG_ENABLE, true);
625 //material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
626 material.MaterialType
627 = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
628 material.setTexture(0, ap.atlas);
630 video::SColor c(255,255,255,255);
632 // Wall at X+ of node
633 video::S3DVertex vertices[4] =
635 video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c,
637 video::S3DVertex(BS/2,-BS/2,0, 0,0,0, c,
639 video::S3DVertex(BS/2,BS/2,0, 0,0,0, c,
641 video::S3DVertex(-BS/2,BS/2,0, 0,0,0, c,
645 for(s32 i=0; i<4; i++)
647 if(dir == v3s16(1,0,0))
648 vertices[i].Pos.rotateXZBy(0);
649 if(dir == v3s16(-1,0,0))
650 vertices[i].Pos.rotateXZBy(180);
651 if(dir == v3s16(0,0,1))
652 vertices[i].Pos.rotateXZBy(90);
653 if(dir == v3s16(0,0,-1))
654 vertices[i].Pos.rotateXZBy(-90);
655 if(dir == v3s16(0,-1,0))
656 vertices[i].Pos.rotateXZBy(45);
657 if(dir == v3s16(0,1,0))
658 vertices[i].Pos.rotateXZBy(-45);
660 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
663 u16 indices[] = {0,1,2,2,3,0};
664 // Add to mesh collector
665 collector.append(material, vertices, 4, indices, 6);
670 video::SMaterial material;
671 material.setFlag(video::EMF_LIGHTING, false);
672 material.setFlag(video::EMF_BACK_FACE_CULLING, false);
673 material.setFlag(video::EMF_BILINEAR_FILTER, false);
674 material.setFlag(video::EMF_FOG_ENABLE, true);
675 material.MaterialType
676 = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
677 AtlasPointer ap = f.tiles[0].texture;
678 material.setTexture(0, ap.atlas);
680 u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio, nodedef));
681 video::SColor c = MapBlock_LightColor(255, l);
683 float d = (float)BS/16;
684 // Wall at X+ of node
685 video::S3DVertex vertices[4] =
687 video::S3DVertex(BS/2-d,BS/2,BS/2, 0,0,0, c,
689 video::S3DVertex(BS/2-d,BS/2,-BS/2, 0,0,0, c,
691 video::S3DVertex(BS/2-d,-BS/2,-BS/2, 0,0,0, c,
693 video::S3DVertex(BS/2-d,-BS/2,BS/2, 0,0,0, c,
697 v3s16 dir = unpackDir(n.param2);
699 for(s32 i=0; i<4; i++)
701 if(dir == v3s16(1,0,0))
702 vertices[i].Pos.rotateXZBy(0);
703 if(dir == v3s16(-1,0,0))
704 vertices[i].Pos.rotateXZBy(180);
705 if(dir == v3s16(0,0,1))
706 vertices[i].Pos.rotateXZBy(90);
707 if(dir == v3s16(0,0,-1))
708 vertices[i].Pos.rotateXZBy(-90);
709 if(dir == v3s16(0,-1,0))
710 vertices[i].Pos.rotateXYBy(-90);
711 if(dir == v3s16(0,1,0))
712 vertices[i].Pos.rotateXYBy(90);
714 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
717 u16 indices[] = {0,1,2,2,3,0};
718 // Add to mesh collector
719 collector.append(material, vertices, 4, indices, 6);
723 video::SMaterial material_papyrus;
724 material_papyrus.setFlag(video::EMF_LIGHTING, false);
725 material_papyrus.setFlag(video::EMF_BILINEAR_FILTER, false);
726 material_papyrus.setFlag(video::EMF_FOG_ENABLE, true);
727 material_papyrus.MaterialType=video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
728 AtlasPointer pa_papyrus = f.tiles[0].texture;
729 material_papyrus.setTexture(0, pa_papyrus.atlas);
731 u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio, nodedef)));
732 video::SColor c = MapBlock_LightColor(255, l);
734 for(u32 j=0; j<4; j++)
736 video::S3DVertex vertices[4] =
738 video::S3DVertex(-BS/2*f.visual_scale,-BS/2,0, 0,0,0, c,
739 pa_papyrus.x0(), pa_papyrus.y1()),
740 video::S3DVertex( BS/2*f.visual_scale,-BS/2,0, 0,0,0, c,
741 pa_papyrus.x1(), pa_papyrus.y1()),
742 video::S3DVertex( BS/2*f.visual_scale,
743 -BS/2 + f.visual_scale*BS,0, 0,0,0, c,
744 pa_papyrus.x1(), pa_papyrus.y0()),
745 video::S3DVertex(-BS/2*f.visual_scale,
746 -BS/2 + f.visual_scale*BS,0, 0,0,0, c,
747 pa_papyrus.x0(), pa_papyrus.y0()),
752 for(u16 i=0; i<4; i++)
753 vertices[i].Pos.rotateXZBy(45);
757 for(u16 i=0; i<4; i++)
758 vertices[i].Pos.rotateXZBy(-45);
762 for(u16 i=0; i<4; i++)
763 vertices[i].Pos.rotateXZBy(135);
767 for(u16 i=0; i<4; i++)
768 vertices[i].Pos.rotateXZBy(-135);
771 for(u16 i=0; i<4; i++)
773 vertices[i].Pos *= f.visual_scale;
774 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
777 u16 indices[] = {0,1,2,2,3,0};
778 // Add to mesh collector
779 collector.append(material_papyrus, vertices, 4, indices, 6);
784 video::SMaterial material_wood;
785 material_wood.setFlag(video::EMF_LIGHTING, false);
786 material_wood.setFlag(video::EMF_BILINEAR_FILTER, false);
787 material_wood.setFlag(video::EMF_FOG_ENABLE, true);
788 material_wood.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
789 TileSpec tile_wood = getNodeTile(n, p, v3s16(0,0,0),
790 &data->m_temp_mods, tsrc, nodedef);
791 AtlasPointer pa_wood = tile_wood.texture;
792 material_wood.setTexture(0, pa_wood.atlas);
794 video::SMaterial material_wood_nomod;
795 material_wood_nomod.setFlag(video::EMF_LIGHTING, false);
796 material_wood_nomod.setFlag(video::EMF_BILINEAR_FILTER, false);
797 material_wood_nomod.setFlag(video::EMF_FOG_ENABLE, true);
798 material_wood_nomod.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
800 TileSpec tile_wood_nomod = getNodeTile(n, p, v3s16(0,0,0),
801 NULL, tsrc, nodedef);
802 AtlasPointer pa_wood_nomod = tile_wood_nomod.texture;
803 material_wood_nomod.setTexture(0, pa_wood_nomod.atlas);
805 u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio, nodedef)));
806 video::SColor c = MapBlock_LightColor(255, l);
808 const f32 post_rad=(f32)BS/10;
809 const f32 bar_rad=(f32)BS/20;
810 const f32 bar_len=(f32)(BS/2)-post_rad;
812 v3f pos = intToFloat(p+blockpos_nodes, BS);
814 // The post - always present
815 aabb3f post(-post_rad,-BS/2,-post_rad,post_rad,BS/2,post_rad);
825 makeCuboid(&collector, post, &material_wood,
826 &pa_wood, 1, c, postuv);
828 // Now a section of fence, +X, if there's a post there
831 MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
832 const ContentFeatures *f2 = &nodedef->get(n2);
833 if(f2->drawtype == NDT_FENCELIKE)
835 aabb3f bar(-bar_len+BS/2,-bar_rad+BS/4,-bar_rad,
836 bar_len+BS/2,bar_rad+BS/4,bar_rad);
846 makeCuboid(&collector, bar, &material_wood_nomod,
847 &pa_wood_nomod, 1, c, xrailuv);
848 bar.MinEdge.Y -= BS/2;
849 bar.MaxEdge.Y -= BS/2;
850 makeCuboid(&collector, bar, &material_wood_nomod,
851 &pa_wood_nomod, 1, c, xrailuv);
854 // Now a section of fence, +Z, if there's a post there
857 n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
858 f2 = &nodedef->get(n2);
859 if(f2->drawtype == NDT_FENCELIKE)
861 aabb3f bar(-bar_rad,-bar_rad+BS/4,-bar_len+BS/2,
862 bar_rad,bar_rad+BS/4,bar_len+BS/2);
873 makeCuboid(&collector, bar, &material_wood_nomod,
874 &pa_wood_nomod, 1, c, zrailuv);
875 bar.MinEdge.Y -= BS/2;
876 bar.MaxEdge.Y -= BS/2;
877 makeCuboid(&collector, bar, &material_wood_nomod,
878 &pa_wood_nomod, 1, c, zrailuv);
883 bool is_rail_x [] = { false, false }; /* x-1, x+1 */
884 bool is_rail_z [] = { false, false }; /* z-1, z+1 */
886 MapNode n_minus_x = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x-1,y,z));
887 MapNode n_plus_x = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x+1,y,z));
888 MapNode n_minus_z = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y,z-1));
889 MapNode n_plus_z = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y,z+1));
891 content_t thiscontent = n.getContent();
892 if(n_minus_x.getContent() == thiscontent)
894 if(n_plus_x.getContent() == thiscontent)
896 if(n_minus_z.getContent() == thiscontent)
898 if(n_plus_z.getContent() == thiscontent)
901 int adjacencies = is_rail_x[0] + is_rail_x[1] + is_rail_z[0] + is_rail_z[1];
904 AtlasPointer ap = f.tiles[0].texture; // straight
906 ap = f.tiles[0].texture; // straight
907 else if(adjacencies == 2)
909 if((is_rail_x[0] && is_rail_x[1]) || (is_rail_z[0] && is_rail_z[1]))
910 ap = f.tiles[0].texture; // straight
912 ap = f.tiles[1].texture; // curved
914 else if(adjacencies == 3)
915 ap = f.tiles[2].texture; // t-junction
916 else if(adjacencies == 4)
917 ap = f.tiles[3].texture; // crossing
919 video::SMaterial material_rail;
920 material_rail.setFlag(video::EMF_LIGHTING, false);
921 material_rail.setFlag(video::EMF_BACK_FACE_CULLING, false);
922 material_rail.setFlag(video::EMF_BILINEAR_FILTER, false);
923 material_rail.setFlag(video::EMF_FOG_ENABLE, true);
924 material_rail.MaterialType
925 = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
926 material_rail.setTexture(0, ap.atlas);
928 u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio, nodedef));
929 video::SColor c = MapBlock_LightColor(255, l);
931 float d = (float)BS/16;
932 video::S3DVertex vertices[4] =
934 video::S3DVertex(-BS/2,-BS/2+d,-BS/2, 0,0,0, c,
936 video::S3DVertex(BS/2,-BS/2+d,-BS/2, 0,0,0, c,
938 video::S3DVertex(BS/2,-BS/2+d,BS/2, 0,0,0, c,
940 video::S3DVertex(-BS/2,-BS/2+d,BS/2, 0,0,0, c,
949 if(is_rail_x[0] || is_rail_x[1])
952 else if(adjacencies == 2)
954 if(is_rail_x[0] && is_rail_x[1])
956 else if(is_rail_x[0] && is_rail_z[0])
958 else if(is_rail_x[0] && is_rail_z[1])
960 else if(is_rail_x[1] && is_rail_z[1])
963 else if(adjacencies == 3)
976 for(u16 i=0; i<4; i++)
977 vertices[i].Pos.rotateXZBy(angle);
980 for(s32 i=0; i<4; i++)
982 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
985 u16 indices[] = {0,1,2,2,3,0};
986 collector.append(material_rail, vertices, 4, indices, 6);