File: morph3d.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 (599 lines) | stat: -rw-r--r-- 20,446 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
(* $Id: morph3d.ml,v 1.18 2001-05-08 01:58:25 garrigue Exp $ *)

open StdLabels

(*-
 * morph3d.c - Shows 3D morphing objects (TK Version)
 *
 * This program was inspired on a WindowsNT(R)'s screen saver. It was written 
 * from scratch and it was not based on any other source code. 
 * 
 * Porting it to xlock (the final objective of this code since the moment I
 * decided to create it) was possible by comparing the original Mesa's gear
 * demo with it's ported version, so thanks for Danny Sung for his indirect
 * help (look at gear.c in xlock source tree). NOTE: At the moment this code
 * was sent to Brian Paul for package inclusion, the XLock Version was not
 * available. In fact, I'll wait it to appear on the next Mesa release (If you
 * are reading this, it means THIS release) to send it for xlock package 
 * inclusion). It will probably there be a GLUT version too.
 *
 * Thanks goes also to Brian Paul for making it possible and inexpensive
 * to use OpenGL at home.
 *
 * Since I'm not a native english speaker, my apologies for any gramatical
 * mistake.
 *
 * My e-mail addresses are
 *
 * vianna@cat.cbpf.br 
 *         and
 * marcelo@venus.rdc.puc-rio.br
 *
 * Marcelo F. Vianna (Feb-13-1997)
 *)

(*
This document is VERY incomplete, but tries to describe the mathematics used
in the program. At this moment it just describes how the polyhedra are 
generated. On futhurer versions, this document will be probabbly improved.

Since I'm not a native english speaker, my apologies for any gramatical
mistake.

Marcelo Fernandes Vianna 
- Undergraduate in Computer Engeneering at Catholic Pontifical University
- of Rio de Janeiro (PUC-Rio) Brasil.
- e-mail: vianna@cat.cbpf.br or marcelo@venus.rdc.puc-rio.br
- Feb-13-1997

POLYHEDRA GENERATION

For the purpose of this program it's not sufficient to know the polyhedra
vertexes coordinates. Since the morphing algorithm applies a nonlinear 
transformation over the surfaces (faces) of the polyhedron, each face has
to be divided into smaller ones. The morphing algorithm needs to transform 
each vertex of these smaller faces individually. It's a very time consoming
task.

In order to reduce calculation overload, and since all the macro faces of
the polyhedron are transformed by the same way, the generation is made by 
creating only one face of the polyhedron, morphing it and then rotating it
around the polyhedron center. 

What we need to know is the face radius of the polyhedron (the radius of 
the inscribed sphere) and the angle between the center of two adjacent 
faces using the center of the sphere as the angle's vertex.

The face radius of the regular polyhedra are known values which I decided
to not waste my time calculating. Following is a table of face radius for
the regular polyhedra with edge length = 1:

    TETRAHEDRON  : 1/(2*sqrt(2))/sqrt(3)
    CUBE	 : 1/2
    OCTAHEDRON   : 1/sqrt(6)
    DODECAHEDRON : T^2 * sqrt((T+2)/5) / 2     -> where T=(sqrt(5)+1)/2
    ICOSAHEDRON  : (3*sqrt(3)+sqrt(15))/12

I've not found any reference about the mentioned angles, so I needed to
calculate them, not a trivial task until I figured out how :)
Curiously these angles are the same for the tetrahedron and octahedron.
A way to obtain this value is inscribing the tetrahedron inside the cube
by matching their vertexes. So you'll notice that the remaining unmatched
vertexes are in the same straight line starting in the cube/tetrahedron
center and crossing the center of each tetrahedron's face. At this point
it's easy to obtain the bigger angle of the isosceles triangle formed by
the center of the cube and two opposite vertexes on the same cube face.
The edges of this triangle have the following lenghts: sqrt(2) for the base
and sqrt(3)/2 for the other two other edges. So the angle we want is:
     +-----------------------------------------------------------+
     | 2*ARCSIN(sqrt(2)/sqrt(3)) = 109.47122063449069174 degrees |
     +-----------------------------------------------------------+
For the cube this angle is obvious, but just for formality it can be
easily obtained because we also know it's isosceles edge lenghts:
sqrt(2)/2 for the base and 1/2 for the other two edges. So the angle we 
want is:
     +-----------------------------------------------------------+
     | 2*ARCSIN((sqrt(2)/2)/1)   = 90.000000000000000000 degrees |
     +-----------------------------------------------------------+
