LocalPlayer::accelerateHorizontal: cleanups
[oweals/minetest.git] / src / mapblock_mesh.cpp
index 0bba644e6c5568846b435ba556d8ea570f5ae6d4..67195cab3696469a35972f9a085af0706979b4dc 100644 (file)
@@ -18,19 +18,14 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 */
 
 #include "mapblock_mesh.h"
-#include "light.h"
 #include "mapblock.h"
 #include "map.h"
 #include "profiler.h"
-#include "nodedef.h"
 #include "mesh.h"
 #include "minimap.h"
 #include "content_mapblock.h"
-#include "noise.h"
-#include "shader.h"
-#include "settings.h"
 #include "util/directiontables.h"
-#include <IMeshManipulator.h>
+#include "client/renderingengine.h"
 
 /*
        MeshMakeData
@@ -38,11 +33,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 
 MeshMakeData::MeshMakeData(Client *client, bool use_shaders,
                bool use_tangent_vertices):
-       m_vmanip(),
-       m_blockpos(-1337,-1337,-1337),
-       m_crack_pos_relative(-1337, -1337, -1337),
-       m_smooth_lighting(false),
-       m_show_hud(false),
        m_client(client),
        m_use_shaders(use_shaders),
        m_use_tangent_vertices(use_tangent_vertices)
@@ -207,7 +197,7 @@ u16 getFaceLight(MapNode n, MapNode n2, v3s16 face_dir, INodeDefManager *ndef)
        Calculate smooth lighting at the XYZ- corner of p.
        Both light banks
 */
