sloped rails

This commit is contained in:
darkrose 2014-01-02 18:06:01 +10:00
parent 63d9e4197a
commit 5c8117c08c
2 changed files with 122 additions and 82 deletions

View File

@ -1358,110 +1358,147 @@ void mapblock_mesh_generate_special(MeshMakeData *data,
{ {
bool is_rail_x [] = { false, false }; /* x-1, x+1 */ bool is_rail_x [] = { false, false }; /* x-1, x+1 */
bool is_rail_z [] = { false, false }; /* z-1, z+1 */ bool is_rail_z [] = { false, false }; /* z-1, z+1 */
content_t type = n.getContent();
bool is_rail_z_minus_y [] = { false, false }; /* z-1, z+1; y-1 */
bool is_rail_x_minus_y [] = { false, false }; /* x-1, z+1; y-1 */
bool is_rail_z_plus_y [] = { false, false }; /* z-1, z+1; y+1 */
bool is_rail_x_plus_y [] = { false, false }; /* x-1, x+1; y+1 */
MapNode n_minus_x = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x-1,y,z)); 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_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_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)); MapNode n_plus_z = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y,z+1));
MapNode n_plus_x_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x+1, y+1, z));
MapNode n_plus_x_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x+1, y-1, z));
MapNode n_minus_x_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x-1, y+1, z));
MapNode n_minus_x_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x-1, y-1, z));
MapNode n_plus_z_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y+1, z+1));
MapNode n_minus_z_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y+1, z-1));
MapNode n_plus_z_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y-1, z+1));
MapNode n_minus_z_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y-1, z-1));
if(n_minus_x.getContent() == type) content_t thiscontent = n.getContent();
if (n_minus_x.getContent() == thiscontent)
is_rail_x[0] = true; is_rail_x[0] = true;
if(n_plus_x.getContent() == type) if (n_minus_x_minus_y.getContent() == thiscontent)
is_rail_x_minus_y[0] = true;
if (n_minus_x_plus_y.getContent() == thiscontent)
is_rail_x_plus_y[0] = true;
if (n_plus_x.getContent() == thiscontent)
is_rail_x[1] = true; is_rail_x[1] = true;
if(n_minus_z.getContent() == type) if (n_plus_x_minus_y.getContent() == thiscontent)
is_rail_x_minus_y[1] = true;
if (n_plus_x_plus_y.getContent() == thiscontent)
is_rail_x_plus_y[1] = true;
if (n_minus_z.getContent() == thiscontent)
is_rail_z[0] = true; is_rail_z[0] = true;
if(n_plus_z.getContent() == type) if (n_minus_z_minus_y.getContent() == thiscontent)
is_rail_z_minus_y[0] = true;
if (n_minus_z_plus_y.getContent() == thiscontent)
is_rail_z_plus_y[0] = true;
if (n_plus_z.getContent() == thiscontent)
is_rail_z[1] = true; is_rail_z[1] = true;
if (n_plus_z_minus_y.getContent() == thiscontent)
is_rail_z_minus_y[1] = true;
if (n_plus_z_plus_y.getContent() == thiscontent)
is_rail_z_plus_y[1] = true;
int adjacencies = is_rail_x[0] + is_rail_x[1] + is_rail_z[0] + is_rail_z[1]; bool is_rail_x_all[] = {false, false};
bool is_rail_z_all[] = {false, false};
is_rail_x_all[0] = is_rail_x[0] || is_rail_x_minus_y[0] || is_rail_x_plus_y[0];
is_rail_x_all[1] = is_rail_x[1] || is_rail_x_minus_y[1] || is_rail_x_plus_y[1];
is_rail_z_all[0] = is_rail_z[0] || is_rail_z_minus_y[0] || is_rail_z_plus_y[0];
is_rail_z_all[1] = is_rail_z[1] || is_rail_z_minus_y[1] || is_rail_z_plus_y[1];
// Assign textures // reasonable default, flat straight unrotated rail
const char *texturename = "rail.png"; bool is_straight = true;
if(adjacencies < 2) int adjacencies = 0;
texturename = "rail.png"; int angle = 0;
else if(adjacencies == 2) u8 tileindex = 0;
{
if((is_rail_x[0] && is_rail_x[1]) || (is_rail_z[0] && is_rail_z[1])) // check for sloped rail
texturename = "rail.png"; if (is_rail_x_plus_y[0] || is_rail_x_plus_y[1] || is_rail_z_plus_y[0] || is_rail_z_plus_y[1]) {
else adjacencies = 5; //5 means sloped
texturename = "rail_curved.png"; is_straight = true; // sloped is always straight
}else{
// is really straight, rails on both sides
is_straight = (is_rail_x_all[0] && is_rail_x_all[1]) || (is_rail_z_all[0] && is_rail_z_all[1]);
adjacencies = is_rail_x_all[0] + is_rail_x_all[1] + is_rail_z_all[0] + is_rail_z_all[1];
} }
else if(adjacencies == 3)
texturename = "rail_t_junction.png";
else if(adjacencies == 4)
texturename = "rail_crossing.png";
AtlasPointer ap = g_texturesource->getTexture(texturename); switch (adjacencies) {
case 1:
if(is_rail_x_all[0] || is_rail_x_all[1])
angle = 90;
break;
case 2:
if(!is_straight)
tileindex = 1; // curved
if(is_rail_x_all[0] && is_rail_x_all[1])
angle = 90;
if(is_rail_z_all[0] && is_rail_z_all[1]){
if (is_rail_z_plus_y[0])
angle = 180;
}
else if(is_rail_x_all[0] && is_rail_z_all[0])
angle = 270;
else if(is_rail_x_all[0] && is_rail_z_all[1])
angle = 180;
else if(is_rail_x_all[1] && is_rail_z_all[1])
angle = 90;
break;
case 3:
// here is where the potential to 'switch' a junction is, but not implemented at present
tileindex = 2; // t-junction
if(!is_rail_x_all[1])
angle=180;
if(!is_rail_z_all[0])
angle=90;
if(!is_rail_z_all[1])
angle=270;
break;
case 4:
tileindex = 3; // crossing
break;
case 5: //sloped
if(is_rail_z_plus_y[0])
angle = 180;
if(is_rail_x_plus_y[0])
angle = 90;
if(is_rail_x_plus_y[1])
angle = -90;
break;
default:
break;
}
video::SMaterial material_rail; TileSpec tile = content_features(thiscontent).tiles[tileindex];
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)); u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio)));
video::SColor c = MapBlock_LightColor(255, l); video::SColor c = MapBlock_LightColor(255, l);
float d = (float)BS/16; float d = (float)BS/32;
video::S3DVertex vertices[4] =
{ char g=-1;
video::S3DVertex(-BS/2,-BS/2+d,-BS/2, 0,0,0, c, if (is_rail_x_plus_y[0] || is_rail_x_plus_y[1] || is_rail_z_plus_y[0] || is_rail_z_plus_y[1])
ap.x0(), ap.y1()), g=1; //Object is at a slope
video::S3DVertex(BS/2,-BS/2+d,-BS/2, 0,0,0, c,
ap.x1(), ap.y1()), video::S3DVertex vertices[4] = {
video::S3DVertex(BS/2,-BS/2+d,BS/2, 0,0,0, c, video::S3DVertex(-BS/2,-BS/2+d,-BS/2, 0,0,0, c, tile.texture.x0(), tile.texture.y1()),
ap.x1(), ap.y0()), video::S3DVertex(BS/2,-BS/2+d,-BS/2, 0,0,0, c, tile.texture.x1(), tile.texture.y1()),
video::S3DVertex(-BS/2,-BS/2+d,BS/2, 0,0,0, c, video::S3DVertex(BS/2,g*BS/2+d,BS/2, 0,0,0, c, tile.texture.x1(), tile.texture.y0()),
ap.x0(), ap.y0()), video::S3DVertex(-BS/2,g*BS/2+d,BS/2, 0,0,0, c, tile.texture.x0(), tile.texture.y0()),
}; };
// Rotate textures for (s32 i=0; i<4; i++) {
int angle = 0; if (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<4; i++)
vertices[i].Pos.rotateXZBy(angle); vertices[i].Pos.rotateXZBy(angle);
} vertices[i].Pos += intToFloat(blockpos_nodes + p, BS);
for(s32 i=0; i<4; i++)
{
vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
} }
u16 indices[] = {0,1,2,2,3,0}; u16 indices[] = {0,1,2,2,3,0};
collector.append(material_rail, vertices, 4, indices, 6); collector.append(tile.getMaterial(), vertices, 4, indices, 6);
} }
break; break;
case CDT_PLANTLIKE_LGE: case CDT_PLANTLIKE_LGE:

View File

@ -2222,8 +2222,11 @@ void content_mapnode_init()
i = CONTENT_RAIL; i = CONTENT_RAIL;
f = &content_features(i); f = &content_features(i);
f->description = std::string("Rail"); f->description = std::string("Rail");
f->setInventoryTexture("rail.png"); f->setAllTextures("rail.png");
f->used_texturenames["rail.png"] = true; f->setTexture(1,"rail_curved.png");
f->setTexture(2,"rail_t_junction.png");
f->setTexture(3,"rail_crossing.png");
f->setAllTextureFlags(0);
f->light_propagates = true; f->light_propagates = true;
f->param_type = CPT_LIGHT; f->param_type = CPT_LIGHT;
f->draw_type = CDT_RAILLIKE; f->draw_type = CDT_RAILLIKE;