For the octahedron we use the same idea used for the tetrahedron, but now
we inscribe the cube inside the octahedron so that all cubes's vertexes
matches excatly the center of each octahedron's face. It's now clear that
this angle is the same of the thetrahedron one:
     +-----------------------------------------------------------+
     | 2*ARCSIN(sqrt(2)/sqrt(3)) = 109.47122063449069174 degrees |
     +-----------------------------------------------------------+
For the dodecahedron it's a little bit harder because it's only relationship
with the cube is useless to us. So we need to solve the problem by another
way. The concept of Face radius also exists on 2D polygons with the name
Edge radius:
  Edge Radius For Pentagon (ERp)
  ERp = (1/2)/TAN(36 degrees) * VRp = 0.6881909602355867905
  (VRp is the pentagon's vertex radio).
  Face Radius For Dodecahedron
  FRd = T^2 * sqrt((T+2)/5) / 2 = 1.1135163644116068404
Why we need ERp? Well, ERp and FRd segments forms a 90 degrees angle, 
completing this triangle, the lesser angle is a half of the angle we are 
looking for, so this angle is:
     +-----------------------------------------------------------+
     | 2*ARCTAN(ERp/FRd)	 = 63.434948822922009981 degrees |
     +-----------------------------------------------------------+
For the icosahedron we can use the same method used for dodecahedron (well
the method used for dodecahedron may be used for all regular polyhedra)
  Edge Radius For Triangle (this one is well known: 1/3 of the triangle height)
  ERt = sin(60)/3 = sqrt(3)/6 = 0.2886751345948128655
  Face Radius For Icosahedron
  FRi= (3*sqrt(3)+sqrt(15))/12 = 0.7557613140761707538
So the angle is:
     +-----------------------------------------------------------+
     | 2*ARCTAN(ERt/FRi)	 = 41.810314895778596167 degrees |
     +-----------------------------------------------------------+

*)


let scale = 0.3

let vect_mul (x1,y1,z1) (x2,y2,z2) =
  (y1 *. z2 -. z1 *. y2, z1 *. x2 -. x1 *. z2, x1 *. y2 -. y1 *. x2)

let sqr a = a *. a

(* Increasing this values produces better image quality, the price is speed. *)
(* Very low values produces erroneous/incorrect plotting *)
let tetradivisions =            23
let cubedivisions =             20
let octadivisions =             21
let dodecadivisions =           10
let icodivisions =              15

let tetraangle =                109.47122063449069174
let cubeangle =                 90.000000000000000000
let octaangle =                 109.47122063449069174
let dodecaangle =               63.434948822922009981
let icoangle =                  41.810314895778596167

let pi = acos (-1.)
let sqrt2 = sqrt 2.
let sqrt3 = sqrt 3.
let sqrt5 = sqrt 5.
let sqrt6 = sqrt 6.
let sqrt15 = sqrt 15.
let cossec36_2 = 0.8506508083520399322
let cosd x =  cos (float x /. 180. *. pi)
let sind x =  sin (float x /. 180. *. pi)
let cos72 = cosd 72
let sin72 = sind 72
let cos36 = cosd 36
let sin36 = sind 36

(*************************************************************************)

let front_shininess =   60.0
let front_specular  =   0.7, 0.7, 0.7, 1.0
let ambient         =   0.0, 0.0, 0.0, 1.0
let diffuse         =   1.0, 1.0, 1.0, 1.0
let position0       =   1.0, 1.0, 1.0, 0.0
let position1       =   -1.0,-1.0, 1.0, 0.0
let lmodel_ambient  =   0.5, 0.5, 0.5, 1.0
let lmodel_twoside  =   true

let materialRed     =   0.7, 0.0, 0.0, 1.0
let materialGreen   =   0.1, 0.5, 0.2, 1.0
let materialBlue    =   0.0, 0.0, 0.7, 1.0
let materialCyan    =   0.2, 0.5, 0.7, 1.0
let materialYellow  =   0.7, 0.7, 0.0, 1.0
let materialMagenta =   0.6, 0.2, 0.5, 1.0
let materialWhite   =   0.7, 0.7, 0.7, 1.0
let materialGray    =   0.2, 0.2, 0.2, 1.0
let all_gray = Array.create 20 materialGray

