Statistics
| Revision:

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

 1 2 3 1 equemene #!/usr/bin/env python2.3  1 equemene # * 3-D gear wheels. This program is in the public domain.  1 equemene # * Brian Paul  1 equemene # * Conversion to GLUT by Mark J. Kilgard  1 equemene # conversion to Python using PyOpenGL with frame rates ala glxgears  1 equemene # Peter Barth  1 equemene import OpenGL  1 equemene OpenGL.ERROR_ON_COPY = True  1 equemene from OpenGL.GL import *  1 equemene from OpenGL.GLUT import *  1 equemene import sys, time  1 equemene from math import sin,cos,sqrt,pi  1 equemene from OpenGL.constants import GLfloat  1 equemene vec4 = GLfloat_4  1 equemene 1 equemene def gear(inner_radius, outer_radius, width, teeth, tooth_depth):  1 equemene  r0 = inner_radius  1 equemene  r1 = outer_radius - tooth_depth/2.0  1 equemene  r2 = outer_radius + tooth_depth/2.0  1 equemene  da = 2.0*pi / teeth / 4.0  1 equemene 1 equemene  glShadeModel(GL_FLAT)  1 equemene  glNormal3f(0.0, 0.0, 1.0)  1 equemene 1 equemene  # draw front face  1 equemene  glBegin(GL_QUAD_STRIP)  1 equemene  for i in range(teeth + 1):  1 equemene  angle = i * 2.0 * pi / teeth  1 equemene  glVertex3f(r0*cos(angle), r0*sin(angle), width*0.5)  1 equemene  glVertex3f(r1*cos(angle), r1*sin(angle), width*0.5)  1 equemene  glVertex3f(r0*cos(angle), r0*sin(angle), width*0.5)  1 equemene  glVertex3f(r1*cos(angle+3*da), r1*sin(angle+3*da), width*0.5)  1 equemene  glEnd()  1 equemene 1 equemene  # draw front sides of teeth  1 equemene  glBegin(GL_QUADS)  1 equemene  da = 2.0*pi / teeth / 4.0  1 equemene  for i in range(teeth):  1 equemene  angle = i * 2.0*pi / teeth  1 equemene  glVertex3f(r1*cos(angle), r1*sin(angle), width*0.5)  1 equemene  glVertex3f(r2*cos(angle+da), r2*sin(angle+da), width*0.5)  1 equemene  glVertex3f(r2*cos(angle+2*da), r2*sin(angle+2*da), width*0.5)  1 equemene  glVertex3f(r1*cos(angle+3*da), r1*sin(angle+3*da), width*0.5)  1 equemene  glEnd()  1 equemene 1 equemene  glNormal3f(0.0, 0.0, -1.0)  1 equemene 1 equemene  # draw back face  1 equemene  glBegin(GL_QUAD_STRIP)  1 equemene  for i in range(teeth + 1):  1 equemene  angle = i * 2.0*pi / teeth  1 equemene  glVertex3f(r1*cos(angle), r1*sin(angle), -width*0.5)  1 equemene  glVertex3f(r0*cos(angle), r0*sin(angle), -width*0.5)  1 equemene  glVertex3f(r1*cos(angle+3*da), r1*sin(angle+3*da),-width*0.5)  1 equemene  glVertex3f(r0*cos(angle), r0*sin(angle), -width*0.5)  1 equemene  glEnd()  1 equemene 1 equemene  # draw back sides of teeth  1 equemene  glBegin(GL_QUADS)  1 equemene  da = 2.0*pi / teeth / 4.0  1 equemene  for i in range(teeth):  1 equemene  angle = i * 2.0*pi / teeth  1 equemene  glVertex3f(r1*cos(angle+3*da), r1*sin(angle+3*da),-width*0.5)  1 equemene  glVertex3f(r2*cos(angle+2*da), r2*sin(angle+2*da),-width*0.5)  1 equemene  glVertex3f(r2*cos(angle+da), r2*sin(angle+da), -width*0.5)  1 equemene  glVertex3f(r1*cos(angle), r1*sin(angle), -width*0.5)  1 equemene  glEnd()  1 equemene 1 equemene  # draw outward faces of teeth  1 equemene  glBegin(GL_QUAD_STRIP);  1 equemene  for i in range(teeth):  1 equemene  angle = i * 2.0*pi / teeth  1 equemene  glVertex3f(r1*cos(angle), r1*sin(angle), width*0.5)  1 equemene  glVertex3f(r1*cos(angle), r1*sin(angle), -width*0.5)  1 equemene  u = r2*cos(angle+da) - r1*cos(angle)  1 equemene  v = r2*sin(angle+da) - r1*sin(angle)  1 equemene  len = sqrt(u*u + v*v)  1 equemene  u = u / len  1 equemene  v = v / len  1 equemene  glNormal3f(v, -u, 0.0)  1 equemene  glVertex3f(r2*cos(angle+da), r2*sin(angle+da), width*0.5)  1 equemene  glVertex3f(r2*cos(angle+da), r2*sin(angle+da), -width*0.5)  1 equemene  glNormal3f(cos(angle), sin(angle), 0.0)  1 equemene  glVertex3f(r2*cos(angle+2*da), r2*sin(angle+2*da), width*0.5)  1 equemene  glVertex3f(r2*cos(angle+2*da), r2*sin(angle+2*da),-width*0.5)  1 equemene  u = r1*cos(angle+3*da) - r2*cos(angle+2*da)  1 equemene  v = r1*sin(angle+3*da) - r2*sin(angle+2*da)  1 equemene  glNormal3f(v, -u, 0.0)  1 equemene  glVertex3f(r1*cos(angle+3*da), r1*sin(angle+3*da), width*0.5)  1 equemene  glVertex3f(r1*cos(angle+3*da), r1*sin(angle+3*da),-width*0.5)  1 equemene  glNormal3f(cos(angle), sin(angle), 0.0)  1 equemene 1 equemene  glVertex3f(r1*cos(0), r1*sin(0), width*0.5)  1 equemene  glVertex3f(r1*cos(0), r1*sin(0), -width*0.5)  1 equemene 1 equemene  glEnd()  1 equemene 1 equemene  glShadeModel(GL_SMOOTH)  1 equemene 1 equemene  # draw inside radius cylinder  1 equemene  glBegin(GL_QUAD_STRIP)  1 equemene  for i in range(teeth + 1):  1 equemene  angle = i * 2.0*pi / teeth;  1 equemene  glNormal3f(-cos(angle), -sin(angle), 0.0)  1 equemene  glVertex3f(r0*cos(angle), r0*sin(angle), -width*0.5)  1 equemene  glVertex3f(r0*cos(angle), r0*sin(angle), width*0.5)  1 equemene  glEnd()  1 equemene 1 equemene 1 equemene (view_rotx,view_roty,view_rotz)=(20.0, 30.0, 0.0)  1 equemene (gear1, gear2, gear3) = (0,0,0)  1 equemene angle = 0.0  1 equemene 1 equemene 1 equemene tStart = t0 = time.time()  1 equemene frames = 0  1 equemene rotationRate = 1.01  1 equemene 1 equemene def framerate():  1 equemene  global t0, frames  1 equemene  t = time.time()  1 equemene  frames += 1  1 equemene  if t - t0 >= 5.0:  1 equemene  seconds = t - t0  1 equemene  fps = frames/seconds  1 equemene  print "%.0f frames in %3.1f seconds = %6.3f FPS" % (frames,seconds,fps)  1 equemene  t0 = t  1 equemene  frames = 0  1 equemene 1 equemene 1 equemene def draw():  1 equemene  rotationRate = (time.time() - tStart) * 1.05  1 equemene  angle = (2 * pi) * ((time.time() - tStart)*rotationRate)# * rotationRate  1 equemene  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)  1 equemene 1 equemene  glPushMatrix()  1 equemene  glRotatef(view_rotx, 1.0, 0.0, 0.0)  1 equemene  glRotatef(view_roty, 0.0, 1.0, 0.0)  1 equemene  glRotatef(view_rotz, 0.0, 0.0, 1.0)  1 equemene 1 equemene  glPushMatrix()  1 equemene  glTranslatef(-3.0, -2.0, 0.0)  1 equemene  glRotatef(angle, 0.0, 0.0, 1.0)  1 equemene  glCallList(gear1)  1 equemene  glPopMatrix()  1 equemene 1 equemene  glPushMatrix()  1 equemene  glTranslatef(3.1, -2.0, 0.0)  1 equemene  glRotatef(-2.0*angle-9.0, 0.0, 0.0, 1.0)  1 equemene  glCallList(gear2)  1 equemene  glPopMatrix()  1 equemene 1 equemene  glPushMatrix()  1 equemene  glTranslatef(-3.1, 4.2, 0.0)  1 equemene  glRotatef(-2.0*angle-25.0, 0.0, 0.0, 1.0)  1 equemene  glCallList(gear3)  1 equemene  glPopMatrix()  1 equemene 1 equemene  glPopMatrix()  1 equemene 1 equemene  glutSwapBuffers()  1 equemene 1 equemene  framerate()  1 equemene 1 equemene def idle():  1 equemene  global angle  1 equemene  angle += 2.0  1 equemene  glutPostRedisplay()  1 equemene 1 equemene 1 equemene # change view angle, exit upon ESC  1 equemene def key(k, x, y):  1 equemene  global view_rotz  1 equemene 1 equemene  if k == 'z':  1 equemene  view_rotz += 5.0  1 equemene  elif k == 'Z':  1 equemene  view_rotz -= 5.0  1 equemene  elif ord(k) == 27: # Escape  1 equemene  sys.exit(0)  1 equemene  else:  1 equemene  return  1 equemene  glutPostRedisplay()  1 equemene 1 equemene 1 equemene # change view angle  1 equemene def special(k, x, y):  1 equemene  global view_rotx, view_roty, view_rotz  1 equemene 1 equemene  if k == GLUT_KEY_UP:  1 equemene  view_rotx += 5.0  1 equemene  elif k == GLUT_KEY_DOWN:  1 equemene  view_rotx -= 5.0  1 equemene  elif k == GLUT_KEY_LEFT:  1 equemene  view_roty += 5.0  1 equemene  elif k == GLUT_KEY_RIGHT:  1 equemene  view_roty -= 5.0  1 equemene  else:  1 equemene  return  1 equemene  glutPostRedisplay()  1 equemene 1 equemene 1 equemene # new window size or exposure  1 equemene def reshape(width, height):  1 equemene  h = float(height) / float(width);  1 equemene  glViewport(0, 0, width, height)  1 equemene  glMatrixMode(GL_PROJECTION)  1 equemene  glLoadIdentity()  1 equemene  glFrustum(-1.0, 1.0, -h, h, 5.0, 60.0)  1 equemene  glMatrixMode(GL_MODELVIEW)  1 equemene  glLoadIdentity()  1 equemene  glTranslatef(0.0, 0.0, -40.0)  1 equemene 1 equemene def init():  1 equemene  global gear1, gear2, gear3  1 equemene 1 equemene 1 equemene  pos = vec4(5.0, 5.0, 10.0, 0.0)  1 equemene  red = vec4(0.8, 0.1, 0.0, 1.0)  1 equemene  green = vec4(0.0, 0.8, 0.2, 1.0)  1 equemene  blue = vec4(0.2, 0.2, 1.0, 1.0)  1 equemene 1 equemene  glLightfv(GL_LIGHT0, GL_POSITION, pos)  1 equemene  glEnable(GL_CULL_FACE)  1 equemene  glEnable(GL_LIGHTING)  1 equemene  glEnable(GL_LIGHT0)  1 equemene  glEnable(GL_DEPTH_TEST)  1 equemene 1 equemene  # make the gears  1 equemene  gear1 = glGenLists(1)  1 equemene  glNewList(gear1, GL_COMPILE)  1 equemene  glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red)  1 equemene  gear(1.0, 4.0, 1.0, 20, 0.7)  1 equemene  glEndList()  1 equemene 1 equemene  gear2 = glGenLists(1)  1 equemene  glNewList(gear2, GL_COMPILE)  1 equemene  glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green)  1 equemene  gear(0.5, 2.0, 2.0, 10, 0.7)  1 equemene  glEndList()  1 equemene 1 equemene  gear3 = glGenLists(1)  1 equemene  glNewList(gear3, GL_COMPILE)  1 equemene  glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue)  1 equemene  gear(1.3, 2.0, 0.5, 10, 0.7)  1 equemene  glEndList()  1 equemene 1 equemene  glEnable(GL_NORMALIZE)  1 equemene 1 equemene def visible(vis):  1 equemene  if vis == GLUT_VISIBLE:  1 equemene  glutIdleFunc(idle)  1 equemene  else:  1 equemene  glutIdleFunc(None)  1 equemene 1 equemene 1 equemene if __name__ == '__main__':  1 equemene  glutInit(sys.argv)  1 equemene  glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH)  1 equemene 1 equemene  glutInitWindowPosition(0, 0)  1 equemene  glutInitWindowSize(300, 300)  1 equemene  glutCreateWindow("pyGears")  1 equemene  init()  1 equemene 1 equemene  glutDisplayFunc(draw)  1 equemene  glutReshapeFunc(reshape)  1 equemene  glutKeyboardFunc(key)  1 equemene  glutSpecialFunc(special)  1 equemene  glutVisibilityFunc(visible)  1 equemene 1 equemene  if "-info" in sys.argv:  1 equemene  print "GL_RENDERER = ", glGetString(GL_RENDERER)  1 equemene  print "GL_VERSION = ", glGetString(GL_VERSION)  1 equemene  print "GL_VENDOR = ", glGetString(GL_VENDOR)  1 equemene  print "GL_EXTENSIONS = ", glGetString(GL_EXTENSIONS)  1 equemene 1 equemene  glutMainLoop()