Skip to content

Commit

Permalink
Reduced AO effect with smooth lighting. The strength of this effect i…
Browse files Browse the repository at this point in the history
…s now a config option.
  • Loading branch information
Unarelith committed Mar 30, 2020
1 parent 218eddb commit 0742dec
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 10 deletions.
1 change: 1 addition & 0 deletions config.example.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ screenWidth = 1600
screenHeight = 1050
guiScale = 3
mipmapLevels = 0
aoStrength = 0.5

-- Input
mouseSensitivity = 8
Expand Down
2 changes: 2 additions & 0 deletions source/client/core/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ u16 Config::screenWidth = 1600;
u16 Config::screenHeight = 1050;
u8 Config::guiScale = 3;
u8 Config::mipmapLevels = 0;
float Config::aoStrength = 0.5f;

// Input
u8 Config::mouseSensitivity = 8;
Expand Down Expand Up @@ -86,6 +87,7 @@ void Config::loadConfigFromFile(const char *file) {
screenHeight = lua["screenHeight"].get_or(screenHeight);
guiScale = lua["guiScale"].get_or(guiScale);
mipmapLevels = lua["mipmapLevels"].get_or(mipmapLevels);
aoStrength = lua["aoStrength"].get_or(aoStrength);

mouseSensitivity = lua["mouseSensitivity"].get_or(mouseSensitivity);

Expand Down
1 change: 1 addition & 0 deletions source/client/core/Config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ namespace Config {
extern u16 screenHeight;
extern u8 guiScale;
extern u8 mipmapLevels;
extern float aoStrength;

// Input
extern u8 mouseSensitivity;
Expand Down
11 changes: 11 additions & 0 deletions source/client/states/SettingsMenuState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <gk/core/Debug.hpp>
#include <gk/core/EventHandler.hpp>
#include <gk/core/Mouse.hpp>
#include <gk/core/Utils.hpp>

#include "Config.hpp"
#include "Events.hpp"
Expand Down Expand Up @@ -196,6 +197,16 @@ void SettingsMenuState::addGraphicsButtons() {
Config::mipmapLevels = (Config::mipmapLevels + 1) % 5;
button.setText("Mipmap Levels: " + std::to_string(Config::mipmapLevels));
});

m_menuWidget.addButton("AO Strength: " + gk::to_string(Config::aoStrength, 2), [] (TextButton &button) {
Config::aoStrength += 0.25f;
if (Config::aoStrength > 1.5f)
Config::aoStrength = 0.f;

button.setText("AO Strength: " + gk::to_string(Config::aoStrength, 2));

World::isReloadRequested = true;
});
}

void SettingsMenuState::addInputButtons() {
Expand Down
22 changes: 15 additions & 7 deletions source/client/world/ChunkBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,24 +380,32 @@ inline u8 ChunkBuilder::getLightForVertex(Light light, s8f x, s8f y, s8f z, cons
(normal.z != 0) ? offset.z : 0
};

gk::Vector3i surroundingBlocks[4]{
{x + minOffset.x, y + minOffset.y, z + offset.z},
{x + offset.x, y + minOffset.y, z + minOffset.z},
{x + minOffset.x, y + offset.y, z + minOffset.z},
{x + offset.x, y + offset.y, z + offset.z}
};

// Get light values for surrounding nodes
s8 lightValues[4] = {
getLight(&chunk, x + minOffset.x, y + minOffset.y, z + offset.z),
getLight(&chunk, x + offset.x, y + minOffset.y, z + minOffset.z),
getLight(&chunk, x + minOffset.x, y + offset.y, z + minOffset.z),
getLight(&chunk, x + offset.x, y + offset.y, z + offset.z),
getLight(&chunk, surroundingBlocks[0].x, surroundingBlocks[0].y, surroundingBlocks[0].z),
getLight(&chunk, surroundingBlocks[1].x, surroundingBlocks[1].y, surroundingBlocks[1].z),
getLight(&chunk, surroundingBlocks[2].x, surroundingBlocks[2].y, surroundingBlocks[2].z),
getLight(&chunk, surroundingBlocks[3].x, surroundingBlocks[3].y, surroundingBlocks[3].z),
};

u8 count = 0, total = 0;
float count = 0, total = 0;
for (u8 i = 0 ; i < 4 ; ++i) {
// Fix light approximation
// if (i == 3 && lightValues[i] > lightValues[0] && !lightValues[1] && !lightValues[2])
// continue;

// If the chunk is initialized, add the light value to the total
if (lightValues[i] != -1) {
total += lightValues[i];
++count;
float strength = ((surroundingBlocks[i] - normal == gk::Vector3i{x, y, z}) ? 1 : Config::aoStrength);
total += lightValues[i] * strength;
count += strength;
}
}

Expand Down
11 changes: 8 additions & 3 deletions source/client/world/ChunkBuilder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,20 @@ class ChunkBuilder {
void addFace(s8f x, s8f y, s8f z, s8f f, const ClientChunk &chunk, const Block &block,
const gk::Vector3i &normal, const glm::vec3 *const vertexPos[4],
const gk::Vector3i *const neighbourOfs[4]);
void addCross(s8f x, s8f y, s8f z, const ClientChunk &chunk, const Block &block, const glm::vec3 *const vertexPos[2][4]);

void addCross(s8f x, s8f y, s8f z, const ClientChunk &chunk, const Block &block,
const glm::vec3 *const vertexPos[2][4]);

enum class Light {
Sun,
Torch
};

u8 getAmbientOcclusion(s8f x, s8f y, s8f z, const gk::Vector3i &offset, const gk::Vector3i &normal, const ClientChunk &chunk);
u8 getLightForVertex(Light light, s8f x, s8f y, s8f z, const gk::Vector3i &offset, const gk::Vector3i &normal, const ClientChunk &chunk);
u8 getAmbientOcclusion(s8f x, s8f y, s8f z, const gk::Vector3i &offset,
const gk::Vector3i &normal, const ClientChunk &chunk);

u8 getLightForVertex(Light light, s8f x, s8f y, s8f z, const gk::Vector3i &offset,
const gk::Vector3i &normal, const ClientChunk &chunk);

std::array<std::vector<gk::Vertex>, layers> m_vertices;

Expand Down

0 comments on commit 0742dec

Please sign in to comment.