download GLCanvas.py
Language: Python
LOC: 251
Project Info
MEDWX
Server: Spider_20090227_inc
Type: filesystem
....2.0.zip\medwx\opengl.save\
   __init__.py
   demo.py
   GLCanvas.py

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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import wx

try:
    from wx import glcanvas
    haveGLCanvas = True
except ImportError:
    haveGLCanvas = False

try:
    # The Python OpenGL package can be found at
    # http://PyOpenGL.sourceforge.net/
    from OpenGL.GL import *
    from OpenGL.GLUT import *
    from OpenGL.GLE import *
    from math import *
    haveOpenGL = True
except ImportError:
    haveOpenGL = False

#----------------------------------------------------------------------

if not haveGLCanvas:
    def runTest(frame, nb, log):
        dlg = wx.MessageDialog(frame, 'The GLCanvas class has not been included with this build of wxPython!',
                          'Sorry', wx.OK | wx.ICON_INFORMATION)
        dlg.ShowModal()
        dlg.Destroy()

elif not haveOpenGL:
    def runTest(frame, nb, log):
        dlg = wx.MessageDialog(frame,
                              'The OpenGL package was not found.  You can get it at\n'
                              'http://PyOpenGL.sourceforge.net/',
                          'Sorry', wx.OK | wx.ICON_INFORMATION)
        dlg.ShowModal()
        dlg.Destroy()




