]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/client/clientmap.cpp
Merge branch 'master' of https://github.com/minetest/minetest
[dragonfireclient.git] / src / client / clientmap.cpp
index d372a8e46c0997851e2ba4bbf2b1aa9db5012fae..68fd41e397845dbd4aaa01cb2093d1f43bb93d48 100644 (file)
@@ -31,6 +31,37 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include <algorithm>
 #include "client/renderingengine.h"
 
+// struct MeshBufListList
+void MeshBufListList::clear()
+{
+       for (auto &list : lists)
+               list.clear();
+}
+
+void MeshBufListList::add(scene::IMeshBuffer *buf, v3s16 position, u8 layer)
+{
+       // Append to the correct layer
+       std::vector<MeshBufList> &list = lists[layer];
+       const video::SMaterial &m = buf->getMaterial();
+       for (MeshBufList &l : list) {
+               // comparing a full material is quite expensive so we don't do it if
+               // not even first texture is equal
+               if (l.m.TextureLayer[0].Texture != m.TextureLayer[0].Texture)
+                       continue;
+
+               if (l.m == m) {
+                       l.bufs.emplace_back(position, buf);
+                       return;
+               }
+       }
+       MeshBufList l;
+       l.m = m;
+       l.bufs.emplace_back(position, buf);
+       list.emplace_back(l);
+}
+
+// ClientMap
+
 ClientMap::ClientMap(
                Client *client,
                MapDrawControl &control,
@@ -122,14 +153,20 @@ void ClientMap::updateDrawList()
        }
        m_drawlist.clear();
 
-       v3f camera_position = m_camera_position;
-       v3f camera_direction = m_camera_direction;
+       const v3f camera_position = m_camera_position;
+       const v3f camera_direction = m_camera_direction;
+
+       // Use a higher fov to accomodate faster camera movements.
+       // Blocks are cropped better when they are drawn.
+       const f32 camera_fov = m_camera_fov * 1.1f;
 
        v3s16 cam_pos_nodes = floatToInt(camera_position, BS);
        v3s16 p_blocks_min;
        v3s16 p_blocks_max;
        getBlocksInViewRange(cam_pos_nodes, &p_blocks_min, &p_blocks_max);
 
+       // Number of blocks currently loaded by the client
+       u32 blocks_loaded = 0;
        // Number of blocks with mesh in rendering range
        u32 blocks_in_range_with_mesh = 0;
        // Number of blocks occlusion culled
@@ -138,7 +175,7 @@ void ClientMap::updateDrawList()
        // No occlusion culling when free_move is on and camera is
        // inside ground
        bool occlusion_culling_enabled = true;
-       if (g_settings->getBool("free_move") && g_settings->getBool("noclip")) {
+       if ((g_settings->getBool("free_move") && g_settings->getBool("noclip")) || g_settings->getBool("freecam")) {
                MapNode n = getNode(cam_pos_nodes);
                if (n.getContent() == CONTENT_IGNORE ||
                                m_nodedef->get(n).solidness == 2)
@@ -154,6 +191,7 @@ void ClientMap::updateDrawList()
                MapSector *sector = sector_it.second;
                v2s16 sp = sector->getPos();
 
+               blocks_loaded += sector->size();
                if (!m_control.range_all) {
                        if (sp.X < p_blocks_min.X || sp.X > p_blocks_max.X ||
                                        sp.Y < p_blocks_min.Z || sp.Y > p_blocks_max.Z)
@@ -175,8 +213,10 @@ void ClientMap::updateDrawList()
                                if not seen on display
                        */
 
-                       if (block->mesh)
-                               block->mesh->updateCameraOffset(m_camera_offset);
+                       if (!block->mesh) {
+                               // Ignore if mesh doesn't exist
+                               continue;
+                       }
 
                        float range = 100000 * BS;
                        if (!m_control.range_all)
@@ -184,14 +224,7 @@ void ClientMap::updateDrawList()
 
                        float d = 0.0;
                        if (!isBlockInSight(block->getPos(), camera_position,
-                                       camera_direction, m_camera_fov, range, &d))
-                               continue;
-
-
-                       /*
-                               Ignore if mesh doesn't exist
-                       */
-                       if (!block->mesh)
+                                       camera_direction, camera_fov, range, &d))
                                continue;
 
                        blocks_in_range_with_mesh++;
@@ -222,52 +255,9 @@ void ClientMap::updateDrawList()
        g_profiler->avg("MapBlock meshes in range [#]", blocks_in_range_with_mesh);
        g_profiler->avg("MapBlocks occlusion culled [#]", blocks_occlusion_culled);
        g_profiler->avg("MapBlocks drawn [#]", m_drawlist.size());
+       g_profiler->avg("MapBlocks loaded [#]", blocks_loaded);
 }
 
