1 # 11/18/2003 - Jeff Grimmett (grimmtooth@softhome.net) 
   3 # o Updated for wx namespace 
   4 # o Note: unable to install PyOpenGL on my system as I am running Python 2.3 
   5 #   and PyOpenGL does not support anything above Python 2.2. Did what I could, 
   6 #   but odds are good there are problems. 
  12     import  wx
.glcanvas 
as  glcanvas
 
  18     # The Python OpenGL package can be found at 
  19     # http://PyOpenGL.sourceforge.net/ 
  21     import  OpenGL
.GL   
as  gl
 
  22     import  OpenGL
.GLUT 
as  glut
 
  27 #---------------------------------------------------------------------- 
  30     def runTest(frame
, nb
, log
): 
  31         dlg 
= wx
.MessageDialog( 
  32             frame
, 'The wx.GLCanvas has not been included with this build of wxPython!', 
  33             'Sorry', wx
.OK | wx
.ICON_INFORMATION
 
  40     def runTest(frame
, nb
, log
): 
  41         dlg 
= wx
.MessageDialog( 
  42             frame
, 'The OpenGL package was not found.  You can get it at\n' 
  43             'http://PyOpenGL.sourceforge.net/', 'Sorry', wx
.OK | wx
.ICON_INFORMATION
 
  52         wx
.NewId() : ('CubeCanvas',      'Cube'), 
  53         wx
.NewId() : ('ConeCanvas',      'Cone'), 
  56     class ButtonPanel(wx
.Panel
): 
  57         def __init__(self
, parent
, log
): 
  58             wx
.Panel
.__init
__(self
, parent
, -1) 
  61             box 
= wx
.BoxSizer(wx
.VERTICAL
) 
  63             keys 
= buttonDefs
.keys() 
  67                 text 
= buttonDefs
[k
][1] 
  68                 btn 
= wx
.Button(self
, k
, text
) 
  69                 box
.Add(btn
, 0, wx
.ALIGN_CENTER|wx
.ALL
, 15) 
  70                 self
.Bind(wx
.EVT_BUTTON
, self
.OnButton
, id=k
) 
  72             #** Enable this to show putting a wx.GLCanvas on the wxPanel 
  76                 box
.Add(c
, 0, wx
.ALIGN_CENTER|wx
.ALL
, 15) 
  78             self
.SetAutoLayout(True) 
  82         def OnButton(self
, evt
): 
  83             canvasClassName 
= buttonDefs
[evt
.GetId()][0] 
  84             canvasClass 
= eval(canvasClassName
) 
  85             frame 
= wx
.Frame(None, -1, canvasClassName
, size
=(400,400)) 
  86             canvas 
= canvasClass(frame
) 
  91     def runTest(frame
, nb
, log
): 
  92         win 
= ButtonPanel(nb
, log
) 
  98     class MyCanvasBase(glcanvas
.GLCanvas
): 
  99         def __init__(self
, parent
): 
 100             glcanvas
.GLCanvas
.__init
__(self
, parent
, -1) 
 102             # initial mouse position 
 103             self
.lastx 
= self
.x 
= 30 
 104             self
.lasty 
= self
.y 
= 30 
 105             self
.Bind(wx
.EVT_ERASE_BACKGROUND
, self
.OnEraseBackground
) 
 106             self
.Bind(wx
.EVT_SIZE
, self
.OnSize
) 
 107             self
.Bind(wx
.EVT_PAINT
, self
.OnPaint
) 
 108             self
.Bind(wx
.EVT_LEFT_DOWN
, self
.OnMouseDown
)  # needs fixing... 
 109             self
.Bind(wx
.EVT_LEFT_UP
, self
.OnMouseUp
) 
 110             self
.Bind(wx
.EVT_MOTION
, self
.OnMouseMotion
) 
 112         def OnEraseBackground(self
, event
): 
 113             pass # Do nothing, to avoid flashing on MSW. 
 115         def OnSize(self
, event
): 
 116             size 
= self
.GetClientSize() 
 118             if self
.GetContext(): 
 120                 glcanvas
.glViewport(0, 0, size
.width
, size
.height
) 
 122         def OnPaint(self
, event
): 
 123             dc 
= wx
.PaintDC(self
) 
 132         def OnMouseDown(self
, evt
): 
 135         def OnMouseUp(self
, evt
): 
 138         def OnMouseMotion(self
, evt
): 
 139             if evt
.Dragging() and evt
.LeftIsDown(): 
 140                 self
.x
, self
.y 
= self
.lastx
, self
.lasty
 
 141                 self
.x
, self
.y 
= evt
.GetPosition() 
 145     class CubeCanvas(MyCanvasBase
): 
 147             # set viewing projection 
 148             glcanvas
.glMatrixMode(glcanvas
.GL_PROJECTION
); 
 149             glcanvas
.glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0); 
 152             glcanvas
.glMatrixMode(glcanvas
.GL_MODELVIEW
); 
 153             glcanvas
.glTranslatef(0.0, 0.0, -2.0); 
 156             glcanvas
.glRotatef(self
.y
, 1.0, 0.0, 0.0); 
 157             glcanvas
.glRotatef(self
.x
, 0.0, 1.0, 0.0); 
 159             glcanvas
.glEnable(glcanvas
.GL_DEPTH_TEST
); 
 160             glcanvas
.glEnable(glcanvas
.GL_LIGHTING
); 
 161             glcanvas
.glEnable(glcanvas
.GL_LIGHT0
); 
 165             # clear color and depth buffers 
 166             glcanvas
.glClear(glcanvas
.GL_COLOR_BUFFER_BIT | glcanvas
.GL_DEPTH_BUFFER_BIT
) 
 168             # draw six faces of a cube 
 169             glcanvas
.glBegin(glcanvas
.GL_QUADS
) 
 170             glcanvas
.glNormal3f( 0.0, 0.0, 1.0) 
 171             glcanvas
.glVertex3f( 0.5, 0.5, 0.5) 
 172             glcanvas
.glVertex3f(-0.5, 0.5, 0.5) 
 173             glcanvas
.glVertex3f(-0.5,-0.5, 0.5) 
 174             glcanvas
.glVertex3f( 0.5,-0.5, 0.5) 
 176             glcanvas
.glNormal3f( 0.0, 0.0,-1.0) 
 177             glcanvas
.glVertex3f(-0.5,-0.5,-0.5) 
 178             glcanvas
.glVertex3f(-0.5, 0.5,-0.5) 
 179             glcanvas
.glVertex3f( 0.5, 0.5,-0.5) 
 180             glcanvas
.glVertex3f( 0.5,-0.5,-0.5) 
 182             glcanvas
.glNormal3f( 0.0, 1.0, 0.0) 
 183             glcanvas
.glVertex3f( 0.5, 0.5, 0.5) 
 184             glcanvas
.glVertex3f( 0.5, 0.5,-0.5) 
 185             glcanvas
.glVertex3f(-0.5, 0.5,-0.5) 
 186             glcanvas
.glVertex3f(-0.5, 0.5, 0.5) 
 188             glcanvas
.glNormal3f( 0.0,-1.0, 0.0) 
 189             glcanvas
.glVertex3f(-0.5,-0.5,-0.5) 
 190             glcanvas
.glVertex3f( 0.5,-0.5,-0.5) 
 191             glcanvas
.glVertex3f( 0.5,-0.5, 0.5) 
 192             glcanvas
.glVertex3f(-0.5,-0.5, 0.5) 
 194             glcanvas
.glNormal3f( 1.0, 0.0, 0.0) 
 195             glcanvas
.glVertex3f( 0.5, 0.5, 0.5) 
 196             glcanvas
.glVertex3f( 0.5,-0.5, 0.5) 
 197             glcanvas
.glVertex3f( 0.5,-0.5,-0.5) 
 198             glcanvas
.glVertex3f( 0.5, 0.5,-0.5) 
 200             glcanvas
.glNormal3f(-1.0, 0.0, 0.0) 
 201             glcanvas
.glVertex3f(-0.5,-0.5,-0.5) 
 202             glcanvas
.glVertex3f(-0.5,-0.5, 0.5) 
 203             glcanvas
.glVertex3f(-0.5, 0.5, 0.5) 
 204             glcanvas
.glVertex3f(-0.5, 0.5,-0.5) 
 207             glcanvas
.glRotatef((self
.lasty 
- self
.y
)/100., 1.0, 0.0, 0.0); 
 208             glcanvas
.glRotatef((self
.lastx 
- self
.x
)/100., 0.0, 1.0, 0.0); 
 213     class ConeCanvas(MyCanvasBase
): 
 215             glcanvas
.glMatrixMode(glcanvas
.GL_PROJECTION
); 
 216             # camera frustrum setup 
 217             glcanvas
.glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0); 
 218             glcanvas
