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_BACK_FACE_CULLING, false);
147 material_leaves1.setFlag(video::EMF_BILINEAR_FILTER, false);
148 material_leaves1.setFlag(video::EMF_FOG_ENABLE, true);
149 material_leaves1.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
150 AtlasPointer pa_leaves1 = g_texturesource->getTexture(
151 g_texturesource->getTextureId("leaves.png"));
152 material_leaves1.setTexture(0, pa_leaves1.atlas);
155 video::SMaterial material_glass;
156 material_glass.setFlag(video::EMF_LIGHTING, false);
157 material_glass.setFlag(video::EMF_BILINEAR_FILTER, false);
158 material_glass.setFlag(video::EMF_FOG_ENABLE, true);
159 material_glass.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
160 AtlasPointer pa_glass = g_texturesource->getTexture(
161 g_texturesource->getTextureId("glass.png"));
162 material_glass.setTexture(0, pa_glass.atlas);
165 video::SMaterial material_wood;
166 material_wood.setFlag(video::EMF_LIGHTING, false);
167 material_wood.setFlag(video::EMF_BILINEAR_FILTER, false);
168 material_wood.setFlag(video::EMF_FOG_ENABLE, true);
169 material_wood.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
170 AtlasPointer pa_wood = g_texturesource->getTexture(
171 g_texturesource->getTextureId("wood.png"));
172 material_wood.setTexture(0, pa_wood.atlas);
174 // General ground material for special output
175 // Texture is modified just before usage
176 video::SMaterial material_general;
177 material_general.setFlag(video::EMF_LIGHTING, false);
178 material_general.setFlag(video::EMF_BILINEAR_FILTER, false);
179 material_general.setFlag(video::EMF_FOG_ENABLE, true);
180 material_general.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
184 video::SMaterial material_papyrus;
185 material_papyrus.setFlag(video::EMF_LIGHTING, false);
186 material_papyrus.setFlag(video::EMF_BILINEAR_FILTER, false);
187 material_papyrus.setFlag(video::EMF_FOG_ENABLE, true);
188 material_papyrus.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
189 AtlasPointer pa_papyrus = g_texturesource->getTexture(
190 g_texturesource->getTextureId("papyrus.png"));
191 material_papyrus.setTexture(0, pa_papyrus.atlas);
194 video::SMaterial material_apple;
195 material_apple.setFlag(video::EMF_LIGHTING, false);
196 material_apple.setFlag(video::EMF_BILINEAR_FILTER, false);
197 material_apple.setFlag(video::EMF_FOG_ENABLE, true);
198 material_apple.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
199 AtlasPointer pa_apple = g_texturesource->getTexture(
200 g_texturesource->getTextureId("apple.png"));
201 material_apple.setTexture(0, pa_apple.atlas);
205 video::SMaterial material_sapling;
206 material_sapling.setFlag(video::EMF_LIGHTING, false);
207 material_sapling.setFlag(video::EMF_BILINEAR_FILTER, false);
208 material_sapling.setFlag(video::EMF_FOG_ENABLE, true);
209 material_sapling.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
210 AtlasPointer pa_sapling = g_texturesource->getTexture(
211 g_texturesource->getTextureId("sapling.png"));
212 material_sapling.setTexture(0, pa_sapling.atlas);
215 // junglegrass material
216 video::SMaterial material_junglegrass;
217 material_junglegrass.setFlag(video::EMF_LIGHTING, false);
218 material_junglegrass.setFlag(video::EMF_BILINEAR_FILTER, false);
219 material_junglegrass.setFlag(video::EMF_FOG_ENABLE, true);
220 material_junglegrass.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
221 AtlasPointer pa_junglegrass = g_texturesource->getTexture(
222 g_texturesource->getTextureId("junglegrass.png"));
223 material_junglegrass.setTexture(0, pa_junglegrass.atlas);
225 for(s16 z=0; z<MAP_BLOCKSIZE; z++)
226 for(s16 y=0; y<MAP_BLOCKSIZE; y++)
227 for(s16 x=0; x<MAP_BLOCKSIZE; x++)
231 MapNode n = data->m_vmanip.getNodeNoEx(blockpos_nodes+p);
236 if(n.getContent() == CONTENT_TORCH)
238 video::SColor c(255,255,255,255);
240 // Wall at X+ of node
241 video::S3DVertex vertices[4] =
243 video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c, 0,1),
244 video::S3DVertex(BS/2,-BS/2,0, 0,0,0, c, 1,1),
245 video::S3DVertex(BS/2,BS/2,0, 0,0,0, c, 1,0),
246 video::S3DVertex(-BS/2,BS/2,0, 0,0,0, c, 0,0),
249 v3s16 dir = unpackDir(n.param2);
251 for(s32 i=0; i<4; i++)
253 if(dir == v3s16(1,0,0))
254 vertices[i].Pos.rotateXZBy(0);
255 if(dir == v3s16(-1,0,0))
256 vertices[i].Pos.rotateXZBy(180);
257 if(dir == v3s16(0,0,1))
258 vertices[i].Pos.rotateXZBy(90);
259 if(dir == v3s16(0,0,-1))
260 vertices[i].Pos.rotateXZBy(-90);
261 if(dir == v3s16(0,-1,0))
262 vertices[i].Pos.rotateXZBy(45);
263 if(dir == v3s16(0,1,0))
264 vertices[i].Pos.rotateXZBy(-45);
266 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
270 video::SMaterial material;
271 material.setFlag(video::EMF_LIGHTING, false);
272 material.setFlag(video::EMF_BACK_FACE_CULLING, false);
273 material.setFlag(video::EMF_BILINEAR_FILTER, false);
274 //material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
275 material.MaterialType
276 = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
278 if(dir == v3s16(0,-1,0))
279 material.setTexture(0,
280 g_texturesource->getTextureRaw("torch_on_floor.png"));
281 else if(dir == v3s16(0,1,0))
282 material.setTexture(0,
283 g_texturesource->getTextureRaw("torch_on_ceiling.png"));
284 // For backwards compatibility
285 else if(dir == v3s16(0,0,0))
286 material.setTexture(0,
287 g_texturesource->getTextureRaw("torch_on_floor.png"));
289 material.setTexture(0,
290 g_texturesource->getTextureRaw("torch.png"));
292 u16 indices[] = {0,1,2,2,3,0};
293 // Add to mesh collector
294 collector.append(material, vertices, 4, indices, 6);
299 else if(n.getContent() == CONTENT_SIGN_WALL)
301 u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio));
302 video::SColor c = MapBlock_LightColor(255, l);
304 float d = (float)BS/16;
305 // Wall at X+ of node
306 video::S3DVertex vertices[4] =
308 video::S3DVertex(BS/2-d,-BS/2,-BS/2, 0,0,0, c, 0,1),
309 video::S3DVertex(BS/2-d,-BS/2,BS/2, 0,0,0, c, 1,1),
310 video::S3DVertex(BS/2-d,BS/2,BS/2, 0,0,0, c, 1,0),
311 video::S3DVertex(BS/2-d,BS/2,-BS/2, 0,0,0, c, 0,0),
314 v3s16 dir = unpackDir(n.param2);
316 for(s32 i=0; i<4; i++)
318 if(dir == v3s16(1,0,0))
319 vertices[i].Pos.rotateXZBy(0);
320 if(dir == v3s16(-1,0,0))
321 vertices[i].Pos.rotateXZBy(180);
322 if(dir == v3s16(0,0,1))
323 vertices[i].Pos.rotateXZBy(90);
324 if(dir == v3s16(0,0,-1))
325 vertices[i].Pos.rotateXZBy(-90);
326 if(dir == v3s16(0,-1,0))
327 vertices[i].Pos.rotateXYBy(-90);
328 if(dir == v3s16(0,1,0))
329 vertices[i].Pos.rotateXYBy(90);
331 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
335 video::SMaterial material;
336 material.setFlag(video::EMF_LIGHTING, false);
337 material.setFlag(video::EMF_BACK_FACE_CULLING, false);
338 material.setFlag(video::EMF_BILINEAR_FILTER, false);
339 material.setFlag(video::EMF_FOG_ENABLE, true);
340 //material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
341 material.MaterialType
342 = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
344 material.setTexture(0,
345 g_texturesource->getTextureRaw("sign_wall.png"));
347 u16 indices[] = {0,1,2,2,3,0};
348 // Add to mesh collector
349 collector.append(material, vertices, 4, indices, 6);
352 Add flowing liquid to mesh
354 else if(content_features(n).liquid_type == LIQUID_FLOWING)
356 assert(content_features(n).special_material);
357 video::SMaterial &liquid_material =
358 *content_features(n).special_material;
359 assert(content_features(n).special_atlas);
360 AtlasPointer &pa_liquid1 =
361 *content_features(n).special_atlas;
363 bool top_is_same_liquid = false;
364 MapNode ntop = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y+1,z));
365 content_t c_flowing = content_features(n).liquid_alternative_flowing;
366 content_t c_source = content_features(n).liquid_alternative_source;
367 if(ntop.getContent() == c_flowing || ntop.getContent() == c_source)
368 top_is_same_liquid = true;
371 // Use the light of the node on top if possible
372 if(content_features(ntop).param_type == CPT_LIGHT)
373 l = decode_light(ntop.getLightBlend(data->m_daynight_ratio));
374 // Otherwise use the light of this node (the liquid)
376 l = decode_light(n.getLightBlend(data->m_daynight_ratio));
377 video::SColor c = MapBlock_LightColor(
378 content_features(n).vertex_alpha, l);
380 // Neighbor liquid levels (key = relative position)
381 // Includes current node
382 core::map<v3s16, f32> neighbor_levels;
383 core::map<v3s16, content_t> neighbor_contents;
384 core::map<v3s16, u8> neighbor_flags;
385 const u8 neighborflag_top_is_same_liquid = 0x01;
386 v3s16 neighbor_dirs[9] = {
397 for(u32 i=0; i<9; i++)
399 content_t content = CONTENT_AIR;
400 float level = -0.5 * BS;
403 v3s16 p2 = p + neighbor_dirs[i];
404 MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
405 if(n2.getContent() != CONTENT_IGNORE)
407 content = n2.getContent();
409 if(n2.getContent() == c_source)
410 level = (-0.5+node_liquid_level) * BS;
411 else if(n2.getContent() == c_flowing)
412 level = (-0.5 + ((float)(n2.param2&LIQUID_LEVEL_MASK)
413 + 0.5) / 8.0 * node_liquid_level) * BS;
415 // Check node above neighbor.
416 // NOTE: This doesn't get executed if neighbor
419 n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
420 if(n2.getContent() == c_source ||
421 n2.getContent() == c_flowing)
422 flags |= neighborflag_top_is_same_liquid;
425 neighbor_levels.insert(neighbor_dirs[i], level);
426 neighbor_contents.insert(neighbor_dirs[i], content);
427 neighbor_flags.insert(neighbor_dirs[i], flags);
430 // Corner heights (average between four liquids)
431 f32 corner_levels[4];
433 v3s16 halfdirs[4] = {
439 for(u32 i=0; i<4; i++)
441 v3s16 cornerdir = halfdirs[i];
442 float cornerlevel = 0;
445 for(u32 j=0; j<4; j++)
447 v3s16 neighbordir = cornerdir - halfdirs[j];
448 content_t content = neighbor_contents[neighbordir];
449 // If top is liquid, draw starting from top of node
450 if(neighbor_flags[neighbordir] &
451 neighborflag_top_is_same_liquid)
453 cornerlevel = 0.5*BS;
457 // Source is always the same height
458 else if(content == c_source)
460 cornerlevel = (-0.5+node_liquid_level)*BS;
464 // Flowing liquid has level information
465 else if(content == c_flowing)
467 cornerlevel += neighbor_levels[neighbordir];
470 else if(content == CONTENT_AIR)
476 cornerlevel = -0.5*BS;
477 else if(valid_count > 0)
478 cornerlevel /= valid_count;
479 corner_levels[i] = cornerlevel;
486 v3s16 side_dirs[4] = {
492 s16 side_corners[4][2] = {
498 for(u32 i=0; i<4; i++)
500 v3s16 dir = side_dirs[i];
503 If our topside is liquid and neighbor's topside
504 is liquid, don't draw side face
506 if(top_is_same_liquid &&
507 neighbor_flags[dir] & neighborflag_top_is_same_liquid)
510 content_t neighbor_content = neighbor_contents[dir];
512 // Don't draw face if neighbor is not air or liquid
513 if(neighbor_content != CONTENT_AIR
514 && content_liquid(neighbor_content) == false)
517 bool neighbor_is_same_liquid = (neighbor_content == c_source
518 || neighbor_content == c_flowing);
520 // Don't draw any faces if neighbor same is liquid and top is
522 if(neighbor_is_same_liquid == true
523 && top_is_same_liquid == false)
526 video::S3DVertex vertices[4] =
528 /*video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c, 0,1),
529 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c, 1,1),
530 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c, 1,0),
531 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c, 0,0),*/
532 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
533 pa_liquid1.x0(), pa_liquid1.y1()),
534 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
535 pa_liquid1.x1(), pa_liquid1.y1()),
536 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
537 pa_liquid1.x1(), pa_liquid1.y0()),
538 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
539 pa_liquid1.x0(), pa_liquid1.y0()),
543 If our topside is liquid, set upper border of face
544 at upper border of node
546 if(top_is_same_liquid)
548 vertices[2].Pos.Y = 0.5*BS;
549 vertices[3].Pos.Y = 0.5*BS;
552 Otherwise upper position of face is corner levels
556 vertices[2].Pos.Y = corner_levels[side_corners[i][0]];
557 vertices[3].Pos.Y = corner_levels[side_corners[i][1]];
561 If neighbor is liquid, lower border of face is corner
564 if(neighbor_is_same_liquid)
566 vertices[0].Pos.Y = corner_levels[side_corners[i][1]];
567 vertices[1].Pos.Y = corner_levels[side_corners[i][0]];
570 If neighbor is not liquid, lower border of face is
575 vertices[0].Pos.Y = -0.5*BS;
576 vertices[1].Pos.Y = -0.5*BS;
579 for(s32 j=0; j<4; j++)
581 if(dir == v3s16(0,0,1))
582 vertices[j].Pos.rotateXZBy(0);
583 if(dir == v3s16(0,0,-1))
584 vertices[j].Pos.rotateXZBy(180);
585 if(dir == v3s16(-1,0,0))
586 vertices[j].Pos.rotateXZBy(90);
587 if(dir == v3s16(1,0,-0))
588 vertices[j].Pos.rotateXZBy(-90);
590 // Do this to not cause glitches when two liquids are
592 if(neighbor_is_same_liquid == false){
593 vertices[j].Pos.X *= 0.98;
594 vertices[j].Pos.Z *= 0.98;
597 vertices[j].Pos += intToFloat(p + blockpos_nodes, BS);
600 u16 indices[] = {0,1,2,2,3,0};
601 // Add to mesh collector
602 collector.append(liquid_material, vertices, 4, indices, 6);
606 Generate top side, if appropriate
609 if(top_is_same_liquid == false)
611 video::S3DVertex vertices[4] =
613 /*video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c, 0,1),
614 video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c, 1,1),
615 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c, 1,0),
616 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c, 0,0),*/
617 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
618 pa_liquid1.x0(), pa_liquid1.y1()),
619 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
620 pa_liquid1.x1(), pa_liquid1.y1()),
621 video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c,
622 pa_liquid1.x1(), pa_liquid1.y0()),
623 video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c,
624 pa_liquid1.x0(), pa_liquid1.y0()),
627 // This fixes a strange bug
628 s32 corner_resolve[4] = {3,2,1,0};
630 for(s32 i=0; i<4; i++)
632 //vertices[i].Pos.Y += liquid_level;
633 //vertices[i].Pos.Y += neighbor_levels[v3s16(0,0,0)];
634 s32 j = corner_resolve[i];
635 vertices[i].Pos.Y += corner_levels[j];
636 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
639 u16 indices[] = {0,1,2,2,3,0};
640 // Add to mesh collector
641 collector.append(liquid_material, vertices, 4, indices, 6);
645 Add water sources to mesh if using new style
647 else if(content_features(n).liquid_type == LIQUID_SOURCE
650 assert(content_features(n).special_material);
651 video::SMaterial &liquid_material =
652 *content_features(n).special_material;
653 assert(content_features(n).special_atlas);
654 AtlasPointer &pa_liquid1 =
655 *content_features(n).special_atlas;
657 bool top_is_air = false;
658 MapNode n = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y+1,z));
659 if(n.getContent() == CONTENT_AIR)
662 if(top_is_air == false)
665 u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio));
666 video::SColor c = MapBlock_LightColor(
667 content_features(n).vertex_alpha, l);
669 video::S3DVertex vertices[4] =
671 /*video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c, 0,1),
672 video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c, 1,1),
673 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c, 1,0),
674 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c, 0,0),*/
675 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
676 pa_liquid1.x0(), pa_liquid1.y1()),
677 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
678 pa_liquid1.x1(), pa_liquid1.y1()),
679 video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c,
680 pa_liquid1.x1(), pa_liquid1.y0()),
681 video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c,
682 pa_liquid1.x0(), pa_liquid1.y0()),
685 for(s32 i=0; i<4; i++)
687 vertices[i].Pos.Y += (-0.5+node_liquid_level)*BS;
688 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
691 u16 indices[] = {0,1,2,2,3,0};
692 // Add to mesh collector
693 collector.append(liquid_material, vertices, 4, indices, 6);
696 Add leaves if using new style
698 else if(n.getContent() == CONTENT_LEAVES && new_style_leaves)
700 /*u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio));*/
701 u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio)));
702 video::SColor c = MapBlock_LightColor(255, l);
704 for(u32 j=0; j<6; j++)
706 video::S3DVertex vertices[4] =
708 /*video::S3DVertex(-BS/2,-BS/2,BS/2, 0,0,0, c, 0,1),
709 video::S3DVertex(BS/2,-BS/2,BS/2, 0,0,0, c, 1,1),
710 video::S3DVertex(BS/2,BS/2,BS/2, 0,0,0, c, 1,0),
711 video::S3DVertex(-BS/2,BS/2,BS/2, 0,0,0, c, 0,0),*/
712 video::S3DVertex(-BS/2,-BS/2,BS/2, 0,0,0, c,
713 pa_leaves1.x0(), pa_leaves1.y1()),
714 video::S3DVertex(BS/2,-BS/2,BS/2, 0,0,0, c,
715 pa_leaves1.x1(), pa_leaves1.y1()),
716 video::S3DVertex(BS/2,BS/2,BS/2, 0,0,0, c,
717 pa_leaves1.x1(), pa_leaves1.y0()),
718 video::S3DVertex(-BS/2,BS/2,BS/2, 0,0,0, c,
719 pa_leaves1.x0(), pa_leaves1.y0()),
724 for(u16 i=0; i<4; i++)
725 vertices[i].Pos.rotateXZBy(0);
729 for(u16 i=0; i<4; i++)
730 vertices[i].Pos.rotateXZBy(180);
734 for(u16 i=0; i<4; i++)
735 vertices[i].Pos.rotateXZBy(-90);
739 for(u16 i=0; i<4; i++)
740 vertices[i].Pos.rotateXZBy(90);
744 for(u16 i=0; i<4; i++)
745 vertices[i].Pos.rotateYZBy(-90);
749 for(u16 i=0; i<4; i++)
750 vertices[i].Pos.rotateYZBy(90);
753 for(u16 i=0; i<4; i++)
755 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
758 u16 indices[] = {0,1,2,2,3,0};
759 // Add to mesh collector
760 collector.append(material_leaves1, vertices, 4, indices, 6);
766 else if(n.getContent() == CONTENT_GLASS)
768 u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio)));
769 video::SColor c = MapBlock_LightColor(255, l);
771 for(u32 j=0; j<6; j++)
773 video::S3DVertex vertices[4] =
775 video::S3DVertex(-BS/2,-BS/2,BS/2, 0,0,0, c,
776 pa_glass.x0(), pa_glass.y1()),
777 video::S3DVertex(BS/2,-BS/2,BS/2, 0,0,0, c,
778 pa_glass.x1(), pa_glass.y1()),
779 video::S3DVertex(BS/2,BS/2,BS/2, 0,0,0, c,
780 pa_glass.x1(), pa_glass.y0()),
781 video::S3DVertex(-BS/2,BS/2,BS/2, 0,0,0, c,
782 pa_glass.x0(), pa_glass.y0()),
787 for(u16 i=0; i<4; i++)
788 vertices[i].Pos.rotateXZBy(0);
792 for(u16 i=0; i<4; i++)
793 vertices[i].Pos.rotateXZBy(180);
797 for(u16 i=0; i<4; i++)
798 vertices[i].Pos.rotateXZBy(-90);
802 for(u16 i=0; i<4; i++)
803 vertices[i].Pos.rotateXZBy(90);
807 for(u16 i=0; i<4; i++)
808 vertices[i].Pos.rotateYZBy(-90);
812 for(u16 i=0; i<4; i++)
813 vertices[i].Pos.rotateYZBy(90);
816 for(u16 i=0; i<4; i++)
818 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
821 u16 indices[] = {0,1,2,2,3,0};
822 // Add to mesh collector
823 collector.append(material_glass, vertices, 4, indices, 6);
829 else if(n.getContent() == CONTENT_FENCE)
831 u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio)));
832 video::SColor c = MapBlock_LightColor(255, l);
834 const f32 post_rad=(f32)BS/10;
835 const f32 bar_rad=(f32)BS/20;
836 const f32 bar_len=(f32)(BS/2)-post_rad;
838 // The post - always present
839 v3f pos = intToFloat(p+blockpos_nodes, BS);
847 makeCuboid(material_wood, &collector,
849 post_rad,BS/2,post_rad, postuv);
851 // Now a section of fence, +X, if there's a post there
854 MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
855 if(n2.getContent() == CONTENT_FENCE)
857 pos = intToFloat(p+blockpos_nodes, BS);
867 makeCuboid(material_wood, &collector,
869 bar_len,bar_rad,bar_rad, xrailuv);
872 makeCuboid(material_wood, &collector,
874 bar_len,bar_rad,bar_rad, xrailuv);
877 // Now a section of fence, +Z, if there's a post there
880 n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
881 if(n2.getContent() == CONTENT_FENCE)
883 pos = intToFloat(p+blockpos_nodes, BS);
893 makeCuboid(material_wood, &collector,
895 bar_rad,bar_rad,bar_len, zrailuv);
897 makeCuboid(material_wood, &collector,
899 bar_rad,bar_rad,bar_len, zrailuv);
906 Add stones with minerals if stone is invisible
908 else if(n.getContent() == CONTENT_STONE && invisible_stone && n.getMineral() != MINERAL_NONE)
910 for(u32 j=0; j<6; j++)
912 // NOTE: Hopefully g_6dirs[j] is the right direction...
913 v3s16 dir = g_6dirs[j];
915 MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + dir);
916 if(content_features(n2).param_type == CPT_LIGHT)
917 l = decode_light(n2.getLightBlend(data->m_daynight_ratio));
921 video::SColor c = MapBlock_LightColor(255, l);
923 // Get the right texture
924 TileSpec ts = n.getTile(dir);
925 AtlasPointer ap = ts.texture;
926 material_general.setTexture(0, ap.atlas);
928 video::S3DVertex vertices[4] =
930 /*video::S3DVertex(-BS/2,-BS/2,BS/2, 0,0,0, c, 0,1),
931 video::S3DVertex(BS/2,-BS/2,BS/2, 0,0,0, c, 1,1),
932 video::S3DVertex(BS/2,BS/2,BS/2, 0,0,0, c, 1,0),
933 video::S3DVertex(-BS/2,BS/2,BS/2, 0,0,0, c, 0,0),*/
934 video::S3DVertex(-BS/2,-BS/2,BS/2, 0,0,0, c,
936 video::S3DVertex(BS/2,-BS/2,BS/2, 0,0,0, c,
938 video::S3DVertex(BS/2,BS/2,BS/2, 0,0,0, c,
940 video::S3DVertex(-BS/2,BS/2,BS/2, 0,0,0, c,
946 for(u16 i=0; i<4; i++)
947 vertices[i].Pos.rotateXZBy(0);
951 for(u16 i=0; i<4; i++)
952 vertices[i].Pos.rotateXZBy(180);
956 for(u16 i=0; i<4; i++)
957 vertices[i].Pos.rotateXZBy(-90);
961 for(u16 i=0; i<4; i++)
962 vertices[i].Pos.rotateXZBy(90);
966 for(u16 i=0; i<4; i++)
968 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
971 u16 indices[] = {0,1,2,2,3,0};
972 // Add to mesh collector
973 collector.append(material_general, vertices, 4, indices, 6);
977 else if(n.getContent() == CONTENT_PAPYRUS)
979 u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio)));
980 video::SColor c = MapBlock_LightColor(255, l);
982 for(u32 j=0; j<4; j++)
984 video::S3DVertex vertices[4] =
986 video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c,
987 pa_papyrus.x0(), pa_papyrus.y1()),
988 video::S3DVertex(BS/2,-BS/2,0, 0,0,0, c,
989 pa_papyrus.x1(), pa_papyrus.y1()),
990 video::S3DVertex(BS/2,BS/2,0, 0,0,0, c,
991 pa_papyrus.x1(), pa_papyrus.y0()),
992 video::S3DVertex(-BS/2,BS/2,0, 0,0,0, c,
993 pa_papyrus.x0(), pa_papyrus.y0()),
998 for(u16 i=0; i<4; i++)
999 vertices[i].Pos.rotateXZBy(45);
1003 for(u16 i=0; i<4; i++)
1004 vertices[i].Pos.rotateXZBy(-45);
1008 for(u16 i=0; i<4; i++)
1009 vertices[i].Pos.rotateXZBy(135);
1013 for(u16 i=0; i<4; i++)
1014 vertices[i].Pos.rotateXZBy(-135);
1017 for(u16 i=0; i<4; i++)
1019 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
1022 u16 indices[] = {0,1,2,2,3,0};
1023 // Add to mesh collector
1024 collector.append(material_papyrus, vertices, 4, indices, 6);
1027 else if(n.getContent() == CONTENT_JUNGLEGRASS)
1029 u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio)));
1030 video::SColor c = MapBlock_LightColor(255, l);
1032 for(u32 j=0; j<4; j++)
1034 video::S3DVertex vertices[4] =
1036 video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c,
1037 pa_papyrus.x0(), pa_papyrus.y1()),
1038 video::S3DVertex(BS/2,-BS/2,0, 0,0,0, c,
1039 pa_papyrus.x1(), pa_papyrus.y1()),
1040 video::S3DVertex(BS/2,BS/1,0, 0,0,0, c,
1041 pa_papyrus.x1(), pa_papyrus.y0()),
1042 video::S3DVertex(-BS/2,BS/1,0, 0,0,0, c,
1043 pa_papyrus.x0(), pa_papyrus.y0()),
1048 for(u16 i=0; i<4; i++)
1049 vertices[i].Pos.rotateXZBy(45);
1053 for(u16 i=0; i<4; i++)
1054 vertices[i].Pos.rotateXZBy(-45);
1058 for(u16 i=0; i<4; i++)
1059 vertices[i].Pos.rotateXZBy(135);
1063 for(u16 i=0; i<4; i++)
1064 vertices[i].Pos.rotateXZBy(-135);
1067 for(u16 i=0; i<4; i++)
1069 vertices[i].Pos *= 1.3;
1070 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
1073 u16 indices[] = {0,1,2,2,3,0};
1074 // Add to mesh collector
1075 collector.append(material_junglegrass, vertices, 4, indices, 6);
1078 else if(n.getContent() == CONTENT_RAIL)
1080 u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio));
1081 video::SColor c = MapBlock_LightColor(255, l);
1083 bool is_rail_x [] = { false, false }; /* x-1, x+1 */
1084 bool is_rail_z [] = { false, false }; /* z-1, z+1 */
1086 MapNode n_minus_x = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x-1,y,z));
1087 MapNode n_plus_x = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x+1,y,z));
1088 MapNode n_minus_z = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y,z-1));
1089 MapNode n_plus_z = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y,z+1));
1091 if(n_minus_x.getContent() == CONTENT_RAIL)
1092 is_rail_x[0] = true;
1093 if(n_plus_x.getContent() == CONTENT_RAIL)
1094 is_rail_x[1] = true;
1095 if(n_minus_z.getContent() == CONTENT_RAIL)
1096 is_rail_z[0] = true;
1097 if(n_plus_z.getContent() == CONTENT_RAIL)
1098 is_rail_z[1] = true;
1100 float d = (float)BS/16;
1101 video::S3DVertex vertices[4] =
1103 video::S3DVertex(-BS/2,-BS/2+d,-BS/2, 0,0,0, c,
1105 video::S3DVertex(BS/2,-BS/2+d,-BS/2, 0,0,0, c,
1107 video::S3DVertex(BS/2,-BS/2+d,BS/2, 0,0,0, c,
1109 video::S3DVertex(-BS/2,-BS/2+d,BS/2, 0,0,0, c,
1113 video::SMaterial material_rail;
1114 material_rail.setFlag(video::EMF_LIGHTING, false);
1115 material_rail.setFlag(video::EMF_BACK_FACE_CULLING, false);
1116 material_rail.setFlag(video::EMF_BILINEAR_FILTER, false);
1117 material_rail.setFlag(video::EMF_FOG_ENABLE, true);
1118 material_rail.MaterialType
1119 = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
1121 int adjacencies = is_rail_x[0] + is_rail_x[1] + is_rail_z[0] + is_rail_z[1];
1125 material_rail.setTexture(0, g_texturesource->getTextureRaw("rail.png"));
1126 else if(adjacencies == 2)
1128 if((is_rail_x[0] && is_rail_x[1]) || (is_rail_z[0] && is_rail_z[1]))
1129 material_rail.setTexture(0, g_texturesource->getTextureRaw("rail.png"));
1131 material_rail.setTexture(0, g_texturesource->getTextureRaw("rail_curved.png"));
1133 else if(adjacencies == 3)
1134 material_rail.setTexture(0, g_texturesource->getTextureRaw("rail_t_junction.png"));
1135 else if(adjacencies == 4)
1136 material_rail.setTexture(0, g_texturesource->getTextureRaw("rail_crossing.png"));
1141 if(adjacencies == 1)
1143 if(is_rail_x[0] || is_rail_x[1])
1146 else if(adjacencies == 2)
1148 if(is_rail_x[0] && is_rail_x[1])
1150 else if(is_rail_x[0] && is_rail_z[0])
1152 else if(is_rail_x[0] && is_rail_z[1])
1154 else if(is_rail_x[1] && is_rail_z[1])
1157 else if(adjacencies == 3)
1170 for(u16 i=0; i<4; i++)
1171 vertices[i].Pos.rotateXZBy(angle);
1174 for(s32 i=0; i<4; i++)
1176 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
1179 u16 indices[] = {0,1,2,2,3,0};
1180 collector.append(material_rail, vertices, 4, indices, 6);
1182 else if (n.getContent() == CONTENT_LADDER) {
1183 u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio));
1184 video::SColor c(255,l,l,l);
1186 float d = (float)BS/16;
1188 // Assume wall is at X+
1189 video::S3DVertex vertices[4] =
1191 video::S3DVertex(BS/2-d,-BS/2,-BS/2, 0,0,0, c, 0,1),
1192 video::S3DVertex(BS/2-d,-BS/2,BS/2, 0,0,0, c, 1,1),
1193 video::S3DVertex(BS/2-d,BS/2,BS/2, 0,0,0, c, 1,0),
1194 video::S3DVertex(BS/2-d,BS/2,-BS/2, 0,0,0, c, 0,0),
1197 v3s16 dir = unpackDir(n.param2);
1199 for(s32 i=0; i<4; i++)
1201 if(dir == v3s16(1,0,0))
1202 vertices[i].Pos.rotateXZBy(0);
1203 if(dir == v3s16(-1,0,0))
1204 vertices[i].Pos.rotateXZBy(180);
1205 if(dir == v3s16(0,0,1))
1206 vertices[i].Pos.rotateXZBy(90);
1207 if(dir == v3s16(0,0,-1))
1208 vertices[i].Pos.rotateXZBy(-90);
1209 if(dir == v3s16(0,-1,0))
1210 vertices[i].Pos.rotateXYBy(-90);
1211 if(dir == v3s16(0,1,0))
1212 vertices[i].Pos.rotateXYBy(90);
1214 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
1217 video::SMaterial material_ladder;
1218 material_ladder.setFlag(video::EMF_LIGHTING, false);
1219 material_ladder.setFlag(video::EMF_BACK_FACE_CULLING, false);
1220 material_ladder.setFlag(video::EMF_BILINEAR_FILTER, false);
1221 material_ladder.setFlag(video::EMF_FOG_ENABLE, true);
1222 material_ladder.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
1223 material_ladder.setTexture(0, g_texturesource->getTextureRaw("ladder.png"));
1225 u16 indices[] = {0,1,2,2,3,0};
1226 // Add to mesh collector
1227 collector.append(material_ladder, vertices, 4, indices, 6);
1229 else if(n.getContent() == CONTENT_APPLE)
1231 u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio)));
1232 video::SColor c = MapBlock_LightColor(255, l);
1234 for(u32 j=0; j<4; j++)
1236 video::S3DVertex vertices[4] =
1238 video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c,
1239 pa_apple.x0(), pa_apple.y1()),
1240 video::S3DVertex(BS/2,-BS/2,0, 0,0,0, c,
1241 pa_apple.x1(), pa_apple.y1()),
1242 video::S3DVertex(BS/2,BS/2,0, 0,0,0, c,
1243 pa_apple.x1(), pa_apple.y0()),
1244 video::S3DVertex(-BS/2,BS/2,0, 0,0,0, c,
1245 pa_apple.x0(), pa_apple.y0()),
1250 for(u16 i=0; i<4; i++)
1251 vertices[i].Pos.rotateXZBy(45);
1255 for(u16 i=0; i<4; i++)
1256 vertices[i].Pos.rotateXZBy(-45);
1260 for(u16 i=0; i<4; i++)
1261 vertices[i].Pos.rotateXZBy(135);
1265 for(u16 i=0; i<4; i++)
1266 vertices[i].Pos.rotateXZBy(-135);
1269 for(u16 i=0; i<4; i++)
1271 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
1274 u16 indices[] = {0,1,2,2,3,0};
1275 // Add to mesh collector
1276 collector.append(material_apple, vertices, 4, indices, 6);
1279 else if(n.getContent() == CONTENT_SAPLING) {
1280 u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio)));
1281 video::SColor c = MapBlock_LightColor(255, l);
1283 for(u32 j=0; j<4; j++)
1285 video::S3DVertex vertices[4] =
1287 video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c,
1288 pa_sapling.x0(), pa_sapling.y1()),
1289 video::S3DVertex(BS/2,-BS/2,0, 0,0,0, c,
1290 pa_sapling.x1(), pa_sapling.y1()),
1291 video::S3DVertex(BS/2,BS/1,0, 0,0,0, c,
1292 pa_sapling.x1(), pa_sapling.y0()),
1293 video::S3DVertex(-BS/2,BS/1,0, 0,0,0, c,
1294 pa_sapling.x0(), pa_sapling.y0()),
1299 for(u16 i=0; i<4; i++)
1300 vertices[i].Pos.rotateXZBy(45);
1304 for(u16 i=0; i<4; i++)
1305 vertices[i].Pos.rotateXZBy(-45);
1309 for(u16 i=0; i<4; i++)
1310 vertices[i].Pos.rotateXZBy(135);
1314 for(u16 i=0; i<4; i++)
1315 vertices[i].Pos.rotateXZBy(-135);
1318 for(u16 i=0; i<4; i++)
1320 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
1323 u16 indices[] = {0,1,2,2,3,0};
1324 // Add to mesh collector
1325 collector.append(material_sapling, vertices, 4, indices, 6);