Statistics
| Revision:

root / PyOpenGL-Demo / proesch / interactionMatrix / interactionMatrix.py @ 1

History | View | Annotate | Download (1.8 kB)

1
# helper class for interactive object motion 
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

    
10
try:
11
  from OpenGL.GLUT import *
12
  from OpenGL.GL import *
13
  from OpenGL.GLU import *
14
except:
15
  print ''' Error: PyOpenGL not installed properly !!'''
16
  sys.exit(  )
17

    
18
class InteractionMatrix ( object ):
19
        """Class holding a matrix representing a rigid transformation.
20

21
        The current OpenGL is read into an internal variable and
22
        updated using rotations and translations given by
23
        user interaction."""
24

    
25
        def __init__( self ):
26
                self.__currentMatrix = None
27
                self.reset( )
28

    
29
        def reset( self ):
30
                """Initialise internal matrix with identity"""
31
                glPushMatrix( )
32
                glLoadIdentity( )
33
                self.__currentMatrix = glGetFloatv( GL_MODELVIEW_MATRIX )
34
                glPopMatrix( )
35

    
36
        def addTranslation( self, tx, ty, tz ):
37
                """Concatenate the internal matrix with a translation matrix"""
38
                glPushMatrix( )
39
                glLoadIdentity( )
40
                glTranslatef(tx, ty, tz)
41
                glMultMatrixf( self.__currentMatrix )
42
                self.__currentMatrix = glGetFloatv( GL_MODELVIEW_MATRIX )
43
                glPopMatrix( )
44

    
45
        def addRotation( self, ang, rx, ry, rz ):
46
                """Concatenate the internal matrix with a translation matrix"""
47
                glPushMatrix( )
48
                glLoadIdentity( )
49
                glRotatef(ang, rx, ry, rz)
50
                glMultMatrixf( self.__currentMatrix )
51
                self.__currentMatrix = glGetFloatv( GL_MODELVIEW_MATRIX )
52
                glPopMatrix( )
53

    
54
        def getCurrentMatrix( self ):
55
                return self.__currentMatrix
56
        
57
if __name__ == '__main__' :
58
        glutInit( sys.argv )
59
        glutCreateWindow( sys.argv[0] )
60
        m=InteractionMatrix()
61
        print m.getCurrentMatrix( )
62
        m.addTranslation(1,2,3)
63
        print m.getCurrentMatrix( )
64
        m.addRotation(30,0,0,1)
65
        print m.getCurrentMatrix( )
66
        m.addTranslation(1,2,3)
67
        print m.getCurrentMatrix( )