GTK Rust and GLArea, gl function was not loaded

I am converting an application that needs to draw a complex 3d map in one area of the window.

I followed the Glium example and got a good working prototype, but had performance as glium doesn’t support glMultiDrawArrays or similar, so am trying to use a plain GLArea.

But as soon as I try to render anything. I get a panic

gl function was not loaded

I am creating the UI using composite template and binding to GLArea widget with

        #[template_child]
        gl_area: TemplateChild<GLArea>,

and connecting the renderring up as follows

    impl ObjectImpl for WorldMapView {
        fn constructed(&self) {
            self.parent_constructed();

            self.gl_area.connect_render( |area, context| unsafe {
                gl::ClearColor(0.0, 0.0, 1.0, 1.0);
                gl::Clear(gl::COLOR_BUFFER_BIT);
                Inhibit{ 0: false }
            });
        }
    }

It panics on the first gl call.
I have read some replies here saying GTK depends on OpenGL and the gl functions should already be loaded, but I have tried loading them as in the Glium example, but that, unsurprisingly, didn’t work.

No idea how to proceed now.

Unbelievably, I found a solution, but it doesn’t seem to be the best one. From the glium example, I copied and extended the initialization code

fn init_opengl() {
    #[cfg(target_os = "macos")]
        let library = unsafe { libloading::os::unix::Library::new("libepoxy.0.dylib") }.unwrap();
    #[cfg(all(unix, not(target_os = "macos")))]
        let library = unsafe { libloading::os::unix::Library::new("libepoxy.so.0") }.unwrap();
    #[cfg(windows)]
        let library = libloading::os::windows::Library::open_already_loaded("libepoxy-0.dll")
        .or_else(|_| libloading::os::windows::Library::open_already_loaded("epoxy-0.dll"))
        .unwrap();

    epoxy::load_with(|name| {
        unsafe { library.get::<_>(name.as_bytes()) }
            .map(|symbol| *symbol)
            .unwrap_or(ptr::null())
    });
    gl::load_with(|name| {
        epoxy::get_proc_addr(name)
    });
}

Hope it may help someone else.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.