Statistics
| Revision:

root / PyOpenGL-Demo / GLUT / gears.py @ 1

History | View | Annotate | Download (7.6 kB)

1 1 equemene
#!/usr/bin/env python2.3
2 1 equemene
# * 3-D gear wheels.  This program is in the public domain.
3 1 equemene
# * Brian Paul
4 1 equemene
# * Conversion to GLUT by Mark J. Kilgard
5 1 equemene
# conversion to Python using PyOpenGL with frame rates ala glxgears
6 1 equemene
# Peter Barth
7 1 equemene
import OpenGL
8 1 equemene
OpenGL.ERROR_ON_COPY = True
9 1 equemene
from OpenGL.GL import *
10 1 equemene
from OpenGL.GLUT import *
11 1 equemene
import sys, time
12 1 equemene
from math import sin,cos,sqrt,pi
13 1 equemene
from OpenGL.constants import GLfloat
14 1 equemene
vec4 = GLfloat_4
15 1 equemene
16 1 equemene
def gear(inner_radius, outer_radius, width, teeth, tooth_depth):
17 1 equemene
    r0 = inner_radius
18 1 equemene
    r1 = outer_radius - tooth_depth/2.0
19 1 equemene
    r2 = outer_radius + tooth_depth/2.0
20 1 equemene
    da = 2.0*pi / teeth / 4.0
21 1 equemene
22 1 equemene
    glShadeModel(GL_FLAT)
23 1 equemene
    glNormal3f(0.0, 0.0, 1.0)
24 1 equemene
25 1 equemene
    # draw front face
26 1 equemene
    glBegin(GL_QUAD_STRIP)
27 1 equemene
    for i in range(teeth + 1):
28 1 equemene
        angle = i * 2.0 * pi / teeth
29 1 equemene
        glVertex3f(r0*cos(angle), r0*sin(angle), width*0.5)
30 1 equemene
        glVertex3f(r1*cos(angle), r1*sin(angle), width*0.5)
31 1 equemene
        glVertex3f(r0*cos(angle), r0*sin(angle), width*0.5)
32 1 equemene
        glVertex3f(r1*cos(angle+3*da), r1*sin(angle+3*da), width*0.5)
33 1 equemene
    glEnd()
34 1 equemene
35 1 equemene
    # draw front sides of teeth
36 1 equemene
    glBegin(GL_QUADS)
37 1 equemene
    da = 2.0*pi / teeth / 4.0
38 1 equemene
    for i in range(teeth):
39 1 equemene
        angle = i * 2.0*pi / teeth
40 1 equemene
        glVertex3f(r1*cos(angle),      r1*sin(angle),      width*0.5)
41 1 equemene
        glVertex3f(r2*cos(angle+da),   r2*sin(angle+da),   width*0.5)
42 1 equemene
        glVertex3f(r2*cos(angle+2*da), r2*sin(angle+2*da), width*0.5)
43 1 equemene
        glVertex3f(r1*cos(angle+3*da), r1*sin(angle+3*da), width*0.5)
44 1 equemene
    glEnd()
45 1 equemene
46 1 equemene
    glNormal3f(0.0, 0.0, -1.0)
47 1 equemene
48 1 equemene
    # draw back face
49 1 equemene
    glBegin(GL_QUAD_STRIP)
50 1 equemene
    for i in range(teeth + 1):
51 1 equemene
        angle = i * 2.0*pi / teeth
52 1 equemene
        glVertex3f(r1*cos(angle), r1*sin(angle), -width*0.5)
53 1 equemene
        glVertex3f(r0*cos(angle), r0*sin(angle), -width*0.5)
54 1 equemene
        glVertex3f(r1*cos(angle+3*da), r1*sin(angle+3*da),-width*0.5)
55 1 equemene
        glVertex3f(r0*cos(angle), r0*sin(angle), -width*0.5)
56 1 equemene
    glEnd()
57 1 equemene
58 1 equemene
    # draw back sides of teeth
59 1 equemene
    glBegin(GL_QUADS)
60 1 equemene
    da = 2.0*pi / teeth / 4.0
