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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
Debug.print "* Loading oplotmain";;
module Make (Graphics : Make_graphics.GRAPHICS) = struct
open Tsdl
module Gl = Gl_legacy
module Gl3 = Tgl3.Gl
open Common
open Points
open Point2
open Oplotdef
open Sysinit
open Renderinit
let go = Debug.go
let do_option o f = match o with Some x -> f x | None -> ()
let default o v = match o with Some x -> x | None -> v
let force_refresh = ref false
let xfig_scale = 45.
let bounding_box dev =
match dev with
| GRAPHICS -> (1., 1., !fwindow_width, !fwindow_height)
| GL -> (0., 0., 1., 1.)
| FIG ->
( 0.,
xfig_scale *. 157.2 *. !fwindow_height /. 600. /. !gl_scale,
xfig_scale *. 210. *. !fwindow_width /. 800. /. !gl_scale,
0. )
let time () = Int32.to_int (Sdl.get_ticks ()) - !time_delay
let elapsed () = time () - !initial_time
let reset_time ?(t0 = 0) () = initial_time := time () - t0
let scale x = x *. !gl_scale
let round x = int_of_float (x +. 0.5)
let iscale i = round (float i *. !gl_scale)
let dpi_scale = ref 1.
let scale_window =
let scaled = ref false in
fun () ->
if not !scaled then begin
window_width := iscale !window_width;
window_height := iscale !window_height;
fwindow_width := float !window_width;
fwindow_height := float !window_height;
scaled := true
end
else Debug.print "Already scaled."
let win = ref None
let glcontext = ref None
let window_os_size () =
( round (float (!window_width + !left_margin + !right_margin) /. !dpi_scale),
round (float (!window_height + !top_margin + !bottom_margin) /. !dpi_scale)
)
let sdl_destroy_window win =
Sdl.destroy_window win;
if !Sys.interactive && Sdl.get_current_video_driver () = Some "cocoa" then begin
Debug.print "cocoa workaround";
Sdl.delay 100l;
(go @@ Sdl.(init Init.joystick));
Sdl.(quit_sub_system Init.joystick)
end
let sdl_get_dpi_scale () =
match
Sdl.create_window "Oplot - SDL Window" ~w:64 ~h:64
Sdl.Window.(opengl + allow_highdpi + hidden)
with
| Error (`Msg e) ->
Debug.print "Cannot open test window: %s" e;
1.
| Ok win ->
let w, h = Sdl.get_window_size win in
let rw, rh = Sdl.gl_get_drawable_size win in
sdl_destroy_window win;
if (rw, rh) <> (w, h) then begin
let dpi_xscale = float rw /. float w in
let dpi_yscale = float rh /. float h in
Debug.print "This display imposes a hard scaling of (%f,%f)."
dpi_xscale dpi_yscale;
min dpi_xscale dpi_yscale
end
else 1.
let gl_clear_color c = Gl.clear_color c.r c.g c.b 1.0
let gl_draw_color c = Gl.color3f c.r c.g c.b
let sdl_init ~show () =
let crucial () =
if Sdl.Init.test (Sdl.was_init None) Sdl.Init.video then
Debug.print "Using existing SDL context."
else begin
Debug.print "Raising up SDL...";
Sdl.init Sdl.Init.(timer + video + events) |> go;
win := None;
at_exit (fun () ->
Debug.print "Quitting SDL";
Sdl.quit ());
match Sdl.get_display_dpi 0 with
| Ok (x, _, _) ->
Debug.print "DPI detected by SDL: %f" x;
gl_scale := max 1. (x /. 110.)
| Error (`Msg m) -> Debug.print "Cannot get DPI from SDL: %s" m
end;
if !win = None then begin
scale_window ();
dpi_scale := sdl_get_dpi_scale ();
let w, h = window_os_size () in
match
Sdl.create_window "Oplot - SDL Window" ~w ~h
Sdl.Window.(opengl + resizable + allow_highdpi)
with
| Error (`Msg e) ->
Sdl.log "Create window error: %s" e;
raise (Debug.Sdl_error e)
| Ok wn ->
win := Some wn;
if !dpi_scale <> 1. then
let rw, rh = Sdl.gl_get_drawable_size wn in
resize_window rw rh
end;
if not show then do_option !win Sdl.hide_window;
Sdlttf.init () |> go;
glcontext :=
match !win with
| Some w -> Some (go @@ Sdl.gl_create_context w)
| None -> None
in
(try crucial ()
with Debug.Sdl_error _ -> (
Debug.print "Hum... Trying again";
Sdl.quit ();
multisampling := false;
Unix.sleep 1;
try crucial ()
with Debug.Sdl_error e ->
Debug.print "Sdl error %s" e;
do_option !glcontext Sdl.gl_delete_context;
Sdl.quit ();
exit 1));
window_open := true;
Debug.print "sdl_init OK"
let gtk_init () = ()
let draw_of_pixel (dx, dy) (bx0, by0, bx1, by1) =
( float dx *. (bx1 -. bx0) /. !fwindow_width,
float dy *. (by1 -. by0) /. !fwindow_height )
let gl_resize () =
Gl.viewport 0 0
(!window_width + !left_margin + !right_margin)
(!window_height + !top_margin + !bottom_margin);
Gl.matrix_mode Gl.projection;
Gl.load_identity ();
let bb = bounding_box GL in
let dxl, dyb = draw_of_pixel (!left_margin, !bottom_margin) bb
and dxr, dyt = draw_of_pixel (!right_margin, !top_margin) bb in
Gl.ortho (-.dxl) (1. +. dxr) (-.dyb) (1. +. dyt) (-2.) 2.;
Gl.matrix_mode Gl.modelview
let gl_rotated2d angle =
let bb = bounding_box GL in
let dxl, dyb = draw_of_pixel (!left_margin, !bottom_margin) bb
and dxr, dyt = draw_of_pixel (!right_margin, !top_margin) bb in
let x = (1. +. dxl +. dxr) /. 2. in
let y = (1. +. dyb +. dyt) /. 2. in
Gl.translatef x y 0.;
Gl.rotated angle 0. 0. 1.;
Gl.translatef (-.x) (-.y) 0.
let gl_init ?(show = true) () =
(match !default_gl with
| GLUT -> Iglut.init ()
| SDL -> sdl_init ~show ()
| GTK -> gtk_init ());
Debug.print "GL inits...";
gl_clear_color !default_bg_color;
Gl3.draw_buffer Gl3.back;
Gl3.read_buffer Gl3.back;
Gl3.clear Gl3.depth;
Gl3.pixel_storei Gl3.unpack_alignment 1;
Gl3.disable Gl3.polygon_smooth;
Gl3.hint Gl3.line_smooth Gl3.fastest;
Gl3.enable Gl3.blend;
Gl3.blend_func Gl3.src_alpha Gl3.one_minus_src_alpha;
Gl3.line_width !gl_scale;
Gl3.point_size !gl_scale;
Gl3.enable Gl3.polygon_offset_fill;
Gl3.polygon_offset 1. 1.;
gl_resize ();
Gl.push_matrix ();
Gl.flush ();
Debug.print "gl_init OK"
let toggle_fullscreen () =
do_option !win (fun w ->
match
Sdl.set_window_fullscreen w
(if not !fullscreen then Sdl.Window.fullscreen_desktop
else Sdl.Window.windowed)
with
| Error (`Msg e) -> Sdl.log "Fullscreen error: %s" e
| Ok () -> fullscreen := not !fullscreen)
let close ?(dev = !default_device) () =
match dev with
| GRAPHICS -> Graphics.close_graph ()
| GL -> (
window_open := false;
if !fullscreen then toggle_fullscreen ();
let close () =
do_option !glcontext Sdl.gl_delete_context;
glcontext := None;
do_option !win (fun w ->
Debug.print "Destroying window";
sdl_destroy_window w;
Sdl.(flush_events Event.first_event Event.last_event));
win := None
in
try close ()
with Debug.Sdl_error e ->
Debug.print "%s. Hum... trying again." e;
close ())
| FIG -> raise (Not_implemented "FIG close")
let quit ?(dev = !default_device) () =
try
close ~dev ();
remove_tmp_dir ()
with e ->
Debug.print "Warning: quit wasn't clean.";
if Debug.debug then raise e
let reset_gllist = ref false
let text_token = 1.
let insert_token x = Gl.Feedback.pass_through x
exception Feedback_Buffer_Overflow
let feedback_render draw_proc =
let rec loop i =
reset_gllist := true;
gl_init ();
let r = Gl.Feedback.setup (1 lsl i) Gl.Feedback.GL_3D_COLOR in
ignore (Gl.render_mode Gl.FEEDBACK);
gl_draw_color default_color;
Debug.print "draw in feedback mode...";
draw_proc ();
Debug.print "done";
let num = Gl.render_mode Gl.RENDER in
if num < 0 then
if i < 31 then
loop (i + 1)
else raise Feedback_Buffer_Overflow
else begin
Debug.print "Created feedback buffer of size: %d for %d objects."
(1 lsl i) num;
(r, num)
end
in
loop 16
let get_vertex r pos =
let open Bigarray.Array1 in
let coord = sub r pos 3 in
let colour = sub r (pos + 3) 4 in
(coord, colour)
let point_of_vertex (coord, _colour) =
let open Bigarray.Array1 in
{ x = get coord 0; y = get coord 1 }
let depth_of_vertex (coord, _colour) =
let open Bigarray.Array1 in
get coord 2
let color_of_vertex (_coord, colour) =
let open Bigarray.Array1 in
let a = get colour 3 in
if a <> 1. then begin
prerr_endline (Printf.sprintf "Feedback Alpha:%f\n" a);
flush stderr
end;
{ r = get colour 0; g = get colour 1; b = get colour 2 }
let feedback_print r n =
for i = 0 to n - 1 do
Printf.printf "%d: %f\n" i (Bigarray.Array1.get r i)
done
let () = Debug.print "Initialise feedback constants"
let feedback_view () =
view (float !left_margin) (float !bottom_margin)
(float (!window_width + !right_margin))
(float (!window_height + !top_margin))
let gl_vertex_size = 7
let feedback_parse_point r n0 nmax =
let rec loop n c0 pl depsum nombre =
if
n >= nmax
|| Gl.Feedback.tokenf (Bigarray.Array1.get r n) <> Gl.Feedback.POINT
then (pl, depsum, nombre, n)
else
let v = get_vertex r (n + 1) in
let c = color_of_vertex v in
if c <> c0 then (pl, depsum, nombre, n)
else
let p = point_of_vertex v and d = depth_of_vertex v in
loop (n + 1 + gl_vertex_size) c0 (p :: pl) (depsum +. d) (nombre + 1)
in
let c0 = color_of_vertex (get_vertex r (n0 + 1)) in
let pl, depsum, nombre, n = loop n0 c0 [] 0. 0 in
([ Color c0; Points pl ], depsum /. float nombre, n)
let feedback_parse_line r n0 nmax =
let rec loop n p0 c0 d0 pl depsum nombre =
if
n >= nmax
|| Gl.Feedback.tokenf (Bigarray.Array1.get r n) <> Gl.Feedback.LINE
then (pl, depsum, nombre, n)
else
let v1, v2 =
(get_vertex r (n + 1), get_vertex r (n + 1 + gl_vertex_size))
in
if
color_of_vertex v1 <> c0
|| point_of_vertex v1 <> p0
|| depth_of_vertex v1 <> d0
then (pl, depsum, nombre, n)
else
let p2 = point_of_vertex v2 and d = depth_of_vertex v2 in
loop
(n + 1 + (2 * gl_vertex_size))
p2 c0 d0 (p2 :: pl) (depsum +. d) (nombre + 1)
in
let v1, v2 =
(get_vertex r (n0 + 1), get_vertex r (n0 + 1 + gl_vertex_size))
in
let c1 = color_of_vertex v1
and d = depth_of_vertex v1 +. depth_of_vertex v2
and p2 = point_of_vertex v2 in
let pl, depsum, nombre, n =
loop
(n0 + 1 + (2 * gl_vertex_size))
p2 c1 d
[ p2; point_of_vertex v1 ]
d 2
in
([ Color c1; Lines [ pl ] ], depsum /. float nombre, n)
let feedback_parse_poly r n0 nmax =
let rec loop n nfin pl depsum nombre =
if n > nfin || n >= nmax then (pl, depsum, nombre, n)
else
let v = get_vertex r n in
let p = point_of_vertex v and d = depth_of_vertex v in
loop (n + gl_vertex_size) nfin (p :: pl) (depsum +. d) (nombre + 1)
in
let num = int_of_float (Bigarray.Array1.get r (n0 + 1)) in
let v0 = get_vertex r (n0 + 2) in
let c0 = color_of_vertex v0 in
let pl, depsum, nombre, n =
loop (n0 + 2) (n0 + 1 + (num * gl_vertex_size)) [] 0. 0
in
let poly_offset =
0.0001
in
([ Color c0; Poly pl ], poly_offset +. (depsum /. float nombre), n)
let feedback_parse_pass r n0 =
let token = Bigarray.Array1.get r (n0 + 1) in
if token = text_token then raise (Not_implemented "text token")
else raise (Not_implemented "unknown pass-through")
let depth_compare ((_, d1) : plot_object list * float) (_, d2) =
if d1 > d2 then -1 else if d1 < d2 then 1 else 0
let feedback_parse r nmax =
let rec loop n pl =
if n >= nmax then pl
else
let x = Bigarray.Array1.get r n in
let open Gl.Feedback in
let pl', dep, n' =
match tokenf x with
| POINT -> feedback_parse_point r n nmax
| LINE | LINE_RESET -> feedback_parse_line r n nmax
| POLYGON -> feedback_parse_poly r n nmax
| PASS_THROUGH -> feedback_parse_pass r n
| _ -> raise (Not_implemented "unknown token")
in
loop n' ((pl', dep) :: pl)
in
let liste = List.sort depth_compare (loop 0 []) in
feedback_view () :: List.flatten (List.map (fun (pl, _) -> pl) liste)
let ( +| ) (x0, y0, z0) (x1, y1, z1) = (x0 +. x1, y0 +. y1, z0 +. z1)
let ( -| ) (x0, y0, z0) (x1, y1, z1) = (x0 -. x1, y0 -. y1, z0 -. z1)
let ( *| ) s (x, y, z) = (s *. x, s *. y, s *. z)
let pscal (x0, y0, z0) (x1, y1, z1) = (x0 *. x1) +. (y0 *. y1) +. (z0 *. z1)
let norm r = 1. /. sqrt (pscal r r) *| r
let pvect (x0, y0, z0) (x1, y1, z1) =
( (z0 *. y1) -. (z1 *. y0),
(x0 *. z1) -. (x1 *. z0),
(y0 *. x1) -. (y1 *. x0) )
let unit_normal a b c = norm (pvect (c -| b) (a -| b))
let light_on = ref true
let get_light () = !light_on
let toggle_light () =
light_on := not !light_on;
reset_gllist := true
let switch_light bool =
match bool with
| true ->
Gl.enable Gl.lighting;
Gl.lightfv Gl.light0 Gl.position [| 1.; -1.; 1.; 0.5 |];
Gl.lightfv Gl.light0 Gl.specular [| 0.; 0.; 0.; 1. |];
Gl.lightfv Gl.light0 Gl.diffuse [| 0.2; 0.2; 0.2; 0.8 |];
Gl.light_modelf Gl.light_model_two_side 1.0;
Gl.enable Gl.light0;
Gl.materialf Gl.front Gl.shininess 30.;
Gl.materialfv Gl.front Gl.emission [| 0.2; 0.2; 0.2; 1. |];
Gl.materialf Gl.back Gl.shininess 10.;
Gl.materialfv Gl.back Gl.emission [| 0.1; 0.1; 0.1; 1. |];
Gl.enable Gl.color_material_enum;
Gl.color_material Gl.front_and_back Gl.specular;
Gl.color_material Gl.front_and_back Gl.ambient_and_diffuse
| false ->
Gl.disable Gl.lighting;
Gl.disable Gl.color_material_enum
let enter3d ({ Point3.x = x1; y = y1; _ }, { Point3.x = x2; y = y2; _ }) =
Gl.push_matrix ();
Gl.matrix_mode Gl.projection;
Gl.push_matrix ();
Gl.load_identity ();
Gl.ortho x1 x2 y1 y2 (-100.) 100.;
Gl.matrix_mode Gl.modelview;
Gl.load_identity ();
Gl.translatef 0. 0. (-50.);
switch_light !light_on;
let zoom = !zoom3d in
Gl.scalef zoom zoom zoom;
let rot = Geom.q_matrix_ba !position3d in
Gl.mult_matrixf rot;
Gl.enable Gl.depth_test
let leave3d () =
Gl.disable Gl.depth_test;
Gl.matrix_mode Gl.projection;
switch_light false;
Gl.pop_matrix ();
Gl.matrix_mode Gl.modelview;
Gl.pop_matrix ()
let getx p = p.x
let gety p = p.y
let mymap f pl c =
let myget = match c with X -> getx | Y -> gety in
match pl with
| [] -> raise Empty_list
| p :: ppl ->
let xlist l = List.rev_map myget l in
List.fold_left f (myget p) (xlist ppl)
let fmin x y : float = if y < x then y else x
let fmax x y : float = if y > x then y else x
let xmin pl = mymap fmin pl X
let xmax pl = mymap fmax pl X
let ymin pl = mymap fmin pl Y
let ymax pl = mymap fmax pl Y
let rescale_list pl v (bx0, by0, bx1, by1) =
match v with
| None ->
if pl <> [] then raise View_expected
else begin
Debug.print "Warning: no view provided for rescale_list";
[]
end
| Some ({ x = x0; y = y0 }, { x = x1; y = y1 }) ->
let xmin, xfactor =
if x1 = x0 then (-0.5, bx1 -. bx0)
else (x0, (bx1 -. bx0) /. (x1 -. x0))
in
let ymin, yfactor =
if y1 = y0 then (-0.5, by1 -. by0)
else (y0, (by1 -. by0) /. (y1 -. y0))
in
let dr_of_point p =
let myi x = bx0 +. ((x -. xmin) *. xfactor) in
let myj y = by0 +. ((y -. ymin) *. yfactor) in
(myi (getx p), myj (gety p))
in
List.rev_map dr_of_point pl
let draw_of_point p v (bx0, by0, bx1, by1) =
List.hd (rescale_list [ p ] v (bx0, by0, bx1, by1))
let rescale_3dpoint (x, y, z) (x0, y0, z0) (x1, y1, z1) =
( (x -. x0) *. 2. /. (x1 -. x0),
(y -. y0) *. (12. /. 8.) /. (y1 -. y0),
-2. +. ((z -. z0) *. 4. /. (z1 -. z0)) )
let rescale_3dlist pl = List.rev_map rescale_3dpoint pl
let point_of_draw (dx, dy) (bx0, by0, bx1, by1) = function
| None -> raise View_expected
| Some ({ x = x0; y = y0 }, { x = x1; y = y1 }) ->
((x1 -. x0) *. dx /. (bx1 -. bx0), (y1 -. y0) *. dy /. (by1 -. by0))
let point_of_pixel (dx, dy) = function
| None -> raise View_expected
| Some ({ x = x0; y = y0 }, { x = x1; y = y1 }) ->
( float dx *. (x1 -. x0) /. !fwindow_width,
float dy *. (y1 -. y0) /. !fwindow_height )
let rec maxview po =
match po with
| Points pl | Poly pl ->
if pl = [] then None
else
let x0, y0, x1, y1 =
(mymap fmin pl X, mymap fmin pl Y, mymap fmax pl X, mymap fmax pl Y)
in
let x1 = if x1 = x0 then x0 +. 1. else x1 in
let y1 = if y1 = y0 then y0 +. 1. else y1 in
Some (point (x0, y0), point (x1, y1))
| Lines pll -> maxview (Points (List.flatten pll))
| View v -> v
| Axis { center = { x = x0; y = y0 }; _ } ->
Some (point (x0 -. 1., y0 -. 1.), point (x0 +. 1., y0 +. 1.))
| Text t ->
let x, y = (t.pos.x, t.pos.y) in
Some (point (x -. 1., y -. 1.), point (x +. 1., y +. 1.))
| Matrix _ -> None
| Grid ((_, v3, _), _) -> view2of3 v3
| Surf3d ((_, _, _, v3, _), _) -> view2of3 v3
| Adapt (_, f) -> maxview (f None)
| User _ | Anim _ ->
None
| _ -> None
let gl2fig gldraw_func plot_func =
let was_init = Sdl.Init.test (Sdl.was_init None) Sdl.Init.video in
let r, num = feedback_render gldraw_func in
Debug.print "feedback_render OK";
let parsed = feedback_parse r num in
let fb_view, fb_list = (maxview (List.hd parsed), List.tl parsed) in
List.iter (fun o -> plot_func ~dev:FIG o fb_view) fb_list;
if not was_init then close () ~dev:GL
let copy_back_buffer () =
Gl3.draw_buffer Gl3.front;
Gl3.read_buffer Gl3.back;
Gl.copy_pixels 0 0 !window_width !window_height Gl.color;
Gl3.draw_buffer Gl3.back;
Gl3.read_buffer Gl3.back
let copy_to_back_buffer () =
Gl3.draw_buffer Gl3.back;
Gl3.read_buffer Gl3.front;
Gl.copy_pixels 0 0 !window_width !window_height Gl.color;
Gl3.read_buffer Gl3.back
let buffer_enum i =
assert (i >= 0 && i < 16);
Gl3.draw_buffer0 + i
let copy_buffer i =
Gl3.draw_buffer (buffer_enum i);
Gl3.read_buffer Gl3.back;
Gl.copy_pixels 0 0 !window_width !window_height Gl.color;
Gl3.draw_buffer Gl3.back;
Gl3.read_buffer Gl3.back
let recall_buffer i =
Gl3.read_buffer (buffer_enum i);
Gl3.draw_buffer Gl3.back;
Gl.copy_pixels 0 0 !window_width !window_height Gl.color;
Gl3.draw_buffer Gl3.back;
Gl3.read_buffer Gl3.back
let user_flush = function
| GRAPHICS -> Graphics.synchronize ()
| GL -> (
match !default_gl with
| GLUT -> Iglut.swapbuffers ()
| SDL -> do_option !win Sdl.gl_swap_window
| GTK -> () )
| FIG -> ()
let sdl_get_pixel_not_used surface x y =
let pitch = Sdl.get_surface_pitch surface in
let format_enum = Sdl.get_surface_format_enum surface in
if format_enum <> Sdl.Pixel.format_argb8888 then begin
Sdl.log "sdl_get_pixel: surface has wrong format";
exit 1
end;
go (Sdl.lock_surface surface);
let pixels = Sdl.get_surface_pixels surface Bigarray.int8_unsigned in
let i0 = (y * pitch) + (4 * x) in
let open Bigarray in
let b = Array1.get pixels i0 in
let g = Array1.get pixels (i0 + 1) in
let r = Array1.get pixels (i0 + 2) in
let a = Array1.get pixels (i0 + 3) in
Sdl.unlock_surface surface;
((r, g, b), a)
let latex_to_sdl message size =
let current_dir = Sys.getcwd () in
Sys.chdir !tmp_dir;
let latex_channel = open_out latex_tmp in
output_string latex_channel
"\\documentclass{article}\n\
\\usepackage{color}\n\
\\usepackage[utf8]{inputenc}\n\
\\usepackage[active,tightpage]{preview}\n\
\\begin{document}\n\
\\begin{preview}\n";
output_string latex_channel message;
output_string latex_channel "\\end{preview}\n\\end{document}\n";
close_out latex_channel;
let base_name = Filename.chop_suffix latex_tmp ".tex" in
shell "latex '\\nonstopmode\\input{%s}'" latex_tmp;
shell "dvips -D 600 %s.dvi -o %s.ps" base_name base_name;
shell
"gs -sDEVICE=pngalpha -dTextAlphaBits=4 -r%d -dGraphicsAlphaBits=4 \
-dSafer -q -dNOPAUSE -sOutputFile=%s.png %s.ps -c quit"
(size * 6)
base_name base_name;
let image =
Tsdl_image.Image.load (Printf.sprintf "%s.png" base_name) |> go
in
Sys.chdir current_dir;
if Sdl.get_surface_format_enum image = Sdl.Pixel.format_argb8888 then image
else Sdl.convert_surface_format image Sdl.Pixel.format_argb8888 |> go
let text_image message size flag =
match flag with
| Normal ->
if size <> !current_font_size then (
current_font := Sdlttf.open_font !font_path size |> go;
current_font_size := size);
let s =
Sdlttf.render_utf8_blended !current_font message
(sdl_color (opaque white))
|> go
in
Sdl.convert_surface_format s Sdl.Pixel.format_abgr8888 |> go
| Latex -> latex_to_sdl message size
let draw_image ?(mode = Gl.modulate) image x0 y0 =
Gl3.tex_parameteri Gl3.texture_2d Gl3.texture_mag_filter Gl3.nearest;
Gl3.tex_parameteri Gl3.texture_2d Gl3.texture_min_filter Gl3.nearest;
go (Sdl.lock_surface image);
let pixels = Sdl.get_surface_pixels image Bigarray.int8_unsigned in
let w, h = Sdl.get_surface_size image in
Gl3.tex_image2d Gl3.texture_2d 0 Gl3.rgba w h 0 Gl3.rgba Gl3.unsigned_byte
(`Data pixels);
Sdl.unlock_surface image;
Gl.enable Gl.texture_2d;
let rx, ry = (float w /. !fwindow_width, float h /. !fwindow_height) in
Gl.tex_envi Gl.texture_env Gl.texture_env_mode mode;
Gl.gl_begin Gl.quads;
Gl.tex_coord2d 0.0 0.0;
Gl.vertex2d x0 (y0 +. ry);
Gl.tex_coord2d 0.0 1.0;
Gl.vertex2d x0 y0;
Gl.tex_coord2d 1.0 1.0;
Gl.vertex2d (x0 +. rx) y0;
Gl.tex_coord2d 1.0 0.0;
Gl.vertex2d (x0 +. rx) (y0 +. ry);
Gl.gl_end ();
Gl.disable Gl.texture_2d
let sdl_screenshot ?(output = png_output) () =
Gl.finish ();
let w = !window_width + !left_margin + !right_margin in
let h = !window_height + !top_margin + !bottom_margin in
let ba_gl =
Bigarray.Array1.create Bigarray.int8_unsigned Bigarray.c_layout (w * h * 4)
in
Gl3.read_pixels 0 0 w h Gl3.rgba Gl3.unsigned_byte (`Data ba_gl);
let ba_sdl =
Bigarray.Array1.create Bigarray.int8_unsigned Bigarray.c_layout (w * h * 4)
in
let pitch = 4 * w in
for y = 0 to h - 1 do
let src = Bigarray.Array1.sub ba_gl (y * pitch) pitch in
let dst = Bigarray.Array1.sub ba_sdl ((h - 1 - y) * pitch) pitch in
Bigarray.Array1.blit src dst
done;
let s =
Sdl.create_rgb_surface_from ba_sdl ~w ~h ~depth:32 ~pitch:(w * 4)
0x000000ffl
0x0000ff00l
0x00ff0000l
0xff000000l
|> go
in
match Tsdl_image.Image.save_png s output with
| 0 -> print_endline (Printf.sprintf "Screenshot saved to [%s]." output)
| i -> Sdl.log "Error %i when saving screenshot to: %s" i output
let set_line_width ?(dev = !default_device) w =
match dev with
| GRAPHICS -> Graphics.set_line_width (int_of_float w)
| GL -> Gl3.line_width w
| FIG -> Debug.print "Not implemented: fig set_line_width"
let set_point_size ?(dev = !default_device) w =
match dev with
| GRAPHICS -> raise (Not_implemented "GRAPHICS set_point_size")
| GL -> Gl3.point_size w
| FIG -> raise (Not_implemented "fig set_line_size")
let set_color ?(dev = !default_device) c =
(match dev with
| GRAPHICS ->
let r, g, b = int_of_color c in
Graphics.set_color (Graphics.rgb r g b)
| GL -> gl_draw_color c
| FIG ->
if fig_of_color c = -1 then (
let r = rgb_of_color c in
Printf.fprintf !xfig_head_channel "0 %d #%.6x\n" !fig_color_counter r;
fig_colors.(!fig_color_counter) <- r;
incr fig_color_counter));
current_color := c
let linear_cmap color1 color2 x =
let f x u1 u2 = (x *. u2) +. ((1. -. x) *. u1) in
{
r = f x color1.r color2.r;
g = f x color1.g color2.g;
b = f x color1.b color2.b;
}
let from_black_cmap = linear_cmap black
let from_white_cmap = linear_cmap white
let to_white_cmap color = linear_cmap color white
let to_black_cmap color = linear_cmap color black
let draw_points pl ?(dev = !default_device) ?dep ?(pixel_size = 2) view =
let ps = rescale_list pl view (bounding_box dev) in
match dev with
| GRAPHICS ->
Graphics.plots
(Array.of_list
(List.rev_map (fun (x, y) -> (int_of_float x, int_of_float y)) ps))
| GL ->
Gl.gl_begin Gl.points;
List.iter (fun (x, y) -> Gl.vertex2f x y) ps;
Gl.gl_end ()
| FIG ->
let ps =
match view with
| Some v ->
rescale_list
(lines_crop pl v |> List.flatten)
view (bounding_box dev)
| None ->
Debug.print "draw_points needs a view";
ps
in
let depth = get_depth dep and co = fig_of_color !current_color in
List.iter
(fun (x, y) ->
Printf.fprintf !xfig_main_channel
"2 1 0 %u %d %d %d -1 -1 0.000 0 0 -1 0 0 1\n" pixel_size co co
depth;
Printf.fprintf !xfig_main_channel "\t%d %d\n" (int_of_float x)
(int_of_float y))
ps
let draw_lines pl ~dev ?dep view =
let ps = rescale_list pl view (bounding_box dev) in
match dev with
| GRAPHICS ->
Graphics.draw_poly_line
(Array.of_list
(List.rev_map (fun (x, y) -> (int_of_float x, int_of_float y)) ps))
| GL ->
Gl.gl_begin Gl.line_strip;
List.iter (fun (x, y) -> Gl.vertex2f x y) ps;
Gl.gl_end ()
| FIG ->
let depth = get_depth dep and co = fig_of_color !current_color in
Printf.fprintf !xfig_main_channel
"2 1 0 %u %d %d %d -1 -1 0.000 0 0 -1 0 0 %d\n" 1 co co
depth (List.length ps);
List.iter
(fun (x, y) ->
Printf.fprintf !xfig_main_channel "\t%d %d\n" (int_of_float x)
(int_of_float y))
ps
let draw_lines pl ?(dev = !default_device) ?dep view =
if dev = FIG then (
let pls =
match view with
| None -> raise View_expected
| Some v -> lines_crop pl v
in
if Debug.debug && List.length pls > 1 then
print_endline "Cropping overflowing Lines for FIG rendering";
List.iter (fun pl -> draw_lines pl ~dev ?dep view) pls)
else draw_lines pl ~dev ?dep view
let draw_poly pl ?(dev = !default_device) ?border_color ?dep view =
let ps = rescale_list pl view (bounding_box dev) in
match dev with
| GRAPHICS ->
Graphics.fill_poly
(Array.of_list
(List.rev_map (fun (x, y) -> (int_of_float x, int_of_float y)) ps))
| GL ->
Gl.gl_begin Gl.polygon;
List.iter (fun (x, y) -> Gl.vertex2f x y) ps;
Gl.gl_end ();
Gl.gl_begin Gl.line_loop;
List.iter (fun (x, y) -> Gl.vertex2f x y) ps;
Gl.gl_end ()
| FIG ->
let depth = get_depth dep and co = fig_of_color !current_color in
let bco =
match border_color with None -> co | Some bc -> fig_of_color bc
in
let pps = List.append ps [ List.hd ps ] in
Printf.fprintf !xfig_main_channel
"2 3 0 0 %d %d %d -1 20 0.000 0 0 -1 0 0 %d\n" bco co depth
(List.length pps);
List.iter
(fun (x, y) ->
Printf.fprintf !xfig_main_channel "\t%d %d\n" (int_of_float x)
(int_of_float y))
pps
let draw_matrix ?(dev = !default_device) ?(cmap = from_white_cmap)
?(min_value = 0) ?(max_value = 255) m =
let h = Array.length m and w = Array.length m.(0) in
let dc = float (max_value - min_value) in
let cmap = cmap !current_color in
begin match dev with
| GRAPHICS -> raise (Not_implemented "GRAPHICS draw_matrix")
| GL ->
let dx = 1. /. float w and dy = 1. /. float h in
for i = 0 to h - 1 do
for j = 0 to w - 1 do
let c = float (m.(i).(j) - min_value) /. dc in
let x = float j *. dx and y = float i *. dy in
gl_draw_color (cmap c);
Gl.gl_begin Gl.quads;
Gl.vertex2f x y;
Gl.vertex2f x (y +. dy);
Gl.vertex2f (x +. dx) (y +. dy);
Gl.vertex2f (x +. dx) y;
Gl.gl_end ()
done
done
| FIG ->
let xmin, ymin, xmax, ymax =
(0., 0., float w, float h)
in
let view = Some ({ x = xmin; y = ymin }, { x = xmax; y = ymax }) in
let dx = (xmax -. xmin) /. float w and dy = (ymax -. ymin) /. float h in
for i = 0 to h - 1 do
for j = 0 to w - 1 do
let c = float (m.(i).(j) - min_value) /. dc in
let x = xmin +. (float j *. dx) and y = ymin +. (float i *. dy) in
set_color ~dev (cmap c);
draw_poly ~dev
[
{ x; y };
{ x; y = y +. dy };
{ x = x +. dx; y = y +. dy };
{ x = x +. dx; y };
]
view
done
done
end;
set_color !current_color
let move3d m =
let t0 =
match m.init_time with
| None ->
(match m.move with
| Translate _ -> ()
| Rotate _ -> ()
| Zoom (_, Some z0) -> zoom3d := z0
| Zoom _ -> ());
let t = time () - int_of_float (1000. *. m.time.min) in
m.init_time <- Some t;
t
| Some t -> t
in
if m.time.min < m.time.max then begin
let t = m.time.min in
let t' = float (time () - t0) /. 1000. in
m.time.min <- t';
let dt = t' -. t in
match m.move with
| Translate _ -> raise (Not_implemented "translate3d")
| Rotate q -> position3d := Geom.q_mult q !position3d
| Zoom (z, _) -> zoom3d := !zoom3d *. (1. +. (dt *. z t))
end
let normal3 (x, y, z) = Gl.normal3f x y z
let vertex3 (x, y, z) = Gl.vertex3f x y z
let rec draw_surf3d ?(dev = !default_device) ?(wire = true) gl plot_func mx my
mz (p1, p2) =
match dev with
| GRAPHICS -> raise (Not_implemented "GRAPHICS draw_surf3d")
| GL -> begin
enter3d (p1, p2);
match !gl with
| Some list when not !reset_gllist -> Gl.call_list list
| _ ->
Debug.print "Creating display list";
let list = Gl.gen_lists 1 in
Gl.new_list list Gl.COMPILE_AND_EXECUTE;
let h = Array.length mx - 2 and w = Array.length mx.(0) - 2 in
let { r; g; b } = !current_color in
let zmin = p1.Point3.z and zmax = p2.Point3.z in
let setcolor =
if (r *. r) +. (g *. g) +. (b *. b) > 1. then
fun ((_, _, z) : float * float * float) ->
let c = (z -. zmin) /. (zmax -. zmin) in
gl_draw_color { r = r *. c; g = g *. c; b = b *. c }
else fun ((_, _, z) : float * float * float) ->
let c = (z -. zmin) /. (zmax -. zmin) in
gl_draw_color
{
r = c +. r -. (r *. c);
g = c +. g -. (g *. c);
b = c +. b -. (b *. c);
}
in
let a i j = (mx.(i).(j), my.(i).(j), mz.(i).(j)) in
let normal_vector i j =
let a0, a1, a2, a3, a4 =
(a i j, a i (j - 1), a (i + 1) j, a i (j + 1), a (i - 1) j)
in
unit_normal a0 a1 a2
+| unit_normal a0 a2 a3
+| unit_normal a0 a3 a4
+| unit_normal a0 a4 a1
in
let a0 = ref (0., 0., 0.) and a1 = ref (0., 0., 0.) in
for i = 1 to h - 1 do
a0 := (mx.(i).(1), my.(i).(1), mz.(i).(1));
a1 := (mx.(i + 1).(1), my.(i + 1).(1), mz.(i + 1).(1));
gl_draw_color !current_color;
for j = 1 to w - 1 do
let a3 = (mx.(i).(j + 1), my.(i).(j + 1), mz.(i).(j + 1))
and a2 =
(mx.(i + 1).(j + 1), my.(i + 1).(j + 1), mz.(i + 1).(j + 1))
in
Gl.gl_begin Gl.quads;
if not !light_on then setcolor !a0;
normal3 (normal_vector i j);
vertex3 !a0;
if not !light_on then setcolor !a1;
normal3 (normal_vector (i + 1) j);
vertex3 !a1;
if not !light_on then setcolor a2;
normal3 (normal_vector (i + 1) (j + 1));
vertex3 a2;
if not !light_on then setcolor a3;
normal3 (normal_vector i (j + 1));
vertex3 a3;
Gl.gl_end ();
a0 := a3;
a1 := a2
done
done;
if wire then begin
switch_light false;
let r, g, b =
if !light_on then (r, g, b) else (r /. 2., g /. 2., b /. 2.)
in
gl_draw_color { r; g; b };
for ii = 1 to h / 2 do
let i = ii * 2 in
Gl.gl_begin Gl.line_strip;
for j = 1 to w do
let a = (mx.(i).(j), my.(i).(j), mz.(i).(j)) in
vertex3 a
done;
Gl.gl_end ()
done;
for jj = 1 to w / 2 do
let j = jj * 2 in
Gl.gl_begin Gl.line_strip;
for i = 1 to h do
let a = (mx.(i).(j), my.(i).(j), mz.(i).(j)) in
vertex3 a
done;
Gl.gl_end ()
done
end;
leave3d ();
Gl.end_list ();
Debug.print "display list created.";
gl := Some list
end
| FIG ->
let draw () =
draw_surf3d ~dev:GL ~wire:true gl plot_func mx my mz (p1, p2)
in
gl2fig draw plot_func
let rec draw_grid gl ?(dev = !default_device) ?(wire = true) plot_func m
(p1, p2) =
let h = Array.length m - 1 and w = Array.length m.(0) - 1 in
let { r; g; b } = !current_color in
match dev with
| GRAPHICS -> raise (Not_implemented "GRAPHICS draw_grid")
| GL -> (
enter3d (p1, p2);
match !gl with
| Some list when not !reset_gllist -> Gl.call_list list
| _ ->
let list = Gl.gen_lists 1 in
Gl.new_list list Gl.COMPILE_AND_EXECUTE;
let { Point3.x = x1; y = y1; z = zmin } = p1
and { Point3.x = x2; y = y2; z = zmax } = p2 in
let dx = (x2 -. x1) /. float w and dy = (y2 -. y1) /. float h in
let setcolor =
if (r *. r) +. (g *. g) +. (b *. b) > 1. then fun z ->
let c = (z -. zmin) /. (zmax -. zmin) in
gl_draw_color { r = r *. c; g = g *. c; b = b *. c }
else fun z ->
let c = (z -. zmin) /. (zmax -. zmin) in
gl_draw_color
{
r = c +. r -. (r *. c);
g = c +. g -. (g *. c);
b = c +. b -. (b *. c);
}
in
for i = 0 to h - 1 do
let y = y1 +. (float i *. dy) in
for j = 0 to w - 1 do
let x = x1 +. (float j *. dx) and z = m.(i).(j) in
setcolor z;
Gl.gl_begin Gl.quads;
vertex3 (x, y, z);
let z = m.(i + 1).(j) in
setcolor z;
vertex3 (x, y +. dy, z);
let z = m.(i + 1).(j + 1) in
setcolor z;
vertex3 (x +. dx, y +. dy, z);
let z = m.(i).(j + 1) in
setcolor z;
vertex3 (x +. dx, y, z);
Gl.gl_end ()
done
done;
gl_draw_color !current_color;
if wire then begin
for ii = 0 to h / 2 do
let i = ii * 2 in
let y = y1 +. (float i *. dy) and z = m.(i).(0) in
Gl.gl_begin Gl.line_strip;
vertex3 (x1, y, z);
for j = 1 to w do
let x = x1 +. (float j *. dx) and z = m.(i).(j) in
vertex3 (x, y, z)
done;
Gl.gl_end ()
done;
for jj = 0 to w / 2 do
let j = jj * 2 in
let x = x1 +. (float j *. dx) and z = m.(0).(j) in
Gl.gl_begin Gl.line_strip;
vertex3 (x, y1, z);
for i = 1 to h do
let y = y1 +. (float i *. dy) and z = m.(i).(j) in
vertex3 (x, y, z)
done;
Gl.gl_end ()
done
end;
leave3d ();
Gl.end_list ();
gl := Some list)
| FIG ->
let draw () = draw_grid ~dev:GL ~wire:true gl plot_func m (p1, p2) in
gl2fig draw plot_func
let rec draw_curve3d ?(dev = !default_device) gl plot_func p3d (p1, p2) =
match dev with
| GRAPHICS -> raise (Not_implemented "GRAPHICS draw_curve3d")
| GL -> begin
enter3d (p1, p2);
match !gl with
| Some list when not !reset_gllist -> Gl.call_list list
| _ ->
let list = Gl.gen_lists 1 in
Gl.new_list list Gl.COMPILE_AND_EXECUTE;
Gl.gl_begin Gl.line_strip;
List.iter (fun { Point3.x; y; z } -> vertex3 (x, y, z)) p3d;
Gl.gl_end ();
Gl.disable Gl.depth_test;
Gl.gl_begin Gl.points;
List.iter (fun { Point3.x; y; z } -> vertex3 (x, y, z)) p3d;
Gl.gl_end ();
leave3d ();
Gl.end_list ();
gl := Some list
end
| FIG ->
let draw () = draw_curve3d ~dev:GL gl plot_func p3d (p1, p2) in
gl2fig draw plot_func
let draw_segments pl ?(dev = !default_device) ?dep view =
let ps = rescale_list pl view (bounding_box dev) in
match dev with
| GRAPHICS ->
Graphics.draw_segments
(Array.of_list
(let rec pair l =
match l with
| (x0, y0) :: (x1, y1) :: ll ->
( int_of_float x0,
int_of_float y0,
int_of_float x1,
int_of_float y1 )
:: pair ll
| _ -> []
in
pair ps))
| GL ->
Gl.gl_begin Gl.lines;
List.iter (fun (x, y) -> Gl.vertex2f x y) ps;
Gl.gl_end ()
| FIG ->
let depth = get_depth dep and co = fig_of_color !current_color in
List.iter
(fun (x0, y0, x1, y1) ->
Printf.fprintf !xfig_main_channel
"2 1 0 %u %d %d %d -1 -1 0.000 0 0 -1 0 0 2\n\t%d %d %d %d\n"
1 co co depth x0 y0 x1 y1)
(let rec pair l =
match l with
| (x0, y0) :: (x1, y1) :: ll ->
( int_of_float x0,
int_of_float y0,
int_of_float x1,
int_of_float y1 )
:: pair ll
| _ -> []
in
pair ps)
let draw_text ?(dev = !default_device) ?dep view t =
let x0, y0 = draw_of_point t.pos view (bounding_box dev) in
match dev with
| GRAPHICS ->
Graphics.set_text_size (iscale t.size);
let size = max 6 (min 40 (iscale t.size) land 62) in
let font_desc =
Printf.sprintf "-*-fixed-*-r-*-*-%d-*-*-*-*-*-iso8859-*" size
in
let () =
try Graphics.set_font font_desc
with _ ->
Debug.print "Cannot find font: %s" font_desc;
Graphics.set_font "fixed"
in
let w, h = Graphics.text_size t.text in
let dx =
match t.align with CENTER -> w / 2 | LEFT -> 0 | RIGHT -> w
in
Graphics.moveto (int_of_float x0 - dx) (int_of_float y0 - (h / 2));
Graphics.draw_string t.text
| GL ->
let s =
match t.pix with
| Some surf when not !force_refresh -> surf
| _ ->
let pix = text_image t.text (iscale t.size) t.flag in
t.pix <- Some pix;
pix
in
let w, h = Sdl.get_surface_size s in
let dw, dh = draw_of_pixel (w, h) (bounding_box dev) in
let dx =
match t.align with CENTER -> dw /. 2. | LEFT -> 0. | RIGHT -> dw
in
Gl.Feedback.pass_through text_token;
draw_image s (x0 -. dx) (y0 -. (dh /. 2.)) ~mode:Gl.modulate
| FIG ->
let _, h =
( 315,
int_of_float
((match t.flag with Normal -> 8.1 | Latex -> 6.)
*. float t.size ) )
and depth = get_depth dep
and co = fig_of_color !current_color in
let fig_align =
match t.align with CENTER -> 1 | LEFT -> 0 | RIGHT -> 2
in
Printf.fprintf
!xfig_main_channel
"4 %d %d %d -1 %d %f 0.0000 %d 180 315 %d %d %s\\001\n" fig_align co
depth
(match t.flag with Normal -> 16 | Latex -> 0)
(float t.size *. 0.75 )
(match t.flag with Normal -> 4 | Latex -> 2)
(int_of_float x0)
(int_of_float y0 + (h / 2))
(String.escaped t.text)
let sign x =
if x > 0. then 1. else if x < 0. then -1. else raise Division_by_zero
let is_finite x =
classify_float x <> FP_infinite && classify_float x <> FP_nan
let is_nan x = not (is_finite x)
let draw_axis a ?(dev = !default_device) view =
let view_has_changed =
!force_refresh
||
match a.view with
| None -> true
| Some _ ->
not (a.view = view && a.window_size = (!window_width, !window_height))
in
let axis_segments, text_labels =
if view_has_changed then begin
a.view <- view;
a.window_size <- (!window_width, !window_height);
let { x = xa; y = ya } = a.center in
let { x = x0; y = y0 }, { x = x1; y = y1 } =
match view with None -> raise View_expected | Some v -> v
in
if is_nan x0 || is_nan y0 || is_nan x1 || is_nan y1 then (
Printf.sprintf
"ERROR: cannot draw axis with infinite view (%f,%f,%f,%f)" x0 y0 x1
y1
|> print_endline;
([], []))
else
let mymodf x =
let m = floor x +. 1. in
(x -. m, m)
in
let myround x =
let r, m = mymodf x and q = log 2. /. log 10. in
if -.r < q then m else m -. q
and minxunit, minyunit = point_of_pixel (iscale 18, iscale 18) view in
let xunit =
sign minxunit *. (10. ** myround (log10 (abs_float minxunit)))
and yunit =
sign minyunit *. (10. ** myround (log10 (abs_float minyunit)))
and vtick, htick = point_of_pixel (iscale 4, iscale 4) view in
let l_axes =
[
{ x = x0; y = ya };
{ x = x1; y = ya };
{ x = xa; y = y0 };
{ x = xa; y = y1 };
]
and l_hticks =
let rec ht i =
let xi = i *. xunit in
if abs_float (xi -. x0) > abs_float (x1 -. x0 -. vtick) then []
else
{ x = xi; y = ya -. htick }
:: { x = xi; y = ya +. htick }
:: ht (i +. 1.)
in
ht (floor (x0 /. xunit) +. 1.)
and l_vticks =
let rec vt i =
let yi = i *. yunit in
if abs_float (yi -. y0) > abs_float (y1 -. y0 -. htick) then []
else
{ x = xa -. vtick; y = yi }
:: { x = xa +. vtick; y = yi }
:: vt (i +. 1.)
in
vt (floor (y0 /. yunit) +. 1.)
and l_harrow, l_varrow =
let xmax, ymax = (fmax x0 x1, fmax y0 y1)
and ahtick, avtick = (abs_float htick, abs_float vtick) in
( [
{ x = xmax -. avtick; y = ya -. ahtick };
{ x = xmax; y = ya };
{ x = xmax -. avtick; y = ya +. ahtick };
{ x = xmax; y = ya };
],
[
{ x = xa -. avtick; y = ymax -. ahtick };
{ x = xa; y = ymax };
{ x = xa +. avtick; y = ymax -. ahtick };
{ x = xa; y = ymax };
] )
in
let xtunit =
sign minxunit *. (10. ** myround (log10 (1.7 *. abs_float minxunit)))
and ytunit =
sign minyunit *. (10. ** myround (log10 (1.2 *. abs_float minyunit)))
in
let xpos, xalign =
if x0 < xa -. (4. *. vtick) then (xa -. (1.5 *. vtick), RIGHT)
else (xa +. (1.5 *. vtick), LEFT )
and ypos =
if y0 < ya -. (3. *. htick) then ya -. (2.5 *. htick)
else ya +. (2.5 *. htick )
in
let l_hnum =
let rec hn i =
let xi = i *. xtunit in
if abs_float (xi -. x0) >= abs_float (x1 -. x0) then []
else
{
pos = { x = xi; y = ypos };
text = Printf.sprintf "%g" xi;
size = 10;
align = CENTER;
flag = Normal;
pix = None;
}
:: hn (i +. 1.)
in
hn (floor (x0 /. xtunit) +. 1.)
and l_vnum =
let rec vn i =
let yi = i *. ytunit in
if abs_float (yi -. y0) >= abs_float (y1 -. y0) then []
else
{
pos = { x = xpos; y = yi };
text = Printf.sprintf "%g" yi;
size = 10;
align = xalign;
flag = Normal;
pix = None;
}
:: vn (i +. 1.)
in
vn (floor (y0 /. ytunit) +. 1.)
in
let t =
( List.concat [ l_vticks; l_hticks; l_axes; l_harrow; l_varrow ],
List.concat [ l_hnum; l_vnum ] )
in
a.ticks <- Some t;
t
end
else match a.ticks with None -> raise View_expected | Some t -> t
in
draw_segments axis_segments view ~dev;
incr counter;
List.iter (draw_text view ~dev) text_labels
let line_width x = User (fun _ dev -> set_line_width ~dev (!gl_scale *. x))
let window_flush ?(dev = !default_device) () =
match dev with
| GRAPHICS -> Graphics.synchronize ()
| GL -> do_option !win Sdl.gl_swap_window
| FIG -> raise (Not_implemented "FIG flush")
let rotate3d ax ay =
let ry = Geom.q_rotation 0. 1. 0. (-.ay)
and rx = Geom.q_rotation 0. 0. 1. (-.ax) in
position3d := Geom.q_mult ry (Geom.q_mult rx !position3d)
let mincr z = z := !z *. 1.01
let mdecr z = z := !z *. 0.99
let gl_zoom_in () =
mincr zoom3d;
mincr zoom3d
let gl_zoom_out () =
mdecr zoom3d;
mdecr zoom3d
let gl_mouse_motion x y =
let dt = 0.01 /. !gl_scale in
let dX = dt *. float (x - !mouse_x) and dY = dt *. float (y - !mouse_y) in
rotate3d dY dX;
mouse_x := x;
mouse_y := y
let print_help () =
print_endline
@@ Printf.sprintf
"\n\
---------------:-------Oplot help---------\n\
'h' or '?' : this help message.\n\n\
'q' or ESC : quit\n\n\
arrows : rotate the 2D scene\n\
'=' : 2D zoom out\n\
SHIFT '=' : 2D zoom in\n\
TAB : reset 2D position\n\n\
CTRL arrows: : rotate the 3D scene\n\
CTRL '=' : 3D zoom out\n\
CTRL SHIFT '=' : 3D zoom in\n\
CTRL TAB : reset 3D position\n\
CTRL 'l' : toggle 3D lighting\n\n\
CTRL 'f' : toggle fullscreen (may change screen resolution)\n\
CTRL 's' : save screenshot (default file = \"%s\")\n\n\
CTRL 'z' : suspend (for debugging)\n\
---------------:---------------------------\n"
Sysinit.png_output
let sdl_key key =
let quit = ref false in
let modifier = Sdl.Event.(get key keyboard_keymod) in
(match Sdl.Event.(get key keyboard_keycode) with
| k when k = Sdl.K.question || k = Sdl.K.h -> print_help ()
| k when k = Sdl.K.left && modifier land Sdl.Kmod.ctrl <> 0 ->
rotate3d 0. (-0.02)
| k when k = Sdl.K.left -> gl_rotated2d 1.
| k when k = Sdl.K.right && modifier land Sdl.Kmod.ctrl <> 0 ->
rotate3d 0. 0.02
| k when k = Sdl.K.right -> gl_rotated2d (-1.)
| k when k = Sdl.K.up && modifier land Sdl.Kmod.ctrl <> 0 ->
rotate3d (-0.02) 0.
| k when k = Sdl.K.up -> Gl.rotated (-1.) 1. 0. 0.
| k when k = Sdl.K.down && modifier land Sdl.Kmod.ctrl <> 0 ->
rotate3d 0.02 0.
| k when k = Sdl.K.down -> Gl.rotated 1. 1. 0. 0.
| k
when k = Sdl.K.equals
&& modifier land Sdl.Kmod.shift <> 0
&& modifier land Sdl.Kmod.ctrl <> 0 ->
mincr zoom3d
| k when k = Sdl.K.equals && modifier land Sdl.Kmod.shift <> 0 ->
Gl.scalef 1.1 1.1 1.1
| k when k = Sdl.K.plus -> Gl.scalef 1.1 1.1 1.1
| k when k = Sdl.K.equals && modifier land Sdl.Kmod.ctrl <> 0 ->
mdecr zoom3d
| k when k = Sdl.K.equals -> Gl.scalef 0.91 0.91 0.91
| k when k = Sdl.K.tab && modifier land Sdl.Kmod.ctrl <> 0 ->
position3d := default_position3d;
zoom3d := default_zoom3d
| k when k = Sdl.K.tab ->
Gl.pop_matrix ();
Gl.push_matrix ();
reset_time ()
| k when k = Sdl.K.l && modifier land Sdl.Kmod.ctrl <> 0 ->
light_on := not !light_on;
reset_gllist := true;
Debug.print "Light = %b" !light_on
| k when k = Sdl.K.f && modifier land Sdl.Kmod.ctrl <> 0 ->
toggle_fullscreen ()
| k when k = Sdl.K.p -> decr pause_pass
| k when k = Sdl.K.escape || k = Sdl.K.q ->
close ();
quit := true
| k when k = Sdl.K.z && modifier land Sdl.Kmod.ctrl <> 0 ->
Debug.print "Suspended";
quit := true
| k when k = Sdl.K.s && modifier land Sdl.Kmod.ctrl <> 0 ->
sdl_screenshot ()
| _
when Sdl.get_mod_state () = 0
->
resume_pause := true
| _ -> ());
!quit
let sdl_mouse_close () =
Sdl.set_event_state Sdl.Event.mouse_motion Sdl.disable;
Sdl.set_event_state Sdl.Event.mouse_button_up Sdl.disable;
()
let sdl_mouse_action _mouse =
let but, (x, y) = Sdl.get_mouse_state () in
if but = Sdl.Button.lmask then gl_mouse_motion x (!window_height - y)
else sdl_mouse_close ()
let sdl_mouse_init mouse =
if Sdl.Event.(get mouse mouse_button_button) = Sdl.Button.left then begin
Sdl.set_event_state Sdl.Event.mouse_motion Sdl.enable;
Sdl.set_event_state Sdl.Event.mouse_button_up Sdl.enable;
let x, y =
Sdl.Event.(get mouse mouse_button_x, get mouse mouse_button_y)
in
mouse_x := x;
mouse_y := !window_height - y
end
let sdl_mouse_wheel mouse =
if Sdl.Event.(get mouse mouse_wheel_y) > 0 then gl_zoom_in ()
else if Sdl.Event.(get mouse mouse_wheel_y) < 0 then gl_zoom_out ()
let sdl_resize w h =
let hx = round (float h *. !dpi_scale) in
let wx = round (float w *. !dpi_scale) in
resize_window wx hx;
do_option !win (fun win ->
let rw, rh = Sdl.gl_get_drawable_size win in
if (rw, rh) <> (wx, hx) then (
Debug.print "Obtained: (%i,%i), wanted: (%i,%i). Forcing resize." rw
rh wx hx;
Sdl.set_window_size win ~w ~h));
gl_resize ()
let sdl_event eo =
let e = match eo with Some e -> e | None -> Sdl.Event.create () in
let rec loop eo =
if eo <> None || Sdl.poll_event (Some e) then
let quit = ref false in
let () =
match Sdl.Event.(enum (get e typ)) with
| `Mouse_button_down -> sdl_mouse_init e
| `Mouse_motion -> sdl_mouse_action e
| `Mouse_button_up -> sdl_mouse_close ()
| `Mouse_wheel -> sdl_mouse_wheel e
| `Key_down -> quit := sdl_key e
| `Window_event -> begin
match Sdl.Event.(window_event_enum (get e window_event_id)) with
| `Size_changed ->
let w, h =
Sdl.Event.(get e window_data1, get e window_data2)
in
sdl_resize (Int32.to_int w) (Int32.to_int h)
| `Close ->
close ();
quit := true
| _ -> ()
end
| _ -> ()
in
!quit || loop None
else false
in
loop eo
let sdl_freeze t =
let init_time = time () in
let e = Sdl.Event.create () in
let has_event = ref false in
Sdl.set_event_state Sdl.Event.window_event Sdl.disable;
do_option !win Sdl.gl_swap_window;
while (not !has_event) && (t = 0 || time () - init_time < t) do
has_event := Sdl.poll_event (Some e);
Sdl.delay (Int32.of_int !frame_length)
done;
time_delay := !time_delay + time () - init_time;
Sdl.set_event_state Sdl.Event.window_event Sdl.enable;
if !has_event && Sdl.Event.(get e typ) = Sdl.Event.key_down then
match Sdl.Event.(get e keyboard_keycode) with
| k when k = Sdl.K.escape || k = Sdl.K.q -> Sdl.push_event e |> ignore
| _ -> ()
let do_freeze ?(dev = !default_device) t =
match dev with
| GRAPHICS ->
Graphics.synchronize ();
if t = 0 then ignore (Graphics.read_key ())
else ignore (Unix.select [] [] [] (float t /. 1000.))
| GL -> (
if !counter <= !pause_pass then ()
else
match !default_gl with
| SDL ->
sdl_freeze t;
pause_pass := !counter
| GLUT -> Iglut.freeze t
| GTK -> () )
| FIG -> ()
let do_pause ?(dev = !default_device) t =
match dev with
| GRAPHICS ->
Graphics.synchronize ();
if t = 0 then ignore (Graphics.read_key ())
else ignore (Unix.select [] [] [] (float t /. 1000.))
| GL -> (
if !counter <= !pause_pass then ()
else
match !pause_time with
| None ->
pause_time := Some (time ());
do_not_draw := true
| Some pt -> (
match !resume_pause || (t != 0 && time () - pt >= t) with
| false -> do_not_draw := true
| true ->
resume_pause := false;
do_not_draw := false;
pause_pass := !counter;
pause_time := None))
| FIG -> ()
let clear_sheet ?(dev = !default_device) c =
match dev with
| GRAPHICS -> Graphics.clear_graph ()
| GL ->
gl_clear_color c;
Gl3.clear Gl3.color
| FIG -> raise (Not_implemented "FIG clear")
let exec_user f view dev =
let v = match view with
| None -> Debug.print "User object could not find a view; using a default";
default_view
| Some v -> v in
f v dev
let rec object_plot ?(addcounter = true) ~dev po view =
if addcounter then incr counter;
match po with
| Points pl -> draw_points pl view ~dev
| Lines pl -> List.iter (fun l -> draw_lines l view ~dev) pl
| Poly pl -> draw_poly pl view ~dev
| Axis a -> draw_axis a view ~dev
| Color c -> set_color c ~dev
| Text t -> draw_text view t ~dev
| Matrix m -> draw_matrix m ~dev
| Grid ((m, v3, w), gl) ->
let v3 = initialize_view3 v3 in
draw_grid ~wire:w gl (object_plot ~addcounter:false) m v3 ~dev
| Surf3d ((fx, fy, fz, v3, w), gl) ->
let v3 = initialize_view3 v3 in
draw_surf3d ~wire:w gl (object_plot ~addcounter:false) fx fy fz v3 ~dev
| Curve3d ((p3d, v3), gl) ->
let v3 = initialize_view3 v3 in
draw_curve3d gl (object_plot ~addcounter:false) p3d v3 ~dev
| Move3d m -> move3d m
| Adapt (vo, f) ->
let obj =
match (!vo, view) with
| (Some w, Some o), Some v when w = v -> o
| _ ->
let o = f view in
vo := (view, Some o);
o
in
object_plot ~dev obj view
| Pause t -> do_pause t ~dev
| Freeze t -> do_freeze t ~dev
| Clear c -> clear_sheet c ~dev
| View _ -> ()
| Anim f ->
let p = f (float (elapsed ()) /. 1000.) in
object_plot p view ~dev
| User f -> exec_user f view dev
| Sheet _ ->
raise (Invalid_argument "object_plot cannot accept Sheet argument")
let _anim_plot_old f ?step ?(t0 = 0.) ?(t1 = 0.) x0 x1 =
let userfu v dev =
let t =
if t1 = 0. then t0 +. (float (elapsed ()) /. 1000.)
else fmin t1 (t0 +. (float (elapsed ()) /. 1000.))
in
let p = plot (f t) ?step x0 x1 in
object_plot p (Some v) ~dev
in
User userfu
let anim_plot f ?step ?(t0 = 0.) ?(t1 = 0.) x0 x1 =
let animfu time =
let t =
if t1 = 0. then t0 +. time else fmin t1 (t0 +. time)
in
plot (f t) ?step x0 x1
in
Anim animfu
let repeat = Anim (fun _ -> Points [])
let gl_zoom_out t pop =
let first_time = ref None in
let foo _ _ =
match !first_time with
| None ->
first_time := Some (time ());
Debug.print "Initialisation"
| Some t0 when time () < t0 + t ->
Gl.scalef 0.91 0.91 0.;
do_pause 0
| _ ->
first_time := None;
resume_pause := true;
if pop then (
Gl.pop_matrix ();
Gl.push_matrix ())
in
foo
let gl_zoom_in t pop =
let first_time = ref None in
let foo _ _ =
match !first_time with
| None ->
first_time := Some (time ());
Debug.print "Initialisation"
| Some t0 when time () < t0 + t ->
Gl.scalef 1.1 1.1 0.;
do_pause 0
| _ ->
first_time := None;
resume_pause := true;
if pop then (
Gl.pop_matrix ();
Gl.push_matrix ())
in
foo
let cleanup dev = set_line_width ~dev !gl_scale
let rec draw ~dev sh view =
if !do_not_draw then ()
else
match view with
| Some _ -> (
match sh with
| Sheet [] -> cleanup dev
| Sheet (po :: ssh) -> (
match po with
| View vv -> draw (Sheet ssh) vv ~dev
| Sheet sssh ->
draw (Sheet sssh) None ~dev;
draw (Sheet ssh) view ~dev
| _ ->
draw po view ~dev;
draw (Sheet ssh) view ~dev)
| po -> object_plot po view ~dev)
| None -> (
match sh with
| Sheet [] -> ()
| Sheet (po :: ssh) ->
let v = maxview po in
draw po v ~dev;
draw (Sheet ssh) v ~dev
| po ->
let v = maxview po in
object_plot po v ~dev)
let draw ?(dev = !default_device) sh view =
draw ~dev sh view;
force_refresh := false;
reset_gllist := false
let force_refresh () = force_refresh := true
let graphics_resize () =
resize_window (Graphics.size_x ()) (Graphics.size_y ())
let graphics_init () =
scale_window ();
Graphics.open_graph (Printf.sprintf " %dx%d" !window_width !window_height);
Graphics.set_window_title "Oplot - Graphics Window";
Graphics.auto_synchronize false
let graphics_key key =
match key with
| '\027' ->
Graphics.close_graph ();
true
| _ -> false
let graphics_event () =
let status = Graphics.(wait_next_event [ Key_pressed; Button_down ]) in
if status.Graphics.keypressed then graphics_key status.Graphics.key
else if status.Graphics.button then (
graphics_resize ();
false)
else false
let rec graphics_mainloop sh =
let r, g, b = int_of_color default_color in
Graphics.set_color (Graphics.rgb r g b);
current_color := default_color;
Graphics.clear_graph ();
draw sh None ~dev:GRAPHICS;
Graphics.synchronize ();
if graphics_event () then () else graphics_mainloop sh
let unixtime =
let start = Unix.gettimeofday () in
fun () -> Unix.gettimeofday () -. start
let frames = ref 0
let ot = ref (int_of_float (unixtime ()))
let wait_event =
let e = Sdl.Event.create () in
fun () ->
let rec loop () =
if Sdl.poll_event (Some e) then e
else (
Sdl.delay 10l;
loop ())
in
loop ()
let rec sdl_mainloop sh wait =
incr frames;
let t = int_of_float (unixtime ()) in
if t <> !ot then begin
Debug.print "%d fps%!" !frames;
frames := 0;
ot := t
end;
Gl3.clear (Gl3.color_buffer_bit lor Gl3.depth_buffer_bit);
gl_draw_color default_color;
current_color := default_color;
counter := 0;
start_time := time ();
draw sh None ~dev:GL;
do_option !win Sdl.gl_swap_window;
let elapsed_time = time () - !start_time in
if elapsed_time < !frame_length then
Sdl.delay (Int32.of_int (!frame_length - elapsed_time))
else Sdl.delay 10l;
let e =
if wait && (!pause_pass = 0 || (!do_not_draw && !pause_pass != 0)) then
Some (wait_event ())
else None
in
if !do_not_draw then do_not_draw := false else pause_pass := 0;
if sdl_event e || !interrupt_request then () else sdl_mainloop sh wait
let gtk_mainloop sh =
Gl3.clear (Gl3.color_buffer_bit lor Gl3.depth_buffer_bit);
gl_draw_color default_color;
current_color := default_color;
reset_view3 ();
counter := 0;
start_time := time ();
draw sh None ~dev:GL;
Gl.flush ();
time () - !start_time
let xfig_init () =
init_fig_colors ();
xfig_main_channel := open_out (Printf.sprintf "%s.main" xfig_output_tmp);
xfig_head_channel := open_out (Printf.sprintf "%s.head" xfig_output_tmp);
output_string !xfig_head_channel
"#FIG 3.2 Produced by oplot.ml, Vu Ngoc San\n";
output_string !xfig_head_channel
"Portrait\nCenter\nMetric\nA4\n100.00\nSingle\n-2\n1200 2\n"
let write_fig_color c num =
let r = rgb_of_color c in
Printf.fprintf !xfig_main_channel "# User color :\n0 %d #%x\n" num r;
fig_colors.(num) <- r;
print_int r
let rec fig_first_pass sh num =
if sh = Sheet [] then ()
else if num > 543 then raise Fig_Too_Many_Colors
else
let next_sh, next_num =
match sh with
| Sheet (Color c :: ssh) when fig_of_color c = -1 ->
write_fig_color c num;
(ssh, num + 1)
| Sheet (_ :: ssh) -> (ssh, num)
| Color c when fig_of_color c = -1 ->
write_fig_color c num;
([], num + 1)
| _ -> ([], num)
in
fig_first_pass (Sheet next_sh) next_num
let xfig_mainloop sh =
set_color default_color ~dev:FIG;
fig_color_counter := 32;
draw sh None ~dev:FIG;
close_out !xfig_main_channel;
close_out !xfig_head_channel;
shell "cat %s.head %s.main > %s"
xfig_output_tmp xfig_output_tmp xfig_output_tmp;
shell "rm %s.head %s.main" xfig_output_tmp xfig_output_tmp
let rec sh_has_latex sh =
match sh with
| Sheet [] -> false
| Text t when t.flag = Latex -> true
| Sheet (po :: ssh) -> sh_has_latex po || sh_has_latex (Sheet ssh)
| _ -> false
let write_eps ?output ?(pdf = true) sh =
let convert = if pdf then fig2pdf else fig2eps in
let output =
match output with
| Some s -> s
| None -> if pdf then pdf_output else eps_output
in
if sh_has_latex sh then (
shell "%s --input=%s %s" convert latex_header xfig_output_tmp;
shell "cp %s.%s %s"
(Filename.remove_extension xfig_output_tmp)
(if pdf then "pdf" else "eps")
output;
if not pdf then begin
shell "mv -f %s %s.tmp.ps" output output;
shell "grep -v \"%%DocumentPaperSizes:\" %s.tmp.ps > %s" output output;
shell "rm %s.tmp.ps" output
end)
else
shell "fig2dev -L %s -F %s %s"
(if pdf then "pdf" else "eps")
xfig_output_tmp output;
Printf.sprintf "Output file: %s" output |> print_endline
let write_bmp ?(output = png_output) sh =
gl_init ~show:false ();
Gl3.clear Gl3.color_buffer_bit;
gl_draw_color default_color;
current_color := default_color;
counter := 0;
draw sh None ~dev:GL;
window_flush () ~dev:GL;
sdl_screenshot ~output ();
close () ~dev:GL
let disp ?(dev = !default_device) ?(fscreen = false) sh =
pause_init ();
reset_view3 ();
match dev with
| GRAPHICS ->
graphics_init ();
graphics_resize ();
graphics_mainloop sh
| GL -> (
gl_init ();
let wait = not (has_anim sh) in
if wait then (
Gl3.enable Gl3.line_smooth;
Gl3.hint Gl3.line_smooth Gl3.nicest)
else Gl3.hint Gl3.line_smooth Gl3.fastest;
resume_pause := false;
pause_pass := 0;
interrupt_request := false;
match !default_gl with
| GLUT ->
if fscreen then Iglut.fullscreen ();
Iglut.mainloop sh
| SDL ->
if fscreen <> !fullscreen then toggle_fullscreen ();
reset_time ();
Gl3.clear (Gl3.color_buffer_bit lor Gl3.depth_buffer_bit);
do_option !win Sdl.gl_swap_window;
sdl_mouse_close ();
sdl_mainloop sh wait
| GTK ->
reset_time ();
ignore (gtk_mainloop sh))
| FIG ->
xfig_init ();
xfig_mainloop sh
let get_gl_scale () = !gl_scale
let set_gl_scale s = gl_scale := s
let ( & ) a b = List.append a b
let display ?(dev = !default_user_device) ?(fscreen = false) ?output sh =
match dev with
| GRAPHICS_d -> disp (Sheet sh) ~dev:GRAPHICS
| GL_d -> disp (Sheet sh) ~dev:GL ~fscreen
| FIG_d ->
disp (Sheet sh) ~dev:FIG;
let output = default output fig_output in
shell "cp %s %s" xfig_output_tmp output;
Printf.sprintf "Output file: %s" output |> print_endline
| EPS_d ->
disp (Sheet sh) ~dev:FIG;
write_eps ?output ~pdf:false (Sheet sh)
| PDF_d ->
disp (Sheet sh) ~dev:FIG;
write_eps ?output ~pdf:true (Sheet sh)
| XFIG_d ->
disp (Sheet sh) ~dev:FIG;
shell "xfig -correct_font_size -zoom 1 %s &" xfig_output_tmp
| GV_d -> (
disp (Sheet sh) ~dev:FIG;
write_eps ~pdf:false ~output:eps_output_tmp (Sheet sh);
match psviewer with
| Some "gv" -> shell "gv --media=BBox --watch %s &" eps_output_tmp
| Some "kghostview" -> shell "kghostview --portrait %s &" eps_output_tmp
| Some prog -> shell "%s %s &" prog eps_output_tmp
| None -> print_endline "No postscript viewer found.")
| BMP_d ->
print_endline "Using PNG instead of BMP.";
write_bmp ?output (Sheet sh)
| PNG_d -> write_bmp ?output (Sheet sh)
| IMG_d -> (
write_bmp (Sheet sh);
match viewer with
| Some prog -> shell "%s %s &" prog png_output
| None -> print_endline "No image viewer found"
)
let display_eps sh =
disp (Sheet sh) ~dev:FIG;
write_eps (Sheet sh);
shell "gv --watch --scalebase=2 %s&" eps_output_tmp
let interrupt () =
interrupt_request := true;
if !window_open then begin
let e = Sdl.Event.create () in
Sdl.Event.(set e typ key_down);
Sdl.Event.(set e keyboard_state Sdl.pressed);
Sdl.Event.(set e keyboard_keycode Sdl.K.a);
Sdl.push_event e |> ignore
end
let interruption int =
prerr_endline (Printf.sprintf "\nInterruption:%d\n" int);
flush stderr;
if !window_open then begin
let e = Sdl.Event.create () in
Sdl.Event.(set e typ key_down);
Sdl.Event.(set e keyboard_state Sdl.pressed);
Sdl.Event.(set e keyboard_keycode Sdl.K.z);
Sdl.Event.(set e keyboard_keymod Sdl.Kmod.ctrl);
Sdl.push_event e |> ignore
end
else prerr_endline "Nothing";
flush stderr;
if int = Sys.sigint then (
remove_tmp_dir ();
exit 0)
let () =
Sys.set_signal Sys.sigusr1 (Sys.Signal_handle interruption);
Sys.set_signal Sys.sigint (Sys.Signal_handle interruption)
end