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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
| ' Gambas module file
' GambasGears ' Released under GPL v2 or later ' aka glxgears for gambas :) ' based on gears.c by Brian Paul / Mark J. Kilgard ' ' Code : Bodard Fabien & Carlier Laurent '
PRIVATE screen AS NEW Window(TRUE) AS "Screen" PRIVATE gearlists AS INTEGER
PRIVATE angle AS FLOAT PRIVATE Frames AS INTEGER PRIVATE fTime AS FLOAT
PUBLIC SUB Main()
WITH screen '.Resizable = True .Resize(480, 480) .Show() END WITH
fTime = Timer
END
PUBLIC SUB Screen_Open()
DIM red AS Float[] DIM green AS Float[] DIM blue AS Float[] DIM pos AS Float[]
red = [0.8, 0.1, 0.0, 0.2] green = [0.0, 0.8, 0.2, 0.8] blue = [0.1, 0.1, 0.8, 1.0]
' we enable lights, depth test, cull face Gl.Lightfv(Gl.LIGHT0, Gl.POSITION, [5.0, 5.0, 10.0, 0.0]) Gl.Enable(Gl.CULL_FACE) Gl.Enable(Gl.LIGHTING) Gl.Enable(Gl.LIGHT0) Gl.Enable(Gl.DEPTH_TEST) Gl.ClearDepth(1.0)
' We need 3 displaylists for the 3 gears gearlists = Gl.GenLists(3)
Gl.NewList(gearlists, Gl.COMPILE) Gl.Materialfv(Gl.FRONT, Gl.AMBIENT_AND_DIFFUSE, red) Gear(1.0, 4.0, 1.0, 20, 0.7) Gl.EndList() Gl.NewList(gearlists + 1, Gl.COMPILE) Gl.Materialfv(Gl.FRONT, Gl.AMBIENT_AND_DIFFUSE, green) Gear(0.5, 2.0, 2.0, 10, 0.7) Gl.EndList() Gl.NewList(gearlists + 2, Gl.COMPILE) Gl.Materialfv(Gl.FRONT, Gl.AMBIENT_AND_DIFFUSE, blue) Gear(1.3, 2.0, 0.5, 10, 0.7) Gl.EndList() Gl.Enable(Gl.NORMALIZE)
END
PUBLIC SUB Screen_Resize()
Gl.Viewport(0, 0, Screen.Width, Screen.Height) Gl.MatrixMode(Gl.PROJECTION) Gl.LoadIdentity() Gl.Frustum(-1.0, 1.0, -(Screen.Height / Screen.Width), (Screen.Height / Screen.Width), 5.0, 60.0) Gl.MatrixMode(Gl.MODELVIEW) Gl.LoadIdentity() Gl.Translatef(0.0, 0.0, -40.0)
END
PUBLIC SUB Screen_Draw()
DIM calc AS FLOAT
angle += 0.05
Gl.Clear(Gl.COLOR_BUFFER_BIT OR Gl.DEPTH_BUFFER_BIT)
Gl.PushMatrix()
Gl.Rotatef(20, 1.0, 0.0, 0.0) Gl.Rotatef(30, 0.0, 1.0, 0.0) Gl.Rotatef(0, 0.0, 0.0, 1.0)
Gl.PushMatrix() Gl.Translatef(-3.0, -2.0, 0.0) Gl.Rotatef(angle, 0.0, 0.0, 1.0) Gl.CallList(gearlists) Gl.PopMatrix()
Gl.PushMatrix() Gl.Translatef(3.1, -2.0, 0.0) Gl.Rotatef((-2.0 * angle) - 9.0, 0.0, 0.0, 1.0) Gl.CallList(gearlists + 1) Gl.PopMatrix()
Gl.PushMatrix() Gl.Translatef(-3.1, 4.2, 0.0) Gl.Rotatef((-2.0 * angle) - 25.0, 0.0, 0.0, 1.0) Gl.CallList(gearlists + 2) Gl.PopMatrix()
Gl.PopMatrix()
IF (Timer > (fTime + 1)) THEN INC fTime PRINT Screen.Framerate; " " & ("FPS") ENDIF
END
PUBLIC SUB Screen_Close()
Gl.DeleteLists(gearLists, 3)
END
PUBLIC SUB Screen_KeyPress()
IF (key.code = key.F1) THEN Screen.Fullscreen = NOT Screen.Fullscreen IF (key.Code = key.Esc) THEN Screen.Close()
END
PUBLIC SUB Screen_MouseMove()
IF Mouse.Button = 0 THEN RETURN
Gl.Rotatef(Mouse.StartY - Mouse.Y, 0, 0, 1) Gl.Rotatef(Mouse.StartX - Mouse.X, 1, 0, 0)
END
PUBLIC SUB Gear(inner_radius AS FLOAT, outer_radius AS FLOAT, width AS FLOAT, teeth AS INTEGER, tooth_depth AS FLOAT)
DIM i AS INTEGER DIM r0 AS FLOAT DIM r1 AS FLOAT DIM r2 AS FLOAT DIM angle AS FLOAT DIM da AS FLOAT DIM u AS FLOAT DIM v AS FLOAT DIM fLen AS FLOAT
r0 = inner_radius r1 = outer_radius - tooth_depth / 2.0 r2 = outer_radius + tooth_depth / 2.0
da = 2.0 * Pi / teeth / 4.0
Gl.ShadeModel(Gl.FLAT) Gl.Normal3f(0.0, 0.0, 1.0)
' Draw front face Gl.Begin(Gl.QUAD_STRIP) FOR i = 0 TO teeth angle = i * 2.0 * Pi / teeth Gl.Vertexf(r0 * Cos(angle), r0 * Sin(angle), width * 0.5) Gl.Vertexf(r1 * Cos(angle), r1 * Sin(angle), width * 0.5) IF i < teeth THEN Gl.Vertexf(r0 * Cos(angle), r0 * Sin(angle), width * 0.5) Gl.Vertexf(r1 * Cos(angle + 3 * da), r1 * Sin(angle + 3 * da), width * 0.5) ENDIF NEXT Gl.End()
' Draw front sides of teeth Gl.Begin(Gl.QUADS) da = 2.0 * Pi / teeth / 4.0 FOR i = 0 TO teeth - 1 angle = i * 2.0 * Pi / teeth Gl.Vertexf(r1 * Cos(angle), r1 * Sin(angle), width * 0.5) Gl.Vertexf(r2 * Cos(angle + da), r2 * Sin(angle + da), width * 0.5) Gl.Vertexf(r2 * Cos(angle + 2 * da), r2 * Sin(angle + 2 * da), width * 0.5) Gl.Vertexf(r1 * Cos(angle + 3 * da), r1 * Sin(angle + 3 * da), width * 0.5) NEXT Gl.End()
Gl.Normal3f(0.0, 0.0, -1.0)
' Draw back face Gl.Begin(Gl.QUAD_STRIP) FOR i = 0 TO teeth angle = i * 2.0 * Pi / teeth Gl.Vertexf(r1 * Cos(angle), r1 * Sin(angle), -width * 0.5) Gl.Vertexf(r0 * Cos(angle), r0 * Sin(angle), -width * 0.5) IF i < teeth THEN Gl.Vertexf(r1 * Cos(angle + 3 * da), r1 * Sin(angle + 3 * da), -width * 0.5) Gl.Vertexf(r0 * Cos(angle), r0 * Sin(angle), -width * 0.5) ENDIF NEXT Gl.End()
' Draw back sides of teeth Gl.Begin(Gl.QUADS) da = 2.0 * Pi / teeth / 4.0 FOR i = 0 TO teeth - 1 angle = i * 2.0 * Pi / teeth Gl.Vertexf(r1 * Cos(angle + 3 * da), r1 * Sin(angle + 3 * da), -width * 0.5) Gl.Vertexf(r2 * Cos(angle + 2 * da), r2 * Sin(angle + 2 * da), -width * 0.5) Gl.Vertexf(r2 * Cos(angle + da), r2 * Sin(angle + da), -width * 0.5) Gl.Vertexf(r1 * Cos(angle), r1 * Sin(angle), -width * 0.5) NEXT Gl.End()
' Draw outward faces of teeth Gl.Begin(Gl.QUAD_STRIP) FOR i = 0 TO teeth - 1 angle = i * 2.0 * Pi / teeth Gl.Vertexf(r1 * Cos(angle), r1 * Sin(angle), width * 0.5) Gl.Vertexf(r1 * Cos(angle), r1 * Sin(angle), -width * 0.5) u = r2 * Cos(angle + da) - r1 * Cos(angle) v = r2 * Sin(angle + da) - r1 * Sin(angle) fLen = Sqr(u * u + v * v) u /= fLen v /= fLen Gl.Normal3f(v, -u, 0.0) Gl.Vertexf(r2 * Cos(angle + da), r2 * Sin(angle + da), width * 0.5) Gl.Vertexf(r2 * Cos(angle + da), r2 * Sin(angle + da), -width * 0.5) Gl.Normal3f(Cos(angle), Sin(angle), 0.0) Gl.Vertexf(r2 * Cos(angle + 2 * da), r2 * Sin(angle + 2 * da), width * 0.5) Gl.Vertexf(r2 * Cos(angle + 2 * da), r2 * Sin(angle + 2 * da), -width * 0.5) u = r1 * Cos(angle + 3 * da) - r2 * Cos(angle + 2 * da) v = r1 * Sin(angle + 3 * da) - r2 * Sin(angle + 2 * da) Gl.Normal3f(v, -u, 0.0) Gl.Vertexf(r1 * Cos(angle + 3 * da), r1 * Sin(angle + 3 * da), width * 0.5) Gl.Vertexf(r1 * Cos(angle + 3 * da), r1 * Sin(angle + 3 * da), -width * 0.5) Gl.Normal3f(Cos(angle), Sin(angle), 0.0) NEXT Gl.Vertexf(r1 * Cos(0), r1 * Sin(0), width * 0.5) Gl.Vertexf(r1 * Cos(0), r1 * Sin(0), -width * 0.5) Gl.End()
Gl.ShadeModel(Gl.SMOOTH)
' Draw inside radius cylinder Gl.Begin(Gl.QUAD_STRIP) FOR i = 0 TO teeth angle = i * 2.0 * Pi / teeth Gl.Normal3f(-Cos(angle), -Sin(angle), 0.0) Gl.Vertexf(r0 * Cos(angle), r0 * Sin(angle), -width * 0.5) Gl.Vertexf(r0 * Cos(angle), r0 * Sin(angle), width * 0.5) NEXT Gl.End()
END
|