[GtkGLExt] Sharing display lists between GLDrawable and GLPixmap



Hi all,

I've a problem with sharing display lists between a gl drawable and a gl
pixmap (that is used for making screenshots). I know this problem has
already been adressed in this list but I did not find a working example.

I tried to code a simple example using pygtk, taking inspiration from
the examples bundled with the distribution. Could someone tell me what
I'm doing wrong ? (there should be two red spheres on the screen)

I'm using gtkglext 1.2.0 and pygtkglext 1.1.0

Nicolas


#!/usr/bin/env python

# This program tries to show how to share display lists between a 
# glarea and an offscreen pixmap.


import sys
import gc
import pygtk
pygtk.require('2.0')
import gtk
import gtk.gtkgl
from OpenGL.GL import *
from OpenGL.GLU import *



class ShareListsDemo(object):

	def __init__(self):
		self.display_mode = gtk.gdkgl.MODE_RGB | gtk.gdkgl.MODE_DEPTH | gtk.gdkgl.MODE_DOUBLE
		try:
			self.glconfig = gtk.gdkgl.Config(mode=self.display_mode)
		except gtk.gdkgl.NoMatches:
			self.display_mode &= ~gtk.gdkgl.MODE_DOUBLE
			self.glconfig = gtk.gdkgl.Config(mode=self.display_mode)

		# Create the window for the app.
		self.win = gtk.Window()
		self.win.set_title('Share-lists')
		if sys.platform != 'win32':
			self.win.set_resize_mode(gtk.RESIZE_IMMEDIATE)
		self.win.set_reallocate_redraws(True)
		self.win.set_border_width(12)
		self.win.connect('destroy', lambda quit: gtk.main_quit())

		# VBox to hold everything.
		hbox = gtk.HBox()
		hbox.set_spacing (6)
		self.win.add(hbox)
		hbox.show()

		# GL DrawingArea for direct OpenGL rendering.
		self.glarea = gtk.gtkgl.DrawingArea(glconfig=self.glconfig,
											share_list=None,
											render_type=gtk.gdkgl.RGBA_TYPE)
		self.glarea.set_size_request(256, 256)
		self.glarea.connect_after('realize', self.on_realize_glarea)
		self.glarea.connect('configure_event', self.on_configure_glarea)
		self.glarea.connect('expose_event', self.on_expose_glarea)
		hbox.pack_start(self.glarea, expand=True, fill=True)
		self.glarea.show()
		self.glarea.realize()
		self.glcontext = self.glarea.get_gl_context()

		# DrawingArea for indirect OpenGL rendering
		self.darea = gtk.DrawingArea()
		self.darea.set_size_request(256, 256)
		hbox.pack_start(self.darea, expand=True, fill=True)
		self.darea.show()	
		self.darea.connect('configure_event', self.on_configure_darea)
		self.darea.connect('expose_event', self.on_expose_darea)
		

	def init (self):
		glEnable(GL_DEPTH_TEST)
		glClearColor(1.0, 1.0, 1.0, 1.0)
		glClearDepth(1.0)
		glMatrixMode(GL_PROJECTION)
		glLoadIdentity()
		gluPerspective(40.0, 1.0, 1.0, 10.0)
		glMatrixMode(GL_MODELVIEW)
		glLoadIdentity()
		gluLookAt(0.0, 0.0, 3.0,
				  0.0, 0.0, 0.0,
				  0.0, 1.0, 0.0)
		glTranslatef(0.0, 0.0, -3.0)

	def on_realize_glarea (self, widget):
		""" Realise event for the GL drawing area """
		glcontext = widget.get_gl_context()
		gldrawable = widget.get_gl_drawable()
		
		if not gldrawable.gl_begin(glcontext): return
		obj = gluNewQuadric()
		gluQuadricDrawStyle(obj, GLU_FILL)
		glNewList(1, GL_COMPILE)
		gluSphere(obj, 1.0, 20, 20)
		glEndList()
		self.init ()
		gldrawable.gl_end()

	def on_configure_glarea (self, widget, event):
		""" Configure event for the GL drawing area """
		glcontext = widget.get_gl_context()
		gldrawable = widget.get_gl_drawable()

		if not gldrawable.gl_begin(glcontext): return
		glViewport(0, 0, widget.allocation.width, widget.allocation.height)
		gldrawable.gl_end()

	def on_configure_darea (self, widget, event):
		self.pixmap = None
		gc.collect()
		self.pixmap = gtk.gdkgl.ext(gtk.gdk.Pixmap(self.darea.window,
												   self.darea.allocation.width,
												   self.darea.allocation.height))
		glconfig = gtk.gdkgl.Config(mode=(gtk.gdkgl.MODE_RGB | gtk.gdkgl.MODE_DEPTH | gtk.gdkgl.MODE_SINGLE))
		gldrawable = self.pixmap.set_gl_capability(glconfig)
		glcontext = gtk.gdkgl.Context(gldrawable, share_list=self.glcontext, direct=False)
		if not gldrawable.gl_begin(glcontext): return
		self.init()
		glViewport(0, 0, self.darea.allocation.width, self.darea.allocation.height)
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
		glCallList(1)
		glFlush()
		gldrawable.gl_end()

	def on_expose_glarea (self,widget,event):
		""" Expose event on the GL drawing area """
		glcontext = widget.get_gl_context()
		gldrawable = widget.get_gl_drawable()

		if not gldrawable.gl_begin(glcontext): return
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
		glColor (1,0,0)
		glCallList(1)
		if gldrawable.is_double_buffered():
			gldrawable.swap_buffers()
		else:
			glFlush()
		gldrawable.gl_end()

	def on_expose_darea (self,widget,event):
		""" Expose event on the drawing area """
		x, y, width, height = event.area
		gc = self.darea.get_style().fg_gc[gtk.STATE_NORMAL]
		self.darea.window.draw_drawable(gc, self.pixmap, x, y, x, y, width, height)

	def run(self):
		self.win.show()
		gtk.main()


if __name__ == '__main__':
	app = ShareListsDemo()
	app.run()


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]