File: share-lists.py

package info (click to toggle)
python-gtkglext1 1.1.0-3.1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,976 kB
  • ctags: 422
  • sloc: sh: 8,993; python: 3,469; makefile: 191; ansic: 132
file content (209 lines) | stat: -rw-r--r-- 6,739 bytes parent folder | download | duplicates (3)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/env python

# This program is a mapping of the share-lists.c program
# written by Naofumi. It should provide a good test case for the
# relevant gtk.gtkgl.*/gtk.gdkgl.* classes and functions in PyGtkGLExt.
#
# Alif Wahid, May 2003.

import sys

import pygtk
pygtk.require('2.0')
import gtk
import gtk.gtkgl

from OpenGL.GL import *
from OpenGL.GLU import *

# Due to the low level nature of this
# program, I think aggregating Gtk
# classes is a better idea rather
# than inheriting. This class is really
# intended to provide a namespace rather
# than an object heirarchy.

class ShareListsDemo(object):

    def __init__(self):
        self.display_mode = gtk.gdkgl.MODE_RGB    | \
                            gtk.gdkgl.MODE_DEPTH  | \
                            gtk.gdkgl.MODE_DOUBLE

        # Try to create a double buffered framebuffer,
        # if not successful then create a single
        # buffered one.
        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(10)
        self.win.connect('destroy', lambda quit: gtk.main_quit())

        # VBox to hold everything.
        vbox = gtk.VBox()
        self.win.add(vbox)
        vbox.show()

        # DrawingArea-1 for OpenGL rendering (main: creates display list).
        glarea = gtk.gtkgl.DrawingArea( glconfig=self.glconfig,
                                        share_list=None,
                                        render_type=gtk.gdkgl.RGBA_TYPE)
        glarea.set_size_request(120, 120)
        red = (1.0, 0.0, 0.0, 0.0)
        glarea.connect_after('realize', self.__realize_main, red)
        glarea.connect('configure_event', self.__configure_event)
        glarea.connect('expose_event', self.__expose_event)
        vbox.pack_start(glarea, expand=True, fill=True, padding=5)
        glarea.show()

        # Get OpenGL context.
        glarea.realize()
        self.glcontext = glarea.get_gl_context()

        # DrawingArea-2 for OpenGL rendering (sub: shares display list).
        glarea = gtk.gtkgl.DrawingArea( glconfig=self.glconfig,
                                        share_list=self.glcontext,
                                        render_type=gtk.gdkgl.RGBA_TYPE)
        glarea.set_size_request(120, 120)
        green = (0.0, 1.0, 0.0, 0.0)
        glarea.connect_after('realize', self.__realize, green)
        glarea.connect('configure_event', self.__configure_event)
        glarea.connect('expose_event', self.__expose_event)
        vbox.pack_start(glarea, expand=True, fill=True, padding=5)
        glarea.show()

        # DrawingArea-3 for OpenGL rendering (sub: shares display list).
        glarea = gtk.gtkgl.DrawingArea( glconfig=self.glconfig,
                                        share_list=self.glcontext,
                                        render_type=gtk.gdkgl.RGBA_TYPE)
        glarea.set_size_request(120, 120)
        blue = (0.0, 0.0, 1.0, 0.0)
        glarea.connect_after('realize', self.__realize, blue)
        glarea.connect('configure_event', self.__configure_event)
        glarea.connect('expose_event', self.__expose_event)
        vbox.pack_start(glarea, expand=True, fill=True, padding=5)
        glarea.show()

        # A quit button.
        button = gtk.Button('Quit')
        button.connect('clicked', lambda quit: self.win.destroy())
        vbox.pack_start(button, expand=False, fill=False, padding=5)
        button.show()

    def __init_gl (self, light_diffuse):
        # OpenGL context initialisation.
        # Only call this while having a valid context!
        light_position = (1.0, 1.0, 1.0, 0.0)

        glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse)
        glLightfv(GL_LIGHT0, GL_POSITION, light_position)
        glEnable(GL_LIGHTING)
        glEnable(GL_LIGHT0)
        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 __realize_main(self, widget, light_diffuse):
        # Get GLContext and GLDrawable
        glcontext = widget.get_gl_context()
        gldrawable = widget.get_gl_drawable()

        # OpenGL begin.
        if not gldrawable.gl_begin(glcontext): return

        # Create display list #1.
        qobj = gluNewQuadric()
        gluQuadricDrawStyle(qobj, GLU_FILL)
        glNewList(1, GL_COMPILE)
        gluSphere(qobj, 1.0, 20, 20)
        glEndList()

        # Initialise rendering context.
        self.__init_gl(light_diffuse)

        # OpenGL end
        gldrawable.gl_end()

    def __realize(self, widget, light_diffuse):
        # Get GLContext and GLDrawable
        glcontext = widget.get_gl_context()
        gldrawable = widget.get_gl_drawable()

        # GL calls
        if not gldrawable.gl_begin(glcontext): return

        # Initialise rendering context.
        self.__init_gl(light_diffuse)

        # 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 | GL_DEPTH_BUFFER_BIT)
        glCallList(1)

        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 = ShareListsDemo()
    app.run()