-struct MeshBufList
-{
-       video::SMaterial m;
-       std::vector<scene::IMeshBuffer*> bufs;
-};
-
-struct MeshBufListList
-{
-       /*!
-        * Stores the mesh buffers of the world.
-        * The array index is the material's layer.
-        * The vector part groups vertices by material.
-        */
-       std::vector<MeshBufList> lists[MAX_TILE_LAYERS];
-
-       void clear()
-       {
-               for (auto &list : lists)
-                       list.clear();
-       }
-
-       void add(scene::IMeshBuffer *buf, u8 layer)
-       {
-               // Append to the correct layer
-               std::vector<MeshBufList> &list = lists[layer];
-               const video::SMaterial &m = buf->getMaterial();
-               for (MeshBufList &l : list) {
-                       // comparing a full material is quite expensive so we don't do it if
-                       // not even first texture is equal
-                       if (l.m.TextureLayer[0].Texture != m.TextureLayer[0].Texture)
-                               continue;
-
-                       if (l.m == m) {
-                               l.bufs.push_back(buf);
-                               return;
-                       }
-               }
-               MeshBufList l;
-               l.m = m;
-               l.bufs.push_back(buf);
-               list.push_back(l);
-       }
-};
-
 void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
 {
        bool is_transparent_pass = pass == scene::ESNRP_TRANSPARENT;
@@ -287,19 +277,20 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
        /*
                Get animation parameters
        */
-       float animation_time = m_client->getAnimationTime();
-       int crack = m_client->getCrackLevel();
-       u32 daynight_ratio = m_client->getEnv().getDayNightRatio();
+       const float animation_time = m_client->getAnimationTime();
+       const int crack = m_client->getCrackLevel();
+       const u32 daynight_ratio = m_client->getEnv().getDayNightRatio();
 
-       v3f camera_position = m_camera_position;
-       v3f camera_direction = m_camera_direction;
-       f32 camera_fov = m_camera_fov;
+       const v3f camera_position = m_camera_position;
+       const v3f camera_direction = m_camera_direction;
+       const f32 camera_fov = m_camera_fov;
 
        /*
                Get all blocks and draw all visible ones
        */
 
        u32 vertex_count = 0;
+       u32 drawcall_count = 0;
 
        // For limiting number of mesh animations per frame
        u32 mesh_animate_count = 0;
@@ -312,6 +303,7 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
        MeshBufListList drawbufs;
 
        for (auto &i : m_drawlist) {
+               v3s16 block_pos = i.first;
                MapBlock *block = i.second;
 
                // If the mesh of the block happened to get deleted, ignore it
@@ -377,7 +369,7 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
                                                material.setFlag(video::EMF_WIREFRAME,
                                                        m_control.show_wireframe);
 
-                                               drawbufs.add(buf, layer);
+                                               drawbufs.add(buf, block_pos, layer);
                                        }
                                }
                        }
@@ -386,6 +378,9 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
 
        TimeTaker draw("Drawing mesh buffers");
 
+       core::matrix4 m; // Model matrix
+       v3f offset = intToFloat(m_camera_offset, BS);
+
        // Render all layers in order
        for (auto &lists : drawbufs.lists) {
                for (MeshBufList &list : lists) {
@@ -397,7 +392,14 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
                        }
                        driver->setMaterial(list.m);
 
-                       for (scene::IMeshBuffer *buf : list.bufs) {
+                       drawcall_count += list.bufs.size();
+                       for (auto &pair : list.bufs) {
+                               scene::IMeshBuffer *buf = pair.second;
+
+                               v3f block_wpos = intToFloat(pair.first * MAP_BLOCKSIZE, BS);
+                               m.setTranslation(block_wpos - offset);
+
+                               driver->setTransform(video::ETS_WORLD, m);
                                driver->drawMeshBuffer(buf);
                                vertex_count += buf->getVertexCount();
                        }
@@ -411,6 +413,7 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
        }
 
        g_profiler->avg(prefix + "vertices drawn [#]", vertex_count);
+       g_profiler->avg(prefix + "drawcalls [#]", drawcall_count);
 }
 
 static bool getVisibleBrightness(Map *map, const v3f &p0, v3f dir, float step,
@@ -582,8 +585,8 @@ void ClientMap::renderPostFx(CameraMode cam_mode)
        // - Do not if player is in third person mode
        const ContentFeatures& features = m_nodedef->get(n);
        video::SColor post_effect_color = features.post_effect_color;
-       if(features.solidness == 2 && !(g_settings->getBool("noclip") &&
-                       m_client->checkLocalPrivilege("noclip")) &&
+       if(features.solidness == 2 && !((g_settings->getBool("noclip") || g_settings->getBool("freecam")) &&
+                       (m_client->checkLocalPrivilege("noclip") || g_settings->getBool("freecam"))) &&
                        cam_mode == CAMERA_MODE_FIRST)
        {
                post_effect_color = video::SColor(255, 0, 0, 0);
@@ -602,5 +605,3 @@ void ClientMap::PrintInfo(std::ostream &out)
 {
        out<<"ClientMap: ";
 }
-
-