]>
Commit | Line | Data |
---|---|---|
cf694132 RD |
1 | |
2 | from wxPython.wx import * | |
3 | try: | |
4 | from wxPython.glcanvas import * | |
5 | haveGLCanvas = true | |
6 | except ImportError: | |
7 | haveGLCanvas = false | |
8 | ||
54b96882 RD |
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 | ||
cf694132 RD |
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 | ||
54b96882 RD |
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() | |
cf694132 RD |
35 | |
36 | ||
54b96882 | 37 | else: |
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) | |
62 | self.init = false | |
63 | ||
64 | def OnEraseBackground(self, event): | |
65 | pass # Do nothing, to avoid flashing. | |
66 | ||
cf694132 RD |
67 | def OnSize(self, event): |
68 | size = self.GetClientSize() | |
d2103cf2 | 69 | if self.GetContext(): |
cf694132 RD |
70 | self.SetCurrent() |
71 | glViewport(0, 0, size.width, size.height) | |
72 | ||
73 | ||
74 | def OnPaint(self, event): | |
75 | dc = wxPaintDC(self) | |
76 | ||
cf694132 RD |
77 | self.SetCurrent() |
78 | ||
cf694132 RD |
79 | if not self.init: |
80 | self.InitGL() | |
81 | self.init = true | |
82 | ||
83 | # clear color and depth buffers | |
84 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
85 | ||
86 | # draw six faces of a cube | |
87 | glBegin(GL_QUADS) | |
88 | glNormal3f( 0.0, 0.0, 1.0) | |
89 | glVertex3f( 0.5, 0.5, 0.5) | |
90 | glVertex3f(-0.5, 0.5, 0.5) | |
91 | glVertex3f(-0.5,-0.5, 0.5) | |
92 | glVertex3f( 0.5,-0.5, 0.5) | |
93 | ||
94 | glNormal3f( 0.0, 0.0,-1.0) | |
95 | glVertex3f(-0.5,-0.5,-0.5) | |
96 | glVertex3f(-0.5, 0.5,-0.5) | |
97 | glVertex3f( 0.5, 0.5,-0.5) | |
98 | glVertex3f( 0.5,-0.5,-0.5) | |
99 | ||
100 | glNormal3f( 0.0, 1.0, 0.0) | |
101 | glVertex3f( 0.5, 0.5, 0.5) | |
102 | glVertex3f( 0.5, 0.5,-0.5) | |
103 | glVertex3f(-0.5, 0.5,-0.5) | |
104 | glVertex3f(-0.5, 0.5, 0.5) | |
105 | ||
106 | glNormal3f( 0.0,-1.0, 0.0) | |
107 | glVertex3f(-0.5,-0.5,-0.5) | |
108 | glVertex3f( 0.5,-0.5,-0.5) | |
109 | glVertex3f( 0.5,-0.5, 0.5) | |
110 | glVertex3f(-0.5,-0.5, 0.5) | |
111 | ||
112 | glNormal3f( 1.0, 0.0, 0.0) | |
113 | glVertex3f( 0.5, 0.5, 0.5) | |
114 | glVertex3f( 0.5,-0.5, 0.5) | |
115 | glVertex3f( 0.5,-0.5,-0.5) | |
116 | glVertex3f( 0.5, 0.5,-0.5) | |
117 | ||
118 | glNormal3f(-1.0, 0.0, 0.0) | |
119 | glVertex3f(-0.5,-0.5,-0.5) | |
120 | glVertex3f(-0.5,-0.5, 0.5) | |
121 | glVertex3f(-0.5, 0.5, 0.5) | |
122 | glVertex3f(-0.5, 0.5,-0.5) | |
123 | glEnd() | |
124 | ||
125 | self.SwapBuffers() | |
126 | ||
127 | ||
128 | def InitGL(self): | |
129 | # set viewing projection | |
130 | glMatrixMode(GL_PROJECTION); | |
131 | glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0); | |
132 | ||
133 | # position viewer | |
134 | glMatrixMode(GL_MODELVIEW); | |
135 | glTranslatef(0.0, 0.0, -2.0); | |
136 | ||
137 | # position object | |
138 | glRotatef(30.0, 1.0, 0.0, 0.0); | |
139 | glRotatef(30.0, 0.0, 1.0, 0.0); | |
140 | ||
141 | glEnable(GL_DEPTH_TEST); | |
142 | glEnable(GL_LIGHTING); | |
143 | glEnable(GL_LIGHT0); | |
144 | ||
145 | ||
146 | ||
d2103cf2 RD |
147 | class ConeCanvas(wxGLCanvas): |
148 | def __init__(self, parent): | |
149 | wxGLCanvas.__init__(self, parent, -1) | |
150 | EVT_ERASE_BACKGROUND(self, self.OnEraseBackground) | |
151 | self.init = false | |
152 | ||
153 | def OnEraseBackground(self, event): | |
154 | pass # Do nothing, to avoid flashing. | |
155 | ||
156 | def OnSize(self, event): | |
157 | size = self.GetClientSize() | |
158 | if self.GetContext(): | |
159 | self.SetCurrent() | |
160 | glViewport(0, 0, size.width, size.height) | |
161 | ||
162 | def GLInit( self ): | |
163 | glMatrixMode(GL_PROJECTION); | |
164 | # camera frustrum setup | |
165 | glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0); | |
166 | glMaterial(GL_FRONT, GL_AMBIENT, [0.2, 0.2, 0.2, 1.0]) | |
167 | glMaterial(GL_FRONT, GL_DIFFUSE, [0.8, 0.8, 0.8, 1.0]) | |
168 | glMaterial(GL_FRONT, GL_SPECULAR, [1.0, 0.0, 1.0, 1.0]) | |
169 | glMaterial(GL_FRONT, GL_SHININESS, 50.0) | |
170 | glLight(GL_LIGHT0, GL_AMBIENT, [0.0, 1.0, 0.0, 1.0]) | |
171 | glLight(GL_LIGHT0, GL_DIFFUSE, [1.0, 1.0, 1.0, 1.0]) | |
172 | glLight(GL_LIGHT0, GL_SPECULAR, [1.0, 1.0, 1.0, 1.0]) | |
173 | glLight(GL_LIGHT0, GL_POSITION, [1.0, 1.0, 1.0, 0.0]); | |
174 | glLightModel(GL_LIGHT_MODEL_AMBIENT, [0.2, 0.2, 0.2, 1.0]) | |
175 | glEnable(GL_LIGHTING) | |
176 | glEnable(GL_LIGHT0) | |
177 | glDepthFunc(GL_LESS) | |
178 | glEnable(GL_DEPTH_TEST) | |
179 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) | |
180 | ||
181 | ||
182 | def OnPaint( self, event ): | |
183 | dc = wxPaintDC(self) | |
184 | if not self.init: | |
185 | self.GLInit() | |
186 | self.init = true | |
187 | ||
188 | ### Tell system to use _this_ glcanvas for all commands | |
189 | self.SetCurrent() | |
190 | # clear color and depth buffers | |
191 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
192 | # position viewer | |
193 | glMatrixMode(GL_MODELVIEW); | |
194 | # use a fresh transformation matrix | |
195 | glPushMatrix() | |
196 | # position object | |
197 | glTranslate(0.0, 0.0, -2.0); | |
198 | glRotate(30.0, 1.0, 0.0, 0.0); | |
199 | glRotate(30.0, 0.0, 1.0, 0.0); | |
200 | ||
201 | ### From cone.py | |
202 | glTranslate(0, -1, 0) | |
203 | glRotate(250, 1, 0, 0) | |
204 | glutSolidCone(1, 2, 50, 10) | |
205 | glPopMatrix() | |
206 | # push into visible buffer | |
207 | self.SwapBuffers() | |
cf694132 RD |
208 | |
209 | ||
e166644c | 210 | #---------------------------------------------------------------------- |
cf694132 RD |
211 | |
212 | ||
213 | ||
214 | ||
215 | ||
216 | ||
217 | ||
218 | ||
219 | overview = """\ | |
220 | """ | |
221 | ||
222 | ||
223 | ||
224 | ||
225 | ||
226 | #---------------------------------------------------------------------- | |
227 | ||
228 | def _test(): | |
229 | class MyApp(wxApp): | |
230 | def OnInit(self): | |
d2103cf2 RD |
231 | frame = wxFrame(NULL, -1, "GL Demos", wxDefaultPosition, wxSize(600,300)) |
232 | #win = ConeCanvas(frame) | |
233 | MySplitter(frame) | |
cf694132 RD |
234 | frame.Show(TRUE) |
235 | self.SetTopWindow(frame) | |
236 | return TRUE | |
237 | ||
238 | app = MyApp(0) | |
239 | app.MainLoop() | |
240 | ||
241 | if __name__ == '__main__': | |
242 | _test() |