Fix some issues introduced earlier

This commit is contained in:
mrkubax10 2024-06-13 20:10:24 +02:00
parent ba87349b07
commit 37917761de
8 changed files with 98 additions and 68 deletions

View File

@ -1,7 +1,55 @@
#version 330 core
#version 110
varying vec4 out_color;
varying vec3 out_pos;
#define glow_height (0.6)
#define glow_blur (0.5)
uniform vec3 uniform_glow_color;
uniform vec3 uniform_sun_vector;
uniform float uniform_glow_intensity;
uniform vec3 uniform_sky_color;
uniform float uniform_stars_intensity;
float map_range_clamped(float x, float in_min, float in_max, float out_min, float out_max) {
return clamp((x - in_min) / (in_max - in_min), 0.0, 1.0) * (out_max - out_min) + out_min;
}
float smoothstep_map_range(float x, float in_min, float in_max, float out_min, float out_max) {
// scale to input range and clamp
x = clamp((x - in_min) / (in_max - in_min), 0.0, 1.0);
// smoothstep
x = x * x * (3.0 - 2.0 * x);
// scale to output range
return x * (out_max - out_min) + out_min;
}
void main() {
gl_FragColor = out_color;
vec3 sphere_pos = normalize(out_pos);
// golden glow
float glow_displacement = dot(uniform_sun_vector, sphere_pos);
glow_displacement = map_range_clamped(glow_displacement, 1.0, -1.0, -glow_height, 0.0) + sphere_pos.y;
float glow_center = map_range_clamped(uniform_glow_intensity, 0.0, 1.0, -1.0, -0.5);
float glow = smoothstep_map_range(glow_displacement, glow_center + glow_blur, glow_center - glow_blur, 0.0, 1.0) * uniform_glow_intensity;
// sky fill gradient
vec3 sky_gradient = smoothstep_map_range(sphere_pos.y, 1.0, -1.0, 0.33333, 3.0) * uniform_sky_color;
// stars
float stars = 0.0 * uniform_stars_intensity; // not implemented
// final color
vec3 final_color = mix(sky_gradient, uniform_glow_color, glow) + stars;
// gamma correction (temporary solution)
final_color = pow(final_color, vec3(1.0 / 2.2));
gl_FragColor = vec4(final_color, 1.0);
}

View File

@ -1,8 +1,7 @@
#version 110
attribute vec3 in_pos;
attribute vec4 in_color;
varying vec4 out_color;
varying vec3 out_pos;
uniform mat4 uniform_view;
uniform mat4 uniform_projection;
@ -10,9 +9,9 @@ uniform mat4 uniform_projection;
void main() {
mat4 view_copy = uniform_view;
view_copy[3] = vec4(0, 0, 0, 1);
view_copy[0][3] = 0;
view_copy[1][3] = 0;
view_copy[2][3] = 0;
gl_Position = uniform_projection * view_copy * vec4(in_pos, 1.0f);
out_color = in_color;
view_copy[0][3] = 0.0;
view_copy[1][3] = 0.0;
view_copy[2][3] = 0.0;
gl_Position = uniform_projection * view_copy * vec4(in_pos, 1.0);
out_pos = in_pos;
}

View File

