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 self
.context
= glcanvas
.GLContext(self
)
83 # initial mouse position
84 self
.lastx
= self
.x
= 30
85 self
.lasty
= self
.y
= 30
87 self
.Bind(wx
.EVT_ERASE_BACKGROUND
, self
.OnEraseBackground
)
88 self
.Bind(wx
.EVT_SIZE
, self
.OnSize
)
89 self
.Bind(wx
.EVT_PAINT
, self
.OnPaint
)
90 self
.Bind(wx
.EVT_LEFT_DOWN
, self
.OnMouseDown
)
91 self
.Bind(wx
.EVT_LEFT_UP
, self
.OnMouseUp
)
92 self
.Bind(wx
.EVT_MOTION
, self
.OnMouseMotion
)
95 def OnEraseBackground(self
, event
):
96 pass # Do nothing, to avoid flashing on MSW.
99 def OnSize(self
, event
):
100 wx
.CallAfter(self
.DoSetViewport
)
103 def DoSetViewport(self
):
104 size
= self
.size
= self
.GetClientSize()
105 self
.SetCurrent(self
.context
)
106 glViewport(0, 0, size
.width
, size
.height
)
110 def OnPaint(self
, event
):
111 dc
= wx
.PaintDC(self
)
112 self
.SetCurrent(self
.context
)
119 def OnMouseDown(self
, evt
):
121 self
.x
, self
.y
= self
.lastx
, self
.lasty
= evt
.GetPosition()
124 def OnMouseUp(self
, evt
):
128 def OnMouseMotion(self
, evt
):
129 if evt
.Dragging() and evt
.LeftIsDown():
130 self
.lastx
, self
.lasty
= self
.x
, self
.y
131 self
.x
, self
.y
= evt
.GetPosition()
137 class CubeCanvas(MyCanvasBase
):
139 # set viewing projection
140 glMatrixMode(GL_PROJECTION
)
141 glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0)
144 glMatrixMode(GL_MODELVIEW
)
145 glTranslatef(0.0, 0.0, -2.0)
148 glRotatef(self
.y
, 1.0, 0.0, 0.0)
149 glRotatef(self
.x
, 0.0, 1.0, 0.0)
151 glEnable(GL_DEPTH_TEST
)
152 glEnable(GL_LIGHTING
)
157 # clear color and depth buffers
158 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
)
160 # draw six faces of a cube
162 glNormal3f( 0.0, 0.0, 1.0)
163 glVertex3f( 0.5, 0.5, 0.5)
164 glVertex3f(-0.5, 0.5, 0.5)
165 glVertex3f(-0.5,-0.5, 0.5)
166 glVertex3f( 0.5,-0.5, 0.5)
168 glNormal3f( 0.0, 0.0,-1.0)
169 glVertex3f(-0.5,-0.5,-0.5)
170 glVertex3f(-0.5, 0.5,-0.5)
171 glVertex3f( 0.5, 0.5,-0.5)
172 glVertex3f( 0.5,-0.5,-0.5)
174 glNormal3f( 0.0, 1.0, 0.0)
175 glVertex3f( 0.5, 0.5, 0.5)
176 glVertex3f( 0.5, 0.5,-0.5)
177 glVertex3f(-0.5, 0.5,-0.5)
178 glVertex3f(-0.5, 0.5, 0.5)
180 glNormal3f( 0.0,-1.0, 0.0)
181 glVertex3f(-0.5,-0.5,-0.5)
182 glVertex3f( 0.5,-0.5,-0.5)
183 glVertex3f( 0.5,-0.5, 0.5)
184 glVertex3f(-0.5,-0.5, 0.5)
186 glNormal3f( 1.0, 0.0, 0.0)
187 glVertex3f( 0.5, 0.5, 0.5)
188 glVertex3f( 0.5,-0.5, 0.5)
189 glVertex3f( 0.5,-0.5,-0.5)
190 glVertex3f( 0.5, 0.5,-0.5)
192 glNormal3f(-1.0, 0.0, 0.0)
193 glVertex3f(-0.5,-0.5,-0.5)
194 glVertex3f(-0.5,-0.5, 0.5)
195 glVertex3f(-0.5, 0.5, 0.5)
196 glVertex3f(-0.5, 0.5,-0.5)
199 if self
.size
is None:
200 self
.size
= self
.GetClientSize()
206 glRotatef((self
.y
- self
.lasty
) * yScale
, 1.0, 0.0, 0.0);
207 glRotatef((self
.x
- self
.lastx
) * xScale
, 0.0, 1.0, 0.0);
215 class ConeCanvas(MyCanvasBase
):
217 glMatrixMode(GL_PROJECTION
)
218 # camera frustrum setup
219 glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0)
220 glMaterial(GL_FRONT
, GL_AMBIENT
, [0.2, 0.2, 0.2, 1.0])
221 glMaterial(GL_FRONT
, GL_DIFFUSE
, [0.8, 0.8, 0.8, 1.0])
222 glMaterial(GL_FRONT
, GL_SPECULAR
, [1.0, 0.0, 1.0, 1.0])
223 glMaterial(GL_FRONT
, GL_SHININESS
, 50.0)
224 glLight(GL_LIGHT0
, GL_AMBIENT
, [0.0, 1.0, 0.0, 1.0])
225 glLight(GL_LIGHT0
, GL_DIFFUSE
, [1.0, 1.0, 1.0, 1.0])
226 glLight(GL_LIGHT0
, GL_SPECULAR
, [1.0, 1.0, 1.0, 1.0])
227 glLight(GL_LIGHT0
, GL_POSITION
, [1.0, 1.0, 1.0, 0.0])
228 glLightModelfv(GL_LIGHT_MODEL_AMBIENT
, [0.2, 0.2, 0.2, 1.0])
229 glEnable(GL_LIGHTING
)
232 glEnable(GL_DEPTH_TEST
)
233 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
)
235 glMatrixMode(GL_MODELVIEW
)
237 glTranslatef(0.0, 0.0, -2.0);
243 # clear color and depth buffers
244 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
)
245 # use a fresh transformation matrix
248 #glTranslate(0.0, 0.0, -2.0)
249 glRotate(30.0, 1.0, 0.0, 0.0)
250 glRotate(30.0, 0.0, 1.0, 0.0)
252 glTranslate(0, -1, 0)
253 glRotate(250, 1, 0, 0)
254 glutSolidCone(0.5, 1, 30, 5)
256 glRotatef((self
.y
- self
.lasty
), 0.0, 0.0, 1.0);
257 glRotatef((self
.x
- self
.lastx
), 1.0, 0.0, 0.0);
258 # push into visible buffer
264 #----------------------------------------------------------------------
267 def runTest(frame
, nb
, log
):
268 win
= ButtonPanel(nb
, log
)
281 if __name__
== '__main__':
284 run
.main(['', os
.path
.basename(sys
.argv
[0])] + sys
.argv
[1:])
289 #----------------------------------------------------------------------