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