使用 ImGUI 时 GLUT 不绘制形状

发布于 2025-01-13 00:53:33 字数 4420 浏览 0 评论 0原文

我使用的是 Windows 10、Visual Studio 2022。 后端:imgui_impl_glut.cpp 和 imgui_impl_opengl3.cpp

现在,我只是想让 freeGLUT 和 ImGUI 正常协同工作。问题是,当我使用 ImGUI 创建窗口时,它不显示 GLUT 绘制的形状。没有 ImGUI,它可以完美地绘制球体。

我尝试在 ImGui 渲染周期之后调用球体的渲染程序,但这没有起作用。这是我的代码:

#include <iostream>
#include <GLEW/glew.h>
#include <GL/freeglut.h>
#pragma comment(lib, "glew32.lib")


#include "imgui/imgui.h"
#include "imgui/imgui_impl_glut.h"
#include "imgui/imgui_impl_opengl3.h"

#ifdef _MSC_VER
#pragma warning (disable: 4505) // unreferenced local function has been removed
#endif

static bool show_demo_window = true;
static bool show_another_window = false;
static ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);

   
void Window(int w, int h) {
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glFrustum(-5.0, 5.0, -5.0, 5.0, 5.0, 200.0);
    glMatrixMode(GL_MODELVIEW);
}



void my_display_code()
{


    // Sample code taken from the ImGUI example files
    {
        static float f = 0.0f;
        static int counter = 0;

        ImGui::Begin("Hello, world!");                          // Create a window called "Hello, world!" and append into it.

        ImGui::Text("This is some useful text.");               // Display some text (you can use a format strings too)
        ImGui::Checkbox("Demo Window", &show_demo_window);      // Edit bools storing our window open/close state
        ImGui::Checkbox("Another Window", &show_another_window);

        ImGui::SliderFloat("float", &f, 0.0f, 1.0f);            // Edit 1 float using a slider from 0.0f to 1.0f
        ImGui::ColorEdit3("clear colour", (float*)&clear_color); // Edit 3 floats representing a color

        if (ImGui::Button("Button"))                            // Buttons return true when clicked (most widgets return true when edited/activated)
            counter++;
        ImGui::SameLine();
        ImGui::Text("counter = %d", counter);

        ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
        ImGui::End();
    }

    // Other sample window
    if (show_another_window)
    {
        ImGui::Begin("Another Window", &show_another_window);   // Pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked)
        ImGui::Text("Hello from another window!");
        if (ImGui::Button("Close Me"))
            show_another_window = false;
        ImGui::End();
    }
}




//Function where the rendering occurs
void Draw(void) {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();


    my_display_code();

    //Rendering
    ImGui::Render();
    ImGuiIO& io = ImGui::GetIO();

    glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w);

    ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());


    gluLookAt(5.0f, 35.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);

    OrbitPath();

    GLUquadric* quadric;
    quadric = gluNewQuadric();

    //Drwaing the Sun
    glPushMatrix();
    glRotatef(0.0, 0.0, 1.0, 0.0);
    glTranslatef(0.0, 0.0f, 0.0);
    glPushMatrix();
    glRotatef(0, 1.0, 0.0, 0.0);
    glRotatef(10, 0.0, 1.0, 0.0);
    glColor3f(1.0f, 1.0f, 1.0f);
    gluQuadricTexture(quadric, 1);
    gluSphere(quadric, 5, 10.0, 10.0);
    glPopMatrix();
    glPopMatrix();

    glutPostRedisplay();
    glutSwapBuffers();
}


//Main function
int main(int argc, char** argv) {
    glutInit(&argc, argv);

    glutInitContextVersion(4, 2);
    glutInitContextProfile(GLUT_COMPATIBILITY_PROFILE);

    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA | GLUT_DEPTH);
    glutInitWindowSize(700, 700);
    glutInitWindowPosition(500, 0);
    glutCreateWindow("Example");
    glutDisplayFunc(Draw);
    glutReshapeFunc(Window);

/**/
    IMGUI_CHECKVERSION();
    ImGui::CreateContext();
    ImGuiIO& io = ImGui::GetIO(); (void)io;

    ImGui::StyleColorsDark();



    ImGui_ImplGLUT_Init();
    ImGui_ImplGLUT_InstallFuncs();
    ImGui_ImplOpenGL3_Init();
/**/

    glewExperimental = GL_TRUE;
    glewInit();

    glClearColor(0.0, 0.0, 0.0, 0.0);
    glEnable(GL_DEPTH_TEST);
    glutMainLoop();

    ImGui_ImplOpenGL3_Shutdown();
    ImGui_ImplGLUT_Shutdown();
    ImGui::DestroyContext();
}

使用 ImGUI 1.87。

I'm using Windows 10, visual studio 2022.
Backends: imgui_impl_glut.cpp and imgui_impl_opengl3.cpp

