]> git.saurik.com Git - wxWidgets.git/blob - utils/wxPython/demo/wxGLCanvas.py
Enabling the use of masks for wxGTK in some of the demos
[wxWidgets.git] / utils / 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://starship.python.net:9673/crew/da/Code/PyOpenGL/
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://starship.python.net:9673/crew/da/Code/PyOpenGL/',
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 EVT_ERASE_BACKGROUND(self, self.OnEraseBackground)
62 self.init = false
63
64 def OnEraseBackground(self, event):
65 pass # Do nothing, to avoid flashing.
66
67 def OnSize(self, event):
68 size = self.GetClientSize()
69 print size
70 if self.GetContext():
71 self.SetCurrent()
72 glViewport(0, 0, size.width, size.height)
73
74
75 def OnPaint(self, event):
76 dc = wxPaintDC(self)
77
78 self.SetCurrent()
79 print self.init
80
81 if not self.init:
82 self.InitGL()
83 self.init = true
84
85 # clear color and depth buffers
86 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
87
88 # draw six faces of a cube
89 glBegin(GL_QUADS)
90 glNormal3f( 0.0, 0.0, 1.0)
91 glVertex3f( 0.5, 0.5, 0.5)
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
96 glNormal3f( 0.0, 0.0,-1.0)
97 glVertex3f(-0.5,-0.5,-0.5)
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
102 glNormal3f( 0.0, 1.0, 0.0)
103 glVertex3f( 0.5, 0.5, 0.5)
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
108 glNormal3f( 0.0,-1.0, 0.0)
109 glVertex3f(-0.5,-0.5,-0.5)
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
114 glNormal3f( 1.0, 0.0, 0.0)
115 glVertex3f( 0.5, 0.5, 0.5)
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
120 glNormal3f(-1.0, 0.0, 0.0)
121 glVertex3f(-0.5,-0.5,-0.5)
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 glEnd()
126
127 self.SwapBuffers()
128
129
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(30.0, 1.0, 0.0, 0.0);
141 glRotatef(30.0, 0.0, 1.0, 0.0);
142
143 glEnable(GL_DEPTH_TEST);
144 glEnable(GL_LIGHTING);
145 glEnable(GL_LIGHT0);
146
147
148
149 class ConeCanvas(wxGLCanvas):
150 def __init__(self, parent):
151 wxGLCanvas.__init__(self, parent, -1)
152 EVT_ERASE_BACKGROUND(self, self.OnEraseBackground)
153 self.init = false
154
155 def OnEraseBackground(self, event):
156 pass # Do nothing, to avoid flashing.
157
158 def OnSize(self, event):
159 size = self.GetClientSize()
160 if self.GetContext():
161 self.SetCurrent()
162 glViewport(0, 0, size.width, size.height)
163
164 def GLInit( self ):
165 glMatrixMode(GL_PROJECTION);
166 # camera frustrum setup
167 glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0);
168 glMaterial(GL_FRONT, GL_AMBIENT, [0.2, 0.2, 0.2, 1.0])
169 glMaterial(GL_FRONT, GL_DIFFUSE, [0.8, 0.8, 0.8, 1.0])
170 glMaterial(GL_FRONT, GL_SPECULAR, [1.0, 0.0, 1.0, 1.0])
171 glMaterial(GL_FRONT, GL_SHININESS, 50.0)
172 glLight(GL_LIGHT0, GL_AMBIENT, [0.0, 1.0, 0.0, 1.0])
173 glLight(GL_LIGHT0, GL_DIFFUSE, [1.0, 1.0, 1.0, 1.0])
174 glLight(GL_LIGHT0, GL_SPECULAR, [1.0, 1.0, 1.0, 1.0])
175 glLight(GL_LIGHT0, GL_POSITION, [1.0, 1.0, 1.0, 0.0]);
176 glLightModel(GL_LIGHT_MODEL_AMBIENT, [0.2, 0.2, 0.2, 1.0])
177 glEnable(GL_LIGHTING)
178 glEnable(GL_LIGHT0)
179 glDepthFunc(GL_LESS)
180 glEnable(GL_DEPTH_TEST)
181 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
182
183
184 def OnPaint( self, event ):
185 dc = wxPaintDC(self)
186 if not self.init:
187 self.GLInit()
188 self.init = true
189
190 ### Tell system to use _this_ glcanvas for all commands
191 self.SetCurrent()
192 # clear color and depth buffers
193 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
194 # position viewer
195 glMatrixMode(GL_MODELVIEW);
196 # use a fresh transformation matrix
197 glPushMatrix()
198 # position object
199 glTranslate(0.0, 0.0, -2.0);
200 glRotate(30.0, 1.0, 0.0, 0.0);
201 glRotate(30.0, 0.0, 1.0, 0.0);
202
203 ### From cone.py
204 glTranslate(0, -1, 0)
205 glRotate(250, 1, 0, 0)
206 glutSolidCone(1, 2, 50, 10)
207 glPopMatrix()
208 # push into visible buffer
209 self.SwapBuffers()
210
211
212 #----------------------------------------------------------------------
213
214
215
216
217
218
219
220
221 overview = """\
222 """
223
224
225
226
227
228 #----------------------------------------------------------------------
229
230 def _test():
231 class MyApp(wxApp):
232 def OnInit(self):
233 frame = wxFrame(NULL, -1, "GL Demos", wxDefaultPosition, wxSize(600,300))
234 #win = ConeCanvas(frame)
235 MySplitter(frame)
236 frame.Show(TRUE)
237 self.SetTopWindow(frame)
238 return TRUE
239
240 app = MyApp(0)
241 app.MainLoop()
242
243 if __name__ == '__main__':
244 _test()