【问题标题】:OpenGL imgui slider rangesOpenGL imgui 滑块范围
【发布时间】:2019-10-13 18:31:09
【问题描述】:

我有一个使用 OpenGL 创建的 960x540 窗口。在这里渲染一个纹理并使用imgui 实现一个滑块,让我在运行时平移 x、y 和 z 位置。

int window_width = 960;
int window_height = 540;

window = glfwCreateWindow(window_width, window_height, "Object Oriented OpenGL", NULL, NULL);

首先我设置了滑块的默认值。

glm::vec3 model_matrix_translation(200, 200, 0);

稍后在我的循环中,只要窗口打开,就会逐帧渲染对象位置,我将 ImGui::SliderFloat3 v_min 和 v_max 参数设置为窗口的尺寸。看起来所有 3 个滑块 x、y 和 z 都使用这些值,但是有没有办法为每个滑块设置范围?如您所见,我的 y 轴滑块可以超过 540。

while (!glfwWindowShouldClose(window))
{
    // renderer and other code before this point
    {
        ImGui::SliderFloat3("Model Matrix Translation", &model_matrix_translation.x, 0.0f, 960.0f); 
    }
    ImGui::Render();
    ImGui_ImplGlfwGL3_RenderDrawData(ImGui::GetDrawData());

    glfwSwapBuffers(window); 
    glfwPollEvents();
}

【问题讨论】:

    标签: c++ opengl textures glfw imgui


    【解决方案1】:

    SliderFloat3 中的implementation 代表SliderScalarN,即implemented:

    bool ImGui::SliderScalarN(const char* label, ImGuiDataType data_type, void* v, int components, const void* v_min, const void* v_max, const char* format, float power)
    {
        ImGuiWindow* window = GetCurrentWindow();
        if (window->SkipItems)
            return false;
    
        ImGuiContext& g = *GImGui;
        bool value_changed = false;
        BeginGroup();
        PushID(label);
        PushMultiItemsWidths(components, CalcItemWidth());
        size_t type_size = GDataTypeInfo[data_type].Size;
        for (int i = 0; i < components; i++)
        {
            PushID(i);
            if (i > 0)
                SameLine(0, g.Style.ItemInnerSpacing.x);
            value_changed |= SliderScalar("", data_type, v, v_min, v_max, format, power);
            PopID();
            PopItemWidth();
            v = (void*)((char*)v + type_size);
        }
        PopID();
    
        const char* label_end = FindRenderedTextEnd(label);
        if (label != label_end)
        {
            SameLine(0, g.Style.ItemInnerSpacing.x);
            TextEx(label, label_end);
        }
    
        EndGroup();
        return value_changed;
    }
    

    出于您的目的,您将需要使用此函数的修改版本,将v_min 和v_max 与v 一起移动,因此在您修改后的函数中,将 for 循环的最后一行更改为:

    v = (void*)((char*)v + type_size);
    v_min = (void*)((char*)v_min + type_size);
    v_max = (void*)((char*)v_max + type_size);
    

    或者只是专门化整个函数来代替float*,然后你可以只用v++, v_min++, v_max++;代替。

    最后,您可以按如下方式调用修改后的函数:

    float mins[] = {0.0f, 0.0f, 0.0f};
    float maxs[] = {960.0f, 540.0f, 0.0f};
    MyModifiedSliderFloat3("Model Matrix Translation", &model_matrix_translation.x, mins, maxs);
    

    【讨论】:

      猜你喜欢
      • 2018-11-21
      • 2011-02-10
      • 2014-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-29
      • 2021-08-22
      相关资源
      最近更新 更多