1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
#!/usr/bin/env python
# This program is a mapping of the color.c program
# written by Naofumi. It should provide a good test case for the
# relevant PyGtkGLExt classes and functions.
#
# Alif Wahid.
import sys
import pygtk
pygtk.require('2.0')
import gtk
import gtk.gtkgl
from OpenGL.GL import *
from OpenGL.GLU import *
class ColorManagementDemo(object):
def __init__(self):
# Check for whether to set rgb mode
# or color index mode.
if '--rgb' in sys.argv:
self.display_mode = gtk.gdkgl.MODE_RGB
else:
self.display_mode = gtk.gdkgl.MODE_INDEX
# Query the OpenGL extension version.
print "OpenGL extension version - %d.%d\n" % gtk.gdkgl.query_version()
# Try to create a double buffered framebuffer,
# if not successful then create a single
# buffered one.
self.display_mode |= 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)
if not self.glconfig.is_rgba():
# Try to allocate non-writeable and perfect match colours.
try:
colormap = self.glconfig.get_colormap()
self.BLACK = colormap.alloc_color(0x0, 0x0, 0x0, False, False)
self.RED = colormap.alloc_color(0xffff, 0x0, 0x0, False, False)
# Raise this dummy exception for testing to
# see if switching to RGB mode occurs
# properly. Currently commented out, but
# uncomment if necessary for testing.
#
#raise RuntimeError
self.GREEN = colormap.alloc_color(0x0, 0xffff, 0x0, False, False)
self.BLUE = colormap.alloc_color(0x0, 0x0, 0xffff, False, False)
self.render_type = gtk.gdkgl.COLOR_INDEX_TYPE
except RuntimeError:
print 'Could not allocate colours in Index mode.'
print 'Switching to RGB mode.'
self.display_mode &= ~gtk.gdkgl.MODE_INDEX # Clear the Index mode flag.
self.display_mode |= gtk.gdkgl.MODE_RGB # Set the RGB mode flag.
self.glconfig = gtk.gdkgl.Config(mode=self.display_mode)
self.__create_Color_objects()
else:
# Directly corresponding gdk.Color objects.
self.__create_Color_objects()
# Create the window for the app.
self.win = gtk.Window()
self.win.set_title('color')
if sys.platform != 'win32':
self.win.set_resize_mode(gtk.RESIZE_IMMEDIATE)
self.win.set_reallocate_redraws(True)
self.win.connect('destroy', lambda quit: gtk.main_quit())
# VBox to hold everything.
vbox = gtk.VBox()
self.win.add(vbox)
vbox.show()
# DrawingArea for OpenGL rendering.
glarea = gtk.gtkgl.DrawingArea(glconfig=self.glconfig,
render_type=self.render_type)
glarea.set_size_request(200, 200)
glarea.connect_after('realize', self.__realize)
glarea.connect('configure_event', self.__configure_event)
glarea.connect('expose_event', self.__expose_event)
vbox.pack_start(glarea)
glarea.show()
# A quit button.
button = gtk.Button('Quit')
button.connect('clicked', lambda quit: self.win.destroy())
vbox.pack_start(button, expand=False)
button.show()
def __create_Color_objects (self):
# Directly corresponding gdk.Color objects.
self.BLACK = gtk.gdk.Color(0x0, 0x0, 0x0)
self.RED = gtk.gdk.Color(0xffff, 0x0, 0x0)
self.GREEN = gtk.gdk.Color(0x0, 0xffff, 0x0)
self.BLUE = gtk.gdk.Color(0x0, 0x0, 0xffff)
self.render_type = gtk.gdkgl.RGBA_TYPE
def __realize(self, widget):
# Get GLContext and GLDrawable
glcontext = widget.get_gl_context()
gldrawable = widget.get_gl_drawable()
# OpenGL begin.
if not gldrawable.gl_begin(glcontext): return
glClearColor(0.0, 0.0, 0.0, 0.0)
# OpenGL end
gldrawable.gl_end()
def __configure_event(self, widget, event):
# Get GLContext and GLDrawable
glcontext = widget.get_gl_context()
gldrawable = widget.get_gl_drawable()
# GL calls
if not gldrawable.gl_begin(glcontext): return
# OpenGL begin.
glViewport(0, 0, widget.allocation.width, widget.allocation.height)
# OpenGL end
gldrawable.gl_end()
return True
def __expose_event(self, widget, event):
# Get GLContext and GLDrawable
glcontext = widget.get_gl_context()
gldrawable = widget.get_gl_drawable()
# GL calls
if not gldrawable.gl_begin(glcontext): return
# OpenGL begin.
glClear(GL_COLOR_BUFFER_BIT)
glBegin(GL_TRIANGLES)
glIndexi(self.RED.pixel)
glColor3f(1.0, 0.0, 0.0)
glVertex2i(0, 1)
glIndexi(self.GREEN.pixel)
glColor3f(0.0, 1.0, 0.0)
glVertex2i(-1, -1)
glIndexi(self.BLUE.pixel)
glColor3f(0.0, 0.0, 1.0)
glVertex2i(1, -1)
glEnd()
if gldrawable.is_double_buffered():
gldrawable.swap_buffers()
else:
glFlush()
# OpenGL end
gldrawable.gl_end()
return True
def run(self):
self.win.show()
gtk.main()
if __name__ == '__main__':
app = ColorManagementDemo()
app.run()
|