61 1 equemene
    for i in range(teeth):
62 1 equemene
        angle = i * 2.0*pi / teeth
63 1 equemene
        glVertex3f(r1*cos(angle+3*da), r1*sin(angle+3*da),-width*0.5)
64 1 equemene
        glVertex3f(r2*cos(angle+2*da), r2*sin(angle+2*da),-width*0.5)
65 1 equemene
        glVertex3f(r2*cos(angle+da),   r2*sin(angle+da),  -width*0.5)
66 1 equemene
        glVertex3f(r1*cos(angle),      r1*sin(angle),     -width*0.5)
67 1 equemene
    glEnd()
68 1 equemene
69 1 equemene
    # draw outward faces of teeth
70 1 equemene
    glBegin(GL_QUAD_STRIP);
71 1 equemene
    for i in range(teeth):
72 1 equemene
        angle = i * 2.0*pi / teeth
73 1 equemene
        glVertex3f(r1*cos(angle), r1*sin(angle),  width*0.5)
74 1 equemene
        glVertex3f(r1*cos(angle), r1*sin(angle), -width*0.5)
75 1 equemene
        u = r2*cos(angle+da) - r1*cos(angle)
76 1 equemene
        v = r2*sin(angle+da) - r1*sin(angle)
77 1 equemene
        len = sqrt(u*u + v*v)
78 1 equemene
        u = u / len
79 1 equemene
        v = v / len
80 1 equemene
        glNormal3f(v, -u, 0.0)
81 1 equemene
        glVertex3f(r2*cos(angle+da),   r2*sin(angle+da),   width*0.5)
82 1 equemene
        glVertex3f(r2*cos(angle+da),   r2*sin(angle+da),  -width*0.5)
83 1 equemene
        glNormal3f(cos(angle), sin(angle), 0.0)
84 1 equemene
        glVertex3f(r2*cos(angle+2*da), r2*sin(angle+2*da), width*0.5)
85 1 equemene
        glVertex3f(r2*cos(angle+2*da), r2*sin(angle+2*da),-width*0.5)
86 1 equemene
        u = r1*cos(angle+3*da) - r2*cos(angle+2*da)
87 1 equemene
        v = r1*sin(angle+3*da) - r2*sin(angle+2*da)
88 1 equemene
        glNormal3f(v, -u, 0.0)
89 1 equemene
        glVertex3f(r1*cos(angle+3*da), r1*sin(angle+3*da), width*0.5)
90 1 equemene
        glVertex3f(r1*cos(angle+3*da), r1*sin(angle+3*da),-width*0.5)
91 1 equemene
        glNormal3f(cos(angle), sin(angle), 0.0)
92 1 equemene
93 1 equemene
    glVertex3f(r1*cos(0), r1*sin(0), width*0.5)
94 1 equemene
    glVertex3f(r1*cos(0), r1*sin(0), -width*0.5)
95 1 equemene
96 1 equemene
    glEnd()
97 1 equemene
98 1 equemene
    glShadeModel(GL_SMOOTH)
99 1 equemene
100 1 equemene
    # draw inside radius cylinder
101 1 equemene
    glBegin(GL_QUAD_STRIP)
102 1 equemene
    for i in range(teeth + 1):
103 1 equemene
        angle = i * 2.0*pi / teeth;
104 1 equemene
        glNormal3f(-cos(angle), -sin(angle), 0.0)
105 1 equemene
        glVertex3f(r0*cos(angle), r0*sin(angle), -width*0.5)
106 1 equemene
        glVertex3f(r0*cos(angle), r0*sin(angle), width*0.5)
107 1 equemene
    glEnd()
108 1 equemene
109 1 equemene
110 1 equemene
(view_rotx,view_roty,view_rotz)=(20.0, 30.0, 0.0)
111 1 equemene
(gear1, gear2, gear3) = (0,0,0)
112 1 equemene
angle = 0.0
113 1 equemene
114 1 equemene
115 1 equemene
tStart = t0 = time.time()
116 1 equemene
frames = 0
117 1 equemene
rotationRate = 1.01
118 1 equemene
119 1 equemene
def framerate():
120 1 equemene
    global t0, frames
