]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/wxGLCanvas.py
hopefully fixes focus assertion
[wxWidgets.git] / wxPython / demo / wxGLCanvas.py
1
2 from wxPython.wx import *
3 try:
4 from wxPython.glcanvas import *
5 haveGLCanvas = true
6 except ImportError:
7 haveGLCanvas = false
8
9 try:
10 # The Python OpenGL package can be found at
11 # http://PyOpenGL.sourceforge.net/
12 from OpenGL.GL import *
13 from OpenGL.GLUT import *
14 haveOpenGL = true
15 except ImportError:
16 haveOpenGL = false
17
18 #----------------------------------------------------------------------
19
20 if 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
27 elif 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'
31 'http://PyOpenGL.sourceforge.net/',
32 'Sorry', wxOK | wxICON_INFORMATION)
33 dlg.ShowModal()
34 dlg.Destroy()
35
36
37 else:
38 def runTest(frame, nb, log):
39 win = wxFrame(frame, -1, "GL Demos", wxDefaultPosition, wxSize(300,300))
40 CubeCanvas(win)
41 #MySplitter(win)
42 frame.otherWin = win
43 win.Show(true)
44 return None
45
46
47 class MySplitter(wxSplitterWindow):
48 def __init__(self, parent):
49 wxSplitterWindow.__init__(self, parent, -1)
50 cube = CubeCanvas(self)
51 cone = ConeCanvas(self)
52
53 self.SplitVertically(cube, cone)
54 self.SetSashPosition(300)
55
56
57
58 class CubeCanvas(wxGLCanvas):
59 def __init__(self, parent):
60 wxGLCanvas.__init__(self, parent, -1) #,
61 #attribList=[GL_RED_BITS, 4, GL_DOUBLEBUFFER] )
62 EVT_ERASE_BACKGROUND(self, self.OnEraseBackground)
63 EVT_SIZE(self, self.OnSize)
64 EVT_PAINT(self, self.OnPaint)
65 self.init = false
66
67 def OnEraseBackground(self, event):
68 pass # Do nothing, to avoid flashing.
69
70 def OnSize(self, event):
71 size = self.GetClientSize()
72 if self.GetContext():
73 self.SetCurrent()
74 glViewport(0, 0, size.width, size.height)
75
76
77 def OnPaint(self, event):
78 dc = wxPaintDC(self)
79
80 self.SetCurrent()
81
82 if not self.init:
83 self.InitGL()
84 self.init = true
85
86 # clear color and depth buffers
87 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
88
89 # draw six faces of a cube
90 glBegin(GL_QUADS)
91 glNormal3f( 0.0, 0.0, 1.0)
92 glVertex3f( 0.5, 0.5, 0.5)
93 glVertex3f(-0.5, 0.5, 0.5)
94 glVertex3f(-0.5,-0.5, 0.5)
95 glVertex3f( 0.5,-0.5, 0.5)
96
97 glNormal3f( 0.0, 0.0,-1.0)
98 glVertex3f(-0.5,-0.5,-0.5)
99 glVertex3f(-0.5, 0.5,-0.5)
100 glVertex3f( 0.5, 0.5,-0.5)
101 glVertex3f( 0.5,-0.5,-0.5)
102
103 glNormal3f( 0.0, 1.0, 0.0)
104 glVertex3f( 0.5, 0.5, 0.5)
105 glVertex3f( 0.5, 0.5,-0.5)
106 glVertex3f(-0.5, 0.5,-0.5)
107 glVertex3f(-0.5, 0.5, 0.5)
108
109 glNormal3f( 0.0,-1.0, 0.0)
110 glVertex3f(-0.5,-0.5,-0.5)
111 glVertex3f( 0.5,-0.5,-0.5)
112 glVertex3f( 0.5,-0.5, 0.5)
113 glVertex3f(-0.5,-0.5, 0.5)
114
115 glNormal3f( 1.0, 0.0, 0.0)
116 glVertex3f( 0.5, 0.5, 0.5)
117 glVertex3f( 0.5,-0.5, 0.5)
118 glVertex3f( 0.5,-0.5,-0.5)
119 glVertex3f( 0.5, 0.5,-0.5)
120
121 glNormal3f(-1.0, 0.0, 0.0)
122 glVertex3f(-0.5,-0.5,-0.5)
123 glVertex3f(-0.5,-0.5, 0.5)
124 glVertex3f(-0.5, 0.5, 0.5)
125 glVertex3f(-0.5, 0.5,-0.5)
126 glEnd()
127
128 self.SwapBuffers()
129
130
131 def InitGL(self):
132 # set viewing projection
133 glMatrixMode(GL_PROJECTION);
134 glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0);
135
136 # position viewer
137 glMatrixMode(GL_MODELVIEW);
138 glTranslatef(0.0, 0.0, -2.0);
139
140 # position object
141 glRotatef(30.0, 1.0, 0.0, 0.0);
142 glRotatef(30.0, 0.0, 1.0, 0.0);
143
144 glEnable(GL_DEPTH_TEST);
145 glEnable(GL_LIGHTING);
146 glEnable(GL_LIGHT0);
147
148
149
150 class ConeCanvas(wxGLCanvas):
151 def __init__(self, parent):
152 wxGLCanvas.__init__(self, parent, -1)
153 EVT_ERASE_BACKGROUND(self, self.OnEraseBackground)
154 EVT_SIZE(self, self.OnSize)
155 EVT_PAINT(self, self.OnPaint)
156 self.init = false
157
158 def OnEraseBackground(self, event):
159 pass # Do nothing, to avoid flashing.
160
161 def OnSize(self, event):
162 size = self.GetClientSize()
163 if self.GetContext():
164 self.SetCurrent()
165 glViewport(0, 0, size.width, size.height)
166
167 def GLInit( self ):
168 glMatrixMode(GL_PROJECTION);
169 # camera frustrum setup
170 glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0);
171 glMaterial(GL_FRONT, GL_AMBIENT, [0.2, 0.2, 0.2, 1.0])
172 glMaterial(GL_FRONT, GL_DIFFUSE, [0.8, 0.8, 0.8, 1.0])
173 glMaterial(GL_FRONT, GL_SPECULAR, [1.0, 0.0, 1.0, 1.0])
174 glMaterial(GL_FRONT, GL_SHININESS, 50.0)
175 glLight(GL_LIGHT0, GL_AMBIENT, [0.0, 1.0, 0.0, 1.0])
176 glLight(GL_LIGHT0, GL_DIFFUSE, [1.0, 1.0, 1.0, 1.0])
177 glLight(GL_LIGHT0, GL_SPECULAR, [1.0, 1.0, 1.0, 1.0])
178 glLight(GL_LIGHT0, GL_POSITION, [1.0, 1.0, 1.0, 0.0]);
179 glLightModel(GL_LIGHT_MODEL_AMBIENT, [0.2, 0.2, 0.2, 1.0])
180 glEnable(GL_LIGHTING)
181 glEnable(GL_LIGHT0)
182 glDepthFunc(GL_LESS)
183 glEnable(GL_DEPTH_TEST)
184 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
185
186
187 def OnPaint( self, event ):
188 dc = wxPaintDC(self)
189 if not self.init:
190 self.GLInit()
191 self.init = true
192
193 ### Tell system to use _this_ glcanvas for all commands
194 self.SetCurrent()
195 # clear color and depth buffers
196 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
197 # position viewer
198 glMatrixMode(GL_MODELVIEW);
199 # use a fresh transformation matrix
200 glPushMatrix()
201 # position object
202 glTranslate(0.0, 0.0, -2.0);
203 glRotate(30.0, 1.0, 0.0, 0.0);
204 glRotate(30.0, 0.0, 1.0, 0.0);
205
206 ### From cone.py
207 glTranslate(0, -1, 0)
208 glRotate(250, 1, 0, 0)
209 glutSolidCone(1, 2, 50, 10)
210 glPopMatrix()
211 # push into visible buffer
212 self.SwapBuffers()
213
214
215 #----------------------------------------------------------------------
216
217
218
219
220
221
222
223
224 overview = """\
225 """
226
227
228
229
230
231 #----------------------------------------------------------------------
232
233 def _test():
234 class MyApp(wxApp):
235 def OnInit(self):
236 frame = wxFrame(None, -1, "GL Demos", wxDefaultPosition, wxSize(600,300))
237 #win = ConeCanvas(frame)
238 MySplitter(frame)
239 frame.Show(TRUE)
240 self.SetTopWindow(frame)
241 return TRUE
242
243 app = MyApp(0)
244 app.MainLoop()
245
246 if __name__ == '__main__':
247 _test()