Browse Source

Initial test of dynamic sky shader

master
C. J. Howard 4 years ago
parent
commit
adae795bfe
4 changed files with 45 additions and 2 deletions
  1. +1
    -0
      src/game/bootloader.cpp
  2. +8
    -0
      src/game/states/play-state.cpp
  3. +24
    -1
      src/renderer/passes/sky-pass.cpp
  4. +12
    -1
      src/renderer/passes/sky-pass.hpp

+ 1
- 0
src/game/bootloader.cpp View File

@ -492,6 +492,7 @@ void setup_rendering(game_context* ctx)
ctx->overworld_clear_pass = new clear_pass(ctx->rasterizer, ctx->framebuffer_hdr); ctx->overworld_clear_pass = new clear_pass(ctx->rasterizer, ctx->framebuffer_hdr);
ctx->overworld_clear_pass->set_cleared_buffers(false, true, true); ctx->overworld_clear_pass->set_cleared_buffers(false, true, true);
ctx->overworld_sky_pass = new sky_pass(ctx->rasterizer, ctx->framebuffer_hdr, ctx->resource_manager); ctx->overworld_sky_pass = new sky_pass(ctx->rasterizer, ctx->framebuffer_hdr, ctx->resource_manager);
ctx->app->get_event_dispatcher()->subscribe<mouse_moved_event>(ctx->overworld_sky_pass);
ctx->overworld_sky_pass->set_enabled(false); ctx->overworld_sky_pass->set_enabled(false);
ctx->overworld_material_pass = new material_pass(ctx->rasterizer, ctx->framebuffer_hdr, ctx->resource_manager); ctx->overworld_material_pass = new material_pass(ctx->rasterizer, ctx->framebuffer_hdr, ctx->resource_manager);
ctx->overworld_material_pass->set_fallback_material(ctx->fallback_material); ctx->overworld_material_pass->set_fallback_material(ctx->fallback_material);

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

@ -38,6 +38,9 @@
#include "math/math.hpp" #include "math/math.hpp"
#include "nest.hpp" #include "nest.hpp"
#include "renderer/material.hpp" #include "renderer/material.hpp"
#include "rasterizer/texture-2d.hpp"
#include "rasterizer/texture-filter.hpp"
#include "rasterizer/texture-wrapping.hpp"
#include "renderer/model.hpp" #include "renderer/model.hpp"
#include "renderer/passes/sky-pass.hpp" #include "renderer/passes/sky-pass.hpp"
#include "resources/resource-manager.hpp" #include "resources/resource-manager.hpp"
@ -89,6 +92,11 @@ void play_state_enter(game_context* ctx)
sky_pass->set_horizon_color(ctx->biome->horizon_color); sky_pass->set_horizon_color(ctx->biome->horizon_color);
sky_pass->set_zenith_color(ctx->biome->zenith_color); sky_pass->set_zenith_color(ctx->biome->zenith_color);
texture_2d* sky_palette = ctx->resource_manager->load<texture_2d>("sky-palette.png");
sky_palette->set_wrapping(texture_wrapping::repeat, texture_wrapping::clamp);
sky_palette->set_filters(texture_min_filter::linear, texture_mag_filter::linear);
sky_pass->set_sky_palette(sky_palette);
ctx->tool_system->set_sun_direction(ctx->sun_direct->get_direction()); ctx->tool_system->set_sun_direction(ctx->sun_direct->get_direction());
resource_manager* resource_manager = ctx->resource_manager; resource_manager* resource_manager = ctx->resource_manager;

+ 24
- 1
src/renderer/passes/sky-pass.cpp View File

