summaryrefslogtreecommitdiffstats
path: root/examples/gears.py
blob: e98df55e9922644b874178daa3378d953464e33e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/usr/bin/env python


import sys
import math
from python_tqt.qt import *
from python_tqt.qtgl import *
from OpenGL.GL import *

def gear(inner_radius,outer_radius,width,teeth,tooth_depth):
  r0 = inner_radius
  r1 = outer_radius - tooth_depth/2.0
  r2 = outer_radius + tooth_depth/2.0

  da = 2.0*math.pi/teeth/4.0

  glShadeModel(GL_FLAT)

  glNormal3f(0.0,0.0,1.0)

  # draw front face

  glBegin(GL_QUAD_STRIP)

  for i in range(teeth+1):
    angle = i * 2.0*math.pi/teeth
    glVertex3f(r0*math.cos(angle),      r0*math.sin(angle),      width*0.5 )
    glVertex3f(r1*math.cos(angle),      r1*math.sin(angle),      width*0.5 )
    glVertex3f(r0*math.cos(angle),      r0*math.sin(angle),      width*0.5 )
    glVertex3f(r1*math.cos(angle+3*da), r1*math.sin(angle+3*da), width*0.5 )

  glEnd()

  # draw front sides of teeth

  da = 2.0*math.pi/teeth/4.0

  glBegin(GL_QUADS)

  for i in range(teeth):
    angle = i*2.0*math.pi/teeth

    glVertex3f( r1*math.cos(angle),      r1*math.sin(angle),      width*0.5 )
    glVertex3f( r2*math.cos(angle+da),   r2*math.sin(angle+da),   width*0.5 )
    glVertex3f( r2*math.cos(angle+2*da), r2*math.sin(angle+2*da), width*0.5 )
    glVertex3f( r1*math.cos(angle+3*da), r1*math.sin(angle+3*da), width*0.5 )

  glEnd()


  glNormal3f( 0.0, 0.0, -1.0 )

  # draw back face

  glBegin( GL_QUAD_STRIP )

  for i in range(teeth+1):
    angle = i*2.0*math.pi/teeth

    glVertex3f( r1*math.cos(angle),      r1*math.sin(angle),      -width*0.5 )
    glVertex3f( r0*math.cos(angle),      r0*math.sin(angle),      -width*0.5 )
    glVertex3f( r1*math.cos(angle+3*da), r1*math.sin(angle+3*da), -width*0.5 )
    glVertex3f( r0*math.cos(angle),      r0*math.sin(angle),      -width*0.5 )

  glEnd()

  # draw back sides of teeth

  da = 2.0*math.pi/teeth/4.0

  glBegin( GL_QUADS )

  for i in range(teeth):
    angle = i*2.0*math.pi/teeth

    glVertex3f( r1*math.cos(angle+3*da), r1*math.sin(angle+3*da), -width*0.5 )
    glVertex3f( r2*math.cos(angle+2*da), r2*math.sin(angle+2*da), -width*0.5 )
    glVertex3f( r2*math.cos(angle+da),   r2*math.sin(angle+da),   -width*0.5 )
    glVertex3f( r1*math.cos(angle),      r1*math.sin(angle),      -width*0.5 )

  glEnd()

  # draw outward faces of teeth

  glBegin( GL_QUAD_STRIP )

  for i in range(teeth):
    angle = i*2.0*math.pi/teeth

    glVertex3f( r1*math.cos(angle), r1*math.sin(angle),  width*0.5 )
    glVertex3f( r1*math.cos(angle), r1*math.sin(angle), -width*0.5 )

    u = r2*math.cos(angle+da) - r1*math.cos(angle)
    v = r2*math.sin(angle+da) - r1*math.sin(angle)

    len = math.sqrt( u*u + v*v )
    u /= len
    v /= len
    glNormal3f( v, -u, 0.0 )
    glVertex3f( r2*math.cos(angle+da), r2*math.sin(angle+da),  width*0.5 )
    glVertex3f( r2*math.cos(angle+da), r2*math.sin(angle+da), -width*0.5 )
    glNormal3f( math.cos(angle), math.sin(angle), 0.0 )
    glVertex3f( r2*math.cos(angle+2*da), r2*math.sin(angle+2*da),  width*0.5 )
    glVertex3f( r2*math.cos(angle+2*da), r2*math.sin(angle+2*da), -width*0.5 )
    u = r1*math.cos(angle+3*da) - r2*math.cos(angle+2*da)
    v = r1*math.sin(angle+3*da) - r2*math.sin(angle+2*da)
    glNormal3f( v, -u, 0.0 )
    glVertex3f( r1*math.cos(angle+3*da), r1*math.sin(angle+3*da),  width*0.5 )
    glVertex3f( r1*math.cos(angle+3*da), r1*math.sin(angle+3*da), -width*0.5 )
    glNormal3f( math.cos(angle), math.sin(angle), 0.0 )

  glVertex3f( r1*math.cos(0.0), r1*math.sin(0.0), width*0.5 )
  glVertex3f( r1*math.cos(0.0), r1*math.sin(0.0), -width*0.5 )

  glEnd()

  glShadeModel (GL_SMOOTH)

  # draw inside radius cylinder

  glBegin( GL_QUAD_STRIP )

  for i in range(teeth+1):
    angle = i * 2.0*math.pi / teeth
    glNormal3f( -math.cos(angle),   -math.sin(angle),   0.0 )
    glVertex3f( r0*math.cos(angle), r0*math.sin(angle), -width*0.5 )
    glVertex3f( r0*math.cos(angle), r0*math.sin(angle),  width*0.5 )

  glEnd()

