1 from wxPython
.wx
import *
3 from wxPython
.glcanvas
import *
9 # The Python OpenGL package can be found at
10 # http://PyOpenGL.sourceforge.net/
11 from OpenGL
.GL
import *
12 from OpenGL
.GLUT
import *
17 #----------------------------------------------------------------------
20 def runTest(frame
, nb
, log
):
21 dlg
= wxMessageDialog(frame
, 'The wxGLCanvas has not been included with this build of wxPython!',
22 'Sorry', wxOK | wxICON_INFORMATION
)
27 def runTest(frame
, nb
, log
):
28 dlg
= wxMessageDialog(frame
,
29 'The OpenGL package was not found. You can get it at\n'
30 'http://PyOpenGL.sourceforge.net/',
31 'Sorry', wxOK | wxICON_INFORMATION
)
40 wxNewId() : ('CubeCanvas', 'Cube'),
41 wxNewId() : ('ConeCanvas', 'Cone'),
44 class ButtonPanel(wxPanel
):
45 def __init__(self
, parent
, log
):
46 wxPanel
.__init
__(self
, parent
, -1)
49 box
= wxBoxSizer(wxVERTICAL
)
51 keys
= buttonDefs
.keys()
54 text
= buttonDefs
[k
][1]
55 btn
= wxButton(self
, k
, text
)
56 box
.Add(btn
, 0, wxALIGN_CENTER|wxALL
, 15)
57 EVT_BUTTON(self
, k
, self
.OnButton
)
59 #** Enable this to show putting a wxGLCanvas on the wxPanel
63 box
.Add(c
, 0, wxALIGN_CENTER|wxALL
, 15)
65 self
.SetAutoLayout(true
)
69 def OnButton(self
, evt
):
70 canvasClassName
= buttonDefs
[evt
.GetId()][0]
71 canvasClass
= eval(canvasClassName
)
72 frame
= wxFrame(None, -1, canvasClassName
, size
=(400,400))
73 canvas
= canvasClass(frame
)
78 def runTest(frame
, nb
, log
):
79 win
= ButtonPanel(nb
, log
)
85 class MyCanvasBase(wxGLCanvas
):
86 def __init__(self
, parent
):
87 wxGLCanvas
.__init
__(self
, parent
, -1)
89 # initial mouse position
90 self
.lastx
= self
.x
= 30
91 self
.lasty
= self
.y
= 30
92 EVT_ERASE_BACKGROUND(self
, self
.OnEraseBackground
)
93 EVT_SIZE(self
, self
.OnSize
)
94 EVT_PAINT(self
, self
.OnPaint
)
95 EVT_LEFT_DOWN(self
, self
.OnMouseDown
) # needs fixing...
96 EVT_LEFT_UP(self
, self
.OnMouseUp
)
97 EVT_MOTION(self
, self
.OnMouseMotion
)
99 def OnEraseBackground(self
, event
):
100 pass # Do nothing, to avoid flashing on MSW.
102 def OnSize(self
, event
):
103 size
= self
.GetClientSize()
104 if self
.GetContext():
106 glViewport(0, 0, size
.width
, size
.height
)
108 def OnPaint(self
, event
):
116 def OnMouseDown(self
, evt
):
119 def OnMouseUp(self
, evt
):
122 def OnMouseMotion(self
, evt
):
123 if evt
.Dragging() and evt
.LeftIsDown():
124 self
.x
, self
.y
= self
.lastx
, self
.lasty
125 self
.x
, self
.y
= evt
.GetPosition()
131 class CubeCanvas(MyCanvasBase
):
133 # set viewing projection
134 glMatrixMode(GL_PROJECTION
);
135 glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0);
138 glMatrixMode(GL_MODELVIEW
);
139 glTranslatef(0.0, 0.0, -2.0);
142 glRotatef(self
.y
, 1.0, 0.0, 0.0);
143 glRotatef(self
.x
, 0.0, 1.0, 0.0);
145 glEnable(GL_DEPTH_TEST
);
146 glEnable(GL_LIGHTING
);
151 # clear color and depth buffers
152 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
);
154 # draw six faces of a cube
156 glNormal3f( 0.0, 0.0, 1.0)
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 glVertex3f( 0.5,-0.5, 0.5)
162 glNormal3f( 0.0, 0.0,-1.0)
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 glVertex3f( 0.5,-0.5,-0.5)
168 glNormal3f( 0.0, 1.0, 0.0)
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 glVertex3f(-0.5, 0.5, 0.5)
174 glNormal3f( 0.0,-1.0, 0.0)
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 glVertex3f(-0.5,-0.5, 0.5)
180 glNormal3f( 1.0, 0.0, 0.0)
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 glVertex3f( 0.5, 0.5,-0.5)
186 glNormal3f(-1.0, 0.0, 0.0)
187 glVertex3f(-0.5,-0.5,-0.5)
188 glVertex3f(-0.5,-0.5, 0.5)
189 glVertex3f(-0.5, 0.5, 0.5)
190 glVertex3f(-0.5, 0.5,-0.5)
193 glRotatef((self
.lasty
- self
.y
)/100., 1.0, 0.0, 0.0);
194 glRotatef((self
.lastx
- self
.x
)/100., 0.0, 1.0, 0.0);
202 class ConeCanvas(MyCanvasBase
):
204 glMatrixMode(GL_PROJECTION
);
205 # camera frustrum setup
206 glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0);
207 glMaterial(GL_FRONT
, GL_AMBIENT
, [0.2, 0.2, 0.2, 1.0])
208 glMaterial(GL_FRONT
, GL_DIFFUSE
, [0.8, 0.8, 0.8, 1.0])
209 glMaterial(GL_FRONT
, GL_SPECULAR
, [1.0, 0.0, 1.0, 1.0])
210 glMaterial(GL_FRONT
, GL_SHININESS
, 50.0)
211 glLight(GL_LIGHT0
, GL_AMBIENT
, [0.0, 1.0, 0.0, 1.0])
212 glLight(GL_LIGHT0
, GL_DIFFUSE
, [1.0, 1.0, 1.0, 1.0])
213 glLight(GL_LIGHT0
, GL_SPECULAR
, [1.0, 1.0, 1.0, 1.0])
214 glLight(GL_LIGHT0
, GL_POSITION
, [1.0, 1.0, 1.0, 0.0]);
215 glLightModel(GL_LIGHT_MODEL_AMBIENT
, [0.2, 0.2, 0.2, 1.0])
216 glEnable(GL_LIGHTING
)
219 glEnable(GL_DEPTH_TEST
)
220 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
)
222 glMatrixMode(GL_MODELVIEW
);
226 # clear color and depth buffers
227 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
);
228 # use a fresh transformation matrix
231 glTranslate(0.0, 0.0, -2.0);
232 glRotate(30.0, 1.0, 0.0, 0.0);
233 glRotate(30.0, 0.0, 1.0, 0.0);
235 glTranslate(0, -1, 0)
236 glRotate(250, 1, 0, 0)
237 glutSolidCone(0.5, 1, 30, 5)
239 glRotatef((self
.lasty
- self
.y
)/100., 0.0, 0.0, 1.0);
240 glRotatef(0.0, (self
.lastx
- self
.x
)/100., 1.0, 0.0);
241 # push into visible buffer
247 #----------------------------------------------------------------------
263 #----------------------------------------------------------------------
268 frame
= wxFrame(None, -1, "GL Demos", wxDefaultPosition
, wxSize(600,300))
269 #win = ConeCanvas(frame)
272 self
.SetTopWindow(frame
)
278 if __name__
== '__main__':