Browse Source

Revise sky pass

master
C. J. Howard 4 years ago
parent
commit
065837bb39
6 changed files with 123 additions and 51 deletions
  1. +4
    -4
      src/game/components/camera-follow-component.hpp
  2. +11
    -5
      src/game/states/play-state.cpp
  3. +5
    -4
      src/game/systems/camera-system.cpp
  4. +37
    -33
      src/renderer/passes/sky-pass.cpp
  5. +14
    -5
      src/renderer/passes/sky-pass.hpp
  6. +52
    -0
      src/utility/gamma.hpp

src/game/components/camera-subject-component.hpp → src/game/components/camera-follow-component.hpp View File

@ -17,17 +17,17 @@
* along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
*/
#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

+ 11
- 5
src/game/states/play-state.cpp View File

@ -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<float>(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<float>;
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<ecs::transform_component>(ctx->focal_point_entity, focal_point_transform);
ecs_registry.assign_or_replace<ecs::camera_subject_component>(ctx->focal_point_entity, focal_point_subject);
ecs_registry.assign_or_replace<ecs::camera_follow_component>(ctx->focal_point_entity, focal_point_follow);
ecs_registry.assign_or_replace<ecs::snap_component>(ctx->focal_point_entity, focal_point_snap);
// Setup camera

+ 5
- 4
src/game/systems/camera-system.cpp View File

@ -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<camera_subject_component, transform_component>().each(
[&](auto entity, auto& subject, auto& transform)
registry.view<camera_follow_component, transform_component>().each(
[&](auto entity, auto& follow, auto& transform)
{
target_focal_point += transform.transform.translation;
++subject_count;

+ 37
- 33
src/renderer/passes/sky-pass.cpp View File

@ -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<texture_2d>("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<float>(3.0f);
// Find sun direction
float3 sun_direction = {0, 0, -1};
const std::list<scene_object_base*>* 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;
}

+ 14
- 5
src/renderer/passes/sky-pass.hpp View File

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

+ 52
- 0
src/utility/gamma.hpp View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#ifndef ANTKEEPER_GAMMA_HPP
#define ANTKEEPER_GAMMA_HPP
#include "math/vector-type.hpp"
#include <cmath>
template <typename T, std::size_t N>
math::vector<T, N> to_linear(math::vector<T, N> 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 <typename T, std::size_t N>
math::vector<T, N> to_srgb(math::vector<T, N> 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

Loading…
Cancel
Save