Continuously render Gtk::GLArea in gtkmm

436 Views Asked by At

I've followed a gtkmm4 + opengl example, the window loads and displays without any errors and renders one frame but then seems to ignore my queue_render() and queue_draw() calls as evidenced by the print statements in the console. I've set auto render to true as well.

#include <iostream>
#include <string>

#include <gtkmm.h>
#include <giomm/resource.h>
#define GLEW_STATIC
#include <gl/glew.h>

class TestWindow : public Gtk::Window {
protected:
    Gtk::GLArea mGlArea;

    void onRealize() {
        std::cout << "onRealize()" << std::endl;

        mGlArea.make_current();

        glewExperimental = true;
        if (glewInit() != GLEW_OK) {
            std::cout << "ERROR: Failed to initialize GLEW" << std::endl;
        }

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

    void onUnrealize() {

    }

    bool onRender(const Glib::RefPtr<Gdk::GLContext>& context) {
        std::cout << "onRender()" << std::endl;

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        mGlArea.queue_render();
        mGlArea.queue_draw();
        return true;
    }
public:
    TestWindow() {
        set_title("Window Title");
        set_child(mGlArea);
        mGlArea.set_auto_render(true);
        mGlArea.set_size_request(800, 600);

        mGlArea.set_required_version(4, 0);

        mGlArea.signal_realize().connect(sigc::mem_fun(*this, &TestWindow::onRealize));
        mGlArea.signal_unrealize().connect(sigc::mem_fun(*this, &TestWindow::onUnrealize), false);
        mGlArea.signal_render().connect(sigc::mem_fun(*this, &TestWindow::onRender), false);

        mGlArea.show();
    }
};

int main(int argc, char* argv[]) {
    auto app = Gtk::Application::create("test.gtkmm");

    return app->make_window_and_run<TestWindow>(argc, argv);
}
2

There are 2 best solutions below

2
razcore-rad On

I'm facing issues with gtkmm-4.0 too with the OpenGL context. Unfortunately I haven't done any OpenGL development so I don't really understand what's happening.

You might have ran into this issue: https://gitlab.gnome.org/GNOME/gtk/-/issues/4950. I made some tests with your code on my NVidia GPU machine and I can confirm that GDK_DEBUG=gl-glx doesn't produce an error.

Although I don't know the implications of glewInit() not working because even with the init removed the app still runs and sets the color etc.

2
Lee Stripp On

I'm currently working on an example. Gtk::GLArea doesn't have many options. For me its opening a 4.6 core profile context (with debug support). No idea yet how to configure this.

Just cleaned up this example code. Working nicely now.

Hello Triangle gtkmm4 OpenGL Gtk::GLArea example - GitLab link