2 from wxPython
.wx
import *
4 from wxPython
.glcanvas
import *
10 # The Python OpenGL package can be found at
11 # http://PyOpenGL.sourceforge.net/
12 from OpenGL
.GL
import *
13 from OpenGL
.GLUT
import *
18 #----------------------------------------------------------------------
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
)
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://PyOpenGL.sourceforge.net/',
32 'Sorry', wxOK | wxICON_INFORMATION
)
41 wxNewId() : ('CubeCanvas', 'Cube'),
42 wxNewId() : ('ConeCanvas', 'Cone'),
45 class ButtonPanel(wxPanel
):
46 def __init__(self
, parent
, log
):
47 wxPanel
.__init
__(self
, parent
, -1)
50 box
= wxBoxSizer(wxVERTICAL
)
52 keys
= buttonDefs
.keys()
55 text
= buttonDefs
[k
][1]
56 btn
= wxButton(self
, k
, text
)
57 box
.Add(btn
, 0, wxALIGN_CENTER|wxALL
, 15)
58 EVT_BUTTON(self
, k
, self
.OnButton
)
60 self
.SetAutoLayout(true
)
63 def OnButton(self
, evt
):
64 canvasClassName
= buttonDefs
[evt
.GetId()][0]
65 canvasClass
= eval(canvasClassName
)
66 frame
= wxFrame(None, -1, canvasClassName
, size
=(400,400))
67 canvas
= canvasClass(frame
)
72 def runTest(frame
, nb
, log
):
73 win
= ButtonPanel(nb
, log
)
78 class MyCanvasBase(wxGLCanvas
):
79 def __init__(self
, parent
):
80 wxGLCanvas
.__init
__(self
, parent
, -1)
82 # initial mouse position
83 self
.lastx
= self
.x
= 30
84 self
.lasty
= self
.y
= 30
85 EVT_ERASE_BACKGROUND(self
, self
.OnEraseBackground
)
86 EVT_SIZE(self
, self
.OnSize
)
87 EVT_PAINT(self
, self
.OnPaint
)
88 #EVT_LEFT_DOWN(self, self.OnMouseDown) # needs fixing...
89 #EVT_LEFT_UP(self, self.OnMouseUp)
90 #EVT_MOTION(self, self.OnMouseMotion)
92 def OnEraseBackground(self
, event
):
93 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
)
101 def OnPaint(self
, event
):
109 def OnMouseDown(self
, evt
):
112 def OnMouseUp(self
, evt
):
115 def OnMouseMotion(self
, evt
):
116 if evt
.Dragging() and evt
.LeftIsDown():
117 self
.x
, self
.y
= self
.lastx
, self
.lasty
118 self
.x
, self
.y
= evt
.GetPosition()
124 class CubeCanvas(MyCanvasBase
):
126 # set viewing projection
127 glMatrixMode(GL_PROJECTION
);
128 glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0);
131 glMatrixMode(GL_MODELVIEW
);
132 glTranslatef(0.0, 0.0, -2.0);
135 glRotatef(self
.y
, 1.0, 0.0, 0.0);
136 glRotatef(self
.x
, 0.0, 1.0, 0.0);
138 glEnable(GL_DEPTH_TEST
);
139 glEnable(GL_LIGHTING
);
144 # clear color and depth buffers
145 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
);
147 # draw six faces of a cube
149 glNormal3f( 0.0, 0.0, 1.0)
150 glVertex3f( 0.5, 0.5, 0.5)
151 glVertex3f(-0.5, 0.5, 0.5)
152 glVertex3f(-0.5,-0.5, 0.5)
153 glVertex3f( 0.5,-0.5, 0.5)
155 glNormal3f( 0.0, 0.0,-1.0)
156 glVertex3f(-0.5,-0.5,-0.5)
157 glVertex3f(-0.5, 0.5,-0.5)
158 glVertex3f( 0.5, 0.5,-0.5)
159 glVertex3f( 0.5,-0.5,-0.5)
161 glNormal3f( 0.0, 1.0, 0.0)
162 glVertex3f( 0.5, 0.5, 0.5)
163 glVertex3f( 0.5, 0.5,-0.5)
164 glVertex3f(-0.5, 0.5,-0.5)
165 glVertex3f(-0.5, 0.5, 0.5)
167 glNormal3f( 0.0,-1.0, 0.0)
168 glVertex3f(-0.5,-0.5,-0.5)
169 glVertex3f( 0.5,-0.5,-0.5)
170 glVertex3f( 0.5,-0.5, 0.5)
171 glVertex3f(-0.5,-0.5, 0.5)
173 glNormal3f( 1.0, 0.0, 0.0)
174 glVertex3f( 0.5, 0.5, 0.5)
175 glVertex3f( 0.5,-0.5, 0.5)
176 glVertex3f( 0.5,-0.5,-0.5)
177 glVertex3f( 0.5, 0.5,-0.5)
179 glNormal3f(-1.0, 0.0, 0.0)
180 glVertex3f(-0.5,-0.5,-0.5)
181 glVertex3f(-0.5,-0.5, 0.5)
182 glVertex3f(-0.5, 0.5, 0.5)
183 glVertex3f(-0.5, 0.5,-0.5)
186 glRotatef(self
.lasty
- self
.y
, 1.0, 0.0, 0.0);
187 glRotatef(self
.lastx
- self
.x
, 0.0, 1.0, 0.0);
195 class ConeCanvas(MyCanvasBase
):
197 glMatrixMode(GL_PROJECTION
);
198 # camera frustrum setup
199 glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0);
200 glMaterial(GL_FRONT
, GL_AMBIENT
, [0.2, 0.2, 0.2, 1.0])
201 glMaterial(GL_FRONT
, GL_DIFFUSE
, [0.8, 0.8, 0.8, 1.0])
202 glMaterial(GL_FRONT
, GL_SPECULAR
, [1.0, 0.0, 1.0, 1.0])
203 glMaterial(GL_FRONT
, GL_SHININESS
, 50.0)
204 glLight(GL_LIGHT0
, GL_AMBIENT
, [0.0, 1.0, 0.0, 1.0])
205 glLight(GL_LIGHT0
, GL_DIFFUSE
, [1.0, 1.0, 1.0, 1.0])
206 glLight(GL_LIGHT0
, GL_SPECULAR
, [1.0, 1.0, 1.0, 1.0])
207 glLight(GL_LIGHT0
, GL_POSITION
, [1.0, 1.0, 1.0, 0.0]);
208 glLightModel(GL_LIGHT_MODEL_AMBIENT
, [0.2, 0.2, 0.2, 1.0])
209 glEnable(GL_LIGHTING
)
212 glEnable(GL_DEPTH_TEST
)
213 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
)
217 # clear color and depth buffers
218 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
);
220 glMatrixMode(GL_MODELVIEW
);
221 # use a fresh transformation matrix
224 glTranslate(0.0, 0.0, -2.0);
225 glRotate(30.0, 1.0, 0.0, 0.0);
226 glRotate(30.0, 0.0, 1.0, 0.0);
228 glTranslate(0, -1, 0)
229 glRotate(250, 1, 0, 0)
230 glutSolidCone(1, 2, 50, 10)
232 # push into visible buffer
238 #----------------------------------------------------------------------
254 #----------------------------------------------------------------------
259 frame
= wxFrame(None, -1, "GL Demos", wxDefaultPosition
, wxSize(600,300))
260 #win = ConeCanvas(frame)
263 self
.SetTopWindow(frame
)
269 if __name__
== '__main__':