From 2dcdcde35343843e34f2c13fa46f0f7a0b928ffc Mon Sep 17 00:00:00 2001 From: Pavel V Date: Mon, 12 Jun 2023 15:47:30 +0300 Subject: [PATCH] api improvements, mtron cleaning --- art.hpp | 21 +++++++++++++-------- main.cpp | 47 ++++++++++++++++++++++------------------------- mtron.cpp | 24 +++++++++++------------- mtron.hpp | 26 +++++++------------------- 4 files changed, 53 insertions(+), 65 deletions(-) diff --git a/art.hpp b/art.hpp index b39663e..ac34e8a 100644 --- a/art.hpp +++ b/art.hpp @@ -22,13 +22,14 @@ class Art { Art(std::string _name) : m_name(_name) {} const char * name() {return m_name.c_str();} - void resized(int _w, int _h) { + void resized(int _w, int _h) { // nb: old buffers -> forbidden to drawdot() + tex_w = _w, tex_h = _h; resize(_w, _h); } bool gui() { if (pb) ScrollableSliderUInt("Max pixels", &pixel_buffer_maximum, 1, pixel_buffer_maximum_max, "%d", pixel_buffer_maximum/16); - bool up = render_gui(); + bool resize_pbo = render_gui(); ImGui::Text("pixels drawn %d, discarded %d", pixels_drawn, pixels_discarded); @@ -37,12 +38,12 @@ class Art { ImGui::Text("pixel_buffer_size %d", pb->buffer.size()); - return up; + return resize_pbo; } void draw(uint32_t *p) { bool direct = render(p); if (pb) { - pb->trunc(pixel_buffer_maximum); + pb->erase_old(pixel_buffer_maximum); render_pixel_buffer(p); } else if (!direct) { std::copy(pixels.begin(), pixels.end(), p); @@ -51,8 +52,6 @@ class Art { virtual void load(std::string json) {}; virtual std::string save() { return ""; }; - virtual bool override_texture_size(int &w, int &h) { return false; }; - void drawdot(uint32_t x, uint32_t y, double o, uint32_t c) { drawdot(x, y, c | ((unsigned)(0xff*o)<<24)); } @@ -61,12 +60,16 @@ class Art { drawdot(data(), x, y, c); } + void drawdot(uint32_t *screen, uint32_t x, uint32_t y, double o, uint32_t c) { + drawdot(screen, x, y, c | ((unsigned)(0xff*o)<<24)); + } + void drawdot(uint32_t *screen, uint32_t x, uint32_t y, uint32_t c) { - if (x >= w || y >= h) { + if (x >= tex_w || y >= tex_h) { ++pixels_discarded; return; } - screen[ y*w + x ] = c; + screen[ y*tex_w + x ] = c; ++pixels_drawn; } @@ -82,6 +85,8 @@ class Art { pixels_drawn = 0; pixels_discarded = 0; } + + int tex_w, tex_h; private: virtual void resize(int _w, int _h) {default_resize(_w, _h);}; virtual bool render_gui() = 0; diff --git a/main.cpp b/main.cpp index a723419..5c5ca40 100644 --- a/main.cpp +++ b/main.cpp @@ -36,7 +36,7 @@ static void } static GLFWwindow* window; -static int tex_w, tex_h, texture_size; +static int texture_size; static int sw = 1024, sh = 1024; @@ -63,9 +63,7 @@ int pbo_index = 0; ImVec4 clear_color = ImVec4(0, 0, 0, 1.00f); void make_pbos() { - if (!art->override_texture_size(tex_w, tex_h)) - tex_w = sw, tex_h = sh; - texture_size = tex_w * tex_h * 4; + texture_size = art->tex_w * art->tex_h * 4; image_data = (uint32_t*)xrealloc(image_data, texture_size); // AUTHOR: Song Ho Ahn (song.ahn@gmail.com) @@ -81,7 +79,7 @@ void make_pbos() { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexImage2D(GL_TEXTURE_2D, - 0, GL_RGBA, tex_w, tex_h, + 0, GL_RGBA, art->tex_w, art->tex_h, 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*)image_data); glBindTexture(GL_TEXTURE_2D, 0); @@ -159,10 +157,6 @@ int main(int argc, char *argv[]) glfwSwapInterval(vsync); - ArtFactory af; - art = af.get_art(); - - IMGUI_CHECKVERSION(); ImGui::CreateContext(); ImGuiIO& io = ImGui::GetIO(); (void)io; @@ -175,6 +169,11 @@ int main(int argc, char *argv[]) ImGui_ImplGlfw_InitForOpenGL(window, true); ImGui_ImplOpenGL3_Init(glsl_version); + + + ArtFactory af; + art = af.get_art(); + get_window_size(0,0); art->resized(sw, sh); make_pbos(); @@ -193,15 +192,19 @@ int main(int argc, char *argv[]) if (af.render_gui()) { art = af.get_art(); + art->resized(sw, sh); destroy_pbos(); make_pbos(); - art->resized(sw, sh); } - ImGui::ColorEdit4("Clear color", (float*)&clear_color); - ScrollableSliderUInt("force clear every N frames", &art->clear_every, 0, 1024, "%d", 2); + if (ImGui::CollapsingHeader("Clear Configuration")) + { + + ScrollableSliderUInt("force clear every N frames", &art->clear_every, 0, 1024, "%d", 2); + ImGui::ColorEdit4("Clear color", (float*)&clear_color); + } - bool reinit = art->gui(); + bool resize_pbo = art->gui(); ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, @@ -211,10 +214,10 @@ int main(int argc, char *argv[]) ImGui::End(); - if (get_window_size(0,0)) { + if (get_window_size(0,0) || resize_pbo) { + art->resized(sw, sh); destroy_pbos(); make_pbos(); - art->resized(sw, sh); } int nexti = pbo_index; @@ -222,7 +225,7 @@ int main(int argc, char *argv[]) glBindTexture(GL_TEXTURE_2D, image_texture); glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pboIds[pbo_index]); glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, - tex_w, tex_h, GL_RGBA, GL_UNSIGNED_BYTE, 0); + art->tex_w, art->tex_h, GL_RGBA, GL_UNSIGNED_BYTE, 0); glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pboIds[nexti]); glBufferData(GL_PIXEL_UNPACK_BUFFER, texture_size, @@ -233,18 +236,12 @@ int main(int argc, char *argv[]) glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER); glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); - //ImGui::GetBackgroundDrawList()->AddImage((void*)(intptr_t)image_texture, - // ImVec2(0, 0), ImVec2(tex_w, tex_h)); - - int display_w, display_h; - glfwGetFramebufferSize(window, &display_w, &display_h); ImGui::GetBackgroundDrawList()->AddImage((void*)(intptr_t)image_texture, - ImVec2(0, 0), ImVec2(display_w, display_h), - ImVec2(0, 0), ImVec2((float)display_w/tex_w, (float)display_h/tex_h)); + ImVec2(0, 0), ImVec2(sw, sh), + ImVec2(0, 0), ImVec2((float)sw/art->tex_w, (float)sh/art->tex_h)); ImGui::Render(); - //glViewport(0, 0, sw, sh); - glViewport(0, 0, display_w, display_h); + glViewport(0, 0, sw, sh); glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w); glClear(GL_COLOR_BUFFER_BIT); diff --git a/mtron.cpp b/mtron.cpp index d93ecd9..21059cf 100644 --- a/mtron.cpp +++ b/mtron.cpp @@ -10,7 +10,9 @@ #include "mtron.hpp" -void Minskytron::reinit() { +void Minskytron::resize(int _w, int _h) { + tex_w = tex_h = 1 << tex_power; + ya=0; xa=0737777<>SB) + W/2; - y = (y>>SB) + H/2; + x = (x >> (32 - tex_power)) + tex_w/2; + y = (y >> (32 - tex_power)) + tex_h/2; - p[ y*W + x ] = c | ((unsigned)(0xff*o)<<24); + drawdot(p, x, y, o, c); } bool Minskytron::render(uint32_t *p) { - //clear(); - //std::fill(p, p+TEXTURE_SIZE, 0); - memset(p, 0, TEXTURE_SIZE); + memset(p, 0, tex_w*tex_h*4); - for (int i = 0; i> sh0; xa -= (ya - yb) >> sh1; @@ -80,15 +80,13 @@ bool Minskytron::render(uint32_t *p) { bool Minskytron::render_gui() { bool up = false; + up |= ScrollableSliderInt("Texture power", &tex_power, 1, 16, "%d", 1); ScrollableSliderInt("Max dots", &maxodots, 1024, 1024*16, "%d", 256); - ScrollableSliderInt("Dots per frame", &maxdots_perframe, 0, 4096, "%d", 8); + ScrollableSliderInt("Cycles", &cycles, 0, 4096, "%d", 8); ScrollableSliderInt("Dots clamped gamma", &dots_clamped, 0, maxodots, "%d", 8); ScrollableSliderFloat("Gamma", &gm, -8, 8, "%.2f", 0.2); - if (BitField("Test word", &tb, 0)) - reinit(); - - ImGui::ColorEdit3("clear color", (float*)&clear_color); + up |= BitField("Test word", &tb, 0); ImGui::ColorEdit3("osc1 color", (float*)&ocolor1); ImGui::ColorEdit3("osc2 color", (float*)&ocolor2); diff --git a/mtron.hpp b/mtron.hpp index 4bba2a7..fdc0a1b 100644 --- a/mtron.hpp +++ b/mtron.hpp @@ -12,18 +12,14 @@ class Minskytron : public Art { public: Minskytron() - : Art("Minskytron") { reinit(); } - virtual bool override_texture_size(int &_w, int &_h) { - _w = W; _h = H; - return true; - } + : Art("Minskytron") {} private: virtual bool render_gui() override; //virtual void resize(int _w, int _h) override; virtual bool render(uint32_t *p) override; - void reinit(); + virtual void resize(int _w, int _h) override; void dt(uint32_t *p, int x, int y, double o, uint32_t c); struct odot { @@ -34,19 +30,14 @@ class Minskytron : public Art { int maxodots = 1024*6; int filler_sleep = 100; - int maxdots_perframe = 64; + int cycles = 64; //float gm = -2.5; float gm = 3.5; - int dots_clamped = 64; - ImVec4 ocolor1 = ImVec4(1, 0, 0, 0); + int dots_clamped = 512; + ImVec4 ocolor1 = ImVec4(1, 1, 0, 0); ImVec4 ocolor2 = ImVec4(0, 1, 0, 0); - ImVec4 ocolor3 = ImVec4(0, 0, 1, 0); - - int density = 32, cycles=0; - ImVec4 clear_color = ImVec4(1, 0, 0, 1.00f); - ImVec4 background = ImVec4(0, 0, 0, 1); - ImVec4 foreground = ImVec4(0, 1, 0, 1); - + ImVec4 ocolor3 = ImVec4(0, 1, 1, 0); + int tex_power = 10; //unsigned int tb = 0b011000111001110011100010000010; unsigned int tb = 0b001100011100111001110001000001; // original @@ -59,9 +50,6 @@ class Minskytron : public Art { int sh0, sh1, sh2, sh3, sh4, sh5; - static constexpr int W = 1024; - static constexpr int H = 1024; - static constexpr int TEXTURE_SIZE = W*H*4; // constatnt shift add static constexpr int CSA = 1; // initial constant multiplier