Statistics
| Revision:

root / PyOpenGL-Demo / proesch / stereo / stereoDemo.py @ 1

History | View | Annotate | Download (4.6 kB)

1
#!/usr/bin/python2.4
2
# OpenGL stereo demo using stereoCamera class
3
#
4
# Copyright (C) 2007  "Peter Roesch" <Peter.Roesch@fh-augsburg.de>
5
#
6
# This code is licensed under the PyOpenGL License.
7
# Details are given in the file license.txt included in this distribution.
8

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

    
18
from stereoCamera import StereoCamera
19
sC = StereoCamera( )
20

    
21
animationAngle = 0.0
22
frameRate = 20
23
stereoMode = "NONE"
24
lightColors = {
25
        "white":(1.0, 1.0, 1.0, 1.0),
26
        "red":(1.0, 0.0, 0.0, 1.0),
27
        "green":(0.0, 1.0, 0.0, 1.0),
28
        "blue":(0.0, 0.0, 1.0, 1.0)
29
}
30

    
31
lightPosition = (5.0, 5.0, 20.0, 1.0)
32

    
33
from time import sleep
34
def animationStep( ):
35
        """Update animated parameters."""
36
        global animationAngle
37
        global frameRate
38
        animationAngle += 2
39
        while animationAngle > 360:
40
                animationAngle -= 360
41
        sleep( 1 / float( frameRate ) )
42
        glutPostRedisplay( )
43

    
44
def setLightColor( s ):
45
        """Set light color to 'white', 'red', 'green' or 'blue'."""
46
        if lightColors.has_key( s ):
47
                c = lightColors[ s ]
48
                glLightfv( GL_LIGHT0, GL_AMBIENT, c )
49
                glLightfv( GL_LIGHT0, GL_DIFFUSE, c )
50
                glLightfv( GL_LIGHT0, GL_SPECULAR, c )
51

    
52
def render( side ):
53
        """Render scene in either GLU_BACK_LEFT or GLU_BACK_RIGHT buffer"""
54
        glViewport( 0, 0,
55
                glutGet( GLUT_WINDOW_WIDTH ), glutGet( GLUT_WINDOW_HEIGHT ))
56
        if side == GL_BACK_LEFT:
57
                f = sC.frustumLeft
58
                l = sC.lookAtLeft
59
        else:
60
                f = sC.frustumRight
61
                l = sC.lookAtRight
62
        glMatrixMode(GL_PROJECTION)
63
        glLoadIdentity()
64
        glFrustum( f[0], f[1], f[2], f[3], f[4], f[5] )
65
        glMatrixMode(GL_MODELVIEW)
66
        glLoadIdentity()
67
        gluLookAt( l[0], l[1], l[2], l[3], l[4], l[5], l[6], l[7], l[8] )
68
        glRotatef( animationAngle, 0.2, 0.7, 0.3 )        
69
        glCallList( teapotList )
70

    
71
def display(  ):
72
        """Glut display function."""
73
        if stereoMode != "SHUTTER":
74
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
75

    
76
        if stereoMode == "SHUTTER":
77
                setLightColor( "white" )
78
                glDrawBuffer( GL_BACK_LEFT )
79
                glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT )
80
                render( GL_BACK_LEFT )
81
                glDrawBuffer( GL_BACK_RIGHT )
82
                glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT )
83
                render( GL_BACK_RIGHT )
84
        elif stereoMode == "ANAGLYPH": 
85
                glDrawBuffer( GL_BACK_LEFT )
86
                glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT )
87
                setLightColor( "red" )
88
                render( GL_BACK_LEFT )
89
                glClear( GL_DEPTH_BUFFER_BIT )
90
                glColorMask( False, True, False, False )
91
                setLightColor( "green" )
92
                render( GL_BACK_RIGHT )
93
                glColorMask( True, True, True, True )
94
        else: 
95
                glDrawBuffer(GL_BACK_LEFT)
96
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
97
                setLightColor( "white" )
98
                render(GL_BACK_LEFT)
99
        glutSwapBuffers( )
100

    
101
teapotList = 0
102
def init(  ):
103
        """Glut init function."""
104
        glClearColor ( 0, 0, 0, 0 )
105
        glEnable( GL_DEPTH_TEST )
106
        glShadeModel( GL_SMOOTH )
107
        glEnable( GL_LIGHTING )
108
        glEnable( GL_LIGHT0 )
109
        glLightModeli( GL_LIGHT_MODEL_TWO_SIDE, 0 )
110
        glLightfv( GL_LIGHT0, GL_POSITION, [4, 4, 4, 1] )
111
        lA = 0.8
112
        glLightfv( GL_LIGHT0, GL_AMBIENT, [lA, lA, lA, 1] )
113
        lD = 1
114
        glLightfv( GL_LIGHT0, GL_DIFFUSE, [lD, lD, lD, 1] )
115
        lS = 1
116
        glLightfv( GL_LIGHT0, GL_SPECULAR, [lS, lS, lS, 1] )
117
        glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, [0.2, 0.2, 0.2, 1] )
118
        glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, [0.7, 0.7, 0.7, 1] )
119
        glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, [0.5, 0.5, 0.5, 1] )
120
        glMaterialf( GL_FRONT_AND_BACK, GL_SHININESS, 50 )
121
        global teapotList
122
        teapotList = glGenLists( 1 )
123
        glNewList( teapotList, GL_COMPILE )
124
        glutSolidTeapot( 1.0 )
125
        glEndList( )
126
        sC.aperture = 40.0
127
        sC.focalLength = 10.0
128
        sC.centerPosition[0], sC.centerPosition[1], sC.centerPosition[2] = \
129
                0.0, 0.0, 5.0
130
        sC.viewingDirection[0], sC.viewingDirection[1], sC.viewingDirection[2] = \
131
                0.0, 0.0, -1.0
132
        sC.near = sC.focalLength / 500.0
133
        sC.far = 1000
134
        sC.eyeSeparation = sC.focalLength / 200.0
135
        sC.whRatio = \
136
         float( glutGet( GLUT_WINDOW_WIDTH ) ) /  glutGet( GLUT_WINDOW_HEIGHT )
137
        sC.update( )
138

    
139
def reshape( width, height ):
140
        """Glut reshape function."""
141
        sC.whRatio = float(width)/float(height)
142
        sC.update( )
143

    
144
if len( argv ) != 2:
145
        print "Usage:"
146
        print "python stereDemo.py SHUTTER | ANAGLYPH | NONE \n"
147
else:
148
        glutInit( sys.argv )
149
        stereoMode = sys.argv[1].upper( )
150
        if stereoMode == "SHUTTER":
151
                glutInitDisplayMode( GLUT_STEREO | GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH )
152
        else:
153
                glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH )
154
        glutInitWindowSize( 250, 250 )
155
        glutInitWindowPosition( 100, 100 )
156
        glutCreateWindow( sys.argv[0] )
157
        init(  )
158
        glutDisplayFunc( display )
159
        glutReshapeFunc( reshape )
160
        glutIdleFunc( animationStep )
161
        glutMainLoop(  )