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