Browse Source

Pass more information from weather system to sky pass

master
C. J. Howard 3 years ago
parent
commit
4ededb3d7b
4 changed files with 66 additions and 78 deletions
  1. +0
    -1
      src/game/states/play-state.cpp
  2. +16
    -25
      src/game/systems/weather-system.cpp
  3. +36
    -43
      src/renderer/passes/sky-pass.cpp
  4. +14
    -9
      src/renderer/passes/sky-pass.hpp

+ 0
- 1
src/game/states/play-state.cpp View File

@ -89,7 +89,6 @@ void play_state_enter(game_context* ctx)
sky_pass* sky_pass = ctx->overworld_sky_pass;
sky_pass->set_enabled(true);
sky_pass->set_sky_model(ctx->resource_manager->load<model>("sky-dome.mdl"));
sky_pass->set_sun_angular_radius(ctx->biome->sun_angular_radius);
sky_pass->set_sun_color(ctx->biome->sun_color * ctx->biome->sun_intensity);
ctx->weather_system->set_coordinates(ctx->biome->coordinates);

+ 16
- 25
src/game/systems/weather-system.cpp View File

@ -228,7 +228,7 @@ void weather_system::update(double t, double dt)
// Obliquity of the ecliptic
double ecl = math::radians<double>(23.4393 - 3.563e-7 * d);
// Calculation sun position
// Calculation sun coordinates
double sun_longitude;
double sun_latitude;
double sun_distance;
@ -240,7 +240,7 @@ void weather_system::update(double t, double dt)
ecliptic_to_equatorial(sun_longitude, sun_latitude, ecl, &sun_right_ascension, &sun_declination);
equatorial_to_horizontal(sun_right_ascension, sun_declination, lmst, latitude, &sun_azimuth, &sun_elevation);
// Calculate moon position
// Calculate moon coordinates
double moon_longitude;
double moon_latitude;
double moon_distance;
@ -264,21 +264,25 @@ void weather_system::update(double t, double dt)
//std::cout << "eOT: " << eot << std::endl;
*/
float2 sun_az_el = float2{static_cast<float>(sun_azimuth), static_cast<float>(sun_elevation)};
math::quaternion<float> sun_azimuth_rotation = math::angle_axis(sun_az_el[0], float3{0, 1, 0});
math::quaternion<float> sun_elevation_rotation = math::angle_axis(sun_az_el[1], float3{-1, 0, 0});
math::quaternion<float> sun_rotation = math::normalize(sun_azimuth_rotation * sun_elevation_rotation);
float3 sun_position = math::normalize(sun_rotation * float3{0, 0, -1});
float2 moon_az_el = float2{static_cast<float>(moon_azimuth), static_cast<float>(moon_elevation)};
math::quaternion<float> moon_azimuth_rotation = math::angle_axis(moon_az_el[0], float3{0, 1, 0});
math::quaternion<float> moon_elevation_rotation = math::angle_axis(moon_az_el[1], float3{-1, 0, 0});
math::quaternion<float> moon_rotation = math::normalize(moon_azimuth_rotation * moon_elevation_rotation);
float3 moon_position = math::normalize(moon_rotation * float3{0, 0, -1});
if (sun_light)
{
math::quaternion<float> sun_azimuth_rotation = math::angle_axis((float)sun_azimuth, float3{0, 1, 0});
math::quaternion<float> sun_elevation_rotation = math::angle_axis((float)sun_elevation, float3{-1, 0, 0});
math::quaternion<float> sun_rotation = math::normalize(sun_azimuth_rotation * sun_elevation_rotation);
sun_direction = math::normalize(sun_rotation * float3{0, 0, -1});
sun_light->set_rotation(sun_rotation);
}
if (moon_light)
{
math::quaternion<float> moon_azimuth_rotation = math::angle_axis((float)moon_azimuth, float3{0, 1, 0});
math::quaternion<float> moon_elevation_rotation = math::angle_axis((float)moon_elevation, float3{-1, 0, 0});
math::quaternion<float> moon_rotation = math::normalize(moon_azimuth_rotation * moon_elevation_rotation);
moon_light->set_rotation(moon_rotation);
}
@ -317,6 +321,9 @@ void weather_system::update(double t, double dt)
sky_pass->set_sky_gradient(gradient);
sky_pass->set_time_of_day(hour * 60.0 * 60.0);
sky_pass->set_observer_coordinates(coordinates);
sky_pass->set_sun_coordinates(sun_position, sun_az_el);
sky_pass->set_moon_coordinates(moon_position, moon_az_el);
}
shadow_light = sun_light;
@ -355,32 +362,16 @@ void weather_system::set_ambient_light(::ambient_light* light)
void weather_system::set_sun_light(directional_light* light)
{
sun_light = light;
if (sky_pass)
{
sky_pass->set_sun_light(sun_light);
}
}
void weather_system::set_moon_light(directional_light* light)
{
moon_light = light;
if (sky_pass)
{
sky_pass->set_moon_light(moon_light);
}
}
void weather_system::set_sky_pass(::sky_pass* pass)
{
sky_pass = pass;
if (sky_pass)
{
sky_pass->set_sun_light(sun_light);
sky_pass->set_moon_light(moon_light);
}
}
void weather_system::set_shadow_map_pass(::shadow_map_pass* pass)

