Upadted GLCanvas demo

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@25358 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Robin Dunn
2004-01-23 22:49:04 +00:00
parent 6ab5b2e6e0
commit 41378d3480

View File

@@ -1,15 +1,8 @@
# 11/18/2003 - Jeff Grimmett (grimmtooth@softhome.net)
#
# o Updated for wx namespace
# o Note: unable to install PyOpenGL on my system as I am running Python 2.3
# and PyOpenGL does not support anything above Python 2.2. Did what I could,
# but odds are good there are problems.
#
import wx import wx
try: try:
import wx.glcanvas as glcanvas from wx import glcanvas
haveGLCanvas = True haveGLCanvas = True
except ImportError: except ImportError:
haveGLCanvas = False haveGLCanvas = False
@@ -17,9 +10,8 @@ except ImportError:
try: try:
# The Python OpenGL package can be found at # The Python OpenGL package can be found at
# http://PyOpenGL.sourceforge.net/ # http://PyOpenGL.sourceforge.net/
from OpenGL.GL import *
import OpenGL.GL as gl from OpenGL.GLUT import *
import OpenGL.GLUT as glut
haveOpenGL = True haveOpenGL = True
except ImportError: except ImportError:
haveOpenGL = False haveOpenGL = False
@@ -28,25 +20,23 @@ except ImportError:
if not haveGLCanvas: if not haveGLCanvas:
def runTest(frame, nb, log): def runTest(frame, nb, log):
dlg = wx.MessageDialog( dlg = wx.MessageDialog(frame, 'The GLCanvas class has not been included with this build of wxPython!',
frame, 'The wx.GLCanvas has not been included with this build of wxPython!', 'Sorry', wx.OK | wx.ICON_INFORMATION)
'Sorry', wx.OK | wx.ICON_INFORMATION
)
dlg.ShowModal() dlg.ShowModal()
dlg.Destroy() dlg.Destroy()
elif not haveOpenGL: elif not haveOpenGL:
def runTest(frame, nb, log): def runTest(frame, nb, log):
dlg = wx.MessageDialog( dlg = wx.MessageDialog(frame,
frame, 'The OpenGL package was not found. You can get it at\n' 'The OpenGL package was not found. You can get it at\n'
'http://PyOpenGL.sourceforge.net/', 'Sorry', wx.OK | wx.ICON_INFORMATION 'http://PyOpenGL.sourceforge.net/',
) 'Sorry', wx.OK | wx.ICON_INFORMATION)
dlg.ShowModal() dlg.ShowModal()
dlg.Destroy() dlg.Destroy()
else: else:
buttonDefs = { buttonDefs = {
wx.NewId() : ('CubeCanvas', 'Cube'), wx.NewId() : ('CubeCanvas', 'Cube'),
@@ -62,14 +52,13 @@ else:
box.Add((20, 30)) box.Add((20, 30))
keys = buttonDefs.keys() keys = buttonDefs.keys()
keys.sort() keys.sort()
for k in keys: for k in keys:
text = buttonDefs[k][1] text = buttonDefs[k][1]
btn = wx.Button(self, k, text) btn = wx.Button(self, k, text)
box.Add(btn, 0, wx.ALIGN_CENTER|wx.ALL, 15) box.Add(btn, 0, wx.ALIGN_CENTER|wx.ALL, 15)
self.Bind(wx.EVT_BUTTON, self.OnButton, id=k) self.Bind(wx.EVT_BUTTON, self.OnButton, btn)
#** Enable this to show putting a wx.GLCanvas on the wxPanel #** Enable this to show putting a GLCanvas on the wx.Panel
if 0: if 0:
c = CubeCanvas(self) c = CubeCanvas(self)
c.SetSize((200, 200)) c.SetSize((200, 200))
@@ -105,7 +94,7 @@ else:
self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground) self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
self.Bind(wx.EVT_SIZE, self.OnSize) self.Bind(wx.EVT_SIZE, self.OnSize)
self.Bind(wx.EVT_PAINT, self.OnPaint) self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(wx.EVT_LEFT_DOWN, self.OnMouseDown) # needs fixing... self.Bind(wx.EVT_LEFT_DOWN, self.OnMouseDown)
self.Bind(wx.EVT_LEFT_UP, self.OnMouseUp) self.Bind(wx.EVT_LEFT_UP, self.OnMouseUp)
self.Bind(wx.EVT_MOTION, self.OnMouseMotion) self.Bind(wx.EVT_MOTION, self.OnMouseMotion)
@@ -114,19 +103,16 @@ else:
def OnSize(self, event): def OnSize(self, event):
size = self.GetClientSize() size = self.GetClientSize()
if self.GetContext(): if self.GetContext():
self.SetCurrent() self.SetCurrent()
glcanvas.glViewport(0, 0, size.width, size.height) glViewport(0, 0, size.width, size.height)
def OnPaint(self, event): def OnPaint(self, event):
dc = wx.PaintDC(self) dc = wx.PaintDC(self)
self.SetCurrent() self.SetCurrent()
if not self.init: if not self.init:
self.InitGL() self.InitGL()
self.init = True self.init = True
self.OnDraw() self.OnDraw()
def OnMouseDown(self, evt): def OnMouseDown(self, evt):
@@ -142,113 +128,118 @@ else:
self.Refresh(False) self.Refresh(False)
class CubeCanvas(MyCanvasBase): class CubeCanvas(MyCanvasBase):
def InitGL(self): def InitGL(self):
# set viewing projection # set viewing projection
glcanvas.glMatrixMode(glcanvas.GL_PROJECTION); glMatrixMode(GL_PROJECTION);
glcanvas.glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0); glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0);
# position viewer # position viewer
glcanvas.glMatrixMode(glcanvas.GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);
glcanvas.glTranslatef(0.0, 0.0, -2.0); glTranslatef(0.0, 0.0, -2.0);
# position object # position object
glcanvas.glRotatef(self.y, 1.0, 0.0, 0.0); glRotatef(self.y, 1.0, 0.0, 0.0);
glcanvas.glRotatef(self.x, 0.0, 1.0, 0.0); glRotatef(self.x, 0.0, 1.0, 0.0);
glcanvas.glEnable(glcanvas.GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glcanvas.glEnable(glcanvas.GL_LIGHTING); glEnable(GL_LIGHTING);
glcanvas.glEnable(glcanvas.GL_LIGHT0); glEnable(GL_LIGHT0);
def OnDraw(self): def OnDraw(self):
# clear color and depth buffers # clear color and depth buffers
glcanvas.glClear(glcanvas.GL_COLOR_BUFFER_BIT | glcanvas.GL_DEPTH_BUFFER_BIT) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
# draw six faces of a cube # draw six faces of a cube
glcanvas.glBegin(glcanvas.GL_QUADS) glBegin(GL_QUADS)
glcanvas.glNormal3f( 0.0, 0.0, 1.0) glNormal3f( 0.0, 0.0, 1.0)
glcanvas.glVertex3f( 0.5, 0.5, 0.5) glVertex3f( 0.5, 0.5, 0.5)
glcanvas.glVertex3f(-0.5, 0.5, 0.5) glVertex3f(-0.5, 0.5, 0.5)
glcanvas.glVertex3f(-0.5,-0.5, 0.5) glVertex3f(-0.5,-0.5, 0.5)
glcanvas.glVertex3f( 0.5,-0.5, 0.5) glVertex3f( 0.5,-0.5, 0.5)
glcanvas.glNormal3f( 0.0, 0.0,-1.0) glNormal3f( 0.0, 0.0,-1.0)
glcanvas.glVertex3f(-0.5,-0.5,-0.5) glVertex3f(-0.5,-0.5,-0.5)
glcanvas.glVertex3f(-0.5, 0.5,-0.5) glVertex3f(-0.5, 0.5,-0.5)
glcanvas.glVertex3f( 0.5, 0.5,-0.5) glVertex3f( 0.5, 0.5,-0.5)
glcanvas.glVertex3f( 0.5,-0.5,-0.5) glVertex3f( 0.5,-0.5,-0.5)
glcanvas.glNormal3f( 0.0, 1.0, 0.0) glNormal3f( 0.0, 1.0, 0.0)
glcanvas.glVertex3f( 0.5, 0.5, 0.5) glVertex3f( 0.5, 0.5, 0.5)
glcanvas.glVertex3f( 0.5, 0.5,-0.5) glVertex3f( 0.5, 0.5,-0.5)
glcanvas.glVertex3f(-0.5, 0.5,-0.5) glVertex3f(-0.5, 0.5,-0.5)
glcanvas.glVertex3f(-0.5, 0.5, 0.5) glVertex3f(-0.5, 0.5, 0.5)
glcanvas.glNormal3f( 0.0,-1.0, 0.0) glNormal3f( 0.0,-1.0, 0.0)
glcanvas.glVertex3f(-0.5,-0.5,-0.5) glVertex3f(-0.5,-0.5,-0.5)
glcanvas.glVertex3f( 0.5,-0.5,-0.5) glVertex3f( 0.5,-0.5,-0.5)
glcanvas.glVertex3f( 0.5,-0.5, 0.5) glVertex3f( 0.5,-0.5, 0.5)
glcanvas.glVertex3f(-0.5,-0.5, 0.5) glVertex3f(-0.5,-0.5, 0.5)
glcanvas.glNormal3f( 1.0, 0.0, 0.0) glNormal3f( 1.0, 0.0, 0.0)
glcanvas.glVertex3f( 0.5, 0.5, 0.5) glVertex3f( 0.5, 0.5, 0.5)
glcanvas.glVertex3f( 0.5,-0.5, 0.5) glVertex3f( 0.5,-0.5, 0.5)
glcanvas.glVertex3f( 0.5,-0.5,-0.5) glVertex3f( 0.5,-0.5,-0.5)
glcanvas.glVertex3f( 0.5, 0.5,-0.5) glVertex3f( 0.5, 0.5,-0.5)
glcanvas.glNormal3f(-1.0, 0.0, 0.0) glNormal3f(-1.0, 0.0, 0.0)
glcanvas.glVertex3f(-0.5,-0.5,-0.5) glVertex3f(-0.5,-0.5,-0.5)
glcanvas.glVertex3f(-0.5,-0.5, 0.5) glVertex3f(-0.5,-0.5, 0.5)
glcanvas.glVertex3f(-0.5, 0.5, 0.5) glVertex3f(-0.5, 0.5, 0.5)
glcanvas.glVertex3f(-0.5, 0.5,-0.5) glVertex3f(-0.5, 0.5,-0.5)
glcanvas.glEnd() glEnd()
glcanvas.glRotatef((self.lasty - self.y)/100., 1.0, 0.0, 0.0); glRotatef((self.lasty - self.y)/100., 1.0, 0.0, 0.0);
glcanvas.glRotatef((self.lastx - self.x)/100., 0.0, 1.0, 0.0); glRotatef((self.lastx - self.x)/100., 0.0, 1.0, 0.0);
self.SwapBuffers() self.SwapBuffers()
class ConeCanvas(MyCanvasBase): class ConeCanvas(MyCanvasBase):
def InitGL( self ): def InitGL( self ):
glcanvas.glMatrixMode(glcanvas.GL_PROJECTION); glMatrixMode(GL_PROJECTION);
# camera frustrum setup # camera frustrum setup
glcanvas.glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0); glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0);
glcanvas.glMaterial(glcanvas.GL_FRONT, glcanvas.GL_AMBIENT, [0.2, 0.2, 0.2, 1.0]) glMaterial(GL_FRONT, GL_AMBIENT, [0.2, 0.2, 0.2, 1.0])
glcanvas.glMaterial(glcanvas.GL_FRONT, glcanvas.GL_DIFFUSE, [0.8, 0.8, 0.8, 1.0]) glMaterial(GL_FRONT, GL_DIFFUSE, [0.8, 0.8, 0.8, 1.0])
glcanvas.glMaterial(glcanvas.GL_FRONT, glcanvas.GL_SPECULAR, [1.0, 0.0, 1.0, 1.0]) glMaterial(GL_FRONT, GL_SPECULAR, [1.0, 0.0, 1.0, 1.0])
glcanvas.glMaterial(glcanvas.GL_FRONT, glcanvas.GL_SHININESS, 50.0) glMaterial(GL_FRONT, GL_SHININESS, 50.0)
glcanvas.glLight(glcanvas.GL_LIGHT0, glcanvas.GL_AMBIENT, [0.0, 1.0, 0.0, 1.0]) glLight(GL_LIGHT0, GL_AMBIENT, [0.0, 1.0, 0.0, 1.0])
glcanvas.glLight(glcanvas.GL_LIGHT0, glcanvas.GL_DIFFUSE, [1.0, 1.0, 1.0, 1.0]) glLight(GL_LIGHT0, GL_DIFFUSE, [1.0, 1.0, 1.0, 1.0])
glcanvas.glLight(glcanvas.GL_LIGHT0, glcanvas.GL_SPECULAR, [1.0, 1.0, 1.0, 1.0]) glLight(GL_LIGHT0, GL_SPECULAR, [1.0, 1.0, 1.0, 1.0])
glcanvas.glLight(glcanvas.GL_LIGHT0, glcanvas.GL_POSITION, [1.0, 1.0, 1.0, 0.0]); glLight(GL_LIGHT0, GL_POSITION, [1.0, 1.0, 1.0, 0.0]);
glcanvas.glLightModel(glcanvas.GL_LIGHT_MODEL_AMBIENT, [0.2, 0.2, 0.2, 1.0]) glLightModel(GL_LIGHT_MODEL_AMBIENT, [0.2, 0.2, 0.2, 1.0])
glcanvas.glEnable(glcanvas.GL_LIGHTING) glEnable(GL_LIGHTING)
glcanvas.glEnable(glcanvas.GL_LIGHT0) glEnable(GL_LIGHT0)
glcanvas.glDepthFunc(glcanvas.GL_LESS) glDepthFunc(GL_LESS)
glcanvas.glEnable(glcanvas.GL_DEPTH_TEST) glEnable(GL_DEPTH_TEST)
glcanvas.glClear(glcanvas.GL_COLOR_BUFFER_BIT | glcanvas.GL_DEPTH_BUFFER_BIT) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
# position viewer # position viewer
glcanvas.glMatrixMode(glcanvas.GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);
def OnDraw(self): def OnDraw(self):
# clear color and depth buffers # clear color and depth buffers
glcanvas.glClear(glcanvas.GL_COLOR_BUFFER_BIT | glcanvas.GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
# use a fresh transformation matrix # use a fresh transformation matrix
glcanvas.glPushMatrix() glPushMatrix()
# position object # position object
glcanvas.glTranslate(0.0, 0.0, -2.0); glTranslate(0.0, 0.0, -2.0);
glcanvas.glRotate(30.0, 1.0, 0.0, 0.0); glRotate(30.0, 1.0, 0.0, 0.0);
glcanvas.glRotate(30.0, 0.0, 1.0, 0.0); glRotate(30.0, 0.0, 1.0, 0.0);
glcanvas.glTranslate(0, -1, 0) glTranslate(0, -1, 0)
glcanvas.glRotate(250, 1, 0, 0) glRotate(250, 1, 0, 0)
glcanvas.glutSolidCone(0.5, 1, 30, 5) glutSolidCone(0.5, 1, 30, 5)
glcanvas.glPopMatrix() glPopMatrix()
glcanvas.glRotatef((self.lasty - self.y)/100., 0.0, 0.0, 1.0); glRotatef((self.lasty - self.y)/100., 0.0, 0.0, 1.0);
glcanvas.glRotatef(0.0, (self.lastx - self.x)/100., 1.0, 0.0); glRotatef(0.0, (self.lastx - self.x)/100., 1.0, 0.0);
# push into visible buffer # push into visible buffer
self.SwapBuffers() self.SwapBuffers()
@@ -259,25 +250,19 @@ else:
overview = """\ overview = """\
""" """
#----------------------------------------------------------------------
def _test():
class MyApp(wx.App):
def OnInit(self):
frame = wx.Frame(None, -1, "GL Demos", wx.DefaultPosition, (600,300))
#win = ConeCanvas(frame)
MySplitter(frame)
frame.Show(True)
self.SetTopWindow(frame)
return True
app = MyApp(0)
app.MainLoop()
if __name__ == '__main__': if __name__ == '__main__':
_test() import sys,os
import run
run.main(['', os.path.basename(sys.argv[0])])
#----------------------------------------------------------------------