Statistics
| Revision:

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

History | View | Annotate | Download (2.8 kB)

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

3 1 equemene
Note:
4 1 equemene
        Has no navigation code ATM.
5 1 equemene
"""
6 1 equemene
7 1 equemene
# This is statement is required by the build system to query build info
8 1 equemene
if __name__ == '__build__':
9 1 equemene
        raise Exception
10 1 equemene
11 1 equemene
__version__='$Revision: 1.1.1.1 $'[11:-2]
12 1 equemene
__date__ = '$Date: 2007/02/15 19:25:11 $'[6:-2]
13 1 equemene
14 1 equemene
import OpenGL
15 1 equemene
OpenGL.ERROR_ON_COPY = True
16 1 equemene
17 1 equemene
from OpenGL.GL import *
18 1 equemene
from OpenGL.GLU import *
19 1 equemene
from OpenGL.GLUT import *
20 1 equemene
import time, sys
21 1 equemene
try:
22 1 equemene
        from numpy import *
23 1 equemene
except ImportError, err:
24 1 equemene
        try:
25 1 equemene
                from Numeric import *
26 1 equemene
        except ImportError, err:
27 1 equemene
                print "This demo requires the numpy or Numeric extension, sorry"
28 1 equemene
                import sys
29 1 equemene
                sys.exit()
30 1 equemene
31 1 equemene
def drawCheckerBoard( N=5, white=GLfloat_3(1,1,1), black=GLfloat_3(0,0,0) ):
32 1 equemene
        """Draw an 2N*2N checkerboard with given colours"""
33 1 equemene
        glDisable(GL_LIGHTING)
34 1 equemene
        try:
35 1 equemene
                for x in range(-N, N):
36 1 equemene
                        for y in range(-N, N):
37 1 equemene
                                if (x + y) % 2 == 0:
38 1 equemene
                                        glColor3fv(white)
39 1 equemene
                                else:
40 1 equemene
                                        glColor3fv(black)
41 1 equemene
                                glRectf(x, y, x + 1, y + 1)
42 1 equemene
        finally:
43 1 equemene
                glEnable(GL_LIGHTING)
44 1 equemene
def drawSphere( center=(0,0,1), radius=1.0, sides=20 ):
45 1 equemene
        glPushMatrix()
46 1 equemene
        try:
47 1 equemene
                glTranslatef(*center)
48 1 equemene
                glutSolidSphere(radius, sides, sides)
49 1 equemene
        finally:
50 1 equemene
                glPopMatrix()
51 1 equemene
52 1 equemene
def display( swap=1, clear=1):
53 1 equemene
        """Callback function for displaying the scene
54 1 equemene

55 1 equemene
        This defines a unit-square environment in which to draw,
56 1 equemene
        i.e. width is one drawing unit, as is height
57 1 equemene
        """
58 1 equemene
        if clear:
59 1 equemene
                glClearColor(0.5, 0.5, 0.5, 0)
60 1 equemene
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
61 1 equemene
62 1 equemene
        # establish the projection matrix (perspective)
63 1 equemene
        glMatrixMode(GL_PROJECTION)
64 1 equemene
        glLoadIdentity()
65 1 equemene
        x,y,width,height = glGetDoublev(GL_VIEWPORT)
66 1 equemene
        gluPerspective(
67 1 equemene
                45, # field of view in degrees
68 1 equemene
                width/float(height or 1), # aspect ratio
69 1 equemene
                .25, # near clipping plane
70 1 equemene
                200, # far clipping plane
71 1 equemene
        )
72 1 equemene
73 1 equemene
        # and then the model view matrix
74 1 equemene
        glMatrixMode(GL_MODELVIEW)
75 1 equemene
        glLoadIdentity()
76 1 equemene
        gluLookAt(
77 1 equemene
                0,1,20, # eyepoint
78 1 equemene
                0,0,0, # center-of-view
79 1 equemene
                0,1,0, # up-vector
80 1 equemene
        )
81 1 equemene
        glLightfv( GL_LIGHT0, GL_DIFFUSE, GLfloat_3(.8,.8,.3) )
82 1 equemene
        glLightfv( GL_LIGHT0, GL_POSITION, GLfloat_4(1,1,3,0) )
83 1 equemene
        glEnable( GL_LIGHT0)
84 1 equemene
85 1 equemene
        rotation()
86 1 equemene
        drawCheckerBoard()
87 1 equemene
        drawSphere()
88 1 equemene
        if swap:
89 1 equemene
                glutSwapBuffers()
90 1 equemene
91 1 equemene
def idle( ):
92 1 equemene
        glutPostRedisplay()
93 1 equemene
94 1 equemene
starttime = time.time()
95 1 equemene
96 1 equemene
def rotation( period = 10):
97 1 equemene
        """Do rotation of the scene at given rate"""
98 1 equemene
        angle = (((time.time()-starttime)%period)/period)* 360
99 1 equemene
        glRotate( angle, 0,1,0)
100 1 equemene
        return angle
101 1 equemene
def key_pressed(*args):
102 1 equemene
        # If escape is pressed, kill everything.
103 1 equemene
        if args[0] == '\033':
104 1 equemene
                sys.exit()
105 1 equemene
106 1 equemene
if __name__ == "__main__":
107 1 equemene
        print """You should see a sphere+checker-board rotating about the origin."""
108 1 equemene
        import sys
109 1 equemene
        glutInit(sys.argv)
110 1 equemene
        glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
111 1 equemene
        glutCreateWindow('Rotating Checkerboard')
112 1 equemene
        glutDisplayFunc(display)
113 1 equemene
        glutKeyboardFunc(key_pressed)
114 1 equemene
        glutIdleFunc(display)
115 1 equemene
        # note need to do this to properly render faceted geometry
116 1 equemene
        glEnable( GL_DEPTH_TEST )
117 1 equemene
        glutMainLoop()