Statistics
| Revision:

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

History | View | Annotate | Download (2.4 kB)

1
#!/usr/bin/python2.4
2
# Plot a curve with control points positioned on a circle
3
# using NURBS.
4
# 
5
# Copyright (C) 2007  "Peter Roesch" <Peter.Roesch@fh-augsburg.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 sys
11
import math
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 ''' Fehler: PyOpenGL nicht intalliert !!'''
20
  sys.exit(  )
21

    
22
animationAngle = 0.0
23
frameRate = 25
24
animationTime = 0
25

    
26
def animationStep( ):
27
        """Update animated parameters"""
28
        global animationAngle
29
        global frameRate
30
        animationAngle += 0.3
31
        while animationAngle > 360:
32
                animationAngle -= 360
33
        sleep( 1 / float( frameRate ) )
34
        glutPostRedisplay( )
35

    
36

    
37
# position points on a circle
38
angleNum = 5
39
radius=1
40
angles = [ 2*math.pi*float(i)/angleNum for i in range( angleNum ) ]
41

    
42
circlePoints = [ 
43
        [ radius*math.cos(theta), radius*math.sin(theta), 0 ] for theta in angles ]
44

    
45
# make sure the curve is closed properly
46
degree=3
47
for i in range( degree-1 ):
48
        circlePoints = circlePoints + [ circlePoints[i] ]
49

    
50
knotNum = len( circlePoints ) + degree 
51
circleKnots =  [ float(i)/(knotNum-1) for i in range( knotNum ) ]
52

    
53
def display(  ):
54
        """Glut display function."""
55
        glClear( GL_COLOR_BUFFER_BIT )
56
        glMatrixMode( GL_PROJECTION )
57
        glLoadIdentity( )
58
        xSize, ySize = glutGet( GLUT_WINDOW_WIDTH ), glutGet( GLUT_WINDOW_HEIGHT )
59
        gluPerspective(60, float(xSize) / float(ySize), 0.1, 50)
60
        glMatrixMode( GL_MODELVIEW )
61
        glLoadIdentity( )
62
        glTranslatef( 0, 0, -2 )
63
        glRotatef( animationAngle, 0, 0, 1 )
64
        global circlePoints, circleKnots
65
        glColor3f(0, 1, 0)
66
        glBegin(GL_LINE_STRIP)
67
        for coord in circlePoints:
68
                glVertex3f(coord[0], coord[1], coord[2]);
69
        glEnd()
70
        global nurb
71
        glColor3f(1, 1, 1)
72
        gluBeginCurve( nurb )
73
        gluNurbsCurve        (        nurb, circleKnots, circlePoints, GL_MAP1_VERTEX_3 )
74
        gluEndCurve( nurb )
75
        glutSwapBuffers( )
76

    
77
nurb=None
78
samplingTolerance=1.0
79
def init(  ):
80
        """Glut init function."""
81
        glClearColor ( 0, 0, 0, 0 )
82
        glShadeModel( GL_FLAT )
83
        global nurb
84
        nurb = gluNewNurbsRenderer()
85
        global samplingTolerance
86
        glLineWidth(2.0)
87
        gluNurbsProperty(nurb, GLU_SAMPLING_TOLERANCE, samplingTolerance)
88

    
89
glutInit( sys.argv )
90
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB )
91
glutInitWindowSize( 250, 250 )
92
glutInitWindowPosition( 100, 100 )
93
glutCreateWindow( sys.argv[0] )
94
init(  )
95
glutDisplayFunc( display )
96
glutIdleFunc( animationStep )
97
glutMainLoop(  )