root / PyOpenGL-Demo / proesch / interactionMatrix / mouseInteractor.py @ 2
Historique | Voir | Annoter | Télécharger (2,43 ko)
| 1 |
# helper class for mouse interaction
|
|---|---|
| 2 |
#
|
| 3 |
# Copyright (C) 2007 "Peter Roesch" <Peter.Roesch@fh-augsburg.de>
|
| 4 |
#
|
| 5 |
# This code is licensed under the PyOpenGL License.
|
| 6 |
# Details are given in the file license.txt included in this distribution.
|
| 7 |
|
| 8 |
import sys |
| 9 |
import math |
| 10 |
|
| 11 |
from interactionMatrix import InteractionMatrix |
| 12 |
|
| 13 |
try:
|
| 14 |
from OpenGL.GLUT import * |
| 15 |
from OpenGL.GL import * |
| 16 |
from OpenGL.GLU import * |
| 17 |
except:
|
| 18 |
print ''' Error: PyOpenGL not installed properly !!''' |
| 19 |
sys.exit( ) |
| 20 |
|
| 21 |
class MouseInteractor ( object ): |
| 22 |
"""Connection between mouse motion and transformation matrix"""
|
| 23 |
def __init__( self, translationScale=0.1, rotationScale=.2): |
| 24 |
self.scalingFactorRotation = rotationScale
|
| 25 |
self.scalingFactorTranslation = translationScale
|
| 26 |
self.rotationMatrix = InteractionMatrix( )
|
| 27 |
self.translationMatrix = InteractionMatrix( )
|
| 28 |
self.mouseButtonPressed = None |
| 29 |
self.oldMousePos = [ 0, 0 ] |
| 30 |
|
| 31 |
def mouseButton( self, button, mode, x, y ): |
| 32 |
"""Callback function for mouse button."""
|
| 33 |
if mode == GLUT_DOWN:
|
| 34 |
self.mouseButtonPressed = button
|
| 35 |
else:
|
| 36 |
self.mouseButtonPressed = None |
| 37 |
self.oldMousePos[0], self.oldMousePos[1] = x, y |
| 38 |
glutPostRedisplay( ) |
| 39 |
|
| 40 |
def mouseMotion( self, x, y ): |
| 41 |
"""Callback function for mouse motion.
|
| 42 |
|
| 43 |
Depending on the button pressed, the displacement of the
|
| 44 |
mouse pointer is either converted into a translation vector
|
| 45 |
or a rotation matrix."""
|
| 46 |
|
| 47 |
deltaX = x - self.oldMousePos[ 0 ] |
| 48 |
deltaY = y - self.oldMousePos[ 1 ] |
| 49 |
if self.mouseButtonPressed == GLUT_RIGHT_BUTTON: |
| 50 |
tX = deltaX * self.scalingFactorTranslation
|
| 51 |
tY = deltaY * self.scalingFactorTranslation
|
| 52 |
self.translationMatrix.addTranslation(tX, -tY, 0) |
| 53 |
elif self.mouseButtonPressed == GLUT_LEFT_BUTTON: |
| 54 |
rY = deltaX * self.scalingFactorRotation
|
| 55 |
self.rotationMatrix.addRotation(rY, 0, 1, 0) |
| 56 |
rX = deltaY * self.scalingFactorRotation
|
| 57 |
self.rotationMatrix.addRotation(rX, 1, 0, 0) |
| 58 |
else:
|
| 59 |
tZ = deltaY * self.scalingFactorTranslation
|
| 60 |
self.translationMatrix.addTranslation(0, 0, tZ) |
| 61 |
self.oldMousePos[0], self.oldMousePos[1] = x, y |
| 62 |
glutPostRedisplay( ) |
| 63 |
|
| 64 |
def applyTransformation( self ): |
| 65 |
"""Concatenation of the current translation and rotation
|
| 66 |
matrices with the current OpenGL transformation matrix"""
|
| 67 |
|
| 68 |
glMultMatrixf( self.translationMatrix.getCurrentMatrix() )
|
| 69 |
glMultMatrixf( self.rotationMatrix.getCurrentMatrix() )
|
| 70 |
|
| 71 |
def registerCallbacks( self ): |
| 72 |
"""Initialise glut callback functions."""
|
| 73 |
glutMouseFunc( self.mouseButton )
|
| 74 |
glutMotionFunc( self.mouseMotion )
|