From: Mikko Rasa Date: Mon, 10 Oct 2022 07:27:37 +0000 (+0300) Subject: Add a falling raindrops effect to forest pond X-Git-Url: https://git.tdb.fi/?a=commitdiff_plain;h=6a4cf0e1a965ab068439e5f5dd6caa8ffec71abf;p=libs%2Fgl.git Add a falling raindrops effect to forest pond --- diff --git a/demos/forestpond/data/exported/Forest.seq b/demos/forestpond/data/exported/Forest.seq index 9acebd35..10faa52e 100644 --- a/demos/forestpond/data/exported/Forest.seq +++ b/demos/forestpond/data/exported/Forest.seq @@ -52,6 +52,15 @@ step "" "shadow_map" depth_test LEQUAL; lighting "Forest.lightn"; }; +step "blended" "shadow_map" +{ + depth_test + { + compare LEQUAL; + write false; + }; + lighting "Forest.lightn"; +}; postprocessor { type ambient_occlusion; diff --git a/demos/forestpond/data/exported/Forest_environment.seq b/demos/forestpond/data/exported/Forest_environment.seq index 84cb4565..19517389 100644 --- a/demos/forestpond/data/exported/Forest_environment.seq +++ b/demos/forestpond/data/exported/Forest_environment.seq @@ -9,3 +9,12 @@ step "" "content" depth_test LEQUAL; lighting "Forest.lightn"; }; +step "blended" "content" +{ + depth_test + { + compare LEQUAL; + write false; + }; + lighting "Forest.lightn"; +}; diff --git a/demos/forestpond/data/rain.glsl b/demos/forestpond/data/rain.glsl new file mode 100644 index 00000000..7008498a --- /dev/null +++ b/demos/forestpond/data/rain.glsl @@ -0,0 +1,32 @@ +import msp_interface; +import shadow; + +uniform Rain +{ + vec4 rain_color; +}; + +uniform DynamicParams +{ + float time; +}; + +#pragma MSP stage(vertex) +void main() +{ + vec4 vpos = vertex; + vpos.z = vpos.z-fract(time/2.5)*20.0; + if(vpos.z<0.0) + vpos.z += 20.0; + out vec4 world_vertex = world_obj_matrix*vpos; + gl_Position = clip_eye_matrix*eye_world_matrix*world_vertex; + passthrough; +} + +#pragma MSP stage(fragment) +layout(location=0) out vec4 frag_color; +void main() +{ + float shadow = get_shadow_factor(0, world_vertex); + frag_color = rain_color*vec4(vec3(0.5+0.5*shadow), texcoord.x); +} diff --git a/demos/forestpond/data/rain.shader b/demos/forestpond/data/rain.shader new file mode 100644 index 00000000..8fe015b5 --- /dev/null +++ b/demos/forestpond/data/rain.shader @@ -0,0 +1,4 @@ +module "rain.glsl" +{ + specialize "use_shadow_map" true; +}; diff --git a/demos/forestpond/data/rain.tech b/demos/forestpond/data/rain.tech new file mode 100644 index 00000000..330d37b5 --- /dev/null +++ b/demos/forestpond/data/rain.tech @@ -0,0 +1,9 @@ +method "blended" +{ + shader "rain.shader"; + uniforms + { + uniform "rain_color" 0.5 0.5 0.5 0.5; + }; + blend SRC_ALPHA ONE_MINUS_SRC_ALPHA; +}; diff --git a/demos/forestpond/source/forestpond.cpp b/demos/forestpond/source/forestpond.cpp index d1a0d032..bbe1f7b0 100644 --- a/demos/forestpond/source/forestpond.cpp +++ b/demos/forestpond/source/forestpond.cpp @@ -13,7 +13,8 @@ ForestPond::ForestPond(int, char **): resources(nullptr), view(window), camera(resources.get("Camera.camera")), - water(resources.get("Water.object"), resources, { resources.get("Terrain.mesh"), -10.0f, -3.0f, 2.0f, 6.0f }) + water(resources.get("Water.object"), resources, { resources.get("Terrain.mesh"), -10.0f, -3.0f, 2.0f, 6.0f }), + rain(resources, 3000, { -1.5f, -3.0f }, 10.0f) { window.set_title("Forest Pond"); window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &ForestPond::exit), 0)); @@ -34,6 +35,7 @@ ForestPond::ForestPond(int, char **): content.add(resources.get("Forest.scene")); content.add(*env_map); + content.add(rain); water.set_matrix(GL::Matrix::translation(GL::Vector3(-3, 2, 0))); view.set_content(sequence.get()); diff --git a/demos/forestpond/source/forestpond.h b/demos/forestpond/source/forestpond.h index b22db689..17d94746 100644 --- a/demos/forestpond/source/forestpond.h +++ b/demos/forestpond/source/forestpond.h @@ -13,6 +13,7 @@ #include #include #include +#include "rain.h" #include "water.h" class ForestPond: public Msp::RegisteredApplication @@ -39,6 +40,7 @@ private: Msp::GL::OrderedScene content; Water water; + Rain rain; std::unique_ptr env_seq; std::unique_ptr env_map; diff --git a/demos/forestpond/source/rain.cpp b/demos/forestpond/source/rain.cpp new file mode 100644 index 00000000..d02e9ef9 --- /dev/null +++ b/demos/forestpond/source/rain.cpp @@ -0,0 +1,47 @@ +#include +#include +#include "rain.h" + +using namespace Msp; + +Rain::Rain(DataFile::Collection &resources, unsigned count, const LinAl::Vector ¢er, float radius): + tech(resources.get("rain.tech")), + mesh((GL::VERTEX3, GL::TEXCOORD1)) +{ + std::minstd_rand rng; + std::uniform_real_distribution dist(0.0f, 1.0f); + GL::MeshBuilder bld(mesh); + bld.begin(GL::LINES); + for(unsigned i=0; i a = Geometry::Angle::from_turns(dist(rng)); + GL::Vector3 p = compose(center, 0.0f) + GL::Vector3(cos(a)*r, sin(a)*r, i*20.0f/count); + bld.texcoord(1.0f); + bld.vertex(p); + bld.texcoord(0.0f); + bld.vertex(p+GL::Vector3(0.0f, 0.0f, 0.2f)); + } + bld.end(); + + shdata.uniform("time", 0.0f); +} + +void Rain::setup_frame(GL::Renderer &) +{ + time += 0.016f; + shdata.uniform("time", time); +} + +void Rain::render(GL::Renderer &renderer, GL::Tag tag) const +{ + const GL::RenderMethod *method = tech.find_method(tag); + if(!method) + return; + + GL::Renderer::Push _push(renderer); + + method->apply(renderer); + renderer.add_shader_data(shdata); + mesh.draw(renderer); +} diff --git a/demos/forestpond/source/rain.h b/demos/forestpond/source/rain.h new file mode 100644 index 00000000..fd38bea4 --- /dev/null +++ b/demos/forestpond/source/rain.h @@ -0,0 +1,25 @@ +#ifndef RAIN_H_ +#define RAIN_H_ + +#include +#include +#include +#include +#include + +class Rain: public Msp::GL::Renderable +{ +private: + const Msp::GL::Technique &tech; + Msp::GL::Mesh mesh; + Msp::GL::ProgramData shdata; + float time = 0.0f; + +public: + Rain(Msp::DataFile::Collection &, unsigned, const Msp::LinAl::Vector &, float); + + void setup_frame(Msp::GL::Renderer &) override; + void render(Msp::GL::Renderer &, Msp::GL::Tag) const override; +}; + +#endif