121 1 equemene
    t = time.time()
122 1 equemene
    frames += 1
123 1 equemene
    if t - t0 >= 5.0:
124 1 equemene
        seconds = t - t0
125 1 equemene
        fps = frames/seconds
126 1 equemene
        print "%.0f frames in %3.1f seconds = %6.3f FPS" % (frames,seconds,fps)
127 1 equemene
        t0 = t
128 1 equemene
        frames = 0
129 1 equemene
130 1 equemene
131 1 equemene
def draw():
132 1 equemene
    rotationRate = (time.time() - tStart) * 1.05
133 1 equemene
    angle = (2 * pi) * ((time.time() - tStart)*rotationRate)# * rotationRate
134 1 equemene
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
135 1 equemene
136 1 equemene
    glPushMatrix()
137 1 equemene
    glRotatef(view_rotx, 1.0, 0.0, 0.0)
138 1 equemene
    glRotatef(view_roty, 0.0, 1.0, 0.0)
139 1 equemene
    glRotatef(view_rotz, 0.0, 0.0, 1.0)
140 1 equemene
141 1 equemene
    glPushMatrix()
142 1 equemene
    glTranslatef(-3.0, -2.0, 0.0)
143 1 equemene
    glRotatef(angle, 0.0, 0.0, 1.0)
144 1 equemene
    glCallList(gear1)
145 1 equemene
    glPopMatrix()
146 1 equemene
147 1 equemene
    glPushMatrix()
148 1 equemene
    glTranslatef(3.1, -2.0, 0.0)
149 1 equemene
    glRotatef(-2.0*angle-9.0, 0.0, 0.0, 1.0)
150 1 equemene
    glCallList(gear2)
151 1 equemene
    glPopMatrix()
152 1 equemene
153 1 equemene
    glPushMatrix()
154 1 equemene
    glTranslatef(-3.1, 4.2, 0.0)
155 1 equemene
    glRotatef(-2.0*angle-25.0, 0.0, 0.0, 1.0)
156 1 equemene
    glCallList(gear3)
157 1 equemene
    glPopMatrix()
158 1 equemene
159 1 equemene
    glPopMatrix()
160 1 equemene
161 1 equemene
    glutSwapBuffers()
162 1 equemene
163 1 equemene
    framerate()
164 1 equemene
165 1 equemene
def idle():
166 1 equemene
    global angle
167 1 equemene
    angle += 2.0
168 1 equemene
    glutPostRedisplay()
169 1 equemene
170 1 equemene
171 1 equemene
# change view angle, exit upon ESC
172 1 equemene
def key(k, x, y):
173 1 equemene
    global view_rotz
174 1 equemene
175 1 equemene
    if k == 'z':
176 1 equemene
        view_rotz += 5.0
177 1 equemene
    elif k == 'Z':
178 1 equemene
        view_rotz -= 5.0
179 1 equemene
    elif ord(k) == 27: # Escape
180 1 equemene
        sys.exit(0)
181 1 equemene
    else:
182 1 equemene
        return
183 1 equemene
    glutPostRedisplay()
184 1 equemene
185 1 equemene
186 1 equemene
# change view angle
187 1 equemene
def special(k, x, y):
188 1 equemene
    global view_rotx, view_roty, view_rotz
189 1 equemene
190 1 equemene
    if k == GLUT_KEY_UP:
191 1 equemene
        view_rotx += 5.0
192 1 equemene
    elif k == GLUT_KEY_DOWN:
193 1 equemene
        view_rotx -= 5.0
194 1 equemene
    elif k == GLUT_KEY_LEFT:
195 1 equemene
        view_roty += 5.0
196 1 equemene
    elif k == GLUT_KEY_RIGHT:
197 1 equemene
        view_roty -= 5.0
198 1 equemene
    else:
199 1 equemene
        return
200 1 equemene
    glutPostRedisplay()
