Advertisement
Guest User

example wx

a guest
Jul 30th, 2015
444
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.52 KB | None | 0 0
  1. import wx
  2. import sys
  3.  
  4. # This working example of the use of OpenGL in the wxPython context
  5. # was assembled in August 2012 from the GLCanvas.py file found in
  6. # the wxPython docs-demo package, plus components of that package's
  7. # run-time environment.
  8.  
  9. # Note that dragging the mouse rotates the view of the 3D cube or cone.
  10.  
  11. try:
  12. from wx import glcanvas
  13. haveGLCanvas = True
  14. except ImportError:
  15. haveGLCanvas = False
  16.  
  17. try:
  18. # The Python OpenGL package can be found at
  19. # http://PyOpenGL.sourceforge.net/
  20. from OpenGL.GL import *
  21. from OpenGL.GLUT import *
  22. haveOpenGL = True
  23. except ImportError:
  24. haveOpenGL = False
  25.  
  26. #----------------------------------------------------------------------
  27.  
  28. buttonDefs = {
  29. wx.NewId() : ('CubeCanvas', 'Cube'),
  30. wx.NewId() : ('ConeCanvas', 'Cone'),
  31. }
  32.  
  33. class ButtonPanel(wx.Panel):
  34. def __init__(self, parent):
  35. wx.Panel.__init__(self, parent, -1)
  36.  
  37. box = wx.BoxSizer(wx.VERTICAL)
  38. box.Add((20, 30))
  39. keys = buttonDefs.keys()
  40. keys.sort()
  41. for k in keys:
  42. text = buttonDefs[k][1]
  43. btn = wx.Button(self, k, text)
  44. box.Add(btn, 0, wx.ALIGN_CENTER|wx.ALL, 15)
  45. self.Bind(wx.EVT_BUTTON, self.OnButton, btn)
  46.  
  47. # With this enabled, you see how you can put a GLCanvas on the wx.Panel
  48. if 1:
  49. c = CubeCanvas(self)
  50. c.SetMinSize((200, 200))
  51. box.Add(c, 0, wx.ALIGN_CENTER|wx.ALL, 15)
  52.  
  53. self.SetAutoLayout(True)
  54. self.SetSizer(box)
  55.  
  56. def OnButton(self, evt):
  57. if not haveGLCanvas:
  58. dlg = wx.MessageDialog(self,
  59. 'The GLCanvas class has not been included with this build of wxPython!',
  60. 'Sorry', wx.OK | wx.ICON_WARNING)
  61. dlg.ShowModal()
  62. dlg.Destroy()
  63.  
  64. elif not haveOpenGL:
  65. dlg = wx.MessageDialog(self,
  66. 'The OpenGL package was not found. You can get it at\n'
  67. 'http://PyOpenGL.sourceforge.net/',
  68. 'Sorry', wx.OK | wx.ICON_WARNING)
  69. dlg.ShowModal()
  70. dlg.Destroy()
  71.  
  72. else:
  73. canvasClassName = buttonDefs[evt.GetId()][0]
  74. canvasClass = eval(canvasClassName)
  75. cx = 0
  76. if canvasClassName == 'ConeCanvas': cx = 400
  77. frame = wx.Frame(None, -1, canvasClassName, size=(400,400), pos=(cx,400))
  78. canvasClass(frame) # CubeCanvas(frame) or ConeCanvas(frame); frame passed to MyCanvasBase
  79. frame.Show(True)
  80.  
  81. class MyCanvasBase(glcanvas.GLCanvas):
  82. def __init__(self, parent):
  83. glcanvas.GLCanvas.__init__(self, parent, -1)
  84. self.init = False
  85. self.context = glcanvas.GLContext(self)
  86.  
  87. # initial mouse position
  88. self.lastx = self.x = 30
  89. self.lasty = self.y = 30
  90. self.size = None
  91. self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
  92. self.Bind(wx.EVT_SIZE, self.OnSize)
  93. self.Bind(wx.EVT_PAINT, self.OnPaint)
  94. self.Bind(wx.EVT_LEFT_DOWN, self.OnMouseDown)
  95. self.Bind(wx.EVT_LEFT_UP, self.OnMouseUp)
  96. self.Bind(wx.EVT_MOTION, self.OnMouseMotion)
  97.  
  98. def OnEraseBackground(self, event):
  99. pass # Do nothing, to avoid flashing on MSW.
  100.  
  101. def OnSize(self, event):
  102. wx.CallAfter(self.DoSetViewport)
  103. event.Skip()
  104.  
  105. def DoSetViewport(self):
  106. size = self.size = self.GetClientSize()
  107. self.SetCurrent(self.context)
  108. glViewport(0, 0, size.width, size.height)
  109.  
  110. def OnPaint(self, event):
  111. dc = wx.PaintDC(self)
  112. self.SetCurrent(self.context)
  113. if not self.init:
  114. self.InitGL()
  115. self.init = True
  116. self.OnDraw()
  117.  
  118. def OnMouseDown(self, evt):
  119. self.CaptureMouse()
  120. print wx.GetMousePosition,self.ScreenToClient(wx.GetMousePosition())
  121. self.x, self.y = self.lastx, self.lasty = evt.GetPosition()
  122.  
  123.  
  124. def OnMouseUp(self, evt):
  125. self.ReleaseMouse()
  126.  
  127. def OnMouseMotion(self, evt):
  128. if evt.Dragging() and evt.LeftIsDown():
  129. self.lastx, self.lasty = self.x, self.y
  130. self.x, self.y = evt.GetPosition()
  131. self.Refresh(False)
  132.  
  133. class CubeCanvas(MyCanvasBase):
  134. def InitGL(self):
  135. # set viewing projection
  136. glMatrixMode(GL_PROJECTION)
  137. glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0)
  138.  
  139. # position viewer
  140. glMatrixMode(GL_MODELVIEW)
  141. glTranslatef(0.0, 0.0, -2.0)
  142.  
  143. # position object
  144. glRotatef(self.y, 1.0, 0.0, 0.0)
  145. glRotatef(self.x, 0.0, 1.0, 0.0)
  146.  
  147. glEnable(GL_DEPTH_TEST)
  148. glEnable(GL_LIGHTING)
  149. glEnable(GL_LIGHT0)
  150.  
  151. def OnDraw(self):
  152. # clear color and depth buffers
  153. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
  154.  
  155. # draw six faces of a cube
  156. glBegin(GL_QUADS)
  157. glNormal3f( 0.0, 0.0, 1.0)
  158. glVertex3f( 0.5, 0.5, 0.5)
  159. glVertex3f(-0.5, 0.5, 0.5)
  160. glVertex3f(-0.5,-0.5, 0.5)
  161. glVertex3f( 0.5,-0.5, 0.5)
  162.  
  163. glNormal3f( 0.0, 0.0,-1.0)
  164. glVertex3f(-0.5,-0.5,-0.5)
  165. glVertex3f(-0.5, 0.5,-0.5)
  166. glVertex3f( 0.5, 0.5,-0.5)
  167. glVertex3f( 0.5,-0.5,-0.5)
  168.  
  169. glNormal3f( 0.0, 1.0, 0.0)
  170. glVertex3f( 0.5, 0.5, 0.5)
  171. glVertex3f( 0.5, 0.5,-0.5)
  172. glVertex3f(-0.5, 0.5,-0.5)
  173. glVertex3f(-0.5, 0.5, 0.5)
  174.  
  175. glNormal3f( 0.0,-1.0, 0.0)
  176. glVertex3f(-0.5,-0.5,-0.5)
  177. glVertex3f( 0.5,-0.5,-0.5)
  178. glVertex3f( 0.5,-0.5, 0.5)
  179. glVertex3f(-0.5,-0.5, 0.5)
  180.  
  181. glNormal3f( 1.0, 0.0, 0.0)
  182. glVertex3f( 0.5, 0.5, 0.5)
  183. glVertex3f( 0.5,-0.5, 0.5)
  184. glVertex3f( 0.5,-0.5,-0.5)
  185. glVertex3f( 0.5, 0.5,-0.5)
  186.  
  187. glNormal3f(-1.0, 0.0, 0.0)
  188. glVertex3f(-0.5,-0.5,-0.5)
  189. glVertex3f(-0.5,-0.5, 0.5)
  190. glVertex3f(-0.5, 0.5, 0.5)
  191. glVertex3f(-0.5, 0.5,-0.5)
  192. glEnd()
  193.  
  194. if self.size is None:
  195. self.size = self.GetClientSize()
  196. w, h = self.size
  197. w = max(w, 1.0)
  198. h = max(h, 1.0)
  199. xScale = 180.0 / w
  200. yScale = 180.0 / h
  201. glRotatef((self.y - self.lasty) * yScale, 1.0, 0.0, 0.0);
  202. glRotatef((self.x - self.lastx) * xScale, 0.0, 1.0, 0.0);
  203.  
  204. self.SwapBuffers()
  205.  
  206. class ConeCanvas(MyCanvasBase):
  207. def InitGL( self ):
  208. glMatrixMode(GL_PROJECTION)
  209. # camera frustrum setup
  210. glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0)
  211. glMaterial(GL_FRONT, GL_AMBIENT, [0.2, 0.2, 0.2, 1.0])
  212. glMaterial(GL_FRONT, GL_DIFFUSE, [0.8, 0.8, 0.8, 1.0])
  213. glMaterial(GL_FRONT, GL_SPECULAR, [1.0, 0.0, 1.0, 1.0])
  214. glMaterial(GL_FRONT, GL_SHININESS, 50.0)
  215. glLight(GL_LIGHT0, GL_AMBIENT, [0.0, 1.0, 0.0, 1.0])
  216. glLight(GL_LIGHT0, GL_DIFFUSE, [1.0, 1.0, 1.0, 1.0])
  217. glLight(GL_LIGHT0, GL_SPECULAR, [1.0, 1.0, 1.0, 1.0])
  218. glLight(GL_LIGHT0, GL_POSITION, [1.0, 1.0, 1.0, 0.0])
  219. glLightModelfv(GL_LIGHT_MODEL_AMBIENT, [0.2, 0.2, 0.2, 1.0])
  220. glEnable(GL_LIGHTING)
  221. glEnable(GL_LIGHT0)
  222. glDepthFunc(GL_LESS)
  223. glEnable(GL_DEPTH_TEST)
  224. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
  225. # position viewer
  226. glMatrixMode(GL_MODELVIEW)
  227. # position viewer
  228. glTranslatef(0.0, 0.0, -2.0);
  229. #
  230. glutInit(sys.argv)
  231.  
  232.  
  233. def OnDraw(self):
  234. # clear color and depth buffers
  235. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
  236. # use a fresh transformation matrix
  237. glPushMatrix()
  238. # position object
  239. #glTranslate(0.0, 0.0, -2.0)
  240. glRotate(30.0, 1.0, 0.0, 0.0)
  241. glRotate(30.0, 0.0, 1.0, 0.0)
  242.  
  243. glTranslate(0, -1, 0)
  244. glRotate(250, 1, 0, 0)
  245. glutSolidCone(0.5, 1, 30, 5)
  246. glPopMatrix()
  247. glRotatef((self.y - self.lasty), 0.0, 0.0, 1.0);
  248. glRotatef((self.x - self.lastx), 1.0, 0.0, 0.0);
  249. # push into visible buffer
  250. self.SwapBuffers()
  251.  
  252.  
  253. #----------------------------------------------------------------------
  254. class RunDemoApp(wx.App):
  255. def __init__(self):
  256. wx.App.__init__(self, redirect=False)
  257.  
  258. def OnInit(self):
  259. frame = wx.Frame(None, -1, "RunDemo: ", pos=(0,0),
  260. style=wx.DEFAULT_FRAME_STYLE, name="run a sample")
  261. #frame.CreateStatusBar()
  262.  
  263. menuBar = wx.MenuBar()
  264. menu = wx.Menu()
  265. item = menu.Append(wx.ID_EXIT, "E&xit\tCtrl-Q", "Exit demo")
  266. self.Bind(wx.EVT_MENU, self.OnExitApp, item)
  267. menuBar.Append(menu, "&File")
  268.  
  269. frame.SetMenuBar(menuBar)
  270. frame.Show(True)
  271. frame.Bind(wx.EVT_CLOSE, self.OnCloseFrame)
  272.  
  273. win = runTest(frame)
  274.  
  275. # set the frame to a good size for showing the two buttons
  276. frame.SetSize((800,400))
  277. win.SetFocus()
  278. self.window = win
  279. frect = frame.GetRect()
  280.  
  281. self.SetTopWindow(frame)
  282. self.frame = frame
  283. return True
  284.  
  285. def OnExitApp(self, evt):
  286. self.frame.Close(True)
  287.  
  288. def OnCloseFrame(self, evt):
  289. if hasattr(self, "window") and hasattr(self.window, "ShutdownDemo"):
  290. self.window.ShutdownDemo()
  291. evt.Skip()
  292.  
  293. def runTest(frame):
  294. win = ButtonPanel(frame)
  295. return win
  296.  
  297. app = RunDemoApp()
  298. app.MainLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement