Statistics
| Revision:

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

History | View | Annotate | Download (7.6 kB)

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

6 1 equemene
/*
7 1 equemene
 * Copyright (c) 1993-1997, Silicon Graphics, Inc.
8 1 equemene
 * ALL RIGHTS RESERVED
9 1 equemene
 * Permission to use, copy, modify, and distribute this software for
10 1 equemene
 * any purpose and without fee is hereby granted, provided that the above
11 1 equemene
 * copyright notice appear in all copies and that both the copyright notice
12 1 equemene
 * and this permission notice appear in supporting documentation, and that
13 1 equemene
 * the name of Silicon Graphics, Inc. not be used in advertising
14 1 equemene
 * or publicity pertaining to distribution of the software without specific,
15 1 equemene
 * written prior permission.
16 1 equemene
 *
17 1 equemene
 * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
18 1 equemene
 * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
19 1 equemene
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
20 1 equemene
 * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
21 1 equemene
 * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
22 1 equemene
 * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
23 1 equemene
 * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
24 1 equemene
 * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
25 1 equemene
 * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
26 1 equemene
 * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
27 1 equemene
 * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
28 1 equemene
 * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
29 1 equemene
 *
30 1 equemene
 * US Government Users Restricted Rights
31 1 equemene
 * Use, duplication, or disclosure by the Government is subject to
32 1 equemene
 * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
33 1 equemene
 * (c)(1)(ii) of the Rights in Technical Data and Computer Software
34 1 equemene
 * clause at DFARS 252.227-7013 and/or in similar or successor
35 1 equemene
 * clauses in the FAR or the DOD or NASA FAR Supplement.
36 1 equemene
 * Unpublished-- rights reserved under the copyright laws of the
37 1 equemene
 * United States.  Contractor/manufacturer is Silicon Graphics,
38 1 equemene
 * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
39 1 equemene
 *
40 1 equemene
 * OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
41 1 equemene
 */
42 1 equemene
'''
43 1 equemene
44 1 equemene
#  teapots.c
45 1 equemene
#  This program demonstrates lots of material properties.
46 1 equemene
#  A single light source illuminates the objects.
47 1 equemene
48 1 equemene
import sys
49 1 equemene
50 1 equemene
try:
51 1 equemene
  from OpenGL.GLUT import *
52 1 equemene
  from OpenGL.GL import *
53 1 equemene
  from OpenGL.GLU import *
54 1 equemene
except:
55 1 equemene
  print '''
