Statistics
| Revision:

root / PyOpenGL-Demo / proesch / colorCube / colorCube.py @ 1

History | View | Annotate | Download (2 kB)

1 1 equemene
#!/usr/bin/python
2 1 equemene
# rotating color cube
3 1 equemene
# Copyright (C) 2007  "Peter Roesch" <Peter.Roesch@fh-augsburg.de>
4 1 equemene
#
5 1 equemene
# This code is licensed under the PyOpenGL License.
6 1 equemene
# Details are given in the file license.txt included in this distribution.
7 1 equemene
8 1 equemene
import sys
9 1 equemene
try:
10 1 equemene
  from OpenGL.GLUT import *
11 1 equemene
  from OpenGL.GL import *
12 1 equemene
  from OpenGL.GLU import *
13 1 equemene
except:
14 1 equemene
  print ''' Error: PyOpenGL not installed properly '''
15 1 equemene
  sys.exit(  )
16 1 equemene
17 1 equemene
import array
18 1 equemene
vertices = array.array('f', [ -1,-1,1, -1,1,1, 1,1,1, 1,-1,1,\
19 1 equemene
  -1,-1,-1, -1,1,-1,  1,1,-1, 1,-1,-1 ] )
20 1 equemene
21 1 equemene
colors = array.array('f', [ 0, 0, 0,  1, 0, 0,  1, 1, 0,  0, 1, 0, \
22 1 equemene
  0, 0, 1,  1, 0, 1,  1, 1, 1,  0, 1, 1] )
23 1 equemene
24 1 equemene
cIndices = array.array('B', [0, 3, 2, 1,  2, 3, 7, 6,  0, 4, 7, 3, \
25 1 equemene
  1, 2, 6, 5,  4, 5, 6, 7,  0, 1, 5, 4 ] )
26 1 equemene
27 1 equemene
animationAngle = 0.0
28 1 equemene
frameRate = 25
29 1 equemene
30 1 equemene
from time import sleep
31 1 equemene
def animationStep( ):
32 1 equemene
        global animationAngle
33 1 equemene
        global frameRate
34 1 equemene
        animationAngle += 2
35 1 equemene
        while animationAngle > 360:
36 1 equemene
                animationAngle -= 360
37 1 equemene
        sleep( 1 / float( frameRate ) )
38 1 equemene
        glutPostRedisplay( )
39 1 equemene
40 1 equemene
def display(  ):
41 1 equemene
        glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT )
42 1 equemene
        glMatrixMode( GL_PROJECTION )
43 1 equemene
        glLoadIdentity( )
44 1 equemene
        glOrtho( -2, 2, -2, 2, -2, 2 )
45 1 equemene
        glMatrixMode( GL_MODELVIEW )
46 1 equemene
        glLoadIdentity( )
47 1 equemene
        glRotatef( animationAngle, 1, 1, 1 )
48 1 equemene
        glEnableClientState( GL_COLOR_ARRAY )
49 1 equemene
        glEnableClientState( GL_VERTEX_ARRAY )
50 1 equemene
        glColorPointer( 3, GL_FLOAT, 0, colors.tostring( ) )
51 1 equemene
        glVertexPointer( 3, GL_FLOAT, 0, vertices.tostring( ) )
52 1 equemene
        glDrawElements( GL_QUADS, 24, GL_UNSIGNED_BYTE, cIndices.tostring( ) )
53 1 equemene
        glDisableClientState( GL_COLOR_ARRAY )
54 1 equemene
        glDisableClientState( GL_VERTEX_ARRAY )
55 1 equemene
        glutSwapBuffers( )
56 1 equemene
57 1 equemene
def init(  ):
58 1 equemene
        if not (glColorPointer and glVertexPointer and glDrawElements):
59 1 equemene
                print ''' Error: no vertex array support'''
60 1 equemene
                sys.exit( )
61 1 equemene
        glClearColor ( 0, 0, 0, 0 )
62 1 equemene
        glEnable(GL_DEPTH_TEST)
63 1 equemene
        glShadeModel( GL_SMOOTH )
64 1 equemene
65 1 equemene
glutInit( sys.argv )
66 1 equemene
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH )
67 1 equemene
glutInitWindowSize( 250, 250 )
68 1 equemene
glutInitWindowPosition( 100, 100 )
69 1 equemene
glutCreateWindow( sys.argv[0] )
70 1 equemene
init(  )
71 1 equemene
glutDisplayFunc( display )
72 1 equemene
glutIdleFunc( animationStep )
73 1 equemene
glutMainLoop(  )