Statistiques
| Révision :

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

Historique | Voir | Annoter | Télécharger (4,32 ko)

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
scene.c from the Redbook examples.  
9
Converted to Python by Jason Petrone(jp@demonseed.net) 8/00
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
 *  scene.c
50
 *  This program demonstrates the use of the GL lighting model.
51
 *  Objects are drawn using a grey material characteristic. 
52
 *  A single light source illuminates the objects.
53
 */
54

55
'''
56

    
57
import sys
58

    
59
try:
60
  from OpenGL.GLUT import *
61
  from OpenGL.GL import *
62
except:
63
  print('''
64
ERROR: PyOpenGL not installed properly.  
65
        ''')
66
  sys.exit()
67

    
68

    
69
#  Initialize material property and light source.
70
def init():
71
   light_ambient =  [0.0, 0.0, 0.0, 1.0]
72
   light_diffuse =  [1.0, 1.0, 1.0, 1.0]
73
   light_specular =  [1.0, 1.0, 1.0, 1.0]
74
#  light_position is NOT default value
75
   light_position =  [1.0, 1.0, 1.0, 0.0]
76

    
77
   glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient)
78
   glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse)
79
   glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular)
80
   glLightfv(GL_LIGHT0, GL_POSITION, light_position)
81
   
82
   glEnable(GL_LIGHTING)
83
   glEnable(GL_LIGHT0)
84
   glEnable(GL_DEPTH_TEST)
85

    
86

    
87
def display():
88
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
89
   glPushMatrix()
90
   glRotatef(20.0, 1.0, 0.0, 0.0)
91
   glPushMatrix()
92
   glTranslatef(-0.75, 0.5, 0.0); 
93
   glRotatef(90.0, 1.0, 0.0, 0.0)
94
   glutSolidTorus(0.275, 0.85, 15, 15)
95
   glPopMatrix()
96

    
97
   glPushMatrix()
98
   glTranslatef(-0.75, -0.5, 0.0); 
99
   glRotatef (270.0, 1.0, 0.0, 0.0)
100
   glutSolidCone(1.0, 2.0, 15, 15)
101
   glPopMatrix()
102

    
103
   glPushMatrix()
104
   glTranslatef(0.75, 0.0, -1.0)
105
   glutSolidSphere(1.0, 15, 15)
106
   glPopMatrix()
107

    
108
   glPopMatrix()
109
   glFlush()
110

    
111

    
112
def reshape(w, h):
113
   glViewport(0, 0, w, h)
114
   glMatrixMode (GL_PROJECTION)
115
   glLoadIdentity()
116
   if w <= h:
117
      glOrtho(-2.5, 2.5, -2.5*h/w, 
118
               2.5*h/w, -10.0, 10.0)
119
   else: 
120
      glOrtho(-2.5*w/h, 
121
               2.5*w/h, -2.5, 2.5, -10.0, 10.0)
122
   glMatrixMode(GL_MODELVIEW)
123
   glLoadIdentity()
124

    
125

    
126
def keyboard(key, x, y):
127
   if key == str.encode(chr(27)):
128
       sys.exit(0)
129
   
130

    
131

    
132
#  Main Loop
133
#  Open window with initial window size, title bar, 
134
#  RGBA display mode, and handle input events.
135

    
136
glutInit(sys.argv)
137
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH)
138
glutInitWindowSize (500, 500)
139
glutCreateWindow(b'scene')
140
init()
141
glutReshapeFunc(reshape)
142
glutKeyboardFunc(keyboard)
143
glutDisplayFunc(display)
144
glutMainLoop()