let vertex ~xf ~yf ~zf ~ampvr2 =
  let xa = xf +. 0.01 and yb = yf +. 0.01 in
  let xf2 = sqr xf and yf2 = sqr yf in
  let factor = 1. -. (xf2 +. yf2) *. ampvr2
  and factor1 = 1. -. (sqr xa +. yf2) *. ampvr2
  and factor2 = 1. -. (xf2 +. sqr yb) *. ampvr2 in
  let vertx = factor *. xf and verty = factor *. yf
  and vertz = factor *. zf in
  let neiax = factor1 *. xa -. vertx and neiay = factor1 *. yf -. verty
  and neiaz = factor1 *. zf -. vertz and neibx = factor2 *. xf -. vertx
  and neiby = factor2 *. yb -. verty and neibz = factor2 *. zf -. vertz in
  GlDraw.normal3 (vect_mul (neiax, neiay, neiaz) (neibx, neiby, neibz));
  GlDraw.vertex3 (vertx, verty, vertz)

let triangle ~edge ~amp ~divisions ~z =
  let divi = float divisions in
  let vr = edge *. sqrt3 /. 3. in
  let ampvr2 = amp /. sqr vr
  and zf = edge *. z in
  let ax = edge *. (0.5 /. divi)
  and ay = edge *. (-0.5 *. sqrt3 /. divi)
  and bx = edge *. (-0.5 /. divi) in
  for ri = 1 to divisions do
    GlDraw.begins `triangle_strip;
    for ti = 0 to ri - 1 do
      vertex ~zf ~ampvr2
	~xf:(float (ri-ti) *. ax +. float ti *. bx)
	~yf:(vr +. float (ri-ti) *. ay +. float ti *. ay);
      vertex ~zf ~ampvr2
	~xf:(float (ri-ti-1) *. ax +. float ti *. bx)
	~yf:(vr +. float (ri-ti-1) *. ay +. float ti *. ay)
    done;
    vertex ~xf:(float ri *. bx) ~yf:(vr +. float ri *. ay) ~zf ~ampvr2;
    GlDraw.ends ()
  done

let square ~edge ~amp ~divisions ~z =
  let divi = float divisions in
  let zf = edge *. z
  and ampvr2 = amp /. sqr (edge *. sqrt2 /. 2.) in
  for yi = 0 to divisions - 1 do
    let yf = edge *. (-0.5 +. float yi /. divi) in
    let yf2 = sqr yf in
    let y = yf +. 1.0 /. divi *. edge in
    let y2 = sqr y in
    GlDraw.begins `quad_strip;
    for xi = 0 to divisions do
      let xf = edge *. (-0.5 +. float xi /. divi) in
      vertex ~xf ~yf:y ~zf ~ampvr2;
      vertex ~xf ~yf ~zf ~ampvr2
    done;
    GlDraw.ends ()
  done

