From: Daniel Karbach Date: Fri, 30 Oct 2015 10:37:03 +0000 (+0100) Subject: light entities according to block light level X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=b4e5dad2ed5c7e77573de413f3bf5be88577856d;p=blank.git light entities according to block light level it's not that nice, but a start --- diff --git a/doc/todo b/doc/todo index c6a3808..ed9fdce 100644 --- a/doc/todo +++ b/doc/todo @@ -45,17 +45,12 @@ entity ai pathfinding, obstacle avoidance, better turning behaviour -(block) lighting +lighting occlusion/neighbor light mixing is implemented, but still linear this could be solved by using a pre-interpolated light texture and mapping light levels to coordinates on that - also: how could block light affect entity lighting? - maybe get the interpolated light level at the entity's center and use - that as the light power for the directional lighting shader and use a - direction that's fixed relative to the camera? - there's a bug where a chunk's model is not updated if its neighbor changes border light levels I kinda mitigated it a little for direct neighbors during linking, but @@ -65,6 +60,10 @@ entity ai having the light propagate into solid blocks, but feels like this could cause some weird behaviours + entity lighting is now derived from block light levels + it's not interpolated and the calculation is very basic, so it + has some unexpected effects + gravity maybe like light levels? should also store a direction with it in diff --git a/src/graphics/DirectionalLighting.hpp b/src/graphics/DirectionalLighting.hpp index 70492ef..4255401 100644 --- a/src/graphics/DirectionalLighting.hpp +++ b/src/graphics/DirectionalLighting.hpp @@ -20,6 +20,7 @@ public: void SetLightDirection(const glm::vec3 &) noexcept; void SetLightColor(const glm::vec3 &) noexcept; + void SetAmbientColor(const glm::vec3 &) noexcept; void SetTexture(ArrayTexture &) noexcept; void SetFogDensity(float) noexcept; @@ -47,6 +48,7 @@ private: GLuint sampler_handle; GLuint light_direction_handle; GLuint light_color_handle; + GLuint ambient_color_handle; GLuint fog_density_handle; }; diff --git a/src/graphics/shader.cpp b/src/graphics/shader.cpp index 0c161f1..85f0184 100644 --- a/src/graphics/shader.cpp +++ b/src/graphics/shader.cpp @@ -180,6 +180,7 @@ DirectionalLighting::DirectionalLighting() , mvp_handle(0) , light_direction_handle(0) , light_color_handle(0) +, ambient_color_handle(0) , fog_density_handle(0) { program.LoadShader( GL_VERTEX_SHADER, @@ -217,6 +218,7 @@ DirectionalLighting::DirectionalLighting() "uniform sampler2DArray tex_sampler;\n" "uniform vec3 light_direction;\n" "uniform vec3 light_color;\n" + "uniform vec3 ambient_color;\n" "uniform float fog_density;\n" "out vec3 color;\n" "vec3 rgb2hsl(vec3 c) {\n" @@ -239,7 +241,7 @@ DirectionalLighting::DirectionalLighting() "hsl_color.y *= frag_hsl_mod.y;\n" "hsl_color.z *= frag_hsl_mod.z;\n" "vec3 base_color = hsl2rgb(hsl_color) * frag_rgb_mod;\n" - "vec3 ambient = vec3(0.1, 0.1, 0.1) * base_color;\n" + "vec3 ambient = ambient_color * base_color;\n" // this should be the same as the clear color, otherwise looks really weird "vec3 fog_color = vec3(0, 0, 0);\n" "float e = 2.718281828;\n" @@ -263,11 +265,13 @@ DirectionalLighting::DirectionalLighting() sampler_handle = program.UniformLocation("tex_sampler"); light_direction_handle = program.UniformLocation("light_direction"); light_color_handle = program.UniformLocation("light_color"); + ambient_color_handle = program.UniformLocation("ambient_color"); fog_density_handle = program.UniformLocation("fog_density"); Activate(); program.Uniform(light_direction_handle, glm::vec3(1.0f, 3.0f, 2.0f)); program.Uniform(light_color_handle, glm::vec3(1.0f)); + program.Uniform(ambient_color_handle, glm::vec3(0.1f)); program.Uniform(fog_density_handle, 0.0f); } @@ -290,6 +294,10 @@ void DirectionalLighting::SetLightColor(const glm::vec3 &col) noexcept { program.Uniform(light_color_handle, col); } +void DirectionalLighting::SetAmbientColor(const glm::vec3 &col) noexcept { + program.Uniform(ambient_color_handle, col); +} + void DirectionalLighting::SetTexture(ArrayTexture &tex) noexcept { glActiveTexture(GL_TEXTURE0); tex.Bind(); diff --git a/src/ui/ui.cpp b/src/ui/ui.cpp index 8768095..8563f7d 100644 --- a/src/ui/ui.cpp +++ b/src/ui/ui.cpp @@ -433,6 +433,8 @@ void HUD::Render(Viewport &viewport) noexcept { if (block_visible) { DirectionalLighting &world_prog = viewport.HUDProgram(); world_prog.SetLightDirection({ 1.0f, 3.0f, 5.0f }); + world_prog.SetLightColor({ 1.0f, 1.0f, 1.0f }); + world_prog.SetAmbientColor({ 0.1f, 0.1f, 0.1f }); // disable distance fog world_prog.SetFogDensity(0.0f); diff --git a/src/world/World.hpp b/src/world/World.hpp index 7ee5f43..273bfa6 100644 --- a/src/world/World.hpp +++ b/src/world/World.hpp @@ -130,6 +130,14 @@ private: const EntityState & ); + /// calculate light direction and intensity at entity's location + void GetLight( + const Entity &entity, + glm::vec3 &direction, + glm::vec3 &color, + glm::vec3 &ambient + ); + private: Config config; diff --git a/src/world/world.cpp b/src/world/world.cpp index 4ac765c..46afc1b 100644 --- a/src/world/world.cpp +++ b/src/world/world.cpp @@ -656,17 +656,63 @@ World::EntityHandle World::RemoveEntity(EntityHandle &eh) { void World::Render(Viewport &viewport) { DirectionalLighting &entity_prog = viewport.EntityProgram(); - entity_prog.SetLightDirection(light_direction); entity_prog.SetFogDensity(fog_density); + glm::vec3 light_dir; + glm::vec3 light_col; + glm::vec3 ambient_col; for (Entity &entity : entities) { glm::mat4 M(entity.Transform(players.front().GetEntity().ChunkCoords())); if (!CullTest(entity.Bounds(), entity_prog.GetVP() * M)) { + GetLight(entity, light_dir, light_col, ambient_col); + entity_prog.SetLightDirection(light_dir); + entity_prog.SetLightColor(light_col); + entity_prog.SetAmbientColor(ambient_col); entity.Render(M, entity_prog); } } } +// this should interpolate based on the fractional part of entity's block position +void World::GetLight( + const Entity &e, + glm::vec3 &dir, + glm::vec3 &col, + glm::vec3 &amb +) { + Chunk *chunk = chunks.Get(e.ChunkCoords()); + if (!chunk) { + // chunk unavailable, so make it really dark and from + // some arbitrary direction + dir = glm::vec3(1.0f, 2.0f, 3.0f); + col = glm::vec3(0.025f); // ~0.8^15 + return; + } + glm::ivec3 base(e.Position()); + int base_light = chunk->GetLight(base); + int max_light = 0; + int min_light = 15; + glm::ivec3 acc(0, 0, 0); + for (glm::ivec3 offset(-1, -1, -1); offset.z < 2; ++offset.z) { + for (offset.y = -1; offset.y < 2; ++offset.y) { + for (offset.x = -1; offset.x < 2; ++offset.x) { + BlockLookup block(chunk, base + offset); + if (!block) { + // missing, just ignore it + continue; + } + // otherwise, accumulate the difference times direction + acc += offset * (base_light - block.GetLight()); + max_light = std::max(max_light, block.GetLight()); + min_light = std::min(min_light, block.GetLight()); + } + } + } + dir = acc; + col = glm::vec3(std::pow(0.8f, 15 - max_light)); + amb = glm::vec3(std::pow(0.8f, 15 - min_light)); +} + namespace { PrimitiveMesh::Buffer debug_buf;