root / PyOpenGL-Demo / GLUT / tom / lorentz.py @ 2
Historique | Voir | Annoter | Télécharger (2,99 ko)
| 1 |
"""GLUT replacement for the original lorentz.py demonstration code
|
|---|---|
| 2 |
|
| 3 |
This is the original lorentz.py demo, with a few changes to
|
| 4 |
run under GLUT and not require the TK examination widget
|
| 5 |
extensions.
|
| 6 |
|
| 7 |
Code is also broken up somewhat more, with the actual drawing
|
| 8 |
of the curve in its own function which can be called instead of
|
| 9 |
the display-list if desired.
|
| 10 |
"""
|
| 11 |
|
| 12 |
# This is statement is required by the build system to query build info
|
| 13 |
if __name__ == '__build__': |
| 14 |
raise Exception |
| 15 |
|
| 16 |
__version__='$Revision: 1.1.1.1 $'[11:-2] |
| 17 |
__date__ = '$Date: 2007/02/15 19:25:13 $'[6:-2] |
| 18 |
|
| 19 |
|
| 20 |
from OpenGL.GL import * |
| 21 |
from OpenGL.GLU import * |
| 22 |
from OpenGL.GLUT import * |
| 23 |
import time |
| 24 |
|
| 25 |
n, dt = 2000, 0.01 |
| 26 |
x, y, z = 0.01, 0.01, 0.01 |
| 27 |
frac = -1.0 * (8.0/3.0) |
| 28 |
|
| 29 |
def lorentz(x, y, z, n=2000, dt=0.01): |
| 30 |
"""Generate Lorentz attractor as a Display-list"""
|
| 31 |
target = glGenLists(1);
|
| 32 |
glNewList(target, GL_COMPILE); |
| 33 |
try:
|
| 34 |
drawLorentz( x,y,z,n,dt ) |
| 35 |
finally:
|
| 36 |
glEndList() |
| 37 |
return target
|
| 38 |
def drawLorentz( x, y, z, n=2000, dt=0.01): |
| 39 |
"""Do the actual drawing & calculation of lorentz"""
|
| 40 |
glDisable(GL_LIGHTING) |
| 41 |
glBegin(GL_LINE_STRIP) |
| 42 |
|
| 43 |
glVertex3f(x, y, z) |
| 44 |
frac = -1.0 * (8.0/3.0) |
| 45 |
for i in range(0, n): |
| 46 |
xp = x + (-10.0 * x * dt + 10.0 * y * dt) |
| 47 |
yp = y + ( 28.0 * x * dt - y * dt - x * dt * z *dt)
|
| 48 |
zp = z + ( frac * z * dt + x * dt * y * dt) |
| 49 |
x=xp |
| 50 |
y=yp |
| 51 |
z=zp |
| 52 |
glColor3f( i/float(n),i/float(n), 0 ) |
| 53 |
glVertex3f(x, y, z) |
| 54 |
glEnd() |
| 55 |
|
| 56 |
glEnable(GL_LIGHTING) |
| 57 |
|
| 58 |
LORENTZ_LIST = None
|
| 59 |
|
| 60 |
def display( ): |
| 61 |
"""Callback function for displaying the scene
|
| 62 |
|
| 63 |
This defines a unit-square environment in which to draw,
|
| 64 |
i.e. width is one drawing unit, as is height
|
| 65 |
"""
|
| 66 |
glClearColor(0.5, 0.5, 0.5, 0) |
| 67 |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) |
| 68 |
|
| 69 |
# establish the projection matrix (perspective)
|
| 70 |
glMatrixMode(GL_PROJECTION) |
| 71 |
glLoadIdentity() |
| 72 |
gluPerspective( |
| 73 |
45, # field of view in degrees |
| 74 |
glutGet(GLUT_WINDOW_WIDTH)/float(glutGet(GLUT_WINDOW_HEIGHT) or 1), # aspect ratio |
| 75 |
1, # near clipping plane |
| 76 |
30000, # far clipping plane |
| 77 |
) |
| 78 |
# and then the model view matrix
|
| 79 |
glMatrixMode(GL_MODELVIEW) |
| 80 |
glLoadIdentity() |
| 81 |
gluLookAt( |
| 82 |
0,0,13000, # eyepoint |
| 83 |
0,0,2000, # center-of-view |
| 84 |
0,1,0, # up-vector |
| 85 |
) |
| 86 |
rotation() |
| 87 |
global LORENTZ_LIST
|
| 88 |
if LORENTZ_LIST is None: |
| 89 |
LORENTZ_LIST = lorentz( 0.01, 0.01, 0.01 ) |
| 90 |
glCallList( LORENTZ_LIST ) |
| 91 |
# drawLorentz( 0.01, 0.01, 0.01 )
|
| 92 |
|
| 93 |
glutSwapBuffers() |
| 94 |
|
| 95 |
def idle( ): |
| 96 |
glutPostRedisplay() |
| 97 |
def key_pressed(*args): |
| 98 |
# If escape is pressed, kill everything.
|
| 99 |
if args[0] == '\033': |
| 100 |
sys.exit() |
| 101 |
|
| 102 |
starttime = time.time() |
| 103 |
|
| 104 |
def rotation( period = 10): |
| 105 |
"""Do rotation of the scene at given rate"""
|
| 106 |
angle = (((time.time()-starttime)%period)/period)* 360
|
| 107 |
glRotate( angle, 0,1,0) |
| 108 |
return angle
|
| 109 |
|
| 110 |
if __name__ == "__main__": |
| 111 |
print """You should see a curved line representing a Lorentz |
| 112 |
attractor, rendered in 3D, rotation about the origin."""
|
| 113 |
import sys |
| 114 |
glutInit(sys.argv) |
| 115 |
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH) |
| 116 |
glutCreateWindow('Lorentz Attractor Demo')
|
| 117 |
glutKeyboardFunc(key_pressed) |
| 118 |
glutDisplayFunc(display) |
| 119 |
glutIdleFunc(display) |
| 120 |
glutMainLoop() |