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