From 298dde9f6036c05168a76d87f695b1f521871b51 Mon Sep 17 00:00:00 2001 From: Jukitsu <84381972+Jukitsu@users.noreply.github.com> Date: Sat, 16 Dec 2023 00:54:59 +0100 Subject: [PATCH] Proper Smooth Lighting Yoooo --- Jukcraft/assets/shaders/terrain/vert.glsl | 6 +++--- Jukcraft/src/blocks/Block.h | 3 +++ Jukcraft/src/renderer/Renderer.h | 1 - Jukcraft/src/renderer/chunk/Mesh.cpp | 20 ++++++++----------- .../src/renderer/texture/TextureManager.h | 2 +- Jukcraft/src/world/chunk/Chunk.cpp | 10 +++++----- 6 files changed, 20 insertions(+), 22 deletions(-) diff --git a/Jukcraft/assets/shaders/terrain/vert.glsl b/Jukcraft/assets/shaders/terrain/vert.glsl index f10bdac..2d3e73a 100644 --- a/Jukcraft/assets/shaders/terrain/vert.glsl +++ b/Jukcraft/assets/shaders/terrain/vert.glsl @@ -27,8 +27,8 @@ layout(location = 1) uniform float u_Daylight; void main(void) { vec3 pos = u_ChunkPos + vec3(a_VertexData >> 17 & 0x1F, a_VertexData >> 22, a_VertexData >> 12 & 0x1F); - float ao = float(a_SmoothLightData & 0x7) / 4.0f; - float blockLight = float(a_SmoothLightData >> 3 & 0x3F) / 4.0f; + float ao = float(1 + (a_SmoothLightData & 0x7)) / 5.0f; + float blockLight = float((a_SmoothLightData >> 3) & 0x3F) / 4.0f; float skyLight = float(a_SmoothLightData >> 9) / 4.0f; float blocklightMultiplier = pow(0.8, 15.0 - blockLight); @@ -45,5 +45,5 @@ void main(void) { clamp(blocklightMultiplier * (1.0 + 0.25 * (1.0 - u_Daylight)), intermediateSkylightMultiplier, 1.0), clamp(skylightMultiplier * u_Daylight, blocklightMultiplier, 1.0) ); - vs_Out.v_Shading = pow(shading * ao, 2.2f); + vs_Out.v_Shading = pow(shading, 2.2f) * ao; } \ No newline at end of file diff --git a/Jukcraft/src/blocks/Block.h b/Jukcraft/src/blocks/Block.h index 6dd9c62..dfbf56b 100644 --- a/Jukcraft/src/blocks/Block.h +++ b/Jukcraft/src/blocks/Block.h @@ -23,6 +23,9 @@ namespace Jukcraft { constexpr bool isTransparent() const { return transparent; } + constexpr bool getOpacity() const { + return !transparent; + } constexpr uint8_t getLight() const { return light; } diff --git a/Jukcraft/src/renderer/Renderer.h b/Jukcraft/src/renderer/Renderer.h index e3a8996..a1b7519 100644 --- a/Jukcraft/src/renderer/Renderer.h +++ b/Jukcraft/src/renderer/Renderer.h @@ -18,7 +18,6 @@ namespace Jukcraft { static void Init() { glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); - glEnable(GL_FRAMEBUFFER_SRGB); glEnable(GL_MULTISAMPLE); std::vector indices; diff --git a/Jukcraft/src/renderer/chunk/Mesh.cpp b/Jukcraft/src/renderer/chunk/Mesh.cpp index b7ce5f1..a7d19fb 100644 --- a/Jukcraft/src/renderer/chunk/Mesh.cpp +++ b/Jukcraft/src/renderer/chunk/Mesh.cpp @@ -8,17 +8,13 @@ namespace Jukcraft { if (!(light && b && c && d)) { std::array l = { light, - std::numeric_limits::max(), - std::numeric_limits::max(), - std::numeric_limits::max() + b > 0 ? b : std::numeric_limits::max(), + c > 0 ? c : std::numeric_limits::max(), + d > 0 ? d : std::numeric_limits::max() }; - if (b > 0) - l[1] = b; - if (c > 0) - l[2] = c; - if (d > 0) - l[3] = d; + uint8_t min_val = *(std::min_element(l.begin(), l.end())); + light = std::max(light, min_val); b = std::max(b, min_val); c = std::max(c, min_val); @@ -31,7 +27,7 @@ namespace Jukcraft { } - static inline uint8_t ao(uint8_t s1, uint8_t s2, uint8_t c) { + static inline uint8_t ao(bool s1, bool s2, bool c) { if (s1 && s2) return 1; @@ -169,7 +165,7 @@ namespace Jukcraft { return bakedQuad; } void Mesh::begin() { - vbo.beginEditRegion(0, size); + vbo.beginEditRegion(0, (uint32_t)size); quadCount = 0; } @@ -187,7 +183,7 @@ namespace Jukcraft { void Mesh::end() { vbo.endEditRegion(); DrawIndirectCommand cmd; - cmd.count = quadCount * 6; + cmd.count = (uint32_t)quadCount * 6; cmd.instanceCount = 1; cmd.firstIndex = 0; cmd.baseVertex = 0; diff --git a/Jukcraft/src/renderer/texture/TextureManager.h b/Jukcraft/src/renderer/texture/TextureManager.h index 16494d4..967f3c8 100644 --- a/Jukcraft/src/renderer/texture/TextureManager.h +++ b/Jukcraft/src/renderer/texture/TextureManager.h @@ -6,7 +6,7 @@ namespace Jukcraft { public: TextureManager(uint16_t dim) :dim(dim), index(0) { glCreateTextures(GL_TEXTURE_2D_ARRAY, 1, &handle); - glTextureStorage3D(handle, static_cast(std::floor(std::log2f(dim))) + 1, GL_SRGB8_ALPHA8, dim, dim, 256); + glTextureStorage3D(handle, static_cast(std::floor(std::log2f(dim))) + 1, GL_RGBA8, dim, dim, 256); } ~TextureManager() { glDeleteTextures(1, &handle); diff --git a/Jukcraft/src/world/chunk/Chunk.cpp b/Jukcraft/src/world/chunk/Chunk.cpp index b4f0a3f..6d5c954 100644 --- a/Jukcraft/src/world/chunk/Chunk.cpp +++ b/Jukcraft/src/world/chunk/Chunk.cpp @@ -128,12 +128,12 @@ namespace Jukcraft { glm::ivec2 newChunkPos = Chunk::ToChunkPos(worldPos); glm::ivec3 newLocalPos = Chunk::ToLocalPos(worldPos); std::optional> chunk = chunkManager.getChunk(newChunkPos); - if (!chunk.has_value() || worldPos.y < 0 || worldPos.y >= CHUNK_HEIGHT) + if (!chunk.has_value() || IsOutside(newLocalPos)) return 0; - return (*chunk)->getBlock(newLocalPos); + return blockTypes[(*chunk)->getBlock(newLocalPos)].getOpacity(); } else { - return getBlock(localPos); + return blockTypes[getBlock(localPos)].getOpacity(); } } @@ -143,7 +143,7 @@ namespace Jukcraft { glm::ivec2 newChunkPos = Chunk::ToChunkPos(worldPos); glm::ivec3 newLocalPos = Chunk::ToLocalPos(worldPos); std::optional> chunk = chunkManager.getChunk(newChunkPos); - if (!chunk.has_value() || IsOutside(localPos)) + if (!chunk.has_value() || IsOutside(newLocalPos)) return 0; return (*chunk)->getBlockLight(newLocalPos); } @@ -158,7 +158,7 @@ namespace Jukcraft { glm::ivec2 newChunkPos = Chunk::ToChunkPos(worldPos); glm::ivec3 newLocalPos = Chunk::ToLocalPos(worldPos); std::optional> chunk = chunkManager.getChunk(newChunkPos); - if (!chunk.has_value() || IsOutside(localPos)) + if (!chunk.has_value() || IsOutside(newLocalPos)) return 15; return (*chunk)->getSkyLight(newLocalPos); }