Statistiques
| Révision :

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

Historique | Voir | Annoter | Télécharger (7,67 ko)

1
#!/usr/bin/python
2
'''
3
teapots.py
4
Converted to Python by Jason Petrone 6/00
5

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

    
44
#  teapots.c
45
#  This program demonstrates lots of material properties.
46
#  A single light source illuminates the objects.
47

    
48
import sys
49

    
50
try:
51
  from OpenGL.GLUT import *
52
  from OpenGL.GL import *
53
  from OpenGL.GLU import *
54
except:
55
  print('''
56
ERROR: PyOpenGL not installed properly.  
57
        ''')
58
  sys.exit()
59

    
60
teapotList = None
61

    
62
# Initialize depth buffer, projection matrix, light source, and lighting
63
# model.  Do not specify a material property here.
64

    
65
def init():
66
   ambient = [0.0, 0.0, 0.0, 1.0]
67
   diffuse = [1.0, 1.0, 1.0, 1.0]
68
   specular = [1.0, 1.0, 1.0, 1.0]
69
   position = [0.0, 3.0, 3.0, 0.0]
70

    
71
   lmodel_ambient = [0.2, 0.2, 0.2, 1.0]
72
   local_view = [0.0]
73

    
74
   glLightfv(GL_LIGHT0, GL_AMBIENT, ambient)
75
   glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse)
76
   glLightfv(GL_LIGHT0, GL_POSITION, position)
77
   glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient)
78
   glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view)
79

    
80
   glFrontFace(GL_CW)
81
   glEnable(GL_LIGHTING)
82
   glEnable(GL_LIGHT0)
83
   glEnable(GL_AUTO_NORMAL)
84
   glEnable(GL_NORMALIZE)
85
   glEnable(GL_DEPTH_TEST) 
86

    
87
   #  be efficient--make teapot display list 
88
   global teapotList
89
   teapotList = glGenLists(1)
90
   glNewList (teapotList, GL_COMPILE)
91
   glutSolidTeapot(1.0)
92
#   glutWireTeapot(1.0)
93
#   glutSolidSphere( 1.0, 8, 8 )
94
   glEndList ()
95

    
96
# Move object into position.  Use 3rd through 12th 
97
# parameters to specify the material property.  Draw a teapot.
98
def renderTeapot(x, y, ambr, ambg, ambb, difr, difg, 
99
                      difb, specr, specg, specb, shine):
100
   mat = [0, 0, 0, 0]
101

    
102
   glPushMatrix()
103
   glTranslatef(x, y, 0.0)
104
   mat[0] = ambr; mat[1] = ambg; mat[2] = ambb; mat[3] = 1.0
105
   glMaterialfv(GL_FRONT, GL_AMBIENT, mat)
106
   mat[0] = difr; mat[1] = difg; mat[2] = difb
107
   glMaterialfv(GL_FRONT, GL_DIFFUSE, mat)
108
   mat[0] = specr; mat[1] = specg; mat[2] = specb
109
   glMaterialfv(GL_FRONT, GL_SPECULAR, mat)
110
   glMaterialf(GL_FRONT, GL_SHININESS, shine * 128.0)
111
   # Linux GLUT seems to optimize away the actual generation of the 
112
   # teapot on modern GLUT when using display-lists, so we have to 
113
   # use direct calls here...
114
#   glCallList(teapotList)
115
   glutSolidTeapot(1.0)
116
   glPopMatrix()
117

    
118

    
119
#  First column:  emerald, jade, obsidian, pearl, ruby, turquoise
120
#  2nd column:  brass, bronze, chrome, copper, gold, silver
121
#  3rd column:  black, cyan, green, red, white, yellow plastic
122
#  4th column:  black, cyan, green, red, white, yellow rubber
123
def display():
124
    global teapotList
125
    if not teapotList:
126
        init();
127
    glClearColor( 1.0,1.0,1.0, 1.0 )
128
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
129
    renderTeapot(2.0, 17.0, 0.0215, 0.1745, 0.0215,
130
      0.07568, 0.61424, 0.07568, 0.633, 0.727811, 0.633, 0.6)
131
    renderTeapot(2.0, 14.0, 0.135, 0.2225, 0.1575,
132
      0.54, 0.89, 0.63, 0.316228, 0.316228, 0.316228, 0.1)
133
    renderTeapot(2.0, 11.0, 0.05375, 0.05, 0.06625,
134
      0.18275, 0.17, 0.22525, 0.332741, 0.328634, 0.346435, 0.3)
135
    renderTeapot(2.0, 8.0, 0.25, 0.20725, 0.20725,
136
      1, 0.829, 0.829, 0.296648, 0.296648, 0.296648, 0.088)
137
    renderTeapot(2.0, 5.0, 0.1745, 0.01175, 0.01175,
138
      0.61424, 0.04136, 0.04136, 0.727811, 0.626959, 0.626959, 0.6)
139
    renderTeapot(2.0, 2.0, 0.1, 0.18725, 0.1745,
140
      0.396, 0.74151, 0.69102, 0.297254, 0.30829, 0.306678, 0.1)
141
    renderTeapot(6.0, 17.0, 0.329412, 0.223529, 0.027451,
142
      0.780392, 0.568627, 0.113725, 0.992157, 0.941176, 0.807843,
143
      0.21794872);
144
    renderTeapot(6.0, 14.0, 0.2125, 0.1275, 0.054,
145
      0.714, 0.4284, 0.18144, 0.393548, 0.271906, 0.166721, 0.2)
146
    renderTeapot(6.0, 11.0, 0.25, 0.25, 0.25,
147
      0.4, 0.4, 0.4, 0.774597, 0.774597, 0.774597, 0.6)
148
    renderTeapot(6.0, 8.0, 0.19125, 0.0735, 0.0225,
149
      0.7038, 0.27048, 0.0828, 0.256777, 0.137622, 0.086014, 0.1)
150
    renderTeapot(6.0, 5.0, 0.24725, 0.1995, 0.0745,
151
      0.75164, 0.60648, 0.22648, 0.628281, 0.555802, 0.366065, 0.4)
152
    renderTeapot(6.0, 2.0, 0.19225, 0.19225, 0.19225,
153
      0.50754, 0.50754, 0.50754, 0.508273, 0.508273, 0.508273, 0.4)
154
    renderTeapot(10.0, 17.0, 0.0, 0.0, 0.0, 0.01, 0.01, 0.01,
155
      0.50, 0.50, 0.50, .25)
156
    renderTeapot(10.0, 14.0, 0.0, 0.1, 0.06, 0.0, 0.50980392, 0.50980392,
157
      0.50196078, 0.50196078, 0.50196078, .25)
158
    renderTeapot(10.0, 11.0, 0.0, 0.0, 0.0,
159
      0.1, 0.35, 0.1, 0.45, 0.55, 0.45, .25);
160
    renderTeapot(10.0, 8.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0,
161
      0.7, 0.6, 0.6, .25)
162
    renderTeapot(10.0, 5.0, 0.0, 0.0, 0.0, 0.55, 0.55, 0.55,
163
      0.70, 0.70, 0.70, .25)
164
    renderTeapot(10.0, 2.0, 0.0, 0.0, 0.0, 0.5, 0.5, 0.0,
165
      0.60, 0.60, 0.50, .25)
166
    renderTeapot(14.0, 17.0, 0.02, 0.02, 0.02, 0.01, 0.01, 0.01,
167
      0.4, 0.4, 0.4, .078125)
168
    renderTeapot(14.0, 14.0, 0.0, 0.05, 0.05, 0.4, 0.5, 0.5,
169
      0.04, 0.7, 0.7, .078125)
170
    renderTeapot(14.0, 11.0, 0.0, 0.05, 0.0, 0.4, 0.5, 0.4,
171
      0.04, 0.7, 0.04, .078125)
172
    renderTeapot(14.0, 8.0, 0.05, 0.0, 0.0, 0.5, 0.4, 0.4,
173
      0.7, 0.04, 0.04, .078125)
174
    renderTeapot(14.0, 5.0, 0.05, 0.05, 0.05, 0.5, 0.5, 0.5,
175
      0.7, 0.7, 0.7, .078125)
176
    renderTeapot(14.0, 2.0, 0.05, 0.05, 0.0, 0.5, 0.5, 0.4,
177
      0.7, 0.7, 0.04, .078125)
178
    glutSwapBuffers()
179

    
180
def reshape(w, h):
181
   glViewport(0, 0, w, h)
182
   glMatrixMode(GL_PROJECTION)
183
   glLoadIdentity()
184
   if (w <= h):
185
      glOrtho(0.0, 16.0, 0.0, 16.0*h/w, -10.0, 10.0)
186
   else:
187
      glOrtho(0.0, 16.0*w/h, 0.0, 16.0, -10.0, 10.0)
188
   glMatrixMode(GL_MODELVIEW)
189

    
190

    
191
def keyboard(key, x, y):
192
   if key == str.encode(chr(27)):
193
      sys.exit(0)
194

    
195
# Main Loop 
196
if __name__ == "__main__":
197
        glutInit(sys.argv);
198
        glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
199
        glutInitWindowSize(500, 600);
200
        glutInitWindowPosition(50,50);
201
        glutCreateWindow(str.encode(sys.argv[0]));
202
        glutReshapeFunc(reshape)
203
        glutDisplayFunc(display)
204
        glutKeyboardFunc(keyboard)
205
        glutMainLoop()