Statistics
| Revision:

root / PyOpenGL-Demo / redbook / movelight.py @ 11

History | View | Annotate | Download (4.6 kB)

1
#!/usr/bin/python
2

    
3
# This is statement is required by the build system to query build info
4
if __name__ == '__build__':
5
        raise(Exception)
6

    
7
'''
8
Converted to Python 6/00 by Jason Petrone(jp@demonseed.net)
9

10

11
/*
12
 * Copyright (c) 1993-1997, Silicon Graphics, Inc.
13
 * ALL RIGHTS RESERVED 
14
 * Permission to use, copy, modify, and distribute this software for 
15
 * any purpose and without fee is hereby granted, provided that the above
16
 * copyright notice appear in all copies and that both the copyright notice
17
 * and this permission notice appear in supporting documentation, and that 
18
 * the name of Silicon Graphics, Inc. not be used in advertising
19
 * or publicity pertaining to distribution of the software without specific,
20
 * written prior permission. 
21
 *
22
 * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
23
 * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
24
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
25
 * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
26
 * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
27
 * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
28
 * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
29
 * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
30
 * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
31
 * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
32
 * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
33
 * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
34
 * 
35
 * US Government Users Restricted Rights 
36
 * Use, duplication, or disclosure by the Government is subject to
37
 * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
38
 * (c)(1)(ii) of the Rights in Technical Data and Computer Software
39
 * clause at DFARS 252.227-7013 and/or in similar or successor
40
 * clauses in the FAR or the DOD or NASA FAR Supplement.
41
 * Unpublished-- rights reserved under the copyright laws of the
42
 * United States.  Contractor/manufacturer is Silicon Graphics,
43
 * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
44
 *
45
 * OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
46
 */
47

48
/*
49
 *  movelight.c
50
 *  This program demonstrates when to issue lighting and
51
 *  transformation commands to render a model with a light
52
 *  which is moved by a modeling transformation (rotate or
53
 *  translate).  The light position is reset after the modeling
54
 *  transformation is called.  The eye position does not change.
55
 *
56
 *  A sphere is drawn using a grey material characteristic.
57
 *  A single light source illuminates the object.
58
 *
59
 *  Interaction:  pressing the left mouse button alters
60
 *  the modeling transformation (x rotation) by 30 degrees.
61
 *  The scene is then redrawn with the light in a new position.
62
 */
63

64
'''
65

    
66
import sys
67

    
68
try:
69
  from OpenGL.GLUT import *
70
  from OpenGL.GL import *
71
  from OpenGL.GLU import *
72
except:
73
  print('''
74
ERROR: PyOpenGL not installed properly.  
75
        ''')
76
  sys.exit()
77

    
78

    
79
spin = 0
80

    
81
#  Initialize material property, light source, lighting model,
82
#  and depth buffer.
83
def init():
84
   glClearColor (0.0, 0.0, 0.0, 0.0)
85
   glShadeModel (GL_SMOOTH)
86
   glEnable(GL_LIGHTING)
87
   glEnable(GL_LIGHT0)
88
   glEnable(GL_DEPTH_TEST)
89

    
90
#  Here is where the light position is reset after the modeling
91
#  transformation (glRotated) is called.  This places the
92
#  light at a new position in world coordinates.  The cube
93
#  represents the position of the light.
94
def display():
95
   position =  [0.0, 0.0, 1.5, 1.0]
96

    
97
   glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
98
   glPushMatrix ()
99
   gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)
100

    
101
   glPushMatrix ()
102
   glRotated (spin, 1.0, 0.0, 0.0)
103
   glLightfv (GL_LIGHT0, GL_POSITION, position)
104

    
105
   glTranslated (0.0, 0.0, 1.5)
106
   glDisable (GL_LIGHTING)
107
   glColor3f (0.0, 1.0, 1.0)
108
   glutWireCube (0.1)
109
   glEnable (GL_LIGHTING)
110
   glPopMatrix ()
111

    
112
   glutSolidTorus (0.275, 0.85, 8, 15)
113
   glPopMatrix ()
114
   glFlush ()
115

    
116
def reshape (w, h):
117
   glViewport (0, 0, w, h)
118
   glMatrixMode (GL_PROJECTION)
119
   glLoadIdentity()
120
   gluPerspective(40.0, w/h, 1.0, 20.0)
121
   glMatrixMode(GL_MODELVIEW)
122
   glLoadIdentity()
123

    
124
def mouse(button, state, x, y):
125
   global spin
126
   if button == GLUT_LEFT_BUTTON and state == GLUT_DOWN:
127
       spin = (spin + 30) % 360
128
       glutPostRedisplay()
129

    
130
def keyboard(key, x, y):
131
   if key == str.encode(chr(27)):
132
       sys.exit(0)
133

    
134
glutInit(sys.argv)
135
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB)
136
glutInitWindowSize (500, 500); 
137
glutInitWindowPosition(100, 100)
138
glutCreateWindow(b"movelight")
139
init()
140
glutDisplayFunc(display)
141
glutReshapeFunc(reshape)
142
glutMouseFunc(mouse)
143
glutKeyboardFunc(keyboard)
144
glutMainLoop()
145