SIP4 python bindings for TQt
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

236 lines
6.5KB

  1. #!/usr/bin/env python
  2. import sys
  3. import math
  4. from python_tqt.qt import *
  5. from python_tqt.qtgl import *
  6. from OpenGL.GL import *
  7. def gear(inner_radius,outer_radius,width,teeth,tooth_depth):
  8. r0 = inner_radius;
  9. r1 = outer_radius - tooth_depth/2.0;
  10. r2 = outer_radius + tooth_depth/2.0;
  11. da = 2.0*math.pi/teeth/4.0;
  12. glShadeModel(GL_FLAT)
  13. glNormal3f(0.0,0.0,1.0)
  14. # draw front face
  15. glBegin(GL_QUAD_STRIP)
  16. for i in range(teeth+1):
  17. angle = i * 2.0*math.pi/teeth;
  18. glVertex3f(r0*math.cos(angle), r0*math.sin(angle), width*0.5 )
  19. glVertex3f(r1*math.cos(angle), r1*math.sin(angle), width*0.5 )
  20. glVertex3f(r0*math.cos(angle), r0*math.sin(angle), width*0.5 )
  21. glVertex3f(r1*math.cos(angle+3*da), r1*math.sin(angle+3*da), width*0.5 )
  22. glEnd()
  23. # draw front sides of teeth
  24. da = 2.0*math.pi/teeth/4.0;
  25. glBegin(GL_QUADS)
  26. for i in range(teeth):
  27. angle = i*2.0*math.pi/teeth
  28. glVertex3f( r1*math.cos(angle), r1*math.sin(angle), width*0.5 )
  29. glVertex3f( r2*math.cos(angle+da), r2*math.sin(angle+da), width*0.5 )
  30. glVertex3f( r2*math.cos(angle+2*da), r2*math.sin(angle+2*da), width*0.5 )
  31. glVertex3f( r1*math.cos(angle+3*da), r1*math.sin(angle+3*da), width*0.5 )
  32. glEnd()
  33. glNormal3f( 0.0, 0.0, -1.0 )
  34. # draw back face
  35. glBegin( GL_QUAD_STRIP );
  36. for i in range(teeth+1):
  37. angle = i*2.0*math.pi/teeth;
  38. glVertex3f( r1*math.cos(angle), r1*math.sin(angle), -width*0.5 )
  39. glVertex3f( r0*math.cos(angle), r0*math.sin(angle), -width*0.5 )
  40. glVertex3f( r1*math.cos(angle+3*da), r1*math.sin(angle+3*da), -width*0.5 )
  41. glVertex3f( r0*math.cos(angle), r0*math.sin(angle), -width*0.5 )
  42. glEnd()
  43. # draw back sides of teeth
  44. da = 2.0*math.pi/teeth/4.0
  45. glBegin( GL_QUADS )
  46. for i in range(teeth):
  47. angle = i*2.0*math.pi/teeth
  48. glVertex3f( r1*math.cos(angle+3*da), r1*math.sin(angle+3*da), -width*0.5 )
  49. glVertex3f( r2*math.cos(angle+2*da), r2*math.sin(angle+2*da), -width*0.5 )
  50. glVertex3f( r2*math.cos(angle+da), r2*math.sin(angle+da), -width*0.5 )
  51. glVertex3f( r1*math.cos(angle), r1*math.sin(angle), -width*0.5 )
  52. glEnd()
  53. # draw outward faces of teeth
  54. glBegin( GL_QUAD_STRIP )
  55. for i in range(teeth):
  56. angle = i*2.0*math.pi/teeth
  57. glVertex3f( r1*math.cos(angle), r1*math.sin(angle), width*0.5 )
  58. glVertex3f( r1*math.cos(angle), r1*math.sin(angle), -width*0.5 )
  59. u = r2*math.cos(angle+da) - r1*math.cos(angle)
  60. v = r2*math.sin(angle+da) - r1*math.sin(angle)
  61. len = math.sqrt( u*u + v*v )
  62. u /= len
  63. v /= len
  64. glNormal3f( v, -u, 0.0 )
  65. glVertex3f( r2*math.cos(angle+da), r2*math.sin(angle+da), width*0.5 )
  66. glVertex3f( r2*math.cos(angle+da), r2*math.sin(angle+da), -width*0.5 )
  67. glNormal3f( math.cos(angle), math.sin(angle), 0.0 )
  68. glVertex3f( r2*math.cos(angle+2*da), r2*math.sin(angle+2*da), width*0.5 )
  69. glVertex3f( r2*math.cos(angle+2*da), r2*math.sin(angle+2*da), -width*0.5 )
  70. u = r1*math.cos(angle+3*da) - r2*math.cos(angle+2*da)
  71. v = r1*math.sin(angle+3*da) - r2*math.sin(angle+2*da)
  72. glNormal3f( v, -u, 0.0 )
  73. glVertex3f( r1*math.cos(angle+3*da), r1*math.sin(angle+3*da), width*0.5 )
  74. glVertex3f( r1*math.cos(angle+3*da), r1*math.sin(angle+3*da), -width*0.5 )
  75. glNormal3f( math.cos(angle), math.sin(angle), 0.0 )
  76. glVertex3f( r1*math.cos(0.0), r1*math.sin(0.0), width*0.5 )
  77. glVertex3f( r1*math.cos(0.0), r1*math.sin(0.0), -width*0.5 )
  78. glEnd()
  79. glShadeModel (GL_SMOOTH)
  80. # draw inside radius cylinder
  81. glBegin( GL_QUAD_STRIP )
  82. for i in range(teeth+1):
  83. angle = i * 2.0*math.pi / teeth;
  84. glNormal3f( -math.cos(angle), -math.sin(angle), 0.0 );
  85. glVertex3f( r0*math.cos(angle), r0*math.sin(angle), -width*0.5 );
  86. glVertex3f( r0*math.cos(angle), r0*math.sin(angle), width*0.5 );
  87. glEnd()
  88. ##############################################################################
  89. class GearWidget(TQGLWidget):
  90. def __init__(self,parent=None,name=None):
  91. TQGLWidget.__init__(self,parent,name)
  92. self.angle=0.0
  93. self.view_rotx=0.0
  94. self.view_roty=0.0
  95. self.view_rotz=0.0
  96. self.startTimer(10)
  97. def timerEvent(self,event):
  98. self.updateGL()
  99. def paintGL(self):
  100. self.angle = self.angle + 2.0
  101. self.view_rotx = self.view_rotx + 1.0
  102. self.view_roty = self.view_roty + 3.0
  103. self.view_rotz = self.view_rotz + 2.0
  104. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT )
  105. glPushMatrix()
  106. glRotatef( self.view_rotx, 1.0, 0.0, 0.0 )
  107. glRotatef( self.view_roty, 0.0, 1.0, 0.0 )
  108. glRotatef( self.view_rotz, 0.0, 0.0, 1.0 )
  109. glPushMatrix()
  110. glTranslatef( -3.0, -2.0, 0.0 )
  111. glRotatef( self.angle, 0.0, 0.0, 1.0 )
  112. glCallList(self.gear1)
  113. glPopMatrix()
  114. glPushMatrix()
  115. glTranslatef( 3.1, -2.0, 0.0 )
  116. glRotatef( -2.0*self.angle-9.0, 0.0, 0.0, 1.0 )
  117. glCallList(self.gear2)
  118. glPopMatrix()
  119. glPushMatrix()
  120. glTranslatef( -3.1, 2.2, -1.8 )
  121. glRotatef( 90.0, 1.0, 0.0, 0.0 )
  122. glRotatef( 2.0*self.angle-2.0, 0.0, 0.0, 1.0 )
  123. glCallList(self.gear3)
  124. glPopMatrix()
  125. glPopMatrix()
  126. def resizeGL(self,width,height):
  127. w = width / float(height)
  128. h = 1.0
  129. glViewport( 0, 0, width, height )
  130. glMatrixMode(GL_PROJECTION)
  131. glLoadIdentity()
  132. glFrustum( -w, w, -h, h, 5.0, 60.0 )
  133. glMatrixMode(GL_MODELVIEW)
  134. glLoadIdentity()
  135. glTranslatef( 0.0, 0.0, -40.0 )
  136. def initializeGL(self):
  137. pos=(5.0, 5.0, 10.0, 1.0 )
  138. ared=(0.8, 0.1, 0.0, 1.0 )
  139. agreen=(0.0, 0.8, 0.2, 1.0 )
  140. ablue=(0.2, 0.2, 1.0, 1.0 )
  141. glLightfv(GL_LIGHT0,GL_POSITION,pos)
  142. glEnable(GL_CULL_FACE)
  143. glEnable(GL_LIGHTING)
  144. glEnable(GL_LIGHT0)
  145. glEnable(GL_DEPTH_TEST)
  146. self.gear1=glGenLists(1)
  147. glNewList(self.gear1,GL_COMPILE)
  148. glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,ared)
  149. gear(1.0,4.0,1.0,20,0.7)
  150. glEndList()
  151. self.gear2=glGenLists(1)
  152. glNewList(self.gear2,GL_COMPILE)
  153. glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,agreen)
  154. gear(0.5,2.0,2.0,10,0.7)
  155. glEndList()
  156. self.gear3=glGenLists(1)
  157. glNewList(self.gear3,GL_COMPILE)
  158. glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,ablue)
  159. gear(1.3,2.0,0.5,10,0.7)
  160. glEndList()
  161. glEnable(GL_NORMALIZE)
  162. ##############################################################################
  163. if __name__=='__main__':
  164. TQApplication.setColorSpec(TQApplication.CustomColor)
  165. app=TQApplication(sys.argv)
  166. if not TQGLFormat.hasOpenGL():
  167. raise 'No TQt OpenGL support.'
  168. widget=GearWidget()
  169. app.setMainWidget(widget)
  170. widget.show()
  171. app.exec_loop()