]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/client/mapblock_mesh.cpp
Improved X-Ray, added AutoEject
[dragonfireclient.git] / src / client / mapblock_mesh.cpp
index 6e0d64b55dc58c49246406d54e9510738d44f998..fbd7e2ab7ce8b577db2dccaa7c305d1d4232282f 100644 (file)
@@ -116,7 +116,7 @@ void MeshMakeData::setCrack(int crack_level, v3s16 crack_pos)
 
 void MeshMakeData::setSmoothLighting(bool smooth_lighting)
 {
-       m_smooth_lighting = smooth_lighting;
+       m_smooth_lighting = smooth_lighting && ! g_settings->getBool("fullbright");
 }
 
 /*
@@ -133,6 +133,8 @@ static u8 getInteriorLight(enum LightBank bank, MapNode n, s32 increment,
        u8 light = n.getLight(bank, ndef);
        if (light > 0)
                light = rangelim(light + increment, 0, LIGHT_SUN);
+       if(g_settings->getBool("fullbright"))
+               return 255;
        return decode_light(light);
 }
 
@@ -226,7 +228,7 @@ static u16 getSmoothLightCombined(const v3s16 &p,
                return f.light_propagates;
        };
 
-       std::array<bool, 4> obstructed = {{ 1, 1, 1, 1 }};
+       bool obstructed[4] = { true, true, true, true };
        add_node(0);
        bool opaque1 = !add_node(1);
        bool opaque2 = !add_node(2);
@@ -373,6 +375,32 @@ void final_color_blend(video::SColor *result,
        Mesh generation helpers
 */
 
+// This table is moved outside getNodeVertexDirs to avoid the compiler using
+// a mutex to initialize this table at runtime right in the hot path.
+// For details search the internet for "cxa_guard_acquire".
+static const v3s16 vertex_dirs_table[] = {
+       // ( 1, 0, 0)
+       v3s16( 1,-1, 1), v3s16( 1,-1,-1),
+       v3s16( 1, 1,-1), v3s16( 1, 1, 1),
+       // ( 0, 1, 0)
+       v3s16( 1, 1,-1), v3s16(-1, 1,-1),
+       v3s16(-1, 1, 1), v3s16( 1, 1, 1),
+       // ( 0, 0, 1)
+       v3s16(-1,-1, 1), v3s16( 1,-1, 1),
+       v3s16( 1, 1, 1), v3s16(-1, 1, 1),
+       // invalid
+       v3s16(), v3s16(), v3s16(), v3s16(),
+       // ( 0, 0,-1)
+       v3s16( 1,-1,-1), v3s16(-1,-1,-1),
+       v3s16(-1, 1,-1), v3s16( 1, 1,-1),
+       // ( 0,-1, 0)
+       v3s16( 1,-1, 1), v3s16(-1,-1, 1),
+       v3s16(-1,-1,-1), v3s16( 1,-1,-1),
+       // (-1, 0, 0)
+       v3s16(-1,-1,-1), v3s16(-1,-1, 1),
+       v3s16(-1, 1, 1), v3s16(-1, 1,-1)
+};
+
 /*
        vertex_dirs: v3s16[4]
 */
@@ -385,44 +413,16 @@ static void getNodeVertexDirs(const v3s16 &dir, v3s16 *vertex_dirs)
                2: top-left
                3: top-right
        */
-       if (dir == v3s16(0, 0, 1)) {
-               // If looking towards z+, this is the face that is behind
-               // the center point, facing towards z+.
-               vertex_dirs[0] = v3s16(-1,-1, 1);
-               vertex_dirs[1] = v3s16( 1,-1, 1);
-               vertex_dirs[2] = v3s16( 1, 1, 1);
-               vertex_dirs[3] = v3s16(-1, 1, 1);
-       } else if (dir == v3s16(0, 0, -1)) {
-               // faces towards Z-
-               vertex_dirs[0] = v3s16( 1,-1,-1);
-               vertex_dirs[1] = v3s16(-1,-1,-1);
-               vertex_dirs[2] = v3s16(-1, 1,-1);
-               vertex_dirs[3] = v3s16( 1, 1,-1);
-       } else if (dir == v3s16(1, 0, 0)) {
-               // faces towards X+
-               vertex_dirs[0] = v3s16( 1,-1, 1);
-               vertex_dirs[1] = v3s16( 1,-1,-1);
-               vertex_dirs[2] = v3s16( 1, 1,-1);
-               vertex_dirs[3] = v3s16( 1, 1, 1);
-       } else if (dir == v3s16(-1, 0, 0)) {
-               // faces towards X-
-               vertex_dirs[0] = v3s16(-1,-1,-1);
-               vertex_dirs[1] = v3s16(-1,-1, 1);
-               vertex_dirs[2] = v3s16(-1, 1, 1);
-               vertex_dirs[3] = v3s16(-1, 1,-1);
-       } else if (dir == v3s16(0, 1, 0)) {
-               // faces towards Y+ (assume Z- as "down" in texture)
-               vertex_dirs[0] = v3s16( 1, 1,-1);
-               vertex_dirs[1] = v3s16(-1, 1,-1);
-               vertex_dirs[2] = v3s16(-1, 1, 1);
-               vertex_dirs[3] = v3s16( 1, 1, 1);
-       } else if (dir == v3s16(0, -1, 0)) {
-               // faces towards Y- (assume Z+ as "down" in texture)
-               vertex_dirs[0] = v3s16( 1,-1, 1);
-               vertex_dirs[1] = v3s16(-1,-1, 1);
-               vertex_dirs[2] = v3s16(-1,-1,-1);
-               vertex_dirs[3] = v3s16( 1,-1,-1);
-       }
+
+       // Direction must be (1,0,0), (-1,0,0), (0,1,0), (0,-1,0),
+       // (0,0,1), (0,0,-1)
+       assert(dir.X * dir.X + dir.Y * dir.Y + dir.Z * dir.Z == 1);
+
+       // Convert direction to single integer for table lookup
+       u8 idx = (dir.X + 2 * dir.Y + 3 * dir.Z) & 7;
+       idx = (idx - 1) * 4;
+
+       memcpy(vertex_dirs, &vertex_dirs_table[idx], 4 * sizeof(v3s16));
 }
 
 static void getNodeTextureCoords(v3f base, const v3f &scale, const v3s16 &dir, float *u, float *v)
