]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/GLCanvas.py
PCH-less compilation fix
[wxWidgets.git] / wxPython / demo / GLCanvas.py
1
2 import wx
3 import sys
4
5 try:
6 from wx import glcanvas
7 haveGLCanvas = True
8 except ImportError:
9 haveGLCanvas = False
10
11 try:
12 # The Python OpenGL package can be found at
13 # http://PyOpenGL.sourceforge.net/
14 from OpenGL.GL import *
15 from OpenGL.GLUT import *
16 haveOpenGL = True
17 except ImportError:
18 haveOpenGL = False
19
20 #----------------------------------------------------------------------
21
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!',
57 'Sorry', wx.OK | wx.ICON_WARNING)
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/',
65 'Sorry', wx.OK | wx.ICON_WARNING)
66 dlg.ShowModal()
67 dlg.Destroy()
68
69 else:
70 canvasClassName = buttonDefs[evt.GetId()][0]
71 canvasClass = eval(canvasClassName)
72 frame = wx.Frame(None, -1, canvasClassName, size=(400,400))
73 canvas = canvasClass(frame)
74 frame.Show(True)
75
76
77 class MyCanvasBase(glcanvas.GLCanvas):
78 def __init__(self, parent):
79 glcanvas.GLCanvas.__init__(self, parent, -1)
80 self.init = False
81 self.context = glcanvas.GLContext(self)
82
83 # initial mouse position
84 self.lastx = self.x = 30
85 self.lasty = self.y = 30
86 self.size = None
87 self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
88 self.Bind(wx.EVT_SIZE, self.OnSize)
89 self.Bind(wx.EVT_PAINT, self.OnPaint)
90 self.Bind(wx.EVT_LEFT_DOWN, self.OnMouseDown)
91 self.Bind(wx.EVT_LEFT_UP, self.OnMouseUp)
92 self.Bind(wx.EVT_MOTION, self.OnMouseMotion)
93
94
95 def OnEraseBackground(self, event):
96 pass # Do nothing, to avoid flashing on MSW.
97
98
99 def OnSize(self, event):
100 wx.CallAfter(self.DoSetViewport)
101 event.Skip()
102
103 def DoSetViewport(self):
104 size = self.size = self.GetClientSize()
105 self.SetCurrent(self.context)
106 glViewport(0, 0, size.width, size.height)
107
108
109
110 def OnPaint(self, event):
111 dc = wx.PaintDC(self)
112 self.SetCurrent(self.context)
113 if not self.init:
114 self.InitGL()
115 self.init = True
116 self.OnDraw()
117
118
119 def OnMouseDown(self, evt):
120 self.CaptureMouse()
121 self.x, self.y = self.lastx, self.lasty = evt.GetPosition()
122
123
124 def OnMouseUp(self, evt):
125 self.ReleaseMouse()
126
127
128 def OnMouseMotion(self, evt):
129 if evt.Dragging() and evt.LeftIsDown():
130 self.lastx, self.lasty = self.x, self.y
131 self.x, self.y = evt.GetPosition()
132 self.Refresh(False)
133
134
135
136
137 class CubeCanvas(MyCanvasBase):
138 def InitGL(self):
139 # set viewing projection
140 glMatrixMode(GL_PROJECTION)
141 glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0)
142
143 # position viewer
144 glMatrixMode(GL_MODELVIEW)
145 glTranslatef(0.0, 0.0, -2.0)
146
147 # position object
148 glRotatef(self.y, 1.0, 0.0, 0.0)
149 glRotatef(self.x, 0.0, 1.0, 0.0)
150
151 glEnable(GL_DEPTH_TEST)
152 glEnable(GL_LIGHTING)
153 glEnable(GL_LIGHT0)
154
155
156 def OnDraw(self):
157 # clear color and depth buffers
158 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
159
160 # draw six faces of a cube
161 glBegin(GL_QUADS)
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, 0.0,-1.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( 0.0,-1.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
192 glNormal3f(-1.0, 0.0, 0.0)
193 glVertex3f(-0.5,-0.5,-0.5)
194 glVertex3f(-0.5,-0.5, 0.5)
195 glVertex3f(-0.5, 0.5, 0.5)
196 glVertex3f(-0.5, 0.5,-0.5)
197 glEnd()
198
199 if self.size is None:
200 self.size = self.GetClientSize()
201 w, h = self.size
202 w = max(w, 1.0)
203 h = max(h, 1.0)
204 xScale = 180.0 / w
205 yScale = 180.0 / h
206 glRotatef((self.y - self.lasty) * yScale, 1.0, 0.0, 0.0);
207 glRotatef((self.x - self.lastx) * xScale, 0.0, 1.0, 0.0);
208
209 self.SwapBuffers()
210
211
212
213
214
215 class ConeCanvas(MyCanvasBase):
216 def InitGL( self ):
217 glMatrixMode(GL_PROJECTION)
218 # camera frustrum setup
219 glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0)
220 glMaterial(GL_FRONT, GL_AMBIENT, [0.2, 0.2, 0.2, 1.0])
221 glMaterial(GL_FRONT, GL_DIFFUSE, [0.8, 0.8, 0.8, 1.0])
222 glMaterial(GL_FRONT, GL_SPECULAR, [1.0, 0.0, 1.0, 1.0])
223 glMaterial(GL_FRONT, GL_SHININESS, 50.0)
224 glLight(GL_LIGHT0, GL_AMBIENT, [0.0, 1.0, 0.0, 1.0])
225 glLight(GL_LIGHT0, GL_DIFFUSE, [1.0, 1.0, 1.0, 1.0])
226 glLight(GL_LIGHT0, GL_SPECULAR, [1.0, 1.0, 1.0, 1.0])
227 glLight(GL_LIGHT0, GL_POSITION, [1.0, 1.0, 1.0, 0.0])
228 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, [0.2, 0.2, 0.2, 1.0])
229 glEnable(GL_LIGHTING)
230 glEnable(GL_LIGHT0)
231 glDepthFunc(GL_LESS)
232 glEnable(GL_DEPTH_TEST)
233 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
234 # position viewer
235 glMatrixMode(GL_MODELVIEW)
236 # position viewer
237 glTranslatef(0.0, 0.0, -2.0);
238 #
239 glutInit(sys.argv)
240
241
242 def OnDraw(self):
243 # clear color and depth buffers
244 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
245 # use a fresh transformation matrix
246 glPushMatrix()
247 # position object
248 #glTranslate(0.0, 0.0, -2.0)
249 glRotate(30.0, 1.0, 0.0, 0.0)
250 glRotate(30.0, 0.0, 1.0, 0.0)
251
252 glTranslate(0, -1, 0)
253 glRotate(250, 1, 0, 0)
254 glutSolidCone(0.5, 1, 30, 5)
255 glPopMatrix()
256 glRotatef((self.y - self.lasty), 0.0, 0.0, 1.0);
257 glRotatef((self.x - self.lastx), 1.0, 0.0, 0.0);
258 # push into visible buffer
259 self.SwapBuffers()
260
261
262
263
264 #----------------------------------------------------------------------
265
266
267 def runTest(frame, nb, log):
268 win = ButtonPanel(nb, log)
269 return win
270
271
272
273
274
275
276 overview = """\
277 """
278
279
280
281 if __name__ == '__main__':
282 import sys,os
283 import run
284 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
285
286
287
288
289 #----------------------------------------------------------------------
290