Gtk,Glade和OpenGL问题

时间:2012-02-07 17:56:35

标签: opengl gtk glade

我在Gtk +,Glade和GtkGlext方面遇到了一些问题。

代码:

#include <gtk-2.0/gtk/gtk.h>
#include <gtkglext-1.0/gtk/gtkgl.h>
#include <GL/gl.h>

GtkBuilder* mBuilder;
GtkWidget* mWindow;
GtkWidget* mAbout;
GtkWidget* mGlArea;
GdkGLConfig *mGlConfig;
GdkGLConfigMode mGlConfigMode;

extern "C" G_MODULE_EXPORT void on_window_destroy(GtkObject *object, gpointer user_data)
{
    gtk_main_quit();
}

extern "C" G_MODULE_EXPORT void on_menuHelpAbout_activate(GtkObject *object, gpointer user_data)
{            
    gtk_widget_show(mAbout);
}

extern "C" G_MODULE_EXPORT void on_aboutdialog1_close(GtkObject *object, gpointer user_data)
{
    gtk_widget_hide(mAbout);
}

extern "C" G_MODULE_EXPORT void configure(GtkWidget *drawarea, GdkEventConfigure *event, gpointer user_data)
{
    printf("%x\n", drawarea);
    GdkGLContext *mGlContext = gtk_widget_get_gl_context(drawarea);
    GdkGLDrawable *mGlDrawable = gtk_widget_get_gl_drawable(drawarea);

    gdk_gl_drawable_gl_begin(mGlDrawable, mGlContext);

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

    gdk_gl_drawable_gl_end(mGlDrawable);
}

extern "C" G_MODULE_EXPORT void expose(GtkWidget *drawarea, GdkEventExpose *event, gpointer user_data)
{
    GdkGLContext *mGlContext = gtk_widget_get_gl_context(drawarea);
    GdkGLDrawable *mGlDrawable = gtk_widget_get_gl_drawable(drawarea);

    gdk_gl_drawable_gl_begin(mGlDrawable, mGlContext);

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
    glLoadIdentity();

    gdk_gl_drawable_swap_buffers(mGlDrawable);

    gdk_gl_drawable_gl_end(mGlDrawable);
}

int main(int argc, char *argv[])
{
    gtk_init(&argc, &argv);
    gtk_gl_init(&argc, &argv);

    mBuilder = gtk_builder_new();
    gtk_builder_add_from_file(mBuilder, "editor.xml", NULL);
    mWindow = GTK_WIDGET(gtk_builder_get_object(mBuilder, "window"));
    mAbout = GTK_WIDGET(gtk_builder_get_object(mBuilder, "aboutdialog1"));
    mGlArea = GTK_WIDGET(gtk_builder_get_object(mBuilder, "drawingarea1"));
    gtk_widget_set_events(mGlArea, GDK_EXPOSURE_MASK);
    gtk_builder_connect_signals(mBuilder, NULL);

    g_object_unref(G_OBJECT(mBuilder));

    gtk_widget_show(mWindow);

    if(gtk_widget_get_realized(mGlArea))
    {
        gtk_widget_unrealize(mGlArea);
    }

    mGlConfigMode = (GdkGLConfigMode)(GDK_GL_MODE_RGB | GDK_GL_MODE_DEPTH | GDK_GL_MODE_DOUBLE | GDK_GL_MODE_STENCIL);

    mGlConfig = gdk_gl_config_new_by_mode(mGlConfigMode);
    if(!mGlConfig)
    {
        printf("Error: Can't get OpenGL configuration\n");
    }

    if(!gtk_widget_set_gl_capability(mGlArea, mGlConfig, NULL, TRUE, GDK_GL_RGBA_TYPE))
    {
        printf("Error: Can't set OpenGL capability to drawable area\n");
    }

    g_signal_connect(mGlArea, "configure-event", G_CALLBACK(configure), NULL);
    g_signal_connect(mGlArea, "expose-event", G_CALLBACK(expose), NULL);

    gtk_widget_show_all(mWindow);

    gtk_main();

    return 0;
}

我实际上不知道为什么我在第30行遇到分段错误,因为地址似乎有效并且OpenGL初始化成功。有人能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:0)

Yup和问题解决了...问题不在代码中,而是在编译和链接阶段。我的libgtkglext-x11-1.0有点腐败(gdb尖叫,XVisualIDFromVisual做了SIGSEGV),所以我认为它可能已经损坏并重新编译了它。