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