Advertisement
here2share

# GL_3D_cube.py ZZZ

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