Statistics
| Revision:

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

History | View | Annotate | Download (2.4 kB)

1 1 equemene
#!/usr/bin/python2.4
2 1 equemene
# Plot a circle using NURBS
3 1 equemene
#
4 1 equemene
# Copyright (C) 2007  "Peter Roesch" <Peter.Roesch@fh-augsburg.de>
5 1 equemene
#
6 1 equemene
# This code is licensed under the PyOpenGL License.
7 1 equemene
# Details are given in the file license.txt included in this distribution.
8 1 equemene
9 1 equemene
import sys
10 1 equemene
import math
11 1 equemene
from time import sleep
12 1 equemene
13 1 equemene
try:
14 1 equemene
  from OpenGL.GLUT import *
15 1 equemene
  from OpenGL.GL import *
16 1 equemene
  from OpenGL.GLU import *
17 1 equemene
except:
18 1 equemene
  print ''' Fehler: PyOpenGL nicht intalliert !!'''
19 1 equemene
  sys.exit(  )
20 1 equemene
21 1 equemene
animationAngle = 0.0
22 1 equemene
frameRate = 25
23 1 equemene
animationTime = 0
24 1 equemene
25 1 equemene
def animationStep( ):
26 1 equemene
        """Update animated parameters"""
27 1 equemene
        global animationAngle
28 1 equemene
        global frameRate
29 1 equemene
        animationAngle += 0.3
30 1 equemene
        while animationAngle > 360:
31 1 equemene
                animationAngle -= 360
32 1 equemene
        sleep( 1 / float( frameRate ) )
33 1 equemene
        glutPostRedisplay( )
34 1 equemene
35 1 equemene
degree=3
36 1 equemene
s2=math.sqrt(2)/2.0
37 1 equemene
38 1 equemene
# Initialise circle control points.
39 1 equemene
circlePoints = [\
40 1 equemene
        [0.0, 1.0, 0.0, 1.0],\
41 1 equemene
        [s2, s2, 0.0, s2],\
42 1 equemene
        [1.0, 0.0, 0.0, 1.0],\
43 1 equemene
        [s2, -s2, 0.0, s2],\
44 1 equemene
        [0.0, -1.0, 0.0, 1.0],\
45 1 equemene
        [-s2, -s2, 0.0, s2],\
46 1 equemene
        [-1.0, 0.0, 0.0, 1.0],\
47 1 equemene
        [-s2, s2, 0.0, s2],\
48 1 equemene
        ]
49 1 equemene
50 1 equemene
# make sure circle is closed properly
51 1 equemene
circlePoints = circlePoints + [circlePoints[0], circlePoints[1]]
52 1 equemene
53 1 equemene
# initialise circle knots
54 1 equemene
circleKnots =  [ 0.0 ] + \
55 1 equemene
        [ float(i/2) for i in range( len( circlePoints ) + degree -1 )]
56 1 equemene
57 1 equemene
def display(  ):
58 1 equemene
        glClear( GL_COLOR_BUFFER_BIT )
59 1 equemene
        glMatrixMode( GL_PROJECTION )
60 1 equemene
        glLoadIdentity( )
61 1 equemene
        xSize, ySize = glutGet( GLUT_WINDOW_WIDTH ), glutGet( GLUT_WINDOW_HEIGHT )
62 1 equemene
        gluPerspective(60, float(xSize) / float(ySize), 0.1, 50)
63 1 equemene
        glMatrixMode( GL_MODELVIEW )
64 1 equemene
        glLoadIdentity( )
65 1 equemene
        glTranslatef( 0, 0, -2 )
66 1 equemene
        glRotatef( animationAngle, 0, 0, 1 )
67 1 equemene
        global circlePoints, circleKnots
68 1 equemene
        glColor3f(0, 1, 0)
69 1 equemene
        glBegin(GL_LINE_STRIP)
70 1 equemene
        for coord in circlePoints:
71 1 equemene
                glVertex3f(coord[0], coord[1], coord[2]);
72 1 equemene
        glEnd()
73 1 equemene
        global nurb
74 1 equemene
        glColor3f(1, 1, 1)
75 1 equemene
        gluBeginCurve( nurb )
76 1 equemene
        gluNurbsCurve        (        nurb, circleKnots, circlePoints, GL_MAP1_VERTEX_4 )
77 1 equemene
        gluEndCurve( nurb )
78 1 equemene
        glutSwapBuffers( )
79 1 equemene
80 1 equemene
nurb=None
81 1 equemene
samplingTolerance=1.0
82 1 equemene
def init(  ):
83 1 equemene
        """Glut init function."""
84 1 equemene
        glClearColor ( 0, 0, 0, 0 )
85 1 equemene
        global nurb
86 1 equemene
        nurb = gluNewNurbsRenderer()
87 1 equemene
        global samplingTolerance
88 1 equemene
        glLineWidth(2.0)
89 1 equemene
        gluNurbsProperty(nurb, GLU_SAMPLING_TOLERANCE, samplingTolerance)
90 1 equemene
91 1 equemene
glutInit( sys.argv )
92 1 equemene
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB )
93 1 equemene
glutInitWindowSize( 250, 250 )
94 1 equemene
glutInitWindowPosition( 100, 100 )
95 1 equemene
glutCreateWindow( sys.argv[0] )
96 1 equemene
init(  )
97 1 equemene
glutDisplayFunc( display )
98 1 equemene
glutIdleFunc( animationStep )
99 1 equemene
glutMainLoop(  )