]>
Commit | Line | Data |
---|---|---|
cf694132 RD |
1 | from wxPython.wx import * |
2 | try: | |
3 | from wxPython.glcanvas import * | |
1e4a197e | 4 | haveGLCanvas = True |
cf694132 | 5 | except ImportError: |
1e4a197e | 6 | haveGLCanvas = False |
cf694132 | 7 | |
54b96882 RD |
8 | try: |
9 | # The Python OpenGL package can be found at | |
c368d904 | 10 | # http://PyOpenGL.sourceforge.net/ |
54b96882 RD |
11 | from OpenGL.GL import * |
12 | from OpenGL.GLUT import * | |
1e4a197e | 13 | haveOpenGL = True |
54b96882 | 14 | except ImportError: |
1e4a197e | 15 | haveOpenGL = False |
54b96882 | 16 | |
cf694132 RD |
17 | #---------------------------------------------------------------------- |
18 | ||
19 | if not haveGLCanvas: | |
20 | def runTest(frame, nb, log): | |
21 | dlg = wxMessageDialog(frame, 'The wxGLCanvas has not been included with this build of wxPython!', | |
22 | 'Sorry', wxOK | wxICON_INFORMATION) | |
23 | dlg.ShowModal() | |
24 | dlg.Destroy() | |
25 | ||
54b96882 RD |
26 | elif not haveOpenGL: |
27 | def runTest(frame, nb, log): | |
28 | dlg = wxMessageDialog(frame, | |
29 | 'The OpenGL package was not found. You can get it at\n' | |
c368d904 | 30 | 'http://PyOpenGL.sourceforge.net/', |
54b96882 RD |
31 | 'Sorry', wxOK | wxICON_INFORMATION) |
32 | dlg.ShowModal() | |
33 | dlg.Destroy() | |
cf694132 RD |
34 | |
35 | ||
5b119149 RD |
36 | |
37 | ||
54b96882 | 38 | else: |
5b119149 RD |
39 | buttonDefs = { |
40 | wxNewId() : ('CubeCanvas', 'Cube'), | |
41 | wxNewId() : ('ConeCanvas', 'Cone'), | |
42 | } | |
43 | ||
44 | class ButtonPanel(wxPanel): | |
45 | def __init__(self, parent, log): | |
46 | wxPanel.__init__(self, parent, -1) | |
47 | self.log = log | |
48 | ||
49 | box = wxBoxSizer(wxVERTICAL) | |
50 | box.Add(20, 30) | |
51 | keys = buttonDefs.keys() | |
52 | keys.sort() | |
53 | for k in keys: | |
54 | text = buttonDefs[k][1] | |
55 | btn = wxButton(self, k, text) | |
56 | box.Add(btn, 0, wxALIGN_CENTER|wxALL, 15) | |
57 | EVT_BUTTON(self, k, self.OnButton) | |
58 | ||
4ece0694 RD |
59 | #** Enable this to show putting a wxGLCanvas on the wxPanel |
60 | if 0: | |
61 | c = CubeCanvas(self) | |
62 | c.SetSize((200, 200)) | |
63 | box.Add(c, 0, wxALIGN_CENTER|wxALL, 15) | |
64 | ||
1e4a197e | 65 | self.SetAutoLayout(True) |
5b119149 RD |
66 | self.SetSizer(box) |
67 | ||
4ece0694 | 68 | |
5b119149 RD |
69 | def OnButton(self, evt): |
70 | canvasClassName = buttonDefs[evt.GetId()][0] | |
71 | canvasClass = eval(canvasClassName) | |
72 | frame = wxFrame(None, -1, canvasClassName, size=(400,400)) | |
73 | canvas = canvasClass(frame) | |
1e4a197e | 74 | frame.Show(True) |
cf694132 RD |
75 | |
76 | ||
d2103cf2 | 77 | |
5b119149 RD |
78 | def runTest(frame, nb, log): |
79 | win = ButtonPanel(nb, log) | |
80 | return win | |
d2103cf2 | 81 | |
cf694132 | 82 | |
d2103cf2 | 83 | |
d58a80fa | 84 | |
5b119149 | 85 | class MyCanvasBase(wxGLCanvas): |
cf694132 | 86 | def __init__(self, parent): |
5b119149 | 87 | wxGLCanvas.__init__(self, parent, -1) |
1e4a197e | 88 | self.init = False |
5b119149 RD |
89 | # initial mouse position |
90 | self.lastx = self.x = 30 | |
91 | self.lasty = self.y = 30 | |
cf694132 | 92 | EVT_ERASE_BACKGROUND(self, self.OnEraseBackground) |
f6bcfd97 BP |
93 | EVT_SIZE(self, self.OnSize) |
94 | EVT_PAINT(self, self.OnPaint) | |
e7d0a414 RD |
95 | EVT_LEFT_DOWN(self, self.OnMouseDown) # needs fixing... |
96 | EVT_LEFT_UP(self, self.OnMouseUp) | |
97 | EVT_MOTION(self, self.OnMouseMotion) | |
cf694132 RD |
98 | |
99 | def OnEraseBackground(self, event): | |
5b119149 | 100 | pass # Do nothing, to avoid flashing on MSW. |
cf694132 | 101 | |
cf694132 RD |
102 | def OnSize(self, event): |
103 | size = self.GetClientSize() | |
d2103cf2 | 104 | if self.GetContext(): |
cf694132 RD |
105 | self.SetCurrent() |
106 | glViewport(0, 0, size.width, size.height) | |
107 | ||
cf694132 RD |
108 | def OnPaint(self, event): |
109 | dc = wxPaintDC(self) | |
cf694132 | 110 | self.SetCurrent() |
cf694132 RD |
111 | if not self.init: |
112 | self.InitGL() | |
1e4a197e | 113 | self.init = True |
5b119149 RD |
114 | self.OnDraw() |
115 | ||
116 | def OnMouseDown(self, evt): | |
117 | self.CaptureMouse() | |
118 | ||
119 | def OnMouseUp(self, evt): | |
120 | self.ReleaseMouse() | |
121 | ||
122 | def OnMouseMotion(self, evt): | |
123 | if evt.Dragging() and evt.LeftIsDown(): | |
124 | self.x, self.y = self.lastx, self.lasty | |
125 | self.x, self.y = evt.GetPosition() | |
1e4a197e | 126 | self.Refresh(False) |
cf694132 | 127 | |
5b119149 RD |
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): | |
cf694132 RD |
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 | ||
d58a80fa RD |
193 | glRotatef((self.lasty - self.y)/100., 1.0, 0.0, 0.0); |
194 | glRotatef((self.lastx - self.x)/100., 0.0, 1.0, 0.0); | |
cf694132 | 195 | |
5b119149 | 196 | self.SwapBuffers() |
cf694132 | 197 | |
cf694132 | 198 | |
cf694132 RD |
199 | |
200 | ||
201 | ||
5b119149 RD |
202 | class ConeCanvas(MyCanvasBase): |
203 | def InitGL( self ): | |
d2103cf2 RD |
204 | glMatrixMode(GL_PROJECTION); |
205 | # camera frustrum setup | |
206 | glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0); | |
207 | glMaterial(GL_FRONT, GL_AMBIENT, [0.2, 0.2, 0.2, 1.0]) | |
208 | glMaterial(GL_FRONT, GL_DIFFUSE, [0.8, 0.8, 0.8, 1.0]) | |
209 | glMaterial(GL_FRONT, GL_SPECULAR, [1.0, 0.0, 1.0, 1.0]) | |
210 | glMaterial(GL_FRONT, GL_SHININESS, 50.0) | |
211 | glLight(GL_LIGHT0, GL_AMBIENT, [0.0, 1.0, 0.0, 1.0]) | |
212 | glLight(GL_LIGHT0, GL_DIFFUSE, [1.0, 1.0, 1.0, 1.0]) | |
213 | glLight(GL_LIGHT0, GL_SPECULAR, [1.0, 1.0, 1.0, 1.0]) | |
214 | glLight(GL_LIGHT0, GL_POSITION, [1.0, 1.0, 1.0, 0.0]); | |
215 | glLightModel(GL_LIGHT_MODEL_AMBIENT, [0.2, 0.2, 0.2, 1.0]) | |
216 | glEnable(GL_LIGHTING) | |
217 | glEnable(GL_LIGHT0) | |
218 | glDepthFunc(GL_LESS) | |
219 | glEnable(GL_DEPTH_TEST) | |
220 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) | |
d58a80fa RD |
221 | # position viewer |
222 | glMatrixMode(GL_MODELVIEW); | |
d2103cf2 RD |
223 | |
224 | ||
5b119149 | 225 | def OnDraw(self): |
d2103cf2 RD |
226 | # clear color and depth buffers |
227 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
d2103cf2 RD |
228 | # use a fresh transformation matrix |
229 | glPushMatrix() | |
230 | # position object | |
231 | glTranslate(0.0, 0.0, -2.0); | |
232 | glRotate(30.0, 1.0, 0.0, 0.0); | |
233 | glRotate(30.0, 0.0, 1.0, 0.0); | |
234 | ||
d2103cf2 RD |
235 | glTranslate(0, -1, 0) |
236 | glRotate(250, 1, 0, 0) | |
d58a80fa | 237 | glutSolidCone(0.5, 1, 30, 5) |
d2103cf2 | 238 | glPopMatrix() |
d58a80fa RD |
239 | glRotatef((self.lasty - self.y)/100., 0.0, 0.0, 1.0); |
240 | glRotatef(0.0, (self.lastx - self.x)/100., 1.0, 0.0); | |
d2103cf2 RD |
241 | # push into visible buffer |
242 | self.SwapBuffers() | |
cf694132 RD |
243 | |
244 | ||
5b119149 RD |
245 | |
246 | ||
e166644c | 247 | #---------------------------------------------------------------------- |
cf694132 RD |
248 | |
249 | ||
250 | ||
251 | ||
252 | ||
253 | ||
254 | ||
255 | ||
256 | overview = """\ | |
257 | """ | |
258 | ||
259 | ||
260 | ||
261 | ||
262 | ||
263 | #---------------------------------------------------------------------- | |
264 | ||
265 | def _test(): | |
266 | class MyApp(wxApp): | |
267 | def OnInit(self): | |
493f1553 | 268 | frame = wxFrame(None, -1, "GL Demos", wxDefaultPosition, wxSize(600,300)) |
d2103cf2 RD |
269 | #win = ConeCanvas(frame) |
270 | MySplitter(frame) | |
1e4a197e | 271 | frame.Show(True) |
cf694132 | 272 | self.SetTopWindow(frame) |
1e4a197e | 273 | return True |
cf694132 RD |
274 | |
275 | app = MyApp(0) | |
276 | app.MainLoop() | |
277 | ||
278 | if __name__ == '__main__': | |
279 | _test() | |
d58a80fa | 280 |