From ba028ae4dbbf93a461c9c9474889d04e640f64e0 Mon Sep 17 00:00:00 2001 From: Samuel Fadel Date: Sun, 4 Jun 2023 18:47:27 +0200 Subject: More fixes to rendering, same as original except for point outlines. --- main.cpp | 25 +++++++++++++++++------- scatterplot.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++---- scatterplot.h | 2 +- transitioncontrol.h | 2 -- transitionworkerthread.cpp | 13 ++++++++----- voronoisplat.cpp | 9 +++++---- 6 files changed, 75 insertions(+), 23 deletions(-) diff --git a/main.cpp b/main.cpp index 17c6597..0d5d8ab 100644 --- a/main.cpp +++ b/main.cpp @@ -718,6 +718,8 @@ int main(int argc, char *argv[]) // Visual components Colormap cpColormap, rpColormap; + // cpColormap.setOrientation(Colormap::Vertical); + // rpColormap.setOrientation(Colormap::Vertical); Scatterplot cpPlot, rpPlot; VoronoiSplat splat; cpPlot.setSize(512, 512); @@ -1197,22 +1199,29 @@ int main(int argc, char *argv[]) } nk_end(&ctx); + struct nk_style *s = &ctx.style; + nk_style_push_color(&ctx, &s->window.background, nk_rgba(0, 0, 0, 0)); + nk_style_push_style_item(&ctx, &s->window.fixed_background, + nk_style_item_color(nk_rgba(0,0,0,0))); if (nk_begin(&ctx, "Scatterplot", nk_rect(20, 20, splat.width(), splat.height()), - NK_WINDOW_BORDER | NK_WINDOW_NO_SCROLLBAR)) { - struct nk_command_buffer *canvas = nk_window_get_canvas(&ctx); - struct nk_rect region{0.0f, 0.0f, - static_cast(splat.width()), - static_cast(splat.height())}; - + NK_WINDOW_NO_SCROLLBAR)) { // Render components to their textures splat.draw(); rpPlot.draw(); cpPlot.draw(); + nk_layout_space_begin(&ctx, NK_STATIC, splat.height(), 4); + struct nk_rect region = nk_layout_space_bounds(&ctx); + // Add white background rect and draw textures on top - nk_layout_space_begin(&ctx, NK_STATIC, region.h, 4); nk_layout_space_push(&ctx, region); + struct nk_command_buffer *canvas = nk_window_get_canvas(&ctx); nk_fill_rect(canvas, region, 0.0f, nk_rgba(255, 255, 255, 255)); + + // Rest uses custom region + region.x = region.y = 0.0f; + region.w = static_cast(splat.width()); + region.h = static_cast(splat.height()); nk_layout_space_push(&ctx, region); nk_image(&ctx, splat_img); nk_layout_space_push(&ctx, region); @@ -1222,6 +1231,8 @@ int main(int argc, char *argv[]) nk_layout_space_end(&ctx); } nk_end(&ctx); + nk_style_pop_color(&ctx); + nk_style_pop_style_item(&ctx); if (nk_begin(&ctx, "Colormap", nk_rect(20, 40 + splat.height(), splat.width(), 120), NK_WINDOW_BORDER | NK_WINDOW_TITLE | NK_WINDOW_NO_SCROLLBAR)) { diff --git a/scatterplot.cpp b/scatterplot.cpp index a7194e8..64859b3 100644 --- a/scatterplot.cpp +++ b/scatterplot.cpp @@ -6,6 +6,9 @@ #include "colormap.h" #include "continuouscolorscale.h" +// Samples used in multisampling for rendering +static const size_t NUM_SAMPLES = 8; + // Glyphs settings static const Color DEFAULT_GLYPH_COLOR(255, 255, 255); static const float DEFAULT_GLYPH_SIZE = 8.0f; @@ -66,6 +69,7 @@ out vec4 FragColor; void main() { + gl_FragDepth = gl_PrimitiveID / 3000; FragColor = color; // FragColor = vec4(1.0); } @@ -113,6 +117,7 @@ out vec4 FragColor; void main() { + gl_FragDepth = gl_PrimitiveID / 2000; FragColor = vec4(0.0, 0.0, 0.0, 1.0); } )EOF"; @@ -166,6 +171,7 @@ Scatterplot::Scatterplot() , m_shouldUpdateMaterials(false) { glGenFramebuffers(1, &m_FBO); + glGenFramebuffers(1, &m_outFBO); glGenBuffers(1, &m_pointsVBO); glGenBuffers(1, &m_valuesVBO); @@ -179,9 +185,11 @@ Scatterplot::Scatterplot() glBindBuffer(GL_ARRAY_BUFFER, m_valuesVBO); glEnableVertexAttribArray(1); glVertexAttribPointer(1, 1, GL_FLOAT, GL_FALSE, 0, nullptr); - glVertexAttribDivisor(1, 1); + // glVertexAttribDivisor(1, 1); glBindVertexArray(0); + glGenTextures(1, &m_depthTex); + glGenTextures(1, &m_tex); glGenTextures(1, &m_outTex); m_shader = std::make_unique( @@ -208,6 +216,22 @@ void Scatterplot::setSize(size_t width, size_t height) m_width = width; m_height = height; + glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, m_tex); + glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, NUM_SAMPLES, + GL_RGBA, m_width, m_height, GL_TRUE); + // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, 0); + + glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, m_depthTex); + glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, NUM_SAMPLES, + GL_DEPTH_COMPONENT, m_width, m_height, GL_TRUE); + glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, 0); + glBindTexture(GL_TEXTURE_2D, m_outTex); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, m_width, m_height, 0, GL_RGBA, GL_FLOAT, 0); @@ -386,7 +410,9 @@ void Scatterplot::draw() glBindFramebuffer(GL_FRAMEBUFFER, m_FBO); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, - GL_TEXTURE_2D, m_outTex, 0); + GL_TEXTURE_2D_MULTISAMPLE, m_tex, 0); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, + GL_TEXTURE_2D_MULTISAMPLE, m_depthTex, 0); glViewport(0, 0, m_width, m_height); m_shader->use(); @@ -398,24 +424,37 @@ void Scatterplot::draw() glBindTexture(GL_TEXTURE_2D, m_colormapTex); m_shader->setUniform("colormap", 0); + glEnable(GL_DEPTH_TEST); glEnable(GL_BLEND); // glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); - glClear(GL_COLOR_BUFFER_BIT); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glBindVertexArray(m_VAO); glDrawArrays(GL_POINTS, 0, m_points.size()); m_shader->release(); // Swap out shaders to draw outline + glLineWidth(GLYPH_OUTLINE_WIDTH); m_shaderOutline->use(); m_shaderOutline->setUniform("transform", m_transform); m_shaderOutline->setUniform("size", m_glyphSize / m_width); m_shaderOutline->setUniform("colormap", 0); glDrawArrays(GL_POINTS, 0, m_points.size()); - glBindVertexArray(0); m_shaderOutline->release(); + glLineWidth(1.0f); + glDisable(GL_DEPTH_TEST); + + glBindFramebuffer(GL_FRAMEBUFFER, m_outFBO); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, + GL_TEXTURE_2D, m_outTex, 0); + + glBindFramebuffer(GL_READ_FRAMEBUFFER, m_FBO); + glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_outFBO); + glBlitFramebuffer(0, 0, m_width, m_height, + 0, 0, m_width, m_height, + GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT, GL_NEAREST); /* GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); diff --git a/scatterplot.h b/scatterplot.h index 5be1647..b7982bf 100644 --- a/scatterplot.h +++ b/scatterplot.h @@ -106,7 +106,7 @@ private: std::unique_ptr m_shader, m_shaderOutline; GLuint m_VAO, m_pointsVBO, m_valuesVBO; - GLuint m_FBO, m_colormapTex, m_outTex; + GLuint m_FBO, m_outFBO, m_colormapTex, m_depthTex, m_tex, m_outTex; GLfloat m_transform[4][4]; std::vector m_points; diff --git a/transitioncontrol.h b/transitioncontrol.h index 6b15679..6d088ef 100644 --- a/transitioncontrol.h +++ b/transitioncontrol.h @@ -1,8 +1,6 @@ #ifndef TRANSITIONCONTROL_H #define TRANSITIONCONTROL_H -#include - /* * This component emits signals indicating how far from its left edge is the * mouse since the mouse button was pressed (starting from t == 1.0 with 0.0 diff --git a/transitionworkerthread.cpp b/transitionworkerthread.cpp index 2ee1d0e..e002be6 100644 --- a/transitionworkerthread.cpp +++ b/transitionworkerthread.cpp @@ -1,13 +1,16 @@ +#include +#include + #include "transitionworkerthread.h" // The full duration (usecs) of the restoration animation static const double DURATION = 250000; -// The time to wait (usecs) before the next animation tick -static const double TICK_TIME = DURATION / 60.0; - // The amount to increase 't' per time step -static const double TICK_SIZE = TICK_TIME / DURATION; +static const double TICK_SIZE = 1.0 / 60.0; + +// The time to wait (usecs) before the next animation tick +static const std::chrono::microseconds TICK_TIME(DURATION * TICK_SIZE); TransitionWorkerThread::TransitionWorkerThread(TransitionControl *control) : m_control(control) @@ -28,7 +31,7 @@ void TransitionWorkerThread::run() while (t + TICK_SIZE < 1.0) { t += TICK_SIZE; m_control->setT(m_easing.valueForProgress(t)); - QThread::usleep(TICK_TIME); + std::this_thread::sleep_for(TICK_TIME); } m_control->setT(1.0); diff --git a/voronoisplat.cpp b/voronoisplat.cpp index 1bcab69..749ec42 100644 --- a/voronoisplat.cpp +++ b/voronoisplat.cpp @@ -142,8 +142,8 @@ void main() { discard; } else { vec4 accum = texelFetch(accumTex, ivec2(gl_FragCoord.xy), 0); - float value = accum.g > 0.0 ? accum.r / accum.g : 0.0; - // float value = (accum.g > 1.0) ? (accum.r - 1.0) / (accum.g - 1.0) : 0.0; + // float value = accum.g > 0.0 ? accum.r / accum.g : 0.0; + float value = (accum.g > 1.0) ? (accum.r - 1.0) / (accum.g - 1.0) : 0.0; fragColor = vec4(getRGB(value), 1.0 - dt / rad_max); } } @@ -359,7 +359,8 @@ void VoronoiSplat::resizeTextures() glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, m_width, m_height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL); + glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, m_width, m_height, 0, + GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, nullptr); glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_2D, m_outTex); @@ -470,7 +471,7 @@ void VoronoiSplat::draw() // glEnable(GL_POINT_SPRITE); glEnable(GL_PROGRAM_POINT_SIZE); glEnable(GL_BLEND); - glBlendFunc(GL_ONE, GL_ZERO); + glBlendFunc(GL_ONE, GL_ONE); glClearColor(1.0f, 1.0f, 1.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); -- cgit v1.2.3