let pentagon ~edge ~amp ~divisions ~z =
  let divi = float divisions in
  let zf = edge *. z
  and ampvr2 = amp /. sqr(edge *. cossec36_2) in
  let x =
    Array.init 6
      ~f:(fun fi -> -. cos (float fi *. 2. *. pi /. 5. +. pi /. 10.)
	             /. divi *. cossec36_2 *. edge)
  and y =
    Array.init 6
      ~f:(fun fi -> sin (float fi *. 2. *. pi /. 5. +. pi /. 10.)
	             /. divi *. cossec36_2 *. edge)
  in
  for ri = 1 to divisions do
    for fi = 0 to 4 do
      GlDraw.begins `triangle_strip;
      for ti = 0 to ri-1 do
	vertex ~zf ~ampvr2
	  ~xf:(float(ri-ti) *. x.(fi) +. float ti *. x.(fi+1))
	  ~yf:(float(ri-ti) *. y.(fi) +. float ti *. y.(fi+1));
	vertex ~zf ~ampvr2
	  ~xf:(float(ri-ti-1) *. x.(fi) +. float ti *. x.(fi+1))
	  ~yf:(float(ri-ti-1) *. y.(fi) +. float ti *. y.(fi+1))
      done;
      vertex ~xf:(float ri *. x.(fi+1)) ~yf:(float ri *. y.(fi+1)) ~zf ~ampvr2;
      GlDraw.ends ()
    done
  done

let call_list list color =
  GlLight.material ~face:`both (`diffuse color);
  GlList.call list

let draw_tetra ~amp ~divisions ~color =
  let list = GlList.create `compile in
  triangle ~edge:2.0 ~amp ~divisions ~z:(0.5 /. sqrt6);
  GlList.ends();

  call_list list color.(0);
  GlMat.push();
  GlMat.rotate ~angle:180.0 ~z:1.0 ();
  GlMat.rotate ~angle:(-.tetraangle) ~x:1.0 ();
  call_list list color.(1);
  GlMat.pop();
  GlMat.push();
  GlMat.rotate ~angle:180.0 ~y:1.0 ();
  GlMat.rotate ~angle:(-180.0 +. tetraangle) ~x:0.5 ~y:(sqrt3 /. 2.) ();
  call_list list color.(2);
  GlMat.pop();
  GlMat.rotate ~angle:180.0 ~y:1.0 ();
  GlMat.rotate ~angle:(-180.0 +. tetraangle) ~x:0.5 ~y:(-.sqrt3 /. 2.) ();
  call_list list color.(3);

  GlList.delete list

let draw_cube ~amp ~divisions ~color =
  let list = GlList.create `compile in
  square ~edge:2.0 ~amp ~divisions ~z:0.5;
  GlList.ends ();

  call_list list color.(0);
  for i = 1 to 3 do
    GlMat.rotate ~angle:cubeangle ~x:1.0 ();
    call_list list color.(i)
  done;
  GlMat.rotate ~angle:cubeangle ~y:1.0 ();
  call_list list color.(4);
  GlMat.rotate ~angle:(2.0 *. cubeangle) ~y:1.0 ();
  call_list list color.(5);

  GlList.delete list

let draw_octa ~amp ~divisions ~color =
  let list = GlList.create `compile in
  triangle ~edge:2.0 ~amp ~divisions ~z:(1.0 /. sqrt6);
  GlList.ends ();

  let do_list (i,y) =
    GlMat.push();
    GlMat.rotate ~angle:180.0 ~y:1.0 ();
    GlMat.rotate ~angle:(-.octaangle) ~x:0.5 ~y ();
    call_list list color.(i);
    GlMat.pop()
  in
  call_list list color.(0);
  GlMat.push();
  GlMat.rotate ~angle:180.0 ~z:1.0 ();
  GlMat.rotate ~angle:(-180.0 +. octaangle) ~x:1.0 ();
  call_list list color.(1);
  GlMat.pop();
  List.iter [2, sqrt3 /. 2.0; 3, -.sqrt3 /. 2.0] ~f:do_list;
  GlMat.rotate ~angle:180.0 ~x:1.0 ();
  GlLight.material ~face:`both (`diffuse color.(4));
  GlList.call list;
  GlMat.push();
  GlMat.rotate ~angle:180.0 ~z:1.0 ();
  GlMat.rotate ~angle:(-180.0 +. octaangle) ~x:1.0 ();
  GlLight.material ~face:`both (`diffuse color.(5));
  GlList.call list;
  GlMat.pop();
  List.iter [6, sqrt3 /. 2.0; 7, -.sqrt3 /. 2.0] ~f:do_list;

  GlList.delete list

