File: gears_a.ml

package info (click to toggle)
lablgl 1:1.05-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,444 kB
  • sloc: ansic: 8,270; ml: 6,118; tcl: 342; makefile: 294; xml: 84; perl: 12
file content (261 lines) | stat: -rw-r--r-- 7,215 bytes parent folder | download | duplicates (4)
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
257
258
259
260
261
(* $Id: gears_a.ml,v 1.2 2003-10-01 10:11:41 raffalli Exp $ *)

(*
 * 3-D gear wheels.  This program is in the public domain.
 *
 * Brian Paul
 * LablGL version by Jacques Garrigue
 *
 * gears_a.ml: use vertex arrays
 *)

let pi = acos (-1.)

(*
 * Draw a gear wheel.  You'll probably want to call this function when
 * building a display list since we do a lot of trig here.
 *
 * Input:  inner_radius - radius of hole at center
 *         outer_radius - radius at center of teeth
 *         width - width of gear
 *         teeth - number of teeth
 *         tooth_depth - depth of tooth
 *)
let gear ~inner ~outer ~width ~teeth ~tooth_depth =
  let r0 = inner
  and r1 = outer -. tooth_depth /. 2.0
  and r2 = outer +. tooth_depth /. 2.0 in

  let ta = 2.0 *. pi /. float teeth in
  let da = ta /. 4.0 in

  GlDraw.shade_model `flat;

  GlDraw.normal ~z:1.0 ();

  let vertex ~r ~z ?(s=0) i =
    let angle = float i *. ta +. float s *. da in
    GlDraw.vertex ~x:(r *. cos angle) ~y:(r *. sin angle) ~z ()
  in

  let raw = Raw.create `float (12 * (teeth+1)) in
  GlArray.vertex `three raw;
  let count = ref 0 in

  let vertexa ~r ~z ?(s=0) i =
    let angle = float i *. ta +. float s *. da in
    let pos = !count * 3 in
    Raw.set_float raw ~pos (r *. cos angle);
    Raw.set_float raw ~pos:(pos+1) (r *. sin angle);
    Raw.set_float raw ~pos:(pos+2) z;
    incr count
  in

  (* draw front face *)
  let z = width *. 0.5 in
  for i=0 to teeth do
    vertexa i ~r:r0 ~z;
    vertexa i ~r:r1 ~z;
    vertexa i ~r:r0 ~z;
    vertexa i ~r:r1 ~z ~s:3;
  done;
  GlArray.draw_arrays `quad_strip  0 !count;
  count := 0;
  
  (* draw front sides of teeth *)
  for i=0 to teeth - 1 do
    vertexa i ~r:r1 ~z;
    vertexa i ~r:r2 ~s:1 ~z;
    vertexa i ~r:r2 ~s:2 ~z;
    vertexa i ~r:r1 ~s:3 ~z;
  done;
  GlArray.draw_arrays `quads 0 !count;
  count := 0;

  GlDraw.normal ~z:(-1.0) ();

  (* draw back face *)
  let z = -. width *. 0.5 in
  for i=0 to teeth do
    vertexa i ~r:r1 ~z;
    vertexa i ~r:r0 ~z;
    vertexa i ~r:r1 ~s:3 ~z;
    vertexa i ~r:r0 ~z;
  done;
  GlArray.draw_arrays `quad_strip  0 !count;
  count := 0;

  (* draw back sides of teeth *)
  for i=0 to teeth - 1 do
    vertexa i ~r:r1 ~s:3 ~z;
    vertexa i ~r:r2 ~s:2 ~z;
    vertexa i ~r:r2 ~s:1 ~z;
    vertexa i ~r:r1 ~z;
  done;
  GlArray.draw_arrays `quads 0 !count;
  count := 0;

  (* draw outward faces of teeth *)
  let z = width *. 0.5 and z' = width *. (-0.5) in
  GlDraw.begins `quad_strip;
  for i=0 to teeth - 1 do
    let angle = float i *. ta in
    vertex i ~r:r1 ~z;
    vertex i ~r:r1 ~z:z';
    let u = r2 *. cos(angle+.da) -. r1 *. cos(angle)
    and v = r2 *. sin(angle+.da) -. r1 *. sin(angle) in
    GlDraw.normal ~x:v ~y:(-.u) ();
    vertex i ~r:r2 ~s:1 ~z;
    vertex i ~r:r2 ~s:1 ~z:z';
    GlDraw.normal ~x:(cos angle) ~y:(sin angle) ();
    vertex i ~r:r2 ~s:2 ~z;
    vertex i ~r:r2 ~s:2 ~z:z';
    let u = r1 *. cos(angle +. 3. *. da) -. r2 *. cos(angle +. 2. *. da)
    and v = r1 *. sin(angle +. 3. *. da) -. r2 *. sin(angle +. 2. *. da) in
    GlDraw.normal ~x:v ~y:(-.u) ();
    vertex i ~r:r1 ~s:3 ~z;
    vertex i ~r:r1 ~s:3 ~z:z';
    GlDraw.normal ~x:(cos angle) ~y:(sin angle) ();
  done;
  vertex 0 ~r:r1 ~z;
  vertex 0 ~r:r1 ~z:z';
  GlDraw.ends ();

  GlDraw.shade_model `smooth;

  (* draw inside radius cylinder *)
  GlDraw.begins `quad_strip;
  for i=0 to teeth do
    let angle = float i *. ta in
    GlDraw.normal ~x:(-. cos angle) ~y:(-. sin angle) ();
    vertex i ~r:r0 ~z:z';
    vertex i ~r:r0 ~z;
  done;
  GlDraw.ends ()

