]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/GLCanvas.py
drop highlight color fix
[wxWidgets.git] / wxPython / demo / GLCanvas.py
CommitLineData
8fa876ca 1
41378d34 2import wx
0a49a386 3import sys
8fa876ca 4
cf694132 5try:
41378d34 6 from wx import glcanvas
1e4a197e 7 haveGLCanvas = True
cf694132 8except ImportError:
1e4a197e 9 haveGLCanvas = False
cf694132 10
54b96882
RD
11try:
12 # The Python OpenGL package can be found at
c368d904 13 # http://PyOpenGL.sourceforge.net/
41378d34
RD
14 from OpenGL.GL import *
15 from OpenGL.GLUT import *
1e4a197e 16 haveOpenGL = True
54b96882 17except ImportError:
1e4a197e 18 haveOpenGL = False
54b96882 19
cf694132
RD
20#----------------------------------------------------------------------
21
2e839e96
RD
22
23buttonDefs = {
24 wx.NewId() : ('CubeCanvas', 'Cube'),
25 wx.NewId() : ('ConeCanvas', 'Cone'),
26 }
27
28class 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!',
c4ef95da 57 'Sorry', wx.OK | wx.ICON_WARNING)
2e839e96
RD
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/',
c4ef95da 65 'Sorry', wx.OK | wx.ICON_WARNING)
2e839e96
RD
66 dlg.ShowModal()
67 dlg.Destroy()
68
69 else:
5b119149
RD
70 canvasClassName = buttonDefs[evt.GetId()][0]
71 canvasClass = eval(canvasClassName)
8fa876ca 72 frame = wx.Frame(None, -1, canvasClassName, size=(400,400))
5b119149 73 canvas = canvasClass(frame)
1e4a197e 74 frame.Show(True)
cf694132
RD
75
76
2e839e96
RD
77class MyCanvasBase(glcanvas.GLCanvas):
78 def __init__(self, parent):
79 glcanvas.GLCanvas.__init__(self, parent, -1)
80 self.init = False
99953df0
RD
81 self.context = glcanvas.GLContext(self)
82
2e839e96
RD
83 # initial mouse position
84 self.lastx = self.x = 30
85 self.lasty = self.y = 30
02b800ce 86 self.size = None
2e839e96
RD
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)
d2103cf2 93
d2103cf2 94
2e839e96
RD
95 def OnEraseBackground(self, event):
96 pass # Do nothing, to avoid flashing on MSW.
cf694132 97
d2103cf2 98
2e839e96 99 def OnSize(self, event):
99953df0 100 wx.CallAfter(self.DoSetViewport)
2e839e96 101 event.Skip()
d58a80fa 102
99953df0
RD
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
cf694132 109
2e839e96
RD
110 def OnPaint(self, event):
111 dc = wx.PaintDC(self)
99953df0 112 self.SetCurrent(self.context)
2e839e96
RD
113 if not self.init:
114 self.InitGL()
115 self.init = True
116 self.OnDraw()
01f44840 117
cf694132 118
2e839e96
RD
119 def OnMouseDown(self, evt):
120 self.CaptureMouse()
02b800ce 121 self.x, self.y = self.lastx, self.lasty = evt.GetPosition()
01f44840 122
cf694132 123
2e839e96
RD
124 def OnMouseUp(self, evt):
125 self.ReleaseMouse()
5b119149 126
01f44840 127
2e839e96
RD
128 def OnMouseMotion(self, evt):
129 if evt.Dragging() and evt.LeftIsDown():
02b800ce 130 self.lastx, self.lasty = self.x, self.y
2e839e96
RD
131 self.x, self.y = evt.GetPosition()
132 self.Refresh(False)
5b119149 133
01f44840 134
5b119149 135
01f44840 136
2e839e96
RD
137class CubeCanvas(MyCanvasBase):
138 def InitGL(self):
139 # set viewing projection
02b800ce
RD
140 glMatrixMode(GL_PROJECTION)
141 glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0)
2e839e96
RD
142
143 # position viewer
02b800ce
RD
144 glMatrixMode(GL_MODELVIEW)
145 glTranslatef(0.0, 0.0, -2.0)
2e839e96
RD
146
147 # position object
02b800ce
RD
148 glRotatef(self.y, 1.0, 0.0, 0.0)
149 glRotatef(self.x, 0.0, 1.0, 0.0)
2e839e96 150
02b800ce
RD
151 glEnable(GL_DEPTH_TEST)
152 glEnable(GL_LIGHTING)
153 glEnable(GL_LIGHT0)
cf694132 154
5b119149 155
2e839e96
RD
156 def OnDraw(self):
157 # clear color and depth buffers
02b800ce 158 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
41378d34 159
2e839e96
RD
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)
41378d34 167
2e839e96
RD
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)
5b119149 173
2e839e96
RD
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)
5b119149 179
2e839e96
RD
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)
cf694132 185
2e839e96
RD
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)
41378d34 191
2e839e96
RD
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()
41378d34 198
02b800ce
RD
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);
41378d34 208
2e839e96 209 self.SwapBuffers()
41378d34 210
41378d34 211
41378d34 212
cf694132 213
cf694132 214
2e839e96
RD
215class ConeCanvas(MyCanvasBase):
216 def InitGL( self ):
02b800ce 217 glMatrixMode(GL_PROJECTION)
2e839e96 218 # camera frustrum setup
02b800ce 219 glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0)
2e839e96
RD
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])
02b800ce 227 glLight(GL_LIGHT0, GL_POSITION, [1.0, 1.0, 1.0, 0.0])
de8137ac 228 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, [0.2, 0.2, 0.2, 1.0])
2e839e96
RD
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
02b800ce
RD
235 glMatrixMode(GL_MODELVIEW)
236 # position viewer
237 glTranslatef(0.0, 0.0, -2.0);
de8137ac 238 #
0a49a386 239 glutInit(sys.argv)
2e839e96
RD
240
241
242 def OnDraw(self):
243 # clear color and depth buffers
02b800ce 244 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
2e839e96
RD
245 # use a fresh transformation matrix
246 glPushMatrix()
247 # position object
02b800ce
RD
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)
2e839e96
RD
251
252 glTranslate(0, -1, 0)
253 glRotate(250, 1, 0, 0)
254 glutSolidCone(0.5, 1, 30, 5)
255 glPopMatrix()
02b800ce
RD
256 glRotatef((self.y - self.lasty), 0.0, 0.0, 1.0);
257 glRotatef((self.x - self.lastx), 1.0, 0.0, 0.0);
2e839e96
RD
258 # push into visible buffer
259 self.SwapBuffers()
cf694132 260
41378d34
RD
261
262
263
2e839e96 264#----------------------------------------------------------------------
cf694132
RD
265
266
2e839e96
RD
267def runTest(frame, nb, log):
268 win = ButtonPanel(nb, log)
269 return win
5b119149
RD
270
271
cf694132
RD
272
273
274
41378d34 275
cf694132
RD
276overview = """\
277"""
278
279
cf694132 280
41378d34
RD
281if __name__ == '__main__':
282 import sys,os
283 import run
8eca4fef 284 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
cf694132 285
cf694132 286
41378d34
RD
287
288
289#----------------------------------------------------------------------
d58a80fa 290