]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/wxGLCanvas.py
An attempt at make the wxCSConv class useful. Uses iconv under Unix,
[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
11 # http://starship.python.net:9673/crew/da/Code/PyOpenGL/
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'
31 'http://starship.python.net:9673/crew/da/Code/PyOpenGL/',
32 'Sorry', wxOK | wxICON_INFORMATION)
33 dlg.ShowModal()
34 dlg.Destroy()
cf694132
RD
35
36
54b96882 37else:
cf694132 38 def runTest(frame, nb, log):
d2103cf2
RD
39 win = wxFrame(frame, -1, "GL Demos", wxDefaultPosition, wxSize(300,300))
40 CubeCanvas(win)
41 #MySplitter(win)
6bddd8c5
RD
42 frame.otherWin = win
43 win.Show(true)
44 return None
cf694132
RD
45
46
d2103cf2
RD
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
cf694132 56
d2103cf2
RD
57
58 class CubeCanvas(wxGLCanvas):
cf694132
RD
59 def __init__(self, parent):
60 wxGLCanvas.__init__(self, parent, -1)
61 EVT_ERASE_BACKGROUND(self, self.OnEraseBackground)
f6bcfd97
BP
62 EVT_SIZE(self, self.OnSize)
63 EVT_PAINT(self, self.OnPaint)
cf694132
RD
64 self.init = false
65
66 def OnEraseBackground(self, event):
67 pass # Do nothing, to avoid flashing.
68
cf694132
RD
69 def OnSize(self, event):
70 size = self.GetClientSize()
d2103cf2 71 if self.GetContext():
cf694132
RD
72 self.SetCurrent()
73 glViewport(0, 0, size.width, size.height)
74
75
76 def OnPaint(self, event):
77 dc = wxPaintDC(self)
78
cf694132
RD
79 self.SetCurrent()
80
cf694132
RD
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
d2103cf2
RD
149 class ConeCanvas(wxGLCanvas):
150 def __init__(self, parent):
151 wxGLCanvas.__init__(self, parent, -1)
152 EVT_ERASE_BACKGROUND(self, self.OnEraseBackground)
f6bcfd97
BP
153 EVT_SIZE(self, self.OnSize)
154 EVT_PAINT(self, self.OnPaint)
d2103cf2
RD
155 self.init = false
156
157 def OnEraseBackground(self, event):
158 pass # Do nothing, to avoid flashing.
159
160 def OnSize(self, event):
161 size = self.GetClientSize()
162 if self.GetContext():
163 self.SetCurrent()
164 glViewport(0, 0, size.width, size.height)
165
166 def GLInit( self ):
167 glMatrixMode(GL_PROJECTION);
168 # camera frustrum setup
169 glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0);
170 glMaterial(GL_FRONT, GL_AMBIENT, [0.2, 0.2, 0.2, 1.0])
171 glMaterial(GL_FRONT, GL_DIFFUSE, [0.8, 0.8, 0.8, 1.0])
172 glMaterial(GL_FRONT, GL_SPECULAR, [1.0, 0.0, 1.0, 1.0])
173 glMaterial(GL_FRONT, GL_SHININESS, 50.0)
174 glLight(GL_LIGHT0, GL_AMBIENT, [0.0, 1.0, 0.0, 1.0])
175 glLight(GL_LIGHT0, GL_DIFFUSE, [1.0, 1.0, 1.0, 1.0])
176 glLight(GL_LIGHT0, GL_SPECULAR, [1.0, 1.0, 1.0, 1.0])
177 glLight(GL_LIGHT0, GL_POSITION, [1.0, 1.0, 1.0, 0.0]);
178 glLightModel(GL_LIGHT_MODEL_AMBIENT, [0.2, 0.2, 0.2, 1.0])
179 glEnable(GL_LIGHTING)
180 glEnable(GL_LIGHT0)
181 glDepthFunc(GL_LESS)
182 glEnable(GL_DEPTH_TEST)
183 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
184
185
186 def OnPaint( self, event ):
187 dc = wxPaintDC(self)
188 if not self.init:
189 self.GLInit()
190 self.init = true
191
192 ### Tell system to use _this_ glcanvas for all commands
193 self.SetCurrent()
194 # clear color and depth buffers
195 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
196 # position viewer
197 glMatrixMode(GL_MODELVIEW);
198 # use a fresh transformation matrix
199 glPushMatrix()
200 # position object
201 glTranslate(0.0, 0.0, -2.0);
202 glRotate(30.0, 1.0, 0.0, 0.0);
203 glRotate(30.0, 0.0, 1.0, 0.0);
204
205 ### From cone.py
206 glTranslate(0, -1, 0)
207 glRotate(250, 1, 0, 0)
208 glutSolidCone(1, 2, 50, 10)
209 glPopMatrix()
210 # push into visible buffer
211 self.SwapBuffers()
cf694132
RD
212
213
e166644c 214#----------------------------------------------------------------------
cf694132
RD
215
216
217
218
219
220
221
222
223overview = """\
224"""
225
226
227
228
229
230#----------------------------------------------------------------------
231
232def _test():
233 class MyApp(wxApp):
234 def OnInit(self):
d2103cf2
RD
235 frame = wxFrame(NULL, -1, "GL Demos", wxDefaultPosition, wxSize(600,300))
236 #win = ConeCanvas(frame)
237 MySplitter(frame)
cf694132
RD
238 frame.Show(TRUE)
239 self.SetTopWindow(frame)
240 return TRUE
241
242 app = MyApp(0)
243 app.MainLoop()
244
245if __name__ == '__main__':
246 _test()