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