1999-04-29 23:29:54 -04:00
|
|
|
|
|
|
|
from wxPython.wx import *
|
|
|
|
try:
|
|
|
|
from wxPython.glcanvas import *
|
|
|
|
haveGLCanvas = true
|
|
|
|
except ImportError:
|
|
|
|
haveGLCanvas = false
|
|
|
|
|
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/
|
1999-12-29 17:10:32 -05:00
|
|
|
from OpenGL.GL import *
|
|
|
|
from OpenGL.GLUT import *
|
|
|
|
haveOpenGL = true
|
|
|
|
except ImportError:
|
|
|
|
haveOpenGL = false
|
|
|
|
|
1999-04-29 23:29:54 -04:00
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
if not haveGLCanvas:
|
|
|
|
def runTest(frame, nb, log):
|
|
|
|
dlg = wxMessageDialog(frame, 'The wxGLCanvas has not been included with this build of wxPython!',
|
|
|
|
'Sorry', wxOK | wxICON_INFORMATION)
|
|
|
|
dlg.ShowModal()
|
|
|
|
dlg.Destroy()
|
|
|
|
|
1999-12-29 17:10:32 -05:00
|
|
|
elif not haveOpenGL:
|
|
|
|
def runTest(frame, nb, log):
|
|
|
|
dlg = wxMessageDialog(frame,
|
|
|
|
'The OpenGL package was not found. You can get it at\n'
|
2000-10-30 16:08:42 -05:00
|
|
|
'http://PyOpenGL.sourceforge.net/',
|
1999-12-29 17:10:32 -05:00
|
|
|
'Sorry', wxOK | wxICON_INFORMATION)
|
|
|
|
dlg.ShowModal()
|
|
|
|
dlg.Destroy()
|
1999-04-29 23:29:54 -04:00
|
|
|
|
|
|
|
|
2001-11-12 22:08:53 -05:00
|
|
|
|
|
|
|
|
1999-12-29 17:10:32 -05:00
|
|
|
else:
|
2001-11-12 22:08:53 -05:00
|
|
|
buttonDefs = {
|
|
|
|
wxNewId() : ('CubeCanvas', 'Cube'),
|
|
|
|
wxNewId() : ('ConeCanvas', 'Cone'),
|
|
|
|
}
|
|
|
|
|
|
|
|
class ButtonPanel(wxPanel):
|
|
|
|
def __init__(self, parent, log):
|
|
|
|
wxPanel.__init__(self, parent, -1)
|
|
|
|
self.log = log
|
|
|
|
|
|
|
|
box = wxBoxSizer(wxVERTICAL)
|
|
|
|
box.Add(20, 30)
|
|
|
|
keys = buttonDefs.keys()
|
|
|
|
keys.sort()
|
|
|
|
for k in keys:
|
|
|
|
text = buttonDefs[k][1]
|
|
|
|
btn = wxButton(self, k, text)
|
|
|
|
box.Add(btn, 0, wxALIGN_CENTER|wxALL, 15)
|
|
|
|
EVT_BUTTON(self, k, self.OnButton)
|
|
|
|
|
|
|
|
self.SetAutoLayout(true)
|
|
|
|
self.SetSizer(box)
|
|
|
|
|
|
|
|
def OnButton(self, evt):
|
|
|
|
canvasClassName = buttonDefs[evt.GetId()][0]
|
|
|
|
canvasClass = eval(canvasClassName)
|
|
|
|
frame = wxFrame(None, -1, canvasClassName, size=(400,400))
|
|
|
|
canvas = canvasClass(frame)
|
|
|
|
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
|
|
|
|
2001-11-12 22:08:53 -05:00
|
|
|
class MyCanvasBase(wxGLCanvas):
|
1999-04-29 23:29:54 -04:00
|
|
|
def __init__(self, parent):
|
2001-11-12 22:08:53 -05:00
|
|
|
wxGLCanvas.__init__(self, parent, -1)
|
|
|
|
self.init = false
|
|
|
|
# initial mouse position
|
|
|
|
self.lastx = self.x = 30
|
|
|
|
self.lasty = self.y = 30
|
1999-04-29 23:29:54 -04:00
|
|
|
EVT_ERASE_BACKGROUND(self, self.OnEraseBackground)
|
2000-07-15 15:51:35 -04:00
|
|
|
EVT_SIZE(self, self.OnSize)
|
|
|
|
EVT_PAINT(self, self.OnPaint)
|
2001-11-12 22:08:53 -05:00
|
|
|
#EVT_LEFT_DOWN(self, self.OnMouseDown) # needs fixing...
|
|
|
|
#EVT_LEFT_UP(self, self.OnMouseUp)
|
|
|
|
#EVT_MOTION(self, self.OnMouseMotion)
|
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
|
|
|
|
|
|
|
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()
|
|
|
|
glViewport(0, 0, size.width, size.height)
|
|
|
|
|
|
|
|
def OnPaint(self, event):
|
|
|
|
dc = wxPaintDC(self)
|
|
|
|
self.SetCurrent()
|
|
|
|
if not self.init:
|
|
|
|
self.InitGL()
|
|
|
|
self.init = true
|
2001-11-12 22:08:53 -05:00
|
|
|
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()
|
1999-04-29 23:29:54 -04:00
|
|
|
|
2001-11-12 22:08:53 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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):
|
1999-04-29 23:29:54 -04:00
|
|
|
# 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()
|
|
|
|
|
2001-11-12 22:08:53 -05:00
|
|
|
glRotatef(self.lasty - self.y, 1.0, 0.0, 0.0);
|
|
|
|
glRotatef(self.lastx - self.x, 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-11-12 22:08:53 -05:00
|
|
|
class ConeCanvas(MyCanvasBase):
|
|
|
|
def InitGL( self ):
|
1999-12-24 13:44:29 -05:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
2001-11-12 22:08:53 -05:00
|
|
|
def OnDraw(self):
|
1999-12-24 13:44:29 -05:00
|
|
|
# clear color and depth buffers
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
# position viewer
|
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
|
|
# 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(1, 2, 50, 10)
|
|
|
|
glPopMatrix()
|
|
|
|
# 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
overview = """\
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
def _test():
|
|
|
|
class MyApp(wxApp):
|
|
|
|
def OnInit(self):
|
2001-05-18 17:59:59 -04:00
|
|
|
frame = wxFrame(None, -1, "GL Demos", wxDefaultPosition, wxSize(600,300))
|
1999-12-24 13:44:29 -05:00
|
|
|
#win = ConeCanvas(frame)
|
|
|
|
MySplitter(frame)
|
1999-04-29 23:29:54 -04:00
|
|
|
frame.Show(TRUE)
|
|
|
|
self.SetTopWindow(frame)
|
|
|
|
return TRUE
|
|
|
|
|
|
|
|
app = MyApp(0)
|
|
|
|
app.MainLoop()
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
_test()
|