/*
Minetest-c55
Copyright (C) 2010-2011 celeron55, Perttu Ahola
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "content_mapblock.h"
#include "main.h" // For g_settings
#include "mineral.h"
#include "mapblock_mesh.h" // For MapBlock_LightColor()
#include "settings.h"
#include "nodedef.h"
#include "gamedef.h"
#ifndef SERVER
// Create a cuboid.
// material - the material to use (for all 6 faces)
// collector - the MeshCollector for the resulting polygons
// pa - texture atlas pointer for the material
// c - vertex colour - used for all
// pos - the position of the centre of the cuboid
// rz,ry,rz - the radius of the cuboid in each dimension
// txc - texture coordinates - this is a list of texture coordinates
// for the opposite corners of each face - therefore, there
// should be (2+2)*6=24 values in the list. Alternatively, pass
// NULL to use the entire texture for each face. The order of
// the faces in the list is top-backi-right-front-left-bottom
// If you specified 0,0,1,1 for each face, that would be the
// same as passing NULL.
void makeCuboid(video::SMaterial &material, MeshCollector *collector,
AtlasPointer* pa, video::SColor &c,
v3f &pos, f32 rx, f32 ry, f32 rz, f32* txc)
{
f32 tu0=pa->x0();
f32 tu1=pa->x1();
f32 tv0=pa->y0();
f32 tv1=pa->y1();
f32 txus=tu1-tu0;
f32 txvs=tv1-tv0;
video::S3DVertex v[4] =
{
video::S3DVertex(0,0,0, 0,0,0, c, tu0, tv1),
video::S3DVertex(0,0,0, 0,0,0, c, tu1, tv1),
video::S3DVertex(0,0,0, 0,0,0, c, tu1, tv0),
video::S3DVertex(0,0,0, 0,0,0, c, tu0, tv0)
};
for(int i=0;indef();
// 0ms
//TimeTaker timer("mapblock_mesh_generate_special()");
/*
Some settings
*/
bool new_style_water = g_settings->getBool("new_style_water");
float node_liquid_level = 1.0;
if(new_style_water)
node_liquid_level = 0.85;
v3s16 blockpos_nodes = data->m_blockpos*MAP_BLOCKSIZE;
/*// General ground material for special output
// Texture is modified just before usage
video::SMaterial material_general;
material_general.setFlag(video::EMF_LIGHTING, false);
material_general.setFlag(video::EMF_BILINEAR_FILTER, false);
material_general.setFlag(video::EMF_FOG_ENABLE, true);
material_general.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;*/
for(s16 z=0; zm_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y+1,z));
if(n.getContent() == CONTENT_AIR)
top_is_air = true;
if(top_is_air == false)
continue;
u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio, nodedef));
video::SColor c = MapBlock_LightColor(
nodedef->get(n).alpha, l);
video::S3DVertex vertices[4] =
{
video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
pa_liquid1.x0(), pa_liquid1.y1()),
video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
pa_liquid1.x1(), pa_liquid1.y1()),
video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c,
pa_liquid1.x1(), pa_liquid1.y0()),
video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c,
pa_liquid1.x0(), pa_liquid1.y0()),
};
for(s32 i=0; iget(n).special_materials[0]);
assert(nodedef->get(n).special_materials[1]);
assert(nodedef->get(n).special_aps[0]);
video::SMaterial &liquid_material =
*nodedef->get(n).special_materials[0];
video::SMaterial &liquid_material_bfculled =
*nodedef->get(n).special_materials[1];
AtlasPointer &pa_liquid1 =
*nodedef->get(n).special_aps[0];
bool top_is_same_liquid = false;
MapNode ntop = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y+1,z));
content_t c_flowing = nodedef->getId(nodedef->get(n).liquid_alternative_flowing);
content_t c_source = nodedef->getId(nodedef->get(n).liquid_alternative_source);
if(ntop.getContent() == c_flowing || ntop.getContent() == c_source)
top_is_same_liquid = true;
u8 l = 0;
// Use the light of the node on top if possible
if(nodedef->get(ntop).param_type == CPT_LIGHT)
l = decode_light(ntop.getLightBlend(data->m_daynight_ratio, nodedef));
// Otherwise use the light of this node (the liquid)
else
l = decode_light(n.getLightBlend(data->m_daynight_ratio, nodedef));
video::SColor c = MapBlock_LightColor(
nodedef->get(n).alpha, l);
// Neighbor liquid levels (key = relative position)
// Includes current node
core::map neighbor_levels;
core::map neighbor_contents;
core::map neighbor_flags;
const u8 neighborflag_top_is_same_liquid = 0x01;
v3s16 neighbor_dirs[9] = {
v3s16(0,0,0),
v3s16(0,0,1),
v3s16(0,0,-1),
v3s16(1,0,0),
v3s16(-1,0,0),
v3s16(1,0,1),
v3s16(-1,0,-1),
v3s16(1,0,-1),
v3s16(-1,0,1),
};
for(u32 i=0; im_vmanip.getNodeNoEx(blockpos_nodes + p2);
if(n2.getContent() != CONTENT_IGNORE)
{
content = n2.getContent();
if(n2.getContent() == c_source)
level = (-0.5+node_liquid_level) * BS;
else if(n2.getContent() == c_flowing)
level = (-0.5 + ((float)(n2.param2&LIQUID_LEVEL_MASK)
+ 0.5) / 8.0 * node_liquid_level) * BS;
// Check node above neighbor.
// NOTE: This doesn't get executed if neighbor
// doesn't exist
p2.Y += 1;
n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
if(n2.getContent() == c_source ||
n2.getContent() == c_flowing)
flags |= neighborflag_top_is_same_liquid;
}
neighbor_levels.insert(neighbor_dirs[i], level);
neighbor_contents.insert(neighbor_dirs[i], content);
neighbor_flags.insert(neighbor_dirs[i], flags);
}
// Corner heights (average between four liquids)
f32 corner_levels[4];
v3s16 halfdirs[4] = {
v3s16(0,0,0),
v3s16(1,0,0),
v3s16(1,0,1),
v3s16(0,0,1),
};
for(u32 i=0; i 0)
cornerlevel /= valid_count;
corner_levels[i] = cornerlevel;
}
/*
Generate sides
*/
v3s16 side_dirs[4] = {
v3s16(1,0,0),
v3s16(-1,0,0),
v3s16(0,0,1),
v3s16(0,0,-1),
};
s16 side_corners[4][2] = {
{1, 2},
{3, 0},
{2, 3},
{0, 1},
};
for(u32 i=0; iget(neighbor_content);
// Don't draw face if neighbor is blocking the view
if(n_feat.solidness == 2)
continue;
bool neighbor_is_same_liquid = (neighbor_content == c_source
|| neighbor_content == c_flowing);
// Don't draw any faces if neighbor same is liquid and top is
// same liquid
if(neighbor_is_same_liquid == true
&& top_is_same_liquid == false)
continue;
// Use backface culled material if neighbor doesn't have a
// solidness of 0
video::SMaterial *current_material = &liquid_material;
if(n_feat.solidness != 0 || n_feat.visual_solidness != 0)
current_material = &liquid_material_bfculled;
video::S3DVertex vertices[4] =
{
video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
pa_liquid1.x0(), pa_liquid1.y1()),
video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
pa_liquid1.x1(), pa_liquid1.y1()),
video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
pa_liquid1.x1(), pa_liquid1.y0()),
video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
pa_liquid1.x0(), pa_liquid1.y0()),
};
/*
If our topside is liquid, set upper border of face
at upper border of node
*/
if(top_is_same_liquid)
{
vertices[2].Pos.Y = 0.5*BS;
vertices[3].Pos.Y = 0.5*BS;
}
/*
Otherwise upper position of face is corner levels
*/
else
{
vertices[2].Pos.Y = corner_levels[side_corners[i][0]];
vertices[3].Pos.Y = corner_levels[side_corners[i][1]];
}
/*
If neighbor is liquid, lower border of face is corner
liquid levels
*/
if(neighbor_is_same_liquid)
{
vertices[0].Pos.Y = corner_levels[side_corners[i][1]];
vertices[1].Pos.Y = corner_levels[side_corners[i][0]];
}
/*
If neighbor is not liquid, lower border of face is
lower border of node
*/
else
{
vertices[0].Pos.Y = -0.5*BS;
vertices[1].Pos.Y = -0.5*BS;
}
for(s32 j=0; jm_vmanip.getNodeNoEx(blockpos_nodes + p2);
const ContentFeatures *f2 = &nodedef->get(n2);
if(f2->drawtype == NDT_FENCELIKE)
{
pos = intToFloat(p+blockpos_nodes, BS);
pos.X += BS/2;
pos.Y += BS/4;
f32 xrailuv[24]={
0,0.4,1,0.6,
0,0.4,1,0.6,
0,0.4,1,0.6,
0,0.4,1,0.6,
0,0.4,1,0.6,
0,0.4,1,0.6};
makeCuboid(material_wood, &collector,
&pa_wood, c, pos,
bar_len,bar_rad,bar_rad, xrailuv);
pos.Y -= BS/2;
makeCuboid(material_wood, &collector,
&pa_wood, c, pos,
bar_len,bar_rad,bar_rad, xrailuv);
}
// Now a section of fence, +Z, if there's a post there
p2 = p;
p2.Z++;
n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
f2 = &nodedef->get(n2);
if(f2->drawtype == NDT_FENCELIKE)
{
pos = intToFloat(p+blockpos_nodes, BS);
pos.Z += BS/2;
pos.Y += BS/4;
f32 zrailuv[24]={
0,0.4,1,0.6,
0,0.4,1,0.6,
0,0.4,1,0.6,
0,0.4,1,0.6,
0,0.4,1,0.6,
0,0.4,1,0.6};
makeCuboid(material_wood, &collector,
&pa_wood, c, pos,
bar_rad,bar_rad,bar_len, zrailuv);
pos.Y -= BS/2;
makeCuboid(material_wood, &collector,
&pa_wood, c, pos,
bar_rad,bar_rad,bar_len, zrailuv);
}
break;}
case NDT_RAILLIKE:
{
bool is_rail_x [] = { false, false }; /* x-1, x+1 */
bool is_rail_z [] = { false, false }; /* z-1, z+1 */
MapNode n_minus_x = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x-1,y,z));
MapNode n_plus_x = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x+1,y,z));
MapNode n_minus_z = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y,z-1));
MapNode n_plus_z = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y,z+1));
content_t thiscontent = n.getContent();
if(n_minus_x.getContent() == thiscontent)
is_rail_x[0] = true;
if(n_plus_x.getContent() == thiscontent)
is_rail_x[1] = true;
if(n_minus_z.getContent() == thiscontent)
is_rail_z[0] = true;
if(n_plus_z.getContent() == thiscontent)
is_rail_z[1] = true;
int adjacencies = is_rail_x[0] + is_rail_x[1] + is_rail_z[0] + is_rail_z[1];
// Assign textures
AtlasPointer ap = f.tiles[0].texture; // straight
if(adjacencies < 2)
ap = f.tiles[0].texture; // straight
else if(adjacencies == 2)
{
if((is_rail_x[0] && is_rail_x[1]) || (is_rail_z[0] && is_rail_z[1]))
ap = f.tiles[0].texture; // straight
else
ap = f.tiles[1].texture; // curved
}
else if(adjacencies == 3)
ap = f.tiles[2].texture; // t-junction
else if(adjacencies == 4)
ap = f.tiles[3].texture; // crossing
video::SMaterial material_rail;
material_rail.setFlag(video::EMF_LIGHTING, false);
material_rail.setFlag(video::EMF_BACK_FACE_CULLING, false);
material_rail.setFlag(video::EMF_BILINEAR_FILTER, false);
material_rail.setFlag(video::EMF_FOG_ENABLE, true);
material_rail.MaterialType
= video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
material_rail.setTexture(0, ap.atlas);
u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio, nodedef));
video::SColor c = MapBlock_LightColor(255, l);
float d = (float)BS/16;
video::S3DVertex vertices[4] =
{
video::S3DVertex(-BS/2,-BS/2+d,-BS/2, 0,0,0, c,
ap.x0(), ap.y1()),
video::S3DVertex(BS/2,-BS/2+d,-BS/2, 0,0,0, c,
ap.x1(), ap.y1()),
video::S3DVertex(BS/2,-BS/2+d,BS/2, 0,0,0, c,
ap.x1(), ap.y0()),
video::S3DVertex(-BS/2,-BS/2+d,BS/2, 0,0,0, c,
ap.x0(), ap.y0()),
};
// Rotate textures
int angle = 0;
if(adjacencies == 1)
{
if(is_rail_x[0] || is_rail_x[1])
angle = 90;
}
else if(adjacencies == 2)
{
if(is_rail_x[0] && is_rail_x[1])
angle = 90;
else if(is_rail_x[0] && is_rail_z[0])
angle = 270;
else if(is_rail_x[0] && is_rail_z[1])
angle = 180;
else if(is_rail_x[1] && is_rail_z[1])
angle = 90;
}
else if(adjacencies == 3)
{
if(!is_rail_x[0])
angle=0;
if(!is_rail_x[1])
angle=180;
if(!is_rail_z[0])
angle=90;
if(!is_rail_z[1])
angle=270;
}
if(angle != 0) {
for(u16 i=0; i