else:
    buttonDefs = {
        wx.NewId() : ('CubeCanvas',      'Cube'),
        wx.NewId() : ('ConeCanvas',      'Cone'),
        wx.NewId() : ('TexasCanvas',      'Texas'),
        
        }

    class ButtonPanel(wx.Panel):
        def __init__(self, parent):
            wx.Panel.__init__(self, parent, -1)

            box = wx.BoxSizer(wx.VERTICAL)
            box.Add((20, 30))
            keys = buttonDefs.keys()
            keys.sort()
            for k in keys:
                text = buttonDefs[k][1]
                btn = wx.Button(self, k, text)
                box.Add(btn, 0, wx.ALIGN_CENTER|wx.ALL, 15)
                self.Bind(wx.EVT_BUTTON, self.OnButton, btn)

            #** Enable this to show putting a GLCanvas on the wx.Panel
            if 0:
                c = CubeCanvas(self)
                c.SetSize((200, 200))
                box.Add(c, 0, wx.ALIGN_CENTER|wx.ALL, 15)

            self.SetAutoLayout(True)
            self.SetSizer(box)


        def OnButton(self, evt):
            canvasClassName = buttonDefs[evt.GetId()][0]
            canvasClass = eval(canvasClassName)
            frame = wx.Frame(None, -1, canvasClassName, size=(400,400))
            canvas = canvasClass(frame)
            frame.Show(True)



    class MyCanvasBase(glcanvas.GLCanvas):
        def __init__(self, parent):
            glcanvas.GLCanvas.__init__(self, parent, -1)
            self.init = False
            # initial mouse position
            self.lastx = self.x = 30
            self.lasty = self.y = 30
            self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
            self.Bind(wx.EVT_SIZE, self.OnSize)
            self.Bind(wx.EVT_PAINT, self.OnPaint)
            self.Bind(wx.EVT_LEFT_DOWN, self.OnMouseDown)
            self.Bind(wx.EVT_LEFT_UP, self.OnMouseUp)
            self.Bind(wx.EVT_MOTION, self.OnMouseMotion)


        def OnEraseBackground(self, event):
            pass # Do nothing, to avoid flashing on MSW.


        def OnSize(self, event):
            size = self.GetClientSize()
            if self.GetContext():
                self.SetCurrent()
                glViewport(0, 0, size.width, size.height)
            event.Skip()
            

        def OnPaint(self, event):
            dc = wx.PaintDC(self)
            self.SetCurrent()
            if not self.init:
                self.InitGL()
                self.init = True
            self.OnDraw()


        def OnMouseDown(self, evt):
            self.CaptureMouse()


        def OnMouseUp(self, evt):
            self.ReleaseMouse()


        def OnMouseMotion(self, evt):
            if evt.Dragging() and evt.LeftIsDown():
                self.x, self.y = self.lastx, self.lasty
                self.x, self.y = evt.GetPosition()
                self.Refresh(False)




    class CubeCanvas(MyCanvasBase):
        def InitGL(self):
            # set viewing projection
            glMatrixMode(GL_PROJECTION);
            glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0);

            # position viewer
            glMatrixMode(GL_MODELVIEW);
            glTranslatef(0.0, 0.0, -2.0);

            # position object
            glRotatef(self.y, 1.0, 0.0, 0.0);
            glRotatef(self.x, 0.0, 1.0, 0.0);

            glEnable(GL_DEPTH_TEST);
            glEnable(GL_LIGHTING);
            glEnable(GL_LIGHT0);


        def OnDraw(self):
            # clear color and depth buffers
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

            # draw six faces of a cube
            glBegin(GL_QUADS)
            glNormal3f( 0.0, 0.0, 1.0)
            glVertex3f( 0.5, 0.5, 0.5)
            glVertex3f(-0.5, 0.5, 0.5)
            glVertex3f(-0.5,-0.5, 0.5)
            glVertex3f( 0.5,-0.5, 0.5)

            glNormal3f( 0.0, 0.0,-1.0)
            glVertex3f(-0.5,-0.5,-0.5)
            glVertex3f(-0.5, 0.5,-0.5)
            glVertex3f( 0.5, 0.5,-0.5)
            glVertex3f( 0.5,-0.5,-0.5)

            glNormal3f( 0.0, 1.0, 0.0)
            glVertex3f( 0.5, 0.5, 0.5)
            glVertex3f( 0.5, 0.5,-0.5)
            glVertex3f(-0.5, 0.5,-0.5)
            glVertex3f(-0.5, 0.5, 0.5)

            glNormal3f( 0.0,-1.0, 0.0)
            glVertex3f(-0.5,-0.5,-0.5)
            glVertex3f( 0.5,-0.5,-0.5)
            glVertex3f( 0.5,-0.5, 0.5)
            glVertex3f(-0.5,-0.5, 0.5)

            glNormal3f( 1.0, 0.0, 0.0)
            glVertex3f( 0.5, 0.5, 0.5)
            glVertex3f( 0.5,-0.5, 0.5)
            glVertex3f( 0.5,-0.5,-0.5)
            glVertex3f( 0.5, 0.5,-0.5)

            glNormal3f(-1.0, 0.0, 0.0)
            glVertex3f(-0.5,-0.5,-0.5)
            glVertex3f(-0.5,-0.5, 0.5)
            glVertex3f(-0.5, 0.5, 0.5)
            glVertex3f(-0.5, 0.5,-0.5)
            glEnd()

            glRotatef((self.lasty - self.y)/100., 1.0, 0.0, 0.0);
            glRotatef((self.lastx - self.x)/100., 0.0, 1.0, 0.0);

            self.SwapBuffers()



    class TexasCanvas(MyCanvasBase):
        def InitGL(self):
            # initialize glut 
            import sys
            #glutInit(sys.argv)
            #glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH)
            #glutCreateWindow("basic demo")
            #glutDisplayFunc(DrawStuff)
            #glutMotionFunc(MouseMotion)

            # create popup menu */
            #	glutCreateMenu (JoinStyle)
            #	glutAddMenuEntry ("Exit", 99)
            #	glutAttachMenu (GLUT_MIDDLE_BUTTON)

            # initialize GL */
            glClearDepth (1.0)
            glEnable (GL_DEPTH_TEST)
            glClearColor (0.0, 0.0, 0.0, 0.0)
            glShadeModel (GL_SMOOTH)

            glMatrixMode (GL_PROJECTION)
            # roughly, measured in centimeters */
            glFrustum (-9.0, 9.0, -9.0, 9.0, 50.0, 150.0)
            glMatrixMode(GL_MODELVIEW)

            # set up a light 
            lightOnePosition = (40.0, 40, 100.0, 0.0)
            lightOneColor = (0.99, 0.99, 0.99, 1.0) 

            lightTwoPosition = (-40.0, 40, 100.0, 0.0)
            lightTwoColor = (0.99, 0.99, 0.99, 1.0) 

            # initialize lighting */
            glLightfv (GL_LIGHT0, GL_POSITION, lightOnePosition)
            glLightfv (GL_LIGHT0, GL_DIFFUSE, lightOneColor)
            glEnable (GL_LIGHT0)
            glLightfv (GL_LIGHT1, GL_POSITION, lightTwoPosition)
            glLightfv (GL_LIGHT1, GL_DIFFUSE, lightTwoColor)
            glEnable (GL_LIGHT1)
            glEnable (GL_LIGHTING)
            glColorMaterial (GL_FRONT_AND_BACK, GL_DIFFUSE)
            glEnable (GL_COLOR_MATERIAL)

            SCALE = 0.8
            TSCALE = 4

            points = ((-1.5, 2.0), (-0.75, 2.0), (-0.75, 1.38), (-0.5, 1.25), (0.88, 1.12), (1.0, 0.62), (1.12, 0.1), (0.5, -0.5), (0.2, -1.12),
                      (0.3, -1.5), (-0.25, -1.45), (-1.06, -0.3), (-1.38, -0.3), (-1.65, -0.6), (-2.5, 0.5), (-1.5, 0.5), (-1.5, 2.0), (-0.75, 2.0))
            self.tspine = map(lambda x: (TSCALE*x[0], TSCALE*x[1], 0), points)

            self.texas_xsection = map(lambda x: (SCALE*x[0], SCALE*x[1]), points[1:])
            self.texas_normal = []

            self.tcolors = []
            self.brand_points = map(lambda x: (0, 0, TSCALE*x), (0.1, 0.0, -5.0, -5.1))
            self.brand_colors =  ((1.0, 0.3, 0.0),)*4

            for i in range(len(self.texas_xsection)):
                self.tcolors.append((((i*33) % 255)/255.0, ((i*47) % 255)/255.0, ((i*89) % 255)/255.0))


            for i in range(1, len(self.texas_xsection)):
                ax = self.texas_xsection[i][0] - self.texas_xsection[i-1][0]
                ay = self.texas_xsection[i][1] - self.texas_xsection[i-1][1]
                alen = sqrt (ax*ax + ay*ay)
                self.texas_normal.append((-ay / alen, ax / alen))

            self.texas_normal.insert(0, self.texas_normal[-1])


        def OnDraw(self):
            glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
            # set up some matrices so that the object spins with the mouse
            gleSetJoinStyle(TUBE_NORM_FACET | TUBE_JN_ANGLE | TUBE_CONTOUR_CLOSED | TUBE_JN_CAP)
            glPushMatrix ()
            glTranslatef (0.0, 0.0, -80.0)
            glRotatef (self.lastx - self.x, 0.0, 1.0, 0.0)
            glRotatef (self.lasty - self.y, 1.0, 0.0, 0.0)

            print 'texas_xsection', self.texas_xsection
            print 'texas_normal', self.texas_normal
            print 'tspine', self.tspine
            print 'tcolors', self.tcolors
            
            gleExtrusion(self.texas_xsection, self.texas_normal, None, self.tspine, self.tcolors)
            gleExtrusion(self.texas_xsection, self.texas_normal, None, self.brand_points, self.brand_colors)
            
            glPopMatrix ()
            self.SwapBuffers()
            #glutSwapBuffers ()


    class ConeCanvas(MyCanvasBase):
        def InitGL( self ):
            glMatrixMode(GL_PROJECTION);
            # camera frustrum setup
            glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0);
            glMaterial(GL_FRONT, GL_AMBIENT, [0.2, 0.2, 0.2, 1.0])
            glMaterial(GL_FRONT, GL_DIFFUSE, [0.8, 0.8, 0.8, 1.0])
            glMaterial(GL_FRONT, GL_SPECULAR, [1.0, 0.0, 1.0, 1.0])
            glMaterial(GL_FRONT, GL_SHININESS, 50.0)
            glLight(GL_LIGHT0, GL_AMBIENT, [0.0, 1.0, 0.0, 1.0])
            glLight(GL_LIGHT0, GL_DIFFUSE, [1.0, 1.0, 1.0, 1.0])
            glLight(GL_LIGHT0, GL_SPECULAR, [1.0, 1.0, 1.0, 1.0])
            glLight(GL_LIGHT0, GL_POSITION, [1.0, 1.0, 1.0, 0.0]);
            glLightModel(GL_LIGHT_MODEL_AMBIENT, [0.2, 0.2, 0.2, 1.0])
            glEnable(GL_LIGHTING)
            glEnable(GL_LIGHT0)
            glDepthFunc(GL_LESS)
            glEnable(GL_DEPTH_TEST)
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
            # position viewer
            glMatrixMode(GL_MODELVIEW);


        def OnDraw(self):
            # clear color and depth buffers
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            # use a fresh transformation matrix
            glPushMatrix()
            # position object
            glTranslate(0.0, 0.0, -2.0);
            glRotate(30.0, 1.0, 0.0, 0.0);
            glRotate(30.0, 0.0, 1.0, 0.0);

            glTranslate(0, -1, 0)
            glRotate(250, 1, 0, 0)
            glutSolidCone(0.5, 1, 30, 5)
            glPopMatrix()
            glRotatef((self.lasty - self.y)/100., 0.0, 0.0, 1.0);
            glRotatef(0.0, (self.lastx - self.x)/100., 1.0, 0.0);
            # push into visible buffer
            self.SwapBuffers()


class Frame(wx.Frame):
    def __init__(self, parent, ID, title):
	w, h = wx.DisplaySize()#58 is the right size to substract
        print '--- wxDisplaySize', w, h
	#w, h = w - 10, h - 58
        wx.Frame.__init__(self, parent, -1, title, size = (200, 300),
                         style=wx.DEFAULT_FRAME_STYLE|wx.NO_FULL_REPAINT_ON_RESIZE)
	
        ButtonPanel(self)
        

class MyApp(wx.App):
    def OnInit(self):
        frame = Frame(None, -1, "ISIS DICOM Images Console")
        frame.Show(True)
        self.SetTopWindow(frame)
        return True

def main():
    import OpenGL
    print dir(OpenGL.GLE)
    print dir(OpenGL.GLUT)
    app = MyApp(0)
    app.MainLoop()

if __name__ == '__main__':
    main()

About Koders | Resources | Downloads | Support | Black Duck | Submit Project | Terms of Service | DMCA | Privacy Policy | Site Map| Contact Us