]>
Commit | Line | Data |
---|---|---|
1 | from wxPython.wx import * | |
2 | try: | |
3 | from wxPython.glcanvas import * | |
4 | haveGLCanvas = true | |
5 | except ImportError: | |
6 | haveGLCanvas = false | |
7 | ||
8 | try: | |
9 | # The Python OpenGL package can be found at | |
10 | # http://PyOpenGL.sourceforge.net/ | |
11 | from OpenGL.GL import * | |
12 | from OpenGL.GLUT import * | |
13 | haveOpenGL = true | |
14 | except ImportError: | |
15 | haveOpenGL = false | |
16 | ||
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 | ||
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' | |
30 | 'http://PyOpenGL.sourceforge.net/', | |
31 | 'Sorry', wxOK | wxICON_INFORMATION) | |
32 | dlg.ShowModal() | |
33 | dlg.Destroy() | |
34 | ||
35 | ||
36 | ||
37 | ||
38 | else: | |
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 | ||
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 | ||
65 | self.SetAutoLayout(true) | |
66 | self.SetSizer(box) | |
67 | ||
68 | ||
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) | |
74 | frame.Show(true) | |
75 | ||
76 | ||
77 | ||
78 | def runTest(frame, nb, log): | |
79 | win = ButtonPanel(nb, log) | |
80 | return win | |
81 | ||
82 | ||
83 | ||
84 | ||
85 | class MyCanvasBase(wxGLCanvas): | |
86 | def __init__(self, parent): | |
87 | wxGLCanvas.__init__(self, parent, -1) | |
88 | self.init = false | |
89 | # initial mouse position | |
90 | self.lastx = self.x = 30 | |
91 | self.lasty = self.y = 30 | |
92 | EVT_ERASE_BACKGROUND(self, self.OnEraseBackground) | |
93 | EVT_SIZE(self, self.OnSize) | |
94 | EVT_PAINT(self, self.OnPaint) | |
95 | EVT_LEFT_DOWN(self, self.OnMouseDown) # needs fixing... | |
96 | EVT_LEFT_UP(self, self.OnMouseUp) | |
97 | EVT_MOTION(self, self.OnMouseMotion) | |
98 | ||
99 | def OnEraseBackground(self, event): | |
100 | pass # Do nothing, to avoid flashing on MSW. | |
101 | ||
102 | def OnSize(self, event): | |
103 | size = self.GetClientSize() | |
104 | if self.GetContext(): | |
105 | self.SetCurrent() | |
106 | glViewport(0, 0, size.width, size.height) | |
107 | ||
108 | def OnPaint(self, event): | |
109 | dc = wxPaintDC(self) | |
110 | self.SetCurrent() | |
111 | if not self.init: | |
112 | self.InitGL() | |
113 | self.init = true | |
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() | |
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 | 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); | |
195 | ||
196 | self.SwapBuffers() | |
197 | ||
198 | ||
199 | ||
200 | ||
201 | ||
202 | class ConeCanvas(MyCanvasBase): | |
203 | def InitGL( self ): | |
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) | |
221 | # position viewer | |
222 | glMatrixMode(GL_MODELVIEW); | |
223 | ||
224 | ||
225 | def OnDraw(self): | |
226 | # clear color and depth buffers | |
227 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
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 | ||
235 | glTranslate(0, -1, 0) | |
236 | glRotate(250, 1, 0, 0) | |
237 | glutSolidCone(0.5, 1, 30, 5) | |
238 | glPopMatrix() | |
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); | |
241 | # push into visible buffer | |
242 | self.SwapBuffers() | |
243 | ||
244 | ||
245 | ||
246 | ||
247 | #---------------------------------------------------------------------- | |
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): | |
268 | frame = wxFrame(None, -1, "GL Demos", wxDefaultPosition, wxSize(600,300)) | |
269 | #win = ConeCanvas(frame) | |
270 | MySplitter(frame) | |
271 | frame.Show(TRUE) | |
272 | self.SetTopWindow(frame) | |
273 | return TRUE | |
274 | ||
275 | app = MyApp(0) | |
276 | app.MainLoop() | |
277 | ||
278 | if __name__ == '__main__': | |
279 | _test() | |
280 |