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 #---------------------------------------------------------------------- 
  23     wx
.NewId() : ('CubeCanvas',      'Cube'), 
  24     wx
.NewId() : ('ConeCanvas',      'Cone'), 
  27 class ButtonPanel(wx
.Panel
): 
  28     def __init__(self
, parent
, log
): 
  29         wx
.Panel
.__init
__(self
, parent
, -1) 
  32         box 
= wx
.BoxSizer(wx
.VERTICAL
) 
  34         keys 
= buttonDefs
.keys() 
  37             text 
= buttonDefs
[k
][1] 
  38             btn 
= wx
.Button(self
, k
, text
) 
  39             box
.Add(btn
, 0, wx
.ALIGN_CENTER|wx
.ALL
, 15) 
  40             self
.Bind(wx
.EVT_BUTTON
, self
.OnButton
, btn
) 
  42         #** Enable this to show putting a GLCanvas on the wx.Panel 
  46             box
.Add(c
, 0, wx
.ALIGN_CENTER|wx
.ALL
, 15) 
  48         self
.SetAutoLayout(True) 
  52     def OnButton(self
, evt
): 
  54             dlg 
= wx
.MessageDialog(self
, 
  55                                    'The GLCanvas class has not been included with this build of wxPython!', 
  56                                    'Sorry', wx
.OK | wx
.ICON_WARNING
) 
  61             dlg 
= wx
.MessageDialog(self
, 
  62                                    'The OpenGL package was not found.  You can get it at\n' 
  63                                    'http://PyOpenGL.sourceforge.net/', 
  64                                    'Sorry', wx
.OK | wx
.ICON_WARNING
) 
  69             canvasClassName 
= buttonDefs
[evt
.GetId()][0] 
  70             canvasClass 
= eval(canvasClassName
) 
  71             frame 
= wx
.Frame(None, -1, canvasClassName
, size
=(400,400)) 
  72             canvas 
= canvasClass(frame
) 
  76 class MyCanvasBase(glcanvas
.GLCanvas
): 
  77     def __init__(self
, parent
): 
  78         glcanvas
.GLCanvas
.__init
__(self
, parent
, -1) 
  80         # initial mouse position 
  81         self
.lastx 
= self
.x 
= 30 
  82         self
.lasty 
= self
.y 
= 30 
  84         self
.Bind(wx
.EVT_ERASE_BACKGROUND
, self
.OnEraseBackground
) 
  85         self
.Bind(wx
.EVT_SIZE
, self
.OnSize
) 
  86         self
.Bind(wx
.EVT_PAINT
, self
.OnPaint
) 
  87         self
.Bind(wx
.EVT_LEFT_DOWN
, self
.OnMouseDown
) 
  88         self
.Bind(wx
.EVT_LEFT_UP
, self
.OnMouseUp
) 
  89         self
.Bind(wx
.EVT_MOTION
, self
.OnMouseMotion
) 
  92     def OnEraseBackground(self
, event
): 
  93         pass # Do nothing, to avoid flashing on MSW. 
  96     def OnSize(self
, event
): 
  97         size 
= self
.size 
= self
.GetClientSize() 
 100             glViewport(0, 0, size
.width
, size
.height
) 
 104     def OnPaint(self
, event
): 
 105         dc 
= wx
.PaintDC(self
) 
 113     def OnMouseDown(self
, evt
): 
 115         self
.x
, self
.y 
= self
.lastx
, self
.lasty 
= evt
.GetPosition() 
 118     def OnMouseUp(self
, evt
): 
 122     def OnMouseMotion(self
, evt
): 
 123         if evt
.Dragging() and evt
.LeftIsDown(): 
 124             self
.lastx
, self
.lasty 
= self
.x
, self
.y
 
 125             self