-static u16 getSmoothLightCombined(v3s16 p, MeshMakeData *data)
+static u16 getSmoothLightCombined(const v3s16 &p, MeshMakeData *data)
 {
        static const v3s16 dirs8[8] = {
                v3s16(0,0,0),
@@ -271,7 +261,7 @@ static u16 getSmoothLightCombined(v3s16 p, MeshMakeData *data)
 
        if (ambient_occlusion > 4)
        {
-               static const float ao_gamma = rangelim(
+               static thread_local const float ao_gamma = rangelim(
                        g_settings->getFloat("ambient_occlusion_gamma"), 0.25, 4.0);
 
                // Table of gamma space multiply factors.
@@ -433,7 +423,7 @@ struct FastFace
        u8 layernum;
 };
 
-static void makeFastFace(TileSpec tile, u16 li0, u16 li1, u16 li2, u16 li3,
+static void makeFastFace(const TileSpec &tile, u16 li0, u16 li1, u16 li2, u16 li3,
        v3f p, v3s16 dir, v3f scale, std::vector<FastFace> &dest)
 {
        // Position is at the center of the cube.
@@ -603,11 +593,12 @@ static void makeFastFace(TileSpec tile, u16 li0, u16 li1, u16 li2, u16 li3,
                core::vector2d<f32>(x0 + w * abs_scale, y0) };
 
        for (int layernum = 0; layernum < MAX_TILE_LAYERS; layernum++) {
-               TileLayer *layer = &tile.layers[layernum];
+               const TileLayer *layer = &tile.layers[layernum];
                if (layer->texture_id == 0)
                        continue;
 
-               dest.push_back(FastFace());
+               // equivalent to dest.push_back(FastFace()) but faster
+               dest.emplace_back();
                FastFace& face = *dest.rbegin();
 
                for (u8 i = 0; i < 4; i++) {
@@ -689,11 +680,11 @@ static u8 face_contents(content_t m1, content_t m2, bool *equivalent,
 /*
        Gets nth node tile (0 <= n <= 5).
 */
-TileSpec getNodeTileN(MapNode mn, v3s16 p, u8 tileindex, MeshMakeData *data)
+void getNodeTileN(MapNode mn, v3s16 p, u8 tileindex, MeshMakeData *data, TileSpec &tile)
 {
        INodeDefManager *ndef = data->m_client->ndef();
        const ContentFeatures &f = ndef->get(mn);
-       TileSpec tile = f.tiles[tileindex];
+       tile = f.tiles[tileindex];
        TileLayer *top_layer = NULL;
        for (int layernum = 0; layernum < MAX_TILE_LAYERS; layernum++) {
                TileLayer *layer = &tile.layers[layernum];
@@ -706,13 +697,12 @@ TileSpec getNodeTileN(MapNode mn, v3s16 p, u8 tileindex, MeshMakeData *data)
        // Apply temporary crack
        if (p == data->m_crack_pos_relative)
                top_layer->material_flags |= MATERIAL_FLAG_CRACK;
-       return tile;
 }
 
 /*
        Gets node tile given a face direction.
 */
-TileSpec getNodeTile(MapNode mn, v3s16 p, v3s16 dir, MeshMakeData *data)
+void getNodeTile(MapNode mn, v3s16 p, v3s16 dir, MeshMakeData *data, TileSpec &tile)
 {
        INodeDefManager *ndef = data->m_client->ndef();
 
@@ -769,9 +759,8 @@ TileSpec getNodeTile(MapNode mn, v3s16 p, v3s16 dir, MeshMakeData *data)
 
        };
        u16 tile_index=facedir*16 + dir_i;
-       TileSpec tile = getNodeTileN(mn, p, dir_to_tile[tile_index], data);
+       getNodeTileN(mn, p, dir_to_tile[tile_index], data, tile);
        tile.rotation = dir_to_tile[tile_index + 1];
-       return tile;
 }
 
 static void getTileInfo(
@@ -791,7 +780,7 @@ static void getTileInfo(
        INodeDefManager *ndef = data->m_client->ndef();
        v3s16 blockpos_nodes = data->m_blockpos * MAP_BLOCKSIZE;
 
-       MapNode &n0 = vmanip.getNodeRefUnsafe(blockpos_nodes + p);
+       const MapNode &n0 = vmanip.getNodeRefUnsafe(blockpos_nodes + p);
 
        // Don't even try to get n1 if n0 is already CONTENT_IGNORE
        if (n0.getContent() == CONTENT_IGNORE) {
@@ -799,8 +788,7 @@ static void getTileInfo(
                return;
        }
 
-       const MapNode &n1 = vmanip.getNodeRefUnsafeCheckFlags(
-               blockpos_nodes + p + face_dir);
+       const MapNode &n1 = vmanip.getNodeRefUnsafeCheckFlags(blockpos_nodes + p + face_dir);
 
        if (n1.getContent() == CONTENT_IGNORE) {
                makes_face = false;
@@ -812,8 +800,7 @@ static void getTileInfo(
        u8 mf = face_contents(n0.getContent(), n1.getContent(),
                        &equivalent, ndef);
 
-       if(mf == 0)
-       {
+       if (mf == 0) {
                makes_face = false;
                return;
        }
@@ -830,34 +817,31 @@ static void getTileInfo(
                p_corrected = p + face_dir;
                face_dir_corrected = -face_dir;
        }
-       tile = getNodeTile(n, p_corrected, face_dir_corrected, data);
+
+       getNodeTile(n, p_corrected, face_dir_corrected, data, tile);
        const ContentFeatures &f = ndef->get(n);
        tile.emissive_light = f.light_source;
 
        // eg. water and glass
-       if (equivalent)
+       if (equivalent) {
                for (int layernum = 0; layernum < MAX_TILE_LAYERS; layernum++)
                        tile.layers[layernum].material_flags |=
                                MATERIAL_FLAG_BACKFACE_CULLING;
+       }
 
-       if (data->m_smooth_lighting == false)
-       {
+       if (!data->m_smooth_lighting) {
                lights[0] = lights[1] = lights[2] = lights[3] =
                                getFaceLight(n0, n1, face_dir, ndef);
        }
-       else
-       {
+       else {
                v3s16 vertex_dirs[4];
                getNodeVertexDirs(face_dir_corrected, vertex_dirs);
-               for(u16 i=0; i<4; i++)
-               {
-                       lights[i] = getSmoothLight(
-                                       blockpos_nodes + p_corrected,
-                                       vertex_dirs[i], data);
+
+               v3s16 light_p = blockpos_nodes + p_corrected;
+               for (u16 i = 0; i < 4; i++) {
+                       lights[i] = getSmoothLight(light_p, vertex_dirs[i], data);
                }
        }
-
-       return;
 }
 
 /*
@@ -867,11 +851,10 @@ static void getTileInfo(
 */
 static void updateFastFaceRow(
                MeshMakeData *data,
-               v3s16 startpos,
+               const v3s16 &&startpos,
                v3s16 translate_dir,
-               v3f translate_dir_f,
-               v3s16 face_dir,
-               v3f face_dir_f,
+               const v3f &&translate_dir_f,
+               const v3s16 &&face_dir,
                std::vector<FastFace> &dest)
 {
        v3s16 p = startpos;
@@ -914,10 +897,7 @@ static void updateFastFaceRow(
                        if (next_makes_face == makes_face
                                        && next_p_corrected == p_corrected + translate_dir
                                        && next_face_dir_corrected == face_dir_corrected
-                                       && next_lights[0] == lights[0]
-                                       && next_lights[1] == lights[1]
-                                       && next_lights[2] == lights[2]
-                                       && next_lights[3] == lights[3]
+                                       && memcmp(next_lights, lights, ARRLEN(lights) * sizeof(u16)) == 0
                                        && next_tile.isTileable(tile)) {
                                next_is_different = false;
                                continuous_tiles_count++;
@@ -932,7 +912,8 @@ static void updateFastFaceRow(
                                // Floating point conversion of the position vector
                                v3f pf(p_corrected.X, p_corrected.Y, p_corrected.Z);
                                // Center point of face (kind of)
-                               v3f sp = pf - ((f32)continuous_tiles_count / 2.0 - 0.5) * translate_dir_f;
+                               v3f sp = pf -
+                                       ((f32)continuous_tiles_count / 2.0f - 0.5f) * translate_dir_f;
                                v3f scale(1,1,1);
 
                                if(translate_dir.X != 0) {
@@ -980,7 +961,6 @@ static void updateAllFastFaceRows(MeshMakeData *data,
                                        v3s16(1,0,0), //dir
                                        v3f  (1,0,0),
                                        v3s16(0,1,0), //face dir
-                                       v3f  (0,1,0),
                                        dest);
                }
        }
@@ -995,7 +975,6 @@ static void updateAllFastFaceRows(MeshMakeData *data,
                                        v3s16(0,0,1), //dir
                                        v3f  (0,0,1),
                                        v3s16(1,0,0), //face dir
-                                       v3f  (1,0,0),
                                        dest);
                }
        }
@@ -1010,7 +989,6 @@ static void updateAllFastFaceRows(MeshMakeData *data,
                                        v3s16(1,0,0), //dir
                                        v3f  (1,0,0),
                                        v3s16(0,0,1), //face dir
-                                       v3f  (0,0,1),
                                        dest);
                }
        }
@@ -1022,15 +1000,11 @@ static void updateAllFastFaceRows(MeshMakeData *data,
 
 MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
        m_minimap_mapblock(NULL),
-       m_client(data->m_client),
-       m_driver(m_client->tsrc()->getDevice()->getVideoDriver()),
-       m_tsrc(m_client->getTextureSource()),
-       m_shdrsrc(m_client->getShaderSource()),
+       m_tsrc(data->m_client->getTextureSource()),
+       m_shdrsrc(data->m_client->getShaderSource()),
        m_animation_force_timer(0), // force initial animation
        m_last_crack(-1),
-       m_crack_materials(),
-       m_last_daynight_ratio((u32) -1),
-       m_daynight_diffs()
+       m_last_daynight_ratio((u32) -1)
 {
        for (int m = 0; m < MAX_TILE_LAYERS; m++)
                m_mesh[m] = new scene::SMesh();
@@ -1082,7 +1056,7 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
                        const u16 indices[] = {0,1,2,2,3,0};
                        const u16 indices_alternate[] = {0,1,3,2,3,1};
 
-                       if (f.layer.texture == NULL)
+                       if (!f.layer.texture)
                                continue;
 
                        const u16 *indices_p =
@@ -1148,8 +1122,7 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
                                        m_animation_frame_offsets[std::pair<u8, u32>(layer, i)] = 0;
                                }
                                // Replace tile texture with the first animation frame
-                               FrameSpec animation_frame = p.layer.frames[0];
-                               p.layer.texture = animation_frame.texture;
+                               p.layer.texture = (*p.layer.frames)[0].texture;
                        }
 
                        if (!m_enable_shaders) {
@@ -1234,7 +1207,7 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
 
                if (m_use_tangent_vertices) {
                        scene::IMeshManipulator* meshmanip =
-                               m_client->getSceneManager()->getMeshManipulator();
+                               RenderingEngine::get_scene_manager()->getMeshManipulator();
                        meshmanip->recalculateTangents(m_mesh[layer], true, false, false);
                }
 
@@ -1269,7 +1242,7 @@ MapBlockMesh::~MapBlockMesh()
                if (m_enable_vbo && m_mesh[m])
                        for (u32 i = 0; i < m_mesh[m]->getMeshBufferCount(); i++) {
                                scene::IMeshBuffer *buf = m_mesh[m]->getMeshBuffer(i);
-                               m_driver->removeHardwareBuffer(buf);
+                               RenderingEngine::get_video_driver()->removeHardwareBuffer(buf);
                        }
                m_mesh[m]->drop();
                m_mesh[m] = NULL;
@@ -1337,7 +1310,7 @@ bool MapBlockMesh::animate(bool faraway, float time, int crack, u32 daynight_rat
                scene::IMeshBuffer *buf = m_mesh[i->first.first]->
                        getMeshBuffer(i->first.second);
 
-               FrameSpec animation_frame = tile.frames[frame];
+               const FrameSpec &animation_frame = (*tile.frames)[frame];
                buf->getMaterial().setTexture(0, animation_frame.texture);
                if (m_enable_shaders) {
                        if (animation_frame.normal_texture) {
@@ -1544,14 +1517,12 @@ void MeshCollector::applyTileColors()
 {
        if (m_use_tangent_vertices)
                for (int layer = 0; layer < MAX_TILE_LAYERS; layer++) {
-                       std::vector<PreMeshBuffer> *p = &prebuffers[layer];
-                       for (std::vector<PreMeshBuffer>::iterator it = p->begin();
-                                       it != p->end(); ++it) {
-                               video::SColor tc = it->layer.color;
+                       for (auto &pmb : prebuffers[layer]) {
+                               video::SColor tc = pmb.layer.color;
                                if (tc == video::SColor(0xFFFFFFFF))
                                        continue;
-                               for (u32 index = 0; index < it->tangent_vertices.size(); index++) {
-                                       video::SColor *c = &it->tangent_vertices[index].Color;
+                               for (auto &tangent_vertice : pmb.tangent_vertices) {
+                                       video::SColor *c = &tangent_vertice.Color;
                                        c->set(c->getAlpha(), c->getRed() * tc.getRed() / 255,
                                                c->getGreen() * tc.getGreen() / 255,
                                                c->getBlue() * tc.getBlue() / 255);
@@ -1560,14 +1531,12 @@ void MeshCollector::applyTileColors()
                }
        else
                for (int layer = 0; layer < MAX_TILE_LAYERS; layer++) {
-                       std::vector<PreMeshBuffer> *p = &prebuffers[layer];
-                       for (std::vector<PreMeshBuffer>::iterator it = p->begin();
-                                       it != p->end(); ++it) {
-                               video::SColor tc = it->layer.color;
+                       for (auto &pmb : prebuffers[layer]) {
+                               video::SColor tc = pmb.layer.color;
                                if (tc == video::SColor(0xFFFFFFFF))
                                        continue;
-                               for (u32 index = 0; index < it->vertices.size(); index++) {
-                                       video::SColor *c = &it->vertices[index].Color;
+                               for (auto &vertice : pmb.vertices) {
+                                       video::SColor *c = &vertice.Color;
                                        c->set(c->getAlpha(), c->getRed() * tc.getRed() / 255,
                                                c->getGreen() * tc.getGreen() / 255,
                                                c->getBlue() * tc.getBlue() / 255);