OpenGL + gtkglextmm + glade

孤街醉人 提交于 2020-01-01 19:02:15

问题


when I am programming with "gtkmm", there is a widget "Gtk::DrawingArea".
I can program that widget "by hand" (so write the code) or more elegant way is to use "glade" user interface designer, where I can do the same "graphically".

Now I am trying to connect OpenGL with gtkmm through "gtkglextmm" library. In that library, there is a widget "Gtk::GL::DrawingArea" - but this widget "IS NOT" available in glade.

So is there any way to program with "OpenGL + gtkglextmm" using "glade" (for the "graphical user interface" part)?

Thanks.


回答1:


First of all libglade is an old library. If you are writing new project start with gtk builder.

As you can see here gtkmm provide easy way to create your own widgets and see them (almost) in glade tool. You simply insert plain DrawinArea widget to window and then tell gtk-builder to put in this place yours derived class.

Here is all together:

Setting up gtk-builder:

refBuilder = Gtk::Builder::create_from_file(ui_file);

GlDrawingArea*glArea = NULL;
refBuilder->get_widget_derived("drawing_gl",glArea);

Opengl DrawingArea class:

 class GlDrawingArea : public Gtk::DrawingArea ,
                       public Gtk::GL::Widget<GlDrawingArea>
 {                   
 public:             
         GlDrawingArea(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& builder);
         virtual ~GlDrawingArea();
 protected:          
         void on_realize();
         bool on_expose_event(GdkEventExpose* event);
         bool on_configure_event(GdkEventConfigure* event);          
 private:
         Glib::RefPtr<Gtk::Builder> refBuilder;
 };

Constructing opengl drawingarea:

// GlDrawingArea:
GlDrawingArea::GlDrawingArea(BaseObjectType*cobject, const Glib::RefPtr<Gtk::Builder>& builder)
        : Gtk::DrawingArea(cobject),
          refBuilder(builder),
          screen_tex(0)
{                                                                                               
        //
        // Configure OpenGL-capable visual.
        //
        Glib::RefPtr<Gdk::GL::Config> glconfig;

        // Try double-buffered visual
        glconfig = Gdk::GL::Config::create(Gdk::GL::MODE_RGB    |
                                           Gdk::GL::MODE_DEPTH  |
                                           Gdk::GL::MODE_DOUBLE);
        if (!glconfig) {
                std::cerr << "*** Cannot find the double-buffered visual.\n"
                          << "*** Trying single-buffered visual.\n";

                // Try single-buffered visual
                glconfig = Gdk::GL::Config::create(Gdk::GL::MODE_RGB |Gdk::GL::MODE_DEPTH);
                if (!glconfig) {
                        std::cerr << "*** Cannot find any OpenGL-capable visual.\n";
                        std::exit(1);
                }
        }

        // print frame buffer attributes.
        GLConfigUtil::examine_gl_attrib(glconfig);

        //
        // Set OpenGL-capability to the widget.
        //
        set_gl_capability(glconfig);
}


来源:https://stackoverflow.com/questions/1904481/opengl-gtkglextmm-glade

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!