@ -55,12 +55,11 @@ namespace polygun::math {
return result;
}
float srgb_val2lin(const float& srgb_value) {
float srgb_val2lin(float srgb_value) {
return srgb_value <= 0.04045f ? srgb_value / 12.92f : std::pow((srgb_value + 0.055f) / 1.055f, 2.4f);
}
float lin_val2srgb(const float& lin_value) {
float lin_val2srgb(float lin_value) {
return lin_value <= 0.0031308f ? lin_value * 12.92f : std::pow(lin_value, 1.0f/2.4f) * 1.055f - 0.055f;
}
}

View File

@ -35,11 +35,11 @@ SOFTWARE.
namespace polygun::math {
float srgb_val2lin(const float& srgb_value);
float srgb_val2lin(float srgb_value);
RGBColorf srgb2lin(const RGBColorf& srgb_color);
RGBAColorf srgb2lin(const RGBAColorf& srgb_color);
float lin_val2srgb(const float& lin_value);
float lin_val2srgb(float lin_value);
RGBColorf lin2srgb(const RGBColorf& lin_color);
RGBAColorf lin2srgb(const RGBAColorf& lin_color);
}

View File

@ -151,11 +151,7 @@ void LegacyGLMeshRenderer::render_textured(Mesh* mesh, Texture* texture) {
m_transform.identity();
}
void LegacyGLMeshRenderer::render_skybox_mesh(Mesh* mesh) {
if(!mesh->has_colors()) {
LOG_ERROR("Attempt to render skybox mesh without colors!");
return;
}
void LegacyGLMeshRenderer::render_skybox_mesh(Mesh* mesh, const math::RGBColorf& glow_color, const math::Vector3f& sun_vector, const float glow_intensity, const math::Vector3f& sky_color, const float stars_intensity) {
LegacyGLMesh* gl_mesh = static_cast<LegacyGLMesh*>(mesh);
glEnableClientState(GL_VERTEX_ARRAY);
CHECK_GL;
@ -163,8 +159,7 @@ void LegacyGLMeshRenderer::render_skybox_mesh(Mesh* mesh) {
CHECK_GL;
glEnableClientState(GL_COLOR_ARRAY);
CHECK_GL;
glColorPointer(4, GL_FLOAT, 0, gl_mesh->m_colors);
CHECK_GL;
set_color_mod(math::RGBAColorf{sky_color[0], sky_color[1], sky_color[2], 1.0f}, mesh);
glMatrixMode(GL_MODELVIEW);
CHECK_GL;
glLoadIdentity();

View File

@ -38,7 +38,7 @@ namespace polygun::renderer {
virtual void render(Mesh* mesh, const math::RGBAColorf& color) override;
virtual void render_textured(Mesh* mesh, Texture* texture) override;
virtual void render_chunk_mesh(Mesh* mesh, Texture* texture, float) override { render_textured(mesh, texture); }
virtual void render_skybox_mesh(Mesh* mesh) override;
virtual void render_skybox_mesh(Mesh* mesh, const math::RGBColorf& glow_color, const math::Vector3f& sun_vector, const float glow_intensity, const math::Vector3f& sky_color, const float stars_intensity) override;
// Unsupported
virtual void render_with_multiple_textures(Mesh* mesh, const std::vector<Texture*>& textures) override {}
// Unsupported

View File

@ -30,7 +30,6 @@ SOFTWARE.
#include "game/renderer/master_renderer.hpp"
#include "game/renderer/mesh.hpp"
#include "game/renderer/shader.hpp"
//#include "common/logger.hpp"
using namespace polygun::world;
@ -70,7 +69,6 @@ const polygun::math::RGBColorf night_sky_color = polygun::math::srgb2lin(polygun
const float glow_climax = 0.6f;
const float glow_falloff = 0.25f;
const float stars_falloff = 0.15f;
////////////////////////
@ -88,6 +86,8 @@ Skybox::Skybox(renderer::MasterRenderer* master_renderer) :
m_day_time(15000)
{
update_skybox_colors();
m_mesh->load_vertices(g_cube_vertices);
m_mesh->load_indices(g_cube_indices);
m_shader->load_from_file("skybox");
}
@ -142,17 +142,13 @@ void Skybox::on_sync(network::NetworkPacket& packet) {
void Skybox::update_skybox_colors() {
acquire();
//const float float_time = std::fmod(m_day_time/65536.0f * 10.0f, 1.0f);
const float float_time = m_day_time/65536.0f;
const float angle = float_time * 2.0f * POLYGUN_PI;
// calculate sun vector //
m_sun_vector = polygun::math::Vector3f{-std::sin(angle), -std::cos(angle), 0.0f};
// calculate glow intensity and stars intensity //
float lerp_factor = float_time;
if (lerp_factor > 0.5f)
lerp_factor = 1.0f - lerp_factor;
@ -163,14 +159,11 @@ void Skybox::update_skybox_colors() {
lerp_factor *= 2.0f;
// calculate distance to glow climax
float distance_to_climax = std::abs(lerp_factor - glow_climax);
const float distance_to_climax = std::abs(lerp_factor - glow_climax);
// if it's smaller than falloff, calculate intensity
m_glow_intensity = (distance_to_climax < glow_falloff) ? (1.0f - distance_to_climax / glow_falloff) : 0.0f;
//LOG_VERBOSE("time: %d | float time: %f | glow intensity: %f", m_day_time, float_time, m_glow_intensity);
// calculate sky color //
// reusing negated cosine for sky color
// 1.0f - m_sun_vector[1] is equivalent to cos(angle) + 1.0f
lerp_factor = std::pow(smoothstep((1.0f - m_sun_vector[1]) / 2.0f), 0.25f);
@ -178,9 +171,5 @@ void Skybox::update_skybox_colors() {
// interpolate between day and night sky colors
m_sky_color = night_sky_color * lerp_factor + day_sky_color * (1.0f - lerp_factor);
m_mesh->load_vertices(g_cube_vertices);
m_mesh->load_indices(g_cube_indices);
release();
}

View File

@ -76,7 +76,6 @@ Server::Server(std::atomic<bool>* running_atomic, std::optional<unsigned short>
#endif
m_command_thread.reset(new std::thread(&Server::command_thread_func, this));
}
m_update_thread.reset(new std::thread(&Server::update_thread_func, this));
m_player_storage.load();
m_chunk_manager.load_map_from_file(m_config.m_default_map);
m_mod_manager.enumerate_modules();
@ -99,6 +98,7 @@ Server::~Server() {
void Server::run() {
m_mod_manager.run_modules();
m_running->store(true);
m_update_thread.reset(new std::thread(&Server::update_thread_func, this));
polygun::network::NetworkPacket packet;
while(m_running->load()) {
bool handled = false;