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"
21 #include "content_mapnode.h"
22 #include "main.h" // For g_settings
24 #include "mapblock_mesh.h" // For MapBlock_LightColor()
31 // material - the material to use (for all 6 faces)
32 // collector - the MeshCollector for the resulting polygons
33 // pa - texture atlas pointer for the material
34 // c - vertex colour - used for all
35 // pos - the position of the centre of the cuboid
36 // rz,ry,rz - the radius of the cuboid in each dimension
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 top-backi-right-front-left-bottom
42 // If you specified 0,0,1,1 for each face, that would be the
43 // same as passing NULL.
44 void makeCuboid(video::SMaterial &material, MeshCollector *collector,
45 AtlasPointer* pa, video::SColor &c,
46 v3f &pos, f32 rx, f32 ry, f32 rz, f32* txc)
55 video::S3DVertex v[4] =
57 video::S3DVertex(0,0,0, 0,0,0, c, tu0, tv1),
58 video::S3DVertex(0,0,0, 0,0,0, c, tu1, tv1),
59 video::S3DVertex(0,0,0, 0,0,0, c, tu1, tv0),
60 video::S3DVertex(0,0,0, 0,0,0, c, tu0, tv0)
68 v[0].Pos.X=-rx; v[0].Pos.Y= ry; v[0].Pos.Z=-rz;
69 v[1].Pos.X=-rx; v[1].Pos.Y= ry; v[1].Pos.Z= rz;
70 v[2].Pos.X= rx; v[2].Pos.Y= ry; v[2].Pos.Z= rz;
71 v[3].Pos.X= rx; v[3].Pos.Y= ry, v[3].Pos.Z=-rz;
74 v[0].Pos.X=-rx; v[0].Pos.Y= ry; v[0].Pos.Z=-rz;
75 v[1].Pos.X= rx; v[1].Pos.Y= ry; v[1].Pos.Z=-rz;
76 v[2].Pos.X= rx; v[2].Pos.Y=-ry; v[2].Pos.Z=-rz;
77 v[3].Pos.X=-rx; v[3].Pos.Y=-ry, v[3].Pos.Z=-rz;
80 v[0].Pos.X= rx; v[0].Pos.Y= ry; v[0].Pos.Z=-rz;
81 v[1].Pos.X= rx; v[1].Pos.Y= ry; v[1].Pos.Z= rz;
82 v[2].Pos.X= rx; v[2].Pos.Y=-ry; v[2].Pos.Z= rz;
83 v[3].Pos.X= rx; v[3].Pos.Y=-ry, v[3].Pos.Z=-rz;
86 v[0].Pos.X= rx; v[0].Pos.Y= ry; v[0].Pos.Z= rz;
87 v[1].Pos.X=-rx; v[1].Pos.Y= ry; v[1].Pos.Z= rz;
88 v[2].Pos.X=-rx; v[2].Pos.Y=-ry; v[2].Pos.Z= rz;
89 v[3].Pos.X= rx; v[3].Pos.Y=-ry, v[3].Pos.Z= rz;
92 v[0].Pos.X=-rx; v[0].Pos.Y= ry; v[0].Pos.Z= rz;
93 v[1].Pos.X=-rx; v[1].Pos.Y= ry; v[1].Pos.Z=-rz;
94 v[2].Pos.X=-rx; v[2].Pos.Y=-ry; v[2].Pos.Z=-rz;
95 v[3].Pos.X=-rx; v[3].Pos.Y=-ry, v[3].Pos.Z= rz;
98 v[0].Pos.X= rx; v[0].Pos.Y=-ry; v[0].Pos.Z= rz;
99 v[1].Pos.X=-rx; v[1].Pos.Y=-ry; v[1].Pos.Z= rz;
100 v[2].Pos.X=-rx; v[2].Pos.Y=-ry; v[2].Pos.Z=-rz;
101 v[3].Pos.X= rx; v[3].Pos.Y=-ry, v[3].Pos.Z=-rz;
107 v[0].TCoords.X=tu0+txus*txc[0]; v[0].TCoords.Y=tv0+txvs*txc[3];
108 v[1].TCoords.X=tu0+txus*txc[2]; v[1].TCoords.Y=tv0+txvs*txc[3];
109 v[2].TCoords.X=tu0+txus*txc[2]; v[2].TCoords.Y=tv0+txvs*txc[1];
110 v[3].TCoords.X=tu0+txus*txc[0]; v[3].TCoords.Y=tv0+txvs*txc[1];
114 for(u16 i=0; i<4; i++)
116 u16 indices[] = {0,1,2,2,3,0};
117 collector->append(material, v, 4, indices, 6);
125 void mapblock_mesh_generate_special(MeshMakeData *data,
126 MeshCollector &collector, IGameDef *gamedef)
128 ITextureSource *tsrc = gamedef->tsrc();
129 INodeDefManager *nodedef = gamedef->ndef();
132 //TimeTaker timer("mapblock_mesh_generate_special()");
137 bool new_style_water = g_settings->getBool("new_style_water");
138 bool new_style_leaves = g_settings->getBool("new_style_leaves");
139 //bool smooth_lighting = g_settings->getBool("smooth_lighting");
140 bool invisible_stone = g_settings->getBool("invisible_stone");
142 float node_liquid_level = 1.0;
144 node_liquid_level = 0.85;
146 v3s16 blockpos_nodes = data->m_blockpos*MAP_BLOCKSIZE;
148 // New-style leaves material
149 video::SMaterial material_leaves1;
150 material_leaves1.setFlag(video::EMF_LIGHTING, false);
151 material_leaves1.setFlag(video::EMF_BILINEAR_FILTER, false);
152 material_leaves1.setFlag(video::EMF_FOG_ENABLE, true);
153 material_leaves1.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
154 AtlasPointer pa_leaves1 = tsrc->getTexture(
155 tsrc->getTextureId("leaves.png"));
156 material_leaves1.setTexture(0, pa_leaves1.atlas);
159 video::SMaterial material_glass;
160 material_glass.setFlag(video::EMF_LIGHTING, false);
161 material_glass.setFlag(video::EMF_BILINEAR_FILTER, false);
162 material_glass.setFlag(video::EMF_FOG_ENABLE, true);
163 material_glass.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
164 AtlasPointer pa_glass = tsrc->getTexture(
165 tsrc->getTextureId("glass.png"));
166 material_glass.setTexture(0, pa_glass.atlas);
169 video::SMaterial material_wood;
170 material_wood.setFlag(video::EMF_LIGHTING, false);
171 material_wood.setFlag(video::EMF_BILINEAR_FILTER, false);
172 material_wood.setFlag(video::EMF_FOG_ENABLE, true);
173 material_wood.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
174 AtlasPointer pa_wood = tsrc->getTexture(
175 tsrc->getTextureId("wood.png"));
176 material_wood.setTexture(0, pa_wood.atlas);
178 // General ground material for special output
179 // Texture is modified just before usage
180 video::SMaterial material_general;
181 material_general.setFlag(video::EMF_LIGHTING, false);
182 material_general.setFlag(video::EMF_BILINEAR_FILTER, false);
183 material_general.setFlag(video::EMF_FOG_ENABLE, true);
184 material_general.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
188 video::SMaterial material_papyrus;
189 material_papyrus.setFlag(video::EMF_LIGHTING, false);
190 material_papyrus.setFlag(video::EMF_BILINEAR_FILTER, false);
191 material_papyrus.setFlag(video::EMF_FOG_ENABLE, true);
192 material_papyrus.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
193 AtlasPointer pa_papyrus = tsrc->getTexture(
194 tsrc->getTextureId("papyrus.png"));
195 material_papyrus.setTexture(0, pa_papyrus.atlas);
198 video::SMaterial material_apple;
199 material_apple.setFlag(video::EMF_LIGHTING, false);
200 material_apple.setFlag(video::EMF_BILINEAR_FILTER, false);
201 material_apple.setFlag(video::EMF_FOG_ENABLE, true);
202 material_apple.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
203 AtlasPointer pa_apple = tsrc->getTexture(
204 tsrc->getTextureId("apple.png"));
205 material_apple.setTexture(0, pa_apple.atlas);
209 video::SMaterial material_sapling;
210 material_sapling.setFlag(video::EMF_LIGHTING, false);
211 material_sapling.setFlag(video::EMF_BILINEAR_FILTER, false);
212 material_sapling.setFlag(video::EMF_FOG_ENABLE, true);
213 material_sapling.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
214 AtlasPointer pa_sapling = tsrc->getTexture(
215 tsrc->getTextureId("sapling.png"));
216 material_sapling.setTexture(0, pa_sapling.atlas);
219 // junglegrass material
220 video::SMaterial material_junglegrass;
221 material_junglegrass.setFlag(video::EMF_LIGHTING, false);
222 material_junglegrass.setFlag(video::EMF_BILINEAR_FILTER, false);
223 material_junglegrass.setFlag(video::EMF_FOG_ENABLE, true);
224 material_junglegrass.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
225 AtlasPointer pa_junglegrass = tsrc->getTexture(
226 tsrc->getTextureId("junglegrass.png"));
227 material_junglegrass.setTexture(0, pa_junglegrass.atlas);
229 for(s16 z=0; z<MAP_BLOCKSIZE; z++)
230 for(s16 y=0; y<MAP_BLOCKSIZE; y++)
231 for(s16 x=0; x<MAP_BLOCKSIZE; x++)
235 MapNode n = data->m_vmanip.getNodeNoEx(blockpos_nodes+p);
237 // Only solidness=0 stuff is drawn here
238 if(nodedef->get(n).solidness != 0)
244 if(n.getContent() == CONTENT_TORCH)
246 v3s16 dir = unpackDir(n.param2);
248 const char *texturename = "torch.png";
249 if(dir == v3s16(0,-1,0)){
250 texturename = "torch_on_floor.png";
251 } else if(dir == v3s16(0,1,0)){
252 texturename = "torch_on_ceiling.png";
253 // For backwards compatibility
254 } else if(dir == v3s16(0,0,0)){
255 texturename = "torch_on_floor.png";
257 texturename = "torch.png";
260 AtlasPointer ap = tsrc->getTexture(texturename);
263 video::SMaterial material;
264 material.setFlag(video::EMF_LIGHTING, false);
265 material.setFlag(video::EMF_BACK_FACE_CULLING, false);
266 material.setFlag(video::EMF_BILINEAR_FILTER, false);
267 material.setFlag(video::EMF_FOG_ENABLE, true);
268 //material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
269 material.MaterialType
270 = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
271 material.setTexture(0, ap.atlas);
273 video::SColor c(255,255,255,255);
275 // Wall at X+ of node
276 video::S3DVertex vertices[4] =
278 video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c,
280 video::S3DVertex(BS/2,-BS/2,0, 0,0,0, c,
282 video::S3DVertex(BS/2,BS/2,0, 0,0,0, c,
284 video::S3DVertex(-BS/2,BS/2,0, 0,0,0, c,
288 for(s32 i=0; i<4; i++)
290 if(dir == v3s16(1,0,0))
291 vertices[i].Pos.rotateXZBy(0);
292 if(dir == v3s16(-1,0,0))
293 vertices[i].Pos.rotateXZBy(180);
294 if(dir == v3s16(0,0,1))
295 vertices[i].Pos.rotateXZBy(90);
296 if(dir == v3s16(0,0,-1))
297 vertices[i].Pos.rotateXZBy(-90);
298 if(dir == v3s16(0,-1,0))
299 vertices[i].Pos.rotateXZBy(45);
300 if(dir == v3s16(0,1,0))
301 vertices[i].Pos.rotateXZBy(-45);
303 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
306 u16 indices[] = {0,1,2,2,3,0};
307 // Add to mesh collector
308 collector.append(material, vertices, 4, indices, 6);
313 else if(n.getContent() == CONTENT_SIGN_WALL)
315 AtlasPointer ap = tsrc->getTexture("sign_wall.png");
318 video::SMaterial material;
319 material.setFlag(video::EMF_LIGHTING, false);
320 material.setFlag(video::EMF_BACK_FACE_CULLING, false);
321 material.setFlag(video::EMF_BILINEAR_FILTER, false);
322 material.setFlag(video::EMF_FOG_ENABLE, true);
323 material.MaterialType
324 = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
325 material.setTexture(0, ap.atlas);
327 u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio, nodedef));
328 video::SColor c = MapBlock_LightColor(255, l);
330 float d = (float)BS/16;
331 // Wall at X+ of node
332 video::S3DVertex vertices[4] =
334 video::S3DVertex(BS/2-d,-BS/2,-BS/2, 0,0,0, c,
336 video::S3DVertex(BS/2-d,-BS/2,BS/2, 0,0,0, c,
338 video::S3DVertex(BS/2-d,BS/2,BS/2, 0,0,0, c,
340 video::S3DVertex(BS/2-d,BS/2,-BS/2, 0,0,0, c,
344 v3s16 dir = unpackDir(n.param2);
346 for(s32 i=0; i<4; i++)
348 if(dir == v3s16(1,0,0))
349 vertices[i].Pos.rotateXZBy(0);
350 if(dir == v3s16(-1,0,0))
351 vertices[i].Pos.rotateXZBy(180);
352 if(dir == v3s16(0,0,1))
353 vertices[i].Pos.rotateXZBy(90);
354 if(dir == v3s16(0,0,-1))
355 vertices[i].Pos.rotateXZBy(-90);
356 if(dir == v3s16(0,-1,0))
357 vertices[i].Pos.rotateXYBy(-90);
358 if(dir == v3s16(0,1,0))
359 vertices[i].Pos.rotateXYBy(90);
361 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
364 u16 indices[] = {0,1,2,2,3,0};
365 // Add to mesh collector
366 collector.append(material, vertices, 4, indices, 6);
369 Add flowing liquid to mesh
371 else if(nodedef->get(n).liquid_type == LIQUID_FLOWING)
373 assert(nodedef->get(n).special_materials[0]);
374 assert(nodedef->get(n).special_materials[1]);
375 assert(nodedef->get(n).special_aps[0]);
377 video::SMaterial &liquid_material =
378 *nodedef->get(n).special_materials[0];
379 video::SMaterial &liquid_material_bfculled =
380 *nodedef->get(n).special_materials[1];
381 AtlasPointer &pa_liquid1 =
382 *nodedef->get(n).special_aps[0];
384 bool top_is_same_liquid = false;
385 MapNode ntop = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y+1,z));
386 content_t c_flowing = nodedef->get(n).liquid_alternative_flowing;
387 content_t c_source = nodedef->get(n).liquid_alternative_source;
388 if(ntop.getContent() == c_flowing || ntop.getContent() == c_source)
389 top_is_same_liquid = true;
392 // Use the light of the node on top if possible
393 if(nodedef->get(ntop).param_type == CPT_LIGHT)
394 l = decode_light(ntop.getLightBlend(data->m_daynight_ratio, nodedef));
395 // Otherwise use the light of this node (the liquid)
397 l = decode_light(n.getLightBlend(data->m_daynight_ratio, nodedef));
398 video::SColor c = MapBlock_LightColor(
399 nodedef->get(n).alpha, l);
401 // Neighbor liquid levels (key = relative position)
402 // Includes current node
403 core::map<v3s16, f32> neighbor_levels;
404 core::map<v3s16, content_t> neighbor_contents;
405 core::map<v3s16, u8> neighbor_flags;
406 const u8 neighborflag_top_is_same_liquid = 0x01;
407 v3s16 neighbor_dirs[9] = {
418 for(u32 i=0; i<9; i++)
420 content_t content = CONTENT_AIR;
421 float level = -0.5 * BS;
424 v3s16 p2 = p + neighbor_dirs[i];
425 MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
426 if(n2.getContent() != CONTENT_IGNORE)
428 content = n2.getContent();
430 if(n2.getContent() == c_source)
431 level = (-0.5+node_liquid_level) * BS;
432 else if(n2.getContent() == c_flowing)
433 level = (-0.5 + ((float)(n2.param2&LIQUID_LEVEL_MASK)
434 + 0.5) / 8.0 * node_liquid_level) * BS;
436 // Check node above neighbor.
437 // NOTE: This doesn't get executed if neighbor
440 n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
441 if(n2.getContent() == c_source ||
442 n2.getContent() == c_flowing)
443 flags |= neighborflag_top_is_same_liquid;
446 neighbor_levels.insert(neighbor_dirs[i], level);
447 neighbor_contents.insert(neighbor_dirs[i], content);
448 neighbor_flags.insert(neighbor_dirs[i], flags);
451 // Corner heights (average between four liquids)
452 f32 corner_levels[4];
454 v3s16 halfdirs[4] = {
460 for(u32 i=0; i<4; i++)
462 v3s16 cornerdir = halfdirs[i];
463 float cornerlevel = 0;
466 for(u32 j=0; j<4; j++)
468 v3s16 neighbordir = cornerdir - halfdirs[j];
469 content_t content = neighbor_contents[neighbordir];
470 // If top is liquid, draw starting from top of node
471 if(neighbor_flags[neighbordir] &
472 neighborflag_top_is_same_liquid)
474 cornerlevel = 0.5*BS;
478 // Source is always the same height
479 else if(content == c_source)
481 cornerlevel = (-0.5+node_liquid_level)*BS;
485 // Flowing liquid has level information
486 else if(content == c_flowing)
488 cornerlevel += neighbor_levels[neighbordir];
491 else if(content == CONTENT_AIR)
497 cornerlevel = -0.5*BS;
498 else if(valid_count > 0)
499 cornerlevel /= valid_count;
500 corner_levels[i] = cornerlevel;
507 v3s16 side_dirs[4] = {
513 s16 side_corners[4][2] = {
519 for(u32 i=0; i<4; i++)
521 v3s16 dir = side_dirs[i];
524 If our topside is liquid and neighbor's topside
525 is liquid, don't draw side face
527 if(top_is_same_liquid &&
528 neighbor_flags[dir] & neighborflag_top_is_same_liquid)
531 content_t neighbor_content = neighbor_contents[dir];
532 const ContentFeatures &n_feat = nodedef->get(neighbor_content);
534 // Don't draw face if neighbor is blocking the view
535 if(n_feat.solidness == 2)
538 bool neighbor_is_same_liquid = (neighbor_content == c_source
539 || neighbor_content == c_flowing);
541 // Don't draw any faces if neighbor same is liquid and top is
543 if(neighbor_is_same_liquid == true
544 && top_is_same_liquid == false)
547 // Use backface culled material if neighbor doesn't have a
549 video::SMaterial *current_material = &liquid_material;
550 if(n_feat.solidness != 0 || n_feat.visual_solidness != 0)
551 current_material = &liquid_material_bfculled;
553 video::S3DVertex vertices[4] =
555 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
556 pa_liquid1.x0(), pa_liquid1.y1()),
557 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
558 pa_liquid1.x1(), pa_liquid1.y1()),
559 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
560 pa_liquid1.x1(), pa_liquid1.y0()),
561 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
562 pa_liquid1.x0(), pa_liquid1.y0()),
566 If our topside is liquid, set upper border of face
567 at upper border of node
569 if(top_is_same_liquid)
571 vertices[2].Pos.Y = 0.5*BS;
572 vertices[3].Pos.Y = 0.5*BS;
575 Otherwise upper position of face is corner levels
579 vertices[2].Pos.Y = corner_levels[side_corners[i][0]];
580 vertices[3].Pos.Y = corner_levels[side_corners[i][1]];
584 If neighbor is liquid, lower border of face is corner
587 if(neighbor_is_same_liquid)
589 vertices[0].Pos.Y = corner_levels[side_corners[i][1]];
590 vertices[1].Pos.Y = corner_levels[side_corners[i][0]];
593 If neighbor is not liquid, lower border of face is
598 vertices[0].Pos.Y = -0.5*BS;
599 vertices[1].Pos.Y = -0.5*BS;
602 for(s32 j=0; j<4; j++)
604 if(dir == v3s16(0,0,1))
605 vertices[j].Pos.rotateXZBy(0);
606 if(dir == v3s16(0,0,-1))
607 vertices[j].Pos.rotateXZBy(180);
608 if(dir == v3s16(-1,0,0))
609 vertices[j].Pos.rotateXZBy(90);
610 if(dir == v3s16(1,0,-0))
611 vertices[j].Pos.rotateXZBy(-90);
613 // Do this to not cause glitches when two liquids are
615 /*if(neighbor_is_same_liquid == false){
616 vertices[j].Pos.X *= 0.98;
617 vertices[j].Pos.Z *= 0.98;
620 vertices[j].Pos += intToFloat(p + blockpos_nodes, BS);
623 u16 indices[] = {0,1,2,2,3,0};
624 // Add to mesh collector
625 collector.append(*current_material, vertices, 4, indices, 6);
629 Generate top side, if appropriate
632 if(top_is_same_liquid == false)
634 video::S3DVertex vertices[4] =
636 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
637 pa_liquid1.x0(), pa_liquid1.y1()),
638 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
639 pa_liquid1.x1(), pa_liquid1.y1()),
640 video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c,
641 pa_liquid1.x1(), pa_liquid1.y0()),
642 video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c,
643 pa_liquid1.x0(), pa_liquid1.y0()),
646 // This fixes a strange bug
647 s32 corner_resolve[4] = {3,2,1,0};
649 for(s32 i=0; i<4; i++)
651 //vertices[i].Pos.Y += liquid_level;
652 //vertices[i].Pos.Y += neighbor_levels[v3s16(0,0,0)];
653 s32 j = corner_resolve[i];
654 vertices[i].Pos.Y += corner_levels[j];
655 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
658 u16 indices[] = {0,1,2,2,3,0};
659 // Add to mesh collector
660 collector.append(liquid_material, vertices, 4, indices, 6);
664 Add water sources to mesh if using new style
666 else if(nodedef->get(n).liquid_type == LIQUID_SOURCE
669 assert(nodedef->get(n).special_materials[0]);
670 //assert(nodedef->get(n).special_materials[1]);
671 assert(nodedef->get(n).special_aps[0]);
673 video::SMaterial &liquid_material =
674 *nodedef->get(n).special_materials[0];
675 /*video::SMaterial &liquid_material_bfculled =
676 *nodedef->get(n).special_materials[1];*/
677 AtlasPointer &pa_liquid1 =
678 *nodedef->get(n).special_aps[0];
680 bool top_is_air = false;
681 MapNode n = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y+1,z));
682 if(n.getContent() == CONTENT_AIR)
685 if(top_is_air == false)
688 u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio, nodedef));
689 video::SColor c = MapBlock_LightColor(
690 nodedef->get(n).alpha, l);
692 video::S3DVertex vertices[4] =
694 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
695 pa_liquid1.x0(), pa_liquid1.y1()),
696 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
697 pa_liquid1.x1(), pa_liquid1.y1()),
698 video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c,
699 pa_liquid1.x1(), pa_liquid1.y0()),
700 video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c,
701 pa_liquid1.x0(), pa_liquid1.y0()),
704 for(s32 i=0; i<4; i++)
706 vertices[i].Pos.Y += (-0.5+node_liquid_level)*BS;
707 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
710 u16 indices[] = {0,1,2,2,3,0};
711 // Add to mesh collector
712 collector.append(liquid_material, vertices, 4, indices, 6);
715 Add leaves if using new style
717 else if(n.getContent() == CONTENT_LEAVES && new_style_leaves)
719 /*u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio, nodedef));*/
720 u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio, nodedef)));
721 video::SColor c = MapBlock_LightColor(255, l);
723 for(u32 j=0; j<6; j++)
725 video::S3DVertex vertices[4] =
727 video::S3DVertex(-BS/2,-BS/2,BS/2, 0,0,0, c,
728 pa_leaves1.x0(), pa_leaves1.y1()),
729 video::S3DVertex(BS/2,-BS/2,BS/2, 0,0,0, c,
730 pa_leaves1.x1(), pa_leaves1.y1()),
731 video::S3DVertex(BS/2,BS/2,BS/2, 0,0,0, c,
732 pa_leaves1.x1(), pa_leaves1.y0()),
733 video::S3DVertex(-BS/2,BS/2,BS/2, 0,0,0, c,
734 pa_leaves1.x0(), pa_leaves1.y0()),
739 for(u16 i=0; i<4; i++)
740 vertices[i].Pos.rotateXZBy(0);
744 for(u16 i=0; i<4; i++)
745 vertices[i].Pos.rotateXZBy(180);
749 for(u16 i=0; i<4; i++)
750 vertices[i].Pos.rotateXZBy(-90);
754 for(u16 i=0; i<4; i++)
755 vertices[i].Pos.rotateXZBy(90);
759 for(u16 i=0; i<4; i++)
760 vertices[i].Pos.rotateYZBy(-90);
764 for(u16 i=0; i<4; i++)
765 vertices[i].Pos.rotateYZBy(90);
768 for(u16 i=0; i<4; i++)
770 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
773 u16 indices[] = {0,1,2,2,3,0};
774 // Add to mesh collector
775 collector.append(material_leaves1, vertices, 4, indices, 6);
781 else if(n.getContent() == CONTENT_GLASS)
783 u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio, nodedef)));
784 video::SColor c = MapBlock_LightColor(255, l);
786 for(u32 j=0; j<6; j++)
788 video::S3DVertex vertices[4] =
790 video::S3DVertex(-BS/2,-BS/2,BS/2, 0,0,0, c,
791 pa_glass.x0(), pa_glass.y1()),
792 video::S3DVertex(BS/2,-BS/2,BS/2, 0,0,0, c,
793 pa_glass.x1(), pa_glass.y1()),
794 video::S3DVertex(BS/2,BS/2,BS/2, 0,0,0, c,
795 pa_glass.x1(), pa_glass.y0()),
796 video::S3DVertex(-BS/2,BS/2,BS/2, 0,0,0, c,
797 pa_glass.x0(), pa_glass.y0()),
802 for(u16 i=0; i<4; i++)
803 vertices[i].Pos.rotateXZBy(0);
807 for(u16 i=0; i<4; i++)
808 vertices[i].Pos.rotateXZBy(180);
812 for(u16 i=0; i<4; i++)
813 vertices[i].Pos.rotateXZBy(-90);
817 for(u16 i=0; i<4; i++)
818 vertices[i].Pos.rotateXZBy(90);
822 for(u16 i=0; i<4; i++)
823 vertices[i].Pos.rotateYZBy(-90);
827 for(u16 i=0; i<4; i++)
828 vertices[i].Pos.rotateYZBy(90);
831 for(u16 i=0; i<4; i++)
833 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
836 u16 indices[] = {0,1,2,2,3,0};
837 // Add to mesh collector
838 collector.append(material_glass, vertices, 4, indices, 6);
844 else if(n.getContent() == CONTENT_FENCE)
846 u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio, nodedef)));
847 video::SColor c = MapBlock_LightColor(255, l);
849 const f32 post_rad=(f32)BS/10;
850 const f32 bar_rad=(f32)BS/20;
851 const f32 bar_len=(f32)(BS/2)-post_rad;
853 // The post - always present
854 v3f pos = intToFloat(p+blockpos_nodes, BS);
862 makeCuboid(material_wood, &collector,
864 post_rad,BS/2,post_rad, postuv);
866 // Now a section of fence, +X, if there's a post there
869 MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
870 if(n2.getContent() == CONTENT_FENCE)
872 pos = intToFloat(p+blockpos_nodes, BS);
882 makeCuboid(material_wood, &collector,
884 bar_len,bar_rad,bar_rad, xrailuv);
887 makeCuboid(material_wood, &collector,
889 bar_len,bar_rad,bar_rad, xrailuv);
892 // Now a section of fence, +Z, if there's a post there
895 n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
896 if(n2.getContent() == CONTENT_FENCE)
898 pos = intToFloat(p+blockpos_nodes, BS);
908 makeCuboid(material_wood, &collector,
910 bar_rad,bar_rad,bar_len, zrailuv);
912 makeCuboid(material_wood, &collector,
914 bar_rad,bar_rad,bar_len, zrailuv);
921 Add stones with minerals if stone is invisible
923 else if(n.getContent() == CONTENT_STONE && invisible_stone && n.getMineral(nodedef) != MINERAL_NONE)
925 for(u32 j=0; j<6; j++)
927 // NOTE: Hopefully g_6dirs[j] is the right direction...
928 v3s16 dir = g_6dirs[j];
930 MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + dir);
931 if(nodedef->get(n2).param_type == CPT_LIGHT)
932 l = decode_light(n2.getLightBlend(data->m_daynight_ratio, nodedef));
936 video::SColor c = MapBlock_LightColor(255, l);
938 // Get the right texture
939 TileSpec ts = n.getTile(dir, tsrc, nodedef);
940 AtlasPointer ap = ts.texture;
941 material_general.setTexture(0, ap.atlas);
943 video::S3DVertex vertices[4] =
945 video::S3DVertex(-BS/2,-BS/2,BS/2, 0,0,0, c,
947 video::S3DVertex(BS/2,-BS/2,BS/2, 0,0,0, c,
949 video::S3DVertex(BS/2,BS/2,BS/2, 0,0,0, c,
951 video::S3DVertex(-BS/2,BS/2,BS/2, 0,0,0, c,
957 for(u16 i=0; i<4; i++)
958 vertices[i].Pos.rotateXZBy(0);
962 for(u16 i=0; i<4; i++)
963 vertices[i].Pos.rotateXZBy(180);
967 for(u16 i=0; i<4; i++)
968 vertices[i].Pos.rotateXZBy(-90);
972 for(u16 i=0; i<4; i++)
973 vertices[i].Pos.rotateXZBy(90);
977 for(u16 i=0; i<4; i++)
979 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
982 u16 indices[] = {0,1,2,2,3,0};
983 // Add to mesh collector
984 collector.append(material_general, vertices, 4, indices, 6);
988 else if(n.getContent() == CONTENT_PAPYRUS)
990 u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio, nodedef)));
991 video::SColor c = MapBlock_LightColor(255, l);
993 for(u32 j=0; j<4; j++)
995 video::S3DVertex vertices[4] =
997 video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c,
998 pa_papyrus.x0(), pa_papyrus.y1()),
999 video::S3DVertex(BS/2,-BS/2,0, 0,0,0, c,
1000 pa_papyrus.x1(), pa_papyrus.y1()),
1001 video::S3DVertex(BS/2,BS/2,0, 0,0,0, c,
1002 pa_papyrus.x1(), pa_papyrus.y0()),
1003 video::S3DVertex(-BS/2,BS/2,0, 0,0,0, c,
1004 pa_papyrus.x0(), pa_papyrus.y0()),
1009 for(u16 i=0; i<4; i++)
1010 vertices[i].Pos.rotateXZBy(45);
1014 for(u16 i=0; i<4; i++)
1015 vertices[i].Pos.rotateXZBy(-45);
1019 for(u16 i=0; i<4; i++)
1020 vertices[i].Pos.rotateXZBy(135);
1024 for(u16 i=0; i<4; i++)
1025 vertices[i].Pos.rotateXZBy(-135);
1028 for(u16 i=0; i<4; i++)
1030 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
1033 u16 indices[] = {0,1,2,2,3,0};
1034 // Add to mesh collector
1035 collector.append(material_papyrus, vertices, 4, indices, 6);
1038 else if(n.getContent() == CONTENT_JUNGLEGRASS)
1040 u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio, nodedef)));
1041 video::SColor c = MapBlock_LightColor(255, l);
1043 for(u32 j=0; j<4; j++)
1045 video::S3DVertex vertices[4] =
1047 video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c,
1048 pa_junglegrass.x0(), pa_junglegrass.y1()),
1049 video::S3DVertex(BS/2,-BS/2,0, 0,0,0, c,
1050 pa_junglegrass.x1(), pa_junglegrass.y1()),
1051 video::S3DVertex(BS/2,BS/1,0, 0,0,0, c,
1052 pa_junglegrass.x1(), pa_junglegrass.y0()),
1053 video::S3DVertex(-BS/2,BS/1,0, 0,0,0, c,
1054 pa_junglegrass.x0(), pa_junglegrass.y0()),
1059 for(u16 i=0; i<4; i++)
1060 vertices[i].Pos.rotateXZBy(45);
1064 for(u16 i=0; i<4; i++)
1065 vertices[i].Pos.rotateXZBy(-45);
1069 for(u16 i=0; i<4; i++)
1070 vertices[i].Pos.rotateXZBy(135);
1074 for(u16 i=0; i<4; i++)
1075 vertices[i].Pos.rotateXZBy(-135);
1078 for(u16 i=0; i<4; i++)
1080 vertices[i].Pos *= 1.3;
1081 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
1084 u16 indices[] = {0,1,2,2,3,0};
1085 // Add to mesh collector
1086 collector.append(material_junglegrass, vertices, 4, indices, 6);
1089 else if(n.getContent() == CONTENT_RAIL)
1091 bool is_rail_x [] = { false, false }; /* x-1, x+1 */
1092 bool is_rail_z [] = { false, false }; /* z-1, z+1 */
1094 MapNode n_minus_x = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x-1,y,z));
1095 MapNode n_plus_x = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x+1,y,z));
1096 MapNode n_minus_z = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y,z-1));
1097 MapNode n_plus_z = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y,z+1));
1099 if(n_minus_x.getContent() == CONTENT_RAIL)
1100 is_rail_x[0] = true;
1101 if(n_plus_x.getContent() == CONTENT_RAIL)
1102 is_rail_x[1] = true;
1103 if(n_minus_z.getContent() == CONTENT_RAIL)
1104 is_rail_z[0] = true;
1105 if(n_plus_z.getContent() == CONTENT_RAIL)
1106 is_rail_z[1] = true;
1108 int adjacencies = is_rail_x[0] + is_rail_x[1] + is_rail_z[0] + is_rail_z[1];
1111 const char *texturename = "rail.png";
1113 texturename = "rail.png";
1114 else if(adjacencies == 2)
1116 if((is_rail_x[0] && is_rail_x[1]) || (is_rail_z[0] && is_rail_z[1]))
1117 texturename = "rail.png";
1119 texturename = "rail_curved.png";
1121 else if(adjacencies == 3)
1122 texturename = "rail_t_junction.png";
1123 else if(adjacencies == 4)
1124 texturename = "rail_crossing.png";
1126 AtlasPointer ap = tsrc->getTexture(texturename);
1128 video::SMaterial material_rail;
1129 material_rail.setFlag(video::EMF_LIGHTING, false);
1130 material_rail.setFlag(video::EMF_BACK_FACE_CULLING, false);
1131 material_rail.setFlag(video::EMF_BILINEAR_FILTER, false);
1132 material_rail.setFlag(video::EMF_FOG_ENABLE, true);
1133 material_rail.MaterialType
1134 = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
1135 material_rail.setTexture(0, ap.atlas);
1137 u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio, nodedef));
1138 video::SColor c = MapBlock_LightColor(255, l);
1140 float d = (float)BS/16;
1141 video::S3DVertex vertices[4] =
1143 video::S3DVertex(-BS/2,-BS/2+d,-BS/2, 0,0,0, c,
1145 video::S3DVertex(BS/2,-BS/2+d,-BS/2, 0,0,0, c,
1147 video::S3DVertex(BS/2,-BS/2+d,BS/2, 0,0,0, c,
1149 video::S3DVertex(-BS/2,-BS/2+d,BS/2, 0,0,0, c,
1156 if(adjacencies == 1)
1158 if(is_rail_x[0] || is_rail_x[1])
1161 else if(adjacencies == 2)
1163 if(is_rail_x[0] && is_rail_x[1])
1165 else if(is_rail_x[0] && is_rail_z[0])
1167 else if(is_rail_x[0] && is_rail_z[1])
1169 else if(is_rail_x[1] && is_rail_z[1])
1172 else if(adjacencies == 3)
1185 for(u16 i=0; i<4; i++)
1186 vertices[i].Pos.rotateXZBy(angle);
1189 for(s32 i=0; i<4; i++)
1191 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
1194 u16 indices[] = {0,1,2,2,3,0};
1195 collector.append(material_rail, vertices, 4, indices, 6);
1197 else if (n.getContent() == CONTENT_LADDER) {
1198 AtlasPointer ap = tsrc->getTexture("ladder.png");
1201 video::SMaterial material_ladder;
1202 material_ladder.setFlag(video::EMF_LIGHTING, false);
1203 material_ladder.setFlag(video::EMF_BACK_FACE_CULLING, false);
1204 material_ladder.setFlag(video::EMF_BILINEAR_FILTER, false);
1205 material_ladder.setFlag(video::EMF_FOG_ENABLE, true);
1206 material_ladder.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
1207 material_ladder.setTexture(0, ap.atlas);
1209 u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio, nodedef));
1210 video::SColor c(255,l,l,l);
1212 float d = (float)BS/16;
1214 // Assume wall is at X+
1215 video::S3DVertex vertices[4] =
1217 video::S3DVertex(BS/2-d,-BS/2,-BS/2, 0,0,0, c,
1219 video::S3DVertex(BS/2-d,-BS/2,BS/2, 0,0,0, c,
1221 video::S3DVertex(BS/2-d,BS/2,BS/2, 0,0,0, c,
1223 video::S3DVertex(BS/2-d,BS/2,-BS/2, 0,0,0, c,
1227 v3s16 dir = unpackDir(n.param2);
1229 for(s32 i=0; i<4; i++)
1231 if(dir == v3s16(1,0,0))
1232 vertices[i].Pos.rotateXZBy(0);
1233 if(dir == v3s16(-1,0,0))
1234 vertices[i].Pos.rotateXZBy(180);
1235 if(dir == v3s16(0,0,1))
1236 vertices[i].Pos.rotateXZBy(90);
1237 if(dir == v3s16(0,0,-1))
1238 vertices[i].Pos.rotateXZBy(-90);
1239 if(dir == v3s16(0,-1,0))
1240 vertices[i].Pos.rotateXYBy(-90);
1241 if(dir == v3s16(0,1,0))
1242 vertices[i].Pos.rotateXYBy(90);
1244 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
1247 u16 indices[] = {0,1,2,2,3,0};
1248 // Add to mesh collector
1249 collector.append(material_ladder, vertices, 4, indices, 6);
1251 else if(n.getContent() == CONTENT_APPLE)
1253 u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio, nodedef)));
1254 video::SColor c = MapBlock_LightColor(255, l);
1256 for(u32 j=0; j<4; j++)
1258 video::S3DVertex vertices[4] =
1260 video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c,
1261 pa_apple.x0(), pa_apple.y1()),
1262 video::S3DVertex(BS/2,-BS/2,0, 0,0,0, c,
1263 pa_apple.x1(), pa_apple.y1()),
1264 video::S3DVertex(BS/2,BS/2,0, 0,0,0, c,
1265 pa_apple.x1(), pa_apple.y0()),
1266 video::S3DVertex(-BS/2,BS/2,0, 0,0,0, c,
1267 pa_apple.x0(), pa_apple.y0()),
1272 for(u16 i=0; i<4; i++)
1273 vertices[i].Pos.rotateXZBy(45);
1277 for(u16 i=0; i<4; i++)
1278 vertices[i].Pos.rotateXZBy(-45);
1282 for(u16 i=0; i<4; i++)
1283 vertices[i].Pos.rotateXZBy(135);
1287 for(u16 i=0; i<4; i++)
1288 vertices[i].Pos.rotateXZBy(-135);
1291 for(u16 i=0; i<4; i++)
1293 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
1296 u16 indices[] = {0,1,2,2,3,0};
1297 // Add to mesh collector
1298 collector.append(material_apple, vertices, 4, indices, 6);
1301 else if(n.getContent() == CONTENT_SAPLING) {
1302 u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio, nodedef)));
1303 video::SColor c = MapBlock_LightColor(255, l);
1305 for(u32 j=0; j<4; j++)
1307 video::S3DVertex vertices[4] =
1309 video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c,
1310 pa_sapling.x0(), pa_sapling.y1()),
1311 video::S3DVertex(BS/2,-BS/2,0, 0,0,0, c,
1312 pa_sapling.x1(), pa_sapling.y1()),
1313 video::S3DVertex(BS/2,BS/1,0, 0,0,0, c,
1314 pa_sapling.x1(), pa_sapling.y0()),
1315 video::S3DVertex(-BS/2,BS/1,0, 0,0,0, c,
1316 pa_sapling.x0(), pa_sapling.y0()),
1321 for(u16 i=0; i<4; i++)
1322 vertices[i].Pos.rotateXZBy(45);
1326 for(u16 i=0; i<4; i++)
1327 vertices[i].Pos.rotateXZBy(-45);
1331 for(u16 i=0; i<4; i++)
1332 vertices[i].Pos.rotateXZBy(135);
1336 for(u16 i=0; i<4; i++)
1337 vertices[i].Pos.rotateXZBy(-135);
1340 for(u16 i=0; i<4; i++)
1342 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
1345 u16 indices[] = {0,1,2,2,3,0};
1346 // Add to mesh collector
1347 collector.append(material_sapling, vertices, 4, indices, 6);