5     from wx 
import glcanvas
 
  11     # The Python OpenGL package can be found at 
  12     # http://PyOpenGL.sourceforge.net/ 
  13     from OpenGL
.GL 
import * 
  14     from OpenGL
.GLUT 
import * 
  19 #---------------------------------------------------------------------- 
  22     def runTest(frame
, nb
, log
): 
  23         dlg 
= wx
.MessageDialog(frame
, 'The GLCanvas class has not been included with this build of wxPython!', 
  24                           'Sorry', wx
.OK | wx
.ICON_INFORMATION
) 
  29     def runTest(frame
, nb
, log
): 
  30         dlg 
= wx
.MessageDialog(frame
, 
  31                               'The OpenGL package was not found.  You can get it at\n' 
  32                               'http://PyOpenGL.sourceforge.net/', 
  33                           'Sorry', wx
.OK | wx
.ICON_INFORMATION
) 
  42         wx
.NewId() : ('CubeCanvas',      'Cube'), 
  43         wx
.NewId() : ('ConeCanvas',      'Cone'), 
  46     class ButtonPanel(wx
.Panel
): 
  47         def __init__(self
, parent
, log
): 
  48             wx
.Panel
.__init
__(self
, parent
, -1) 
  51             box 
= wx
.BoxSizer(wx
.VERTICAL
) 
  53             keys 
= buttonDefs
.keys() 
  56                 text 
= buttonDefs
[k
][1] 
  57                 btn 
= wx
.Button(self
, k
, text
) 
  58                 box
.Add(btn
, 0, wx
.ALIGN_CENTER|wx
.ALL
, 15) 
  59                 self
.Bind(wx
.EVT_BUTTON
, self
.OnButton
, btn
) 
  61             #** Enable this to show putting a GLCanvas on the wx.Panel 
  65                 box
.Add(c
, 0, wx
.ALIGN_CENTER|wx
.ALL
, 15) 
  67             self
.SetAutoLayout(True) 
  71         def OnButton(self
, evt
): 
  72             canvasClassName 
= buttonDefs
[evt
.GetId()][0] 
  73             canvasClass 
= eval(canvasClassName
) 
  74             frame 
= wx
.Frame(None, -1, canvasClassName
, size
=(400,400)) 
  75             canvas 
= canvasClass(frame
) 
  80     def runTest(frame
, nb
, log
): 
  81         win 
= ButtonPanel(nb
, log
) 
  87     class MyCanvasBase(glcanvas
.GLCanvas
): 
  88         def __init__(self
, parent
): 
  89             glcanvas
.GLCanvas
.__init
__(self
, parent
, -1) 
  91             # initial mouse position 
  92             self
.lastx 
= self
.x 
= 30 
  93             self
.lasty 
= self
.y 
= 30 
  94             self
.Bind(wx
.EVT_ERASE_BACKGROUND
, self
.OnEraseBackground
) 
  95             self
.Bind(wx
.EVT_SIZE
, self
.OnSize
) 
  96             self
.Bind(wx
.EVT_PAINT
, self
.OnPaint
) 
  97             self
.Bind(wx
.EVT_LEFT_DOWN
, self
.OnMouseDown
) 
  98             self
.Bind(wx
.EVT_LEFT_UP
, self
.OnMouseUp
) 
  99             self
.Bind(wx
.EVT_MOTION
, self
.OnMouseMotion
) 
 102         def OnEraseBackground(self
, event
): 
 103             pass # Do nothing, to avoid flashing on MSW. 
 106         def OnSize(self
, event
): 
 107             size 
= self
.GetClientSize() 
 108             if self
.GetContext(): 
 110                 glViewport(0, 0, size
.width
, size
.height
) 
 114         def OnPaint(self
, event
): 
 115             dc 
= wx
.PaintDC(self
) 
 123         def OnMouseDown(self
, evt
): 
 127         def OnMouseUp(self
, evt
): 
 131         def OnMouseMotion(self
, evt
): 
 132             if evt
.Dragging() and evt
.LeftIsDown(): 
 133                 self
.x
, self
.y 
= self
.lastx
, self
.lasty
 
 134                 self
