]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/GLCanvas.py
default values for option and flag (TODO: preferences dialog)
[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 # initial mouse position
82 self.lastx = self.x = 30
83 self.lasty = self.y = 30
84 self.size = None
85 self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
86 self.Bind(wx.EVT_SIZE, self.OnSize)
87 self.Bind(wx.EVT_PAINT, self.OnPaint)
88 self.Bind(wx.EVT_LEFT_DOWN, self.OnMouseDown)
89 self.Bind(wx.EVT_LEFT_UP, self.OnMouseUp)
90 self.Bind(wx.EVT_MOTION, self.OnMouseMotion)
91
92
93 def OnEraseBackground(self, event):
94 pass # Do nothing, to avoid flashing on MSW.
95
96
97 def OnSize(self, event):
98 size = self.size = self.GetClientSize()
99 if self.GetContext():
100 self.SetCurrent()
101 glViewport(0, 0, size.width, size.height)
102 event.Skip()
103
104
105 def OnPaint(self, event):
106 dc = wx.PaintDC(self)
107 self.SetCurrent()
108 if not self.init:
109 self.InitGL()
110 self.init = True
111 self.OnDraw()
112
113
114 def OnMouseDown(self, evt):
115 self.CaptureMouse()
116 self.x, self.y = self.lastx, self.lasty = evt.GetPosition()
117
118
119 def OnMouseUp(self, evt):
120 self.ReleaseMouse()
121
122
123 def OnMouseMotion(self, evt):
124 if evt.Dragging() and evt.LeftIsDown():
125 self.lastx, self.lasty = self.x, self.y
126 self.x, self.y = evt.GetPosition()
127 self.Refresh(False)
128
129
130
131
132 class CubeCanvas(MyCanvasBase):
133 def InitGL(self):
134 # set viewing projection
135 glMatrixMode(GL_PROJECTION)
136 glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0)
137
138 # position viewer
139 glMatrixMode(GL_MODELVIEW)
140 glTranslatef(0.0, 0.0, -2.0)
141
142 # position object
143 glRotatef(self.y, 1.0, 0.0, 0.0)
144 glRotatef(self.x, 0.0, 1.0, 0.0)
145
146 glEnable(GL_DEPTH_TEST)
147 glEnable(GL_LIGHTING)
148 glEnable(GL_LIGHT0)
149
150
151 def OnDraw(self):
152 # clear color and depth buffers
153 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
154
155 # draw six faces of a cube
156 glBegin(GL_QUADS)
157 glNormal3f( 0.0, 0.0, 1.0)
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 glVertex3f( 0.5,-0.5, 0.5)
162
163 glNormal3f( 0.0, 0.0,-1.0)
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 glVertex3f( 0.5,-0.5,-0.5)
168
169 glNormal3f( 0.0, 1.0, 0.0)
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 glVertex3f(-0.5, 0.5, 0.5)
174
175 glNormal3f( 0.0,-1.0, 0.0)
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 glVertex3f(-0.5,-0.5, 0.5)
180
181 glNormal3f( 1.0, 0.0, 0.0)
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 glVertex3f( 0.5, 0.5,-0.5)
186
187 glNormal3f(-1.0, 0.0, 0.0)
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 glVertex3f(-0.5, 0.5,-0.5)
192 glEnd()
193
194 if self.size is None:
195 self.size = self.GetClientSize()
196 w, h = self.size
197 w = max(w, 1.0)
198 h = max(h, 1.0)
199 xScale = 180.0 / w
200 yScale = 180.0 / h
201 glRotatef((self.y - self.lasty) * yScale, 1.0, 0.0, 0.0);
202 glRotatef((self.x - self.lastx) * xScale, 0.0, 1.0, 0.0);
203
204 self.SwapBuffers()
205
206
207
208
209
210 class ConeCanvas(MyCanvasBase):
211 def InitGL( self ):
212 glMatrixMode(GL_PROJECTION)
213 # camera frustrum setup
214 glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0)
215 glMaterial(GL_FRONT, GL_AMBIENT, [0.2, 0.2, 0.2, 1.0])
216 glMaterial(GL_FRONT, GL_DIFFUSE, [0.8, 0.8, 0.8, 1.0])
217 glMaterial(GL_FRONT, GL_SPECULAR, [1.0, 0.0, 1.0, 1.0])
218 glMaterial(GL_FRONT, GL_SHININESS, 50.0)
219 glLight(GL_LIGHT0, GL_AMBIENT, [0.0, 1.0, 0.0, 1.0])
220 glLight(GL_LIGHT0, GL_DIFFUSE, [1.0, 1.0, 1.0, 1.0])
221 glLight(GL_LIGHT0, GL_SPECULAR, [1.0, 1.0, 1.0, 1.0])
222 glLight(GL_LIGHT0, GL_POSITION, [1.0, 1.0, 1.0, 0.0])
223 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, [0.2, 0.2, 0.2, 1.0])
224 glEnable(GL_LIGHTING)
225 glEnable(GL_LIGHT0)
226 glDepthFunc(GL_LESS)
227 glEnable(GL_DEPTH_TEST)
228 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
229 # position viewer
230 glMatrixMode(GL_MODELVIEW)
231 # position viewer
232 glTranslatef(0.0, 0.0, -2.0);
233 #
234 glutInit(sys.argv)
235
236
237 def OnDraw(self):
238 # clear color and depth buffers
239 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
240 # use a fresh transformation matrix
241 glPushMatrix()
242 # position object
243 #glTranslate(0.0, 0.0, -2.0)
244 glRotate(30.0, 1.0, 0.0, 0.0)
245 glRotate(30.0, 0.0, 1.0, 0.0)
246
247 glTranslate(0, -1, 0)
248 glRotate(250, 1, 0, 0)
249 glutSolidCone(0.5, 1, 30, 5)
250 glPopMatrix()
251 glRotatef((self.y - self.lasty), 0.0, 0.0, 1.0);
252 glRotatef((self.x - self.lastx), 1.0, 0.0, 0.0);
253 # push into visible buffer
254 self.SwapBuffers()
255
256
257
258
259 #----------------------------------------------------------------------
260
261
262 def runTest(frame, nb, log):
263 win = ButtonPanel(nb, log)
264 return win
265
266
267
268
269
270
271 overview = """\
272 """
273
274
275
276 if __name__ == '__main__':
277 import sys,os
278 import run
279 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
280
281
282
283
284 #----------------------------------------------------------------------
285