56 1 equemene
ERROR: PyOpenGL not installed properly.
57 1 equemene
        '''
58 1 equemene
  sys.exit()
59 1 equemene
60 1 equemene
teapotList = None
61 1 equemene
62 1 equemene
# Initialize depth buffer, projection matrix, light source, and lighting
63 1 equemene
# model.  Do not specify a material property here.
64 1 equemene
65 1 equemene
def init():
66 1 equemene
   ambient = [0.0, 0.0, 0.0, 1.0]
67 1 equemene
   diffuse = [1.0, 1.0, 1.0, 1.0]
68 1 equemene
   specular = [1.0, 1.0, 1.0, 1.0]
69 1 equemene
   position = [0.0, 3.0, 3.0, 0.0]
70 1 equemene
71 1 equemene
   lmodel_ambient = [0.2, 0.2, 0.2, 1.0]
72 1 equemene
   local_view = [0.0]
73 1 equemene
74 1 equemene
   glLightfv(GL_LIGHT0, GL_AMBIENT, ambient)
75 1 equemene
   glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse)
76 1 equemene
   glLightfv(GL_LIGHT0, GL_POSITION, position)
77 1 equemene
   glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient)
78 1 equemene
   glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view)
79 1 equemene
80 1 equemene
   glFrontFace(GL_CW)
81 1 equemene
   glEnable(GL_LIGHTING)
82 1 equemene
   glEnable(GL_LIGHT0)
83 1 equemene
   glEnable(GL_AUTO_NORMAL)
84 1 equemene
   glEnable(GL_NORMALIZE)
85 1 equemene
   glEnable(GL_DEPTH_TEST)
86 1 equemene
87 1 equemene
   #  be efficient--make teapot display list
88 1 equemene
   global teapotList
89 1 equemene
   teapotList = glGenLists(1)
90 1 equemene
   glNewList (teapotList, GL_COMPILE)
91 1 equemene
   glutSolidTeapot(1.0)
92 1 equemene
#   glutWireTeapot(1.0)
93 1 equemene
#   glutSolidSphere( 1.0, 8, 8 )
94 1 equemene
   glEndList ()
95 1 equemene
96 1 equemene
# Move object into position.  Use 3rd through 12th
97 1 equemene
# parameters to specify the material property.  Draw a teapot.
98 1 equemene
def renderTeapot(x, y, ambr, ambg, ambb, difr, difg,
99 1 equemene
                      difb, specr, specg, specb, shine):
100 1 equemene
   mat = [0, 0, 0, 0]
101 1 equemene
102 1 equemene
   glPushMatrix()
103 1 equemene
   glTranslatef(x, y, 0.0)
104 1 equemene
   mat[0] = ambr; mat[1] = ambg; mat[2] = ambb; mat[3] = 1.0
105 1 equemene
   glMaterialfv(GL_FRONT, GL_AMBIENT, mat)
106 1 equemene
   mat[0] = difr; mat[1] = difg; mat[2] = difb
107 1 equemene
   glMaterialfv(GL_FRONT, GL_DIFFUSE, mat)
108 1 equemene
   mat[0] = specr; mat[1] = specg; mat[2] = specb
109 1 equemene
   glMaterialfv(GL_FRONT, GL_SPECULAR, mat)
110 1 equemene
   glMaterialf(GL_FRONT, GL_SHININESS, shine * 128.0)
111 1 equemene
   # Linux GLUT seems to optimize away the actual generation of the
112 1 equemene
   # teapot on modern GLUT when using display-lists, so we have to
113 1 equemene
   # use direct calls here...
114 1 equemene
#   glCallList(teapotList)
115 1 equemene
   glutSolidTeapot(1.0)
116 1 equemene
   glPopMatrix()
117 1 equemene
118 1 equemene
119 1 equemene
#  First column:  emerald, jade, obsidian, pearl, ruby, turquoise
120 1 equemene
#  2nd column:  brass, bronze, chrome, copper, gold, silver
121 1 equemene
#  3rd column:  black, cyan, green, red, white, yellow plastic
122 1 equemene
#  4th column:  black, cyan, green, red, white, yellow rubber
123 1 equemene
def display():
124 1 equemene
    global teapotList
125 1 equemene
    if not teapotList:
126 1 equemene
        init();
127 1 equemene
    glClearColor( 1.0,1.0,1.0, 1.0 )
128 1 equemene
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
129 1 equemene
    renderTeapot(2.0, 17.0, 0.0215, 0.1745, 0.0215,
130 1 equemene
      0.07568, 0.61424, 0.07568, 0.633, 0.727811, 0.633, 0.6)
131 1 equemene
    renderTeapot(2.0, 14.0, 0.135, 0.2225, 0.1575,
132 1 equemene
      0.54, 0.89, 0.63, 0.316228, 0.316228, 0.316228, 0.1)
133 1 equemene
    renderTeapot(2.0, 11.0, 0.05375, 0.05, 0.06625,
134 1 equemene
      0.18275, 0.17, 0.22525, 0.332741, 0.328634, 0.346435, 0.3)
135 1 equemene
    renderTeapot(2.0, 8.0, 0.25, 0.20725, 0.20725,
136 1 equemene
      1, 0.829, 0.829, 0.296648, 0.296648, 0.296648, 0.088)
137 1 equemene
    renderTeapot(2.0, 5.0, 0.1745, 0.01175, 0.01175,
138 1 equemene
      0.61424, 0.04136, 0.04136, 0.727811, 0.626959, 0.626959, 0.6)
139 1 equemene
    renderTeapot(2.0, 2.0, 0.1, 0.18725, 0.1745,
140 1 equemene
      0.396, 0.74151, 0.69102, 0.297254, 0.30829, 0.306678, 0.1)
141 1 equemene
    renderTeapot(6.0, 17.0, 0.329412, 0.223529, 0.027451,
142 1 equemene
      0.780392, 0.568627, 0.113725, 0.992157, 0.941176, 0.807843,
143 1 equemene
      0.21794872);
144 1 equemene
    renderTeapot(6.0, 14.0, 0.2125, 0.1275, 0.054,
145 1 equemene
      0.714, 0.4284, 0.18144, 0.393548, 0.271906, 0.166721, 0.2)
146 1 equemene
    renderTeapot(6.0, 11.0, 0.25, 0.25, 0.25,
147 1 equemene
      0.4, 0.4, 0.4, 0.774597, 0.774597, 0.774597, 0.6)
148 1 equemene
    renderTeapot(6.0, 8.0, 0.19125, 0.0735, 0.0225,
149 1 equemene
      0.7038, 0.27048, 0.0828, 0.256777, 0.137622, 0.086014, 0.1)
150 1 equemene
    renderTeapot(6.0, 5.0, 0.24725, 0.1995, 0.0745,
151 1 equemene
      0.75164, 0.60648, 0.22648, 0.628281, 0.555802, 0.366065, 0.4)
152 1 equemene
    renderTeapot(6.0, 2.0, 0.19225, 0.19225, 0.19225,
153 1 equemene
      0.50754, 0.50754, 0.50754, 0.508273, 0.508273, 0.508273, 0.4)
154 1 equemene
    renderTeapot(10.0, 17.0, 0.0, 0.0, 0.0, 0.01, 0.01, 0.01,
155 1 equemene
      0.50, 0.50, 0.50, .25)
156 1 equemene
    renderTeapot(10.0, 14.0, 0.0, 0.1, 0.06, 0.0, 0.50980392, 0.50980392,
157 1 equemene
      0.50196078, 0.50196078, 0.50196078, .25)
158 1 equemene
    renderTeapot(10.0, 11.0, 0.0, 0.0, 0.0,
159 1 equemene
      0.1, 0.35, 0.1, 0.45, 0.55, 0.45, .25);
160 1 equemene
    renderTeapot(10.0, 8.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0,
161 1 equemene
      0.7, 0.6, 0.6, .25)
162 1 equemene
    renderTeapot(10.0, 5.0, 0.0, 0.0, 0.0, 0.55, 0.55, 0.55,
163 1 equemene
      0.70, 0.70, 0.70, .25)
164 1 equemene
    renderTeapot(10.0, 2.0, 0.0, 0.0, 0.0, 0.5, 0.5, 0.0,
165 1 equemene
      0.60, 0.60, 0.50, .25)
166 1 equemene
    renderTeapot(14.0, 17.0, 0.02, 0.02, 0.02, 0.01, 0.01, 0.01,
167 1 equemene
      0.4, 0.4, 0.4, .078125)
168 1 equemene
    renderTeapot(14.0, 14.0, 0.0, 0.05, 0.05, 0.4, 0.5, 0.5,
169 1 equemene
      0.04, 0.7, 0.7, .078125)
170 1 equemene
    renderTeapot(14.0, 11.0, 0.0, 0.05, 0.0, 0.4, 0.5, 0.4,
171 1 equemene
      0.04, 0.7, 0.04, .078125)
172 1 equemene
    renderTeapot(14.0, 8.0, 0.05, 0.0, 0.0, 0.5, 0.4, 0.4,
173 1 equemene
      0.7, 0.04, 0.04, .078125)
174 1 equemene
    renderTeapot(14.0, 5.0, 0.05, 0.05, 0.05, 0.5, 0.5, 0.5,
175 1 equemene
      0.7, 0.7, 0.7, .078125)
176 1 equemene
    renderTeapot(14.0, 2.0, 0.05, 0.05, 0.0, 0.5, 0.5, 0.4,
177 1 equemene
      0.7, 0.7, 0.04, .078125)
178 1 equemene
    glutSwapBuffers()
179 1 equemene
180 1 equemene
def reshape(w, h):
181 1 equemene
   glViewport(0, 0, w, h)
182 1 equemene
   glMatrixMode(GL_PROJECTION)
183 1 equemene
   glLoadIdentity()
184 1 equemene
   if (w <= h):
185 1 equemene
      glOrtho(0.0, 16.0, 0.0, 16.0*h/w, -10.0, 10.0)
186 1 equemene
   else:
187 1 equemene
      glOrtho(0.0, 16.0*w/h, 0.0, 16.0, -10.0, 10.0)
188 1 equemene
   glMatrixMode(GL_MODELVIEW)
189 1 equemene
190 1 equemene
191 1 equemene
def keyboard(key, x, y):
192 1 equemene
   if key == chr(27):
193 1 equemene
      sys.exit(0)
194 1 equemene
195 1 equemene
# Main Loop
196 1 equemene
if __name__ == "__main__":
197 1 equemene
        glutInit(sys.argv);
198 1 equemene
        glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
199 1 equemene
        glutInitWindowSize(500, 600);
200 1 equemene
        glutInitWindowPosition(50,50);
201 1 equemene
        glutCreateWindow(sys.argv[0]);
202 1 equemene
        glutReshapeFunc(reshape)
203 1 equemene
        glutDisplayFunc(display)
204 1 equemene
        glutKeyboardFunc(keyboard)
205 1 equemene
        glutMainLoop()