let draw_dodeca ~amp ~divisions ~color =
  let tau = (sqrt5 +. 1.0) /. 2.0 in
  let list = GlList.create `compile in
  pentagon ~edge:2.0 ~amp ~divisions
    ~z:(sqr(tau) *. sqrt ((tau+.2.0)/.5.0) /. 2.0);
  GlList.ends ();

  let do_list (i,angle,x,y) =
    GlMat.push();
    GlMat.rotate ~angle:angle ~x ~y ();
    call_list list color.(i);
    GlMat.pop();
  in
  GlMat.push ();
  call_list list color.(0);
  GlMat.rotate ~angle:180.0 ~z:1.0 ();
  List.iter ~f:do_list
    [ 1, -.dodecaangle, 1.0, 0.0;
      2, -.dodecaangle, cos72, sin72;
      3, -.dodecaangle, cos72, -.sin72;
      4, dodecaangle, cos36, -.sin36;
      5, dodecaangle, cos36, sin36 ];
  GlMat.pop ();
  GlMat.rotate ~angle:180.0 ~x:1.0 ();
  call_list list color.(6);
  GlMat.rotate ~angle:180.0 ~z:1.0 ();
  List.iter ~f:do_list
    [ 7, -.dodecaangle, 1.0, 0.0;
      8, -.dodecaangle, cos72, sin72;
      9, -.dodecaangle, cos72, -.sin72;
      10, dodecaangle, cos36, -.sin36 ];
  GlMat.rotate ~angle:dodecaangle ~x:cos36 ~y:sin36 ();
  call_list list color.(11);

  GlList.delete list

let draw_ico ~amp ~divisions ~color =
  let list = GlList.create `compile in
  triangle ~edge:1.5 ~amp ~divisions
    ~z:((3.0 *. sqrt3 +. sqrt15) /. 12.0);
  GlList.ends ();

  let do_list1 i =
    GlMat.rotate ~angle:180.0 ~y:1.0 ();
    GlMat.rotate ~angle:(-180.0 +. icoangle) ~x:0.5 ~y:(sqrt3/.2.0) ();
    call_list list color.(i)
  and do_list2 i =
    GlMat.rotate ~angle:180.0 ~y:1.0 ();
    GlMat.rotate ~angle:(-180.0 +. icoangle) ~x:0.5 ~y:(-.sqrt3/.2.0) ();
    call_list list color.(i)
  and do_list3 i =
    GlMat.rotate ~angle:180.0 ~z:1.0 ();
    GlMat.rotate ~angle:(-.icoangle) ~x:1.0 ();
    call_list list color.(i)
  in
  GlMat.push ();
  call_list list color.(0);
  GlMat.push ();
  do_list3 1;
  GlMat.push ();
  do_list1 2;
  GlMat.pop ();
  do_list2 3;
  GlMat.pop ();
  GlMat.push ();
  do_list1 4;
  GlMat.push ();
  do_list1 5;
  GlMat.pop();
  do_list3 6;
  GlMat.pop ();
  do_list2 7;
  GlMat.push ();
  do_list2 8;
  GlMat.pop ();
  do_list3 9;
  GlMat.pop ();
  GlMat.rotate ~angle:180.0 ~x:1.0 ();
  call_list list color.(10);
  GlMat.push ();
  do_list3 11;
  GlMat.push ();
  do_list1 12;
  GlMat.pop ();
  do_list2 13;
  GlMat.pop ();
  GlMat.push ();
  do_list1 14;
  GlMat.push ();
  do_list1 15;
  GlMat.pop ();
  do_list3 16;
  GlMat.pop ();
  do_list2 17;
  GlMat.push ();
  do_list2 18;
  GlMat.pop ();
  do_list3 19;

  GlList.delete list

