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