epezent / implot

Immediate Mode Plotting
MIT License
4.79k stars 531 forks source link

ImGuiBackendFlags_RendererHasVtxOffset problem in rendering large data points #291

Closed asjfowe closed 3 years ago

asjfowe commented 3 years ago

I am trying to develop a widget that displays a large number of points greater than 100,000 points using PlotScatter or PlotLine. During the development, I used two monitors. My laptop screen and external monitor.

So the program worked fine when I used the external screen as my main screen. It displayed the plot nicely and the program is working fine. However, when I switched my main screen to the laptop screen. A warning message showed up in the ImPlot::ShowDemoWindow(). (WARNING: ImDrawIdx is 16-bit and ImGuiBackendFlags_RendererHasVtxOffset is false. Expect visual glitches and artifacts! See README for more information.).

So, I did some research and find that I could use ImGuiBackendFlags_RendererHasVtxOffset flag to solve the issue. The program was able to load the data but with some glitches. So my question is what is exactly causing the problem and I would like to have some support.

The plot when using my monitor as my main screen and ImGuiBackendFlags_RendereHasVtxOffset is 0. image

The plot when using my laptop screen and ImGuiBackendFlags_RenderHasVtxOffset is 1 because when it is set to 0 the program won't work. image

However, when I zoom in on the previous screenshot it seems to be working fine. image

I am using ImGui 1.84.2

My main file is as follow

#include <iostream>
#include <string>

#include "imgui.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"

#include "implot.h"

#include <GLFW/glfw3.h>

#if defined(_MSC_VER) && (_MSC_VER >= 1900) && !defined(IMGUI_DISABLE_WIN32_FUNCTIONS)
#pragma comment(lib, "legacy_stdio_definitions")
#endif

#include "Data.cpp"
#include "Range.cpp"

#include "Helpers.h"
#include "Constants.h"

static void glfw_error_callback(int error, const char* description)
{
    fprintf(stderr, "Glfw Error %d: %s\n", error, description);
}

int main() {
    // Set the reading and writing precision to 
    std::cout.precision(DOUBLE_PRECISION);

    // Reading the data as Data object
    std::string fileName = "ExampleSet.csv";
    Data data = parseCSVFile(fileName);
    Data normalData = data;
    std::vector<Range> ranges;

    // Setup window
    glfwSetErrorCallback(glfw_error_callback);
    if (!glfwInit())
        return 1;

    // GL 3.0 + GLSL 130
    const char* glsl_version = "#version 130";
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);

    // Create window with graphics context
    GLFWwindow* window = glfwCreateWindow(1280, 720, "Range Selector", NULL, NULL);
    if (window == NULL)
        return 1;
    glfwMakeContextCurrent(window);
    glfwSwapInterval(1); // Enable vsync

    // Setup Dear ImGui context and ImPlot context
    IMGUI_CHECKVERSION();
    ImGui::CreateContext();
    ImPlot::CreateContext();
    ImGuiIO& io = ImGui::GetIO(); (void)io;
    io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset;

    // Setup Dear ImGui style
    ImGui::StyleColorsDark();
    ImPlot::GetStyle().UseLocalTime;

    // Setup Platform/Renderer backends
    ImGui_ImplGlfw_InitForOpenGL(window, true);
    ImGui_ImplOpenGL3_Init(glsl_version);

    // ImGui window values
    ImVec4 backgroundColor = ImVec4(0.1f, 0.1f, 0.1f, 1.00f);

    double* a = &normalData.columns.at(0).values[0];

    while (!glfwWindowShouldClose(window)) {

            glfwPollEvents();

            // Start the Dear ImGui frame
            ImGui_ImplOpenGL3_NewFrame();
            ImGui_ImplGlfw_NewFrame();
            ImGui::NewFrame();
            //ImPlot::ShowDemoWindow();

            if (ImPlot::BeginPlot("Test Plot", NULL, NULL, ImVec2(-1, 0), NULL, ImPlotAxisFlags_Time)) {
                for (int i = 0; i < normalData.numberOfColumns - 1; i++) {
                    ImPlot::PlotLine("testaaaa" + i, &normalData.columns.at(6).values[0], &normalData.columns.at(i).values[0], normalData.numberOfRows);
                }
                ImPlot::EndPlot();
            }
            ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);

            // Rendering
            ImGui::Render();
            int display_w, display_h;
            glfwGetFramebufferSize(window, &display_w, &display_h);
            glViewport(0, 0, display_w, display_h);
            glClearColor(backgroundColor.x * backgroundColor.w, backgroundColor.y * backgroundColor.w, backgroundColor.z * backgroundColor.w, backgroundColor.w);
            glClear(GL_COLOR_BUFFER_BIT);
            ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
            glfwSwapBuffers(window);
            glfwWaitEvents();
    }
}
asjfowe commented 3 years ago

never mind I just found this issue #237 and it solve my problem ^_^