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 and g_texturesource
24 #include "mapblock_mesh.h" // For MapBlock_LightColor()
29 // material - the material to use (for all 6 faces)
30 // collector - the MeshCollector for the resulting polygons
31 // pa - texture atlas pointer for the material
32 // c - vertex colour - used for all
33 // pos - the position of the centre of the cuboid
34 // rz,ry,rz - the radius of the cuboid in each dimension
35 // txc - texture coordinates - this is a list of texture coordinates
36 // for the opposite corners of each face - therefore, there
37 // should be (2+2)*6=24 values in the list. Alternatively, pass
38 // NULL to use the entire texture for each face. The order of
39 // the faces in the list is top-backi-right-front-left-bottom
40 // If you specified 0,0,1,1 for each face, that would be the
41 // same as passing NULL.
42 void makeCuboid(video::SMaterial &material, MeshCollector *collector,
43 AtlasPointer* pa, video::SColor &c,
44 v3f &pos, f32 rx, f32 ry, f32 rz, f32* txc)
53 video::S3DVertex v[4] =
55 video::S3DVertex(0,0,0, 0,0,0, c, tu0, tv1),
56 video::S3DVertex(0,0,0, 0,0,0, c, tu1, tv1),
57 video::S3DVertex(0,0,0, 0,0,0, c, tu1, tv0),
58 video::S3DVertex(0,0,0, 0,0,0, c, tu0, tv0)
66 v[0].Pos.X=-rx; v[0].Pos.Y= ry; v[0].Pos.Z=-rz;
67 v[1].Pos.X=-rx; v[1].Pos.Y= ry; v[1].Pos.Z= rz;
68 v[2].Pos.X= rx; v[2].Pos.Y= ry; v[2].Pos.Z= rz;
69 v[3].Pos.X= rx; v[3].Pos.Y= ry, v[3].Pos.Z=-rz;
72 v[0].Pos.X=-rx; v[0].Pos.Y= ry; v[0].Pos.Z=-rz;
73 v[1].Pos.X= rx; v[1].Pos.Y= ry; v[1].Pos.Z=-rz;
74 v[2].Pos.X= rx; v[2].Pos.Y=-ry; v[2].Pos.Z=-rz;
75 v[3].Pos.X=-rx; v[3].Pos.Y=-ry, v[3].Pos.Z=-rz;
78 v[0].Pos.X= rx; v[0].Pos.Y= ry; v[0].Pos.Z=-rz;
79 v[1].Pos.X= rx; v[1].Pos.Y= ry; v[1].Pos.Z= rz;
80 v[2].Pos.X= rx; v[2].Pos.Y=-ry; v[2].Pos.Z= rz;
81 v[3].Pos.X= rx; v[3].Pos.Y=-ry, v[3].Pos.Z=-rz;
84 v[0].Pos.X= rx; v[0].Pos.Y= ry; v[0].Pos.Z= rz;
85 v[1].Pos.X=-rx; v[1].Pos.Y= ry; v[1].Pos.Z= rz;
86 v[2].Pos.X=-rx; v[2].Pos.Y=-ry; v[2].Pos.Z= rz;
87 v[3].Pos.X= rx; v[3].Pos.Y=-ry, v[3].Pos.Z= rz;
90 v[0].Pos.X=-rx; v[0].Pos.Y= ry; v[0].Pos.Z= rz;
91 v[1].Pos.X=-rx; v[1].Pos.Y= ry; v[1].Pos.Z=-rz;
92 v[2].Pos.X=-rx; v[2].Pos.Y=-ry; v[2].Pos.Z=-rz;
93 v[3].Pos.X=-rx; v[3].Pos.Y=-ry, v[3].Pos.Z= rz;
96 v[0].Pos.X= rx; v[0].Pos.Y=-ry; v[0].Pos.Z= rz;
97 v[1].Pos.X=-rx; v[1].Pos.Y=-ry; v[1].Pos.Z= rz;
98 v[2].Pos.X=-rx; v[2].Pos.Y=-ry; v[2].Pos.Z=-rz;
99 v[3].Pos.X= rx; v[3].Pos.Y=-ry, v[3].Pos.Z=-rz;
105 v[0].TCoords.X=tu0+txus*txc[0]; v[0].TCoords.Y=tv0+txvs*txc[3];
106 v[1].TCoords.X=tu0+txus*txc[2]; v[1].TCoords.Y=tv0+txvs*txc[3];
107 v[2].TCoords.X=tu0+txus*txc[2]; v[2].TCoords.Y=tv0+txvs*txc[1];
108 v[3].TCoords.X=tu0+txus*txc[0]; v[3].TCoords.Y=tv0+txvs*txc[1];
112 for(u16 i=0; i<4; i++)
114 u16 indices[] = {0,1,2,2,3,0};
115 collector->append(material, v, 4, indices, 6);
123 void mapblock_mesh_generate_special(MeshMakeData *data,
124 MeshCollector &collector)
127 //TimeTaker timer("mapblock_mesh_generate_special()");
132 bool new_style_water = g_settings->getBool("new_style_water");
133 bool new_style_leaves = g_settings->getBool("new_style_leaves");
134 //bool smooth_lighting = g_settings->getBool("smooth_lighting");
135 bool invisible_stone = g_settings->getBool("invisible_stone");
137 float node_liquid_level = 1.0;
139 node_liquid_level = 0.85;
141 v3s16 blockpos_nodes = data->m_blockpos*MAP_BLOCKSIZE;
143 // New-style leaves material
144 video::SMaterial material_leaves1;
145 material_leaves1.setFlag(video::EMF_LIGHTING, false);
146 material_leaves1.setFlag(video::EMF_BILINEAR_FILTER, false);
147 material_leaves1.setFlag(video::EMF_FOG_ENABLE, true);
148 material_leaves1.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
149 AtlasPointer pa_leaves1 = g_texturesource->getTexture(
150 g_texturesource->getTextureId("leaves.png"));
151 material_leaves1.setTexture(0, pa_leaves1.atlas);
154 video::SMaterial material_glass;
155 material_glass.setFlag(video::EMF_LIGHTING, false);
156 material_glass.setFlag(video::EMF_BILINEAR_FILTER, false);
157 material_glass.setFlag(video::EMF_FOG_ENABLE, true);
158 material_glass.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
159 AtlasPointer pa_glass = g_texturesource->getTexture(
160 g_texturesource->getTextureId("glass.png"));
161 material_glass.setTexture(0, pa_glass.atlas);
164 video::SMaterial material_wood;
165 material_wood.setFlag(video::EMF_LIGHTING, false);
166 material_wood.setFlag(video::EMF_BILINEAR_FILTER, false);
167 material_wood.setFlag(video::EMF_FOG_ENABLE, true);
168 material_wood.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
169 AtlasPointer pa_wood = g_texturesource->getTexture(
170 g_texturesource->getTextureId("wood.png"));
171 material_wood.setTexture(0, pa_wood.atlas);
173 // General ground material for special output
174 // Texture is modified just before usage
175 video::SMaterial material_general;
176 material_general.setFlag(video::EMF_LIGHTING, false);
177 material_general.setFlag(video::EMF_BILINEAR_FILTER, false);
178 material_general.setFlag(video::EMF_FOG_ENABLE, true);
179 material_general.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
183 video::SMaterial material_papyrus;
184 material_papyrus.setFlag(video::EMF_LIGHTING, false);
185 material_papyrus.setFlag(video::EMF_BILINEAR_FILTER, false);
186 material_papyrus.setFlag(video::EMF_FOG_ENABLE, true);
187 material_papyrus.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
188 AtlasPointer pa_papyrus = g_texturesource->getTexture(
189 g_texturesource->getTextureId("papyrus.png"));
190 material_papyrus.setTexture(0, pa_papyrus.atlas);
193 video::SMaterial material_apple;
194 material_apple.setFlag(video::EMF_LIGHTING, false);
195 material_apple.setFlag(video::EMF_BILINEAR_FILTER, false);
196 material_apple.setFlag(video::EMF_FOG_ENABLE, true);
197 material_apple.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
198 AtlasPointer pa_apple = g_texturesource->getTexture(
199 g_texturesource->getTextureId("apple.png"));
200 material_apple.setTexture(0, pa_apple.atlas);
204 video::SMaterial material_sapling;
205 material_sapling.setFlag(video::EMF_LIGHTING, false);
206 material_sapling.setFlag(video::EMF_BILINEAR_FILTER, false);
207 material_sapling.setFlag(video::EMF_FOG_ENABLE, true);
208 material_sapling.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
209 AtlasPointer pa_sapling = g_texturesource->getTexture(
210 g_texturesource->getTextureId("sapling.png"));
211 material_sapling.setTexture(0, pa_sapling.atlas);
214 // junglegrass material
215 video::SMaterial material_junglegrass;
216 material_junglegrass.setFlag(video::EMF_LIGHTING, false);
217 material_junglegrass.setFlag(video::EMF_BILINEAR_FILTER, false);
218 material_junglegrass.setFlag(video::EMF_FOG_ENABLE, true);
219 material_junglegrass.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
220 AtlasPointer pa_junglegrass = g_texturesource->getTexture(
221 g_texturesource->getTextureId("junglegrass.png"));
222 material_junglegrass.setTexture(0, pa_junglegrass.atlas);
224 for(s16 z=0; z<MAP_BLOCKSIZE; z++)
225 for(s16 y=0; y<MAP_BLOCKSIZE; y++)
226 for(s16 x=0; x<MAP_BLOCKSIZE; x++)
230 MapNode n = data->m_vmanip.getNodeNoEx(blockpos_nodes+p);
235 if(n.getContent() == CONTENT_TORCH)
237 v3s16 dir = unpackDir(n.param2);
239 const char *texturename = "torch.png";
240 if(dir == v3s16(0,-1,0)){
241 texturename = "torch_on_floor.png";
242 } else if(dir == v3s16(0,1,0)){
243 texturename = "torch_on_ceiling.png";
244 // For backwards compatibility
245 } else if(dir == v3s16(0,0,0)){
246 texturename = "torch_on_floor.png";
248 texturename = "torch.png";
251 AtlasPointer ap = g_texturesource->getTexture(texturename);
254 video::SMaterial material;
255 material.setFlag(video::EMF_LIGHTING, false);
256 material.setFlag(video::EMF_BACK_FACE_CULLING, false);
257 material.setFlag(video::EMF_BILINEAR_FILTER, false);
258 material.setFlag(video::EMF_FOG_ENABLE, true);
259 //material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
260 material.MaterialType
261 = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
262 material.setTexture(0, ap.atlas);
264 video::SColor c(255,255,255,255);
266 // Wall at X+ of node
267 video::S3DVertex vertices[4] =
269 video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c,
271 video::S3DVertex(BS/2,-BS/2,0, 0,0,0, c,
273 video::S3DVertex(BS/2,BS/2,0, 0,0,0, c,
275 video::S3DVertex(-BS/2,BS/2,0, 0,0,0, c,
279 for(s32 i=0; i<4; i++)
281 if(dir == v3s16(1,0,0))
282 vertices[i].Pos.rotateXZBy(0);
283 if(dir == v3s16(-1,0,0))
284 vertices[i].Pos.rotateXZBy(180);
285 if(dir == v3s16(0,0,1))
286 vertices[i].Pos.rotateXZBy(90);
287 if(dir == v3s16(0,0,-1))
288 vertices[i].Pos.rotateXZBy(-90);
289 if(dir == v3s16(0,-1,0))
290 vertices[i].Pos.rotateXZBy(45);
291 if(dir == v3s16(0,1,0))
292 vertices[i].Pos.rotateXZBy(-45);
294 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
297 u16 indices[] = {0,1,2,2,3,0};
298 // Add to mesh collector
299 collector.append(material, vertices, 4, indices, 6);
304 else if(n.getContent() == CONTENT_SIGN_WALL)
306 AtlasPointer ap = g_texturesource->getTexture("sign_wall.png");
309 video::SMaterial material;
310 material.setFlag(video::EMF_LIGHTING, false);
311 material.setFlag(video::EMF_BACK_FACE_CULLING, false);
312 material.setFlag(video::EMF_BILINEAR_FILTER, false);
313 material.setFlag(video::EMF_FOG_ENABLE, true);
314 material.MaterialType
315 = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
316 material.setTexture(0, ap.atlas);
318 u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio));
319 video::SColor c = MapBlock_LightColor(255, l);
321 float d = (float)BS/16;
322 // Wall at X+ of node
323 video::S3DVertex vertices[4] =
325 video::S3DVertex(BS/2-d,-BS/2,-BS/2, 0,0,0, c,
327 video::S3DVertex(BS/2-d,-BS/2,BS/2, 0,0,0, c,
329 video::S3DVertex(BS/2-d,BS/2,BS/2, 0,0,0, c,
331 video::S3DVertex(BS/2-d,BS/2,-BS/2, 0,0,0, c,
335 v3s16 dir = unpackDir(n.param2);
337 for(s32 i=0; i<4; i++)
339 if(dir == v3s16(1,0,0))
340 vertices[i].Pos.rotateXZBy(0);
341 if(dir == v3s16(-1,0,0))
342 vertices[i].Pos.rotateXZBy(180);
343 if(dir == v3s16(0,0,1))
344 vertices[i].Pos.rotateXZBy(90);
345 if(dir == v3s16(0,0,-1))
346 vertices[i].Pos.rotateXZBy(-90);
347 if(dir == v3s16(0,-1,0))
348 vertices[i].Pos.rotateXYBy(-90);
349 if(dir == v3s16(0,1,0))
350 vertices[i].Pos.rotateXYBy(90);
352 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
355 u16 indices[] = {0,1,2,2,3,0};
356 // Add to mesh collector
357 collector.append(material, vertices, 4, indices, 6);
360 Add flowing liquid to mesh
362 else if(content_features(n).liquid_type == LIQUID_FLOWING)
364 assert(content_features(n).special_material);
365 video::SMaterial &liquid_material =
366 *content_features(n).special_material;
367 video::SMaterial &liquid_material_bfculled =
368 *content_features(n).special_material2;
370 assert(content_features(n).special_atlas);
371 AtlasPointer &pa_liquid1 =
372 *content_features(n).special_atlas;
374 bool top_is_same_liquid = false;
375 MapNode ntop = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y+1,z));
376 content_t c_flowing = content_features(n).liquid_alternative_flowing;
377 content_t c_source = content_features(n).liquid_alternative_source;
378 if(ntop.getContent() == c_flowing || ntop.getContent() == c_source)
379 top_is_same_liquid = true;
382 // Use the light of the node on top if possible
383 if(content_features(ntop).param_type == CPT_LIGHT)
384 l = decode_light(ntop.getLightBlend(data->m_daynight_ratio));
385 // Otherwise use the light of this node (the liquid)
387 l = decode_light(n.getLightBlend(data->m_daynight_ratio));
388 video::SColor c = MapBlock_LightColor(
389 content_features(n).vertex_alpha, l);
391 // Neighbor liquid levels (key = relative position)
392 // Includes current node
393 core::map<v3s16, f32> neighbor_levels;
394 core::map<v3s16, content_t> neighbor_contents;
395 core::map<v3s16, u8> neighbor_flags;
396 const u8 neighborflag_top_is_same_liquid = 0x01;
397 v3s16 neighbor_dirs[9] = {
408 for(u32 i=0; i<9; i++)
410 content_t content = CONTENT_AIR;
411 float level = -0.5 * BS;
414 v3s16 p2 = p + neighbor_dirs[i];
415 MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
416 if(n2.getContent() != CONTENT_IGNORE)
418 content = n2.getContent();
420 if(n2.getContent() == c_source)
421 level = (-0.5+node_liquid_level) * BS;
422 else if(n2.getContent() == c_flowing)
423 level = (-0.5 + ((float)(n2.param2&LIQUID_LEVEL_MASK)
424 + 0.5) / 8.0 * node_liquid_level) * BS;
426 // Check node above neighbor.
427 // NOTE: This doesn't get executed if neighbor
430 n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
431 if(n2.getContent() == c_source ||
432 n2.getContent() == c_flowing)
433 flags |= neighborflag_top_is_same_liquid;
436 neighbor_levels.insert(neighbor_dirs[i], level);
437 neighbor_contents.insert(neighbor_dirs[i], content);
438 neighbor_flags.insert(neighbor_dirs[i], flags);
441 // Corner heights (average between four liquids)
442 f32 corner_levels[4];
444 v3s16 halfdirs[4] = {
450 for(u32 i=0; i<4; i++)
452 v3s16 cornerdir = halfdirs[i];
453 float cornerlevel = 0;
456 for(u32 j=0; j<4; j++)
458 v3s16 neighbordir = cornerdir - halfdirs[j];
459 content_t content = neighbor_contents[neighbordir];
460 // If top is liquid, draw starting from top of node
461 if(neighbor_flags[neighbordir] &
462 neighborflag_top_is_same_liquid)
464 cornerlevel = 0.5*BS;
468 // Source is always the same height
469 else if(content == c_source)
471 cornerlevel = (-0.5+node_liquid_level)*BS;
475 // Flowing liquid has level information
476 else if(content == c_flowing)
478 cornerlevel += neighbor_levels[neighbordir];
481 else if(content == CONTENT_AIR)
487 cornerlevel = -0.5*BS;
488 else if(valid_count > 0)
489 cornerlevel /= valid_count;
490 corner_levels[i] = cornerlevel;
497 v3s16 side_dirs[4] = {
503 s16 side_corners[4][2] = {
509 for(u32 i=0; i<4; i++)
511 v3s16 dir = side_dirs[i];
514 If our topside is liquid and neighbor's topside
515 is liquid, don't draw side face
517 if(top_is_same_liquid &&
518 neighbor_flags[dir] & neighborflag_top_is_same_liquid)
521 content_t neighbor_content = neighbor_contents[dir];
522 ContentFeatures &n_feat = content_features(neighbor_content);
524 // Don't draw face if neighbor is blocking the view
525 if(n_feat.solidness == 2)
528 bool neighbor_is_same_liquid = (neighbor_content == c_source
529 || neighbor_content == c_flowing);
531 // Don't draw any faces if neighbor same is liquid and top is
533 if(neighbor_is_same_liquid == true
534 && top_is_same_liquid == false)
537 // Use backface culled material if neighbor doesn't have a
539 video::SMaterial *current_material = &liquid_material;
540 if(n_feat.solidness != 0 || n_feat.visual_solidness != 0)
541 current_material = &liquid_material_bfculled;
543 video::S3DVertex vertices[4] =
545 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
546 pa_liquid1.x0(), pa_liquid1.y1()),
547 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
548 pa_liquid1.x1(), pa_liquid1.y1()),
549 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
550 pa_liquid1.x1(), pa_liquid1.y0()),
551 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
552 pa_liquid1.x0(), pa_liquid1.y0()),
556 If our topside is liquid, set upper border of face
557 at upper border of node
559 if(top_is_same_liquid)
561 vertices[2].Pos.Y = 0.5*BS;
562 vertices[3].Pos.Y = 0.5*BS;
565 Otherwise upper position of face is corner levels
569 vertices[2].Pos.Y = corner_levels[side_corners[i][0]];
570 vertices[3].Pos.Y = corner_levels[side_corners[i][1]];
574 If neighbor is liquid, lower border of face is corner
577 if(neighbor_is_same_liquid)
579 vertices[0].Pos.Y = corner_levels[side_corners[i][1]];
580 vertices[1].Pos.Y = corner_levels[side_corners[i][0]];
583 If neighbor is not liquid, lower border of face is
588 vertices[0].Pos.Y = -0.5*BS;
589 vertices[1].Pos.Y = -0.5*BS;
592 for(s32 j=0; j<4; j++)
594 if(dir == v3s16(0,0,1))
595 vertices[j].Pos.rotateXZBy(0);
596 if(dir == v3s16(0,0,-1))
597 vertices[j].Pos.rotateXZBy(180);
598 if(dir == v3s16(-1,0,0))
599 vertices[j].Pos.rotateXZBy(90);
600 if(dir == v3s16(1,0,-0))
601 vertices[j].Pos.rotateXZBy(-90);
603 // Do this to not cause glitches when two liquids are
605 /*if(neighbor_is_same_liquid == false){
606 vertices[j].Pos.X *= 0.98;
607 vertices[j].Pos.Z *= 0.98;
610 vertices[j].Pos += intToFloat(p + blockpos_nodes, BS);
613 u16 indices[] = {0,1,2,2,3,0};
614 // Add to mesh collector
615 collector.append(*current_material, vertices, 4, indices, 6);
619 Generate top side, if appropriate
622 if(top_is_same_liquid == false)
624 video::S3DVertex vertices[4] =
626 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
627 pa_liquid1.x0(), pa_liquid1.y1()),
628 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
629 pa_liquid1.x1(), pa_liquid1.y1()),
630 video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c,
631 pa_liquid1.x1(), pa_liquid1.y0()),
632 video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c,
633 pa_liquid1.x0(), pa_liquid1.y0()),
636 // This fixes a strange bug
637 s32 corner_resolve[4] = {3,2,1,0};
639 for(s32 i=0; i<4; i++)
641 //vertices[i].Pos.Y += liquid_level;
642 //vertices[i].Pos.Y += neighbor_levels[v3s16(0,0,0)];
643 s32 j = corner_resolve[i];
644 vertices[i].Pos.Y += corner_levels[j];
645 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
648 u16 indices[] = {0,1,2,2,3,0};
649 // Add to mesh collector
650 collector.append(liquid_material, vertices, 4, indices, 6);
654 Add water sources to mesh if using new style
656 else if(content_features(n).liquid_type == LIQUID_SOURCE
659 assert(content_features(n).special_material);
660 video::SMaterial &liquid_material =
661 *content_features(n).special_material;
662 assert(content_features(n).special_atlas);
663 AtlasPointer &pa_liquid1 =
664 *content_features(n).special_atlas;
666 bool top_is_air = false;
667 MapNode n = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y+1,z));
668 if(n.getContent() == CONTENT_AIR)
671 if(top_is_air == false)
674 u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio));
675 video::SColor c = MapBlock_LightColor(
676 content_features(n).vertex_alpha, l);
678 video::S3DVertex vertices[4] =
680 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
681 pa_liquid1.x0(), pa_liquid1.y1()),
682 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
683 pa_liquid1.x1(), pa_liquid1.y1()),
684 video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c,
685 pa_liquid1.x1(), pa_liquid1.y0()),
686 video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c,
687 pa_liquid1.x0(), pa_liquid1.y0()),
690 for(s32 i=0; i<4; i++)
692 vertices[i].Pos.Y += (-0.5+node_liquid_level)*BS;
693 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
696 u16 indices[] = {0,1,2,2,3,0};
697 // Add to mesh collector
698 collector.append(liquid_material, vertices, 4, indices, 6);
701 Add leaves if using new style
703 else if(n.getContent() == CONTENT_LEAVES && new_style_leaves)
705 /*u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio));*/
706 u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio)));
707 video::SColor c = MapBlock_LightColor(255, l);
709 for(u32 j=0; j<6; j++)
711 video::S3DVertex vertices[4] =
713 video::S3DVertex(-BS/2,-BS/2,BS/2, 0,0,0, c,
714 pa_leaves1.x0(), pa_leaves1.y1()),
715 video::S3DVertex(BS/2,-BS/2,BS/2, 0,0,0, c,
716 pa_leaves1.x1(), pa_leaves1.y1()),
717 video::S3DVertex(BS/2,BS/2,BS/2, 0,0,0, c,
718 pa_leaves1.x1(), pa_leaves1.y0()),
719 video::S3DVertex(-BS/2,BS/2,BS/2, 0,0,0, c,
720 pa_leaves1.x0(), pa_leaves1.y0()),
725 for(u16 i=0; i<4; i++)
726 vertices[i].Pos.rotateXZBy(0);
730 for(u16 i=0; i<4; i++)
731 vertices[i].Pos.rotateXZBy(180);
735 for(u16 i=0; i<4; i++)
736 vertices[i].Pos.rotateXZBy(-90);
740 for(u16 i=0; i<4; i++)
741 vertices[i].Pos.rotateXZBy(90);
745 for(u16 i=0; i<4; i++)
746 vertices[i].Pos.rotateYZBy(-90);
750 for(u16 i=0; i<4; i++)
751 vertices[i].Pos.rotateYZBy(90);
754 for(u16 i=0; i<4; i++)
756 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
759 u16 indices[] = {0,1,2,2,3,0};
760 // Add to mesh collector
761 collector.append(material_leaves1, vertices, 4, indices, 6);
767 else if(n.getContent() == CONTENT_GLASS)
769 u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio)));
770 video::SColor c = MapBlock_LightColor(255, l);
772 for(u32 j=0; j<6; j++)
774 video::S3DVertex vertices[4] =
776 video::S3DVertex(-BS/2,-BS/2,BS/2, 0,0,0, c,
777 pa_glass.x0(), pa_glass.y1()),
778 video::S3DVertex(BS/2,-BS/2,BS/2, 0,0,0, c,
779 pa_glass.x1(), pa_glass.y1()),
780 video::S3DVertex(BS/2,BS/2,BS/2, 0,0,0, c,
781 pa_glass.x1(), pa_glass.y0()),
782 video::S3DVertex(-BS/2,BS/2,BS/2, 0,0,0, c,
783 pa_glass.x0(), pa_glass.y0()),
788 for(u16 i=0; i<4; i++)
789 vertices[i].Pos.rotateXZBy(0);
793 for(u16 i=0; i<4; i++)
794 vertices[i].Pos.rotateXZBy(180);
798 for(u16 i=0; i<4; i++)
799 vertices[i].Pos.rotateXZBy(-90);
803 for(u16 i=0; i<4; i++)
804 vertices[i].Pos.rotateXZBy(90);
808 for(u16 i=0; i<4; i++)
809 vertices[i].Pos.rotateYZBy(-90);
813 for(u16 i=0; i<4; i++)
814 vertices[i].Pos.rotateYZBy(90);
817 for(u16 i=0; i<4; i++)
819 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
822 u16 indices[] = {0,1,2,2,3,0};
823 // Add to mesh collector
824 collector.append(material_glass, vertices, 4, indices, 6);
830 else if(n.getContent() == CONTENT_FENCE)
832 u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio)));
833 video::SColor c = MapBlock_LightColor(255, l);
835 const f32 post_rad=(f32)BS/10;
836 const f32 bar_rad=(f32)BS/20;
837 const f32 bar_len=(f32)(BS/2)-post_rad;
839 // The post - always present
840 v3f pos = intToFloat(p+blockpos_nodes, BS);
848 makeCuboid(material_wood, &collector,
850 post_rad,BS/2,post_rad, postuv);
852 // Now a section of fence, +X, if there's a post there
855 MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
856 if(n2.getContent() == CONTENT_FENCE)
858 pos = intToFloat(p+blockpos_nodes, BS);
868 makeCuboid(material_wood, &collector,
870 bar_len,bar_rad,bar_rad, xrailuv);
873 makeCuboid(material_wood, &collector,
875 bar_len,bar_rad,bar_rad, xrailuv);
878 // Now a section of fence, +Z, if there's a post there
881 n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
882 if(n2.getContent() == CONTENT_FENCE)
884 pos = intToFloat(p+blockpos_nodes, BS);
894 makeCuboid(material_wood, &collector,
896 bar_rad,bar_rad,bar_len, zrailuv);
898 makeCuboid(material_wood, &collector,
900 bar_rad,bar_rad,bar_len, zrailuv);
907 Add stones with minerals if stone is invisible
909 else if(n.getContent() == CONTENT_STONE && invisible_stone && n.getMineral() != MINERAL_NONE)
911 for(u32 j=0; j<6; j++)
913 // NOTE: Hopefully g_6dirs[j] is the right direction...
914 v3s16 dir = g_6dirs[j];
916 MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + dir);
917 if(content_features(n2).param_type == CPT_LIGHT)
918 l = decode_light(n2.getLightBlend(data->m_daynight_ratio));
922 video::SColor c = MapBlock_LightColor(255, l);
924 // Get the right texture
925 TileSpec ts = n.getTile(dir);
926 AtlasPointer ap = ts.texture;
927 material_general.setTexture(0, ap.atlas);
929 video::S3DVertex vertices[4] =
931 video::S3DVertex(-BS/2,-BS/2,BS/2, 0,0,0, c,
933 video::S3DVertex(BS/2,-BS/2,BS/2, 0,0,0, c,
935 video::S3DVertex(BS/2,BS/2,BS/2, 0,0,0, c,
937 video::S3DVertex(-BS/2,BS/2,BS/2, 0,0,0, c,
943 for(u16 i=0; i<4; i++)
944 vertices[i].Pos.rotateXZBy(0);
948 for(u16 i=0; i<4; i++)
949 vertices[i].Pos.rotateXZBy(180);
953 for(u16 i=0; i<4; i++)
954 vertices[i].Pos.rotateXZBy(-90);
958 for(u16 i=0; i<4; i++)
959 vertices[i].Pos.rotateXZBy(90);
963 for(u16 i=0; i<4; i++)
965 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
968 u16 indices[] = {0,1,2,2,3,0};
969 // Add to mesh collector
970 collector.append(material_general, vertices, 4, indices, 6);
974 else if(n.getContent() == CONTENT_PAPYRUS)
976 u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio)));
977 video::SColor c = MapBlock_LightColor(255, l);
979 for(u32 j=0; j<4; j++)
981 video::S3DVertex vertices[4] =
983 video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c,
984 pa_papyrus.x0(), pa_papyrus.y1()),
985 video::S3DVertex(BS/2,-BS/2,0, 0,0,0, c,
986 pa_papyrus.x1(), pa_papyrus.y1()),
987 video::S3DVertex(BS/2,BS/2,0, 0,0,0, c,
988 pa_papyrus.x1(), pa_papyrus.y0()),
989 video::S3DVertex(-BS/2,BS/2,0, 0,0,0, c,
990 pa_papyrus.x0(), pa_papyrus.y0()),
995 for(u16 i=0; i<4; i++)
996 vertices[i].Pos.rotateXZBy(45);
1000 for(u16 i=0; i<4; i++)
1001 vertices[i].Pos.rotateXZBy(-45);
1005 for(u16 i=0; i<4; i++)
1006 vertices[i].Pos.rotateXZBy(135);
1010 for(u16 i=0; i<4; i++)
1011 vertices[i].Pos.rotateXZBy(-135);
1014 for(u16 i=0; i<4; i++)
1016 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
1019 u16 indices[] = {0,1,2,2,3,0};
1020 // Add to mesh collector
1021 collector.append(material_papyrus, vertices, 4, indices, 6);
1024 else if(n.getContent() == CONTENT_JUNGLEGRASS)
1026 u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio)));
1027 video::SColor c = MapBlock_LightColor(255, l);
1029 for(u32 j=0; j<4; j++)
1031 video::S3DVertex vertices[4] =
1033 video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c,
1034 pa_junglegrass.x0(), pa_junglegrass.y1()),
1035 video::S3DVertex(BS/2,-BS/2,0, 0,0,0, c,
1036 pa_junglegrass.x1(), pa_junglegrass.y1()),
1037 video::S3DVertex(BS/2,BS/1,0, 0,0,0, c,
1038 pa_junglegrass.x1(), pa_junglegrass.y0()),
1039 video::S3DVertex(-BS/2,BS/1,0, 0,0,0, c,
1040 pa_junglegrass.x0(), pa_junglegrass.y0()),
1045 for(u16 i=0; i<4; i++)
1046 vertices[i].Pos.rotateXZBy(45);
1050 for(u16 i=0; i<4; i++)
1051 vertices[i].Pos.rotateXZBy(-45);
1055 for(u16 i=0; i<4; i++)
1056 vertices[i].Pos.rotateXZBy(135);
1060 for(u16 i=0; i<4; i++)
1061 vertices[i].Pos.rotateXZBy(-135);
1064 for(u16 i=0; i<4; i++)
1066 vertices[i].Pos *= 1.3;
1067 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
1070 u16 indices[] = {0,1,2,2,3,0};
1071 // Add to mesh collector
1072 collector.append(material_junglegrass, vertices, 4, indices, 6);
1075 else if(n.getContent() == CONTENT_RAIL)
1077 bool is_rail_x [] = { false, false }; /* x-1, x+1 */
1078 bool is_rail_z [] = { false, false }; /* z-1, z+1 */
1080 MapNode n_minus_x = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x-1,y,z));
1081 MapNode n_plus_x = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x+1,y,z));
1082 MapNode n_minus_z = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y,z-1));
1083 MapNode n_plus_z = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y,z+1));
1085 if(n_minus_x.getContent() == CONTENT_RAIL)
1086 is_rail_x[0] = true;
1087 if(n_plus_x.getContent() == CONTENT_RAIL)
1088 is_rail_x[1] = true;
1089 if(n_minus_z.getContent() == CONTENT_RAIL)
1090 is_rail_z[0] = true;
1091 if(n_plus_z.getContent() == CONTENT_RAIL)
1092 is_rail_z[1] = true;
1094 int adjacencies = is_rail_x[0] + is_rail_x[1] + is_rail_z[0] + is_rail_z[1];
1097 const char *texturename = "rail.png";
1099 texturename = "rail.png";
1100 else if(adjacencies == 2)
1102 if((is_rail_x[0] && is_rail_x[1]) || (is_rail_z[0] && is_rail_z[1]))
1103 texturename = "rail.png";
1105 texturename = "rail_curved.png";
1107 else if(adjacencies == 3)
1108 texturename = "rail_t_junction.png";
1109 else if(adjacencies == 4)
1110 texturename = "rail_crossing.png";
1112 AtlasPointer ap = g_texturesource->getTexture(texturename);
1114 video::SMaterial material_rail;
1115 material_rail.setFlag(video::EMF_LIGHTING, false);
1116 material_rail.setFlag(video::EMF_BACK_FACE_CULLING, false);
1117 material_rail.setFlag(video::EMF_BILINEAR_FILTER, false);
1118 material_rail.setFlag(video::EMF_FOG_ENABLE, true);
1119 material_rail.MaterialType
1120 = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
1121 material_rail.setTexture(0, ap.atlas);
1123 u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio));
1124 video::SColor c = MapBlock_LightColor(255, l);
1126 float d = (float)BS/16;
1127 video::S3DVertex vertices[4] =
1129 video::S3DVertex(-BS/2,-BS/2+d,-BS/2, 0,0,0, c,
1131 video::S3DVertex(BS/2,-BS/2+d,-BS/2, 0,0,0, c,
1133 video::S3DVertex(BS/2,-BS/2+d,BS/2, 0,0,0, c,
1135 video::S3DVertex(-BS/2,-BS/2+d,BS/2, 0,0,0, c,
1142 if(adjacencies == 1)
1144 if(is_rail_x[0] || is_rail_x[1])
1147 else if(adjacencies == 2)
1149 if(is_rail_x[0] && is_rail_x[1])
1151 else if(is_rail_x[0] && is_rail_z[0])
1153 else if(is_rail_x[0] && is_rail_z[1])
1155 else if(is_rail_x[1] && is_rail_z[1])
1158 else if(adjacencies == 3)
1171 for(u16 i=0; i<4; i++)
1172 vertices[i].Pos.rotateXZBy(angle);
1175 for(s32 i=0; i<4; i++)
1177 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
1180 u16 indices[] = {0,1,2,2,3,0};
1181 collector.append(material_rail, vertices, 4, indices, 6);
1183 else if (n.getContent() == CONTENT_LADDER) {
1184 AtlasPointer ap = g_texturesource->getTexture("ladder.png");
1187 video::SMaterial material_ladder;
1188 material_ladder.setFlag(video::EMF_LIGHTING, false);
1189 material_ladder.setFlag(video::EMF_BACK_FACE_CULLING, false);
1190 material_ladder.setFlag(video::EMF_BILINEAR_FILTER, false);
1191 material_ladder.setFlag(video::EMF_FOG_ENABLE, true);
1192 material_ladder.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
1193 material_ladder.setTexture(0, ap.atlas);
1195 u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio));
1196 video::SColor c(255,l,l,l);
1198 float d = (float)BS/16;
1200 // Assume wall is at X+
1201 video::S3DVertex vertices[4] =
1203 video::S3DVertex(BS/2-d,-BS/2,-BS/2, 0,0,0, c,
1205 video::S3DVertex(BS/2-d,-BS/2,BS/2, 0,0,0, c,
1207 video::S3DVertex(BS/2-d,BS/2,BS/2, 0,0,0, c,
1209 video::S3DVertex(BS/2-d,BS/2,-BS/2, 0,0,0, c,
1213 v3s16 dir = unpackDir(n.param2);
1215 for(s32 i=0; i<4; i++)
1217 if(dir == v3s16(1,0,0))
1218 vertices[i].Pos.rotateXZBy(0);
1219 if(dir == v3s16(-1,0,0))
1220 vertices[i].Pos.rotateXZBy(180);
1221 if(dir == v3s16(0,0,1))
1222 vertices[i].Pos.rotateXZBy(90);
1223 if(dir == v3s16(0,0,-1))
1224 vertices[i].Pos.rotateXZBy(-90);
1225 if(dir == v3s16(0,-1,0))
1226 vertices[i].Pos.rotateXYBy(-90);
1227 if(dir == v3s16(0,1,0))
1228 vertices[i].Pos.rotateXYBy(90);
1230 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
1233 u16 indices[] = {0,1,2,2,3,0};
1234 // Add to mesh collector
1235 collector.append(material_ladder, vertices, 4, indices, 6);
1237 else if(n.getContent() == CONTENT_APPLE)
1239 u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio)));
1240 video::SColor c = MapBlock_LightColor(255, l);
1242 for(u32 j=0; j<4; j++)
1244 video::S3DVertex vertices[4] =
1246 video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c,
1247 pa_apple.x0(), pa_apple.y1()),
1248 video::S3DVertex(BS/2,-BS/2,0, 0,0,0, c,
1249 pa_apple.x1(), pa_apple.y1()),
1250 video::S3DVertex(BS/2,BS/2,0, 0,0,0, c,
1251 pa_apple.x1(), pa_apple.y0()),
1252 video::S3DVertex(-BS/2,BS/2,0, 0,0,0, c,
1253 pa_apple.x0(), pa_apple.y0()),
1258 for(u16 i=0; i<4; i++)
1259 vertices[i].Pos.rotateXZBy(45);
1263 for(u16 i=0; i<4; i++)
1264 vertices[i].Pos.rotateXZBy(-45);
1268 for(u16 i=0; i<4; i++)
1269 vertices[i].Pos.rotateXZBy(135);
1273 for(u16 i=0; i<4; i++)
1274 vertices[i].Pos.rotateXZBy(-135);
1277 for(u16 i=0; i<4; i++)
1279 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
1282 u16 indices[] = {0,1,2,2,3,0};
1283 // Add to mesh collector
1284 collector.append(material_apple, vertices, 4, indices, 6);
1287 else if(n.getContent() == CONTENT_SAPLING) {
1288 u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio)));
1289 video::SColor c = MapBlock_LightColor(255, l);
1291 for(u32 j=0; j<4; j++)
1293 video::S3DVertex vertices[4] =
1295 video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c,
1296 pa_sapling.x0(), pa_sapling.y1()),
1297 video::S3DVertex(BS/2,-BS/2,0, 0,0,0, c,
1298 pa_sapling.x1(), pa_sapling.y1()),
1299 video::S3DVertex(BS/2,BS/1,0, 0,0,0, c,
1300 pa_sapling.x1(), pa_sapling.y0()),
1301 video::S3DVertex(-BS/2,BS/1,0, 0,0,0, c,
1302 pa_sapling.x0(), pa_sapling.y0()),
1307 for(u16 i=0; i<4; i++)
1308 vertices[i].Pos.rotateXZBy(45);
1312 for(u16 i=0; i<4; i++)
1313 vertices[i].Pos.rotateXZBy(-45);
1317 for(u16 i=0; i<4; i++)
1318 vertices[i].Pos.rotateXZBy(135);
1322 for(u16 i=0; i<4; i++)
1323 vertices[i].Pos.rotateXZBy(-135);
1326 for(u16 i=0; i<4; i++)
1328 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
1331 u16 indices[] = {0,1,2,2,3,0};
1332 // Add to mesh collector
1333 collector.append(material_sapling, vertices, 4, indices, 6);