Statistics
| Revision:

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

History | View | Annotate | Download (2 kB)

1
#!/usr/bin/python
2
# rotating color cube 
3
# Copyright (C) 2007  "Peter Roesch" <Peter.Roesch@fh-augsburg.de>
4
#
5
# This code is licensed under the PyOpenGL License.
6
# Details are given in the file license.txt included in this distribution.
7

    
8
import sys
9
try:
10
  from OpenGL.GLUT import *
11
  from OpenGL.GL import *
12
  from OpenGL.GLU import *
13
except:
14
  print ''' Error: PyOpenGL not installed properly '''
15
  sys.exit(  )
16

    
17
import array
18
vertices = array.array('f', [ -1,-1,1, -1,1,1, 1,1,1, 1,-1,1,\
19
  -1,-1,-1, -1,1,-1,  1,1,-1, 1,-1,-1 ] )
20

    
21
colors = array.array('f', [ 0, 0, 0,  1, 0, 0,  1, 1, 0,  0, 1, 0, \
22
  0, 0, 1,  1, 0, 1,  1, 1, 1,  0, 1, 1] )
23

    
24
cIndices = array.array('B', [0, 3, 2, 1,  2, 3, 7, 6,  0, 4, 7, 3, \
25
  1, 2, 6, 5,  4, 5, 6, 7,  0, 1, 5, 4 ] )
26

    
27
animationAngle = 0.0
28
frameRate = 25
29

    
30
from time import sleep
31
def animationStep( ):
32
        global animationAngle
33
        global frameRate
34
        animationAngle += 2
35
        while animationAngle > 360:
36
                animationAngle -= 360
37
        sleep( 1 / float( frameRate ) )
38
        glutPostRedisplay( )
39

    
40
def display(  ):
41
        glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT )
42
        glMatrixMode( GL_PROJECTION )
43
        glLoadIdentity( )
44
        glOrtho( -2, 2, -2, 2, -2, 2 ) 
45
        glMatrixMode( GL_MODELVIEW )
46
        glLoadIdentity( )
47
        glRotatef( animationAngle, 1, 1, 1 )
48
        glEnableClientState( GL_COLOR_ARRAY )
49
        glEnableClientState( GL_VERTEX_ARRAY )
50
        glColorPointer( 3, GL_FLOAT, 0, colors.tostring( ) )
51
        glVertexPointer( 3, GL_FLOAT, 0, vertices.tostring( ) )
52
        glDrawElements( GL_QUADS, 24, GL_UNSIGNED_BYTE, cIndices.tostring( ) )
53
        glDisableClientState( GL_COLOR_ARRAY )
54
        glDisableClientState( GL_VERTEX_ARRAY )
55
        glutSwapBuffers( )
56

    
57
def init(  ):
58
        if not (glColorPointer and glVertexPointer and glDrawElements):
59
                print ''' Error: no vertex array support'''
60
                sys.exit( )
61
        glClearColor ( 0, 0, 0, 0 )
62
        glEnable(GL_DEPTH_TEST)
63
        glShadeModel( GL_SMOOTH )
64

    
65
glutInit( sys.argv )
66
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH )
67
glutInitWindowSize( 250, 250 )
68
glutInitWindowPosition( 100, 100 )
69
glutCreateWindow( sys.argv[0] )
70
init(  )
71
glutDisplayFunc( display )
72
glutIdleFunc( animationStep )
73
glutMainLoop(  )