.glMaterial(glcanvas
.GL_FRONT
, glcanvas
.GL_AMBIENT
, [0.2, 0.2, 0.2, 1.0]) 
 219             glcanvas
.glMaterial(glcanvas
.GL_FRONT
, glcanvas
.GL_DIFFUSE
, [0.8, 0.8, 0.8, 1.0]) 
 220             glcanvas
.glMaterial(glcanvas
.GL_FRONT
, glcanvas
.GL_SPECULAR
, [1.0, 0.0, 1.0, 1.0]) 
 221             glcanvas
.glMaterial(glcanvas
.GL_FRONT
, glcanvas
.GL_SHININESS
, 50.0) 
 222             glcanvas
.glLight(glcanvas
.GL_LIGHT0
, glcanvas
.GL_AMBIENT
, [0.0, 1.0, 0.0, 1.0]) 
 223             glcanvas
.glLight(glcanvas
.GL_LIGHT0
, glcanvas
.GL_DIFFUSE
, [1.0, 1.0, 1.0, 1.0]) 
 224             glcanvas
.glLight(glcanvas
.GL_LIGHT0
, glcanvas
.GL_SPECULAR
, [1.0, 1.0, 1.0, 1.0]) 
 225             glcanvas
.glLight(glcanvas
.GL_LIGHT0
, glcanvas
.GL_POSITION
, [1.0, 1.0, 1.0, 0.0]); 
 226             glcanvas
