Reduced AO effect with smooth lighting. The strength of this effect is now a config option.

This commit is contained in:
Quentin Bazin 2020-03-30 04:36:00 +02:00
parent 218eddb7e4
commit 0742dec5d0
6 changed files with 40 additions and 12 deletions

View File

@ -25,6 +25,7 @@ screenWidth = 1600
screenHeight = 1050
guiScale = 3
mipmapLevels = 0
aoStrength = 0.5
-- Input
mouseSensitivity = 8

View File

@ -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;
@ -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);

View File

@ -53,6 +53,7 @@ namespace Config {
extern u16 screenHeight;
extern u8 guiScale;
extern u8 mipmapLevels;
extern float aoStrength;
// Input
extern u8 mouseSensitivity;

View File

@ -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"
@ -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() {

View File

@ -380,15 +380,22 @@ inline u8 ChunkBuilder::getLightForVertex(Light light, s8f x, s8f y, s8f z, cons
(normal.z != 0) ? offset.z : 0
};
// 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),
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}
};
u8 count = 0, total = 0;
// Get light values for surrounding nodes
s8 lightValues[4] = {
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),
};
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])
@ -396,8 +403,9 @@ inline u8 ChunkBuilder::getLightForVertex(Light light, s8f x, s8f y, s8f z, cons
// 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;
}
}

View File

@ -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;