Statistics
| Revision:

root / PyOpenGL-Demo / da / dots.py @ 1

History | View | Annotate | Download (2.8 kB)

1
#!/usr/bin/python
2

    
3
# This is statement is required by the build system to query build info
4
if __name__ == '__build__':
5
        raise Exception
6

    
7
import string
8
__version__ = string.split('$Revision: 1.1.1.1 $')[1]
9
__date__ = string.join(string.split('$Date: 2007/02/15 19:25:28 $')[1:3], ' ')
10
__author__ = 'Tarn Weisner Burton <twburton@users.sourceforge.net>'
11

    
12
try:
13
        from numpy import *
14
        from numpy.random import *
15
except ImportError, err:
16
        try: 
17
                from Numeric import *
18
                from RandomArray import *
19
        except ImportError, err:
20
                print "This demo requires the numpy or Numeric extension, sorry"
21
                import sys
22
                sys.exit()
23
import string, sys
24
from OpenGL.GL import *
25
from OpenGL.GLUT import *
26

    
27
MY_LIST=1
28
NUMDOTS = 500
29
NUMDOTS2 = 600
30
x = random(NUMDOTS)*2-1
31
y = random(NUMDOTS)*2-1
32
MAX_AGE = 13
33
age = randint(0,MAX_AGE,(NUMDOTS,))
34
move_length = .005  # 1.0 = screen width
35
angle = 0                  # in radians
36
delta_angle = .2  # in radians
37
move_x = move_length*cos(angle)
38
move_y = move_length*sin(angle)
39
halted = 0
40

    
41
def display(*args):
42
        global x, y, move_x, move_y, NUMDOTS, NUMDOTS2, MAX_AGE, age
43
        glClearColor(0.0, 0.0, 0.0, 0.0)
44
        glClear(GL_COLOR_BUFFER_BIT)
45
        glColor3f(1.0,1.0,0.0)
46
        x = x + move_x
47
        y = y + move_y
48
        age = age + 1
49
        which = greater(age, MAX_AGE)
50
        x = choose(which, (x, random(NUMDOTS)))
51
        y = choose(which, (y, random(NUMDOTS)))
52
        age = choose(which, (age, 0))
53
        x = choose(greater(x,1.0),(x,x-1.0))  # very cool - wraparound
54
        y = choose(greater(y,1.0),(y,y-1.0))
55
        x2 = random(NUMDOTS2)
56
        y2 = random(NUMDOTS2)
57
        v = concatenate((transpose(array([x,y])), transpose(array([x-.005,y+.005])), transpose(array([x+.005,y-.005])), transpose(array([x2,y2]))))
58
        glVertexPointerd(v)
59
        glEnableClientState(GL_VERTEX_ARRAY)
60
        glDrawArrays(GL_POINTS, 0, len(v))
61
        glDisableClientState(GL_VERTEX_ARRAY)
62
        glFlush()
63
        glutSwapBuffers()
64

    
65

    
66
def halt():
67
        pass
68

    
69
def keyboard(*args):
70
        sys.exit()
71

    
72
def mouse(button, state, x, y):
73
        global angle, delta_angle, move_x, move_y, move_length, halted
74
        if button == GLUT_LEFT_BUTTON:
75
                angle = angle + delta_angle
76
        elif button == GLUT_RIGHT_BUTTON:
77
                angle = angle - delta_angle
78
        elif button == GLUT_MIDDLE_BUTTON and state == GLUT_DOWN:
79
                if halted:
80
                        glutIdleFunc(display)
81
                        halted = 0
82
                else:
83
                        glutIdleFunc(halt)
84
                        halted = 1
85
        move_x = move_length * cos(angle)
86
        move_y = move_length * sin(angle)
87

    
88
def setup_viewport():
89
        glMatrixMode(GL_PROJECTION)
90
        glLoadIdentity()
91
        glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0)
92

    
93
def reshape(w, h):
94
        glViewport(0, 0, w, h)
95
        setup_viewport()
96

    
97
def main():
98
        glutInit(sys.argv)
99
        glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB)
100
        glutInitWindowSize(300, 300)
101
        glutCreateWindow('Dots')
102
        setup_viewport()
103
        glutReshapeFunc(reshape)
104
        glutDisplayFunc(display)
105
        glutIdleFunc(display)
106
        glutMouseFunc(mouse)
107
        glutKeyboardFunc(keyboard)
108
        glutMainLoop()
109

    
110
print "Use the mouse buttons to control some of the dots."
111
print "Hit any key to quit."
112
main()
113

    
114

    
115