Statistics
| Revision:

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

History | View | Annotate | Download (2.8 kB)

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

3
Note:
4
        Has no navigation code ATM.
5
"""
6

    
7
# This is statement is required by the build system to query build info
8
if __name__ == '__build__':
9
        raise Exception
10

    
11
__version__='$Revision: 1.1.1.1 $'[11:-2]
12
__date__ = '$Date: 2007/02/15 19:25:11 $'[6:-2]
13

    
14
import OpenGL 
15
OpenGL.ERROR_ON_COPY = True 
16

    
17
from OpenGL.GL import *
18
from OpenGL.GLU import *
19
from OpenGL.GLUT import *
20
import time, sys
21
try:
22
        from numpy import *
23
except ImportError, err:
24
        try: 
25
                from Numeric import *
26
        except ImportError, err:
27
                print "This demo requires the numpy or Numeric extension, sorry"
28
                import sys
29
                sys.exit()
30

    
31
def drawCheckerBoard( N=5, white=GLfloat_3(1,1,1), black=GLfloat_3(0,0,0) ):
32
        """Draw an 2N*2N checkerboard with given colours"""
33
        glDisable(GL_LIGHTING)
34
        try:
35
                for x in range(-N, N):
36
                        for y in range(-N, N):
37
                                if (x + y) % 2 == 0:
38
                                        glColor3fv(white)
39
                                else:
40
                                        glColor3fv(black)        
41
                                glRectf(x, y, x + 1, y + 1)
42
        finally:
43
                glEnable(GL_LIGHTING)
44
def drawSphere( center=(0,0,1), radius=1.0, sides=20 ):
45
        glPushMatrix()
46
        try:
47
                glTranslatef(*center)
48
                glutSolidSphere(radius, sides, sides)
49
        finally:
50
                glPopMatrix()
51

    
52
def display( swap=1, clear=1):
53
        """Callback function for displaying the scene
54

55
        This defines a unit-square environment in which to draw,
56
        i.e. width is one drawing unit, as is height
57
        """
58
        if clear:
59
                glClearColor(0.5, 0.5, 0.5, 0)
60
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
61

    
62
        # establish the projection matrix (perspective)
63
        glMatrixMode(GL_PROJECTION)
64
        glLoadIdentity()
65
        x,y,width,height = glGetDoublev(GL_VIEWPORT)
66
        gluPerspective(
67
                45, # field of view in degrees
68
                width/float(height or 1), # aspect ratio
69
                .25, # near clipping plane
70
                200, # far clipping plane
71
        )
72

    
73
        # and then the model view matrix
74
        glMatrixMode(GL_MODELVIEW)
75
        glLoadIdentity()
76
        gluLookAt(
77
                0,1,20, # eyepoint
78
                0,0,0, # center-of-view
79
                0,1,0, # up-vector
80
        )
81
        glLightfv( GL_LIGHT0, GL_DIFFUSE, GLfloat_3(.8,.8,.3) )
82
        glLightfv( GL_LIGHT0, GL_POSITION, GLfloat_4(1,1,3,0) )
83
        glEnable( GL_LIGHT0)
84
        
85
        rotation()
86
        drawCheckerBoard()
87
        drawSphere()
88
        if swap:
89
                glutSwapBuffers()
90

    
91
def idle( ):
92
        glutPostRedisplay()
93

    
94
starttime = time.time()
95

    
96
def rotation( period = 10):
97
        """Do rotation of the scene at given rate"""
98
        angle = (((time.time()-starttime)%period)/period)* 360
99
        glRotate( angle, 0,1,0)
100
        return angle
101
def key_pressed(*args):
102
        # If escape is pressed, kill everything.
103
        if args[0] == '\033':
104
                sys.exit()
105

    
106
if __name__ == "__main__":
107
        print """You should see a sphere+checker-board rotating about the origin."""
108
        import sys
109
        glutInit(sys.argv)
110
        glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
111
        glutCreateWindow('Rotating Checkerboard')
112
        glutDisplayFunc(display)
113
        glutKeyboardFunc(key_pressed)
114
        glutIdleFunc(display)
115
        # note need to do this to properly render faceted geometry
116
        glEnable( GL_DEPTH_TEST )
117
        glutMainLoop()