Skip to content

Commit

Permalink
WIP SMooth lighting (fix some stuff on the way)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jukitsu committed Nov 27, 2023
1 parent 1ed8102 commit 7d0307e
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 118 deletions.
4 changes: 3 additions & 1 deletion Jukcraft/assets/shaders/terrain/vert.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ void main(void) {
float intermediateSkylightMultiplier = pow(0.8, 15.0 - skyLight * u_Daylight);
float skylightMultiplier = pow(0.8, 15.0 - skyLight);

float shading = float((a_VertexData & 0x3) + 2) / 5.0f;

gl_Position = u_CameraTransforms * vec4(pos, 1.0f);

vs_Out.v_TexCoords = vec3(c_TexCoords[a_VertexData >> 10 & 0x3], a_VertexData >> 2 & 0xFF);
Expand All @@ -43,5 +45,5 @@ void main(void) {
clamp(blocklightMultiplier * 1.25, intermediateSkylightMultiplier, 1.0),
clamp(skylightMultiplier, blocklightMultiplier, 1.0)
);
vs_Out.v_Shading = pow(float((a_VertexData & 0x3) + 2) / 5.0f, 2.2f);
vs_Out.v_Shading = pow(shading * ao, 2.2f);
}
31 changes: 18 additions & 13 deletions Jukcraft/src/renderer/chunk/Mesh.cpp
Original file line number Diff line number Diff line change
@@ -1,34 +1,38 @@
#include "pch.h"
#include "renderer/chunk/Mesh.h"
#include "world/chunk/ChunkManager.h"

