]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/GLCanvas.py
HandlePrintClient shouldn't go above a top-level window
[wxWidgets.git] / wxPython / demo / GLCanvas.py
CommitLineData
8fa876ca 1
41378d34 2import wx
8fa876ca 3
cf694132 4try:
41378d34 5 from wx import glcanvas
1e4a197e 6 haveGLCanvas = True
cf694132 7except ImportError:
1e4a197e 8 haveGLCanvas = False
cf694132 9
54b96882
RD
10try:
11 # The Python OpenGL package can be found at
c368d904 12 # http://PyOpenGL.sourceforge.net/
41378d34
RD
13 from OpenGL.GL import *
14 from OpenGL.GLUT import *
1e4a197e 15 haveOpenGL = True
54b96882 16except ImportError:
1e4a197e 17 haveOpenGL = False
54b96882 18
cf694132
RD
19#----------------------------------------------------------------------
20
2e839e96
RD
21
22buttonDefs = {
23 wx.NewId() : ('CubeCanvas', 'Cube'),
24 wx.NewId() : ('ConeCanvas', 'Cone'),
25 }
26
27class ButtonPanel(wx.Panel):
28 def __init__(self, parent, log):
29 wx.Panel.__init__(self, parent, -1)
30 self.log = log
31
32 box = wx.BoxSizer(wx.VERTICAL)
33 box.Add((20, 30))
34 keys = buttonDefs.keys()
35 keys.sort()
36 for k in keys:
37 text = buttonDefs[k][1]
38 btn = wx.Button(self, k, text)
39 box.Add(btn, 0, wx.ALIGN_CENTER|wx.ALL, 15)
40 self.Bind(wx.EVT_BUTTON, self.OnButton, btn)
41
42 #** Enable this to show putting a GLCanvas on the wx.Panel
43 if 0:
44 c = CubeCanvas(self)
45 c.SetSize((200, 200))
46 box.Add(c, 0, wx.ALIGN_CENTER|wx.ALL, 15)
47
48 self.SetAutoLayout(True)
49 self.SetSizer(box)
50
51
52 def OnButton(self, evt):
53 if not haveGLCanvas:
54 dlg = wx.MessageDialog(self,
55 'The GLCanvas class has not been included with this build of wxPython!',
c4ef95da 56 'Sorry', wx.OK | wx.ICON_WARNING)
2e839e96
RD
57 dlg.ShowModal()
58 dlg.Destroy()
59
60 elif not haveOpenGL:
61 dlg = wx.MessageDialog(self,
62 'The OpenGL package was not found. You can get it at\n'
63 'http://PyOpenGL.sourceforge.net/',
c4ef95da 64 'Sorry', wx.OK | wx.ICON_WARNING)
2e839e96
RD
65 dlg.ShowModal()
66 dlg.Destroy()
67
68 else:
5b119149
RD
69 canvasClassName = buttonDefs[evt.GetId()][0]
70 canvasClass = eval(canvasClassName)
8fa876ca 71 frame = wx.Frame(None, -1, canvasClassName, size=(400,400))
5b119149 72 canvas = canvasClass(frame)
1e4a197e 73 frame.Show(True)
cf694132
RD
74
75
2e839e96
RD
76class MyCanvasBase(glcanvas.GLCanvas):
77 def __init__(self, parent):
78 glcanvas.GLCanvas.__init__(self, parent, -1)
79 self.init = False
80 # initial mouse position
81 self.lastx = self.x = 30
82 self.lasty = self.y = 30
83 self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
84 self.Bind(wx.EVT_SIZE, self.OnSize)
85 self.Bind(wx.EVT_PAINT, self.OnPaint)
86 self.Bind(wx.EVT_LEFT_DOWN, self.OnMouseDown)
87 self.Bind(wx.EVT_LEFT_UP, self.OnMouseUp)
88 self.Bind(wx.EVT_MOTION, self.OnMouseMotion)
d2103cf2 89
d2103cf2 90
2e839e96
RD
91 def OnEraseBackground(self, event):
92 pass # Do nothing, to avoid flashing on MSW.
cf694132 93
d2103cf2 94
2e839e96
RD
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 event.Skip()
d58a80fa 101
cf694132 102
2e839e96
RD
103 def OnPaint(self, event):
104 dc = wx.PaintDC(self)
105 self.SetCurrent()
106 if not self.init:
107 self.InitGL()
108 self.init = True
109 self.OnDraw()
01f44840 110
cf694132 111
2e839e96
RD
112 def OnMouseDown(self, evt):
113 self.CaptureMouse()
01f44840 114
cf694132 115
2e839e96
RD
116 def OnMouseUp(self, evt):
117 self.ReleaseMouse()
5b119149 118
01f44840 119
2e839e96
RD
120 def OnMouseMotion(self, evt):
121 if evt.Dragging() and evt.LeftIsDown():
122 self.x, self.y = self.lastx, self.lasty
123 self.x, self.y = evt.GetPosition()
124 self.Refresh(False)
5b119149 125
01f44840 126
5b119149 127
01f44840 128
2e839e96
RD
129class CubeCanvas(MyCanvasBase):
130 def InitGL(self):
131 # set viewing projection
132 glMatrixMode(GL_PROJECTION);
133 glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0);
134
135 # position viewer
136 glMatrixMode(GL_MODELVIEW);
137 glTranslatef(0.0, 0.0, -2.0);
138
139 # position object
140 glRotatef(self.y, 1.0, 0.0, 0.0);
141 glRotatef(self.x, 0.0, 1.0, 0.0);
142
143 glEnable(GL_DEPTH_TEST);
144 glEnable(GL_LIGHTING);
145 glEnable(GL_LIGHT0);
cf694132 146
5b119149 147
2e839e96
RD
148 def OnDraw(self):
149 # clear color and depth buffers
150 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
41378d34 151
2e839e96
RD
152 # draw six faces of a cube
153 glBegin(GL_QUADS)
154 glNormal3f( 0.0, 0.0, 1.0)
155 glVertex3f( 0.5, 0.5, 0.5)
156 glVertex3f(-0.5, 0.5, 0.5)
157 glVertex3f(-0.5,-0.5, 0.5)
158 glVertex3f( 0.5,-0.5, 0.5)
41378d34 159
2e839e96
RD
160 glNormal3f( 0.0, 0.0,-1.0)
161 glVertex3f(-0.5,-0.5,-0.5)
162 glVertex3f(-0.5, 0.5,-0.5)
163 glVertex3f( 0.5, 0.5,-0.5)
164 glVertex3f( 0.5,-0.5,-0.5)
5b119149 165
2e839e96
RD
166 glNormal3f( 0.0, 1.0, 0.0)
167 glVertex3f( 0.5, 0.5, 0.5)
168 glVertex3f( 0.5, 0.5,-0.5)
169 glVertex3f(-0.5, 0.5,-0.5)
170 glVertex3f(-0.5, 0.5, 0.5)
5b119149 171
2e839e96
RD
172 glNormal3f( 0.0,-1.0, 0.0)
173 glVertex3f(-0.5,-0.5,-0.5)
174 glVertex3f( 0.5,-0.5,-0.5)
175 glVertex3f( 0.5,-0.5, 0.5)
176 glVertex3f(-0.5,-0.5, 0.5)
cf694132 177
2e839e96
RD
178 glNormal3f( 1.0, 0.0, 0.0)
179 glVertex3f( 0.5, 0.5, 0.5)
180 glVertex3f( 0.5,-0.5, 0.5)
181 glVertex3f( 0.5,-0.5,-0.5)
182 glVertex3f( 0.5, 0.5,-0.5)
41378d34 183
2e839e96
RD
184 glNormal3f(-1.0, 0.0, 0.0)
185 glVertex3f(-0.5,-0.5,-0.5)
186 glVertex3f(-0.5,-0.5, 0.5)
187 glVertex3f(-0.5, 0.5, 0.5)
188 glVertex3f(-0.5, 0.5,-0.5)
189 glEnd()
41378d34 190
2e839e96
RD
191 glRotatef((self.lasty - self.y)/100., 1.0, 0.0, 0.0);
192 glRotatef((self.lastx - self.x)/100., 0.0, 1.0, 0.0);
41378d34 193
2e839e96 194 self.SwapBuffers()
41378d34 195
41378d34 196
41378d34 197
cf694132 198
cf694132 199
2e839e96
RD
200class ConeCanvas(MyCanvasBase):
201 def InitGL( self ):
202 glMatrixMode(GL_PROJECTION);
203 # camera frustrum setup
204 glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0);
205 glMaterial(GL_FRONT, GL_AMBIENT, [0.2, 0.2, 0.2, 1.0])
206 glMaterial(GL_FRONT, GL_DIFFUSE, [0.8, 0.8, 0.8, 1.0])
207 glMaterial(GL_FRONT, GL_SPECULAR, [1.0, 0.0, 1.0, 1.0])
208 glMaterial(GL_FRONT, GL_SHININESS, 50.0)
209 glLight(GL_LIGHT0, GL_AMBIENT, [0.0, 1.0, 0.0, 1.0])
210 glLight(GL_LIGHT0, GL_DIFFUSE, [1.0, 1.0, 1.0, 1.0])
211 glLight(GL_LIGHT0, GL_SPECULAR, [1.0, 1.0, 1.0, 1.0])
212 glLight(GL_LIGHT0, GL_POSITION, [1.0, 1.0, 1.0, 0.0]);
213 glLightModel(GL_LIGHT_MODEL_AMBIENT, [0.2, 0.2, 0.2, 1.0])
214 glEnable(GL_LIGHTING)
215 glEnable(GL_LIGHT0)
216 glDepthFunc(GL_LESS)
217 glEnable(GL_DEPTH_TEST)
218 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
219 # position viewer
220 glMatrixMode(GL_MODELVIEW);
221
222
223 def OnDraw(self):
224 # clear color and depth buffers
225 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
226 # use a fresh transformation matrix
227 glPushMatrix()
228 # position object
229 glTranslate(0.0, 0.0, -2.0);
230 glRotate(30.0, 1.0, 0.0, 0.0);
231 glRotate(30.0, 0.0, 1.0, 0.0);
232
233 glTranslate(0, -1, 0)
234 glRotate(250, 1, 0, 0)
235 glutSolidCone(0.5, 1, 30, 5)
236 glPopMatrix()
237 glRotatef((self.lasty - self.y)/100., 0.0, 0.0, 1.0);
238 glRotatef(0.0, (self.lastx - self.x)/100., 1.0, 0.0);
239 # push into visible buffer
240 self.SwapBuffers()
cf694132 241
41378d34
RD
242
243
244
2e839e96 245#----------------------------------------------------------------------
cf694132
RD
246
247
2e839e96
RD
248def runTest(frame, nb, log):
249 win = ButtonPanel(nb, log)
250 return win
5b119149
RD
251
252
cf694132
RD
253
254
255
41378d34 256
cf694132
RD
257overview = """\
258"""
259
260
cf694132 261
41378d34
RD
262if __name__ == '__main__':
263 import sys,os
264 import run
8eca4fef 265 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
cf694132 266
cf694132 267
41378d34
RD
268
269
270#----------------------------------------------------------------------
d58a80fa 271