+ 36
- 43
src/renderer/passes/sky-pass.cpp View File

@ -35,8 +35,6 @@
#include "renderer/model.hpp"
#include "scene/camera.hpp"
#include "scene/scene.hpp"
#include "scene/ambient-light.hpp"
#include "scene/directional-light.hpp"
#include "scene/scene.hpp"
#include "utility/fundamental-types.hpp"
#include <cmath>
@ -45,19 +43,14 @@
sky_pass::sky_pass(::rasterizer* rasterizer, const ::framebuffer* framebuffer, resource_manager* resource_manager):
render_pass(rasterizer, framebuffer),
mouse_position({0.0f, 0.0f}),
sun_light(nullptr),
moon_light(nullptr),
time_of_day(0.0f),
sky_model(nullptr),
sky_model_vao(nullptr),
blue_noise_map(nullptr)
blue_noise_map(nullptr),
observer_coordinates{0.0f, 0.0f}
{
shader_program = resource_manager->load<::shader_program>("sky.glsl");
model_view_projection_input = shader_program->get_input("model_view_projection");
sun_direction_input = shader_program->get_input("sun_direction");
moon_direction_input = shader_program->get_input("moon_direction");
sun_angular_radius_input = shader_program->get_input("sun_angular_radius");
sun_angular_radius_input = shader_program->get_input("sun_angular_radius");
sun_color_input = shader_program->get_input("sun_color");
sky_gradient_input = shader_program->get_input("sky_gradient");
mouse_input = shader_program->get_input("mouse");
@ -65,7 +58,12 @@ sky_pass::sky_pass(::rasterizer* rasterizer, const ::framebuffer* framebuffer, r
time_input = shader_program->get_input("time");
time_of_day_input = shader_program->get_input("time_of_day");
blue_noise_map_input = shader_program->get_input("blue_noise_map");
observer_coordinates_input = shader_program->get_input("observer_coordinates");
sun_position_input = shader_program->get_input("sun_position");
sun_az_el_input = shader_program->get_input("sun_az_el");
moon_position_input = shader_program->get_input("moon_position");
moon_az_el_input = shader_program->get_input("moon_az_el");
const float vertex_data[] =
{
-1.0f, 1.0f, 0.0f,
@ -115,18 +113,6 @@ void sky_pass::render(render_context* context) const
float time = (time_tween) ? time_tween->interpolate(context->alpha) : 0.0f;
float2 resolution = {static_cast<float>(std::get<0>(viewport)), static_cast<float>(std::get<1>(viewport))};
float3 sun_direction = {0, -1, 0};
if (sun_light)
{
sun_direction = sun_light->get_direction_tween().interpolate(context->alpha);
}
float3 moon_direction = {0, -1, 0};
if (moon_light)
{
moon_direction = moon_light->get_direction_tween().interpolate(context->alpha);
}
const ::camera& camera = *context->camera;
float clip_near = camera.get_clip_near_tween().interpolate(context->alpha);
float clip_far = camera.get_clip_far_tween().interpolate(context->alpha);
@ -142,12 +128,6 @@ void sky_pass::render(render_context* context) const
// Upload shader parameters
if (model_view_projection_input)
model_view_projection_input->upload(model_view_projection);
if (sun_direction_input)
sun_direction_input->upload(sun_direction);
if (moon_direction_input)
moon_direction_input->upload(moon_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 (sky_gradient_input)
@ -162,6 +142,17 @@ void sky_pass::render(render_context* context) const
time_of_day_input->upload(time_of_day);
if (blue_noise_map_input)
blue_noise_map_input->upload(blue_noise_map);
if (observer_coordinates_input)
observer_coordinates_input->upload(observer_coordinates);
if (sun_position_input)
sun_position_input->upload(sun_position);
if (sun_az_el_input)
sun_az_el_input->upload(sun_az_el);
if (moon_position_input)
moon_position_input->upload(moon_position);
if (moon_az_el_input)
moon_az_el_input->upload(moon_az_el);
// Draw sky model
rasterizer->draw_arrays(*sky_model_vao, sky_model_drawing_mode, sky_model_start_index, sky_model_index_count);
@ -189,26 +180,11 @@ void sky_pass::set_sky_model(const model* model)
}
}
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_sun_light(const directional_light* light)
{
sun_light = light;
}
void sky_pass::set_moon_light(const directional_light* light)
{
moon_light = light;
}
void sky_pass::set_sky_gradient(const std::array<float4, 4>& gradient)
{
sky_gradient = gradient;
@ -229,6 +205,23 @@ void sky_pass::set_blue_noise_map(const texture_2d* texture)
blue_noise_map = texture;
}
void sky_pass::set_observer_coordinates(const float2& coordinates)
{
observer_coordinates = coordinates;
}
void sky_pass::set_sun_coordinates(const float3& position, const float2& az_el)
{
sun_position = position;
sun_az_el = az_el;
}
void sky_pass::set_moon_coordinates(const float3& position, const float2& az_el)
{
moon_position = position;
moon_az_el = az_el;
}
void sky_pass::handle_event(const mouse_moved_event& event)
{
mouse_position = {static_cast<float>(event.x), static_cast<float>(event.y)};

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

@ -32,7 +32,6 @@ class vertex_buffer;
class vertex_array;
class texture_2d;
class resource_manager;
class directional_light;
class model;
enum class drawing_mode;
@ -48,31 +47,34 @@ public:
virtual void render(render_context* context) const final;
void set_sky_model(const model* model);
void set_sun_angular_radius(float angle);
void set_sun_color(const float3& color);
void set_sun_light(const directional_light* light);
void set_moon_light(const directional_light* light);
void set_sky_gradient(const std::array<float4, 4>& gradient);
void set_time_of_day(float time);
void set_blue_noise_map(const texture_2d* texture);
void set_time_tween(const tween<double>* time);
void set_observer_coordinates(const float2& coordinates);
void set_sun_coordinates(const float3& position, const float2& az_el);
void set_moon_coordinates(const float3& position, const float2& az_el);
private:
virtual void handle_event(const mouse_moved_event& event);
shader_program* shader_program;
const shader_input* model_view_projection_input;
const shader_input* sun_direction_input;
const shader_input* moon_direction_input;
const shader_input* sun_angular_radius_input;
const shader_input* sun_color_input;
const shader_input* sky_gradient_input;
const shader_input* mouse_input;
const shader_input* resolution_input;
const shader_input* time_input;
const shader_input* time_of_day_input;
const shader_input* observer_coordinates_input;
const shader_input* sun_position_input;
const shader_input* sun_az_el_input;
const shader_input* moon_position_input;
const shader_input* moon_az_el_input;
const shader_input* blue_noise_map_input;
vertex_buffer* quad_vbo;
vertex_array* quad_vao;
@ -84,13 +86,16 @@ private:
float sun_angular_radius;
float3 sun_color;
const directional_light* sun_light;
const directional_light* moon_light;
const texture_2d* blue_noise_map;
float2 mouse_position;
std::array<float4, 4> sky_gradient;
const tween<double>* time_tween;
float time_of_day;
float2 observer_coordinates;
float3 sun_position;
float2 sun_az_el;
float3 moon_position;
float2 moon_az_el;
};
#endif // ANTKEEPER_SKY_PASS_HPP

Loading…
Cancel
Save