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