]> git.tdb.fi Git - libs/gl.git/commitdiff
Add a falling raindrops effect to forest pond
authorMikko Rasa <tdb@tdb.fi>
Mon, 10 Oct 2022 07:27:37 +0000 (10:27 +0300)
committerMikko Rasa <tdb@tdb.fi>
Mon, 10 Oct 2022 07:27:37 +0000 (10:27 +0300)
demos/forestpond/data/exported/Forest.seq
demos/forestpond/data/exported/Forest_environment.seq
demos/forestpond/data/rain.glsl [new file with mode: 0644]
demos/forestpond/data/rain.shader [new file with mode: 0644]
demos/forestpond/data/rain.tech [new file with mode: 0644]
demos/forestpond/source/forestpond.cpp
demos/forestpond/source/forestpond.h
demos/forestpond/source/rain.cpp [new file with mode: 0644]
demos/forestpond/source/rain.h [new file with mode: 0644]

index 9acebd35e7699b39fa715d3614a8cc1ef59c0cee..10faa52ed7a6da9813efa19dfdccd7a86ae1634b 100644 (file)
@@ -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;
index 84cb4565369e7777b5a9e89cf63d7b2147f07d0a..19517389a2939a7cb39ce3273ce03e4b7f7411bc 100644 (file)
@@ -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 (file)
index 0000000..7008498
--- /dev/null
@@ -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 (file)
index 0000000..8fe015b
--- /dev/null
@@ -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 (file)
index 0000000..330d37b
--- /dev/null
@@ -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;
+};
index d1a0d03215f21d7f1c87bf4b8a016a0376dc761e..bbe1f7b062c4b6ea07908698cff9114bf9bbb082 100644 (file)
@@ -13,7 +13,8 @@ ForestPond::ForestPond(int, char **):
        resources(nullptr),
        view(window),
        camera(resources.get<GL::Camera>("Camera.camera")),
-       water(resources.get<GL::Object>("Water.object"), resources, { resources.get<GL::Mesh>("Terrain.mesh"), -10.0f, -3.0f, 2.0f, 6.0f })
+       water(resources.get<GL::Object>("Water.object"), resources, { resources.get<GL::Mesh>("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<GL::Scene>("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());
index b22db689eda89586ce3ab3e5c49f50691217392e..17d94746c0306461220f54d0cff6160d2eeae5ea 100644 (file)
@@ -13,6 +13,7 @@
 #include <msp/gl/windowview.h>
 #include <msp/graphics/display.h>
 #include <msp/graphics/window.h>
+#include "rain.h"
 #include "water.h"
 
 class ForestPond: public Msp::RegisteredApplication<ForestPond>
@@ -39,6 +40,7 @@ private:
 
        Msp::GL::OrderedScene content;
        Water water;
+       Rain rain;
        std::unique_ptr<Msp::GL::Sequence> env_seq;
        std::unique_ptr<Msp::GL::EnvironmentMap> env_map;
 
diff --git a/demos/forestpond/source/rain.cpp b/demos/forestpond/source/rain.cpp
new file mode 100644 (file)
index 0000000..d02e9ef
--- /dev/null
@@ -0,0 +1,47 @@
+#include <msp/gl/meshbuilder.h>
+#include <msp/gl/renderer.h>
+#include "rain.h"
+
+using namespace Msp;
+
+Rain::Rain(DataFile::Collection &resources, unsigned count, const LinAl::Vector<float, 2> &center, float radius):
+       tech(resources.get<GL::Technique>("rain.tech")),
+       mesh((GL::VERTEX3, GL::TEXCOORD1))
+{
+       std::minstd_rand rng;
+       std::uniform_real_distribution<float> dist(0.0f, 1.0f);
+       GL::MeshBuilder bld(mesh);
+       bld.begin(GL::LINES);
+       for(unsigned i=0; i<count; ++i)
+       {
+               float r = sqrt(dist(rng))*radius;
+               Geometry::Angle<float> a = Geometry::Angle<float>::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 (file)
index 0000000..fd38bea
--- /dev/null
@@ -0,0 +1,25 @@
+#ifndef RAIN_H_
+#define RAIN_H_
+
+#include <random>
+#include <msp/datafile/collection.h>
+#include <msp/gl/mesh.h>
+#include <msp/gl/renderable.h>
+#include <msp/gl/technique.h>
+
+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, 2> &, float);
+
+       void setup_frame(Msp::GL::Renderer &) override;
+       void render(Msp::GL::Renderer &, Msp::GL::Tag) const override;
+};
+
+#endif