Package pyforms :: Package gui :: Package Controls :: Module ControlOpenGL |
[hide private]
[frames] | no frames]

# Source Code for Module pyforms.gui.Controls.ControlOpenGL

      1  from pyforms.gui.Controls.ControlBase import ControlBase
      2
      3  try:
      4          from OpenGL.GL  import *
      5          from OpenGL.GLU import *
      6          from PyQt4.QtOpenGL import QGLWidget
      7  except:
      8          print ("No OpenGL library available")
      9
     10
     11
     12 -class OpenglGLWidget(QGLWidget):
     13
     14 -        def __init__(self, parent=None):
     15                  QGLWidget.__init__(self, parent)
     16
     17                  self._zoom = 1.0
     18                  self._scene = None
     19                  self._rotation = [0, 0, 0]
     20
     21                  self._mouseLeftDown     = False
     22                  self._mouseRightDown    = False
     23
     24                  self._mouseGLPosition       = [0,0,0]
     25                  self._lastMouseGLPosition   = [0,0,0]
     26
     27                  self._mousePosition         = [0,0]  #Current mouse position
     28                  self._lastMousePosition     = [0,0]  #Last mouse position
     29
     30                  self._mouseStartDragPoint   = None
     31
     32                  self.setMinimumHeight(100)
     33                  self.setMinimumWidth(100)
     34                  self.setMouseTracking(True)
     35                  self.setAcceptDrops(True)
     36
     37
     38 -        def initializeGL(self):
     39                  glClearDepth(1.0)
     40                  glClearColor(0, 0, 0, 1.0)
     41                  #glDisable(GL_CULL_FACE)
     42                  #glEnable(GL_DEPTH_TEST)
     43                  #glDisable(GL_DEPTH_TEST)
     44
     45
     46                  #glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
     47                  #glBlendFunc(GL_ONE_MINUS_DST_ALPHA,GL_DST_ALPHA)
     48                  glBlendFunc(GL_SRC_ALPHA,GL_ONE)
     49                  glEnable( GL_BLEND )
     50
     51 -        def resizeGL(self, width, height):
     52                  glViewport(0, 0, width, height)
     53                  glMatrixMode(GL_PROJECTION)
     54                  glLoadIdentity()
     55                  gluPerspective(65.0, float(width)/float(height), 0.01, 800.0)
     56
     57
     58 -        def paintGL(self):
     59                  glClearColor(0.0, 0.0, 0.0, 1.0)
     60                  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
     61                  glMatrixMode(GL_MODELVIEW)
     62                  glLoadIdentity()
     63                  glBlendFunc(GL_SRC_ALPHA,GL_ONE)
     64                  glEnable( GL_BLEND )
     65
     66                  glScalef(1,-1,-1)
     67
     68                  glTranslatef( 0, 0, self._zoom)
     69                  glRotatef( self._rotation[0], 1,0,0 )
     70                  glRotatef( self._rotation[1], 0,1,0 )
     71                  glRotatef( self._rotation[2], 0,0,1 )
     72
     73
     74
     75                  if self._scene!=None: self._scene.DrawGLScene()
     76
     77
     78                  if self.mouseDown:
     79                          #Get mouse position
     80                          modelview, projection = glGetDoublev( GL_MODELVIEW_MATRIX ), glGetDoublev( GL_PROJECTION_MATRIX )
     81                          viewport = glGetIntegerv( GL_VIEWPORT )
     82                          winX, winY = float(self._mousePosition[0]), float(viewport[3] - self._mousePosition[1])
     83                          winZ = glReadPixels(winX, winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT)
     84                          self._mouseGLPosition = gluUnProject( winX, winY, winZ[0][0], modelview, projection, viewport)
     85
     86 -        def __updateMouse(self, event, pressed = None):
     87                  self._mousePosition[0] = event.x()
     88                  self._mousePosition[1] = event.y()
     89
     90                  if pressed!=None:
     91                          if event.button()==2: self._mouseRightDown = pressed
     92                          if event.button()==1: self._mouseLeftDown  = pressed
     93
     94 -        def wheelEvent(self, event):
     95                  if event.delta()<0: self._zoom += 1
     96                  else: self._zoom -= 1
     97
     98                  self.repaint()
     99
    100 -        def mouseReleaseEvent(self, event):
    101                  self.__updateMouse(event)
    102
    103                  if self._mouseRightDown:
    104                          self._lastMouseGLPosition = self._mouseGLPosition
    105                          self._mouseRightDown = False
    106
    107                  if self._mouseLeftDown:
    108                          self.onEndDrag( self._mouseStartDragPoint, self._mouseGLPosition )
    109                          self._mouseLeftDown = False
    110
    111
    112
    113 -        def mousePressEvent(self, event):
    114                  QGLWidget.mousePressEvent(self, event)
    115                  self.__updateMouse(event, pressed = True)
    116                  self.repaint()
    117
    118                  if self._mouseLeftDown:  self._mouseStartDragPoint = self._mouseGLPosition
    119                  if self._mouseRightDown: self._lastMouseGLPosition = self._mouseGLPosition
    120
    121                  self.onPress( event.button(), self._mousePosition, self._mouseGLPosition )
    122
    123 -        def mouseMoveEvent(self, event):
    124                  QGLWidget.mouseMoveEvent(self, event)
    125                  self.__updateMouse(event)
    126                  self.repaint()
    127
    128                  self.onMove( (self._mouseGLPosition[0], self._mouseGLPosition[1] ) )
    129
    130                  if self.mouseDown:
    131                          if self._mouseLeftDown:
    132                                  #p = self._mouseGLPosition[0] - self._x, self._mouseGLPosition[1] + self._y
    133                                  #p = self._mouseGLPosition[0], self._mouseGLPosition[1]
    134                                  self.onDrag(self._lastMousePosition, self._mousePosition)
    135
    136                  self._lastMousePosition = list(self._mousePosition)
    137
    138 -        def onMove(self, point):
    139                  pass
    140
    141 -        def onPress(self, button, point, glpoint=None):
    142                  pass
    143
    144
    145 -        def onDrag(self, startPoint, endPoint, startGLPoint=None, endGLPoint=None):
    146                  movX, movY = endPoint[0]-startPoint[0], endPoint[1]-startPoint[1]
    147                  self._rotation[2] -= float(movX)*0.15
    148                  self._rotation[0] += float(movY)*0.15
    149
    150 -        def onEndDrag(self, startPoint, endPoint, startGLPoint=None, endGLPoint=None):
    151                  pass
    152
    153          ##############################################################################
    154          ###### Properties ############################################################
    155          ##############################################################################
    156
    157          @property
    158 -        def scene(self): return self._scene
    159          @scene.setter
    160 -        def scene(self, value): self._scene = value
    161
    162          @property
    163 -        def mouseDown(self): return self._mouseLeftDown or self._mouseRightDown
    164
    165
    166 -        def resetZoomAndRotation(self): self._zoom, self._rotation = 0.0, [0,0,0]
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182 -class ControlOpenGL(ControlBase):
    183
    184
    185 -        def initForm(self): self._form = OpenglGLWidget()
    186
    187          @property
    188 -        def value(self): return self._form.scene
    189
    190          @value.setter
    191 -        def value(self, value):  self._form.scene = value; self._form.repaint()
    192
    193 -        def repaint(self): self._form.repaint()
    194
    195
    196 -        def resetZoomAndRotation(self): self._form.resetZoomAndRotation()
    197
    198
    199          @property
    200 -        def width(self): return self._form.width()
    201          @property
    202 -        def height(self): return self._form.height()
    203