Statistics
| Revision:

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

History | View | Annotate | Download (2.4 kB)

1
#!/usr/bin/python2.4
2
# Plot a circle using NURBS
3
# 
4
# Copyright (C) 2007  "Peter Roesch" <Peter.Roesch@fh-augsburg.de>
5
#
6
# This code is licensed under the PyOpenGL License.
7
# Details are given in the file license.txt included in this distribution.
8

    
9
import sys
10
import math
11
from time import sleep
12

    
13
try:
14
  from OpenGL.GLUT import *
15
  from OpenGL.GL import *
16
  from OpenGL.GLU import *
17
except:
18
  print ''' Fehler: PyOpenGL nicht intalliert !!'''
19
  sys.exit(  )
20

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

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

    
35
degree=3
36
s2=math.sqrt(2)/2.0
37

    
38
# Initialise circle control points.
39
circlePoints = [\
40
        [0.0, 1.0, 0.0, 1.0],\
41
        [s2, s2, 0.0, s2],\
42
        [1.0, 0.0, 0.0, 1.0],\
43
        [s2, -s2, 0.0, s2],\
44
        [0.0, -1.0, 0.0, 1.0],\
45
        [-s2, -s2, 0.0, s2],\
46
        [-1.0, 0.0, 0.0, 1.0],\
47
        [-s2, s2, 0.0, s2],\
48
        ]
49

    
50
# make sure circle is closed properly
51
circlePoints = circlePoints + [circlePoints[0], circlePoints[1]]
52

    
53
# initialise circle knots
54
circleKnots =  [ 0.0 ] + \
55
        [ float(i/2) for i in range( len( circlePoints ) + degree -1 )]
56

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

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

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