class view togl = object (self)
  val togl = togl
  val mutable smooth = true
  val mutable step = 0.
  val mutable obj = 1
  val mutable draw_object = fun ~amp -> ()
  val mutable magnitude = 0.

  method width = Togl.width togl
  method height = Togl.height togl

  method draw =
    let ratio = float self#height /. float self#width in
    GlClear.clear [`color;`depth];
    GlMat.push ();
    GlMat.translate () ~z:(-10.0);
    GlMat.scale () ~x:(scale *. ratio) ~y:scale ~z:scale;
    GlMat.translate ()
      ~x:(2.5 *. ratio *. sin (step *. 1.11))
      ~y:(2.5 *. cos (step *. 1.25 *. 1.11));
    GlMat.rotate ~angle:(step *. 100.) ~x:1.0 ();
    GlMat.rotate ~angle:(step *. 95.) ~y:1.0 ();
    GlMat.rotate ~angle:(step *. 90.) ~z:1.0 ();
    draw_object ~amp:((sin step +. 1.0/.3.0) *. (4.0/.5.0) *. magnitude);
    GlMat.pop();
    Gl.flush();
    Togl.swap_buffers togl;
    step <- step +. 0.05

  method reshape =
    GlDraw.viewport ~x:0 ~y:0 ~w:self#width ~h:self#height;
    GlMat.mode `projection;
    GlMat.load_identity();
    GlMat.frustum ~x:(-1.0, 1.0) ~y:(-1.0, 1.0) ~z:(5.0, 15.0);
    GlMat.mode `modelview

  method key sym =
    begin match sym with
      "1" -> obj <- 1
    | "2" -> obj <- 2
    | "3" -> obj <- 3
    | "4" -> obj <- 4
    | "5" -> obj <- 5
    | "Return" -> smooth <- not smooth
    | "Escape" -> Tk.destroy (Winfo.toplevel togl); exit 0
    | _ -> ()
    end;
    self#pinit

  method pinit =
    begin match obj with
      1 ->
	draw_object <- draw_tetra
	     ~divisions:tetradivisions
	     ~color:[|materialRed;  materialGreen;
		     materialBlue; materialWhite|];
	magnitude <- 2.5
    | 2 ->
	draw_object <- draw_cube
	     ~divisions:cubedivisions
	     ~color:[|materialRed; materialGreen; materialCyan;
		     materialMagenta; materialYellow; materialBlue|];
	magnitude <- 2.0
    | 3 ->
	draw_object <- draw_octa
	     ~divisions:octadivisions
	     ~color:[|materialRed; materialGreen; materialBlue;
		     materialWhite; materialCyan; materialMagenta;
		     materialGray; materialYellow|];
	magnitude <- 2.5
    | 4 ->
      draw_object <- draw_dodeca
	   ~divisions:dodecadivisions
	   ~color:[|materialRed; materialGreen; materialCyan;
		   materialBlue; materialMagenta; materialYellow;
		   materialGreen; materialCyan; materialRed;
		   materialMagenta; materialBlue; materialYellow|];
      magnitude <- 2.0
    | 5 ->
	draw_object <- draw_ico
	     ~divisions:icodivisions
	     ~color:[|materialRed; materialGreen; materialBlue;
		     materialCyan; materialYellow; materialMagenta;
		     materialRed; materialGreen; materialBlue;
		     materialWhite; materialCyan; materialYellow;
		     materialMagenta; materialRed; materialGreen;
		     materialBlue; materialCyan; materialYellow;
		     materialMagenta; materialGray|];
	magnitude <- 3.5
    | _ -> ()
    end;
    GlDraw.shade_model (if smooth then `smooth else `flat)
end

open Tk

let main () =
  List.iter ~f:print_string
    [ "Morph 3D - Shows morphing platonic polyhedra\n";
      "Author: Marcelo Fernandes Vianna (vianna@cat.cbpf.br)\n";
      "Ported to LablGL by Jacques Garrigue\n\n";
      "  [1]    - Tetrahedron\n";
      "  [2]    - Hexahedron (Cube)\n";
      "  [3]    - Octahedron\n";
      "  [4]    - Dodecahedron\n";
      "  [5]    - Icosahedron\n";
      "[RETURN] - Toggle smooth/flat shading\n";
      " [ESC]   - Quit\n" ];
  flush stdout;

  let top = openTk () in
  let togl = Togl.create top ~width:640 ~height:480
      ~depth:true ~double:true ~rgba:true in
  Wm.title_set top "Morph 3D - Shows morphing platonic polyhedra";
  GlClear.depth 1.0;
  GlClear.color (0.0, 0.0, 0.0);
  GlDraw.color (1.0, 1.0, 1.0);

  GlClear.clear [`color;`depth];
  Gl.flush();
  Togl.swap_buffers togl;

  List.iter ~f:(GlLight.light ~num:0)
    [`ambient ambient; `diffuse diffuse; `position position0];
  List.iter ~f:(GlLight.light ~num:1)
    [`ambient ambient; `diffuse diffuse; `position position1];
  GlLight.light_model (`ambient lmodel_ambient);
  GlLight.light_model (`two_side lmodel_twoside);
  List.iter ~f:Gl.enable
    [`lighting;`light0;`light1;`depth_test;`normalize];

  GlLight.material ~face:`both (`shininess front_shininess);
  GlLight.material ~face:`both (`specular front_specular);

  GlMisc.hint `fog `fastest;
  GlMisc.hint `perspective_correction `fastest;
  GlMisc.hint `polygon_smooth `fastest;

  let view = new view togl in
  view#pinit;

  Togl.display_func togl ~cb:(fun () -> view#draw);
  Togl.reshape_func togl ~cb:(fun () -> view#reshape);
  Togl.timer_func ~ms:20 ~cb:(fun () -> view#draw);
  bind togl ~events:[`KeyPress] ~fields:[`KeySymString]
    ~action:(fun ev -> view#key ev.ev_KeySymString);
  bind togl ~events:[`Enter] ~action:(fun _ -> Focus.set togl);
  pack [togl] ~expand:true ~fill:`Both;
  mainLoop ()

let _ = main ()