root / PyOpenGLDemo / proesch / bezier / bezier.py @ 1
History  View  Annotate  Download (4 kB)
1 
#!/usr/bin/python


2 
# Surface deforming according to a damped oscillation consisting

3 
# of bezier patches.

4 
#

5 
# Copyright (C) 2007 "Peter Roesch" <Peter.Roesch@fhaugsburg.de>

6 
#

7 
# This code is licensed under the PyOpenGL License.

8 
# Details are given in the file license.txt included in this distribution.

9  
10 
import math 
11 
import sys 
12 
from time import sleep 
13  
14 
try:

15 
from OpenGL.GLUT import * 
16 
from OpenGL.GL import * 
17 
from OpenGL.GLU import * 
18 
except:

19 
print ''' Error: PyOpenGL is not installed properly !!''' 
20 
sys.exit( ) 
21  
22 
try:

23 
import psyco 
24 
psyco.full() 
25 
except ImportError: 
26 
print 'no psyco availiable' 
27  
28 
animationAngle = 0.0

29 
frameRate = 25

30 
animationTime = 0

31  
32 
def animationStep( ): 
33 
"""Update animated parameters.

34 

35 
This Function is made active by glutSetIdleFunc"""

36 
global animationAngle

37 
global frameRate

38 
global animationTime

39 
animationAngle += 0.3

40 
animationTime += 0.1

41 
while animationAngle > 360: 
42 
animationAngle = 360

43 
sleep( 1 / float( frameRate ) ) 
44 
glutPostRedisplay( ) 
45  
46 
sigma = 0.5;

47 
twoSigSq = 2. * sigma * sigma;

48  
49 
def dampedOscillation( u, v, t): 
50 
"""Calculation of a R2 > R1 function at position u,v at time t.

51 

52 
A tdependent cosine function is multiplied with a 2D gaussian.

53 
Both functions depend on the distance of (u,v) to the origin."""

54  
55 
distSq = u * u + v * v; 
56 
dist = math.pi * 4 * math.sqrt( distSq );

57 
global twoSigSq

58 
return 0.5 * math.exp(distSq / twoSigSq) * math.cos(dist  t); 
59  
60 
# number of patches in x and y direction

61 
divisions = 7

62 
nPts = divisions*3+1 
63 
xMin, xMax, yMin, yMax = 1.0, 1.0, 1.0, 1.0 
64 
xStep = (xMaxxMin)/(nPts1)

65 
yStep = (yMaxyMin)/(nPts1)

66 
divisionsGL = 20

67  
68 
# initialise a list representing a regular 2D grid of control points.

69 
controlPoints = [ \ 
70 
[ [ yMin+y*yStep, xMin+x*xStep, 0.0 ] for x in range ( nPts )]\ 
71 
for y in range( nPts ) ] 
72  
73 
# The actual surface is divided into patches of 4 by 4

74 
# control points

75 
patch = [ [ [ ] for x in range( 4 )] for y in range( 4 ) ] 
76  
77 
def updateControlPoints( t ): 
78 
"""Calculate function values for all 2D grid points."""

79  
80 
for row in controlPoints: 
81 
for coord in row: 
82 
coord[2] = dampedOscillation( coord[0], coord[1], t ) 
83  
84 
def display( ): 
85 
"""OpenGL display function."""

86 
glClear( GL_COLOR_BUFFER_BIT  GL_DEPTH_BUFFER_BIT ) 
87 
glMatrixMode( GL_PROJECTION ) 
88 
glLoadIdentity( ) 
89 
xSize, ySize = glutGet( GLUT_WINDOW_WIDTH ), glutGet( GLUT_WINDOW_HEIGHT ) 
90 
gluPerspective(60, float(xSize) / float(ySize), 0.1, 50) 
91 
glMatrixMode( GL_MODELVIEW ) 
92 
glLoadIdentity( ) 
93 
glTranslatef( 0, 0, 3 ) 
94 
glRotatef( 30, 1, .3, 0) 
95 
glRotatef( animationAngle, 0, 0, 1 ) 
96 
global animationTime

97 
updateControlPoints( animationTime ) 
98 
global controlPoints, patch

99 
global nPts, divisionsGL

100 
# plot all surface patches

101 
# loop over all patches

102 
for y in range( 0, nPts1, 3): 
103 
for x in range( 0, nPts1, 3 ): 
104 
# display the current patch

105 
for i in range( 4 ): 
106 
for k in range( 4 ): 
107 
patch[i][k]=controlPoints[y+i][x+k] 
108 
glMap2f( GL_MAP2_VERTEX_3, 0, 1, 0, 1, patch ) 
109 
glMapGrid2f( divisionsGL, 0.0, 1.0, divisionsGL, 0.0, 1.0 ) 
110 
glEvalMesh2( GL_FILL, 0, divisionsGL, 0, divisionsGL ) 
111 
glutSwapBuffers( ) 
112  
113 
def init( ): 
114 
"""Glut init function."""

115 
glClearColor ( 0, 0, 0, 0 ) 
116 
glEnable( GL_DEPTH_TEST ) 
117 
glShadeModel( GL_SMOOTH ) 
118 
glEnable( GL_LIGHTING ) 
119 
glEnable( GL_LIGHT0 ) 
120 
glLightModeli( GL_LIGHT_MODEL_TWO_SIDE, 0 )

121 
glLightfv( GL_LIGHT0, GL_POSITION, [2, 0, 10, 1] ) 
122 
lA = 0.8; glLightfv( GL_LIGHT0, GL_AMBIENT, [lA, lA, lA, 1] ) 
123 
lD = 1.0; glLightfv( GL_LIGHT0, GL_DIFFUSE, [lD, lD, lD, 1] ) 
124 
lS = 1.0; glLightfv( GL_LIGHT0, GL_SPECULAR, [lS, lS, lS, 1] ) 
125 
glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, [0.2, 0.2, 0.2, 1] ) 
126 
glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, [0.7, 0.7, 0.7, 1] ) 
127 
glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, [0.5, 0.5, 0.5, 1] ) 
128 
glMaterialf( GL_FRONT_AND_BACK, GL_SHININESS, 50 )

129 
glEnable( GL_MAP2_VERTEX_3 ) 
130 
glEnable( GL_AUTO_NORMAL ) 
131  
132 
glutInit( sys.argv ) 
133 
glutInitDisplayMode( GLUT_DOUBLE  GLUT_RGB  GLUT_DEPTH ) 
134 
glutInitWindowSize( 250, 250 ) 
135 
glutInitWindowPosition( 100, 100 ) 
136 
glutCreateWindow( sys.argv[0] )

137 
init( ) 
138 
glutDisplayFunc( display ) 
139 
glutIdleFunc( animationStep ) 
140 
glutMainLoop( ) 