]> git.saurik.com Git - wxWidgets.git/blame_incremental - wxPython/demo/GLCanvas.py
correcting allocated buffer size
[wxWidgets.git] / wxPython / demo / GLCanvas.py
... / ...
CommitLineData
1
2import wx
3
4try:
5 from wx import glcanvas
6 haveGLCanvas = True
7except ImportError:
8 haveGLCanvas = False
9
10try:
11 # The Python OpenGL package can be found at
12 # http://PyOpenGL.sourceforge.net/
13 from OpenGL.GL import *
14 from OpenGL.GLUT import *
15 haveOpenGL = True
16except ImportError:
17 haveOpenGL = False
18
19#----------------------------------------------------------------------
20
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!',
56 'Sorry', wx.OK | wx.ICON_WARNING)
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/',
64 'Sorry', wx.OK | wx.ICON_WARNING)
65 dlg.ShowModal()
66 dlg.Destroy()
67
68 else:
69 canvasClassName = buttonDefs[evt.GetId()][0]
70 canvasClass = eval(canvasClassName)
71 frame = wx.Frame(None, -1, canvasClassName, size=(400,400))
72 canvas = canvasClass(frame)
73 frame.Show(True)
74
75
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)
89
90
91 def OnEraseBackground(self, event):
92 pass # Do nothing, to avoid flashing on MSW.
93
94
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()
101
102
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()
110
111
112 def OnMouseDown(self, evt):
113 self.CaptureMouse()
114
115
116 def OnMouseUp(self, evt):
117 self.ReleaseMouse()
118
119
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)
125
126
127
128
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);
146
147
148 def OnDraw(self):
149 # clear color and depth buffers
150 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
151
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)
159
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)
165
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)
171
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)
177
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)
183
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()
190
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);
193
194 self.SwapBuffers()
195
196
197
198
199
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()
241
242
243
244
245#----------------------------------------------------------------------
246
247
248def runTest(frame, nb, log):
249 win = ButtonPanel(nb, log)
250 return win
251
252
253
254
255
256
257overview = """\
258"""
259
260
261
262if __name__ == '__main__':
263 import sys,os
264 import run
265 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
266
267
268
269
270#----------------------------------------------------------------------
271