class view ~gear1 ~gear2 ~gear3 ?(limit=0) togl = object (self)
  val mutable view_rotx = 0.0
  val mutable view_roty = 0.0
  val mutable view_rotz = 0.0
  val mutable angle = 0.0
  val mutable count = 1

  method rotx a = view_rotx <- a
  method roty a = view_roty <- a

  method draw =
    GlClear.clear [`color;`depth];

    GlMat.push ();
    GlMat.rotate ~angle:view_rotx ~x:1.0 ();
    GlMat.rotate ~angle:view_roty ~y:1.0 ();
    GlMat.rotate ~angle:view_rotz ~z:1.0 ();

    GlMat.push ();
    GlMat.translate ~x:(-3.0) ~y:(-2.0) ();
    GlMat.rotate ~angle:angle ~z:1.0 ();
    (* gear inner:1.0 outer:4.0 width:1.0 teeth:20 tooth_depth:0.7; *)
    GlList.call gear1;
    GlMat.pop ();

    GlMat.push ();
    GlMat.translate ~x:3.1 ~y:(-2.0) ();
    GlMat.rotate ~angle:(-2.0 *. angle -. 9.0) ~z:1.0 ();
    (* gear inner:0.5 outer:2.0 width:2.0 teeth:10 tooth_depth:0.7; *)
    GlList.call gear2;
    GlMat.pop ();

    GlMat.push ();
    GlMat.translate ~x:(-3.1) ~y:4.2 ();
    GlMat.rotate ~angle:(-2.0 *. angle -. 25.0) ~z:1.0 ();
    (* gear inner:1.3 outer:2.0 width:0.5 teeth:10 tooth_depth:0.7; *)
    GlList.call gear3;
    GlMat.pop ();

    GlMat.pop ();
    
    Togl.swap_buffers togl;

    count <- count + 1;
    if count =limit then exit 0

  method idle =
    angle <- angle +. 2.0;
    self#draw

  method reshape =
    let w = Togl.width togl and h = Togl.height togl in
    GlDraw.viewport ~x:0 ~y:0 ~w ~h;
    GlMat.mode `projection;
    GlMat.load_identity ();
    let r = float w /. float h in
    let r' = 1. /. r in
    if (w>h) then
      GlMat.frustum ~x:(-. r,r) ~y:(-1.0,1.0) ~z:(5.0,60.0)
    else
      GlMat.frustum ~x:(-1.0,1.0) ~y:(-.r',r') ~z:(5.0,60.0);

    GlMat.mode `modelview;
    GlMat.load_identity();
    GlMat.translate ~z:(-40.0) ();
    GlClear.clear[`color;`depth]
end

let init () =
  let pos = 5.0, 5.0, 10.0, 0.0
  and red = 0.8, 0.1, 0.0, 1.0
  and green = 0.0, 0.8, 0.2, 1.0
  and blue = 0.2, 0.2, 1.0, 1.0 in

  GlLight.light ~num:0 (`position pos);
  List.iter Gl.enable
    [`cull_face;`lighting;`light0;`depth_test;`normalize];
  GlArray.enable `vertex;

  (* make the gears *)
  let make_gear ~inner ~outer ~width ~teeth ~color =
    let list = GlList.create `compile in
    GlLight.material ~face:`front (`ambient_and_diffuse color);
    gear ~inner ~outer ~width ~teeth ~tooth_depth:0.7;
    GlList.ends ();
    list
  in
  let gear1 = make_gear ~inner:1.0 ~outer:4.0 ~width:1.0 ~teeth:20 ~color:red
  and gear2 = make_gear ~inner:0.5 ~outer:2.0 ~width:2.0 ~teeth:10 ~color:green
  and gear3 = make_gear ~inner:1.3 ~outer:2.0 ~width:0.5 ~teeth:10 ~color:blue in

  (gear1, gear2, gear3)

open Tk

let main () =
  let top = openTk () in
  let f = Frame.create top in
  let v = Textvariable.create () in
  let my_scale =
    Scale.create ~min:0. ~max:180. ~showvalue:false ~highlightbackground:`Black
  in
  let togl =
    Togl.create f ~width:300 ~height:300
      ~rgba:true ~depth:true ~double:true
  and sh = my_scale f ~orient:`Horizontal
  and sv = my_scale top ~orient:`Vertical
  in
  
  Wm.title_set top "Gears";

  let gear1, gear2, gear3 = init() in
  let view = new view togl ~gear1 ~gear2 ~gear3 in
  Scale.configure sv ~command:(view#rotx);
  Scale.configure sh ~command:(view#roty);
  Scale.set sh 20.; Scale.set sv 40.;
  Togl.reshape_func togl ~cb:(fun () -> view#reshape);
  Togl.display_func togl ~cb:(fun () -> view#draw);
  Togl.timer_func ~ms:20 ~cb:(fun () -> view#idle);
  pack [sv] ~side:`Right ~fill:`Y;
  pack [f] ~expand:true ~fill:`Both;
  pack [togl] ~side:`Top ~expand:true ~fill:`Both;
  pack [sh] ~side:`Bottom ~fill:`X;
  Tk.mainLoop ()

let _ = main ()