5 from wx
import glcanvas
11 # The Python OpenGL package can be found at
12 # http://PyOpenGL.sourceforge.net/
13 from OpenGL
.GL
import *
14 from OpenGL
.GLUT
import *
19 #----------------------------------------------------------------------
23 wx
.NewId() : ('CubeCanvas', 'Cube'),
24 wx
.NewId() : ('ConeCanvas', 'Cone'),
27 class ButtonPanel(wx
.Panel
):
28 def __init__(self
, parent
, log
):
29 wx
.Panel
.__init
__(self
, parent
, -1)
32 box
= wx
.BoxSizer(wx
.VERTICAL
)
34 keys
= buttonDefs
.keys()
37 text
= buttonDefs
[k
][1]
38 btn
= wx
.Button(self
, k
, text
)
39 box
.Add(btn
, 0, wx
.ALIGN_CENTER|wx
.ALL
, 15)
40 self
.Bind(wx
.EVT_BUTTON
, self
.OnButton
, btn
)
42 #** Enable this to show putting a GLCanvas on the wx.Panel
46 box
.Add(c
, 0, wx
.ALIGN_CENTER|wx
.ALL
, 15)
48 self
.SetAutoLayout(True)
52 def OnButton(self
, evt
):
54 dlg
= wx
.MessageDialog(self
,
55 'The GLCanvas class has not been included with this build of wxPython!',
56 'Sorry', wx
.OK | wx
.ICON_WARNING
)
61 dlg
= wx
.MessageDialog(self
,
62 'The OpenGL package was not found. You can get it at\n'
63 'http://PyOpenGL.sourceforge.net/',
64 'Sorry', wx
.OK | wx
.ICON_WARNING
)
69 canvasClassName
= buttonDefs
[evt
.GetId()][0]
70 canvasClass
= eval(canvasClassName
)
71 frame
= wx
.Frame(None, -1, canvasClassName
, size
=(400,400))
72 canvas
= canvasClass(frame
)
76 class MyCanvasBase(glcanvas
.GLCanvas
):
77 def __init__(self
, parent
):
78 glcanvas
.GLCanvas
.__init
__(self
, parent
, -1)
80 # initial mouse position
81 self
.lastx
= self
.x
= 30
82 self
.lasty
= self
.y
= 30
83 self
.Bind(wx
.EVT_ERASE_BACKGROUND
, self
.OnEraseBackground
)
84 self
.Bind(wx
.EVT_SIZE
, self
.OnSize
)
85 self
.Bind(wx
.EVT_PAINT
, self
.OnPaint
)
86 self
.Bind(wx
.EVT_LEFT_DOWN
, self
.OnMouseDown
)
87 self
.Bind(wx
.EVT_LEFT_UP
, self
.OnMouseUp
)
88 self
.Bind(wx
.EVT_MOTION
, self
.OnMouseMotion
)
91 def OnEraseBackground(self
, event
):
92 pass # Do nothing, to avoid flashing on MSW.
95 def OnSize(self
, event
):
96 size
= self
.GetClientSize()
99 glViewport(0, 0, size
.width
, size
.height
)
103 def OnPaint(self
, event
):
104 dc
= wx
.PaintDC(self
)
112 def OnMouseDown(self
, evt
):
116 def OnMouseUp(self
, evt
):
120 def OnMouseMotion(self
, evt
):
121 if evt
.Dragging() and evt
.LeftIsDown():
122 self
.x
, self
.y
= self
.lastx
, self
.lasty
123 self
.x
, self
.y
= evt
.GetPosition()
129 class CubeCanvas(MyCanvasBase
):
131 # set viewing projection
132 glMatrixMode(GL_PROJECTION
);
133 glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0);
136 glMatrixMode(GL_MODELVIEW
);
137 glTranslatef(0.0, 0.0, -2.0);
140 glRotatef(self
.y
, 1.0, 0.0, 0.0);
141 glRotatef(self
.x
, 0.0, 1.0, 0.0);
143 glEnable(GL_DEPTH_TEST
);
144 glEnable(GL_LIGHTING
);
149 # clear color and depth buffers
150 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
);
152 # draw six faces of a cube
154 glNormal3f( 0.0, 0.0, 1.0)
155 glVertex3f( 0.5, 0.5, 0.5)
156 glVertex3f(-0.5, 0.5, 0.5)
157 glVertex3f(-0.5,-0.5, 0.5)
158 glVertex3f( 0.5,-0.5, 0.5)
160 glNormal3f( 0.0, 0.0,-1.0)
161 glVertex3f(-0.5,-0.5,-0.5)
162 glVertex3f(-0.5, 0.5,-0.5)
163 glVertex3f( 0.5, 0.5,-0.5)
164 glVertex3f( 0.5,-0.5,-0.5)
166 glNormal3f( 0.0, 1.0, 0.0)
167 glVertex3f( 0.5, 0.5, 0.5)
168 glVertex3f( 0.5, 0.5,-0.5)
169 glVertex3f(-0.5, 0.5,-0.5)
170 glVertex3f(-0.5, 0.5, 0.5)
172 glNormal3f( 0.0,-1.0, 0.0)
173 glVertex3f(-0.5,-0.5,-0.5)
174 glVertex3f( 0.5,-0.5,-0.5)
175 glVertex3f( 0.5,-0.5, 0.5)
176 glVertex3f(-0.5,-0.5, 0.5)
178 glNormal3f( 1.0, 0.0, 0.0)
179 glVertex3f( 0.5, 0.5, 0.5)
180 glVertex3f( 0.5,-0.5, 0.5)
181 glVertex3f( 0.5,-0.5,-0.5)
182 glVertex3f( 0.5, 0.5,-0.5)
184 glNormal3f(-1.0, 0.0, 0.0)
185 glVertex3f(-0.5,-0.5,-0.5)
186 glVertex3f(-0.5,-0.5, 0.5)
187 glVertex3f(-0.5, 0.5, 0.5)
188 glVertex3f(-0.5, 0.5,-0.5)
191 glRotatef((self
.lasty
- self
.y
)/100., 1.0, 0.0, 0.0);
192 glRotatef((self
.lastx
- self
.x
)/100., 0.0, 1.0, 0.0);
200 class ConeCanvas(MyCanvasBase
):
202 glMatrixMode(GL_PROJECTION
);
203 # camera frustrum setup
204 glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0);
205 glMaterial(GL_FRONT
, GL_AMBIENT
, [0.2, 0.2, 0.2, 1.0])
206 glMaterial(GL_FRONT
, GL_DIFFUSE
, [0.8, 0.8, 0.8, 1.0])
207 glMaterial(GL_FRONT
, GL_SPECULAR
, [1.0, 0.0, 1.0, 1.0])
208 glMaterial(GL_FRONT
, GL_SHININESS
, 50.0)
209 glLight(GL_LIGHT0
, GL_AMBIENT
, [0.0, 1.0, 0.0, 1.0])
210 glLight(GL_LIGHT0
, GL_DIFFUSE
, [1.0, 1.0, 1.0, 1.0])
211 glLight(GL_LIGHT0
, GL_SPECULAR
, [1.0, 1.0, 1.0, 1.0])
212 glLight(GL_LIGHT0
, GL_POSITION
, [1.0, 1.0, 1.0, 0.0]);
213 glLightModel(GL_LIGHT_MODEL_AMBIENT
, [0.2, 0.2, 0.2, 1.0])
214 glEnable(GL_LIGHTING
)
217 glEnable(GL_DEPTH_TEST
)
218 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
)
220 glMatrixMode(GL_MODELVIEW
);
224 # clear color and depth buffers
225 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
);
226 # use a fresh transformation matrix
229 glTranslate(0.0, 0.0, -2.0);
230 glRotate(30.0, 1.0, 0.0, 0.0);
231 glRotate(30.0, 0.0, 1.0, 0.0);
233 glTranslate(0, -1, 0)
234 glRotate(250, 1, 0, 0)
235 glutSolidCone(0.5, 1, 30, 5)
237 glRotatef((self
.lasty
- self
.y
)/100., 0.0, 0.0, 1.0);
238 glRotatef(0.0, (self
.lastx
- self
.x
)/100., 1.0, 0.0);
239 # push into visible buffer
245 #----------------------------------------------------------------------
248 def runTest(frame
, nb
, log
):
249 win
= ButtonPanel(nb
, log
)
262 if __name__
== '__main__':
265 run
.main(['', os
.path
.basename(sys
.argv
[0])] + sys
.argv
[1:])
270 #----------------------------------------------------------------------