2003-12-08 20:23:28 -05:00
|
|
|
|
2004-01-23 17:49:04 -05:00
|
|
|
import wx
|
2003-12-08 20:23:28 -05:00
|
|
|
|
1999-04-29 23:29:54 -04:00
|
|
|
try:
|
2004-01-23 17:49:04 -05:00
|
|
|
from wx import glcanvas
|
2003-03-25 01:35:27 -05:00
|
|
|
haveGLCanvas = True
|
1999-04-29 23:29:54 -04:00
|
|
|
except ImportError:
|
2003-03-25 01:35:27 -05:00
|
|
|
haveGLCanvas = False
|
1999-04-29 23:29:54 -04:00
|
|
|
|
1999-12-29 17:10:32 -05:00
|
|
|
try:
|
|
|
|
# The Python OpenGL package can be found at
|
2000-10-30 16:08:42 -05:00
|
|
|
# http://PyOpenGL.sourceforge.net/
|
2004-01-23 17:49:04 -05:00
|
|
|
from OpenGL.GL import *
|
|
|
|
from OpenGL.GLUT import *
|
2003-03-25 01:35:27 -05:00
|
|
|
haveOpenGL = True
|
1999-12-29 17:10:32 -05:00
|
|
|
except ImportError:
|
2003-03-25 01:35:27 -05:00
|
|
|
haveOpenGL = False
|
1999-12-29 17:10:32 -05:00
|
|
|
|
1999-04-29 23:29:54 -04:00
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
if not haveGLCanvas:
|
|
|
|
def runTest(frame, nb, log):
|
2004-01-23 17:49:04 -05:00
|
|
|
dlg = wx.MessageDialog(frame, 'The GLCanvas class has not been included with this build of wxPython!',
|
|
|
|
'Sorry', wx.OK | wx.ICON_INFORMATION)
|
1999-04-29 23:29:54 -04:00
|
|
|
dlg.ShowModal()
|
|
|
|
dlg.Destroy()
|
|
|
|
|
1999-12-29 17:10:32 -05:00
|
|
|
elif not haveOpenGL:
|
|
|
|
def runTest(frame, nb, log):
|
2004-01-23 17:49:04 -05:00
|
|
|
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)
|
1999-12-29 17:10:32 -05:00
|
|
|
dlg.ShowModal()
|
|
|
|
dlg.Destroy()
|
1999-04-29 23:29:54 -04:00
|
|
|
|
|
|
|
|
2004-01-23 17:49:04 -05:00
|
|
|
|
|
|
|
|
1999-12-29 17:10:32 -05:00
|
|
|
else:
|
2001-11-12 22:08:53 -05:00
|
|
|
buttonDefs = {
|
2003-12-08 20:23:28 -05:00
|
|
|
wx.NewId() : ('CubeCanvas', 'Cube'),
|
|
|
|
wx.NewId() : ('ConeCanvas', 'Cone'),
|
2001-11-12 22:08:53 -05:00
|
|
|
}
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
class ButtonPanel(wx.Panel):
|
2001-11-12 22:08:53 -05:00
|
|
|
def __init__(self, parent, log):
|
2003-12-08 20:23:28 -05:00
|
|
|
wx.Panel.__init__(self, parent, -1)
|
2001-11-12 22:08:53 -05:00
|
|
|
self.log = log
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
box = wx.BoxSizer(wx.VERTICAL)
|
|
|
|
box.Add((20, 30))
|
2001-11-12 22:08:53 -05:00
|
|
|
keys = buttonDefs.keys()
|
|
|
|
keys.sort()
|
|
|
|
for k in keys:
|
|
|
|
text = buttonDefs[k][1]
|
2003-12-08 20:23:28 -05:00
|
|
|
btn = wx.Button(self, k, text)
|
|
|
|
box.Add(btn, 0, wx.ALIGN_CENTER|wx.ALL, 15)
|
2004-01-23 17:49:04 -05:00
|
|
|
self.Bind(wx.EVT_BUTTON, self.OnButton, btn)
|
2001-11-12 22:08:53 -05:00
|
|
|
|
2004-01-23 17:49:04 -05:00
|
|
|
#** Enable this to show putting a GLCanvas on the wx.Panel
|
2002-06-13 12:57:18 -04:00
|
|
|
if 0:
|
|
|
|
c = CubeCanvas(self)
|
|
|
|
c.SetSize((200, 200))
|
2003-12-08 20:23:28 -05:00
|
|
|
box.Add(c, 0, wx.ALIGN_CENTER|wx.ALL, 15)
|
2002-06-13 12:57:18 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
self.SetAutoLayout(True)
|
2001-11-12 22:08:53 -05:00
|
|
|
self.SetSizer(box)
|
|
|
|
|
2002-06-13 12:57:18 -04:00
|
|
|
|
2001-11-12 22:08:53 -05:00
|
|
|
def OnButton(self, evt):
|
|
|
|
canvasClassName = buttonDefs[evt.GetId()][0]
|
|
|
|
canvasClass = eval(canvasClassName)
|
2003-12-08 20:23:28 -05:00
|
|
|
frame = wx.Frame(None, -1, canvasClassName, size=(400,400))
|
2001-11-12 22:08:53 -05:00
|
|
|
canvas = canvasClass(frame)
|
2003-03-25 01:35:27 -05:00
|
|
|
frame.Show(True)
|
1999-04-29 23:29:54 -04:00
|
|
|
|
|
|
|
|
1999-12-24 13:44:29 -05:00
|
|
|
|
2001-11-12 22:08:53 -05:00
|
|
|
def runTest(frame, nb, log):
|
|
|
|
win = ButtonPanel(nb, log)
|
|
|
|
return win
|
1999-12-24 13:44:29 -05:00
|
|
|
|
1999-04-29 23:29:54 -04:00
|
|
|
|
1999-12-24 13:44:29 -05:00
|
|
|
|
2002-06-13 12:51:53 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
class MyCanvasBase(glcanvas.GLCanvas):
|
1999-04-29 23:29:54 -04:00
|
|
|
def __init__(self, parent):
|
2003-12-08 20:23:28 -05:00
|
|
|
glcanvas.GLCanvas.__init__(self, parent, -1)
|
2003-03-25 01:35:27 -05:00
|
|
|
self.init = False
|
2001-11-12 22:08:53 -05:00
|
|
|
# initial mouse position
|
|
|
|
self.lastx = self.x = 30
|
|
|
|
self.lasty = self.y = 30
|
2003-12-08 20:23:28 -05:00
|
|
|
self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
|
|
|
|
self.Bind(wx.EVT_SIZE, self.OnSize)
|
|
|
|
self.Bind(wx.EVT_PAINT, self.OnPaint)
|
2004-01-23 17:49:04 -05:00
|
|
|
self.Bind(wx.EVT_LEFT_DOWN, self.OnMouseDown)
|
2003-12-08 20:23:28 -05:00
|
|
|
self.Bind(wx.EVT_LEFT_UP, self.OnMouseUp)
|
|
|
|
self.Bind(wx.EVT_MOTION, self.OnMouseMotion)
|
1999-04-29 23:29:54 -04:00
|
|
|
|
2004-02-26 14:33:48 -05:00
|
|
|
|
1999-04-29 23:29:54 -04:00
|
|
|
def OnEraseBackground(self, event):
|
2001-11-12 22:08:53 -05:00
|
|
|
pass # Do nothing, to avoid flashing on MSW.
|
1999-04-29 23:29:54 -04:00
|
|
|
|
2004-02-26 14:33:48 -05:00
|
|
|
|
1999-04-29 23:29:54 -04:00
|
|
|
def OnSize(self, event):
|
|
|
|
size = self.GetClientSize()
|
1999-12-24 13:44:29 -05:00
|
|
|
if self.GetContext():
|
1999-04-29 23:29:54 -04:00
|
|
|
self.SetCurrent()
|
2004-01-23 17:49:04 -05:00
|
|
|
glViewport(0, 0, size.width, size.height)
|
2004-02-26 14:33:48 -05:00
|
|
|
event.Skip()
|
|
|
|
|
1999-04-29 23:29:54 -04:00
|
|
|
|
|
|
|
def OnPaint(self, event):
|
2003-12-08 20:23:28 -05:00
|
|
|
dc = wx.PaintDC(self)
|
1999-04-29 23:29:54 -04:00
|
|
|
self.SetCurrent()
|
|
|
|
if not self.init:
|
|
|
|
self.InitGL()
|
2003-03-25 01:35:27 -05:00
|
|
|
self.init = True
|
2001-11-12 22:08:53 -05:00
|
|
|
self.OnDraw()
|
|
|
|
|
2004-02-26 14:33:48 -05:00
|
|
|
|
2001-11-12 22:08:53 -05:00
|
|
|
def OnMouseDown(self, evt):
|
|
|
|
self.CaptureMouse()
|
|
|
|
|
2004-02-26 14:33:48 -05:00
|
|
|
|
2001-11-12 22:08:53 -05:00
|
|
|
def OnMouseUp(self, evt):
|
|
|
|
self.ReleaseMouse()
|
|
|
|
|
2004-02-26 14:33:48 -05:00
|
|
|
|
2001-11-12 22:08:53 -05:00
|
|
|
def OnMouseMotion(self, evt):
|
|
|
|
if evt.Dragging() and evt.LeftIsDown():
|
|
|
|
self.x, self.y = self.lastx, self.lasty
|
|
|
|
self.x, self.y = evt.GetPosition()
|
2003-03-25 01:35:27 -05:00
|
|
|
self.Refresh(False)
|
1999-04-29 23:29:54 -04:00
|
|
|
|
2001-11-12 22:08:53 -05:00
|
|
|
|
2004-01-23 17:49:04 -05:00
|
|
|
|
|
|
|
|
2001-11-12 22:08:53 -05:00
|
|
|
class CubeCanvas(MyCanvasBase):
|
|
|
|
def InitGL(self):
|
|
|
|
# set viewing projection
|
2004-01-23 17:49:04 -05:00
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
|
|
glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0);
|
2001-11-12 22:08:53 -05:00
|
|
|
|
|
|
|
# position viewer
|
2004-01-23 17:49:04 -05:00
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
|
|
glTranslatef(0.0, 0.0, -2.0);
|
2001-11-12 22:08:53 -05:00
|
|
|
|
|
|
|
# position object
|
2004-01-23 17:49:04 -05:00
|
|
|
glRotatef(self.y, 1.0, 0.0, 0.0);
|
|
|
|
glRotatef(self.x, 0.0, 1.0, 0.0);
|
2001-11-12 22:08:53 -05:00
|
|
|
|
2004-01-23 17:49:04 -05:00
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
glEnable(GL_LIGHTING);
|
|
|
|
glEnable(GL_LIGHT0);
|
2001-11-12 22:08:53 -05:00
|
|
|
|
|
|
|
|
|
|
|
def OnDraw(self):
|
1999-04-29 23:29:54 -04:00
|
|
|
# clear color and depth buffers
|
2004-01-23 17:49:04 -05:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
1999-04-29 23:29:54 -04:00
|
|
|
|
|
|
|
# draw six faces of a cube
|
2004-01-23 17:49:04 -05:00
|
|
|
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);
|
1999-04-29 23:29:54 -04:00
|
|
|
|
2001-11-12 22:08:53 -05:00
|
|
|
self.SwapBuffers()
|
1999-04-29 23:29:54 -04:00
|
|
|
|
|
|
|
|
2004-01-23 17:49:04 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
2001-11-12 22:08:53 -05:00
|
|
|
class ConeCanvas(MyCanvasBase):
|
|
|
|
def InitGL( self ):
|
2004-01-23 17:49:04 -05:00
|
|
|
glMatrixMode(GL_PROJECTION);
|
1999-12-24 13:44:29 -05:00
|
|
|
# camera frustrum setup
|
2004-01-23 17:49:04 -05:00
|
|
|
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)
|
2002-06-13 12:51:53 -04:00
|
|
|
# position viewer
|
2004-01-23 17:49:04 -05:00
|
|
|
glMatrixMode(GL_MODELVIEW);
|
1999-12-24 13:44:29 -05:00
|
|
|
|
|
|
|
|
2001-11-12 22:08:53 -05:00
|
|
|
def OnDraw(self):
|
1999-12-24 13:44:29 -05:00
|
|
|
# clear color and depth buffers
|
2004-01-23 17:49:04 -05:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
1999-12-24 13:44:29 -05:00
|
|
|
# use a fresh transformation matrix
|
2004-01-23 17:49:04 -05:00
|
|
|
glPushMatrix()
|
1999-12-24 13:44:29 -05:00
|
|
|
# position object
|
2004-01-23 17:49:04 -05:00
|
|
|
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);
|
1999-12-24 13:44:29 -05:00
|
|
|
# push into visible buffer
|
|
|
|
self.SwapBuffers()
|
1999-04-29 23:29:54 -04:00
|
|
|
|
|
|
|
|
2001-11-12 22:08:53 -05:00
|
|
|
|
|
|
|
|
1999-09-17 01:55:00 -04:00
|
|
|
#----------------------------------------------------------------------
|
1999-04-29 23:29:54 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2004-01-23 17:49:04 -05:00
|
|
|
|
1999-04-29 23:29:54 -04:00
|
|
|
overview = """\
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
2004-01-23 17:49:04 -05:00
|
|
|
if __name__ == '__main__':
|
|
|
|
import sys,os
|
|
|
|
import run
|
2004-03-04 19:06:33 -05:00
|
|
|
run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
|
1999-04-29 23:29:54 -04:00
|
|
|
|
|
|
|
|
2004-01-23 17:49:04 -05:00
|
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
2002-06-13 12:51:53 -04:00
|
|
|
|