.x
, self
.y 
= evt
.GetPosition() 
 140     class CubeCanvas(MyCanvasBase
): 
 142             # set viewing projection 
 143             glMatrixMode(GL_PROJECTION
); 
 144             glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0); 
 147             glMatrixMode(GL_MODELVIEW
); 
 148             glTranslatef(0.0, 0.0, -2.0); 
 151             glRotatef(self
.y
, 1.0, 0.0, 0.0); 
 152             glRotatef(self
.x
, 0.0, 1.0, 0.0); 
 154             glEnable(GL_DEPTH_TEST
); 
 155             glEnable(GL_LIGHTING
); 
 160             # clear color and depth buffers 
 161             glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
); 
 163             # draw six faces of a cube 
 165             glNormal3f( 0.0, 0.0, 1.0) 
 166             glVertex3f( 0.5, 0.5, 0.5) 
 167             glVertex3f(-0.5, 0.5, 0.5) 
 168             glVertex3f(-0.5,-0.5, 0.5) 
 169             glVertex3f( 0.5,-0.5, 0.5) 
 171             glNormal3f( 0.0, 0.0,-1.0) 
 172             glVertex3f(-0.5,-0.5,-0.5) 
 173             glVertex3f(-0.5, 0.5,-0.5) 
 174             glVertex3f( 0.5, 0.5,-0.5) 
 175             glVertex3f( 0.5,-0.5,-0.5) 
 177             glNormal3f( 0.0, 1.0, 0.0) 
 178             glVertex3f( 0.5, 0.5, 0.5) 
 179             glVertex3f( 0.5, 0.5,-0.5) 
 180             glVertex3f(-0.5, 0.5,-0.5) 
 181             glVertex3f(-0.5, 0.5, 0.5) 
 183             glNormal3f( 0.0,-1.0, 0.0) 
 184             glVertex3f(-0.5,-0.5,-0.5) 
 185             glVertex3f( 0.5,-0.5,-0.5) 
 186             glVertex3f( 0.5,-0.5, 0.5) 
 187             glVertex3f(-0.5,-0.5, 0.5) 
 189             glNormal3f( 1.0, 0.0, 0.0) 
 190             glVertex3f( 0.5, 0.5, 0.5) 
 191             glVertex3f( 0.5,-0.5, 0.5) 
 192             glVertex3f( 0.5,-0.5,-0.5) 
 193             glVertex3f( 0.5, 0.5,-0.5) 
 195             glNormal3f(-1.0, 0.0, 0.0) 
 196             glVertex3f(-0.5,-0.5,-0.5) 
 197             glVertex3f(-0.5,-0.5, 0.5) 
 198             glVertex3f(-0.5, 0.5, 0.5) 
 199             glVertex3f(-0.5, 0.5,-0.5) 
 202             glRotatef((self
.lasty 
- self
.y
)/100., 1.0, 0.0, 0.0); 
 203             glRotatef((self
.lastx 
- self
.x
)/100., 0.0, 1.0, 0.0); 
 211     class ConeCanvas(MyCanvasBase
): 
 213             glMatrixMode(GL_PROJECTION
); 
 214             # camera frustrum setup 
 215             glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0); 
 216             glMaterial(GL_FRONT
, GL_AMBIENT
, [0.2, 0.2, 0.2, 1.0]) 
 217             glMaterial(GL_FRONT
, GL_DIFFUSE
, [0.8, 0.8, 0.8, 1.0]) 
 218             glMaterial(GL_FRONT
, GL_SPECULAR
, [1.0, 0.0, 1.0, 1.0]) 
 219             glMaterial(GL_FRONT
, GL_SHININESS
, 50.0) 
 220             glLight(GL_LIGHT0
, GL_AMBIENT
, [0.0, 1.0, 0.0, 1.0]) 
 221             glLight(GL_LIGHT0
, GL_DIFFUSE
, [1.0, 1.0, 1.0, 1.0]) 
 222             glLight(GL_LIGHT0
, GL_SPECULAR
, [1.0, 1.0, 1.0, 1.0]) 
 223             glLight(GL_LIGHT0
, GL_POSITION
, [1.0, 1.0, 1.0, 0.0]); 
 224             glLightModel(GL_LIGHT_MODEL_AMBIENT
, [0.2, 0.2, 0.2, 1.0]) 
 225             glEnable(GL_LIGHTING
) 
 228             glEnable(GL_DEPTH_TEST
) 
 229             glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
) 
 231             glMatrixMode(GL_MODELVIEW
); 
 235             # clear color and depth buffers 
 236             glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
); 
 237             # use a fresh transformation matrix 
 240             glTranslate(0.0, 0.0, -2.0); 
 241             glRotate(30.0, 1.0, 0.0, 0.0); 
 242             glRotate(30.0, 0.0, 1.0, 0.0); 
 244             glTranslate(0, -1, 0) 
 245             glRotate(250, 1, 0, 0) 
 246             glutSolidCone(0.5, 1, 30, 5) 
 248             glRotatef((self
.lasty 
- self
.y
)/100., 0.0, 0.0, 1.0); 
 249             glRotatef(0.0, (self
.lastx 
- self
.x
)/100., 1.0, 0.0); 
 250             # push into visible buffer 
 256 #---------------------------------------------------------------------- 
 266 if __name__ 
== '__main__': 
 269     run
.main(['', os
.path
.basename(sys
.argv
[0])] + sys
.argv
[1:]) 
 274 #----------------------------------------------------------------------