1

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.

genpfault
  • 51,148
  • 11
  • 85
  • 139

1 Answers1

1

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.

Blindy
  • 65,249
  • 10
  • 91
  • 131
  • If I were to use the OpenGL2 header instead, would that work better? If so, how would I fix it in that case? – Undesignated Mar 08 '22 at 16:51
  • Better? No, never use the fixed pipeline. It would however work with your obsolete code: https://github.com/ocornut/imgui/blob/master/examples/example_glut_opengl2/main.cpp – Blindy Mar 08 '22 at 17:10
  • Ok, I tried switching to OpenGL2, but it still doesn't draw anything. I would prefer to keep using GLUT simply because its friendlier to a beginner like me. Anything else I could do? – Undesignated Mar 08 '22 at 17:22
  • Is it friendlier? You seem to not be able to draw anything. And I linked you the official imgui glut example a little bit above, are you having problems even with that? – Blindy Mar 08 '22 at 17:35
  • I can draw things when I'm not using ImGUI, and when I am using ImGUI, it only shows the ImGUI window. I'm still having problems with that. I utilised that code you linked in my code above. – Undesignated Mar 08 '22 at 22:12