From 065837bb397a476bb1d92ad7e4e6d477a0976fe2 Mon Sep 17 00:00:00 2001 From: "C. J. Howard" Date: Thu, 20 Aug 2020 09:12:15 -0700 Subject: [PATCH] Revise sky pass --- ...ponent.hpp => camera-follow-component.hpp} | 8 +-- src/game/states/play-state.cpp | 16 +++-- src/game/systems/camera-system.cpp | 9 +-- src/renderer/passes/sky-pass.cpp | 70 ++++++++++--------- src/renderer/passes/sky-pass.hpp | 19 +++-- src/utility/gamma.hpp | 52 ++++++++++++++ 6 files changed, 123 insertions(+), 51 deletions(-) rename src/game/components/{camera-subject-component.hpp => camera-follow-component.hpp} (81%) create mode 100644 src/utility/gamma.hpp diff --git a/src/game/components/camera-subject-component.hpp b/src/game/components/camera-follow-component.hpp similarity index 81% rename from src/game/components/camera-subject-component.hpp rename to src/game/components/camera-follow-component.hpp index 4436605..047cd3b 100644 --- a/src/game/components/camera-subject-component.hpp +++ b/src/game/components/camera-follow-component.hpp @@ -17,17 +17,17 @@ * along with Antkeeper source code. If not, see . */ -#ifndef ANTKEEPER_ECS_CAMERA_SUBJECT_COMPONENT_HPP -#define ANTKEEPER_ECS_CAMERA_SUBJECT_COMPONENT_HPP +#ifndef ANTKEEPER_ECS_CAMERA_FOLLOW_COMPONENT_HPP +#define ANTKEEPER_ECS_CAMERA_FOLLOW_COMPONENT_HPP namespace ecs { -struct camera_subject_component +struct camera_follow_component { }; } // namespace ecs -#endif // ANTKEEPER_ECS_CAMERA_SUBJECT_COMPONENT_HPP +#endif // ANTKEEPER_ECS_CAMERA_FOLLOW_COMPONENT_HPP diff --git a/src/game/states/play-state.cpp b/src/game/states/play-state.cpp index 1e44e5a..9e3502c 100644 --- a/src/game/states/play-state.cpp +++ b/src/game/states/play-state.cpp @@ -31,7 +31,7 @@ #include "game/components/terrain-component.hpp" #include "game/components/tool-component.hpp" #include "game/components/transform-component.hpp" -#include "game/components/camera-subject-component.hpp" +#include "game/components/camera-follow-component.hpp" #include "game/entity-commands.hpp" #include "game/game-context.hpp" #include "game/states/game-states.hpp" @@ -48,14 +48,20 @@ #include "game/systems/camera-system.hpp" #include "game/systems/render-system.hpp" #include "utility/fundamental-types.hpp" +#include "utility/gamma.hpp" void play_state_enter(game_context* ctx) { logger* logger = ctx->logger; logger->push_task("Entering play state"); - // Enable sky pass - ctx->overworld_sky_pass->set_enabled(true); + // Set up sky pass + sky_pass* sky_pass = ctx->overworld_sky_pass; + sky_pass->set_enabled(true); + sky_pass->set_sun_angular_radius(math::radians(3.0f)); + sky_pass->set_sun_color({2.0f, 2.0f, 2.0f}); + sky_pass->set_horizon_color(to_linear(float3{81.0f, 162.0f, 219.0f} / 255.0f)); + sky_pass->set_zenith_color(to_linear(float3{7.0f, 134.0f, 206.0f} / 255.0f)); resource_manager* resource_manager = ctx->resource_manager; entt::registry& ecs_registry = *ctx->ecs_registry; @@ -162,7 +168,7 @@ void play_state_enter(game_context* ctx) ecs::transform_component focal_point_transform; focal_point_transform.transform = math::identity_transform; focal_point_transform.warp = true; - ecs::camera_subject_component focal_point_subject; + ecs::camera_follow_component focal_point_follow; ecs::snap_component focal_point_snap; focal_point_snap.ray = {float3{0, 10000, 0}, float3{0, -1, 0}}; focal_point_snap.warp = false; @@ -170,7 +176,7 @@ void play_state_enter(game_context* ctx) focal_point_snap.autoremove = false; ecs_registry.assign_or_replace(ctx->focal_point_entity, focal_point_transform); - ecs_registry.assign_or_replace(ctx->focal_point_entity, focal_point_subject); + ecs_registry.assign_or_replace(ctx->focal_point_entity, focal_point_follow); ecs_registry.assign_or_replace(ctx->focal_point_entity, focal_point_snap); // Setup camera diff --git a/src/game/systems/camera-system.cpp b/src/game/systems/camera-system.cpp index a62e8a3..56a9861 100644 --- a/src/game/systems/camera-system.cpp +++ b/src/game/systems/camera-system.cpp @@ -18,7 +18,7 @@ */ #include "camera-system.hpp" -#include "game/components/camera-subject-component.hpp" +#include "game/components/camera-follow-component.hpp" #include "game/components/transform-component.hpp" #include "scene/camera.hpp" #include "math/math.hpp" @@ -34,7 +34,8 @@ camera_system::camera_system(entt::registry& registry): viewport{0, 0, 0, 0}, mouse_position{0, 0} { - orbit_cam.set_elevation_limits({math::radians(5.0f), math::radians(89.0f)}); + //orbit_cam.set_elevation_limits({math::radians(5.0f), math::radians(89.0f)}); + orbit_cam.set_elevation_limits({math::radians(-89.0f), math::radians(89.0f)}); orbit_cam.set_focal_distance_limits({2.0f, 200.0f}); orbit_cam.set_fov_limits({math::radians(80.0f), math::radians(35.0f)}); orbit_cam.set_clip_near_limits({0.1f, 5.0f}); @@ -61,8 +62,8 @@ void camera_system::update(double t, double dt) // Determine target focal point int subject_count = 0; float3 target_focal_point = {0, 0, 0}; - registry.view().each( - [&](auto entity, auto& subject, auto& transform) + registry.view().each( + [&](auto entity, auto& follow, auto& transform) { target_focal_point += transform.transform.translation; ++subject_count; diff --git a/src/renderer/passes/sky-pass.cpp b/src/renderer/passes/sky-pass.cpp index 46e126a..e84f46e 100644 --- a/src/renderer/passes/sky-pass.cpp +++ b/src/renderer/passes/sky-pass.cpp @@ -48,8 +48,10 @@ sky_pass::sky_pass(::rasterizer* rasterizer, const ::framebuffer* framebuffer, r matrix_input = shader_program->get_input("matrix"); sun_direction_input = shader_program->get_input("sun_direction"); sun_angular_radius_input = shader_program->get_input("sun_angular_radius"); - sky_gradient_input = shader_program->get_input("sky_gradient"); - bayer_matrix_input = shader_program->get_input("bayer_matrix"); + horizon_color_input = shader_program->get_input("horizon_color"); + zenith_color_input = shader_program->get_input("zenith_color"); + sun_angular_radius_input = shader_program->get_input("sun_angular_radius"); + sun_color_input = shader_program->get_input("sun_color"); const float vertex_data[] = { @@ -68,32 +70,10 @@ sky_pass::sky_pass(::rasterizer* rasterizer, const ::framebuffer* framebuffer, r quad_vbo = new vertex_buffer(sizeof(float) * vertex_size * vertex_count, vertex_data); quad_vao = new vertex_array(); quad_vao->bind_attribute(VERTEX_POSITION_LOCATION, *quad_vbo, 3, vertex_attribute_type::float_32, vertex_stride, 0); - - sky_gradient = resource_manager->load("grassland-sky-gradient.png"); - sky_gradient->set_wrapping(texture_wrapping::clamp, texture_wrapping::clamp); - sky_gradient->set_filters(texture_min_filter::linear, texture_mag_filter::linear); - - // Generate Bayer matrix texture - static const char bayer_matrix_data[] = - { - 0, 32, 8, 40, 2, 34, 10, 42, - 48, 16, 56, 24, 50, 18, 58, 26, - 12, 44, 4, 36, 14, 46, 6, 38, - 60, 28, 52, 20, 62, 30, 54, 22, - 3, 35, 11, 43, 1, 33, 9, 41, - 51, 19, 59, 27, 49, 17, 57, 25, - 15, 47, 7, 39, 13, 45, 5, 37, - 63, 31, 55, 23, 61, 29, 53, 21 - }; - bayer_matrix = new texture_2d(8, 8, pixel_type::int_8, pixel_format::r, bayer_matrix_data); - bayer_matrix->set_wrapping(texture_wrapping::repeat, texture_wrapping::repeat); - bayer_matrix->set_filters(texture_min_filter::nearest, texture_mag_filter::nearest); - bayer_matrix->set_max_anisotropy(0.0f); } sky_pass::~sky_pass() { - delete bayer_matrix; delete quad_vao; delete quad_vbo; } @@ -109,11 +89,9 @@ void sky_pass::render(render_context* context) const auto viewport = framebuffer->get_dimensions(); rasterizer->set_viewport(0, 0, std::get<0>(viewport), std::get<1>(viewport)); - - float3 sun_direction = {0, 0, -1}; - float sun_angular_radius = math::radians(3.0f); // Find sun direction + float3 sun_direction = {0, 0, -1}; const std::list* lights = context->scene->get_objects(light::object_type_id); for (const scene_object_base* object: *lights) { @@ -135,13 +113,39 @@ void sky_pass::render(render_context* context) const rasterizer->use_program(*shader_program); // Upload shader parameters - matrix_input->upload(matrix); - sun_direction_input->upload(sun_direction); - sun_angular_radius_input->upload(sun_angular_radius); - sky_gradient_input->upload(sky_gradient); - bayer_matrix_input->upload(bayer_matrix); - + if (matrix_input) + matrix_input->upload(matrix); + if (sun_direction_input) + sun_direction_input->upload(sun_direction); + if (sun_angular_radius_input) + sun_angular_radius_input->upload(sun_angular_radius); + if (sun_color_input) + sun_color_input->upload(sun_color); + if (horizon_color_input) + horizon_color_input->upload(horizon_color); + if (zenith_color_input) + zenith_color_input->upload(zenith_color); + // Draw quad rasterizer->draw_arrays(*quad_vao, drawing_mode::triangles, 0, 6); } +void sky_pass::set_sun_angular_radius(float angle) +{ + sun_angular_radius = angle; +} + +void sky_pass::set_sun_color(const float3& color) +{ + sun_color = color; +} + +void sky_pass::set_horizon_color(const float3& color) +{ + horizon_color = color; +} + +void sky_pass::set_zenith_color(const float3& color) +{ + zenith_color = color; +} diff --git a/src/renderer/passes/sky-pass.hpp b/src/renderer/passes/sky-pass.hpp index 1043a72..a456c89 100644 --- a/src/renderer/passes/sky-pass.hpp +++ b/src/renderer/passes/sky-pass.hpp @@ -21,6 +21,7 @@ #define ANTKEEPER_SKY_PASS_HPP #include "renderer/render-pass.hpp" +#include "utility/fundamental-types.hpp" class shader_program; class shader_input; @@ -38,20 +39,28 @@ public: sky_pass(::rasterizer* rasterizer, const ::framebuffer* framebuffer, resource_manager* resource_manager); virtual ~sky_pass(); virtual void render(render_context* context) const final; + + void set_sun_angular_radius(float angle); + void set_sun_color(const float3& color); + void set_horizon_color(const float3& color); + void set_zenith_color(const float3& color); private: shader_program* shader_program; const shader_input* matrix_input; const shader_input* sun_direction_input; const shader_input* sun_angular_radius_input; - const shader_input* sky_gradient_input; - const shader_input* bayer_matrix_input; + const shader_input* sun_color_input; + const shader_input* horizon_color_input; + const shader_input* zenith_color_input; vertex_buffer* quad_vbo; vertex_array* quad_vao; - texture_2d* sky_gradient; - texture_2d* bayer_matrix; + + float sun_angular_radius; + float3 sun_color; + float3 horizon_color; + float3 zenith_color; }; #endif // ANTKEEPER_SKY_PASS_HPP - diff --git a/src/utility/gamma.hpp b/src/utility/gamma.hpp new file mode 100644 index 0000000..c8d1269 --- /dev/null +++ b/src/utility/gamma.hpp @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2020 Christopher J. Howard + * + * This file is part of Antkeeper source code. + * + * Antkeeper source code is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Antkeeper source code is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Antkeeper source code. If not, see . + */ + +#ifndef ANTKEEPER_GAMMA_HPP +#define ANTKEEPER_GAMMA_HPP + +#include "math/vector-type.hpp" +#include + +template +math::vector to_linear(math::vector v) +{ + constexpr T gamma = T(2.2); + + for (std::size_t i = 0; i < N; ++i) + { + v[i] = pow(v[i], gamma); + } + + return v; +} + +template +math::vector to_srgb(math::vector v) +{ + constexpr T inverse_gamma = T(1) / T(2.2); + + for (std::size_t i = 0; i < N; ++i) + { + v[i] = pow(v[i], inverse_gamma); + } + + return v; +} + +#endif // ANTKEEPER_GAMMA_HPP