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()
28 // material - the material to use (for all 6 faces)
29 // collector - the MeshCollector for the resulting polygons
30 // pa - texture atlas pointer for the material
31 // c - vertex colour - used for all
32 // pos - the position of the centre of the cuboid
33 // rz,ry,rz - the radius of the cuboid in each dimension
34 // txc - texture coordinates - this is a list of texture coordinates
35 // for the opposite corners of each face - therefore, there
36 // should be (2+2)*6=24 values in the list. Alternatively, pass
37 // NULL to use the entire texture for each face. The order of
38 // the faces in the list is top-backi-right-front-left-bottom
39 // If you specified 0,0,1,1 for each face, that would be the
40 // same as passing NULL.
41 void makeCuboid(video::SMaterial &material, MeshCollector *collector,
42 AtlasPointer* pa, video::SColor &c,
43 v3f &pos, f32 rx, f32 ry, f32 rz, f32* txc)
52 video::S3DVertex v[4] =
54 video::S3DVertex(0,0,0, 0,0,0, c, tu0, tv1),
55 video::S3DVertex(0,0,0, 0,0,0, c, tu1, tv1),
56 video::S3DVertex(0,0,0, 0,0,0, c, tu1, tv0),
57 video::S3DVertex(0,0,0, 0,0,0, c, tu0, tv0)
65 v[0].Pos.X=-rx; v[0].Pos.Y= ry; v[0].Pos.Z=-rz;
66 v[1].Pos.X=-rx; v[1].Pos.Y= ry; v[1].Pos.Z= rz;
67 v[2].Pos.X= rx; v[2].Pos.Y= ry; v[2].Pos.Z= rz;
68 v[3].Pos.X= rx; v[3].Pos.Y= ry, v[3].Pos.Z=-rz;
71 v[0].Pos.X=-rx; v[0].Pos.Y= ry; v[0].Pos.Z=-rz;
72 v[1].Pos.X= rx; v[1].Pos.Y= ry; v[1].Pos.Z=-rz;
73 v[2].Pos.X= rx; v[2].Pos.Y=-ry; v[2].Pos.Z=-rz;
74 v[3].Pos.X=-rx; v[3].Pos.Y=-ry, v[3].Pos.Z=-rz;
77 v[0].Pos.X= rx; v[0].Pos.Y= ry; v[0].Pos.Z=-rz;
78 v[1].Pos.X= rx; v[1].Pos.Y= ry; v[1].Pos.Z= rz;
79 v[2].Pos.X= rx; v[2].Pos.Y=-ry; v[2].Pos.Z= rz;
80 v[3].Pos.X= rx; v[3].Pos.Y=-ry, v[3].Pos.Z=-rz;
83 v[0].Pos.X= rx; v[0].Pos.Y= ry; v[0].Pos.Z= rz;
84 v[1].Pos.X=-rx; v[1].Pos.Y= ry; v[1].Pos.Z= rz;
85 v[2].Pos.X=-rx; v[2].Pos.Y=-ry; v[2].Pos.Z= rz;
86 v[3].Pos.X= rx; v[3].Pos.Y=-ry, v[3].Pos.Z= rz;
89 v[0].Pos.X=-rx; v[0].Pos.Y= ry; v[0].Pos.Z= rz;
90 v[1].Pos.X=-rx; v[1].Pos.Y= ry; v[1].Pos.Z=-rz;
91 v[2].Pos.X=-rx; v[2].Pos.Y=-ry; v[2].Pos.Z=-rz;
92 v[3].Pos.X=-rx; v[3].Pos.Y=-ry, v[3].Pos.Z= rz;
95 v[0].Pos.X= rx; v[0].Pos.Y=-ry; v[0].Pos.Z= rz;
96 v[1].Pos.X=-rx; v[1].Pos.Y=-ry; v[1].Pos.Z= rz;
97 v[2].Pos.X=-rx; v[2].Pos.Y=-ry; v[2].Pos.Z=-rz;
98 v[3].Pos.X= rx; v[3].Pos.Y=-ry, v[3].Pos.Z=-rz;
104 v[0].TCoords.X=tu0+txus*txc[0]; v[0].TCoords.Y=tv0+txvs*txc[3];
105 v[1].TCoords.X=tu0+txus*txc[2]; v[1].TCoords.Y=tv0+txvs*txc[3];
106 v[2].TCoords.X=tu0+txus*txc[2]; v[2].TCoords.Y=tv0+txvs*txc[1];
107 v[3].TCoords.X=tu0+txus*txc[0]; v[3].TCoords.Y=tv0+txvs*txc[1];
111 for(u16 i=0; i<4; i++)
113 u16 indices[] = {0,1,2,2,3,0};
114 collector->append(material, v, 4, indices, 6);
122 void mapblock_mesh_generate_special(MeshMakeData *data,
123 MeshCollector &collector)
126 //TimeTaker timer("mapblock_mesh_generate_special()");
131 bool new_style_water = g_settings.getBool("new_style_water");
132 bool new_style_leaves = g_settings.getBool("new_style_leaves");
133 //bool smooth_lighting = g_settings.getBool("smooth_lighting");
134 bool invisible_stone = g_settings.getBool("invisible_stone");
136 float node_liquid_level = 1.0;
138 node_liquid_level = 0.85;
140 v3s16 blockpos_nodes = data->m_blockpos*MAP_BLOCKSIZE;
142 // New-style leaves material
143 video::SMaterial material_leaves1;
144 material_leaves1.setFlag(video::EMF_LIGHTING, false);
145 //material_leaves1.setFlag(video::EMF_BACK_FACE_CULLING, 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);
202 // junglegrass material
203 video::SMaterial material_junglegrass;
204 material_junglegrass.setFlag(video::EMF_LIGHTING, false);
205 material_junglegrass.setFlag(video::EMF_BILINEAR_FILTER, false);
206 material_junglegrass.setFlag(video::EMF_FOG_ENABLE, true);
207 material_junglegrass.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
208 AtlasPointer pa_junglegrass = g_texturesource->getTexture(
209 g_texturesource->getTextureId("junglegrass.png"));
210 material_junglegrass.setTexture(0, pa_junglegrass.atlas);
212 for(s16 z=0; z<MAP_BLOCKSIZE; z++)
213 for(s16 y=0; y<MAP_BLOCKSIZE; y++)
214 for(s16 x=0; x<MAP_BLOCKSIZE; x++)
218 MapNode n = data->m_vmanip.getNodeNoEx(blockpos_nodes+p);
223 if(n.getContent() == CONTENT_TORCH)
225 video::SColor c(255,255,255,255);
227 // Wall at X+ of node
228 video::S3DVertex vertices[4] =
230 video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c, 0,1),
231 video::S3DVertex(BS/2,-BS/2,0, 0,0,0, c, 1,1),
232 video::S3DVertex(BS/2,BS/2,0, 0,0,0, c, 1,0),
233 video::S3DVertex(-BS/2,BS/2,0, 0,0,0, c, 0,0),
236 v3s16 dir = unpackDir(n.param2);
238 for(s32 i=0; i<4; i++)
240 if(dir == v3s16(1,0,0))
241 vertices[i].Pos.rotateXZBy(0);
242 if(dir == v3s16(-1,0,0))
243 vertices[i].Pos.rotateXZBy(180);
244 if(dir == v3s16(0,0,1))
245 vertices[i].Pos.rotateXZBy(90);
246 if(dir == v3s16(0,0,-1))
247 vertices[i].Pos.rotateXZBy(-90);
248 if(dir == v3s16(0,-1,0))
249 vertices[i].Pos.rotateXZBy(45);
250 if(dir == v3s16(0,1,0))
251 vertices[i].Pos.rotateXZBy(-45);
253 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
257 video::SMaterial material;
258 material.setFlag(video::EMF_LIGHTING, false);
259 material.setFlag(video::EMF_BACK_FACE_CULLING, false);
260 material.setFlag(video::EMF_BILINEAR_FILTER, false);
261 //material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
262 material.MaterialType
263 = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
265 if(dir == v3s16(0,-1,0))
266 material.setTexture(0,
267 g_texturesource->getTextureRaw("torch_on_floor.png"));
268 else if(dir == v3s16(0,1,0))
269 material.setTexture(0,
270 g_texturesource->getTextureRaw("torch_on_ceiling.png"));
271 // For backwards compatibility
272 else if(dir == v3s16(0,0,0))
273 material.setTexture(0,
274 g_texturesource->getTextureRaw("torch_on_floor.png"));
276 material.setTexture(0,
277 g_texturesource->getTextureRaw("torch.png"));
279 u16 indices[] = {0,1,2,2,3,0};
280 // Add to mesh collector
281 collector.append(material, vertices, 4, indices, 6);
286 else if(n.getContent() == CONTENT_SIGN_WALL)
288 u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio));
289 video::SColor c = MapBlock_LightColor(255, l);
291 float d = (float)BS/16;
292 // Wall at X+ of node
293 video::S3DVertex vertices[4] =
295 video::S3DVertex(BS/2-d,-BS/2,-BS/2, 0,0,0, c, 0,1),
296 video::S3DVertex(BS/2-d,-BS/2,BS/2, 0,0,0, c, 1,1),
297 video::S3DVertex(BS/2-d,BS/2,BS/2, 0,0,0, c, 1,0),
298 video::S3DVertex(BS/2-d,BS/2,-BS/2, 0,0,0, c, 0,0),
301 v3s16 dir = unpackDir(n.param2);
303 for(s32 i=0; i<4; i++)
305 if(dir == v3s16(1,0,0))
306 vertices[i].Pos.rotateXZBy(0);
307 if(dir == v3s16(-1,0,0))
308 vertices[i].Pos.rotateXZBy(180);
309 if(dir == v3s16(0,0,1))
310 vertices[i].Pos.rotateXZBy(90);
311 if(dir == v3s16(0,0,-1))
312 vertices[i].Pos.rotateXZBy(-90);
313 if(dir == v3s16(0,-1,0))
314 vertices[i].Pos.rotateXYBy(-90);
315 if(dir == v3s16(0,1,0))
316 vertices[i].Pos.rotateXYBy(90);
318 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
322 video::SMaterial material;
323 material.setFlag(video::EMF_LIGHTING, false);
324 material.setFlag(video::EMF_BACK_FACE_CULLING, false);
325 material.setFlag(video::EMF_BILINEAR_FILTER, false);
326 material.setFlag(video::EMF_FOG_ENABLE, true);
327 //material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
328 material.MaterialType
329 = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
331 material.setTexture(0,
332 g_texturesource->getTextureRaw("sign_wall.png"));
334 u16 indices[] = {0,1,2,2,3,0};
335 // Add to mesh collector
336 collector.append(material, vertices, 4, indices, 6);
339 Add flowing liquid to mesh
341 else if(content_features(n).liquid_type == LIQUID_FLOWING)
343 assert(content_features(n).special_material);
344 video::SMaterial &liquid_material =
345 *content_features(n).special_material;
346 assert(content_features(n).special_atlas);
347 AtlasPointer &pa_liquid1 =
348 *content_features(n).special_atlas;
350 bool top_is_same_liquid = false;
351 MapNode ntop = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y+1,z));
352 content_t c_flowing = content_features(n).liquid_alternative_flowing;
353 content_t c_source = content_features(n).liquid_alternative_source;
354 if(ntop.getContent() == c_flowing || ntop.getContent() == c_source)
355 top_is_same_liquid = true;
358 // Use the light of the node on top if possible
359 if(content_features(ntop).param_type == CPT_LIGHT)
360 l = decode_light(ntop.getLightBlend(data->m_daynight_ratio));
361 // Otherwise use the light of this node (the liquid)
363 l = decode_light(n.getLightBlend(data->m_daynight_ratio));
364 video::SColor c = MapBlock_LightColor(
365 content_features(n).vertex_alpha, l);
367 // Neighbor liquid levels (key = relative position)
368 // Includes current node
369 core::map<v3s16, f32> neighbor_levels;
370 core::map<v3s16, content_t> neighbor_contents;
371 core::map<v3s16, u8> neighbor_flags;
372 const u8 neighborflag_top_is_same_liquid = 0x01;
373 v3s16 neighbor_dirs[9] = {
384 for(u32 i=0; i<9; i++)
386 content_t content = CONTENT_AIR;
387 float level = -0.5 * BS;
390 v3s16 p2 = p + neighbor_dirs[i];
391 MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
392 if(n2.getContent() != CONTENT_IGNORE)
394 content = n2.getContent();
396 if(n2.getContent() == c_source)
397 level = (-0.5+node_liquid_level) * BS;
398 else if(n2.getContent() == c_flowing)
399 level = (-0.5 + ((float)(n2.param2&LIQUID_LEVEL_MASK)
400 + 0.5) / 8.0 * node_liquid_level) * BS;
402 // Check node above neighbor.
403 // NOTE: This doesn't get executed if neighbor
406 n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
407 if(n2.getContent() == c_source ||
408 n2.getContent() == c_flowing)
409 flags |= neighborflag_top_is_same_liquid;
412 neighbor_levels.insert(neighbor_dirs[i], level);
413 neighbor_contents.insert(neighbor_dirs[i], content);
414 neighbor_flags.insert(neighbor_dirs[i], flags);
417 // Corner heights (average between four liquids)
418 f32 corner_levels[4];
420 v3s16 halfdirs[4] = {
426 for(u32 i=0; i<4; i++)
428 v3s16 cornerdir = halfdirs[i];
429 float cornerlevel = 0;
432 for(u32 j=0; j<4; j++)
434 v3s16 neighbordir = cornerdir - halfdirs[j];
435 content_t content = neighbor_contents[neighbordir];
436 // If top is liquid, draw starting from top of node
437 if(neighbor_flags[neighbordir] &
438 neighborflag_top_is_same_liquid)
440 cornerlevel = 0.5*BS;
444 // Source is always the same height
445 else if(content == c_source)
447 cornerlevel = (-0.5+node_liquid_level)*BS;
451 // Flowing liquid has level information
452 else if(content == c_flowing)
454 cornerlevel += neighbor_levels[neighbordir];
457 else if(content == CONTENT_AIR)
463 cornerlevel = -0.5*BS;
464 else if(valid_count > 0)
465 cornerlevel /= valid_count;
466 corner_levels[i] = cornerlevel;
473 v3s16 side_dirs[4] = {
479 s16 side_corners[4][2] = {
485 for(u32 i=0; i<4; i++)
487 v3s16 dir = side_dirs[i];
490 If our topside is liquid and neighbor's topside
491 is liquid, don't draw side face
493 if(top_is_same_liquid &&
494 neighbor_flags[dir] & neighborflag_top_is_same_liquid)
497 content_t neighbor_content = neighbor_contents[dir];
499 // Don't draw face if neighbor is not air or liquid
500 if(neighbor_content != CONTENT_AIR
501 && content_liquid(neighbor_content) == false)
504 bool neighbor_is_same_liquid = (neighbor_content == c_source
505 || neighbor_content == c_flowing);
507 // Don't draw any faces if neighbor same is liquid and top is
509 if(neighbor_is_same_liquid == true
510 && top_is_same_liquid == false)
513 video::S3DVertex vertices[4] =
515 /*video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c, 0,1),
516 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c, 1,1),
517 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c, 1,0),
518 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c, 0,0),*/
519 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
520 pa_liquid1.x0(), pa_liquid1.y1()),
521 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
522 pa_liquid1.x1(), pa_liquid1.y1()),
523 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
524 pa_liquid1.x1(), pa_liquid1.y0()),
525 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
526 pa_liquid1.x0(), pa_liquid1.y0()),
530 If our topside is liquid, set upper border of face
531 at upper border of node
533 if(top_is_same_liquid)
535 vertices[2].Pos.Y = 0.5*BS;
536 vertices[3].Pos.Y = 0.5*BS;
539 Otherwise upper position of face is corner levels
543 vertices[2].Pos.Y = corner_levels[side_corners[i][0]];
544 vertices[3].Pos.Y = corner_levels[side_corners[i][1]];
548 If neighbor is liquid, lower border of face is corner
551 if(neighbor_is_same_liquid)
553 vertices[0].Pos.Y = corner_levels[side_corners[i][1]];
554 vertices[1].Pos.Y = corner_levels[side_corners[i][0]];
557 If neighbor is not liquid, lower border of face is
562 vertices[0].Pos.Y = -0.5*BS;
563 vertices[1].Pos.Y = -0.5*BS;
566 for(s32 j=0; j<4; j++)
568 if(dir == v3s16(0,0,1))
569 vertices[j].Pos.rotateXZBy(0);
570 if(dir == v3s16(0,0,-1))
571 vertices[j].Pos.rotateXZBy(180);
572 if(dir == v3s16(-1,0,0))
573 vertices[j].Pos.rotateXZBy(90);
574 if(dir == v3s16(1,0,-0))
575 vertices[j].Pos.rotateXZBy(-90);
577 // Do this to not cause glitches when two liquids are
579 if(neighbor_is_same_liquid == false){
580 vertices[j].Pos.X *= 0.98;
581 vertices[j].Pos.Z *= 0.98;
584 vertices[j].Pos += intToFloat(p + blockpos_nodes, BS);
587 u16 indices[] = {0,1,2,2,3,0};
588 // Add to mesh collector
589 collector.append(liquid_material, vertices, 4, indices, 6);
593 Generate top side, if appropriate
596 if(top_is_same_liquid == false)
598 video::S3DVertex vertices[4] =
600 /*video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c, 0,1),
601 video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c, 1,1),
602 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c, 1,0),
603 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c, 0,0),*/
604 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
605 pa_liquid1.x0(), pa_liquid1.y1()),
606 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
607 pa_liquid1.x1(), pa_liquid1.y1()),
608 video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c,
609 pa_liquid1.x1(), pa_liquid1.y0()),
610 video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c,
611 pa_liquid1.x0(), pa_liquid1.y0()),
614 // This fixes a strange bug
615 s32 corner_resolve[4] = {3,2,1,0};
617 for(s32 i=0; i<4; i++)
619 //vertices[i].Pos.Y += liquid_level;
620 //vertices[i].Pos.Y += neighbor_levels[v3s16(0,0,0)];
621 s32 j = corner_resolve[i];
622 vertices[i].Pos.Y += corner_levels[j];
623 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
626 u16 indices[] = {0,1,2,2,3,0};
627 // Add to mesh collector
628 collector.append(liquid_material, vertices, 4, indices, 6);
632 Add water sources to mesh if using new style
634 else if(content_features(n).liquid_type == LIQUID_SOURCE
637 assert(content_features(n).special_material);
638 video::SMaterial &liquid_material =
639 *content_features(n).special_material;
640 assert(content_features(n).special_atlas);
641 AtlasPointer &pa_liquid1 =
642 *content_features(n).special_atlas;
644 bool top_is_air = false;
645 MapNode n = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y+1,z));
646 if(n.getContent() == CONTENT_AIR)
649 if(top_is_air == false)
652 u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio));
653 video::SColor c = MapBlock_LightColor(
654 content_features(n).vertex_alpha, l);
656 video::S3DVertex vertices[4] =
658 /*video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c, 0,1),
659 video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c, 1,1),
660 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c, 1,0),
661 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c, 0,0),*/
662 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
663 pa_liquid1.x0(), pa_liquid1.y1()),
664 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
665 pa_liquid1.x1(), pa_liquid1.y1()),
666 video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c,
667 pa_liquid1.x1(), pa_liquid1.y0()),
668 video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c,
669 pa_liquid1.x0(), pa_liquid1.y0()),
672 for(s32 i=0; i<4; i++)
674 vertices[i].Pos.Y += (-0.5+node_liquid_level)*BS;
675 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
678 u16 indices[] = {0,1,2,2,3,0};
679 // Add to mesh collector
680 collector.append(liquid_material, vertices, 4, indices, 6);
683 Add leaves if using new style
685 else if(n.getContent() == CONTENT_LEAVES && new_style_leaves)
687 /*u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio));*/
688 u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio)));
689 video::SColor c = MapBlock_LightColor(255, l);
691 for(u32 j=0; j<6; j++)
693 video::S3DVertex vertices[4] =
695 /*video::S3DVertex(-BS/2,-BS/2,BS/2, 0,0,0, c, 0,1),
696 video::S3DVertex(BS/2,-BS/2,BS/2, 0,0,0, c, 1,1),
697 video::S3DVertex(BS/2,BS/2,BS/2, 0,0,0, c, 1,0),
698 video::S3DVertex(-BS/2,BS/2,BS/2, 0,0,0, c, 0,0),*/
699 video::S3DVertex(-BS/2,-BS/2,BS/2, 0,0,0, c,
700 pa_leaves1.x0(), pa_leaves1.y1()),
701 video::S3DVertex(BS/2,-BS/2,BS/2, 0,0,0, c,
702 pa_leaves1.x1(), pa_leaves1.y1()),
703 video::S3DVertex(BS/2,BS/2,BS/2, 0,0,0, c,
704 pa_leaves1.x1(), pa_leaves1.y0()),
705 video::S3DVertex(-BS/2,BS/2,BS/2, 0,0,0, c,
706 pa_leaves1.x0(), pa_leaves1.y0()),
711 for(u16 i=0; i<4; i++)
712 vertices[i].Pos.rotateXZBy(0);
716 for(u16 i=0; i<4; i++)
717 vertices[i].Pos.rotateXZBy(180);
721 for(u16 i=0; i<4; i++)
722 vertices[i].Pos.rotateXZBy(-90);
726 for(u16 i=0; i<4; i++)
727 vertices[i].Pos.rotateXZBy(90);
731 for(u16 i=0; i<4; i++)
732 vertices[i].Pos.rotateYZBy(-90);
736 for(u16 i=0; i<4; i++)
737 vertices[i].Pos.rotateYZBy(90);
740 for(u16 i=0; i<4; i++)
742 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
745 u16 indices[] = {0,1,2,2,3,0};
746 // Add to mesh collector
747 collector.append(material_leaves1, vertices, 4, indices, 6);
753 else if(n.getContent() == CONTENT_GLASS)
755 u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio)));
756 video::SColor c = MapBlock_LightColor(255, l);
758 for(u32 j=0; j<6; j++)
760 video::S3DVertex vertices[4] =
762 video::S3DVertex(-BS/2,-BS/2,BS/2, 0,0,0, c,
763 pa_glass.x0(), pa_glass.y1()),
764 video::S3DVertex(BS/2,-BS/2,BS/2, 0,0,0, c,
765 pa_glass.x1(), pa_glass.y1()),
766 video::S3DVertex(BS/2,BS/2,BS/2, 0,0,0, c,
767 pa_glass.x1(), pa_glass.y0()),
768 video::S3DVertex(-BS/2,BS/2,BS/2, 0,0,0, c,
769 pa_glass.x0(), pa_glass.y0()),
774 for(u16 i=0; i<4; i++)
775 vertices[i].Pos.rotateXZBy(0);
779 for(u16 i=0; i<4; i++)
780 vertices[i].Pos.rotateXZBy(180);
784 for(u16 i=0; i<4; i++)
785 vertices[i].Pos.rotateXZBy(-90);
789 for(u16 i=0; i<4; i++)
790 vertices[i].Pos.rotateXZBy(90);
794 for(u16 i=0; i<4; i++)
795 vertices[i].Pos.rotateYZBy(-90);
799 for(u16 i=0; i<4; i++)
800 vertices[i].Pos.rotateYZBy(90);
803 for(u16 i=0; i<4; i++)
805 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
808 u16 indices[] = {0,1,2,2,3,0};
809 // Add to mesh collector
810 collector.append(material_glass, vertices, 4, indices, 6);
816 else if(n.getContent() == CONTENT_FENCE)
818 u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio)));
819 video::SColor c = MapBlock_LightColor(255, l);
821 const f32 post_rad=(f32)BS/10;
822 const f32 bar_rad=(f32)BS/20;
823 const f32 bar_len=(f32)(BS/2)-post_rad;
825 // The post - always present
826 v3f pos = intToFloat(p+blockpos_nodes, BS);
834 makeCuboid(material_wood, &collector,
836 post_rad,BS/2,post_rad, postuv);
838 // Now a section of fence, +X, if there's a post there
841 MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
842 if(n2.getContent() == CONTENT_FENCE)
844 pos = intToFloat(p+blockpos_nodes, BS);
854 makeCuboid(material_wood, &collector,
856 bar_len,bar_rad,bar_rad, xrailuv);
859 makeCuboid(material_wood, &collector,
861 bar_len,bar_rad,bar_rad, xrailuv);
864 // Now a section of fence, +Z, if there's a post there
867 n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
868 if(n2.getContent() == CONTENT_FENCE)
870 pos = intToFloat(p+blockpos_nodes, BS);
880 makeCuboid(material_wood, &collector,
882 bar_rad,bar_rad,bar_len, zrailuv);
884 makeCuboid(material_wood, &collector,
886 bar_rad,bar_rad,bar_len, zrailuv);
893 Add stones with minerals if stone is invisible
895 else if(n.getContent() == CONTENT_STONE && invisible_stone && n.getMineral() != MINERAL_NONE)
897 for(u32 j=0; j<6; j++)
899 // NOTE: Hopefully g_6dirs[j] is the right direction...
900 v3s16 dir = g_6dirs[j];
902 MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + dir);
903 if(content_features(n2).param_type == CPT_LIGHT)
904 l = decode_light(n2.getLightBlend(data->m_daynight_ratio));
908 video::SColor c = MapBlock_LightColor(255, l);
910 // Get the right texture
911 TileSpec ts = n.getTile(dir);
912 AtlasPointer ap = ts.texture;
913 material_general.setTexture(0, ap.atlas);
915 video::S3DVertex vertices[4] =
917 /*video::S3DVertex(-BS/2,-BS/2,BS/2, 0,0,0, c, 0,1),
918 video::S3DVertex(BS/2,-BS/2,BS/2, 0,0,0, c, 1,1),
919 video::S3DVertex(BS/2,BS/2,BS/2, 0,0,0, c, 1,0),
920 video::S3DVertex(-BS/2,BS/2,BS/2, 0,0,0, c, 0,0),*/
921 video::S3DVertex(-BS/2,-BS/2,BS/2, 0,0,0, c,
923 video::S3DVertex(BS/2,-BS/2,BS/2, 0,0,0, c,
925 video::S3DVertex(BS/2,BS/2,BS/2, 0,0,0, c,
927 video::S3DVertex(-BS/2,BS/2,BS/2, 0,0,0, c,
933 for(u16 i=0; i<4; i++)
934 vertices[i].Pos.rotateXZBy(0);
938 for(u16 i=0; i<4; i++)
939 vertices[i].Pos.rotateXZBy(180);
943 for(u16 i=0; i<4; i++)
944 vertices[i].Pos.rotateXZBy(-90);
948 for(u16 i=0; i<4; i++)
949 vertices[i].Pos.rotateXZBy(90);
953 for(u16 i=0; i<4; i++)
955 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
958 u16 indices[] = {0,1,2,2,3,0};
959 // Add to mesh collector
960 collector.append(material_general, vertices, 4, indices, 6);
964 else if(n.getContent() == CONTENT_PAPYRUS)
966 u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio)));
967 video::SColor c = MapBlock_LightColor(255, l);
969 for(u32 j=0; j<4; j++)
971 video::S3DVertex vertices[4] =
973 video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c,
974 pa_papyrus.x0(), pa_papyrus.y1()),
975 video::S3DVertex(BS/2,-BS/2,0, 0,0,0, c,
976 pa_papyrus.x1(), pa_papyrus.y1()),
977 video::S3DVertex(BS/2,BS/2,0, 0,0,0, c,
978 pa_papyrus.x1(), pa_papyrus.y0()),
979 video::S3DVertex(-BS/2,BS/2,0, 0,0,0, c,
980 pa_papyrus.x0(), pa_papyrus.y0()),
985 for(u16 i=0; i<4; i++)
986 vertices[i].Pos.rotateXZBy(45);
990 for(u16 i=0; i<4; i++)
991 vertices[i].Pos.rotateXZBy(-45);
995 for(u16 i=0; i<4; i++)
996 vertices[i].Pos.rotateXZBy(135);
1000 for(u16 i=0; i<4; i++)
1001 vertices[i].Pos.rotateXZBy(-135);
1004 for(u16 i=0; i<4; i++)
1006 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
1009 u16 indices[] = {0,1,2,2,3,0};
1010 // Add to mesh collector
1011 collector.append(material_papyrus, vertices, 4, indices, 6);
1014 else if(n.getContent() == CONTENT_JUNGLEGRASS)
1016 u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio)));
1017 video::SColor c = MapBlock_LightColor(255, l);
1019 for(u32 j=0; j<4; j++)
1021 video::S3DVertex vertices[4] =
1023 video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c,
1024 pa_papyrus.x0(), pa_papyrus.y1()),
1025 video::S3DVertex(BS/2,-BS/2,0, 0,0,0, c,
1026 pa_papyrus.x1(), pa_papyrus.y1()),
1027 video::S3DVertex(BS/2,BS/1,0, 0,0,0, c,
1028 pa_papyrus.x1(), pa_papyrus.y0()),
1029 video::S3DVertex(-BS/2,BS/1,0, 0,0,0, c,
1030 pa_papyrus.x0(), pa_papyrus.y0()),
1035 for(u16 i=0; i<4; i++)
1036 vertices[i].Pos.rotateXZBy(45);
1040 for(u16 i=0; i<4; i++)
1041 vertices[i].Pos.rotateXZBy(-45);
1045 for(u16 i=0; i<4; i++)
1046 vertices[i].Pos.rotateXZBy(135);
1050 for(u16 i=0; i<4; i++)
1051 vertices[i].Pos.rotateXZBy(-135);
1054 for(u16 i=0; i<4; i++)
1056 vertices[i].Pos *= 1.3;
1057 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
1060 u16 indices[] = {0,1,2,2,3,0};
1061 // Add to mesh collector
1062 collector.append(material_junglegrass, vertices, 4, indices, 6);
1065 else if(n.getContent() == CONTENT_RAIL)
1067 u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio));
1068 video::SColor c = MapBlock_LightColor(255, l);
1070 bool is_rail_x [] = { false, false }; /* x-1, x+1 */
1071 bool is_rail_z [] = { false, false }; /* z-1, z+1 */
1073 MapNode n_minus_x = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x-1,y,z));
1074 MapNode n_plus_x = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x+1,y,z));
1075 MapNode n_minus_z = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y,z-1));
1076 MapNode n_plus_z = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y,z+1));
1078 if(n_minus_x.getContent() == CONTENT_RAIL)
1079 is_rail_x[0] = true;
1080 if(n_plus_x.getContent() == CONTENT_RAIL)
1081 is_rail_x[1] = true;
1082 if(n_minus_z.getContent() == CONTENT_RAIL)
1083 is_rail_z[0] = true;
1084 if(n_plus_z.getContent() == CONTENT_RAIL)
1085 is_rail_z[1] = true;
1087 float d = (float)BS/16;
1088 video::S3DVertex vertices[4] =
1090 video::S3DVertex(-BS/2,-BS/2+d,-BS/2, 0,0,0, c,
1092 video::S3DVertex(BS/2,-BS/2+d,-BS/2, 0,0,0, c,
1094 video::S3DVertex(BS/2,-BS/2+d,BS/2, 0,0,0, c,
1096 video::S3DVertex(-BS/2,-BS/2+d,BS/2, 0,0,0, c,
1100 video::SMaterial material_rail;
1101 material_rail.setFlag(video::EMF_LIGHTING, false);
1102 material_rail.setFlag(video::EMF_BACK_FACE_CULLING, false);
1103 material_rail.setFlag(video::EMF_BILINEAR_FILTER, false);
1104 material_rail.setFlag(video::EMF_FOG_ENABLE, true);
1105 material_rail.MaterialType
1106 = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
1108 int adjacencies = is_rail_x[0] + is_rail_x[1] + is_rail_z[0] + is_rail_z[1];
1112 material_rail.setTexture(0, g_texturesource->getTextureRaw("rail.png"));
1113 else if(adjacencies == 2)
1115 if((is_rail_x[0] && is_rail_x[1]) || (is_rail_z[0] && is_rail_z[1]))
1116 material_rail.setTexture(0, g_texturesource->getTextureRaw("rail.png"));
1118 material_rail.setTexture(0, g_texturesource->getTextureRaw("rail_curved.png"));
1120 else if(adjacencies == 3)
1121 material_rail.setTexture(0, g_texturesource->getTextureRaw("rail_t_junction.png"));
1122 else if(adjacencies == 4)
1123 material_rail.setTexture(0, g_texturesource->getTextureRaw("rail_crossing.png"));
1128 if(adjacencies == 1)
1130 if(is_rail_x[0] || is_rail_x[1])
1133 else if(adjacencies == 2)
1135 if(is_rail_x[0] && is_rail_x[1])
1137 else if(is_rail_x[0] && is_rail_z[0])
1139 else if(is_rail_x[0] && is_rail_z[1])
1141 else if(is_rail_x[1] && is_rail_z[1])
1144 else if(adjacencies == 3)
1157 for(u16 i=0; i<4; i++)
1158 vertices[i].Pos.rotateXZBy(angle);
1161 for(s32 i=0; i<4; i++)
1163 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
1166 u16 indices[] = {0,1,2,2,3,0};
1167 collector.append(material_rail, vertices, 4, indices, 6);
1169 else if (n.getContent() == CONTENT_LADDER) {
1170 u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio));
1171 video::SColor c(255,l,l,l);
1173 float d = (float)BS/16;
1175 // Assume wall is at X+
1176 video::S3DVertex vertices[4] =
1178 video::S3DVertex(BS/2-d,-BS/2,-BS/2, 0,0,0, c, 0,1),
1179 video::S3DVertex(BS/2-d,-BS/2,BS/2, 0,0,0, c, 1,1),
1180 video::S3DVertex(BS/2-d,BS/2,BS/2, 0,0,0, c, 1,0),
1181 video::S3DVertex(BS/2-d,BS/2,-BS/2, 0,0,0, c, 0,0),
1184 v3s16 dir = unpackDir(n.param2);
1186 for(s32 i=0; i<4; i++)
1188 if(dir == v3s16(1,0,0))
1189 vertices[i].Pos.rotateXZBy(0);
1190 if(dir == v3s16(-1,0,0))
1191 vertices[i].Pos.rotateXZBy(180);
1192 if(dir == v3s16(0,0,1))
1193 vertices[i].Pos.rotateXZBy(90);
1194 if(dir == v3s16(0,0,-1))
1195 vertices[i].Pos.rotateXZBy(-90);
1196 if(dir == v3s16(0,-1,0))
1197 vertices[i].Pos.rotateXYBy(-90);
1198 if(dir == v3s16(0,1,0))
1199 vertices[i].Pos.rotateXYBy(90);
1201 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
1204 video::SMaterial material_ladder;
1205 material_ladder.setFlag(video::EMF_LIGHTING, false);
1206 material_ladder.setFlag(video::EMF_BACK_FACE_CULLING, false);
1207 material_ladder.setFlag(video::EMF_BILINEAR_FILTER, false);
1208 material_ladder.setFlag(video::EMF_FOG_ENABLE, true);
1209 material_ladder.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
1210 material_ladder.setTexture(0, g_texturesource->getTextureRaw("ladder.png"));
1212 u16 indices[] = {0,1,2,2,3,0};
1213 // Add to mesh collector
1214 collector.append(material_ladder, vertices, 4, indices, 6);
1216 else if(n.getContent() == CONTENT_APPLE)
1218 u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio)));
1219 video::SColor c = MapBlock_LightColor(255, l);
1221 for(u32 j=0; j<4; j++)
1223 video::S3DVertex vertices[4] =
1225 video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c,
1226 pa_apple.x0(), pa_apple.y1()),
1227 video::S3DVertex(BS/2,-BS/2,0, 0,0,0, c,
1228 pa_apple.x1(), pa_apple.y1()),
1229 video::S3DVertex(BS/2,BS/2,0, 0,0,0, c,
1230 pa_apple.x1(), pa_apple.y0()),
1231 video::S3DVertex(-BS/2,BS/2,0, 0,0,0, c,
1232 pa_apple.x0(), pa_apple.y0()),
1237 for(u16 i=0; i<4; i++)
1238 vertices[i].Pos.rotateXZBy(45);
1242 for(u16 i=0; i<4; i++)
1243 vertices[i].Pos.rotateXZBy(-45);
1247 for(u16 i=0; i<4; i++)
1248 vertices[i].Pos.rotateXZBy(135);
1252 for(u16 i=0; i<4; i++)
1253 vertices[i].Pos.rotateXZBy(-135);
1256 for(u16 i=0; i<4; i++)
1258 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
1261 u16 indices[] = {0,1,2,2,3,0};
1262 // Add to mesh collector
1263 collector.append(material_apple, vertices, 4, indices, 6);