Background color not changing

Hello all,

I’m trying to change the background color in a GLFW generated window, but it’s not changing. I tried changing the values in glClearColor() and putting glClear() in various places, even encapsulating in a class method.

Code below is very similar to the code snippet in the GLFW docs.

GLFWwindow* window = glfwCreateWindow(640, 480, "My Title", NULL, NULL);
    if (!window)
    {
        // Window or OpenGL context creation failed
        std::cout << "Error creating window!" << std::endl;
        glfwTerminate();
        exit(EXIT_FAILURE);
    }

    glfwMakeContextCurrent(window);
    glfwSwapInterval(1);

    glClearColor(1.0f, 0.2f, 0.0f, 1.0f);

    while (!glfwWindowShouldClose(window)) 
    {
        int width, height;

        glfwGetFramebufferSize(window, &width, &height);

        glViewport(0, 0, width, height);
        glClear(GL_COLOR_BUFFER_BIT);



        glfwSwapBuffers(window);
        glfwPollEvents();
    }

Everything compiles and links well. I’m on Ubuntu 20.04, and when I tried a similar code on Visual Studio on Windows 10, everything worked perfectly.

I recompiled the GLFW library several times to determine if it was the problem.
I can’t draw anything because I can’t control color so I’m not sure how to go forward.

I would first try running some of the GLFW tests and examples by cloning and compiling the GitHub repository: GitHub - glfw/glfw: A multi-platform library for OpenGL, OpenGL ES, Vulkan, window and input

If those work you could then try this cmake GLFW starter which also sets the background colour.

It didn’t work at first, but once I installed the GLAD library, it finally worked. It was probably because the GL functions were from an older version of OpenGL on my system. GLAD updated those functions.