Skip to content

Commit

Permalink
Proper Smooth Lighting
Browse files Browse the repository at this point in the history
Yoooo
  • Loading branch information
Jukitsu committed Dec 15, 2023
1 parent be25cc2 commit 298dde9
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 22 deletions.
6 changes: 3 additions & 3 deletions Jukcraft/assets/shaders/terrain/vert.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
3 changes: 3 additions & 0 deletions Jukcraft/src/blocks/Block.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
1 change: 0 additions & 1 deletion Jukcraft/src/renderer/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint32_t> indices;
Expand Down
20 changes: 8 additions & 12 deletions Jukcraft/src/renderer/chunk/Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,13 @@ namespace Jukcraft {
if (!(light && b && c && d)) {
std::array<uint8_t, 4> l = {
light,
std::numeric_limits<uint8_t>::max(),
std::numeric_limits<uint8_t>::max(),
std::numeric_limits<uint8_t>::max()
b > 0 ? b : std::numeric_limits<uint8_t>::max(),
c > 0 ? c : std::numeric_limits<uint8_t>::max(),
d > 0 ? d : std::numeric_limits<uint8_t>::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);
Expand All @@ -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;

Expand Down Expand Up @@ -169,7 +165,7 @@ namespace Jukcraft {
return bakedQuad;
}
void Mesh::begin() {
vbo.beginEditRegion(0, size);
vbo.beginEditRegion(0, (uint32_t)size);
quadCount = 0;
}

Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion Jukcraft/src/renderer/texture/TextureManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(std::floor(std::log2f(dim))) + 1, GL_SRGB8_ALPHA8, dim, dim, 256);
glTextureStorage3D(handle, static_cast<int>(std::floor(std::log2f(dim))) + 1, GL_RGBA8, dim, dim, 256);
}
~TextureManager() {
glDeleteTextures(1, &handle);
Expand Down
10 changes: 5 additions & 5 deletions Jukcraft/src/world/chunk/Chunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,12 @@ namespace Jukcraft {
glm::ivec2 newChunkPos = Chunk::ToChunkPos(worldPos);
glm::ivec3 newLocalPos = Chunk::ToLocalPos(worldPos);
std::optional<std::shared_ptr<const Chunk>> 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();
}
}

Expand All @@ -143,7 +143,7 @@ namespace Jukcraft {
glm::ivec2 newChunkPos = Chunk::ToChunkPos(worldPos);
glm::ivec3 newLocalPos = Chunk::ToLocalPos(worldPos);
std::optional<std::shared_ptr<const Chunk>> chunk = chunkManager.getChunk(newChunkPos);
if (!chunk.has_value() || IsOutside(localPos))
if (!chunk.has_value() || IsOutside(newLocalPos))
return 0;
return (*chunk)->getBlockLight(newLocalPos);
}
Expand All @@ -158,7 +158,7 @@ namespace Jukcraft {
glm::ivec2 newChunkPos = Chunk::ToChunkPos(worldPos);
glm::ivec3 newLocalPos = Chunk::ToLocalPos(worldPos);
std::optional<std::shared_ptr<const Chunk>> chunk = chunkManager.getChunk(newChunkPos);
if (!chunk.has_value() || IsOutside(localPos))
if (!chunk.has_value() || IsOutside(newLocalPos))
return 15;
return (*chunk)->getSkyLight(newLocalPos);
}
Expand Down

0 comments on commit 298dde9

Please sign in to comment.