.x
, self
.y 
= evt
.GetPosition() 
 131 class CubeCanvas(MyCanvasBase
): 
 133         # set viewing projection 
 134         glMatrixMode(GL_PROJECTION
) 
 135         glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0) 
 138         glMatrixMode(GL_MODELVIEW
) 
 139         glTranslatef(0.0, 0.0, -2.0) 
 142         glRotatef(self
.y
, 1.0, 0.0, 0.0) 
 143         glRotatef(self
.x
, 0.0, 1.0, 0.0) 
 145         glEnable(GL_DEPTH_TEST
) 
 146         glEnable(GL_LIGHTING
) 
 151         # clear color and depth buffers 
 152         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
) 
 154         # draw six faces of a cube 
 156         glNormal3f( 0.0, 0.0, 1.0) 
 157         glVertex3f( 0.5, 0.5, 0.5) 
 158         glVertex3f(-0.5, 0.5, 0.5) 
 159         glVertex3f(-0.5,-0.5, 0.5) 
 160         glVertex3f( 0.5,-0.5, 0.5) 
 162         glNormal3f( 0.0, 0.0,-1.0) 
 163         glVertex3f(-0.5,-0.5,-0.5) 
 164         glVertex3f(-0.5, 0.5,-0.5) 
 165         glVertex3f( 0.5, 0.5,-0.5) 
 166         glVertex3f( 0.5,-0.5,-0.5) 
 168         glNormal3f( 0.0, 1.0, 0.0) 
 169         glVertex3f( 0.5, 0.5, 0.5) 
 170         glVertex3f( 0.5, 0.5,-0.5) 
 171         glVertex3f(-0.5, 0.5,-0.5) 
 172         glVertex3f(-0.5, 0.5, 0.5) 
 174         glNormal3f( 0.0,-1.0, 0.0) 
 175         glVertex3f(-0.5,-0.5,-0.5) 
 176         glVertex3f( 0.5,-0.5,-0.5) 
 177         glVertex3f( 0.5,-0.5, 0.5) 
 178         glVertex3f(-0.5,-0.5, 0.5) 
 180         glNormal3f( 1.0, 0.0, 0.0) 
 181         glVertex3f( 0.5, 0.5, 0.5) 
 182         glVertex3f( 0.5,-0.5, 0.5) 
 183         glVertex3f( 0.5,-0.5,-0.5) 
 184         glVertex3f( 0.5, 0.5,-0.5) 
 186         glNormal3f(-1.0, 0.0, 0.0) 
 187         glVertex3f(-0.5,-0.5,-0.5) 
 188         glVertex3f(-0.5,-0.5, 0.5) 
 189         glVertex3f(-0.5, 0.5, 0.5) 
 190         glVertex3f(-0.5, 0.5,-0.5) 
 193         if self
.size 
is None: 
 194             self
.size 
= self
.GetClientSize() 
 200         glRotatef((self
.y 
- self
.lasty
) * yScale
, 1.0, 0.0, 0.0); 
 201         glRotatef((self
.x 
- self
.lastx
) * xScale
, 0.0, 1.0, 0.0); 
 209 class ConeCanvas(MyCanvasBase
): 
 211         glMatrixMode(GL_PROJECTION
) 
 212         # camera frustrum setup 
 213         glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0) 
 214         glMaterial(GL_FRONT
, GL_AMBIENT
, [0.2, 0.2, 0.2, 1.0]) 
 215         glMaterial(GL_FRONT
, GL_DIFFUSE
, [0.8, 0.8, 0.8, 1.0]) 
 216         glMaterial(GL_FRONT
, GL_SPECULAR
, [1.0, 0.0, 1.0, 1.0]) 
 217         glMaterial(GL_FRONT
, GL_SHININESS
, 50.0) 
 218         glLight(GL_LIGHT0
, GL_AMBIENT
, [0.0, 1.0, 0.0, 1.0]) 
 219         glLight(GL_LIGHT0
, GL_DIFFUSE
, [1.0, 1.0, 1.0, 1.0]) 
 220         glLight(GL_LIGHT0
, GL_SPECULAR
, [1.0, 1.0, 1.0, 1.0]) 
 221         glLight(GL_LIGHT0
, GL_POSITION
, [1.0, 1.0, 1.0, 0.0]) 
 222         glLightModel(GL_LIGHT_MODEL_AMBIENT
, [0.2, 0.2, 0.2, 1.0]) 
 223         glEnable(GL_LIGHTING
) 
 226         glEnable(GL_DEPTH_TEST
) 
 227         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
) 
 229         glMatrixMode(GL_MODELVIEW
) 
 231         glTranslatef(0.0, 0.0, -2.0); 
 236         # clear color and depth buffers 
 237         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
) 
 238         # use a fresh transformation matrix 
 241         #glTranslate(0.0, 0.0, -2.0) 
 242         glRotate(30.0, 1.0, 0.0, 0.0) 
 243         glRotate(30.0, 0.0, 1.0, 0.0) 
 245         glTranslate(0, -1, 0) 
 246         glRotate(250, 1, 0, 0) 
 247         glutSolidCone(0.5, 1, 30, 5) 
 249         glRotatef((self
.y 
- self
.lasty
), 0.0, 0.0, 1.0); 
 250         glRotatef((self
.x 
- self
.lastx
), 1.0, 0.0, 0.0); 
 251         # push into visible buffer 
 257 #---------------------------------------------------------------------- 
 260 def runTest(frame
, nb
, log
): 
 261     win 
= ButtonPanel(nb
, log
) 
 274 if __name__ 
== '__main__': 
 277     run
.main(['', os
.path
.basename(sys
.argv
[0])] + sys
.argv
[1:]) 
 282 #----------------------------------------------------------------------