Skip to content

Commit

Permalink
api improvements, mtron cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
yekm committed Jun 12, 2023
1 parent 89b8200 commit 2dcdcde
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 65 deletions.
21 changes: 13 additions & 8 deletions art.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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));
}
Expand All @@ -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;
}

Expand All @@ -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;
Expand Down
47 changes: 22 additions & 25 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand All @@ -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 ([email protected])
Expand All @@ -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);

Expand Down Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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,
Expand All @@ -211,18 +214,18 @@ 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;
pbo_index = pbo_index ? 0 : 1;
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,
Expand All @@ -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);
Expand Down
24 changes: 11 additions & 13 deletions mtron.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<<ICM;
yb=060000<<ICM; xb=0;
yc=0; xc=020000<<ICM;
Expand All @@ -32,18 +34,16 @@ void Minskytron::reinit() {
void Minskytron::dt(uint32_t *p, int x, int y, double o, uint32_t c) {
// keep 10 bits to wrap around 1024 screen pixels
#define SB (32-10)
x = (x>>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<maxdots_perframe; ++i) {
for (int i = 0; i<cycles; ++i) {
ya += (xa + xb) >> sh0;
xa -= (ya - yb) >> sh1;

Expand Down Expand Up @@ -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);
Expand Down
26 changes: 7 additions & 19 deletions mtron.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 2dcdcde

Please sign in to comment.