201 1 equemene
202 1 equemene
203 1 equemene
# new window size or exposure
204 1 equemene
def reshape(width, height):
205 1 equemene
    h = float(height) / float(width);
206 1 equemene
    glViewport(0, 0, width, height)
207 1 equemene
    glMatrixMode(GL_PROJECTION)
208 1 equemene
    glLoadIdentity()
209 1 equemene
    glFrustum(-1.0, 1.0, -h, h, 5.0, 60.0)
210 1 equemene
    glMatrixMode(GL_MODELVIEW)
211 1 equemene
    glLoadIdentity()
212 1 equemene
    glTranslatef(0.0, 0.0, -40.0)
213 1 equemene
214 1 equemene
def init():
215 1 equemene
    global gear1, gear2, gear3
216 1 equemene
217 1 equemene
218 1 equemene
    pos = vec4(5.0, 5.0, 10.0, 0.0)
219 1 equemene
    red = vec4(0.8, 0.1, 0.0, 1.0)
220 1 equemene
    green = vec4(0.0, 0.8, 0.2, 1.0)
221 1 equemene
    blue = vec4(0.2, 0.2, 1.0, 1.0)
222 1 equemene
223 1 equemene
    glLightfv(GL_LIGHT0, GL_POSITION, pos)
224 1 equemene
    glEnable(GL_CULL_FACE)
225 1 equemene
    glEnable(GL_LIGHTING)
226 1 equemene
    glEnable(GL_LIGHT0)
227 1 equemene
    glEnable(GL_DEPTH_TEST)
228 1 equemene
229 1 equemene
    # make the gears
230 1 equemene
    gear1 = glGenLists(1)
231 1 equemene
    glNewList(gear1, GL_COMPILE)
232 1 equemene
    glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red)
233 1 equemene
    gear(1.0, 4.0, 1.0, 20, 0.7)
234 1 equemene
    glEndList()
235 1 equemene
236 1 equemene
    gear2 = glGenLists(1)
237 1 equemene
    glNewList(gear2, GL_COMPILE)
238 1 equemene
    glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green)
239 1 equemene
    gear(0.5, 2.0, 2.0, 10, 0.7)
240 1 equemene
    glEndList()
241 1 equemene
242 1 equemene
    gear3 = glGenLists(1)
243 1 equemene
    glNewList(gear3, GL_COMPILE)
244 1 equemene
    glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue)
245 1 equemene
    gear(1.3, 2.0, 0.5, 10, 0.7)
246 1 equemene
    glEndList()
247 1 equemene
248 1 equemene
    glEnable(GL_NORMALIZE)
249 1 equemene
250 1 equemene
def visible(vis):
251 1 equemene
    if vis == GLUT_VISIBLE:
252 1 equemene
        glutIdleFunc(idle)
253 1 equemene
    else:
254 1 equemene
        glutIdleFunc(None)
255 1 equemene
256 1 equemene
257 1 equemene
if __name__ == '__main__':
258 1 equemene
    glutInit(sys.argv)
259 1 equemene
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH)
260 1 equemene
261 1 equemene
    glutInitWindowPosition(0, 0)
262 1 equemene
    glutInitWindowSize(300, 300)
263 1 equemene
    glutCreateWindow("pyGears")
264 1 equemene
    init()
265 1 equemene
266 1 equemene
    glutDisplayFunc(draw)
267 1 equemene
    glutReshapeFunc(reshape)
268 1 equemene
    glutKeyboardFunc(key)
269 1 equemene
    glutSpecialFunc(special)
270 1 equemene
    glutVisibilityFunc(visible)
271 1 equemene
272 1 equemene
    if "-info" in sys.argv:
273 1 equemene
        print "GL_RENDERER   = ", glGetString(GL_RENDERER)
274 1 equemene
        print "GL_VERSION    = ", glGetString(GL_VERSION)
275 1 equemene
        print "GL_VENDOR     = ", glGetString(GL_VENDOR)
276 1 equemene
        print "GL_EXTENSIONS = ", glGetString(GL_EXTENSIONS)
277 1 equemene
278 1 equemene
    glutMainLoop()