For now, i'm simply trying to get freeGLUT and ImGUI to work together properly. The problem is that when I create a window with ImGUI, it doesn't show the shape that GLUT draws. Without ImGUI, it draws a sphere perfectly well.

I've tried calling my rendering procedure for the sphere after the ImGui render cycle, but that hasn't worked. Here is my code:

#include <iostream>
#include <GLEW/glew.h>
#include <GL/freeglut.h>
#pragma comment(lib, "glew32.lib")


#include "imgui/imgui.h"
#include "imgui/imgui_impl_glut.h"
#include "imgui/imgui_impl_opengl3.h"

#ifdef _MSC_VER
#pragma warning (disable: 4505) // unreferenced local function has been removed
#endif

static bool show_demo_window = true;
static bool show_another_window = false;
static ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);

   
void Window(int w, int h) {
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glFrustum(-5.0, 5.0, -5.0, 5.0, 5.0, 200.0);
    glMatrixMode(GL_MODELVIEW);
}



void my_display_code()
{


    // Sample code taken from the ImGUI example files
    {
        static float f = 0.0f;
        static int counter = 0;

        ImGui::Begin("Hello, world!");                          // Create a window called "Hello, world!" and append into it.

        ImGui::Text("This is some useful text.");               // Display some text (you can use a format strings too)
        ImGui::Checkbox("Demo Window", &show_demo_window);      // Edit bools storing our window open/close state
        ImGui::Checkbox("Another Window", &show_another_window);

        ImGui::SliderFloat("float", &f, 0.0f, 1.0f);            // Edit 1 float using a slider from 0.0f to 1.0f
        ImGui::ColorEdit3("clear colour", (float*)&clear_color); // Edit 3 floats representing a color

        if (ImGui::Button("Button"))                            // Buttons return true when clicked (most widgets return true when edited/activated)
            counter++;
        ImGui::SameLine();
        ImGui::Text("counter = %d", counter);

        ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
        ImGui::End();
    }

    // Other sample window
    if (show_another_window)
    {
        ImGui::Begin("Another Window", &show_another_window);   // Pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked)
        ImGui::Text("Hello from another window!");
        if (ImGui::Button("Close Me"))
            show_another_window = false;
        ImGui::End();
    }
}




//Function where the rendering occurs
void Draw(void) {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();


    my_display_code();

    //Rendering
    ImGui::Render();
    ImGuiIO& io = ImGui::GetIO();

    glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w);

    ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());


    gluLookAt(5.0f, 35.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);

    OrbitPath();

    GLUquadric* quadric;
    quadric = gluNewQuadric();

    //Drwaing the Sun
    glPushMatrix();
    glRotatef(0.0, 0.0, 1.0, 0.0);
    glTranslatef(0.0, 0.0f, 0.0);
    glPushMatrix();
    glRotatef(0, 1.0, 0.0, 0.0);
    glRotatef(10, 0.0, 1.0, 0.0);
    glColor3f(1.0f, 1.0f, 1.0f);
    gluQuadricTexture(quadric, 1);
    gluSphere(quadric, 5, 10.0, 10.0);
    glPopMatrix();
    glPopMatrix();

    glutPostRedisplay();
    glutSwapBuffers();
}


//Main function
int main(int argc, char** argv) {
    glutInit(&argc, argv);

    glutInitContextVersion(4, 2);
    glutInitContextProfile(GLUT_COMPATIBILITY_PROFILE);

    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA | GLUT_DEPTH);
    glutInitWindowSize(700, 700);
    glutInitWindowPosition(500, 0);
    glutCreateWindow("Example");
    glutDisplayFunc(Draw);
    glutReshapeFunc(Window);

/**/
    IMGUI_CHECKVERSION();
    ImGui::CreateContext();
    ImGuiIO& io = ImGui::GetIO(); (void)io;

    ImGui::StyleColorsDark();



    ImGui_ImplGLUT_Init();
    ImGui_ImplGLUT_InstallFuncs();
    ImGui_ImplOpenGL3_Init();
/**/

    glewExperimental = GL_TRUE;
    glewInit();

    glClearColor(0.0, 0.0, 0.0, 0.0);
    glEnable(GL_DEPTH_TEST);
    glutMainLoop();

    ImGui_ImplOpenGL3_Shutdown();
    ImGui_ImplGLUT_Shutdown();
    ImGui::DestroyContext();
}

Using ImGUI 1.87.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听不够的曲调 2025-01-20 00:53:33

您正在混合和匹配(或者更确切地说,不匹配)imgui 正在使用的现代模块化管道(注意 OpenGL3 标头)和过时的固定管道过剩(以及您自己)正在使用。

只是不要使用过剩,而是使用现代管道。

You're mixing and matching (or rather, not matching) the modern, modular pipeline that imgui is using (note the OpenGL3 header) and the obsolete fixed pipeline glut (and yourself as well) are using.

Simply don't use glut and use the modern pipeline instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文