.glLightModel(glcanvas
.GL_LIGHT_MODEL_AMBIENT
, [0.2, 0.2, 0.2, 1.0]) 
 227             glcanvas
.glEnable(glcanvas
.GL_LIGHTING
) 
 228             glcanvas
.glEnable(glcanvas
.GL_LIGHT0
) 
 229             glcanvas
.glDepthFunc(glcanvas
.GL_LESS
) 
 230             glcanvas
.glEnable(glcanvas
.GL_DEPTH_TEST
) 
 231             glcanvas
.glClear(glcanvas
.GL_COLOR_BUFFER_BIT | glcanvas
.GL_DEPTH_BUFFER_BIT
) 
 233             glcanvas
.glMatrixMode(glcanvas
.GL_MODELVIEW
); 
 237             # clear color and depth buffers 
 238             glcanvas
.glClear(glcanvas
.GL_COLOR_BUFFER_BIT | glcanvas
.GL_DEPTH_BUFFER_BIT
); 
 239             # use a fresh transformation matrix 
 240             glcanvas
.glPushMatrix() 
 242             glcanvas
.glTranslate(0.0, 0.0, -2.0); 
 243             glcanvas
.glRotate(30.0, 1.0, 0.0, 0.0); 
 244             glcanvas
.glRotate(30.0, 0.0, 1.0, 0.0); 
 246             glcanvas
.glTranslate(0, -1, 0) 
 247             glcanvas
.glRotate(250, 1, 0, 0) 
 248             glcanvas
.glutSolidCone(0.5, 1, 30, 5) 
 249             glcanvas
.glPopMatrix() 
 250             glcanvas
.glRotatef((self
.lasty 
- self
.y
)/100., 0.0, 0.0, 1.0); 
 251             glcanvas
.glRotatef(0.0, (self
.lastx 
- self
.x
)/100., 1.0, 0.0); 
 252             # push into visible buffer 
 258 #---------------------------------------------------------------------- 
 266 #---------------------------------------------------------------------- 
 271             frame 
= wx
.Frame(None, -1, "GL Demos", wx
.DefaultPosition
, (600,300)) 
 272             #win = ConeCanvas(frame) 
 275             self
.SetTopWindow(frame
) 
 281 if __name__ 
== '__main__':