##############################################################################
class GearWidget(TQGLWidget):
  def __init__(self,parent=None,name=None):
    TQGLWidget.__init__(self,parent,name)

    self.angle=0.0
    self.view_rotx=0.0
    self.view_roty=0.0
    self.view_rotz=0.0

    self.startTimer(10)

  def timerEvent(self,event):
    self.updateGL()

  def paintGL(self):
    self.angle = self.angle + 2.0
    self.view_rotx = self.view_rotx + 1.0
    self.view_roty = self.view_roty + 3.0
    self.view_rotz = self.view_rotz + 2.0

    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT )

    glPushMatrix()
    glRotatef( self.view_rotx, 1.0, 0.0, 0.0 )
    glRotatef( self.view_roty, 0.0, 1.0, 0.0 )
    glRotatef( self.view_rotz, 0.0, 0.0, 1.0 )

    glPushMatrix()
    glTranslatef( -3.0, -2.0, 0.0 )
    glRotatef( self.angle, 0.0, 0.0, 1.0 )
    glCallList(self.gear1)
    glPopMatrix()

    glPushMatrix()
    glTranslatef( 3.1, -2.0, 0.0 )
    glRotatef( -2.0*self.angle-9.0, 0.0, 0.0, 1.0 )
    glCallList(self.gear2)
    glPopMatrix()

    glPushMatrix()
    glTranslatef( -3.1, 2.2, -1.8 )
    glRotatef( 90.0, 1.0, 0.0, 0.0 )
    glRotatef( 2.0*self.angle-2.0, 0.0, 0.0, 1.0 )
    glCallList(self.gear3)
    glPopMatrix()

    glPopMatrix()

  def resizeGL(self,width,height):
    w = width / float(height)
    h = 1.0

    glViewport( 0, 0, width, height )
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    glFrustum( -w, w, -h, h, 5.0, 60.0 )
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()
    glTranslatef( 0.0, 0.0, -40.0 )

  def initializeGL(self):
    pos=(5.0, 5.0, 10.0, 1.0 )
    ared=(0.8, 0.1, 0.0, 1.0 )
    agreen=(0.0, 0.8, 0.2, 1.0 )
    ablue=(0.2, 0.2, 1.0, 1.0 )

    glLightfv(GL_LIGHT0,GL_POSITION,pos)
    glEnable(GL_CULL_FACE)
    glEnable(GL_LIGHTING)
    glEnable(GL_LIGHT0)
    glEnable(GL_DEPTH_TEST)

    self.gear1=glGenLists(1)
    glNewList(self.gear1,GL_COMPILE)
    glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,ared)
    gear(1.0,4.0,1.0,20,0.7)
    glEndList()

    self.gear2=glGenLists(1)
    glNewList(self.gear2,GL_COMPILE)
    glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,agreen)
    gear(0.5,2.0,2.0,10,0.7)
    glEndList()

    self.gear3=glGenLists(1)
    glNewList(self.gear3,GL_COMPILE)
    glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,ablue)
    gear(1.3,2.0,0.5,10,0.7)
    glEndList()

    glEnable(GL_NORMALIZE)

##############################################################################
if __name__=='__main__':
  TQApplication.setColorSpec(TQApplication.CustomColor)
  app=TQApplication(sys.argv)

  if not TQGLFormat.hasOpenGL():
    raise Exception('No TQt OpenGL support.')

  widget=GearWidget()
  app.setMainWidget(widget)
  widget.show()
  app.exec_loop()