6 from wx
import glcanvas
12 # The Python OpenGL package can be found at
13 # http://PyOpenGL.sourceforge.net/
14 from OpenGL
.GL
import *
15 from OpenGL
.GLUT
import *
20 #----------------------------------------------------------------------
24 wx
.NewId() : ('CubeCanvas', 'Cube'),
25 wx
.NewId() : ('ConeCanvas', 'Cone'),
28 class ButtonPanel(wx
.Panel
):
29 def __init__(self
, parent
, log
):
30 wx
.Panel
.__init
__(self
, parent
, -1)
33 box
= wx
.BoxSizer(wx
.VERTICAL
)
35 keys
= buttonDefs
.keys()
38 text
= buttonDefs
[k
][1]
39 btn
= wx
.Button(self
, k
, text
)
40 box
.Add(btn
, 0, wx
.ALIGN_CENTER|wx
.ALL
, 15)
41 self
.Bind(wx
.EVT_BUTTON
, self
.OnButton
, btn
)
43 #** Enable this to show putting a GLCanvas on the wx.Panel
47 box
.Add(c
, 0, wx
.ALIGN_CENTER|wx
.ALL
, 15)
49 self
.SetAutoLayout(True)
53 def OnButton(self
, evt
):
55 dlg
= wx
.MessageDialog(self
,
56 'The GLCanvas class has not been included with this build of wxPython!',
57 'Sorry', wx
.OK | wx
.ICON_WARNING
)
62 dlg
= wx
.MessageDialog(self
,
63 'The OpenGL package was not found. You can get it at\n'
64 'http://PyOpenGL.sourceforge.net/',
65 'Sorry', wx
.OK | wx
.ICON_WARNING
)
70 canvasClassName
= buttonDefs
[evt
.GetId()][0]
71 canvasClass
= eval(canvasClassName
)
72 frame
= wx
.Frame(None, -1, canvasClassName
, size
=(400,400))
73 canvas
= canvasClass(frame
)
77 class MyCanvasBase(glcanvas
.GLCanvas
):
78 def __init__(self
, parent
):
79 glcanvas
.GLCanvas
.__init
__(self
, parent
, -1)
81 # initial mouse position
82 self
.lastx
= self
.x
= 30
83 self
.lasty
= self
.y
= 30
85 self
.Bind(wx
.EVT_ERASE_BACKGROUND
, self
.OnEraseBackground
)
86 self
.Bind(wx
.EVT_SIZE
, self
.OnSize
)
87 self
.Bind(wx
.EVT_PAINT
, self
.OnPaint
)
88 self
.Bind(wx
.EVT_LEFT_DOWN
, self
.OnMouseDown
)
89 self
.Bind(wx
.EVT_LEFT_UP
, self
.OnMouseUp
)
90 self
.Bind(wx
.EVT_MOTION
, self
.OnMouseMotion
)
93 def OnEraseBackground(self
, event
):
94 pass # Do nothing, to avoid flashing on MSW.
97 def OnSize(self
, event
):
98 size
= self
.size
= self
.GetClientSize()
101 glViewport(0, 0, size
.width
, size
.height
)
105 def OnPaint(self
, event
):
106 dc
= wx
.PaintDC(self
)
114 def OnMouseDown(self
, evt
):
116 self
.x
, self
.y
= self
.lastx
, self
.lasty
= evt
.GetPosition()
119 def OnMouseUp(self
, evt
):
123 def OnMouseMotion(self
, evt
):
124 if evt
.Dragging() and evt
.LeftIsDown():
125 self
.lastx
, self
.lasty
= self
.x
, self
.y
126 self
.x
, self
.y
= evt
.GetPosition()
132 class CubeCanvas(MyCanvasBase
):
134 # set viewing projection
135 glMatrixMode(GL_PROJECTION
)
136 glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0)
139 glMatrixMode(GL_MODELVIEW
)
140 glTranslatef(0.0, 0.0, -2.0)
143 glRotatef(self
.y
, 1.0, 0.0, 0.0)
144 glRotatef(self
.x
, 0.0, 1.0, 0.0)
146 glEnable(GL_DEPTH_TEST
)
147 glEnable(GL_LIGHTING
)
152 # clear color and depth buffers
153 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
)
155 # draw six faces of a cube
157 glNormal3f( 0.0, 0.0, 1.0)
158 glVertex3f( 0.5, 0.5, 0.5)
159 glVertex3f(-0.5, 0.5, 0.5)
160 glVertex3f(-0.5,-0.5, 0.5)
161 glVertex3f( 0.5,-0.5, 0.5)
163 glNormal3f( 0.0, 0.0,-1.0)
164 glVertex3f(-0.5,-0.5,-0.5)
165 glVertex3f(-0.5, 0.5,-0.5)
166 glVertex3f( 0.5, 0.5,-0.5)
167 glVertex3f( 0.5,-0.5,-0.5)
169 glNormal3f( 0.0, 1.0, 0.0)
170 glVertex3f( 0.5, 0.5, 0.5)
171 glVertex3f( 0.5, 0.5,-0.5)
172 glVertex3f(-0.5, 0.5,-0.5)
173 glVertex3f(-0.5, 0.5, 0.5)
175 glNormal3f( 0.0,-1.0, 0.0)
176 glVertex3f(-0.5,-0.5,-0.5)
177 glVertex3f( 0.5,-0.5,-0.5)
178 glVertex3f( 0.5,-0.5, 0.5)
179 glVertex3f(-0.5,-0.5, 0.5)
181 glNormal3f( 1.0, 0.0, 0.0)
182 glVertex3f( 0.5, 0.5, 0.5)
183 glVertex3f( 0.5,-0.5, 0.5)
184 glVertex3f( 0.5,-0.5,-0.5)
185 glVertex3f( 0.5, 0.5,-0.5)
187 glNormal3f(-1.0, 0.0, 0.0)
188 glVertex3f(-0.5,-0.5,-0.5)
189 glVertex3f(-0.5,-0.5, 0.5)
190 glVertex3f(-0.5, 0.5, 0.5)
191 glVertex3f(-0.5, 0.5,-0.5)
194 if self
.size
is None:
195 self
.size
= self
.GetClientSize()
201 glRotatef((self
.y
- self
.lasty
) * yScale
, 1.0, 0.0, 0.0);
202 glRotatef((self
.x
- self
.lastx
) * xScale
, 0.0, 1.0, 0.0);
210 class ConeCanvas(MyCanvasBase
):
212 glMatrixMode(GL_PROJECTION
)
213 # camera frustrum setup
214 glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0)
215 glMaterial(GL_FRONT
, GL_AMBIENT
, [0.2, 0.2, 0.2, 1.0])
216 glMaterial(GL_FRONT
, GL_DIFFUSE
, [0.8, 0.8, 0.8, 1.0])
217 glMaterial(GL_FRONT
, GL_SPECULAR
, [1.0, 0.0, 1.0, 1.0])
218 glMaterial(GL_FRONT
, GL_SHININESS
, 50.0)
219 glLight(GL_LIGHT0
, GL_AMBIENT
, [0.0, 1.0, 0.0, 1.0])
220 glLight(GL_LIGHT0
, GL_DIFFUSE
, [1.0, 1.0, 1.0, 1.0])
221 glLight(GL_LIGHT0
, GL_SPECULAR
, [1.0, 1.0, 1.0, 1.0])
222 glLight(GL_LIGHT0
, GL_POSITION
, [1.0, 1.0, 1.0, 0.0])
223 glLightModelfv(GL_LIGHT_MODEL_AMBIENT
, [0.2, 0.2, 0.2, 1.0])
224 glEnable(GL_LIGHTING
)
227 glEnable(GL_DEPTH_TEST
)
228 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
)
230 glMatrixMode(GL_MODELVIEW
)
232 glTranslatef(0.0, 0.0, -2.0);
238 # clear color and depth buffers
239 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
)
240 # use a fresh transformation matrix
243 #glTranslate(0.0, 0.0, -2.0)
244 glRotate(30.0, 1.0, 0.0, 0.0)
245 glRotate(30.0, 0.0, 1.0, 0.0)
247 glTranslate(0, -1, 0)
248 glRotate(250, 1, 0, 0)
249 glutSolidCone(0.5, 1, 30, 5)
251 glRotatef((self
.y
- self
.lasty
), 0.0, 0.0, 1.0);
252 glRotatef((self
.x
- self
.lastx
), 1.0, 0.0, 0.0);
253 # push into visible buffer
259 #----------------------------------------------------------------------
262 def runTest(frame
, nb
, log
):
263 win
= ButtonPanel(nb
, log
)
276 if __name__
== '__main__':
279 run
.main(['', os
.path
.basename(sys
.argv
[0])] + sys
.argv
[1:])
284 #----------------------------------------------------------------------