root / PyOpenGL-Demo / proesch / simple / simpleInteraction.py @ 2
Historique | Voir | Annoter | Télécharger (4,2 ko)
| 1 |
#!/usr/bin/python2.4
|
|---|---|
| 2 |
# Copyright (C) 2007 "Peter Roesch" <Peter.Roesch@fh-augsburg.de>
|
| 3 |
#
|
| 4 |
# This code is licensed under the PyOpenGL License.
|
| 5 |
# Details are given in the file license.txt included in this distribution.
|
| 6 |
|
| 7 |
import sys |
| 8 |
try:
|
| 9 |
from OpenGL.GLUT import * |
| 10 |
from OpenGL.GL import * |
| 11 |
from OpenGL.GLU import * |
| 12 |
except:
|
| 13 |
print ''' Error PyOpenGL not installed properly!!''' |
| 14 |
sys.exit( ) |
| 15 |
|
| 16 |
def setList( l, v ): |
| 17 |
"""Set all elements of a list to the same bvalue"""
|
| 18 |
for i in range( len( l ) ): |
| 19 |
l[i] = v |
| 20 |
|
| 21 |
class renderParam( object ): |
| 22 |
"""Class holding current parameters for rendering.
|
| 23 |
|
| 24 |
Parameters are modified by user interaction"""
|
| 25 |
def __init__( self ): |
| 26 |
self.initialColor = [1, 1, 1] |
| 27 |
self.drawColor = self.initialColor |
| 28 |
self.tVec = [0, 0, 0] |
| 29 |
self.mouseButton = None |
| 30 |
|
| 31 |
def reset( self ): |
| 32 |
self.drawColor = self.initialColor |
| 33 |
setList( self.tVec, 0 ) |
| 34 |
self.mouseButton = None |
| 35 |
|
| 36 |
rP = renderParam( ) |
| 37 |
|
| 38 |
oldMousePos = [ 0, 0 ] |
| 39 |
def mouseButton( button, mode, x, y ): |
| 40 |
"""Callback function (mouse button pressed or released).
|
| 41 |
|
| 42 |
The current and old mouse positions are stored in
|
| 43 |
a global renderParam and a global list respectively"""
|
| 44 |
|
| 45 |
global rP, oldMousePos
|
| 46 |
if mode == GLUT_DOWN:
|
| 47 |
rP.mouseButton = button |
| 48 |
else:
|
| 49 |
rP.mouseButton = None
|
| 50 |
oldMousePos[0], oldMousePos[1] = x, y |
| 51 |
glutPostRedisplay( ) |
| 52 |
|
| 53 |
def mouseMotion( x, y ): |
| 54 |
"""Callback function (mouse moved while button is pressed).
|
| 55 |
|
| 56 |
The current and old mouse positions are stored in
|
| 57 |
a global renderParam and a global list respectively.
|
| 58 |
The global translation vector is updated according to
|
| 59 |
the movement of the mouse pointer."""
|
| 60 |
|
| 61 |
global rP, oldMousePos
|
| 62 |
deltaX = x - oldMousePos[ 0 ]
|
| 63 |
deltaY = y - oldMousePos[ 1 ]
|
| 64 |
if rP.mouseButton == GLUT_LEFT_BUTTON:
|
| 65 |
factor = 0.01
|
| 66 |
rP.tVec[0] += deltaX * factor
|
| 67 |
rP.tVec[1] -= deltaY * factor
|
| 68 |
oldMousePos[0], oldMousePos[1] = x, y |
| 69 |
glutPostRedisplay( ) |
| 70 |
|
| 71 |
def keyPressed( key, x, y ): |
| 72 |
"""Callback function (ordinary key pressed)."""
|
| 73 |
|
| 74 |
global rP
|
| 75 |
if key in ('r', 'R'): |
| 76 |
rP.reset( ) |
| 77 |
elif key in ('q', 'Q'): |
| 78 |
sys.exit( ) |
| 79 |
glutPostRedisplay( ) |
| 80 |
|
| 81 |
def specialKeyPressed( key, x, y): |
| 82 |
"""Callback function (special key pressed).
|
| 83 |
|
| 84 |
Arrow keys are used to shift the object displayed."""
|
| 85 |
stepSize = 0.1
|
| 86 |
global rP
|
| 87 |
if key == GLUT_KEY_LEFT:
|
| 88 |
rP.tVec[0] -= stepSize
|
| 89 |
elif key == GLUT_KEY_RIGHT:
|
| 90 |
rP.tVec[0] += stepSize
|
| 91 |
elif key == GLUT_KEY_DOWN:
|
| 92 |
rP.tVec[1] -= stepSize
|
| 93 |
elif key == GLUT_KEY_UP:
|
| 94 |
rP.tVec[1] += stepSize
|
| 95 |
glutPostRedisplay( ) |
| 96 |
|
| 97 |
MENU_RED, MENU_GREEN, MENU_BLUE, MENU_QUIT = 0, 1, 2, 3 |
| 98 |
colors = [ [1, 0, 0], [0, 1, 0], [0, 0, 1] ] |
| 99 |
def handleMenu( selection ): |
| 100 |
"""Callback function (menu).
|
| 101 |
|
| 102 |
Glut menus are not supported in PyOpenGL-3.0.0a6"""
|
| 103 |
|
| 104 |
global colors, rP
|
| 105 |
if selection == MENU_QUIT:
|
| 106 |
sys.exit() |
| 107 |
elif MENU_RED <= selection <= MENU_BLUE:
|
| 108 |
rP.drawColor = colors[ selection ] |
| 109 |
else:
|
| 110 |
print 'Warning: illegel Menu entry' |
| 111 |
glutPostRedisplay( ) |
| 112 |
|
| 113 |
def initMenus( ): |
| 114 |
global handleMenu
|
| 115 |
colorMenu = glutCreateMenu( handleMenu ) |
| 116 |
glutAddMenuEntry( "red", MENU_RED )
|
| 117 |
glutAddMenuEntry( "green", MENU_GREEN )
|
| 118 |
glutAddMenuEntry( "blue", MENU_BLUE )
|
| 119 |
glutCreateMenu( handleMenu ) |
| 120 |
glutAddSubMenu( "color", colorMenu)
|
| 121 |
glutAddMenuEntry( "quit", MENU_QUIT )
|
| 122 |
glutAttachMenu( GLUT_RIGHT_BUTTON ) |
| 123 |
|
| 124 |
def display( ): |
| 125 |
"""OpenGL display function."""
|
| 126 |
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) |
| 127 |
glMatrixMode( GL_MODELVIEW ) |
| 128 |
glLoadIdentity( ) |
| 129 |
glColor3f( rP.drawColor[0], rP.drawColor[1], rP.drawColor[2] ) |
| 130 |
glTranslatef( rP.tVec[0], rP.tVec[1], rP.tVec[2] ) |
| 131 |
glBegin( GL_QUADS ) |
| 132 |
glVertex3f( -0.25, 0.25, 0 ) |
| 133 |
glVertex3f( -0.25, -0.25, 0 ) |
| 134 |
glVertex3f( 0.25, -0.25, 0 ) |
| 135 |
glVertex3f( 0.25, 0.25, 0 ) |
| 136 |
glEnd( ) |
| 137 |
# m = glGetFloatv( GL_MODELVIEW_MATRIX ); print m; print
|
| 138 |
glutSwapBuffers( ) |
| 139 |
|
| 140 |
def registerCallbacks( ): |
| 141 |
"""Initialise glut settings concerning functions"""
|
| 142 |
glutMouseFunc( mouseButton ) |
| 143 |
glutMotionFunc( mouseMotion ) |
| 144 |
glutKeyboardFunc( keyPressed ) |
| 145 |
glutSpecialFunc( specialKeyPressed ) |
| 146 |
glutDisplayFunc( display ) |
| 147 |
|
| 148 |
def init( ): |
| 149 |
# Glut menus are not supported in PyOpenGL-3.0.0a6
|
| 150 |
# initMenus( )
|
| 151 |
registerCallbacks( ) |
| 152 |
glClearColor ( 0, 0, 0, 0 ) |
| 153 |
glShadeModel( GL_SMOOTH ) |
| 154 |
|
| 155 |
glutInit( sys.argv ) |
| 156 |
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB ) |
| 157 |
glutInitWindowSize( 250, 250 ) |
| 158 |
glutInitWindowPosition( 100, 100 ) |
| 159 |
glutCreateWindow( sys.argv[0] )
|
| 160 |
init( ) |
| 161 |
glutMainLoop( ) |