Statistics
| Revision:

root / PyOpenGL-Demo / GLUT / tom / lorentz.py @ 1

History | View | Annotate | Download (3 kB)

1 1 equemene
"""GLUT replacement for the original lorentz.py demonstration code
2 1 equemene

3 1 equemene
This is the original lorentz.py demo, with a few changes to
4 1 equemene
run under GLUT and not require the TK examination widget
5 1 equemene
extensions.
6 1 equemene

7 1 equemene
Code is also broken up somewhat more, with the actual drawing
8 1 equemene
of the curve in its own function which can be called instead of
9 1 equemene
the display-list if desired.
10 1 equemene
"""
11 1 equemene
12 1 equemene
# This is statement is required by the build system to query build info
13 1 equemene
if __name__ == '__build__':
14 1 equemene
        raise Exception
15 1 equemene
16 1 equemene
__version__='$Revision: 1.1.1.1 $'[11:-2]
17 1 equemene
__date__ = '$Date: 2007/02/15 19:25:13 $'[6:-2]
18 1 equemene
19 1 equemene
20 1 equemene
from OpenGL.GL import *
21 1 equemene
from OpenGL.GLU import *
22 1 equemene
from OpenGL.GLUT import *
23 1 equemene
import time
24 1 equemene
25 1 equemene
n, dt = 2000, 0.01
26 1 equemene
x, y, z = 0.01, 0.01, 0.01
27 1 equemene
frac = -1.0 * (8.0/3.0)
28 1 equemene
29 1 equemene
def lorentz(x, y, z, n=2000, dt=0.01):
30 1 equemene
        """Generate Lorentz attractor as a Display-list"""
31 1 equemene
        target = glGenLists(1);
32 1 equemene
        glNewList(target, GL_COMPILE);
33 1 equemene
        try:
34 1 equemene
                drawLorentz( x,y,z,n,dt )
35 1 equemene
        finally:
36 1 equemene
                glEndList()
37 1 equemene
        return target
38 1 equemene
def drawLorentz( x, y, z, n=2000, dt=0.01):
39 1 equemene
        """Do the actual drawing & calculation of lorentz"""
40 1 equemene
        glDisable(GL_LIGHTING)
41 1 equemene
        glBegin(GL_LINE_STRIP)
42 1 equemene
43 1 equemene
        glVertex3f(x, y, z)
44 1 equemene
        frac = -1.0 * (8.0/3.0)
45 1 equemene
        for i in range(0, n):
46 1 equemene
                xp = x + (-10.0 * x * dt + 10.0 * y * dt)
47 1 equemene
                yp = y + ( 28.0 * x * dt - y * dt - x * dt * z *dt)
48 1 equemene
                zp = z + ( frac * z * dt + x * dt * y * dt)
49 1 equemene
                x=xp
50 1 equemene
                y=yp
51 1 equemene
                z=zp
52 1 equemene
                glColor3f( i/float(n),i/float(n), 0 )
53 1 equemene
                glVertex3f(x, y, z)
54 1 equemene
        glEnd()
55 1 equemene
56 1 equemene
        glEnable(GL_LIGHTING)
57 1 equemene
58 1 equemene
LORENTZ_LIST = None
59 1 equemene
60 1 equemene
def display( ):
61 1 equemene
        """Callback function for displaying the scene
62 1 equemene

63 1 equemene
        This defines a unit-square environment in which to draw,
64 1 equemene
        i.e. width is one drawing unit, as is height
65 1 equemene
        """
66 1 equemene
        glClearColor(0.5, 0.5, 0.5, 0)
67 1 equemene
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
68 1 equemene
69 1 equemene
        # establish the projection matrix (perspective)
70 1 equemene
        glMatrixMode(GL_PROJECTION)
71 1 equemene
        glLoadIdentity()
72 1 equemene
        gluPerspective(
73 1 equemene
                45, # field of view in degrees
74 1 equemene
                glutGet(GLUT_WINDOW_WIDTH)/float(glutGet(GLUT_WINDOW_HEIGHT) or 1), # aspect ratio
75 1 equemene
                1, # near clipping plane
76 1 equemene
                30000, # far clipping plane
77 1 equemene
        )
78 1 equemene
        # and then the model view matrix
79 1 equemene
        glMatrixMode(GL_MODELVIEW)
80 1 equemene
        glLoadIdentity()
81 1 equemene
        gluLookAt(
82 1 equemene
                0,0,13000, # eyepoint
83 1 equemene
                0,0,2000, # center-of-view
84 1 equemene
                0,1,0, # up-vector
85 1 equemene
        )
86 1 equemene
        rotation()
87 1 equemene
        global LORENTZ_LIST
88 1 equemene
        if LORENTZ_LIST is None:
89 1 equemene
                LORENTZ_LIST = lorentz( 0.01, 0.01, 0.01 )
90 1 equemene
        glCallList( LORENTZ_LIST )
91 1 equemene
#        drawLorentz( 0.01, 0.01, 0.01 )
92 1 equemene
93 1 equemene
        glutSwapBuffers()
94 1 equemene
95 1 equemene
def idle( ):
96 1 equemene
        glutPostRedisplay()
97 1 equemene
def key_pressed(*args):
98 1 equemene
        # If escape is pressed, kill everything.
99 1 equemene
        if args[0] == '\033':
100 1 equemene
                sys.exit()
101 1 equemene
102 1 equemene
starttime = time.time()
103 1 equemene
104 1 equemene
def rotation( period = 10):
105 1 equemene
        """Do rotation of the scene at given rate"""
106 1 equemene
        angle = (((time.time()-starttime)%period)/period)* 360
107 1 equemene
        glRotate( angle, 0,1,0)
108 1 equemene
        return angle
109 1 equemene
110 1 equemene
if __name__ == "__main__":
111 1 equemene
        print """You should see a curved line representing a Lorentz
112 1 equemene
attractor, rendered in 3D, rotation about the origin."""
113 1 equemene
        import sys
114 1 equemene
        glutInit(sys.argv)
115 1 equemene
        glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
116 1 equemene
        glutCreateWindow('Lorentz Attractor Demo')
117 1 equemene
        glutKeyboardFunc(key_pressed)
118 1 equemene
        glutDisplayFunc(display)
119 1 equemene
        glutIdleFunc(display)
120 1 equemene
        glutMainLoop()