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