]>
Commit | Line | Data |
---|---|---|
8fa876ca | 1 | |
41378d34 | 2 | import wx |
8fa876ca | 3 | |
cf694132 | 4 | try: |
41378d34 | 5 | from wx import glcanvas |
1e4a197e | 6 | haveGLCanvas = True |
cf694132 | 7 | except ImportError: |
1e4a197e | 8 | haveGLCanvas = False |
cf694132 | 9 | |
54b96882 RD |
10 | try: |
11 | # The Python OpenGL package can be found at | |
c368d904 | 12 | # http://PyOpenGL.sourceforge.net/ |
41378d34 RD |
13 | from OpenGL.GL import * |
14 | from OpenGL.GLUT import * | |
1e4a197e | 15 | haveOpenGL = True |
54b96882 | 16 | except ImportError: |
1e4a197e | 17 | haveOpenGL = False |
54b96882 | 18 | |
cf694132 RD |
19 | #---------------------------------------------------------------------- |
20 | ||
2e839e96 RD |
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!', | |
c4ef95da | 56 | 'Sorry', wx.OK | wx.ICON_WARNING) |
2e839e96 RD |
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/', | |
c4ef95da | 64 | 'Sorry', wx.OK | wx.ICON_WARNING) |
2e839e96 RD |
65 | dlg.ShowModal() |
66 | dlg.Destroy() | |
67 | ||
68 | else: | |
5b119149 RD |
69 | canvasClassName = buttonDefs[evt.GetId()][0] |
70 | canvasClass = eval(canvasClassName) | |
8fa876ca | 71 | frame = wx.Frame(None, -1, canvasClassName, size=(400,400)) |
5b119149 | 72 | canvas = canvasClass(frame) |
1e4a197e | 73 | frame.Show(True) |
cf694132 RD |
74 | |
75 | ||
2e839e96 RD |
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 | |
02b800ce | 83 | self.size = None |
2e839e96 RD |
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) | |
d2103cf2 | 90 | |
d2103cf2 | 91 | |
2e839e96 RD |
92 | def OnEraseBackground(self, event): |
93 | pass # Do nothing, to avoid flashing on MSW. | |
cf694132 | 94 | |
d2103cf2 | 95 | |
2e839e96 | 96 | def OnSize(self, event): |
02b800ce | 97 | size = self.size = self.GetClientSize() |
2e839e96 RD |
98 | if self.GetContext(): |
99 | self.SetCurrent() | |
100 | glViewport(0, 0, size.width, size.height) | |
101 | event.Skip() | |
d58a80fa | 102 | |
cf694132 | 103 | |
2e839e96 RD |
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() | |
01f44840 | 111 | |
cf694132 | 112 | |
2e839e96 RD |
113 | def OnMouseDown(self, evt): |
114 | self.CaptureMouse() | |
02b800ce | 115 | self.x, self.y = self.lastx, self.lasty = evt.GetPosition() |
01f44840 | 116 | |
cf694132 | 117 | |
2e839e96 RD |
118 | def OnMouseUp(self, evt): |
119 | self.ReleaseMouse() | |
5b119149 | 120 | |
01f44840 | 121 | |
2e839e96 RD |
122 | def OnMouseMotion(self, evt): |
123 | if evt.Dragging() and evt.LeftIsDown(): | |
02b800ce | 124 | self.lastx, self.lasty = self.x, self.y |
2e839e96 RD |
125 | self.x, self.y = evt.GetPosition() |
126 | self.Refresh(False) | |
5b119149 | 127 | |
01f44840 | 128 | |
5b119149 | 129 | |
01f44840 | 130 | |
2e839e96 RD |
131 | class CubeCanvas(MyCanvasBase): |
132 | def InitGL(self): | |
133 | # set viewing projection | |
02b800ce RD |
134 | glMatrixMode(GL_PROJECTION) |
135 | glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0) | |
2e839e96 RD |
136 | |
137 | # position viewer | |
02b800ce RD |
138 | glMatrixMode(GL_MODELVIEW) |
139 | glTranslatef(0.0, 0.0, -2.0) | |
2e839e96 RD |
140 | |
141 | # position object | |
02b800ce RD |
142 | glRotatef(self.y, 1.0, 0.0, 0.0) |
143 | glRotatef(self.x, 0.0, 1.0, 0.0) | |
2e839e96 | 144 | |
02b800ce RD |
145 | glEnable(GL_DEPTH_TEST) |
146 | glEnable(GL_LIGHTING) | |
147 | glEnable(GL_LIGHT0) | |
cf694132 | 148 | |
5b119149 | 149 | |
2e839e96 RD |
150 | def OnDraw(self): |
151 | # clear color and depth buffers | |
02b800ce | 152 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) |
41378d34 | 153 | |
2e839e96 RD |
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) | |
41378d34 | 161 | |
2e839e96 RD |
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) | |
5b119149 | 167 | |
2e839e96 RD |
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) | |
5b119149 | 173 | |
2e839e96 RD |
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) | |
cf694132 | 179 | |
2e839e96 RD |
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) | |
41378d34 | 185 | |
2e839e96 RD |
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() | |
41378d34 | 192 | |
02b800ce RD |
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); | |
41378d34 | 202 | |
2e839e96 | 203 | self.SwapBuffers() |
41378d34 | 204 | |
41378d34 | 205 | |
41378d34 | 206 | |
cf694132 | 207 | |
cf694132 | 208 | |
2e839e96 RD |
209 | class ConeCanvas(MyCanvasBase): |
210 | def InitGL( self ): | |
02b800ce | 211 | glMatrixMode(GL_PROJECTION) |
2e839e96 | 212 | # camera frustrum setup |
02b800ce | 213 | glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0) |
2e839e96 RD |
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]) | |
02b800ce | 221 | glLight(GL_LIGHT0, GL_POSITION, [1.0, 1.0, 1.0, 0.0]) |
2e839e96 RD |
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 | |
02b800ce RD |
229 | glMatrixMode(GL_MODELVIEW) |
230 | # position viewer | |
231 | glTranslatef(0.0, 0.0, -2.0); | |
232 | ||
2e839e96 RD |
233 | |
234 | ||
235 | def OnDraw(self): | |
236 | # clear color and depth buffers | |
02b800ce | 237 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) |
2e839e96 RD |
238 | # use a fresh transformation matrix |
239 | glPushMatrix() | |
240 | # position object | |
02b800ce RD |
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) | |
2e839e96 RD |
244 | |
245 | glTranslate(0, -1, 0) | |
246 | glRotate(250, 1, 0, 0) | |
247 | glutSolidCone(0.5, 1, 30, 5) | |
248 | glPopMatrix() | |
02b800ce RD |
249 | glRotatef((self.y - self.lasty), 0.0, 0.0, 1.0); |
250 | glRotatef((self.x - self.lastx), 1.0, 0.0, 0.0); | |
2e839e96 RD |
251 | # push into visible buffer |
252 | self.SwapBuffers() | |
cf694132 | 253 | |
41378d34 RD |
254 | |
255 | ||
256 | ||
2e839e96 | 257 | #---------------------------------------------------------------------- |
cf694132 RD |
258 | |
259 | ||
2e839e96 RD |
260 | def runTest(frame, nb, log): |
261 | win = ButtonPanel(nb, log) | |
262 | return win | |
5b119149 RD |
263 | |
264 | ||
cf694132 RD |
265 | |
266 | ||
267 | ||
41378d34 | 268 | |
cf694132 RD |
269 | overview = """\ |
270 | """ | |
271 | ||
272 | ||
cf694132 | 273 | |
41378d34 RD |
274 | if __name__ == '__main__': |
275 | import sys,os | |
276 | import run | |
8eca4fef | 277 | run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:]) |
cf694132 | 278 | |
cf694132 | 279 | |
41378d34 RD |
280 | |
281 | ||
282 | #---------------------------------------------------------------------- | |
d58a80fa | 283 |