@ -42,7 +42,9 @@
#include <glad/glad.h> #include <glad/glad.h>
sky_pass::sky_pass(::rasterizer* rasterizer, const ::framebuffer* framebuffer, resource_manager* resource_manager): sky_pass::sky_pass(::rasterizer* rasterizer, const ::framebuffer* framebuffer, resource_manager* resource_manager):
render_pass(rasterizer, framebuffer)
render_pass(rasterizer, framebuffer),
sky_palette(nullptr),
mouse_position({0.0f, 0.0f})
{ {
shader_program = resource_manager->load<::shader_program>("sky.glsl"); shader_program = resource_manager->load<::shader_program>("sky.glsl");
matrix_input = shader_program->get_input("matrix"); matrix_input = shader_program->get_input("matrix");
@ -52,6 +54,9 @@ sky_pass::sky_pass(::rasterizer* rasterizer, const ::framebuffer* framebuffer, r
zenith_color_input = shader_program->get_input("zenith_color"); zenith_color_input = shader_program->get_input("zenith_color");
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"); sun_color_input = shader_program->get_input("sun_color");
sky_palette_input = shader_program->get_input("sky_palette");
mouse_input = shader_program->get_input("mouse");
resolution_input = shader_program->get_input("resolution");
const float vertex_data[] = const float vertex_data[] =
{ {
@ -90,6 +95,8 @@ void sky_pass::render(render_context* context) const
auto viewport = framebuffer->get_dimensions(); auto viewport = framebuffer->get_dimensions();
rasterizer->set_viewport(0, 0, std::get<0>(viewport), std::get<1>(viewport)); rasterizer->set_viewport(0, 0, std::get<0>(viewport), std::get<1>(viewport));
float2 resolution = {static_cast<float>(std::get<0>(viewport)), static_cast<float>(std::get<1>(viewport))};
// Find sun direction // Find sun direction
float3 sun_direction = {0, 0, -1}; float3 sun_direction = {0, 0, -1};
const std::list<scene_object_base*>* lights = context->scene->get_objects(light::object_type_id); const std::list<scene_object_base*>* lights = context->scene->get_objects(light::object_type_id);
@ -125,6 +132,12 @@ void sky_pass::render(render_context* context) const
horizon_color_input->upload(horizon_color); horizon_color_input->upload(horizon_color);
if (zenith_color_input) if (zenith_color_input)
zenith_color_input->upload(zenith_color); zenith_color_input->upload(zenith_color);
if (sky_palette_input && sky_palette)
sky_palette_input->upload(sky_palette);
if (mouse_input)
mouse_input->upload(mouse_position);
if (resolution_input)
resolution_input->upload(resolution);
// Draw quad // Draw quad
rasterizer->draw_arrays(*quad_vao, drawing_mode::triangles, 0, 6); rasterizer->draw_arrays(*quad_vao, drawing_mode::triangles, 0, 6);
@ -149,3 +162,13 @@ void sky_pass::set_zenith_color(const float3& color)
{ {
zenith_color = color; zenith_color = color;
} }
void sky_pass::set_sky_palette(const texture_2d* texture)
{
sky_palette = texture;
}
void sky_pass::handle_event(const mouse_moved_event& event)
{
mouse_position = {static_cast<float>(event.x), static_cast<float>(event.y)};
}

+ 12
- 1
src/renderer/passes/sky-pass.hpp View File

@ -22,6 +22,8 @@
#include "renderer/render-pass.hpp" #include "renderer/render-pass.hpp"
#include "utility/fundamental-types.hpp" #include "utility/fundamental-types.hpp"
#include "event/event-handler.hpp"
#include "event/input-events.hpp"
class shader_program; class shader_program;
class shader_input; class shader_input;
@ -33,7 +35,8 @@ class resource_manager;
/** /**
* *
*/ */
class sky_pass: public render_pass
class sky_pass: public render_pass,
public event_handler<mouse_moved_event>
{ {
public: public:
sky_pass(::rasterizer* rasterizer, const ::framebuffer* framebuffer, resource_manager* resource_manager); sky_pass(::rasterizer* rasterizer, const ::framebuffer* framebuffer, resource_manager* resource_manager);
@ -44,8 +47,11 @@ public:
void set_sun_color(const float3& color); void set_sun_color(const float3& color);
void set_horizon_color(const float3& color); void set_horizon_color(const float3& color);
void set_zenith_color(const float3& color); void set_zenith_color(const float3& color);
void set_sky_palette(const texture_2d* texture);
private: private:
virtual void handle_event(const mouse_moved_event& event);
shader_program* shader_program; shader_program* shader_program;
const shader_input* matrix_input; const shader_input* matrix_input;
const shader_input* sun_direction_input; const shader_input* sun_direction_input;
@ -53,6 +59,9 @@ private:
const shader_input* sun_color_input; const shader_input* sun_color_input;
const shader_input* horizon_color_input; const shader_input* horizon_color_input;
const shader_input* zenith_color_input; const shader_input* zenith_color_input;
const shader_input* sky_palette_input;
const shader_input* mouse_input;
const shader_input* resolution_input;
vertex_buffer* quad_vbo; vertex_buffer* quad_vbo;
vertex_array* quad_vao; vertex_array* quad_vao;
@ -61,6 +70,8 @@ private:
float3 sun_color; float3 sun_color;
float3 horizon_color; float3 horizon_color;
float3 zenith_color; float3 zenith_color;
const texture_2d* sky_palette;
float2 mouse_position;
}; };
#endif // ANTKEEPER_SKY_PASS_HPP #endif // ANTKEEPER_SKY_PASS_HPP

Loading…
Cancel
Save