]> git.lizzy.rs Git - minetest.git/blobdiff - src/mapblock_mesh.cpp
Do not allow the m_transforming_liquid queue to increase until all RAM is consumed
[minetest.git] / src / mapblock_mesh.cpp
index a7fafa68341fd8b549ddff135eef8177f82fb240..29b0e92f5855a117f3a113d3536bebd53aed5d48 100644 (file)
@@ -48,6 +48,8 @@ MeshMakeData::MeshMakeData(IGameDef *gamedef):
        m_crack_pos_relative(-1337, -1337, -1337),
        m_highlighted_pos_relative(-1337, -1337, -1337),
        m_smooth_lighting(false),
+       m_show_hud(false),
+       m_highlight_mesh_color(255, 255, 255, 255),
        m_gamedef(gamedef)
 {}
 
@@ -63,8 +65,9 @@ void MeshMakeData::fill(MapBlock *block)
 
        // Allocate this block + neighbors
        m_vmanip.clear();
-       m_vmanip.addArea(VoxelArea(blockpos_nodes-v3s16(1,1,1)*MAP_BLOCKSIZE,
-                       blockpos_nodes+v3s16(1,1,1)*MAP_BLOCKSIZE*2-v3s16(1,1,1)));
+       VoxelArea voxel_area(blockpos_nodes - v3s16(1,1,1) * MAP_BLOCKSIZE,
+                       blockpos_nodes + v3s16(1,1,1) * MAP_BLOCKSIZE*2-v3s16(1,1,1));
+       m_vmanip.addArea(voxel_area);
 
        {
                //TimeTaker timer("copy central block data");
@@ -219,11 +222,11 @@ u16 getFaceLight(MapNode n, MapNode n2, v3s16 face_dir, INodeDefManager *ndef)
 
 /*
        Calculate smooth lighting at the XYZ- corner of p.
-       Single light bank.
+       Both light banks
 */
-static u8 getSmoothLight(enum LightBank bank, v3s16 p, MeshMakeData *data)
+static u16 getSmoothLightCombined(v3s16 p, MeshMakeData *data)
 {
-       static v3s16 dirs8[8] = {
+       static const v3s16 dirs8[8] = {
                v3s16(0,0,0),
                v3s16(0,0,1),
                v3s16(0,1,0),
@@ -237,10 +240,12 @@ static u8 getSmoothLight(enum LightBank bank, v3s16 p, MeshMakeData *data)
        INodeDefManager *ndef = data->m_gamedef->ndef();
 
        u16 ambient_occlusion = 0;
-       u16 light = 0;
        u16 light_count = 0;
        u8 light_source_max = 0;
-       for(u32 i=0; i<8; i++)
+       u16 light_day = 0;
+       u16 light_night = 0;
+
+       for (u32 i = 0; i < 8; i++)
        {
                MapNode n = data->m_vmanip.getNodeNoEx(p - dirs8[i]);
 
@@ -250,52 +255,53 @@ static u8 getSmoothLight(enum LightBank bank, v3s16 p, MeshMakeData *data)
                }
 
                const ContentFeatures &f = ndef->get(n);
-               if(f.light_source > light_source_max)
+               if (f.light_source > light_source_max)
                        light_source_max = f.light_source;
-               // Check f.solidness because fast-style leaves look
-               // better this way
-               if(f.param_type == CPT_LIGHT && f.solidness != 2)
-               {
-                       light += decode_light(n.getLight(bank, ndef));
+               // Check f.solidness because fast-style leaves look better this way
+               if (f.param_type == CPT_LIGHT && f.solidness != 2) {
+                       light_day += decode_light(n.getLightNoChecks(LIGHTBANK_DAY, &f));
+                       light_night += decode_light(n.getLightNoChecks(LIGHTBANK_NIGHT, &f));
                        light_count++;
-               }
-               else {
+               } else {
                        ambient_occlusion++;
                }
        }
 
        if(light_count == 0)
-               return 255;
+               return 0xffff;
 
-       light /= light_count;
+       light_day /= light_count;
+       light_night /= light_count;
 
        // Boost brightness around light sources
-       if(decode_light(light_source_max) >= light)
-               //return decode_light(undiminish_light(light_source_max));
-               return decode_light(light_source_max);
+       bool skip_ambient_occlusion_day = false;
+       if(decode_light(light_source_max) >= light_day) {
+               light_day = decode_light(light_source_max);
+               skip_ambient_occlusion_day = true;
+       }
 
-       if(ambient_occlusion > 4)
+       bool skip_ambient_occlusion_night = false;
+       if(decode_light(light_source_max) >= light_night) {
+               light_night = decode_light(light_source_max);
+               skip_ambient_occlusion_night = true;
+       }
+
+       if (ambient_occlusion > 4)
        {
-               //calculate table index for gamma space multiplier
-               ambient_occlusion -= 5;
                //table of precalculated gamma space multiply factors
                //light^2.2 * factor (0.75, 0.5, 0.25, 0.0), so table holds factor ^ (1 / 2.2)
-               const float light_amount[4] = {0.877424315, 0.729740053, 0.532520545, 0.0};
-               light = core::clamp(core::round32(light*light_amount[ambient_occlusion]), 0, 255);
-       }
+               static const float light_amount[4] = { 0.877424315, 0.729740053, 0.532520545, 0.0 };
 
-       return light;
-}
+               //calculate table index for gamma space multiplier
+               ambient_occlusion -= 5;
 
-/*
-       Calculate smooth lighting at the XYZ- corner of p.
-       Both light banks.
-*/
-static u16 getSmoothLight(v3s16 p, MeshMakeData *data)
-{
-       u16 day = getSmoothLight(LIGHTBANK_DAY, p, data);
-       u16 night = getSmoothLight(LIGHTBANK_NIGHT, p, data);
-       return day | (night << 8);
+               if (!skip_ambient_occlusion_day)
+                       light_day = rangelim(core::round32(light_day*light_amount[ambient_occlusion]), 0, 255);
+               if (!skip_ambient_occlusion_night)
+                       light_night = rangelim(core::round32(light_night*light_amount[ambient_occlusion]), 0, 255);
+       }
+
+       return light_day | (light_night << 8);
 }
 
 /*
@@ -305,20 +311,20 @@ static u16 getSmoothLight(v3s16 p, MeshMakeData *data)
 u16 getSmoothLight(v3s16 p, v3s16 corner, MeshMakeData *data)
 {
        if(corner.X == 1) p.X += 1;
-       else              assert(corner.X == -1);
+       // else corner.X == -1
        if(corner.Y == 1) p.Y += 1;
-       else              assert(corner.Y == -1);
+       // else corner.Y == -1
        if(corner.Z == 1) p.Z += 1;
-       else              assert(corner.Z == -1);
+       // else corner.Z == -1
 
-       return getSmoothLight(p, data);
+       return getSmoothLightCombined(p, data);
 }
 
 /*
        Converts from day + night color values (0..255)
        and a given daynight_ratio to the final SColor shown on screen.
 */
-static void finalColorBlend(video::SColor& result,
+void finalColorBlend(video::SColor& result,
                u8 day, u8 night, u32 daynight_ratio)
 {
        s32 rg = (day * daynight_ratio + night * (1000-daynight_ratio)) / 1000;
@@ -330,7 +336,7 @@ static void finalColorBlend(video::SColor& result,
 
        // Emphase blue a bit in darker places
        // Each entry of this array represents a range of 8 blue levels
-       static u8 emphase_blue_when_dark[32] = {
+       static const u8 emphase_blue_when_dark[32] = {
                1, 4, 6, 6, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        };
@@ -338,7 +344,7 @@ static void finalColorBlend(video::SColor& result,
        b = irr::core::clamp (b, 0, 255);
 
        // Artificial light is yellow-ish
-       static u8 emphase_yellow_when_artificial[16] = {
+       static const u8 emphase_yellow_when_artificial[16] = {
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 10, 15, 15, 15
        };
        rg += emphase_yellow_when_artificial[night/16];
@@ -1086,7 +1092,7 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
 
        mapblock_mesh_generate_special(data, collector);
 
-       m_highlight_mesh_color = data->m_highlight_mesh_color; 
+       m_highlight_mesh_color = data->m_highlight_mesh_color;
 
        /*
                Convert MeshCollector to SMesh
@@ -1156,12 +1162,16 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
                                        applyFacesShading (vc, 0.836660);
                                }
                        }
-                       // - Classic lighting
-                       // Set initial real color and store for later updates
-                       u8 day = vc.getRed();
-                       u8 night = vc.getGreen();
-                       finalColorBlend(vc, day, night, 1000);
-                       m_daynight_diffs[i][j] = std::make_pair(day, night);
+                       if(!m_enable_shaders)
+                       {
+                               // - Classic lighting (shaders handle this by themselves)
+                               // Set initial real color and store for later updates
+                               u8 day = vc.getRed();
+                               u8 night = vc.getGreen();
+                               finalColorBlend(vc, day, night, 1000);
+                               if(day != night)
+                                       m_daynight_diffs[i][j] = std::make_pair(day, night);
+                       }
                }
 
                // Create material
@@ -1325,7 +1335,7 @@ bool MapBlockMesh::animate(bool faraway, float time, int crack, u32 daynight_rat
        }
 
        // Day-night transition
-       if(daynight_ratio != m_last_daynight_ratio)
+       if(!m_enable_shaders && (daynight_ratio != m_last_daynight_ratio))
        {
                for(std::map<u32, std::map<u32, std::pair<u8, u8> > >::iterator
                                i = m_daynight_diffs.begin();