]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/GLCanvas.py
Stop the timer when the demo is shutdown
[wxWidgets.git] / wxPython / demo / GLCanvas.py
1 # 11/18/2003 - Jeff Grimmett (grimmtooth@softhome.net)
2 #
3 # o Updated for wx namespace
4 # o Note: unable to install PyOpenGL on my system as I am running Python 2.3
5 # and PyOpenGL does not support anything above Python 2.2. Did what I could,
6 # but odds are good there are problems.
7 #
8
9 import wx
10
11 try:
12 import wx.glcanvas as glcanvas
13 haveGLCanvas = True
14 except ImportError:
15 haveGLCanvas = False
16
17 try:
18 # The Python OpenGL package can be found at
19 # http://PyOpenGL.sourceforge.net/
20
21 import OpenGL.GL as gl
22 import OpenGL.GLUT as glut
23 haveOpenGL = True
24 except ImportError:
25 haveOpenGL = False
26
27 #----------------------------------------------------------------------
28
29 if not haveGLCanvas:
30 def runTest(frame, nb, log):
31 dlg = wx.MessageDialog(
32 frame, 'The wx.GLCanvas has not been included with this build of wxPython!',
33 'Sorry', wx.OK | wx.ICON_INFORMATION
34 )
35
36 dlg.ShowModal()
37 dlg.Destroy()
38
39 elif not haveOpenGL:
40 def runTest(frame, nb, log):
41 dlg = wx.MessageDialog(
42 frame, 'The OpenGL package was not found. You can get it at\n'
43 'http://PyOpenGL.sourceforge.net/', 'Sorry', wx.OK | wx.ICON_INFORMATION
44 )
45
46 dlg.ShowModal()
47 dlg.Destroy()
48
49
50 else:
51 buttonDefs = {
52 wx.NewId() : ('CubeCanvas', 'Cube'),
53 wx.NewId() : ('ConeCanvas', 'Cone'),
54 }
55
56 class ButtonPanel(wx.Panel):
57 def __init__(self, parent, log):
58 wx.Panel.__init__(self, parent, -1)
59 self.log = log
60
61 box = wx.BoxSizer(wx.VERTICAL)
62 box.Add((20, 30))
63 keys = buttonDefs.keys()
64 keys.sort()
65
66 for k in keys:
67 text = buttonDefs[k][1]
68 btn = wx.Button(self, k, text)
69 box.Add(btn, 0, wx.ALIGN_CENTER|wx.ALL, 15)
70 self.Bind(wx.EVT_BUTTON, self.OnButton, id=k)
71
72 #** Enable this to show putting a wx.GLCanvas on the wxPanel
73 if 0:
74 c = CubeCanvas(self)
75 c.SetSize((200, 200))
76 box.Add(c, 0, wx.ALIGN_CENTER|wx.ALL, 15)
77
78 self.SetAutoLayout(True)
79 self.SetSizer(box)
80
81
82 def OnButton(self, evt):
83 canvasClassName = buttonDefs[evt.GetId()][0]
84 canvasClass = eval(canvasClassName)
85 frame = wx.Frame(None, -1, canvasClassName, size=(400,400))
86 canvas = canvasClass(frame)
87 frame.Show(True)
88
89
90
91 def runTest(frame, nb, log):
92 win = ButtonPanel(nb, log)
93 return win
94
95
96
97
98 class MyCanvasBase(glcanvas.GLCanvas):
99 def __init__(self, parent):
100 glcanvas.GLCanvas.__init__(self, parent, -1)
101 self.init = False
102 # initial mouse position
103 self.lastx = self.x = 30
104 self.lasty = self.y = 30
105 self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
106 self.Bind(wx.EVT_SIZE, self.OnSize)
107 self.Bind(wx.EVT_PAINT, self.OnPaint)
108 self.Bind(wx.EVT_LEFT_DOWN, self.OnMouseDown) # needs fixing...
109 self.Bind(wx.EVT_LEFT_UP, self.OnMouseUp)
110 self.Bind(wx.EVT_MOTION, self.OnMouseMotion)
111
112 def OnEraseBackground(self, event):
113 pass # Do nothing, to avoid flashing on MSW.
114
115 def OnSize(self, event):
116 size = self.GetClientSize()
117
118 if self.GetContext():
119 self.SetCurrent()
120 glcanvas.glViewport(0, 0, size.width, size.height)
121
122 def OnPaint(self, event):
123 dc = wx.PaintDC(self)
124 self.SetCurrent()
125
126 if not self.init:
127 self.InitGL()
128 self.init = True
129
130 self.OnDraw()
131
132 def OnMouseDown(self, evt):
133 self.CaptureMouse()
134
135 def OnMouseUp(self, evt):
136 self.ReleaseMouse()
137
138 def OnMouseMotion(self, evt):
139 if evt.Dragging() and evt.LeftIsDown():
140 self.x, self.y = self.lastx, self.lasty
141 self.x, self.y = evt.GetPosition()
142 self.Refresh(False)
143
144
145 class CubeCanvas(MyCanvasBase):
146 def InitGL(self):
147 # set viewing projection
148 glcanvas.glMatrixMode(glcanvas.GL_PROJECTION);
149 glcanvas.glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0);
150
151 # position viewer
152 glcanvas.glMatrixMode(glcanvas.GL_MODELVIEW);
153 glcanvas.glTranslatef(0.0, 0.0, -2.0);
154
155 # position object
156 glcanvas.glRotatef(self.y, 1.0, 0.0, 0.0);
157 glcanvas.glRotatef(self.x, 0.0, 1.0, 0.0);
158
159 glcanvas.glEnable(glcanvas.GL_DEPTH_TEST);
160 glcanvas.glEnable(glcanvas.GL_LIGHTING);
161 glcanvas.glEnable(glcanvas.GL_LIGHT0);
162
163
164 def OnDraw(self):
165 # clear color and depth buffers
166 glcanvas.glClear(glcanvas.GL_COLOR_BUFFER_BIT | glcanvas.GL_DEPTH_BUFFER_BIT)
167
168 # draw six faces of a cube
169 glcanvas.glBegin(glcanvas.GL_QUADS)
170 glcanvas.glNormal3f( 0.0, 0.0, 1.0)
171 glcanvas.glVertex3f( 0.5, 0.5, 0.5)
172 glcanvas.glVertex3f(-0.5, 0.5, 0.5)
173 glcanvas.glVertex3f(-0.5,-0.5, 0.5)
174 glcanvas.glVertex3f( 0.5,-0.5, 0.5)
175
176 glcanvas.glNormal3f( 0.0, 0.0,-1.0)
177 glcanvas.glVertex3f(-0.5,-0.5,-0.5)
178 glcanvas.glVertex3f(-0.5, 0.5,-0.5)
179 glcanvas.glVertex3f( 0.5, 0.5,-0.5)
180 glcanvas.glVertex3f( 0.5,-0.5,-0.5)
181
182 glcanvas.glNormal3f( 0.0, 1.0, 0.0)
183 glcanvas.glVertex3f( 0.5, 0.5, 0.5)
184 glcanvas.glVertex3f( 0.5, 0.5,-0.5)
185 glcanvas.glVertex3f(-0.5, 0.5,-0.5)
186 glcanvas.glVertex3f(-0.5, 0.5, 0.5)
187
188 glcanvas.glNormal3f( 0.0,-1.0, 0.0)
189 glcanvas.glVertex3f(-0.5,-0.5,-0.5)
190 glcanvas.glVertex3f( 0.5,-0.5,-0.5)
191 glcanvas.glVertex3f( 0.5,-0.5, 0.5)
192 glcanvas.glVertex3f(-0.5,-0.5, 0.5)
193
194 glcanvas.glNormal3f( 1.0, 0.0, 0.0)
195 glcanvas.glVertex3f( 0.5, 0.5, 0.5)
196 glcanvas.glVertex3f( 0.5,-0.5, 0.5)
197 glcanvas.glVertex3f( 0.5,-0.5,-0.5)
198 glcanvas.glVertex3f( 0.5, 0.5,-0.5)
199
200 glcanvas.glNormal3f(-1.0, 0.0, 0.0)
201 glcanvas.glVertex3f(-0.5,-0.5,-0.5)
202 glcanvas.glVertex3f(-0.5,-0.5, 0.5)
203 glcanvas.glVertex3f(-0.5, 0.5, 0.5)
204 glcanvas.glVertex3f(-0.5, 0.5,-0.5)
205 glcanvas.glEnd()
206
207 glcanvas.glRotatef((self.lasty - self.y)/100., 1.0, 0.0, 0.0);
208 glcanvas.glRotatef((self.lastx - self.x)/100., 0.0, 1.0, 0.0);
209
210 self.SwapBuffers()
211
212
213 class ConeCanvas(MyCanvasBase):
214 def InitGL( self ):
215 glcanvas.glMatrixMode(glcanvas.GL_PROJECTION);
216 # camera frustrum setup
217 glcanvas.glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0);
218 glcanvas.glMaterial(glcanvas.GL_FRONT, glcanvas.GL_AMBIENT, [0.2, 0.2, 0.2, 1.0])
219 glcanvas.glMaterial(glcanvas.GL_FRONT, glcanvas.GL_DIFFUSE, [0.8, 0.8, 0.8, 1.0])
220 glcanvas.glMaterial(glcanvas.GL_FRONT, glcanvas.GL_SPECULAR, [1.0, 0.0, 1.0, 1.0])
221 glcanvas.glMaterial(glcanvas.GL_FRONT, glcanvas.GL_SHININESS, 50.0)
222 glcanvas.glLight(glcanvas.GL_LIGHT0, glcanvas.GL_AMBIENT, [0.0, 1.0, 0.0, 1.0])
223 glcanvas.glLight(glcanvas.GL_LIGHT0, glcanvas.GL_DIFFUSE, [1.0, 1.0, 1.0, 1.0])
224 glcanvas.glLight(glcanvas.GL_LIGHT0, glcanvas.GL_SPECULAR, [1.0, 1.0, 1.0, 1.0])
225 glcanvas.glLight(glcanvas.GL_LIGHT0, glcanvas.GL_POSITION, [1.0, 1.0, 1.0, 0.0]);
226 glcanvas.glLightModel(glcanvas.GL_LIGHT_MODEL_AMBIENT, [0.2, 0.2, 0.2, 1.0])
227 glcanvas.glEnable(glcanvas.GL_LIGHTING)
228 glcanvas.glEnable(glcanvas.GL_LIGHT0)
229 glcanvas.glDepthFunc(glcanvas.GL_LESS)
230 glcanvas.glEnable(glcanvas.GL_DEPTH_TEST)
231 glcanvas.glClear(glcanvas.GL_COLOR_BUFFER_BIT | glcanvas.GL_DEPTH_BUFFER_BIT)
232 # position viewer
233 glcanvas.glMatrixMode(glcanvas.GL_MODELVIEW);
234
235
236 def OnDraw(self):
237 # clear color and depth buffers
238 glcanvas.glClear(glcanvas.GL_COLOR_BUFFER_BIT | glcanvas.GL_DEPTH_BUFFER_BIT);
239 # use a fresh transformation matrix
240 glcanvas.glPushMatrix()
241 # position object
242 glcanvas.glTranslate(0.0, 0.0, -2.0);
243 glcanvas.glRotate(30.0, 1.0, 0.0, 0.0);
244 glcanvas.glRotate(30.0, 0.0, 1.0, 0.0);
245
246 glcanvas.glTranslate(0, -1, 0)
247 glcanvas.glRotate(250, 1, 0, 0)
248 glcanvas.glutSolidCone(0.5, 1, 30, 5)
249 glcanvas.glPopMatrix()
250 glcanvas.glRotatef((self.lasty - self.y)/100., 0.0, 0.0, 1.0);
251 glcanvas.glRotatef(0.0, (self.lastx - self.x)/100., 1.0, 0.0);
252 # push into visible buffer
253 self.SwapBuffers()
254
255
256
257
258 #----------------------------------------------------------------------
259
260
261
262 overview = """\
263 """
264
265
266 #----------------------------------------------------------------------
267
268 def _test():
269 class MyApp(wx.App):
270 def OnInit(self):
271 frame = wx.Frame(None, -1, "GL Demos", wx.DefaultPosition, (600,300))
272 #win = ConeCanvas(frame)
273 MySplitter(frame)
274 frame.Show(True)
275 self.SetTopWindow(frame)
276 return True
277
278 app = MyApp(0)
279 app.MainLoop()
280
281 if __name__ == '__main__':
282 _test()
283