Guest User

Untitled

a guest
Feb 18th, 2011
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.25 KB | None | 0 0
  1. import  wx
  2. from wx import glcanvas
  3. from OpenGL.GL import *
  4. from OpenGL.GLUT import *
  5.  
  6. class MyCanvasBase(glcanvas.GLCanvas):
  7.    
  8.     def __init__(self, parent):
  9.         glcanvas.GLCanvas.__init__(self, parent, -1)
  10.         self.init = False
  11.         # initial mouse position
  12.         self.lastx = self.x = 30
  13.         self.lasty = self.y = 30
  14.         self.size = None
  15.         self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
  16.         self.Bind(wx.EVT_SIZE, self.OnSize)
  17.         self.Bind(wx.EVT_PAINT, self.OnPaint)
  18.         self.Bind(wx.EVT_LEFT_DOWN, self.OnMouseDown)
  19.         self.Bind(wx.EVT_LEFT_UP, self.OnMouseUp)
  20.         self.Bind(wx.EVT_MOTION, self.OnMouseMotion)
  21.  
  22.  
  23.     def OnEraseBackground(self, event):
  24.         pass # Do nothing, to avoid flashing on MSW.
  25.  
  26.  
  27.     def OnSize(self, event):
  28.         size = self.size = self.GetClientSize()
  29.         if self.GetContext():
  30.             self.SetCurrent()
  31.             glViewport(0, 0, size.width, size.height)
  32.         event.Skip()
  33.  
  34.  
  35.     def OnPaint(self, event):
  36.         dc = wx.PaintDC(self)
  37.         self.SetCurrent()
  38.         if not self.init:
  39.             self.InitGL()
  40.             self.init = True
  41.         self.OnDraw()
  42.  
  43.  
  44.     def OnMouseDown(self, evt):
  45.         self.CaptureMouse()
  46.         self.x, self.y = self.lastx, self.lasty = evt.GetPosition()
  47.  
  48.  
  49.     def OnMouseUp(self, evt):
  50.         self.ReleaseMouse()
  51.  
  52.  
  53.     def OnMouseMotion(self, evt):
  54.         if evt.Dragging() and evt.LeftIsDown():
  55.             self.lastx, self.lasty = self.x, self.y
  56.             self.x, self.y = evt.GetPosition()
  57.             self.Refresh(False)
  58.  
  59.  
  60. class CubeCanvas(MyCanvasBase):
  61.     def InitGL(self):
  62.         # set viewing projection
  63.         glMatrixMode(GL_PROJECTION)
  64.         glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0)
  65.  
  66.         # position viewer
  67.         glMatrixMode(GL_MODELVIEW)
  68.         glTranslatef(0.0, 0.0, -2.0)
  69.  
  70.         # position object
  71.         glRotatef(self.y, 1.0, 0.0, 0.0)
  72.         glRotatef(self.x, 0.0, 1.0, 0.0)
  73.  
  74.         glEnable(GL_DEPTH_TEST)
  75.         glEnable(GL_LIGHTING)
  76.         glEnable(GL_LIGHT0)
  77.  
  78.  
  79.     def OnDraw(self):
  80.         # clear color and depth buffers
  81.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
  82.  
  83.         # draw six faces of a cube
  84.         glBegin(GL_QUADS)
  85.         glNormal3f( 0.0, 0.0, 1.0)
  86.         glVertex3f( 0.5, 0.5, 0.5)
  87.         glVertex3f(-0.5, 0.5, 0.5)
  88.         glVertex3f(-0.5,-0.5, 0.5)
  89.         glVertex3f( 0.5,-0.5, 0.5)
  90.  
  91.         glNormal3f( 0.0, 0.0,-1.0)
  92.         glVertex3f(-0.5,-0.5,-0.5)
  93.         glVertex3f(-0.5, 0.5,-0.5)
  94.         glVertex3f( 0.5, 0.5,-0.5)
  95.         glVertex3f( 0.5,-0.5,-0.5)
  96.  
  97.         glNormal3f( 0.0, 1.0, 0.0)
  98.         glVertex3f( 0.5, 0.5, 0.5)
  99.         glVertex3f( 0.5, 0.5,-0.5)
  100.         glVertex3f(-0.5, 0.5,-0.5)
  101.         glVertex3f(-0.5, 0.5, 0.5)
  102.  
  103.         glNormal3f( 0.0,-1.0, 0.0)
  104.         glVertex3f(-0.5,-0.5,-0.5)
  105.         glVertex3f( 0.5,-0.5,-0.5)
  106.         glVertex3f( 0.5,-0.5, 0.5)
  107.         glVertex3f(-0.5,-0.5, 0.5)
  108.  
  109.         glNormal3f( 1.0, 0.0, 0.0)
  110.         glVertex3f( 0.5, 0.5, 0.5)
  111.         glVertex3f( 0.5,-0.5, 0.5)
  112.         glVertex3f( 0.5,-0.5,-0.5)
  113.         glVertex3f( 0.5, 0.5,-0.5)
  114.  
  115.         glNormal3f(-1.0, 0.0, 0.0)
  116.         glVertex3f(-0.5,-0.5,-0.5)
  117.         glVertex3f(-0.5,-0.5, 0.5)
  118.         glVertex3f(-0.5, 0.5, 0.5)
  119.         glVertex3f(-0.5, 0.5,-0.5)
  120.         glEnd()
  121.  
  122.         if self.size is None:
  123.             self.size = self.GetClientSize()
  124.         w, h = self.size
  125.         w = max(w, 1.0)
  126.         h = max(h, 1.0)
  127.         xScale = 180.0 / w
  128.         yScale = 180.0 / h
  129.         glRotatef((self.y - self.lasty) * yScale, 1.0, 0.0, 0.0);
  130.         glRotatef((self.x - self.lastx) * xScale, 0.0, 1.0, 0.0);
  131.  
  132.         self.SwapBuffers()
  133.  
  134.  
  135.  
  136. class MyFrame(wx.Frame):
  137.    
  138.     def __init__(self):
  139.         wx.Frame.__init__(self, None, -1, 'Test OpenGL', size=(300,320))
  140.         button_panel = wx.Panel(self, -1)
  141.         wx.Button(button_panel, -1, 'TEST')
  142.         gl_panel = CubeCanvas(self)
  143.         sizer = wx.BoxSizer(wx.VERTICAL)
  144.         sizer.Add(button_panel, 0, wx.ALL|wx.EXPAND, 5)
  145.         sizer.Add(gl_panel, 1, wx.ALL|wx.EXPAND, 5)
  146.         self.SetSizer(sizer)
  147.  
  148.  
  149.  
  150. app = wx.App()
  151. frame = MyFrame()
  152. app.SetTopWindow(frame)
  153. frame.Show(True)
  154. app.MainLoop()
Advertisement
Add Comment
Please, Sign In to add comment