@@ -679,6 +679,7 @@ static u8 face_contents(content_t m1, content_t m2, bool *equivalent,
        u8 c1 = f1.solidness;
        u8 c2 = f2.solidness;
 
+
        if (c1 == c2)
                return 0;
 
@@ -687,6 +688,7 @@ static u8 face_contents(content_t m1, content_t m2, bool *equivalent,
        else if (c2 == 0)
                c2 = f2.visual_solidness;
 
+
        if (c1 == c2) {
                *equivalent = true;
                // If same solidness, liquid takes precense
@@ -786,6 +788,24 @@ void getNodeTile(MapNode mn, const v3s16 &p, const v3s16 &dir, MeshMakeData *dat
        tile.rotation = tile.world_aligned ? 0 : dir_to_tile[tile_index + 1];
 }
 
+std::set<content_t> splitToContentT(std::string str, const NodeDefManager *ndef)
+{
+       str += "\n";
+       std::set<content_t> dat;
+       std::string buf;
+       for (char c : str) {
+               if (c == ',' || c == '\n') {
+                       if (! buf.empty()) {
+                               dat.insert(ndef->getId(buf));
+                       }
+                       buf.clear();
+               } else if (c != ' ') {
+                       buf += c;
+               }
+       }
+       return dat;
+}
+
 static void getTileInfo(
                // Input:
                MeshMakeData *data,
@@ -797,31 +817,41 @@ static void getTileInfo(
                v3s16 &face_dir_corrected,
                u16 *lights,
                u8 &waving,
-               TileSpec &tile
+               TileSpec &tile,
+               bool xray,
+               std::set<content_t> xraySet
        )
 {
        VoxelManipulator &vmanip = data->m_vmanip;
        const NodeDefManager *ndef = data->m_client->ndef();
        v3s16 blockpos_nodes = data->m_blockpos * MAP_BLOCKSIZE;
-
+       
        const MapNode &n0 = vmanip.getNodeRefUnsafe(blockpos_nodes + p);
 
+       content_t c0 = n0.getContent();
+       if (xray && xraySet.find(c0) != xraySet.end())
+               c0 = CONTENT_AIR;
+
        // Don't even try to get n1 if n0 is already CONTENT_IGNORE
-       if (n0.getContent() == CONTENT_IGNORE) {
+       if (c0 == CONTENT_IGNORE) {
                makes_face = false;
                return;
        }
 
        const MapNode &n1 = vmanip.getNodeRefUnsafeCheckFlags(blockpos_nodes + p + face_dir);
 
-       if (n1.getContent() == CONTENT_IGNORE) {
+       content_t c1 = n1.getContent();
+       if (xray && xraySet.find(c1) != xraySet.end())
+               c1 = CONTENT_AIR;
+
+       if (c1 == CONTENT_IGNORE) {
                makes_face = false;
                return;
        }
 
        // This is hackish
        bool equivalent = false;
-       u8 mf = face_contents(n0.getContent(), n1.getContent(),
+       u8 mf = face_contents(c0, c1,
                        &equivalent, ndef);
 
        if (mf == 0) {
@@ -877,7 +907,9 @@ static void updateFastFaceRow(
                v3s16 translate_dir,
                const v3f &&translate_dir_f,
                const v3s16 &&face_dir,
-               std::vector<FastFace> &dest)
+               std::vector<FastFace> &dest,
+               bool xray,
+               std::set<content_t> xraySet)
 {
        static thread_local const bool waving_liquids =
                g_settings->getBool("enable_shaders") &&
@@ -891,11 +923,13 @@ static void updateFastFaceRow(
        v3s16 p_corrected;
        v3s16 face_dir_corrected;
        u16 lights[4] = {0, 0, 0, 0};
-       u8 waving;
+       u8 waving = 0;
        TileSpec tile;
+
+       // Get info of first tile
        getTileInfo(data, p, face_dir,
                        makes_face, p_corrected, face_dir_corrected,
-                       lights, waving, tile);
+                       lights, waving, tile, xray, xraySet);
 
        // Unroll this variable which has a significant build cost
        TileSpec next_tile;
@@ -903,8 +937,6 @@ static void updateFastFaceRow(
                // If tiling can be done, this is set to false in the next step
                bool next_is_different = true;
 
-               v3s16 p_next;
-
                bool next_makes_face = false;
                v3s16 next_p_corrected;
                v3s16 next_face_dir_corrected;
@@ -913,18 +945,20 @@ static void updateFastFaceRow(
                // If at last position, there is nothing to compare to and
                // the face must be drawn anyway
                if (j != MAP_BLOCKSIZE - 1) {
-                       p_next = p + translate_dir;
+                       p += translate_dir;
 
-                       getTileInfo(data, p_next, face_dir,
+                       getTileInfo(data, p, face_dir,
                                        next_makes_face, next_p_corrected,
                                        next_face_dir_corrected, next_lights,
                                        waving,
-                                       next_tile);
+                                       next_tile,
+                                       xray,
+                                       xraySet);
 
                        if (next_makes_face == makes_face
                                        && next_p_corrected == p_corrected + translate_dir
                                        && next_face_dir_corrected == face_dir_corrected
-                                       && memcmp(next_lights, lights, ARRLEN(lights) * sizeof(u16)) == 0
+                                       && memcmp(next_lights, lights, sizeof(lights)) == 0
                                        // Don't apply fast faces to waving water.
                                        && (waving != 3 || !waving_liquids)
                                        && next_tile.isTileable(tile)) {
@@ -962,15 +996,14 @@ static void updateFastFaceRow(
                makes_face = next_makes_face;
                p_corrected = next_p_corrected;
                face_dir_corrected = next_face_dir_corrected;
-               std::memcpy(lights, next_lights, ARRLEN(lights) * sizeof(u16));
+               memcpy(lights, next_lights, sizeof(lights));
                if (next_is_different)
-                       tile = next_tile;
-               p = p_next;
+                       tile = std::move(next_tile); // faster than copy
        }
 }
 
 static void updateAllFastFaceRows(MeshMakeData *data,
-               std::vector<FastFace> &dest)
+               std::vector<FastFace> &dest, bool xray, std::set<content_t> xraySet)
 {
        /*
                Go through every y,z and get top(y+) faces in rows of x+
@@ -982,7 +1015,9 @@ static void updateAllFastFaceRows(MeshMakeData *data,
                                v3s16(1, 0, 0), //dir
                                v3f  (1, 0, 0),
                                v3s16(0, 1, 0), //face dir
-                               dest);
+                               dest,
+                               xray,
+                               xraySet);
 
        /*
                Go through every x,y and get right(x+) faces in rows of z+
@@ -994,7 +1029,9 @@ static void updateAllFastFaceRows(MeshMakeData *data,
                                v3s16(0, 0, 1), //dir
                                v3f  (0, 0, 1),
                                v3s16(1, 0, 0), //face dir
-                               dest);
+                               dest,
+                               xray,
+                               xraySet);
 
        /*
                Go through every y,z and get back(z+) faces in rows of x+
@@ -1006,7 +1043,9 @@ static void updateAllFastFaceRows(MeshMakeData *data,
                                v3s16(1, 0, 0), //dir
                                v3f  (1, 0, 0),
                                v3s16(0, 0, 1), //face dir
-                               dest);
+                               dest,
+                               xray,
+                               xraySet);
 }
 
 static void applyTileColor(PreMeshBuffer &pmb)
@@ -1053,18 +1092,25 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
 
        std::vector<FastFace> fastfaces_new;
        fastfaces_new.reserve(512);
-
+       /*
+               X-Ray
+       */
+       bool xray = g_settings->getBool("xray");
+       std::set<content_t> xraySet;
+       if (xray)
+               xraySet = splitToContentT(g_settings->get("xray_nodes"), data->m_client->ndef());
+       
        /*
                We are including the faces of the trailing edges of the block.
                This means that when something changes, the caller must
                also update the meshes of the blocks at the leading edges.
 
                NOTE: This is the slowest part of this method.
-       */
+       */      
        {
                // 4-23ms for MAP_BLOCKSIZE=16  (NOTE: probably outdated)
                //TimeTaker timer2("updateAllFastFaceRows()");
-               updateAllFastFaceRows(data, fastfaces_new);
+               updateAllFastFaceRows(data, fastfaces_new, xray, xraySet);
        }
        // End of slow part