int init_opengl_gdk_3Demu( GdkDrawable * drawable) { GdkGLConfig *glconfig; /* create the off screen pixmap */ target_pixmap = gdk_pixmap_new ( drawable, 256, 192, -1); if ( target_pixmap == NULL) { g_print ("*** Failed to create pixmap.\n"); return 0; } glconfig = gdk_gl_config_new_by_mode ((GdkGLConfigMode)(GDK_GL_MODE_RGBA | GDK_GL_MODE_DEPTH | GDK_GL_MODE_STENCIL | GDK_GL_MODE_SINGLE)); if (glconfig == NULL) { g_print ("*** No appropriate OpenGL-capable visual found.\n"); return 0; } /* * Set OpenGL-capability to the pixmap */ gldrawable = GDK_GL_DRAWABLE (gdk_pixmap_set_gl_capability (target_pixmap, glconfig, NULL)); if ( gldrawable == NULL) { g_print ("Failed to create the GdkGLPixmap\n"); return 0; } glcontext = gdk_gl_context_new (gldrawable, NULL, FALSE, GDK_GL_RGBA_TYPE); if (glcontext == NULL) { g_print ("Connot create the OpenGL rendering context\n"); return 0; } oglrender_init = _oglrender_init; oglrender_beginOpenGL = _oglrender_beginOpenGL; oglrender_endOpenGL = _oglrender_endOpenGL; return 1; }
/// Returns a "global" (to the document) gdkgl context that can be used to share display lists GdkGLContext* gdkgl_share_list() { if(!m_gdkgl_share_list) { GdkGLConfig* const config = gdk_gl_config_new_by_mode( static_cast<GdkGLConfigMode>(GDK_GL_MODE_RGBA | GDK_GL_MODE_DOUBLE | GDK_GL_MODE_DEPTH)); return_val_if_fail(config, 0); GdkPixmap* const pixmap = gdk_pixmap_new(0, 8, 8, gdk_gl_config_get_depth(config)); return_val_if_fail(pixmap, 0); GdkGLPixmap* const glpixmap = gdk_pixmap_set_gl_capability(pixmap, config, 0); return_val_if_fail(glpixmap, 0); GdkGLContext* const context = gdk_gl_context_new( gdk_pixmap_get_gl_drawable(pixmap), 0, true, GDK_GL_RGBA_TYPE); return_val_if_fail(context, 0); m_gdkgl_share_list = context; } return m_gdkgl_share_list; }
static gboolean configure_event (GtkWidget *widget, GdkEventConfigure *event, gpointer data) { GdkGLDrawable *gldrawable; static gboolean is_initialized = FALSE; /* * Create an OpenGL off-screen rendering area. */ if (pixmap != NULL) g_object_unref (G_OBJECT (pixmap)); pixmap = gdk_pixmap_new (widget->window, widget->allocation.width, widget->allocation.height, -1); /* * Set OpenGL-capability to the pixmap */ gldrawable = GDK_GL_DRAWABLE (gdk_pixmap_set_gl_capability (pixmap, glconfig, NULL)); /* * Create OpenGL rendering context (not direct). */ if (glcontext == NULL) { glcontext = gdk_gl_context_new (gldrawable, NULL, FALSE, GDK_GL_RGBA_TYPE); if (glcontext == NULL) { g_print ("Connot create the OpenGL rendering context\n"); if (gtk_main_level () != 0) gtk_main_quit (); return TRUE; } g_print ("The OpenGL rendering context is created\n"); } /*** OpenGL BEGIN ***/ if (!gdk_gl_drawable_gl_begin (gldrawable, glcontext)) goto NO_GL; if (!is_initialized) { init (); is_initialized = TRUE; } glViewport (0, 0, widget->allocation.width, widget->allocation.height); glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); /* Sync. */ gdk_gl_drawable_wait_gl (gldrawable); /* GDK rendering. */ gdk_draw_rectangle (GDK_DRAWABLE (gldrawable), widget->style->black_gc, TRUE, widget->allocation.width/10, widget->allocation.height/10, widget->allocation.width*8/10, widget->allocation.height*8/10); /* Sync. */ gdk_gl_drawable_wait_gdk (gldrawable); glCallList (1); glFlush (); gdk_gl_drawable_gl_end (gldrawable); /*** OpenGL END ***/ NO_GL: return TRUE; }