Statistics
| Revision:

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

History | View | Annotate | Download (4.3 kB)

1 1 equemene
#!/usr/bin/python
2 1 equemene
3 1 equemene
# This is statement is required by the build system to query build info
4 1 equemene
if __name__ == '__build__':
5 11 equemene
        raise(Exception)
6 1 equemene
7 1 equemene
'''
8 1 equemene
scene.c from the Redbook examples.
9 1 equemene
Converted to Python by Jason Petrone(jp@demonseed.net) 8/00
10 1 equemene

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

48 1 equemene
/*
49 1 equemene
 *  scene.c
50 1 equemene
 *  This program demonstrates the use of the GL lighting model.
51 1 equemene
 *  Objects are drawn using a grey material characteristic.
52 1 equemene
 *  A single light source illuminates the objects.
53 1 equemene
 */
54 1 equemene

55 1 equemene
'''
56 1 equemene
57 1 equemene
import sys
58 1 equemene
59 1 equemene
try:
60 1 equemene
  from OpenGL.GLUT import *
61 1 equemene
  from OpenGL.GL import *
62 1 equemene
except:
63 11 equemene
  print('''
64 1 equemene
ERROR: PyOpenGL not installed properly.
65 11 equemene
        ''')
66 1 equemene
  sys.exit()
67 1 equemene
68 1 equemene
69 1 equemene
#  Initialize material property and light source.
70 1 equemene
def init():
71 1 equemene
   light_ambient =  [0.0, 0.0, 0.0, 1.0]
72 1 equemene
   light_diffuse =  [1.0, 1.0, 1.0, 1.0]
73 1 equemene
   light_specular =  [1.0, 1.0, 1.0, 1.0]
74 1 equemene
#  light_position is NOT default value
75 1 equemene
   light_position =  [1.0, 1.0, 1.0, 0.0]
76 1 equemene
77 1 equemene
   glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient)
78 1 equemene
   glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse)
79 1 equemene
   glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular)
80 1 equemene
   glLightfv(GL_LIGHT0, GL_POSITION, light_position)
81 1 equemene
82 1 equemene
   glEnable(GL_LIGHTING)
83 1 equemene
   glEnable(GL_LIGHT0)
84 1 equemene
   glEnable(GL_DEPTH_TEST)
85 1 equemene
86 1 equemene
87 1 equemene
def display():
88 1 equemene
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
89 1 equemene
   glPushMatrix()
90 1 equemene
   glRotatef(20.0, 1.0, 0.0, 0.0)
91 1 equemene
   glPushMatrix()
92 1 equemene
   glTranslatef(-0.75, 0.5, 0.0);
93 1 equemene
   glRotatef(90.0, 1.0, 0.0, 0.0)
94 1 equemene
   glutSolidTorus(0.275, 0.85, 15, 15)
95 1 equemene
   glPopMatrix()
96 1 equemene
97 1 equemene
   glPushMatrix()
98 1 equemene
   glTranslatef(-0.75, -0.5, 0.0);
99 1 equemene
   glRotatef (270.0, 1.0, 0.0, 0.0)
100 1 equemene
   glutSolidCone(1.0, 2.0, 15, 15)
101 1 equemene
   glPopMatrix()
102 1 equemene
103 1 equemene
   glPushMatrix()
104 1 equemene
   glTranslatef(0.75, 0.0, -1.0)
105 1 equemene
   glutSolidSphere(1.0, 15, 15)
106 1 equemene
   glPopMatrix()
107 1 equemene
108 1 equemene
   glPopMatrix()
109 1 equemene
   glFlush()
110 1 equemene
111 1 equemene
112 1 equemene
def reshape(w, h):
113 1 equemene
   glViewport(0, 0, w, h)
114 1 equemene
   glMatrixMode (GL_PROJECTION)
115 1 equemene
   glLoadIdentity()
116 1 equemene
   if w <= h:
117 1 equemene
      glOrtho(-2.5, 2.5, -2.5*h/w,
118 1 equemene
               2.5*h/w, -10.0, 10.0)
119 1 equemene
   else:
120 1 equemene
      glOrtho(-2.5*w/h,
121 1 equemene
               2.5*w/h, -2.5, 2.5, -10.0, 10.0)
122 1 equemene
   glMatrixMode(GL_MODELVIEW)
123 1 equemene
   glLoadIdentity()
124 1 equemene
125 1 equemene
126 1 equemene
def keyboard(key, x, y):
127 11 equemene
   if key == str.encode(chr(27)):
128 1 equemene
       sys.exit(0)
129 1 equemene
130 1 equemene
131 1 equemene
132 1 equemene
#  Main Loop
133 1 equemene
#  Open window with initial window size, title bar,
134 1 equemene
#  RGBA display mode, and handle input events.
135 1 equemene
136 1 equemene
glutInit(sys.argv)
137 1 equemene
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH)
138 1 equemene
glutInitWindowSize (500, 500)
139 11 equemene
glutCreateWindow(b'scene')
140 1 equemene
init()
141 1 equemene
glutReshapeFunc(reshape)
142 1 equemene
glutKeyboardFunc(keyboard)
143 1 equemene
glutDisplayFunc(display)
144 1 equemene
glutMainLoop()