namespace Jukcraft {
static constexpr uint8_t smooth(uint8_t light, uint8_t b, uint8_t c, uint8_t d) {
static inline uint8_t smooth(uint8_t light, uint8_t b, uint8_t c, uint8_t d) {
if (!(light && b && c && d)) {
std::array<uint8_t, 4> l = {
light,
std::numeric_limits<uint8_t>::infinity(),
std::numeric_limits<uint8_t>::infinity(),
std::numeric_limits<uint8_t>::infinity()
std::numeric_limits<uint8_t>::max(),
std::numeric_limits<uint8_t>::max(),
std::numeric_limits<uint8_t>::max()
};
if (b)
if (b > 0)
l[1] = b;
if (c)
if (c > 0)
l[2] = c;
if (d)
if (d > 0)
l[3] = d;
uint8_t min_val = *std::min_element(l.begin(), l.end());
uint8_t min_val = *(std::min_element(l.begin(), l.end()));
uint8_t light = std::max(light, min_val);
uint8_t b = std::max(b, min_val);
uint8_t c = std::max(c, min_val);
uint8_t d = std::max(d, min_val);
return light + b + c + d; // To divide by 4



}
return light + b + c + d; // To divide by 4

}

constexpr uint8_t ao(uint8_t s1, uint8_t s2, uint8_t c) {
static inline uint8_t ao(uint8_t s1, uint8_t s2, uint8_t c) {
if (s1 && s2)
return 1;
return 4;

return 4 - (s1 + s2 + c); // To divide by 4
}
Expand Down Expand Up @@ -107,7 +111,7 @@ namespace Jukcraft {
case DOWN_INDEX:
neighbours = {
npos + ISOUTH + IWEST, npos + ISOUTH, npos + ISOUTH + IEAST,
npos + IWEST, npos + IEAST,
npos + IWEST, npos + IEAST,
npos + INORTH + IWEST, npos + INORTH, npos + INORTH + IEAST
};
break;
Expand All @@ -133,11 +137,12 @@ namespace Jukcraft {
}

BakedQuad Mesh::bakeCubeFace(const glm::ivec3& localPos, uint8_t normalIndex, uint8_t blocklight, uint8_t skylight) {
BakedQuad bakedQuad;
BakedQuad bakedQuad(blocklight, skylight);

glm::ivec3 npos = localPos + IDIRECTIONS[normalIndex];
std::array<glm::ivec3, 8> neighbours = getNeighbourVoxels(npos, normalIndex);
std::array<uint8_t, 8> neighbourOpacity, neighbourLights, neighbourSkyLights;

for (uint8_t i = 0; i < 8; i++) {
neighbourOpacity[i] = meshDelegates.opacityGetterDelegate(npos + neighbours[i]);
neighbourLights[i] = meshDelegates.lightGetterDelegate(npos + neighbours[i]);
Expand Down
20 changes: 20 additions & 0 deletions Jukcraft/src/renderer/chunk/Mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,26 @@
namespace Jukcraft {

struct BakedQuad {
BakedQuad(uint8_t blocklight, uint8_t skylight) {
smoothBlockLightData = {
(uint8_t)(blocklight * 4),
(uint8_t)(blocklight * 4),
(uint8_t)(blocklight * 4),
(uint8_t)(blocklight * 4)
};
smoothSkyLightData = {
(uint8_t)(skylight * 4),
(uint8_t)(skylight * 4),
(uint8_t)(skylight * 4),
(uint8_t)(skylight * 4)
};
ambientOcclusionData = {
4,
4,
4,
4
};
}
std::array<uint8_t, 4> smoothBlockLightData;
std::array<uint8_t, 4> smoothSkyLightData;
std::array<uint8_t, 4> ambientOcclusionData;
Expand Down
54 changes: 51 additions & 3 deletions Jukcraft/src/world/chunk/Chunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
#include "renderer/Renderer.h"
#include "world/chunk/Chunk.h"

#include "world/chunk/ChunkManager.h"

namespace Jukcraft {

using ChunkGetter = std::function<std::optional<std::shared_ptr<const Chunk>>(const glm::ivec2&)>;

Chunk::Chunk(const glm::ivec2& chunkPos, const std::vector<Block>& blockTypes)
:blockTypes(blockTypes), chunkPos(chunkPos),
Chunk::Chunk(const glm::ivec2& chunkPos, const std::vector<Block>& blockTypes, const ChunkGetter& chunkGetter)
:blockTypes(blockTypes), chunkPos(chunkPos), chunkGetter(chunkGetter),
mesh(
CHUNK_DIM * CHUNK_DIM * CHUNK_HEIGHT * 6 * 4,
MeshDelegates {
Expand Down Expand Up @@ -96,7 +98,9 @@ namespace Jukcraft {
uint8_t light = 15 << 4;
uint8_t blocklight = getBlockLightSafe(localPos + IDIRECTIONS[i]);
uint8_t skylight = getSkyLightSafe(localPos + IDIRECTIONS[i]);
BakedQuad bakedQuad = mesh.bakeCubeFace(localPos, i, blocklight, skylight);

// BakedQuad bakedQuad = mesh.bakeCubeFace(localPos, i, blocklight, skylight);
BakedQuad bakedQuad(blocklight, skylight);
mesh.pushCubeFace(type.getModel().getQuads()[i], localPos, type.getTextureLayout()[i], bakedQuad);
quadCount++;
}
Expand Down Expand Up @@ -124,8 +128,52 @@ namespace Jukcraft {
return nullptr;
}

bool Chunk::canRenderFacing(const glm::ivec3& localPos) const {
if (IsOutside(localPos)) {
glm::ivec3 worldPos = Chunk::ToWorldPos(chunkPos, localPos);
glm::ivec2 newChunkPos = Chunk::ToChunkPos(worldPos);
glm::ivec3 newLocalPos = Chunk::ToLocalPos(worldPos);
std::optional<std::shared_ptr<const Chunk>> chunk = chunkGetter(newChunkPos);
if (!chunk.has_value() || IsOutside(localPos))
return 0;
return (*chunk)->getBlock(newLocalPos);
}
else {
return !getBlock(localPos);
}
}

[[nodiscard]] uint8_t Chunk::getBlockLightSafe(const glm::ivec3& localPos) const {
if (IsOutside(localPos)) {
glm::ivec3 worldPos = Chunk::ToWorldPos(chunkPos, localPos);
glm::ivec2 newChunkPos = Chunk::ToChunkPos(worldPos);
glm::ivec3 newLocalPos = Chunk::ToLocalPos(worldPos);
std::optional<std::shared_ptr<const Chunk>> chunk = chunkGetter(newChunkPos);
if (!chunk.has_value() || IsOutside(localPos))
return 0;
return (*chunk)->getBlockLight(newLocalPos);
}
else {
return getBlockLight(localPos);
}
}

[[nodiscard]] uint8_t Chunk::getSkyLightSafe(const glm::ivec3& localPos) const {
if (IsOutside(localPos)) {
glm::ivec3 worldPos = Chunk::ToWorldPos(chunkPos, localPos);
glm::ivec2 newChunkPos = Chunk::ToChunkPos(worldPos);
glm::ivec3 newLocalPos = Chunk::ToLocalPos(worldPos);
std::optional<std::shared_ptr<const Chunk>> chunk = chunkGetter(newChunkPos);
if (!chunk.has_value() || IsOutside(localPos))
return 15;
return (*chunk)->getSkyLight(newLocalPos);
}
else {
return getSkyLight(localPos);
}
}


void Chunk::updateLayers() {
buildCubeLayer();
}
Expand Down
Loading

0 comments on commit 7d0307e

Please sign in to comment.