Statistics
| Revision:

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

History | View | Annotate | Download (2.4 kB)

1 1 equemene
"""GLUT replacement for the original arraytest.py demonstration code
2 1 equemene
"""
3 1 equemene
4 1 equemene
# This is statement is required by the build system to query build info
5 1 equemene
if __name__ == '__build__':
6 1 equemene
        raise Exception
7 1 equemene
8 1 equemene
__version__='$Revision: 1.1.1.1 $'[11:-2]
9 1 equemene
__date__ = '$Date: 2007/02/15 19:25:11 $'[6:-2]
10 1 equemene
11 1 equemene
12 1 equemene
from OpenGL.GL import *
13 1 equemene
from OpenGL.GLU import *
14 1 equemene
from OpenGL.GLUT import *
15 1 equemene
import time, sys
16 1 equemene
try:
17 1 equemene
        from numpy import *
18 1 equemene
        from numpy.random import *
19 1 equemene
except ImportError, err:
20 1 equemene
        try:
21 1 equemene
                from Numeric import *
22 1 equemene
                from RandomArray import *
23 1 equemene
        except ImportError, err:
24 1 equemene
                print "This demo requires the numpy or Numeric extension, sorry"
25 1 equemene
                import sys
26 1 equemene
                sys.exit()
27 1 equemene
28 1 equemene
n=50
29 1 equemene
30 1 equemene
def shuffle(a,b):
31 1 equemene
        return ravel(transpose(reshape(concatenate([a,b]), (2,len(a)))))
32 1 equemene
33 1 equemene
def buildArrays( ):
34 1 equemene
        a = arange(0,n)
35 1 equemene
        vertex = shuffle(cos(2*pi*a/n), sin(2*pi*a/n))
36 1 equemene
        vertex.shape = (n, 2)
37 1 equemene
        color = random(n*3)
38 1 equemene
        color.shape = (n, 3)
39 1 equemene
        return vertex,color
40 1 equemene
41 1 equemene
vertex,color = buildArrays()
42 1 equemene
43 1 equemene
def drawArrays( ):
44 1 equemene
        glVertexPointerd(vertex)
45 1 equemene
        glColorPointerd(color)
46 1 equemene
        glEnableClientState(GL_VERTEX_ARRAY)
47 1 equemene
        glEnableClientState(GL_COLOR_ARRAY)
48 1 equemene
        glDisable(GL_LIGHTING)
49 1 equemene
        try:
50 1 equemene
                glDrawArrays(GL_LINE_LOOP, 0, n)
51 1 equemene
        finally:
52 1 equemene
                glEnable(GL_LIGHTING)
53 1 equemene
54 1 equemene
def display( swap=1, clear=1):
55 1 equemene
        """Callback function for displaying the scene
56 1 equemene

57 1 equemene
        This defines a unit-square environment in which to draw,
58 1 equemene
        i.e. width is one drawing unit, as is height
59 1 equemene
        """
60 1 equemene
        glClearColor(0.5, 0.5, 0.5, 0)
61 1 equemene
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
62 1 equemene
63 1 equemene
        # establish the projection matrix (perspective)
64 1 equemene
        glMatrixMode(GL_PROJECTION)
65 1 equemene
        glLoadIdentity()
66 1 equemene
        glOrtho(-1, 1, -1, 1, -1, 1)
67 1 equemene
68 1 equemene
        # and then the model view matrix
69 1 equemene
        glMatrixMode(GL_MODELVIEW)
70 1 equemene
        glLoadIdentity()
71 1 equemene
        rotation()
72 1 equemene
73 1 equemene
        drawArrays()
74 1 equemene
75 1 equemene
        glutSwapBuffers()
76 1 equemene
77 1 equemene
def idle( ):
78 1 equemene
        glutPostRedisplay()
79 1 equemene
80 1 equemene
starttime = time.time()
81 1 equemene
82 1 equemene
def rotation( period = 10):
83 1 equemene
        """Do rotation of the scene at given rate"""
84 1 equemene
        angle = (((time.time()-starttime)%period)/period)* 360
85 1 equemene
        glRotate( angle, 0,1,0)
86 1 equemene
        return angle
87 1 equemene
88 1 equemene
def key_pressed(*args):
89 1 equemene
        # If escape is pressed, kill everything.
90 1 equemene
        if args[0] == '\033':
91 1 equemene
                sys.exit()
92 1 equemene
93 1 equemene
94 1 equemene
if __name__ == "__main__":
95 1 equemene
        print """You should see a polynomial curve rotating about the origin."""
96 1 equemene
        import sys
97 1 equemene
        glutInit(sys.argv)
98 1 equemene
        glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
99 1 equemene
        glutCreateWindow('Array Drawing Demo')
100 1 equemene
        glutDisplayFunc(display)
101 1 equemene
        glutIdleFunc(display)
102 1 equemene
        glutKeyboardFunc(key_pressed)
103 1 equemene
        # note need to do this to properly render faceted geometry
104 1 equemene
        glutMainLoop()