From 455f2db26dfc969865a4a3784700e0cc4ea19993 Mon Sep 17 00:00:00 2001 From: Lauchmelder Date: Mon, 27 Dec 2021 21:00:14 +0100 Subject: [PATCH] =?UTF-8?q?added=20scrolling=20plot=20and=20weierstra?= =?UTF-8?q?=C3=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Application.cpp | 42 +++++++++++++++++++++++++------------ src/Application.hpp | 4 ++-- src/CMakeLists.txt | 1 + src/ScrollingPlot.cpp | 48 +++++++++++++++++++++++++++++++++++++++++++ src/ScrollingPlot.hpp | 32 +++++++++++++++++++++++++++++ src/Topology.hpp | 8 +++++--- vendor/lol | 2 +- 7 files changed, 118 insertions(+), 19 deletions(-) create mode 100644 src/ScrollingPlot.cpp create mode 100644 src/ScrollingPlot.hpp diff --git a/src/Application.cpp b/src/Application.cpp index a23e298..1afe2c5 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -130,26 +130,41 @@ void Application::Init(int width, int height, const std::string& title) float aspectRatio = (float)windowWidth / (float)windowHeight; camera = OrbitingCamera(glm::vec3(0.0f, 0.0f, 0.0f), 6.0f); camera.SetPerspective(100.0f, aspectRatio, 0.01f, 100.0f); - pitch = 45.0f; - yaw = 90.0f; + pitch = 90.0f; + yaw = 88.0f; distance = 6.0f; data.camera = &camera; data.aspectRatio = (float)width / (float)height; - topology = new Topology(manager, glm::vec2(15.0f, 7.5f), glm::uvec2(200, 100)); - glm::uvec2 size = topology->GetSize(); + orthogonal = true; - float* pixels = topology->GetTopology(); - for (unsigned int y = 0; y < size.y; y++) - { - for (unsigned int x = 0; x < size.x; x++) + topology = new ScrollingPlot( + manager, + glm::vec2(15.0f, 1.0f), + glm::uvec2(2000, 2), + glm::vec2(-1.0f, 1.0f), + 0.001f, + // [](float t, float y) + // { + // return cos(t) + 0.5f * cos(2.0f * t) + 0.1f * sin(1.3f * t) + 0.3f * cos(3.5f * t) + 2.0f * (y * y) - 2.0f; + // } + [](float t, float y) { - pixels[y * size.x + x] = cos(x * glm::two_pi() / ((float)size.x * 0.5f)) + 2.0f * (y / (float)size.y * y / (float)size.y) - 2.0f; - } - } + // Weierstraß + float z = 0.0f; + for(unsigned k = 1; k < 100; k++) + { + unsigned int twoK = std::pow(2, k); + z += twoK * sin(twoK * t) / std::pow(3, k); + } - topology->MakeTexture(); + return z; + } + ); + + colormap = 3; + topology->SetColormap(colormaps[colormap]); glfwWindowHint(GLFW_SAMPLES, 4); @@ -167,7 +182,8 @@ void Application::Launch() topology->SetHeightMapping(enableHeightMap); topology->SetColorMapping(enableColorMap); - topology->Scroll(enableScroll); + if(enableScroll) + topology->StepForward(3); if(orthogonal) camera.SetOrthogonal(-width / 2.0f * data.aspectRatio, width / 2.0f * data.aspectRatio, -width / 2.0, width / 2.0f, -1.0f, 100.0f); diff --git a/src/Application.hpp b/src/Application.hpp index 0c271fc..dbee10a 100644 --- a/src/Application.hpp +++ b/src/Application.hpp @@ -2,7 +2,7 @@ #include #include "OrbitingCamera.hpp" -#include "Topology.hpp" +#include "ScrollingPlot.hpp" #include "Util.hpp" struct GLFWwindow; @@ -56,5 +56,5 @@ private: bool enableScroll = false; int colormap = 0; - Topology* topology; + ScrollingPlot* topology; }; \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index aafe8f3..069edf6 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -3,6 +3,7 @@ add_executable(visualizer "OrbitingCamera.cpp" "Topology.cpp" "Colormaps.cpp" + "ScrollingPlot.cpp" ) target_sources(visualizer PUBLIC diff --git a/src/ScrollingPlot.cpp b/src/ScrollingPlot.cpp new file mode 100644 index 0000000..2a9cbe4 --- /dev/null +++ b/src/ScrollingPlot.cpp @@ -0,0 +1,48 @@ +#include "ScrollingPlot.hpp" + +ScrollingPlot::ScrollingPlot( + lol::ObjectManager& manager, + const glm::vec2& size, + const glm::uvec2& subdivision, + const glm::vec2& domain, + float temporalResolution, + std::function func + ): + Topology(manager, size, subdivision), domain(domain), dt(temporalResolution), func(func) +{ + // Create initial texture for t = 0 + for(unsigned int x = 0; x < subdivision.x; x++) + { + CalculateStrip(x); + t += dt; + } + + MakeTexture(); +} + +void ScrollingPlot::StepForward(unsigned int steps) +{ + for(unsigned int n = 0; n < steps; n++) + { + t += dt; + CalculateStrip(currentStrip); + currentStrip = (currentStrip + 1) % image.GetDimensions().x; + + offset += 1.0f / (float)image.GetDimensions().x; + } + MakeTexture(); +} + +void ScrollingPlot::CalculateStrip(unsigned int strip) +{ + float* pixels = GetTopology(); + glm::uvec2 size = image.GetDimensions(); + + for(unsigned int y = 0; y < size.y; y++) + { + pixels[y * size.x + strip] = func( + t, + Map(glm::vec2(0.0f, size.y), domain, y) + ); + } +} \ No newline at end of file diff --git a/src/ScrollingPlot.hpp b/src/ScrollingPlot.hpp new file mode 100644 index 0000000..1886b64 --- /dev/null +++ b/src/ScrollingPlot.hpp @@ -0,0 +1,32 @@ +#pragma once + +#include +#include + +#include "Topology.hpp" + +class ScrollingPlot : public Topology +{ +public: + ScrollingPlot( + lol::ObjectManager& manager, + const glm::vec2& size, const glm::uvec2& subdivision, + const glm::vec2& domain, + float temporalResolution, + std::function func + ); + + void StepForward(unsigned int steps); + inline virtual void Scroll(bool enable) override {} + +private: + void CalculateStrip(unsigned int strip); + +private: + float t = 0.0f; + float dt = 0.0f; + unsigned currentStrip = 0; + + glm::vec2 domain; + std::function func; +}; \ No newline at end of file diff --git a/src/Topology.hpp b/src/Topology.hpp index 2dfd939..86f1832 100644 --- a/src/Topology.hpp +++ b/src/Topology.hpp @@ -18,7 +18,7 @@ public: inline void SetHeightMapping(bool enable) { heightFactor = enable ? 2.0f : 0.0f; } inline void SetColorMapping(bool enable) { renderColor = enable; } - inline void Scroll(bool enable) { scroll = enable; } + inline virtual void Scroll(bool enable) { scroll = enable; } inline float* GetTopology() const { return (float*)image.GetPixels(); }; inline const glm::uvec2& GetSize() const { return image.GetDimensions(); }; @@ -29,16 +29,18 @@ public: private: void RegisterColormap(const Colormap& cm); -private: +protected: lol::Image image; lol::Texture2D* texture; lol::ObjectManager& manager; std::shared_ptr colormap; + glm::vec2 range; float offset = 0.0f; + +private: float heightFactor = 2.0f; bool renderColor = true; bool scroll = false; - glm::vec2 range; }; \ No newline at end of file diff --git a/vendor/lol b/vendor/lol index 9306dba..052e9f3 160000 --- a/vendor/lol +++ b/vendor/lol @@ -1 +1 @@ -Subproject commit 9306dbaae70b7b915e723ab9a084118f5a969bdf +Subproject commit 052e9f3a406e9c710a23258598505a31b9d5224a