File: ge3d_ogl.c

package info (click to toggle)
vrwave 0.9-4
  • links: PTS
  • area: non-free
  • in suites: slink
  • size: 5,032 kB
  • ctags: 7,153
  • sloc: java: 15,050; ansic: 8,219; sh: 458; makefile: 181
file content (3731 lines) | stat: -rw-r--r-- 103,747 bytes parent folder | download | duplicates (3)
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
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
#if 0
//<copyright>
//
// Copyright (c) 1994,95,96,97
// Institute for Information Processing and Computer Supported New Media (IICM),
// Graz University of Technology, Austria.
//
// This file is part of VRweb.
//
// VRweb is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2, or (at your option)
// any later version.
//
// VRweb is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with VRweb; see the file LICENCE. If not, write to the
// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
//
//</copyright>
#endif

/*
 * File:     ge3d_ogl.c
 *
 * Authors:  Michael Pichler
 *
 * Created:   5 Sep 94  (taken from GL implementation)
 *
 * Changed:  14 Apr 97  Karl Heinz Wagenbrunn
 *
 * Changed:  21 May 97  Michael Pichler
 *
 * $Id: ge3d_ogl.c,v 1.45 1997/09/22 14:53:56 jwolte Exp $
 *
 */


/*
 * Implementation of Graphics Engine 3D
 *
 * for Open GL graphics library - and workalikes
 *
 */


#include "ge3drc.h"
const char* what_ge3d_1_5 =
  "@(#)[HG-LIB] ge3d\t1.5 [3D Graphics Engine for OpenGL (and workalikes)] [Michael Pichler]";


/*
 * GE3D_PROTOTYPES may determine whether you get a stable version of this library or not.
 *
 * When compiling ge3d with C++ it is set automatically and you don't have to worry about.
 *
 * When compiling with cc in K&R mode it must not be set;
 * note that the header files of the Mesa library require Ansi-C.
 *
 * When compiling with cc in Ansi-C mode or gcc you should set it for prototyping,
 * *unless* you link the library against a C++ frontend like cfront that calls
 * a K&R C-compiler for real compilation.
 */


#if !defined(PMAX) && !defined(HPUX9)
#define GE3D_PROTOTYPES
#endif


#include "ge3d.h"



/* switch between ANSI C function prototypes and old C function declarations */


#ifdef GE3D_PROTOTYPES

#  define PARMS1(t1,p1)                                 (t1 p1)
#  define PARMS2(t1,p1,t2,p2)                           (t1 p1, t2 p2)
#  define PARMS3(t1,p1,t2,p2,t3,p3)                     (t1 p1, t2 p2, t3 p3)
#  define PARMS4(t1,p1,t2,p2,t3,p3,t4,p4)               (t1 p1, t2 p2, t3 p3, t4 p4)
#  define PARMS5(t1,p1,t2,p2,t3,p3,t4,p4,t5,p5)         (t1 p1, t2 p2, t3 p3, t4 p4, t5 p5)
#  define PARMS6(t1,p1,t2,p2,t3,p3,t4,p4,t5,p5,t6,p6) \
   (t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6)
#  define PARMS7(t1,p1,t2,p2,t3,p3,t4,p4,t5,p5,t6,p6,t7,p7) \
   (t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7)
#  define PARMS8(t1,p1,t2,p2,t3,p3,t4,p4,t5,p5,t6,p6,t7,p7,t8,p8) \
   (t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7, t8 p8)
#  define PARMS9(t1,p1,t2,p2,t3,p3,t4,p4,t5,p5,t6,p6,t7,p7,t8,p8,t9,p9) \
   (t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7, t8 p8, t9 p9)
#  define PARMS10(t1,p1,t2,p2,t3,p3,t4,p4,t5,p5,t6,p6,t7,p7,t8,p8,t9,p9,t10,p10) \
   (t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7, t8 p8, t9 p9, t10 p10)
#  define PARMS11(t1,p1,t2,p2,t3,p3,t4,p4,t5,p5,t6,p6,t7,p7,t8,p8,t9,p9,t10,p10,t11,p11) \
   (t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7, t8 p8, t9 p9, t10 p10, t11 p11)

#else

#  define PARMS1(t1,p1)                                 (p1) t1 p1;
#  define PARMS2(t1,p1,t2,p2)                           (p1, p2) t1 p1; t2 p2;
#  define PARMS3(t1,p1,t2,p2,t3,p3)                     (p1, p2, p3) t1 p1; t2 p2; t3 p3;
#  define PARMS4(t1,p1,t2,p2,t3,p3,t4,p4)               (p1, p2, p3, p4) t1 p1; t2 p2; t3 p3; t4 p4;
#  define PARMS5(t1,p1,t2,p2,t3,p3,t4,p4,t5,p5)         (p1, p2, p3, p4, p5) t1 p1; t2 p2; t3 p3; t4 p4; t5 p5;
#  define PARMS6(t1,p1,t2,p2,t3,p3,t4,p4,t5,p5,t6,p6) \
   (p1, p2, p3, p4, p5, p6) t1 p1; t2 p2; t3 p3; t4 p4; t5 p5; t6 p6;
#  define PARMS7(t1,p1,t2,p2,t3,p3,t4,p4,t5,p5,t6,p6,t7,p7) \
   (p1, p2, p3, p4, p5, p6, p7) t1 p1; t2 p2; t3 p3; t4 p4; t5 p5; t6 p6; t7 p7;
#  define PARMS8(t1,p1,t2,p2,t3,p3,t4,p4,t5,p5,t6,p6,t7,p7,t8,p8) \
   (p1, p2, p3, p4, p5, p6, p7, p8) t1 p1; t2 p2; t3 p3; t4 p4; t5 p5; t6 p6; t7 p7; t8 p8;
#  define PARMS9(t1,p1,t2,p2,t3,p3,t4,p4,t5,p5,t6,p6,t7,p7,t8,p8,t9,p9) \
   (p1, p2, p3, p4, p5, p6, p7, p8, p9) t1 p1; t2 p2; t3 p3; t4 p4; t5 p5; t6 p6; t7 p7; t8 p8; t9 p9;
#  define PARMS10(t1,p1,t2,p2,t3,p3,t4,p4,t5,p5,t6,p6,t7,p7,t8,p8,t9,p9,t10,p10) \
   (p1, p2, p3, p4, p5, p6, p7, p8, p9, p10) t1 p1; t2 p2; t3 p3; t4 p4; t5 p5; t6 p6; t7 p7; t8 p8; t9 p9; t10 p10;
#  define PARMS11(t1,p1,t2,p2,t3,p3,t4,p4,t5,p5,t6,p6,t7,p7,t8,p8,t9,p9,t10,p10,t11,p11) \
   (p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11) \
   t1 p1; t2 p2; t3 p3; t4 p4; t5 p5; t6 p6; t7 p7; t8 p8; t9 p9; t10 p10; t11 p11;

#endif



#ifdef __PC__
#include "stdafx.h"
#define M_PI 3.14159265359
#endif

/* #include <GL/glx.h> */
#include <GL/glu.h>

#include <stdio.h>
#include <math.h>
#ifndef M_PI
#include <values.h>
#endif
#ifndef __PC__
#include <unistd.h>  /* not sure what it was needed for */
#endif
#include <stdlib.h>  /* malloc.h */

#include "stipple.h"  /* stipplePattern */

/* internal data of ge3d library: */

static int ge3d_mode = ge3d_wireframe;   /* default drawing mode: wireframe */
/* static long ge3d_overlay_drawmode = NORMALDRAW;  / * no overlays by default */
static int ge3d_do_texturing = 0;
static int ge3d_depthbuffering = 1;  /* use Z-Buffer for hidden-line/surface-elimination */
static int ge3d_backfaceculling = 1;  /* do backface culling */
static int ge3d_lighting = 1;  /* lighting in flat/smooth shading mode */
static int ge3d_texlighting = 0;  /* combine lighting and texturing */
static int ge3d_texmipmapping = ge3d_maxmipmap_quality;  /* OpenGL default: highest quality */
static int ge3d_diffuse = GL_DIFFUSE;  /* GL_DIFFUSE or GL_AMBIENT_AND_DIFFUSE */
static int ge3d_quadslices = 12;  /* slice count of quadrics; >= 4 */
static int ge3d_aaflags = 0;  /* no anti-aliasing by default */
static int ge3d_transparency = transp_stipple;  /* default: stipple (screendoor) transparency */
static int ge3d_ccw = 1;  /* default: frontfaces are defined ccw */

static float ge3d_tm [16];  /* scratch matrix data, ROW major order (see glMultMatrix) */

/* static float rgb_black [3] = { 0.0, 0.0, 0.0 }; */
/* static float rgb_gray [3] = { 0.3, 0.3, 0.3 }; */
static GLfloat fill_color [4] = { 1.0, 1.0, 1.0, 1.0 };
static float line_color [3] = { 1.0, 1.0, 1.0 };
static float backg_color [3] = { 0.0, 0.0, 0.0 };
static int samelfcolor = 1;  /* same line and fill color */
static point3D cur_position = { 0.0, 0.0, 0.0 };  /* for moveto/lineto */

static GLUquadricObj* gluquadobj = 0;

/* identity matrix need not be stored - glLoadIdentity loads one on the stack */

/* #define HL_OFFSET 0.0025 */
/* offset for pseudo hidden line; 0.002 to 0.003 seems fine */

#define HL_FACTOR 1.007
/* translation factor for pseudo hidden line; somewhere between 1.001 and 1.01 */


#define assigncolor( c, r, g, b ) \
(c) [0] = (r),  (c) [1] = (g),  (c) [2] = (b)

#define assignRGBA( c, r, g, b, a ) \
(c) [0] = (r),  (c) [1] = (g),  (c) [2] = (b),  (c) [3] = (a)


/* modify z translation for pseudo hidden line */
/* macro needs a float variable oldztran to save the old value */
/* TODO: consider using EXT_polygon_offset when available */
/* TODO: convert to a function! */

#define BEGINPOLYGONOFFSET(oldztran)  \
  glMatrixMode (GL_PROJECTION);  \
  glGetFloatv (GL_PROJECTION_MATRIX, ge3d_tm);  \
  oldztran = ge3d_tm [14];  \
  ge3d_tm [14] *= HL_FACTOR;  \
  glLoadMatrixf (ge3d_tm);  \
  glMatrixMode (GL_MODELVIEW);

#define ENDPOLYGONOFFSET(oldztran)  \
  glMatrixMode (GL_PROJECTION);  \
  ge3d_tm [14] = oldztran;  \
  glLoadMatrixf (ge3d_tm);  \
  glMatrixMode (GL_MODELVIEW);



void ge3d_openwindow ()
{
  /*** attention: this is not part of Open GL functionality!!! ***/
  ge3d_init_ ();

} /* ge3d_openwindow */



void ge3d_init_ ()
{
  glMatrixMode (GL_MODELVIEW);  /* multi matrix mode */

/* speed gain approx. 5 % */
/* fprintf (stderr, "test: normalization diabled\n"); */
/*  glDisable (GL_NORMALIZE); */
  glEnable (GL_NORMALIZE);  /* otherwise on scaling wrong normals (for lighting) */

  /*glEnable (GL_DITHER);*/  /* does not prevent bad flat shading on 8bit display */

  glDepthFunc (GL_LEQUAL);  /* use "<=" instead of "<" as z-buffer function */

  glColor4fv (fill_color);  /* default color */

  ge3d_mode = ge3d_wireframe;
  /* default: no backface removal, no depth buffer, no lighting */

  glPointSize (3);  /* to see anything */

/*glLightModeli (GL_LIGHT_MODEL_LOCAL_VIEWER, 1);*/  /* no significat difference for specular materials */

  glPixelStorei (GL_UNPACK_ALIGNMENT, 1);  /* texture images are byte (and not word) aligned */

  glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);  /* "normal" blending for anti-aliasing */
  glHint (GL_LINE_SMOOTH_HINT, GL_NICEST);  /* "best" line anti-aliasing */
  /* glHint (GL_POLYGON_SMOOTH_HINT, GL_NICEST);  / * "best" polygon anti-aliasing */
  /* polygon antialiasing does not work properly with Z-buffer! */

/* // textures now bottom-to-top by default
  glMatrixMode (GL_TEXTURE);
  glLoadIdentity ();
  glScalef (1.0, -1.0, 1.0);
  glMatrixMode (GL_MODELVIEW);
*/
  ge3dHint (hint_texlighting, 1);  /* texturing and lighting */

  ge3dTextureMipmapping (ge3d_texmipmapping);  /* texture filtering */

  if (!gluquadobj)
    gluquadobj = gluNewQuadric ();

  /* use default lighting model (could change ambient light) */

  /* only effective when GL_COLOR_MATERIAL enabled */
  glColorMaterial (GL_FRONT_AND_BACK, GL_DIFFUSE);

} /* ge3d_init_ */



/* ge3dHint */
/* flags that allow modification of ge3d behaviour */

void ge3dHint PARMS2 (
  int, flag,
  int, value
)
{
  switch (flag)
  {
    case hint_depthbuffer:
      /* must be set before activating a drawing mode to be effective */
      ge3d_depthbuffering = value;
    break;

    case hint_backfaceculling:
      /* may be changed any time */
      if (ge3d_backfaceculling != value)
      {
        ge3d_backfaceculling = value;
        if (value && ge3d_mode != ge3d_wireframe)
        { glEnable (GL_CULL_FACE);
          glLightModeli (GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
        }
        else
        { glDisable (GL_CULL_FACE);
          glLightModeli (GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
        }
      }
    break;

    case hint_lighting:
      /* may be changed at any time */
      if (ge3d_lighting != value)
      {
        ge3d_lighting = value;
        if (value && ge3d_mode >= ge3d_flat_shading)
          glEnable (GL_LIGHTING);
        else
          glDisable (GL_LIGHTING);
      }
    break;

    case hint_texlighting:
      if (ge3d_texlighting != value)
      { /* note: texturing+lighting not yet supported for "old style" ge3dPolyhedron (TODO) */
        ge3d_texlighting = value;
        /* did not work on SGI when texturing is disabled */
        glEnable (GL_TEXTURE_2D);
        glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, value ? GL_MODULATE : GL_DECAL);
        ge3dDoTexturing (ge3d_do_texturing);
        /* warning: behaviour of GL_DECAL undefined for 1 and 2 component textures */
      }
    break;

    case hint_ambientcolor:
      /* apply fillcolor to both ambient and diffuse or diffuse only (default) */
      /* only affects ge3d_setfillcolor, not ge3dMaterial */
      ge3d_diffuse = value ? GL_AMBIENT_AND_DIFFUSE : GL_DIFFUSE;
    break;

    case hint_quadslices:
      if (value < 4)
        ge3d_quadslices = 4;
      else
        ge3d_quadslices = value;
    break;

    case hint_transparency:
      ge3d_transparency = value;  /* valid from next ge3dMaterial call on */
      /* blending used for anti-aliasing and blended transparency */
      if (value == transp_blend || ge3d_aaflags)
        glEnable (GL_BLEND);
      else
        glDisable (GL_BLEND);
    break;

    case hint_ccw:
      if (ge3d_ccw != value)
      {
        ge3d_ccw = value;
        if (value)
          glFrontFace (GL_CCW);
        else
          glFrontFace (GL_CW);
      }
    break;
  }
} /* ge3dHint */



void ge3d_clearscreen ()
{
/*fprintf (stderr, "ge3d: setting clear color to (%g, %g, %g, 0.0)\n",
  backg_color [0], backg_color [1], backg_color [2]);
*/
  glClearColor (backg_color [0], backg_color [1], backg_color [2], 0.0);

  if ((ge3d_mode != ge3d_wireframe) && ge3d_depthbuffering)
    glClear ((GLbitfield) (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT));
  else
    glClear (GL_COLOR_BUFFER_BIT);
}



void ge3d_swapbuffers ()
{
  /* swapping of buffers is a part of GLX and not of pure OpenGL */
}



/* ge3d_apply_material: internal function for applying the i-th one
   out of materials (compare with public ge3dMaterial) */
/* fill_color and line_color are not changed */
/* index will be cached (no operation for same index),
   internal index can be cleared with NULL material */
/* a change occurs only when there is more than one component (it is
   assumed that the first one was activated ahead */
/* if there are too few items, they are cycled through individually */
/* scope hardwired to front and back (VRML) */

static void ge3d_apply_material PARMS2(
  const materialsGE3D*, materials,
  int, i
)
{
  static float color [4];
  const colorRGB* c;
  static int last_i = -1;  /* index of previous call */

/*fprintf (stderr, "applying material %d  ", i);*/

  float A = 1.0;  /* Alpha: 1.0 opaque, 0.0 fully transparent */
  register int n;

  /* caching */
  if (!materials)
  { last_i = -1;
    return;
  }
  if (i == last_i)
    return;
  last_i = i;

  n = materials->num_transparency;
  if (n && ge3d_lighting)
    A = 1.0 - materials->val_transparency [i % n];
  color [3] = A;

  /* use diffuse color (not base) because of GL_COLOR_MATERIAL */
  /* diffuse color always set with glColor call:
     of course, when !ge3d_lighting, and because GL_COLOR_MATERIAL is enabled on lighting */
  n = materials->num_diffuse;
  if (n > 1)
  { /* per face/vertex alpha change only implemented for blended transparency */
    if (ge3d_transparency == transp_blend && ge3d_lighting)
    { c = materials->rgb_diffuse + (i % n);
      if (A < 1)
        glColor4f (c->R, c->G, c->B, A);
      else
        glColor3fv ((float*) c);  /* alpha of 1.0 */
    }
    else
      glColor3fv ((float*) (materials->rgb_diffuse + (i % n)));
  }

#if 0
  /* dead code */
  { c = materials->rgb_diffuse + (i % n);
    if (ge3d_lighting && materials->multival)
    { assigncolor (color, c->R, c->G, c->B);
      glMaterialfv (GL_FRONT_AND_BACK, GL_DIFFUSE, color);
    }
    else  /* use diffuse color only */
    {
      if (A < 1.0)
      { assigncolor (color, c->R, c->G, c->B);
        glColor4fv (color);
      }
      else
        glColor3fv ((float*) c);  /* implicit alpha of 1.0 */
      return;
    }
  }
#endif

  if (!ge3d_lighting || !materials->multival)
    return;

  n = materials->num_ambient;
  if (n > 1)
  { c = materials->rgb_ambient + (i % n);
    assigncolor (color, c->R, c->G, c->B);
    glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT, color);
  }
  n = materials->num_specular;
  if (n > 1)
  { c = materials->rgb_specular + (i % n);
    assigncolor (color, c->R, c->G, c->B);
    glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR, color);
  }
  n = materials->num_emissive;
  if (n > 1)
  { c = materials->rgb_emissive + (i % n);
    assigncolor (color, c->R, c->G, c->B);
    glMaterialfv (GL_FRONT_AND_BACK, GL_EMISSION, color);
  }
  n = materials->num_shininess;
  if (n > 1)
    glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, materials->val_shininess [i % n] * 128.0);

} /* ge3d_apply_material */



/* ge3d_wire_material: as ge3d_apply_material, but for wireframe drawings */
/* TODO: integrate into render loop (n does not change) */

static void ge3d_wire_material PARMS2(
  const materialsGE3D*, materials,
  int, i
)
{
  /* use base color; no alpha wireframes */

  int n = materials->num_base;
  if (n > 1)
    glColor3fv ((float*) (materials->rgb_base + (i % n)));

} /* ge3d_wire_material */



void ge3d_moveto PARMS3(
float, x,
float, y,
float, z
)
{
  /* no pendant for moveto/lineto in Open GL - simulated */
  init3D (cur_position, x, y, z);
  /* should use ge3d_line/ge3dLine */
}


void ge3dMoveTo PARMS1(
const point3D*, p
)
{
  cur_position = *p;
}



void ge3d_lineto PARMS3(
float, x,
float, y,
float, z
)
{
  glBegin (GL_LINES);
  glVertex3fv ((float*) &cur_position);
  init3D (cur_position, x, y, z);
  glVertex3fv ((float*) &cur_position);
  glEnd ();
}


void ge3dLineTo PARMS1(
const point3D*, p
)
{
  glBegin (GL_LINES);
  glVertex3fv ((float*) &cur_position);
  cur_position = *p;
  glVertex3fv ((float*) &cur_position);
  glEnd ();
}


void ge3d_line PARMS6(  /* draw a line */
  float, x0,
  float, y0,
  float, z0,
  float, x1,
  float, y1,
  float, z1
)
{
  /* in GL lines drawn with move/draw were not subjected to lighting */
  /* in OpenGL all lines appear black while lighting is on (BUG?) */
  /* to achieve old behaviour turn off lighting while drawing lines */
  /* possibly inefficient - should think for a better solution */
  /* on the other hand I never considered it useful/necessary to shade lines */

  /* TODO: should use a variable ge3d_do_lighting instead (compare with ge3d_do_texturing) */
  register int didlighting = (ge3d_mode >= ge3d_flat_shading && ge3d_lighting);
  if (didlighting)
    glDisable (GL_LIGHTING);

  glBegin (GL_LINES);
  glVertex3f (x0, y0, z0);
  glVertex3f (x1, y1, z1);
  glEnd ();

  if (didlighting)
    glEnable (GL_LIGHTING);
}


void ge3dLine PARMS2(  /* draw a line */
  const point3D*, p,
  const point3D*, q
)
{
  register int didlighting = (ge3d_mode >= ge3d_flat_shading && ge3d_lighting);
  if (didlighting)  /* see ge3d_line */
    glDisable (GL_LIGHTING);

  glBegin (GL_LINES);
  glVertex3fv ((float*) p);
  glVertex3fv ((float*) q);
  glEnd ();

  if (didlighting)
    glEnable (GL_LIGHTING);
}


/* wirecube: wireframe of an aligned cube */
/* x0, x1 etc. need not be ordered */

void ge3d_wirecube PARMS6(
float, x0,
float, y0,
float, z0,
float, x1,
float, y1,
float, z1
)
{
  register int didlighting = (ge3d_mode >= ge3d_flat_shading && ge3d_lighting);

  if (didlighting)  /* ge3d_wirecube/ge3dWireCube should not be affected from lighting */
    glDisable (GL_LIGHTING);
  if (ge3d_do_texturing)  /* wirecube untextured */
    glDisable (GL_TEXTURE_2D);

  glColor3fv (line_color);
  glBegin (GL_LINE_LOOP);  /* bottom */
    glVertex3f (x0, y0, z0);
    glVertex3f (x0, y1, z0);
    glVertex3f (x1, y1, z0);
    glVertex3f (x1, y0, z0);
  glEnd ();
  glColor3fv (line_color);
  glBegin (GL_LINE_LOOP);  /* top */
    glVertex3f (x0, y0, z1);
    glVertex3f (x0, y1, z1);
    glVertex3f (x1, y1, z1);
    glVertex3f (x1, y0, z1);
  glEnd ();
  glColor3fv (line_color);
  glBegin (GL_LINES);  /* remaining edges */
    glVertex3f (x0, y0, z0);  glVertex3f (x0, y0, z1);
    glVertex3f (x0, y1, z0);  glVertex3f (x0, y1, z1);
    glVertex3f (x1, y0, z0);  glVertex3f (x1, y0, z1);
    glVertex3f (x1, y1, z0);  glVertex3f (x1, y1, z1);
  glEnd ();

  if (ge3d_do_texturing)
    glEnable (GL_TEXTURE_2D);
  if (didlighting)
    glEnable (GL_LIGHTING);

} /* ge3d_wirecube */



/* ge3dPolyLines2D (float* p)
   draws a sequence of (open) polylines,
   i.e. arbitrary many polylines in sequence,
   terminated with (float) 0 (!);
   each polyline begins with the number of points, followed
   by that many pairs of (x, y) coordinates;
   since the polyline is not automatically closed,
   only lines with two or more points make sense;
   all line attributes apply -
   line color should be set immediately before call
*/


void ge3dPolyLines2D (float* p)
{
  register unsigned int n;
  register int didlighting = (ge3d_mode >= ge3d_flat_shading && ge3d_lighting);
  if (didlighting)  /* sorry for need of this; see ge3d_line */
    glDisable (GL_LIGHTING);

  while ((n = (unsigned int) *p++) != 0)
  {
    glBegin (GL_LINE_STRIP);
    while (n--)  /* n points */
    { glVertex2fv (p);
      p += 2;
    }
    glEnd ();  /* line is not closed */
  }

  if (didlighting)
    glEnable (GL_LIGHTING);
}



/* cube: solid cube, arguments: two opposite vertices */

void ge3dCube PARMS2(
  const point3D*, p,  /* two opposite vertices, orderd (p.x <= q.x) etc. */
  const point3D*, q   /* otherwise wrong normal vectors */
)
{
  point3D vertex [8];  /* 8 vertices */
  point3D* vptr;
  int vindex [4];  /* 4 vertices per face */
  vector3D normal;
  register unsigned int i;
  static const point2D texverts [4] =
  { { 0.0, 0.0 },
    { 1.0, 0.0 },
    { 1.0, 1.0 },
    { 0.0, 1.0 }
  };
  static const int tindex [4] = { 0, 1, 2, 3 };


  if (ge3d_mode == ge3d_wireframe)
  {
    ge3dWireCube (p, q);
    return;
  }

  /* compute vertices */
  for (i = 0, vptr = vertex;  i < 8;  i++, vptr++)
    init3D (*vptr, (i & 4) ? q->x : p->x, (i & 2) ? q->y : p->y, (i & 1) ? q->z : p->z);

  /* somewhat inefficient, because each polygon is drawn per se */
  /* TODO: optimization */
#define cubeface(nx, ny, nz, v0, v1, v2, v3)  \
  init3D (normal, nx, ny, nz);  \
  vindex [0] = v0,  vindex [1] = v1,  vindex [2] = v2, vindex [3] = v3;  \
  ge3dPolygon (vertex, 4, vindex, 0, 0, 0, &normal, texverts, 4, tindex)

  /* 6 faces (provide face but no vertex normals) */
  /* for each face normal vector and vertex indices */
  cubeface ( 1,  0,  0,  5, 4, 6, 7);  /* R */
  cubeface (-1,  0,  0,  0, 1, 3, 2);  /* L */
  cubeface ( 0,  1,  0,  3, 7, 6, 2);  /* T */
  cubeface ( 0, -1,  0,  0, 4, 5, 1);  /* B */
  cubeface ( 0,  0,  1,  1, 5, 7, 3);  /* front */
  cubeface ( 0,  0, -1,  4, 0, 2, 6);  /* back */
#undef cubeface
} /* ge3dCube */



/* ShadedPolygon:
   polygon with color for each vertex,
   the polygon is filled (regardless of current mode),
   fillcolor and linecolor have no influence;
   switches to gouraud shading
*/

void ge3dShadedPolygon PARMS3(
  int, nverts,
  const point3D*, vertexlist,
  const colorRGB*, colorlist
)
{
  glShadeModel (GL_SMOOTH);  /* gouraud shading */
  glBegin (GL_POLYGON);
  while (nverts--)
  { glColor3fv  ((float*) colorlist++);
    glVertex3fv ((float*) vertexlist++);
  }
  glEnd ();
}



/* polygon */

void ge3dPolygon PARMS10(
  const point3D*, vertexlist,
  int, nverts,
  const int*, vertexindexlist,
  const vector3D*, normallist,
  int, nnormals,
  const int*, normalindexlist,
  const vector3D*, f_normal,
  const point2D*, texvertlist,
  int, ntexverts,
  const int*, texvindexlist
)
{
  register int i;
  register const int *v_ptr, *n_ptr;
  int didlighting = 0;

  if (ge3d_mode == ge3d_wireframe)
  {
    glColor3fv (line_color);
    glBegin (GL_LINE_LOOP);
    v_ptr = vertexindexlist;
    i = nverts;
    while (i--)
      glVertex3fv ((float*) (vertexlist + *v_ptr++));
    glEnd ();
    return;
  }
  else if (ge3d_mode == ge3d_hidden_line)
  {
    /* OpenGL includes glPolygonMode, but there is no mode to draw
       interior and lines with a different colour, sorry. */
    glColor3fv (backg_color);
    glBegin (GL_POLYGON);
    v_ptr = vertexindexlist;
    i = nverts;
    while (i--)
      glVertex3fv ((float*) (vertexlist + *v_ptr++));
    glEnd ();
  }
  else /* if (ge3d_mode == ge3d_flat_shading, ge3d_smooth_shading, or ge3d_texturing) */
  {
    didlighting = ge3d_lighting;
    if (nverts == 3)
      glBegin (GL_TRIANGLES);
    else
      glBegin (GL_POLYGON);
    glColor4fv (fill_color);

    /* ignore texture vertices if not texturing */
    if (ge3d_mode != ge3d_texturing || !ge3d_do_texturing)
      texvertlist = 0;

    v_ptr = vertexindexlist;
    n_ptr = normalindexlist;
    if (texvertlist && ntexverts == nverts)
    {
      register const int* t_ptr = texvindexlist;

      if (f_normal)
        glNormal3fv ((float*) f_normal);
      i = nverts;
      while (i--)
      { glTexCoord2fv ((float*) (texvertlist + *t_ptr++));
        glVertex3fv ((float*) (vertexlist + *v_ptr++));
      }
    }
    else if (ge3d_mode == ge3d_flat_shading || nnormals < nverts)
    {
      if (f_normal)
        glNormal3fv ((float*) f_normal);
      i = nverts;
      while (i--)
        glVertex3fv ((float*) (vertexlist + *v_ptr++));
    }
    else  /* send vertices and normals */
    {
      i = nverts;
      while (i--)
      { glNormal3fv ((float*) (normallist + *n_ptr++));
        glVertex3fv ((float*) (vertexlist + *v_ptr++));
      }
    }
    glEnd ();
  }

  if (ge3d_mode == ge3d_hidden_line || !samelfcolor)  /* draw edges in linecolor */
  {
    float oldztran;

    BEGINPOLYGONOFFSET (oldztran)

    /* line colours are not subject to lighting (as in IRIX-GL implementation of ge3d) */
    if (didlighting)
      glDisable (GL_LIGHTING);

    glColor3fv (line_color);
    /*fprintf (stderr, "cur. line color: (%f, %f, %f)\n", line_color [0], line_color [1], line_color [2]);*/
    glBegin (GL_LINE_LOOP);
    v_ptr = vertexindexlist;
    i = nverts;
    while (i--)
      glVertex3fv ((float*) (vertexlist + *v_ptr++));
    glEnd ();

    ENDPOLYGONOFFSET (oldztran)

    if (didlighting)
      glEnable (GL_LIGHTING);
  }

} /* ge3dPolygon */



void ge3dPolyhedron PARMS5(
  point3D*, vertexlist,
  vector3D*, normallist, 
  point2D*, texvertlist,
  int, numfaces,
  face*, facelist
)
{
  register unsigned i;
  register face* faceptr;
  int didlighting = 0;

/*fprintf (stderr, "ge3dPolyhedron for %d faces; same-line-and-fill-color: %d\n", numfaces, samelfcolor);*/

  /* this loop would work always, but for efficiency drawing for each mode is done below */
/*
  for (i = 0, faceptr = facelist;  i < numfaces;  i++, faceptr++)
  {
    ge3dPolygon (
      vertexlist, faceptr->num_faceverts, faceptr->facevert,
      normallist, faceptr->num_facenormals, faceptr->facenormal, &faceptr->normal,
      texvertlist, faceptr->num_facetexverts, faceptr->facetexvert);
    );
  }
  return;
*/

  if (ge3d_mode == ge3d_wireframe)
  {
    ge3d_wirepolyhedron (vertexlist, normallist, numfaces, facelist);
    return;
  }
  else if (ge3d_mode == ge3d_hidden_line)
  {
    register unsigned j;  /* nverts */
    register int* v_ptr;

    glColor3fv (backg_color);
    for (i = numfaces, faceptr = facelist;  i--;  faceptr++)
    {
      j = faceptr->num_faceverts;
      v_ptr = faceptr->facevert;
      glBegin (GL_POLYGON);
      while (j--)
        glVertex3fv ((float*) (vertexlist + *v_ptr++));
      glEnd ();
    }
  }
  else /* if (ge3d_mode == ge3d_flat_shading || ge3d_mode == ge3d_smooth_shading) */
  {
    register unsigned j;
    register int nnormals, ntexverts, * v_ptr, * n_ptr, * t_ptr;

    didlighting = ge3d_lighting;
    glColor4fv (fill_color);

    /* ignore texture vertices if not texturing */
    if (ge3d_mode != ge3d_texturing || !ge3d_do_texturing)
      texvertlist = 0;

    for (i = numfaces, faceptr = facelist;  i--;  faceptr++)
    {
      j = faceptr->num_faceverts;  /* nverts */
      v_ptr = faceptr->facevert;
      nnormals = faceptr->num_facenormals;
      n_ptr = faceptr->facenormal;
      ntexverts = texvertlist ? faceptr->num_facetexverts : 0;
      t_ptr = faceptr->facetexvert;

      if (j == 3)  /* some OpenGL implementations are optimized on triangles */
        glBegin (GL_TRIANGLES);
      else
        glBegin (GL_POLYGON);

      if (ntexverts == j)  /* texturing: Texture + Vertex data */
      { /* will also have to send normals when combining texture mapping with lighting */
        /* assert: only herein if a texture is actually active */
        glNormal3fv ((float*) &faceptr->normal);  /* if assertion fails: flat shading */
        while (j--)
        { glTexCoord2fv ((float*) (texvertlist + *t_ptr++));
          /* no glNormal3fv */
          glVertex3fv ((float*) (vertexlist + *v_ptr++));
        }
      }
      else if (ge3d_mode == ge3d_flat_shading || nnormals < j)
      {                    /* flat shading: Vertex data only */
        glNormal3fv ((float*) &faceptr->normal);
        while (j--)
          glVertex3fv ((float*) (vertexlist + *v_ptr++));
      }
      else                 /* smooth shading: Normal + Vertex data */
      { while (j--)
        { glNormal3fv ((float*) (normallist + *n_ptr++));
          glVertex3fv ((float*) (vertexlist + *v_ptr++));
        }
      }

      glEnd ();

    }
  } /* flat and smooth shading */


  if (ge3d_mode == ge3d_hidden_line || !samelfcolor)  /* draw edges in linecolor */
  {
    float oldztran;

    BEGINPOLYGONOFFSET (oldztran)

    if (didlighting)
      glDisable (GL_LIGHTING);

    /* (no backface culling here) */
    ge3d_wirepolyhedron (vertexlist, normallist, numfaces, facelist);

    ENDPOLYGONOFFSET (oldztran)

    if (didlighting)
      glEnable (GL_LIGHTING);
  }

} /* ge3d_polyhedron */



/* ge3d_wirepolyhedron */
/* always draws a polyhedron in wireframe (independent of current mode) */

void ge3d_wirepolyhedron PARMS4(
  point3D*, vertexlist,
  vector3D*, normallist, 
  int, numfaces,
  face*, faceptr
)
{
  register int nverts, * v_ptr;

  glColor3fv (line_color);
  while (numfaces--)
  {
    nverts = faceptr->num_faceverts;
    v_ptr = faceptr->facevert;  /* vertexindexlist */
    glBegin (GL_LINE_LOOP);
    while (nverts--)
      glVertex3fv ((float*) (vertexlist + *v_ptr++));
    glEnd ();
    faceptr++;
  }

} /* ge3d_wirepolyhedron */



/* ge3dFaceSet */
/* argument list becomes quite long; will probably introduce a type for it (like face) */
/* normalbinding implicit: */
/* smooth shading requires normallist, normalindex and numnormalind >= numcoordind */
/* no texture binding: */
/* texturing requires texvertlist, texvertindex and numtexind >= numcoordind */
/* TODO: eliminate cycling (% operations) whereever possible */

void ge3dFaceSet
#ifdef GE3D_PROTOTYPES
(
  const point3D* vertexlist,           /* array of vertices */
  int numcoordind,                     /* number of vertex indices */
  const int* coordindex,               /* indices into vertexlist, -1 for closing face */
  const materialsGE3D* materials,      /* material definitions */
  int matbinding,                      /* material binding (ge3d_matbinding_t) */
  int nummatind,                       /* number of material indices */
  const int* materindex,               /* indices into arrays of materlist */
  const vector3D* normallist,          /* array of vertex normal vectors (0 allowed) */
  int numnormalind,                    /* number of vertex normal indices (0 allowed) */
  const int* normalindex,              /* indices into normallist (corresponding to coordindex) */
  const vector3D* facenormals,         /* face normal array (one per face; necessary for shading) */
  const point2D* texvertlist,          /* array of texture vertices */
  int numtexind,                       /* number of texture vertex indices (0 allowed) */
  const int* texvertindex              /* indices into texvertlist */
)
#else
( vertexlist, numcoordind, coordindex,
  materials, matbinding, nummatind, materindex,
  normallist, numnormalind, normalindex, facenormals,
  texvertlist, numtexind, texvertindex
)
  const point3D* vertexlist;  int numcoordind;  const int* coordindex;
  const materialsGE3D* materials;  int matbinding, nummatind;  const int* materindex;
  const vector3D* normallist;  int numnormalind;  const int* normalindex;  const vector3D* facenormals;
  const point2D* texvertlist;  int numtexind;  const int* texvertindex
#endif
{
  register int cindex;

  if (!numcoordind)
    return;

  if (coordindex [numcoordind - 1] < 0)
    numcoordind--;  /* avoid empty glBegin/glEnd for last primitive (automatically closed) */

  /*fprintf (stderr, "ge3dFaceSet with %d face indices.\n", numcoordind);*/
  if (ge3d_mode == ge3d_wireframe)
  {
    /* for speed wireframes are not drawn with per vertex materials */
    glBegin (GL_LINE_LOOP);  /* closed polylines */

    if ((matbinding == matb_perpart || matbinding == matb_perface) && materials)
    {
      register int curface = 0;
      ge3d_wire_material (materials, curface++);
      while (numcoordind--)
      {
        cindex = *coordindex++;
        if (cindex < 0)
        { glEnd ();
          ge3d_wire_material (materials, curface++);
          glBegin (GL_LINE_LOOP);
        }
        else
          glVertex3fv ((float*) (vertexlist + cindex));
      }
    }
    else if ((matbinding == matb_perpartindexed || matbinding == matb_perfaceindexed)
         && materials && nummatind && materindex)
    {
      register int curface = 0;
      ge3d_wire_material (materials, materindex [curface++ % nummatind]);

      while (numcoordind--)
      {
        cindex = *coordindex++;
        if (cindex < 0)
        { glEnd ();
          ge3d_wire_material (materials, materindex [curface++ % nummatind]);
          glBegin (GL_LINE_LOOP);
        }
        else
          glVertex3fv ((float*) (vertexlist + cindex));
      }
    }
    else  /* wireframe with overall material binding */
    {
      while (numcoordind--)
      {
        cindex = *coordindex++;
        /*fprintf (stderr, "[%d] ", cindex);*/
        if (cindex < 0)
        { glEnd ();
          glBegin (GL_LINE_LOOP);
        }
        else
          glVertex3fv ((float*) (vertexlist + cindex));
      }
    }

    glEnd ();  /* end of last polyline */
    /*fprintf (stderr, "\n");*/
  }
  else if (ge3d_mode == ge3d_hidden_line)
  {
    register int ncind = numcoordind;
    register const int* crdind = coordindex;
    float oldztran;

    glColor3fv (backg_color);
    glBegin (GL_POLYGON);
    while (ncind--)
    {
      cindex = *crdind++;
      if (cindex < 0)
      { glEnd ();
        glBegin (GL_POLYGON);
      }
      else
        glVertex3fv ((float*) (vertexlist + cindex));
    }
    glEnd ();  /* end of last polygon */

    /* currently always same line and fill color for ge3dFaceSet (from VRML) */
    /* (if not do this code also on !samelfcolor at the end; take care of didlighting) */

    BEGINPOLYGONOFFSET (oldztran)

    glColor3fv (line_color);
    glBegin (GL_LINE_LOOP);

    if ((matbinding == matb_perpart || matbinding == matb_perface) && materials)
    {
      register int curface = 0;
      ge3d_wire_material (materials, curface++);

      while (numcoordind--)
      {
        cindex = *coordindex++;
        if (cindex < 0)
        { glEnd ();
          ge3d_wire_material (materials, curface++);
          glBegin (GL_LINE_LOOP);
        }
        else
          glVertex3fv ((float*) (vertexlist + cindex));
      }
    }
    else if ((matbinding == matb_perpartindexed || matbinding == matb_perfaceindexed)
         && materials && nummatind && materindex)
    {
      register int curface = 0;
      ge3d_wire_material (materials, materindex [curface++ % nummatind]);

      while (numcoordind--)
      {
        cindex = *coordindex++;
        if (cindex < 0)
        { glEnd ();
          ge3d_wire_material (materials, materindex [curface++ % nummatind]);
          glBegin (GL_LINE_LOOP);
        }
        else
          glVertex3fv ((float*) (vertexlist + cindex));
      }
    }
    else
    {
      while (numcoordind--)
      {
        cindex = *coordindex++;
        if (cindex < 0)
        { glEnd ();
          glBegin (GL_LINE_LOOP);
        }
        else
          glVertex3fv ((float*) (vertexlist + cindex));
      }
    }

    glEnd ();  /* end of last polyline */

    ENDPOLYGONOFFSET (oldztran)
  }
  else  /* ge3d_mode one of ge3d_flat_shading, ge3d_smooth_shading, ge3d_texturing */
  {
    ge3d_apply_material (0, -1);  /* index caching only for one face set */

    glColor4fv (fill_color);

    if (ge3d_lighting)
      glEnable (GL_COLOR_MATERIAL);
    glBegin (GL_POLYGON);

    if (ge3d_mode == ge3d_texturing && ge3d_do_texturing &&
        numtexind >= numcoordind && texvertlist && texvertindex)  /* texturing */
    {
      register unsigned curvert = 0;
      /* TODO: other materialbindings than overall (only relevant when lighting is on) */

      if (ge3d_texlighting && numnormalind >= numcoordind)  /* smooth shaded texture */
      { /* assume overall material binding */
        glNormal3fv ((float*) facenormals++);
        while (numcoordind--)
        {
          cindex = *coordindex++;
          if (cindex < 0)
          { normalindex++;  /* skip -1 */
            texvertindex++;  /* skip -1 */
            glEnd ();
            glBegin (GL_POLYGON);
            curvert++;  /* skip -1 */
          }
          else
          { glTexCoord2fv ((float*) (texvertlist + *texvertindex++));
            glNormal3fv ((float*) (normallist + *normalindex++));
            glVertex3fv ((float*) (vertexlist + cindex));
          }
        }
      }
      else if (ge3d_texlighting && facenormals)  /* flat shaded texture */
      { /* assume overall material binding */
        glNormal3fv ((float*) facenormals++);
        while (numcoordind--)
        {
          cindex = *coordindex++;
          if (cindex < 0)
          { glEnd ();
            glBegin (GL_POLYGON);
            glNormal3fv ((float*) facenormals++);
            texvertindex++;  /* skip -1 */
            curvert++;  /* skip -1 */
          }
          else
          { glTexCoord2fv ((float*) (texvertlist + *texvertindex++));
            glVertex3fv ((float*) (vertexlist + cindex));
          }
        }
      }
      else  /* unlit texture */
      {
        while (numcoordind--)
        {
          cindex = *coordindex++;
          if (cindex < 0)
          { glEnd ();
            glBegin (GL_POLYGON);
            texvertindex++;  /* skip -1 */
            curvert++;  /* skip -1 */
          }
          else
          { glTexCoord2fv ((float*) (texvertlist + *texvertindex++));
            glVertex3fv ((float*) (vertexlist + cindex));
          }
        }
      }
    } /* texturing */
    else if (ge3d_mode == ge3d_flat_shading || numnormalind < numcoordind)
    { /* insufficient vertex normals or flat shading */
      if (facenormals)  /* flat shading */
      {
        /* will soon introduce macros for specialized rendering loops */
        if ((matbinding == matb_perpart || matbinding == matb_perface) && materials)
        {
          register int curface = 0;
          ge3d_apply_material (materials, curface++);

          glNormal3fv ((float*) facenormals++);
          while (numcoordind--)
          {
            cindex = *coordindex++;
            if (cindex < 0)
            { glEnd ();
              ge3d_apply_material (materials, curface++);
              glBegin (GL_POLYGON);
              glNormal3fv ((float*) facenormals++);
            }
            else
              glVertex3fv ((float*) (vertexlist + cindex));
          }
        }
        else if ((matbinding == matb_perpartindexed || matbinding == matb_perfaceindexed)
             && materials && nummatind && materindex)
        {
          register int curface = 0;
          ge3d_apply_material (materials, materindex [curface++ % nummatind]);

          glNormal3fv ((float*) facenormals++);
          while (numcoordind--)
          {
            cindex = *coordindex++;
            if (cindex < 0)
            { glEnd ();
              ge3d_apply_material (materials, materindex [curface++ % nummatind]);
              glBegin (GL_POLYGON);
              glNormal3fv ((float*) facenormals++);
            }
            else
              glVertex3fv ((float*) (vertexlist + cindex));
          }
        }
        else if (matbinding == matb_pervertex && materials)
        { /* per vertex binding only visible in smooth shading */
          register unsigned curvert = 0;

          glNormal3fv ((float*) facenormals++);

          if (ge3d_mode == ge3d_flat_shading)
          { /* flat shading: first vertex color used for whole face */
            register unsigned int firstvert = 1;

            while (numcoordind--)
            {
              cindex = *coordindex++;
              if (cindex < 0)
              { glEnd ();
                glBegin (GL_POLYGON);
                firstvert = 1;
                if (ge3d_lighting)
                  glNormal3fv ((float*) facenormals++);
              }
              else
              { if (firstvert)
                { ge3d_apply_material (materials, curvert);  firstvert = 0;
                }
                curvert++;
                glVertex3fv ((float*) (vertexlist + cindex));
              }
            }
          }
          else  /* per vertex material binding, smooth shaded, no vertex normals */
          {
            while (numcoordind--)
            {
              cindex = *coordindex++;
              if (cindex < 0)
              { glEnd ();
                glBegin (GL_POLYGON);
                if (ge3d_lighting)
                  glNormal3fv ((float*) facenormals++);
              }
              else
              { ge3d_apply_material (materials, curvert++);
                glVertex3fv ((float*) (vertexlist + cindex));
              }
            }
          }
        }
        else if (matbinding == matb_pervertexindexed && materials && nummatind && materindex)
        {
          register unsigned curvert = 0;

          /* even if (num_diffuse <= 1) materials must be applied per vertex; */
          /* other components (like emissive) may cycle */

          /* basic loop to be varied: */
          /* without lighting no normals and diffuse color only, for flat shading no materials per vertex */
          /*
          glNormal3fv ((float*) facenormals++);
          while (numcoordind--)
          {
            cindex = *coordindex++;
            if (cindex < 0)
            { glEnd ();
              glBegin (GL_POLYGON);
              glNormal3fv ((float*) facenormals++);
              curvert++;
            }
            else
            { ge3d_apply_material (materials, materindex [curvert++ % nummatind]);
              glVertex3fv ((float*) (vertexlist + cindex));
            }
          }
          */

          if (ge3d_lighting)
          {
            glNormal3fv ((float*) facenormals++);

            if (ge3d_mode == ge3d_flat_shading)
            {
              register unsigned int firstvert = 1;
              while (numcoordind--)
              {
                cindex = *coordindex++;
                if (cindex < 0)
                { glEnd ();
                  glBegin (GL_POLYGON);
                  firstvert = 1;
                  glNormal3fv ((float*) facenormals++);
                  curvert++;  /* skip -1 */
                }
                else
                { if (firstvert)
                  { ge3d_apply_material (materials, materindex [curvert % nummatind]);  firstvert = 0;
                  }
                  curvert++;
                  glVertex3fv ((float*) (vertexlist + cindex));
                }
              }
            }
            else
            {
              while (numcoordind--)
              {
                cindex = *coordindex++;
                if (cindex < 0)
                { glEnd ();
                  glBegin (GL_POLYGON);
                  glNormal3fv ((float*) facenormals++);
                  curvert++;  /* skip -1 */
                }
                else
                { ge3d_apply_material (materials, materindex [curvert++ % nummatind]);
                  glVertex3fv ((float*) (vertexlist + cindex));
                }
              }
            }
          } /* pervertexindexed with lighting */
          else  /* no lighting calculations (frequent case for pervertex material binding */
          {
            register unsigned int i;
            register unsigned int num_diffuse = materials->num_diffuse;

            if (num_diffuse <= 1)  /* degenerate case: no need for per vertex binding */
            {
              while (numcoordind--)
              {
                cindex = *coordindex++;
                if (cindex < 0)
                { glEnd ();
                  glBegin (GL_POLYGON);
                }
                else
                  glVertex3fv ((float*) (vertexlist + cindex));
              }
            }
            else if (ge3d_mode == ge3d_flat_shading)  /* flat, no lighting */
            {
              register unsigned int firstvert = 1;

              while (numcoordind--)
              {
                cindex = *coordindex++;
                if (cindex < 0)
                { glEnd ();
                  glBegin (GL_POLYGON);
                  firstvert = 1;
                  curvert++;  /* skip -1 */
                }
                else
                {
                  if (firstvert)
                  {
                    /* ge3d_apply_material (materials, materindex [curvert % nummatind]); */
  
                    i = materindex [curvert % nummatind];
                    /* no transparency support without lighting calculations */
                    glColor3fv ((float*) (materials->rgb_diffuse + (i % num_diffuse)));
                    firstvert = 0;
                  }
                  curvert++;
                  glVertex3fv ((float*) (vertexlist + cindex));
                }
              }
            }
            else  /* smooth, no lighting */
            {
              while (numcoordind--)
              {
                cindex = *coordindex++;
                if (cindex < 0)
                { glEnd ();
                  glBegin (GL_POLYGON);
                  curvert++;  /* skip -1 */
                }
                else
                {
                  /* ge3d_apply_material (materials, materindex [curvert++ % nummatind]); */

                  i = materindex [curvert++ % nummatind];
                  /* no transparency support without lighting calculations */
                  glColor3fv ((float*) (materials->rgb_diffuse + (i % num_diffuse)));
                  glVertex3fv ((float*) (vertexlist + cindex));
                }
              }
            }
          } /* pervertexindexed without lighting */
        } /* pervertexindexed */
        else  /* flat shading with overall material binding */
        {
          glNormal3fv ((float*) facenormals++);
          while (numcoordind--)
          {
            cindex = *coordindex++;
            if (cindex < 0)
            { glEnd ();
              glBegin (GL_POLYGON);
              glNormal3fv ((float*) facenormals++);
            }
            else
              glVertex3fv ((float*) (vertexlist + cindex));
          }
        }
      }
      else  /* no face normals - will yield in incorrect flat shading */
      { /* facenormals should always be provided, this branch not maintained */
        while (numcoordind--)
        {
          cindex = *coordindex++;
          if (cindex < 0)
          { glEnd ();
            glBegin (GL_POLYGON);
          }
          else
            glVertex3fv ((float*) (vertexlist + cindex));
        }
      }
    }
    else /* smooth shading and numnormalind == numcoordind */
    { /* smooth shading: normal for each vertex */
      /* note: lighting may be on or off (then still per-vertex materials poss.) */
      if ((matbinding == matb_perpart || matbinding == matb_perface) && materials)
      {
        register int curface = 0;
        ge3d_apply_material (materials, curface++);

        while (numcoordind--)
        {
          cindex = *coordindex++;
          /*fprintf (stderr, "[%d/%d] ", cindex, *normalindex);*/
          /* nindex = *normalindex++; */
          if (cindex < 0)
          { normalindex++;  /* skip -1 */
            glEnd ();
            ge3d_apply_material (materials, curface++);
            glBegin (GL_POLYGON);
          }
          else
          { glNormal3fv ((float*) (normallist + *normalindex++));
            glVertex3fv ((float*) (vertexlist + cindex));
          }
        }
      }
      else if ((matbinding == matb_perpartindexed || matbinding == matb_perfaceindexed)
           && materials && nummatind && materindex)
      {
        register int curface = 0;
        ge3d_apply_material (materials, materindex [curface++ % nummatind]);

        while (numcoordind--)
        {
          cindex = *coordindex++;
          /*fprintf (stderr, "[%d/%d] ", cindex, *normalindex);*/
          /* nindex = *normalindex++; */
          if (cindex < 0)
          { normalindex++;  /* skip -1 */
            glEnd ();
            ge3d_apply_material (materials, materindex [curface++ % nummatind]);
            glBegin (GL_POLYGON);
          }
          else
          { glNormal3fv ((float*) (normallist + *normalindex++));
            glVertex3fv ((float*) (vertexlist + cindex));
          }
        }
      }
      else if (matbinding == matb_pervertex && materials)
      {
        register unsigned curvert = 0;
        /* TODO: if (!ge3d_lighting) normal vectors can be omitted, use diffuse component only */

        while (numcoordind--)
        {
          cindex = *coordindex++;
          /*fprintf (stderr, "[%d/%d] ", cindex, *normalindex);*/
          /* nindex = *normalindex++; */
          if (cindex < 0)
          { normalindex++;  /* skip -1 */
            glEnd ();
            glBegin (GL_POLYGON);
          }
          else
          { ge3d_apply_material (materials, curvert++);
            glNormal3fv ((float*) (normallist + *normalindex++));
            glVertex3fv ((float*) (vertexlist + cindex));
          }
        }
      }
      else if (matbinding == matb_pervertexindexed && materials && nummatind && materindex)
      {
        register unsigned curvert = 0;

        /* basic loop to be varied: without lighting no normals and diffuse color only */
        /*
        while (numcoordind--)
        {
          cindex = *coordindex++;
          if (cindex < 0)
          { normalindex++;
            glEnd ();
            glBegin (GL_POLYGON);
            curvert++;
          }
          else
          { ge3d_apply_material (materials, materindex [curvert++ % nummatind]);
            glNormal3fv ((float*) (normallist + *normalindex++));
            glVertex3fv ((float*) (vertexlist + cindex));
          }
        }
        */

        if (ge3d_lighting)
        {
          while (numcoordind--)
          {
            cindex = *coordindex++;
            /*fprintf (stderr, "[%d/%d] ", cindex, *normalindex);*/
            /* nindex = *normalindex++; */
            if (cindex < 0)
            { normalindex++;  /* skip -1 */
              glEnd ();
              glBegin (GL_POLYGON);
              curvert++;  /* skip -1 */
            }
            else
            { ge3d_apply_material (materials, materindex [curvert++ % nummatind]);
              glNormal3fv ((float*) (normallist + *normalindex++));
              glVertex3fv ((float*) (vertexlist + cindex));
            }
          }
        }
        else  /* no lighting calculations (frequent case for pervertex material binding */
        {
          register unsigned i;
          register unsigned num_diffuse = materials->num_diffuse;

          if (num_diffuse <= 1)  /* degenerate case: no need for per vertex binding */
          {
/* this will become macro FACESET_VERTSONLY */
            while (numcoordind--)
            {
              cindex = *coordindex++;
              if (cindex < 0)
              { glEnd ();
                glBegin (GL_POLYGON);
              }
              else
              { glVertex3fv ((float*) (vertexlist + cindex));
              }
            }
          }
          else  /* smooth, no lighting, per vertex indexed */
          {
/* this will become function SMOOTH_DIFFUSEPERVERTEXINDEXED */
            while (numcoordind--)
            {
              cindex = *coordindex++;
              /*fprintf (stderr, "[%d/%d] ", cindex, *normalindex);*/
              /* nindex = *normalindex++; */
              if (cindex < 0)
              { glEnd ();
                glBegin (GL_POLYGON);
                curvert++;  /* skip -1 */
              }
              else
              { /* ge3d_apply_material (materials, materindex [curvert++ % nummatind]); */
                i = materindex [curvert++ % nummatind];
                /* no transparency support without lighting calculations */
                glColor3fv ((float*) (materials->rgb_diffuse + (i % num_diffuse)));

                /* normals irrelevant without lighting calculations */
                /* glNormal3fv ((float*) (normallist + *normalindex++)); */
                glVertex3fv ((float*) (vertexlist + cindex));
              }
            }
          }
        }
      }
      else  /* smooth shading with overall material binding */
      {
        while (numcoordind--)
        {
          cindex = *coordindex++;
          /*fprintf (stderr, "[%d/%d] ", cindex, *normalindex);*/
          /* nindex = *normalindex++; */
          if (cindex < 0)
          { normalindex++;  /* skip -1 */
            glEnd ();
            glBegin (GL_POLYGON);
          }
          else
          { glNormal3fv ((float*) (normallist + *normalindex++));
            glVertex3fv ((float*) (vertexlist + cindex));
          }
        }
      }
      /*fprintf (stderr, "\n");*/
    }

    glEnd ();  /* end of last polygon */
    if (ge3d_lighting)
      glDisable (GL_COLOR_MATERIAL);

  } /* flat/smooth shading */

} /* ge3dFaceSet */



/* ge3dLineSet -- under construction */

void ge3dLineSet PARMS3(
  const point3D*, vertexlist,
  int, numcoordind,
  const int*, coordindex
/*,
  const void*, materlist,
  int, nummatind,
  const int*, materindex,
*/
)
{
  register int cindex;
  int didlighting = (ge3d_mode >= ge3d_flat_shading && ge3d_lighting);

  /*fprintf (stderr, "ge3dLineSet with %d polyline indices.\n", numcoordind);*/

  if (didlighting)
    glDisable (GL_LIGHTING);

  glBegin (GL_LINE_STRIP);  /* open polylines */
  while (numcoordind--)
  {
    cindex = *coordindex++;
    /*fprintf (stderr, "[%d] ", cindex);*/
    if (cindex < 0)
    { glEnd ();
      glBegin (GL_LINE_STRIP);
    }
    else
      glVertex3fv ((float*) (vertexlist + cindex));
  }
  glEnd ();  /* end of last polyline */

  if (didlighting)
    glEnable (GL_LIGHTING);
  /*fprintf (stderr, "\n");*/

} /* ge3dLineSet */



/* ge3dLineSet2 */

void ge3dLineSet2 PARMS8(
  const point3D*, vertexlist,
  int, numcoordind,
  const int*, coordindex,
  int, numcolor,
  const colorRGB*, color,
  int, numcolorind,
  const int*, colorindex,
  int, pervertex
)
{
  register int cindex;
  int didlighting = (ge3d_mode >= ge3d_flat_shading && ge3d_lighting);

  /*fprintf (stderr, "ge3dLineSet with %d polyline indices.\n", numcoordind);*/

  if (didlighting)
    glDisable (GL_LIGHTING);

  glBegin (GL_LINE_STRIP);

  if (!pervertex && !colorindex && color)  /* perface */
  {
    register int curpline = 1;
    glColor3fv ((float*) color);
    while (numcoordind--)
    {
      cindex = *coordindex++;
      if (cindex < 0)
      {
        glEnd ();
        glColor3fv ((float*) (color+(curpline%numcolor)));
        curpline++;
        glBegin (GL_LINE_STRIP);
      }
      else
        glVertex3fv ((float*) (vertexlist + cindex));
    }
  }
  else if (!pervertex && colorindex && color)  /* perfaceindexed */
  {
    register int curpline = 0;
    glColor3fv ((float*) (color+(colorindex[curpline%numcolorind] % numcolor)));
    curpline++;

    while (numcoordind--)
    {
      cindex = *coordindex++;
      if (cindex < 0)
      {
        glEnd ();
        glColor3fv ((float*) (color+(colorindex[curpline%numcolorind] % numcolor)));
        curpline++;
        glBegin (GL_LINE_STRIP);
      }
      else
        glVertex3fv ((float*) (vertexlist + cindex));
    }
  }
  else if (pervertex && colorindex && color)  /* pervertexindexed */
  {
    register int curvert = 0;   

    while (numcoordind--)
    {
      cindex = *coordindex++;
      if (cindex < 0)
      {
        glEnd ();
        curvert++;
        glBegin (GL_LINE_STRIP);
      }
      else
      {
        glColor3fv ((float*) (color+(colorindex[curvert%numcolorind] % numcolor)));
        glVertex3fv ((float*) (vertexlist + cindex));
        curvert++;
      }
    }
  }
  else  /* overall material binding */
  {
    while (numcoordind--)
    {
      cindex = *coordindex++;
      if (cindex < 0)
      {
        glEnd ();
        glBegin (GL_LINE_STRIP);
      }
      else
        glVertex3fv ((float*) (vertexlist + cindex));
    }
  }

  glEnd ();  /* end of last polyline */

  if (didlighting)
    glEnable (GL_LIGHTING);
  /*fprintf (stderr, "\n");*/

} /* ge3dLineSet2 */



/* ge3dPointSet -- under construction */

void ge3dPointSet PARMS2(
  const point3D*, points,
  int, numpts
/*,
  const void*, materlist,
  int, nummatind,
  const int*, materindex,
*/
)
{
  int didlighting = (ge3d_mode >= ge3d_flat_shading && ge3d_lighting);

  if (didlighting)
    glDisable (GL_LIGHTING);

  glBegin (GL_POINTS);  /* points */

  while (numpts--)
    glVertex3fv ((float*) points++);

  glEnd ();  /* end of last polyline */

  if (didlighting)
    glEnable (GL_LIGHTING);

} /* ge3dPointSet */


/* ge3dPointSet2 */

void ge3dPointSet2 PARMS4(
  const point3D*, points,
  int, numpts,
  const colorRGB*, color,
  int, numcolor
)
{
  int curpt = 0;
  int didlighting = (ge3d_mode >= ge3d_flat_shading && ge3d_lighting);

  if (didlighting)
    glDisable (GL_LIGHTING);

  glBegin (GL_POINTS);  /* points */

  while (numpts--)
  {
    if (color)
      glColor3fv ((float*) (color+(curpt++%numcolor)));
    glVertex3fv ((float*) points++);
  }

  glEnd ();  /* end of last polyline */

  if (didlighting)
    glEnable (GL_LIGHTING);

} /* ge3dPointSet2 */


/*
 * test made with display lists brought no siginificant savings for quadrics;
 * difficult to maintain (depends on radius, slices, stacks);
 * also glRotate does not hit performance significantly
 */

void ge3dSphere PARMS1(
  float, radius
)
{
  int stacks = ge3d_quadslices / 2;

  glPushMatrix ();
  glRotatef (-90, 1, 0, 0);  /* y axis, not z, is vertical */

  if (ge3d_mode == ge3d_hidden_line)
  {
    float oldztran;
    glColor3fv (backg_color);
    gluQuadricDrawStyle (gluquadobj, (GLenum) GLU_FILL);
    gluSphere (gluquadobj, radius, ge3d_quadslices, stacks);
    gluQuadricDrawStyle (gluquadobj, (GLenum) GLU_LINE);
    glColor3fv (line_color);
    BEGINPOLYGONOFFSET (oldztran)
    gluSphere (gluquadobj, radius, ge3d_quadslices, stacks);
    ENDPOLYGONOFFSET (oldztran)
  }
  else
    gluSphere (gluquadobj, radius, ge3d_quadslices, stacks);

  glPopMatrix ();
} /* ge3dSphere */


void ge3dCylinder PARMS5(
  float, botradius,
  float, topradius,             /* use same as botradius for a cylinder, or 0 for a cone */
  float, bottom,
  float, height,
  int, parts                    /* cyl_all or combination of cyl_sides/bottom/top */
)
{
  /* might prefer to draw a single circle instead of a gluDisk
     gluDisk with 2 rings (use same no. of slices) */
  /* be aware that a display list would depend on the drawing mode */

  int rings = ge3d_quadslices / 6;

  if (!parts)
    return;

  if (!rings)
    rings = 1;

  glPushMatrix ();
  glRotatef (-90, 1, 0, 0);  /* y axis, not z, is vertical */
  glTranslatef (0, 0, bottom);  /* glu functions draw into xy-plane (z = 0) */

#define CYLINDER_BODY  \
  if (parts & cyl_sides)  /* slices, stacks */  \
    gluCylinder (gluquadobj, botradius, topradius, height, ge3d_quadslices, ge3d_quadslices / 3);  \
  if ((parts & cyl_top) && topradius)  \
  { glTranslatef (0, 0, height);  \
    gluDisk (gluquadobj, 0, topradius, ge3d_quadslices, rings);  \
    glTranslatef (0, 0, -height);  \
  }

  if (ge3d_mode == ge3d_hidden_line)
  {
    float oldztran;
    glColor3fv (backg_color);
    gluQuadricDrawStyle (gluquadobj, (GLenum) GLU_FILL);
    CYLINDER_BODY
    gluQuadricDrawStyle (gluquadobj, (GLenum) GLU_LINE);
    glColor3fv (line_color);
    BEGINPOLYGONOFFSET (oldztran)
    CYLINDER_BODY
    ENDPOLYGONOFFSET (oldztran)
  }
  else
  { CYLINDER_BODY
  }
#undef CYLINDER_BODY

  if ((parts & cyl_bottom) && botradius)
  { /* do not use negative scale: interferes lighting and does not affect normal set in gluDisk */
    switch (ge3d_mode)
    {
      case ge3d_wireframe:    /* orientation irrelevant */
        gluDisk (gluquadobj, 0, botradius, ge3d_quadslices, rings);
      break;
      case ge3d_hidden_line:  /* draw interior and outline */
      {
        float oldztran;
        glColor3fv (backg_color);
        gluQuadricDrawStyle (gluquadobj, (GLenum) GLU_FILL);
        gluQuadricOrientation (gluquadobj, (GLenum) GLU_INSIDE);
        gluDisk (gluquadobj, 0, botradius, ge3d_quadslices, rings);
        gluQuadricOrientation (gluquadobj, (GLenum) GLU_OUTSIDE);
        gluQuadricDrawStyle (gluquadobj, (GLenum) GLU_LINE);
        glColor3fv (line_color);
        BEGINPOLYGONOFFSET (oldztran)
        gluDisk (gluquadobj, 0, botradius, ge3d_quadslices, rings);
        ENDPOLYGONOFFSET (oldztran)
      }
      break;
      case ge3d_texturing:    /* for proper texture orientation do a rotation */
        glRotatef (180, 1.0, 0.0, 0.0);
        gluDisk (gluquadobj, 0, botradius, ge3d_quadslices, rings);
      break;
      default:  /* reverse orientation and normal vector generated by gluDisk (simpler than rotation) */
        gluQuadricOrientation (gluquadobj, (GLenum) GLU_INSIDE);
        gluDisk (gluquadobj, 0, botradius, ge3d_quadslices, rings);
        gluQuadricOrientation (gluquadobj, (GLenum) GLU_OUTSIDE);
    }
  } /* bottom */

  glPopMatrix ();
} /* ge3dCylinder */



/* ge3d_setmode */
/* changes the drawing mode */

void ge3d_setmode PARMS1( 
  int, newmode
)
{
  /*if (ge3d_mode == newmode)*/
  /*  return;                */

  /* wireframe turns everything off */
  /* other modes enable backface culling (if ge3d_backfaceculling) */
  /* and depth test (if ge3d_depthbuffering); */
  /* depth buffer is cleared when mode was previously wireframe */

  if (!gluquadobj)
    gluquadobj = gluNewQuadric ();

  glShadeModel ((newmode >= ge3d_smooth_shading) ? GL_SMOOTH : GL_FLAT);  /* smooth shading */

  if (newmode > ge3d_wireframe && ge3d_backfaceculling)  /* backface culling if enabled */
    glEnable (GL_CULL_FACE);
  else  /* unless wireframe */
    glDisable (GL_CULL_FACE);

  if (newmode == ge3d_wireframe || !ge3d_depthbuffering)
    glDisable (GL_DEPTH_TEST);
  else if (ge3d_mode == ge3d_wireframe)
  { /* activate and clear depth buffer */
    glEnable (GL_DEPTH_TEST);
    glClear (GL_DEPTH_BUFFER_BIT);
  }
  else
    glEnable (GL_DEPTH_TEST);

  if (newmode >= ge3d_flat_shading && ge3d_lighting)
    glEnable (GL_LIGHTING);
  else
    glDisable (GL_LIGHTING);

  /* hidden line not supported by GLU; emulated in sphere and cone routines */
  /* normal vectors not needed for orientation in hidden line */
  gluQuadricDrawStyle (gluquadobj, (GLenum) ((newmode >= ge3d_flat_shading) ? GLU_FILL : GLU_LINE));
  gluQuadricNormals (gluquadobj,
    (GLenum) ((newmode < ge3d_flat_shading) ? GLU_NONE :
    (newmode == ge3d_flat_shading) ? GLU_FLAT : GLU_SMOOTH));
  gluQuadricTexture (gluquadobj, newmode == ge3d_texturing);

  ge3d_mode = newmode;

} /* ge3d_setmode */



void ge3d_setlinecolor PARMS3(
float, r,
float, g,
float, b
)
{ 
  assigncolor (line_color, r, g, b);
  glColor3fv (line_color);
  samelfcolor = 0;
}


void ge3dLineColor PARMS1(
const colorRGB*, c
)
{
  assigncolor (line_color, c->R, c->G, c->B);
  glColor3fv (line_color);
  samelfcolor = 0;
}


void ge3d_setlinestyle PARMS1(
short, pattern
)
{
  if (pattern == -1)
    glDisable (GL_LINE_STIPPLE);  /* solid line */
  else
  { glLineStipple (1 /* bit repetition factor */, pattern /* 16 bit */);
    glEnable (GL_LINE_STIPPLE);
  }
}


void ge3d_setlinewidth PARMS1(
short, width
)
{
  glLineWidth (width);  /* would allow float argument */
}


void ge3dAntialiasing PARMS1(
int, flags
)
{
  ge3d_aaflags = flags;

  /* blending used for anti-aliasing and blended transparency */
  if (flags || ge3d_transparency == transp_blend)
    glEnable (GL_BLEND);
  else
    glDisable (GL_BLEND);

  /* "normal" blending for anti-aliasing or blended transparency */
  glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

  if (flags & ge3d_aa_lines)
    glEnable (GL_LINE_SMOOTH);
  else
    glDisable (GL_LINE_SMOOTH);

  /* ordinary polygon anti-aliasing gives poor results for adjacent polygons */
  /* other approaches: mutliple drawings into accumulation buffer (jitter) or multi-sample (HW dep.) */
  if ((flags & ge3d_aa_polygons) || (flags & ge3d_aa_polygons_front2back))
  {
    if (flags & ge3d_aa_polygons_front2back)        /* requires Alpha bitplanes */
      glBlendFunc (GL_SRC_ALPHA_SATURATE, GL_ONE);  /* and *front to back* (!) sorted polygons */
    glEnable (GL_POLYGON_SMOOTH);
  }
  else
    glDisable (GL_POLYGON_SMOOTH);
}


int ge3dAntialiasingSupport ()
{
  return ge3d_aa_lines | ge3d_aa_polygons | ge3d_aa_polygons_front2back;
}


/* turn on (pixels with A >= threshold are drawn, typically threshold == 0.5)
   or off (when called with 0.0) alpha test (billboard transparency)
*/

void ge3dAlphaTest PARMS1(
  float, threshold
)
{
  if (threshold)
  {
    if (ge3d_transparency == transp_blend)  /* otherwise semi-transparent polygons would disappear */
      threshold = 0.03125;  /* 1/32 */
    /* (GL_NOTEQUAL, 0) may be faster on some systems and it also works with blending */
    /* influences the border line of anti-aliased transparent textures */
    glAlphaFunc (GL_GEQUAL, threshold);
    glEnable (GL_ALPHA_TEST);
  }
  else
    glDisable (GL_ALPHA_TEST);
}


/* define NOFONT if ge3d_text/ge3dText is provided from another library */

#ifndef NOFONT
void ge3d_text PARMS4(
float, x,
float, y,
float, z,
const char*, s
)
{
/* fonts are not a core OpenGL functionality */
}


void ge3dText PARMS2(
const point3D*, p,
const char*, s
)
{
/* fonts are not a core OpenGL functionality */
}
#endif


void ge3d_rect PARMS4(  /* rectangle (outline) */
float, x1,
float, y1,
float, x2,
float, y2
)
{
  glBegin (GL_LINE_LOOP);  /* prevent filling */
  glVertex2f (x1, y1);
  glVertex2f (x2, y1);
  glVertex2f (x2, y2);
  glVertex2f (x1, y2);
  glEnd ();
}


void ge3d_rectf PARMS4(  /* filled rectangle */
float, x1,
float, y1,
float, x2,
float, y2
)
{
  glRectf (x1, y1, x2, y2);  /* have to care for filling in constant color! */
}


void ge3dFilledRect PARMS5(  /* shaded, filled rectangle */
float, x1,
float, y1,
float, x2,
float, y2,
float, z
)
{
  glBegin (GL_POLYGON);
  glNormal3f (0, 0, 1);
  glVertex3f (x1, y1, z);
  glVertex3f (x2, y1, z);
  glVertex3f (x2, y2, z);
  glVertex3f (x1, y2, z);
  glEnd ();
}



/* internal circle drawing routine */
/* caller responsible for appropriate glBegin/glEnd pair */

static void ge3d_draw_circle PARMS3(
float, x,
float, y,
float, r
)
{
/* could use display list instead of point array */
#define POINTSONCIRCLE 32
  static int firstcall = 1;
  static float sines [POINTSONCIRCLE];
  static float cosin [POINTSONCIRCLE];

  const float* sinus;
  const float* cosinus;
  int i;

  if (firstcall)
  {
    float twopioverpoints = 2.0 * M_PI / POINTSONCIRCLE;
    float angle;

    firstcall = 0;  /* calculate points only once */

    for (i = 0;  i < POINTSONCIRCLE;  i++)
    { angle = i * twopioverpoints;
      sines [i] = sin (angle);
      cosin [i] = cos (angle);
    }
  }

  sinus = sines;
  cosinus = cosin;
  for (i = POINTSONCIRCLE;  i;  i--)
    glVertex2f (*cosinus++ * r + x, *sinus++ * r + y);

} /* ge3d_draw_circle */



void ge3d_circle PARMS3(  /* circle (outline) */
float, x,
float, y,
float, r
)
{
  glBegin (GL_LINE_LOOP);
  ge3d_draw_circle (x, y, r);
  glEnd ();
}



void ge3d_circf PARMS3(  /* filled circle */
float, x,
float, y,
float, r
)
{
  glBegin (GL_POLYGON);  /* should turn lighting off!!! */
  ge3d_draw_circle (x, y, r);
  glEnd ();
}



void ge3d_arc PARMS5(  /* arc (outline) */
float, x,
float, y,
float, radius,
float, startangle,     /* counterclockwise from positive x-axis, */
float, endangle        /* in degrees */
)
{
/* arc (x, y, radius, startangle*10, endangle*10); */

  float angle;
  int i;

  angle = M_PI / 180.0;  /* work with radians */
  startangle *= angle;
  endangle *= angle;
  angle = (endangle - startangle) / POINTSONCIRCLE;  /* may be < 0 */

  glBegin (GL_LINE_LOOP);
  for (i = 0;  i < POINTSONCIRCLE;  i++)
  {
    glVertex2f (cos (startangle) * radius + x, sin (startangle) * radius + y);
    startangle += angle;
  }
  glEnd ();
}



void ge3d_setfillcolor PARMS3(
float, r,
float, g,
float, b
)
{ 
  float color [4];  /* RGBA */

/*fprintf (stderr, "ge3d_setfillcolor (%g, %g, %g)\n", r, g, b);*/

  assigncolor (line_color, r, g, b);  /* set also linecolor */
  assignRGBA (fill_color, r, g, b, 1.0);  /* A of 1.0 */
  glColor4fv (fill_color);
  samelfcolor = 1;

  if (ge3d_mode == ge3d_wireframe || ge3d_mode == ge3d_hidden_line)
    return;

  /* set diffuse material component */
  /* might use glColorMaterial instead */
  assigncolor (color, r, g, b);
  color [3] = 1.0;  /* Alpha */
  glMaterialfv (GL_FRONT_AND_BACK, (GLenum) ge3d_diffuse, color);
}


void ge3dMaterial PARMS2(
  int, scope,
  const materialsGE3D*, materials
)
{
  static int glscope [] =  /* must match mat_front/back definitions */
  { GL_FRONT, GL_BACK, GL_FRONT_AND_BACK };
  float color [4];
  const colorRGB* c;
  float A = 1.0;  /* Alpha: 1.0 opaque, 0.0 fully transparent */
  int n;

  if (materials->num_base)
  { /* base color as fillcolor */
    const colorRGB* basecol;
    /* for GL_COLOR_MATERIAL current color must contain *diffuse* color */
    if (ge3d_lighting)
      basecol = materials->rgb_diffuse;
    else
      basecol = materials->rgb_base;

    assigncolor (line_color, basecol->R, basecol->G, basecol->B);
    assignRGBA (fill_color, basecol->R, basecol->G, basecol->B, A);
    glColor4fv (fill_color);
    samelfcolor = 1;
  }

  if (ge3d_mode < ge3d_flat_shading || !ge3d_lighting)
    return;
  /* no transparency support without lighting calculations */

  scope = glscope [scope];

  n = materials->num_transparency;
  if (n)
  {
    float Alpha = 1.0 - *materials->val_transparency;
    /* kwagen: screendoor transparency */
    if (Alpha < 1.0)
    {
      if (ge3d_transparency == transp_stipple)
      {
        int stipple = (int) (Alpha * 16.0 + 0.5);
        if (stipple < 16)
        { glEnable (GL_POLYGON_STIPPLE);
          glPolygonStipple (stippleMask [stipple < 0 ? 0 : stipple]);
          /* should keep track of current stipple; changing polygon stipple may be expensive */
        } /* stipple==0 will mask out all pixels before the alpha test */
        else
          glDisable (GL_POLYGON_STIPPLE);
      }
      else if (ge3d_transparency == transp_blend)
        A = Alpha;  /* only on blended transparency, Alpha is passed to OpenGL */
    }
    else if (ge3d_transparency == transp_stipple)
      glDisable (GL_POLYGON_STIPPLE);
  }
  else if (ge3d_transparency == transp_stipple)
    glDisable (GL_POLYGON_STIPPLE);

  /* if (A < 1.0) */
  {
    color [3] = fill_color [3] = A;
    glColor4fv (fill_color);
  }

  /* blended transparency requires back-to-front ordered drawing */
  /* or two-pass rendering (opaque/transparent) with glDepthMask (0) for transparent objects */

  if (materials->num_ambient)
  { c = materials->rgb_ambient;
    assigncolor (color, c->R, c->G, c->B);
    glMaterialfv ((GLenum) scope, GL_AMBIENT, color);
  }
  if (materials->num_diffuse)
  { c = materials->rgb_diffuse;
    assigncolor (color, c->R, c->G, c->B);
    glMaterialfv ((GLenum) scope, GL_DIFFUSE, color);
  }
  if (materials->num_specular)
  { c = materials->rgb_specular;
    assigncolor (color, c->R, c->G, c->B);
    glMaterialfv ((GLenum) scope, GL_SPECULAR, color);
  }
  if (materials->num_emissive)
  { c = materials->rgb_emissive;
    assigncolor (color, c->R, c->G, c->B);
    glMaterialfv ((GLenum) scope, GL_EMISSION, color);
  }
  if (materials->num_shininess)  /* VRML/Inventor/ge3d: 0.0 to 1.0; OpenGL: 0.0 to 128.0 */
    glMaterialf ((GLenum) scope, GL_SHININESS, *materials->val_shininess * 128.0);

} /* ge3dMaterial */


void ge3dDefaultMaterial ()
{
  /* see OpenGL manual: */
  static float def_ambient [4] = { 0.2, 0.2, 0.2, 1.0 };
  static float def_diffuse [4] = { 0.8, 0.8, 0.8, 1.0 };
  static float def_none [4] = { 0.0, 0.0, 0.0, 1.0 };

  /* reset material to default values */
  glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT, def_ambient);
  glMaterialfv (GL_FRONT_AND_BACK, GL_DIFFUSE, def_diffuse);
  glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR, def_none);
  glMaterialfv (GL_FRONT_AND_BACK, GL_EMISSION, def_none);
  glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, 0.0);

  /* in case of screendoor transparency */
  glDisable (GL_POLYGON_STIPPLE);
}


/* ge3d_setbackgcolor/ge3dBackgroundColor may be set at a time when gl
   routines (glClearColor) are not yet callable */

void ge3d_setbackgcolor PARMS3(
float, r,
float, g,
float, b
)
{ 
  assigncolor (backg_color, r, g, b);
}



void ge3dBackgroundColor PARMS1(
const colorRGB*, c
)
{
  assigncolor (backg_color, c->R, c->G, c->B);
}



/* ge3d_set_light_source */
/* private function that sets up a light source (positional or
   directional) in camera coordinates
*/

static void ge3d_set_light_source PARMS4(
int, index,
const colorRGB*, color,
const point3D*, pos,
float, positional
)
{
  GLfloat val [4];  /* RGBA or xyzw */

/*fprintf (stderr, "ge3d_set_light_source (%d) colour (%g, %g, %g) position (%g, %g, %g) positional (%g)\n",
         index, color->R, color->G, color->B, pos->x, pos->y, pos->z, positional);*/
/*fprintf (stderr, "doing lighting: %d", (int) glIsEnabled (GL_LIGHTING));*/

  index += GL_LIGHT0;  /* guaranteed to be continuous */

  /* fprintf (stderr, "setting light at position (%f, %f, %f) with intensities (%f, %f, %f)\n",
     pos->x, pos->y, pos->z, color->R, color->G, color->B);
  */
  assigncolor (val, color->R, color->G, color->B);
  val [3] = 1.0;
  glLightfv ((GLenum) index, GL_DIFFUSE, val);

  glPushMatrix ();
  glLoadIdentity ();  /* prevent transformation with current matrix */

  assigncolor (val, pos->x, pos->y, pos->z);
  val [3] = positional;  /* 1.0 or 0.0 */
  glLightfv ((GLenum) index, GL_POSITION, val);

  /* turn off spot light properties for ordinary lights */
  glLightf ((GLenum) index, GL_SPOT_EXPONENT, 0.0);
  glLightf ((GLenum) index, GL_SPOT_CUTOFF, 180.0);
  /* GL_SPOT_DIRECTION now insignificant */

  glPopMatrix ();
} /* ge3d_set_light_source */


/* ge3dSetLightSource */
/* arguments: index of light source, light color,
   position or direction (in world coordinates),
   positional flag (1.0 = pos., 0.0 = dir.);
   directions must be given *towards* light source,
   positions and directions are relative to camera
   (use of ge3d_setlightsource is discouraged; ge3dLightSource
   has a more flexible interface) */
/* ge3d_setlightsource no longer exists; call ge3dSetLightSource with flags
   for positional and camera light source or ge3dLightSource providing a
   transformation matrix
*/


void ge3dSetLightSource PARMS5(
int, index,
const colorRGB*, color,
const point3D*, pos,
float, positional,
int, camlgt
)
{
  point3D trfpos;

  if (!camlgt)
  {
    if (positional)
      ge3dTransformMcWc (pos, &trfpos);
    else
      ge3dTransformVectorMcWc (pos, &trfpos);
    pos = &trfpos;  /* use transformed position */
  }

  ge3d_set_light_source (index, color, pos, positional);
}



/* ge3dLightSource */
/* similar to ge3dSetLightSource, but position/orientation is not given
   as point/vector, but a transformation matrix that transforms the
   default position (0, 0, 0) or orientation (-1, 0, 0).
*/
/* note light sources must be defined and switched on again
   after each change of camera */
/* for directional light sources the default direction is
   from negative x axis towards origin ('from left') - GL
   expects a vector *towards* the light source, which is
   (-1, 0, 0) for the default direction
*/

void ge3dLightSource PARMS5(
int, index,
const colorRGB*, color,
/*const*/ matrix4D, mat,  /* const sacrificed for dull compilers */
float, positional,
int, camlgt
)
{
  point3D pos;

  glPushMatrix ();

  if (camlgt)  /* light position relative to current camera */
    glLoadMatrixf ((float*) mat);
  else         /* light position in world coordinates */
    glMultMatrixf ((float*) mat);  /* necessary for correct lighting */

  glGetFloatv (GL_MODELVIEW_MATRIX, ge3d_tm);

  if (positional)
  { /* ge3d_transform_mc_wc ((float) 0, (float) 0, (float) 0, &pos.x, &pos.y, &pos.z); */
    init3D (pos, ge3d_tm [12], ge3d_tm [13], ge3d_tm [14]);
  }
  else
  { /* ge3d_transformvector_mc_wc ((float) -1, (float) 0, (float) 0, &pos.x, &pos.y, &pos.z); */
    init3D (pos, - ge3d_tm [0], - ge3d_tm [1], - ge3d_tm [2]);
  }

  glPopMatrix ();

  ge3d_set_light_source (index, color, &pos, positional);
  /* already transformed to camera coordinates */
} /* ge3dLightSource */


void ge3dSpotLight PARMS6(
  int, index,                           /* light index */
  const colorRGB*, color,               /* color */
  const point3D*, pos,                  /* light position (in object coordinates) */
  const vector3D*, dir,                 /* spot direction - along light emission */
  float, dropoff,                       /* exponent for angular dropoff */
  float, cutoffangle                    /* spread angle from center, degrees, range [0, 90] or 180 */
)
{
  GLfloat val [4];  /* RGBA or xyzw */

  index += GL_LIGHT0;  /* guaranteed to be continuous */

  assigncolor (val, color->R, color->G, color->B);  val [3] = 1.0;
  glLightfv ((GLenum) index, GL_DIFFUSE, val);

  assigncolor (val, pos->x, pos->y, pos->z);  val [3] = 1.0;  /* positional */
  glLightfv ((GLenum) index, GL_POSITION, val);

  assigncolor (val, dir->x, dir->y, dir->z);  /* val [3] ignored */
  glLightfv ((GLenum) index, GL_SPOT_DIRECTION, val);

  /* dropOffRate: VRML/Inventor/ge3d: 0..1; OpenGL: 0..128 */
  glLightf ((GLenum) index, GL_SPOT_EXPONENT, dropoff * 128.0);

  glLightf ((GLenum) index, GL_SPOT_CUTOFF, cutoffangle);

} /* ge3dSpotLight */


void ge3d_switchlight PARMS2(
int, index,
int, state
)
{ 
  /* 0..7 guaranteed to work; caller should do range-check */
/*
  if (index > 7)
  { fprintf (stderr, "ge3d. error: invalid light index %d.\n", index);
    return;
  }
*/
/*
  pushmatrix ();
  loadmatrix (ge3d_identitymat);
  lmbind (lighttarget [index], state ? index : 0);
  popmatrix ();  
*/
  index += GL_LIGHT0;  /* guaranteed to be continuous */
  if (state)
    glEnable ((GLenum) index);
  else
    glDisable ((GLenum) index);
}


/* ge3dGlobalAmbient: set the global ambient color
   default is (0.2, 0.2, 0.2)
 */

void ge3dGlobalAmbient PARMS1(
  const colorRGB*, color
)
{
  float rgba [4];
  assigncolor (rgba, color->R, color->G, color->B);
  rgba [3] = 1.0;  /* Alpha unchanged */

  glLightModelfv (GL_LIGHT_MODEL_AMBIENT, rgba);
}


void ge3d_transform_mc_wc PARMS6(
float, inx,
float, iny,
float, inz,
float*, outx,
float*, outy,
float*, outz
)
{
  glGetFloatv (GL_MODELVIEW_MATRIX, ge3d_tm);
  *outx = inx * ge3d_tm [0] + iny * ge3d_tm [4] + inz * ge3d_tm [8] + ge3d_tm [12];
  *outy = inx * ge3d_tm [1] + iny * ge3d_tm [5] + inz * ge3d_tm [9] + ge3d_tm [13];
  *outz = inx * ge3d_tm [2] + iny * ge3d_tm [6] + inz * ge3d_tm[10] + ge3d_tm [14];
}


void ge3dTransformMcWc PARMS2(
const point3D*, in,
point3D*, out
)
{
  float x = in->x, y = in->y, z = in->z;  /* beware of transform (p, p)! */
  glGetFloatv (GL_MODELVIEW_MATRIX, ge3d_tm);
  out->x = x * ge3d_tm [0] + y * ge3d_tm [4] + z * ge3d_tm [8] + ge3d_tm [12];
  out->y = x * ge3d_tm [1] + y * ge3d_tm [5] + z * ge3d_tm [9] + ge3d_tm [13];
  out->z = x * ge3d_tm [2] + y * ge3d_tm [6] + z * ge3d_tm[10] + ge3d_tm [14];
}


void ge3d_transformvector_mc_wc PARMS6(
float, inx,
float, iny,
float, inz,
float*, outx,
float*, outy,
float*, outz
)
{
  glGetFloatv (GL_MODELVIEW_MATRIX, ge3d_tm);
  *outx = inx * ge3d_tm [0] + iny * ge3d_tm [4] + inz * ge3d_tm [8];
  *outy = inx * ge3d_tm [1] + iny * ge3d_tm [5] + inz * ge3d_tm [9];
  *outz = inx * ge3d_tm [2] + iny * ge3d_tm [6] + inz * ge3d_tm[10];
}


void ge3dTransformVectorMcWc PARMS2(
const vector3D*, in,
vector3D*, out
)
{
  float x = in->x, y = in->y, z = in->z;  /* beware of transform (v, v)! */
  glGetFloatv (GL_MODELVIEW_MATRIX, ge3d_tm);

  out->x = x * ge3d_tm [0] + y * ge3d_tm [4] + z * ge3d_tm [8];
  out->y = x * ge3d_tm [1] + y * ge3d_tm [5] + z * ge3d_tm [9];
  out->z = x * ge3d_tm [2] + y * ge3d_tm [6] + z * ge3d_tm[10];
}


/* ge3d_clearzbuffer */
/* clears anything besides the color buffer
 * to draw a second, independent image atop the current one;
 * i.e. the z buffer (if depth buffer enabled)
 * and the A bitplanes (for ge3d_aa_polygons_front2back)
 */

void ge3d_clearzbuffer ()
{
  if (ge3d_mode == ge3d_wireframe)
    return;

  if (ge3d_depthbuffering)
    glClear (GL_DEPTH_BUFFER_BIT);

  if (ge3d_aaflags & ge3d_aa_polygons_front2back)
  {
    glColorMask (0, 0, 0, 1);
    glClear (GL_COLOR_BUFFER_BIT);
    glColorMask (1, 1, 1, 1);
  }
}


void ge3d_colormask PARMS4(
  int, r,
  int, g,
  int, b,
  int, a
)
{
  glColorMask (r, g, b, a);  /* default: all true */
}


void ge3d_setcamera PARMS11(    /* set up a perspective camera */
const point3D*, eye,            /* camera position */
const point3D*, ref,            /* reference (lookat) point */
const vector3D*, up,            /* up vector (defaults to (0, 1, 0) on NULL) */
float, aper,                    /* window height ... */
float, focal,                   /* ... at distance focal */
float, aspect,                  /* ratio width / height */
float, hither,                  /* near and ... */
float, yon,                     /* ... far clipping plane (both from eye; positive) */
int, mode,                      /* stereo mode (eye_...) */
float, convperc,                /* stereo: convergence distance (fraction of far clipping plane) */
float, eyedist                  /* stereo: half eye separation */
)
{
  glMatrixMode (GL_PROJECTION);
  /* fprintf (stderr, "calling gluPerspective (%f, %f, %f, %f)\n",
     (float) (alpha * 180 / M_PI), aspect, hither, yon); */
  glLoadIdentity ();  /* replace old projection matrix */
  if (mode)  /* kwagen: stereo */
  {
    float a2f = aper / (2.0 * focal);
    float left, right, top, bottom, conv;

    top = a2f * hither;
    bottom = -top;

    /* conv = (hither+yon) / 2.0; */
    conv = convperc * yon;
    /* stereo: ratio screenwidth/eye distance ... does not work well in practise */
    /* eyedist = a2f * aspect * conv * scr_eyed_prop; */
    /* eyedist = (mode == eye_right) ? 0.3 : -0.3; */
    if (mode == eye_left)
       eyedist = -eyedist;

    right = a2f * aspect * hither;
    left = -right - eyedist * hither / conv;
    right = right - eyedist * hither / conv;

    glFrustum (left, right, bottom, top, hither, yon);

    glMatrixMode (GL_MODELVIEW);
    glLoadIdentity ();  /* replace old camera transformation */
    glTranslatef (-eyedist, 0.0, 0.0);  /* eye separation */
  }
  else  /* mono */
  {
    float alpha = 2 * atan2 (aper, 2 * focal);  /* in radians */
    gluPerspective (alpha * 180 / M_PI, aspect, hither, yon);

    glMatrixMode (GL_MODELVIEW);  /* load matrix of lookat on viewing matrix stack - otherwise lighting is incorrect */
    glLoadIdentity ();  /* replace old camera transformation */
  }

  if (up)
    gluLookAt (eye->x, eye->y, eye->z, ref->x, ref->y, ref->z, up->x, up->y, up->z);  /* use up vector */
  else
    gluLookAt (eye->x, eye->y, eye->z, ref->x, ref->y, ref->z, 0.0, 1.0, 0.0);  /* y axis as up direction */
  /* fprintf (stderr, "lookat matrix\n"); ge3d_print_cur_matrix (); */
  /* GL_MODELVIEW */
}


void ge3dCamera PARMS8(                 /* camera (perspective/orthographic; new style) */
  int, type,                            /* cam_perspective/orthographic | cam_absolute/relative */
  const point3D*, pos,                  /* camera position */
  float, rotangle,                      /* angle of rotation (degrees) ... */
  const vector3D*, rotaxis,             /* ... around axis */
  float, height,        /* perspective: total vertical viewing angle, degrees; orthographic: window height */
  float, aspect,                        /* ratio width / height */
  float, hither,                        /* near and ... */
  float, yon                            /* ... far clipping plane (both from eye; positive) */
)                                       /* perspective: both clipping planes must be positive */
{
  /* only perspective part of camera transformation has to be on projection matrix */
  /* otherwise incorrect lighting */

  if (type & cam_perspective)
  {
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();  /* replace old projection matrix */
    gluPerspective (height/*angle*/, aspect, hither, yon);
  }
  else if (type & cam_orthographic)
  {
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();  /* replace old projection matrix */
    height /= 2.0;     /* half height */
    aspect *= height;  /* half width */
    glOrtho (-aspect, aspect, -height, height, hither, yon);
  } /* may have to place gluOrtho onto modelview matrix stack for correct lighting (?) */
  /* otherwise no change of projection matrix */

  /* we have to translate, then rotate the camera, */
  /* so apply the inverse rotation and translation to the scene */
  glMatrixMode (GL_MODELVIEW);
  if (type & cam_absolute)
    glLoadIdentity ();  /* replace old camera transformation */
  if (type & (cam_absolute | cam_relative))
  { glRotatef (- rotangle, rotaxis->x, rotaxis->y, rotaxis->z);
    glTranslatef (- pos->x, - pos->y, - pos->z);
  }
  /* otherwise no change of modelview matrix */
  /* GL_MODELVIEW */
}


void ge3d_ortho_cam PARMS7(     /* set up an orthogonal camera */
const point3D*, eye,            /* camera position */
const point3D*, ref,            /* reference (lookat) point */
const vector3D*, up,            /* up vector (defaults to (0, 1, 0) on NULL) */
float, width,                   /* window width */
float, height,                  /* window height */
float, hither,                  /* near and ... */
float, yon                      /* ... far clipping plane (both from eye; neg. allowed) */
)
{
  width /= 2;
  height /= 2;
  glMatrixMode (GL_PROJECTION);
  glLoadIdentity ();
  glOrtho (-width, width, -height, height, hither, yon);

  glMatrixMode (GL_MODELVIEW);  /* load matrix of lookat on viewing matrix stack - otherwise lighting is incorrect */
  glLoadIdentity ();  /* replace old camera transformation */
  if (up)
    gluLookAt (eye->x, eye->y, eye->z, ref->x, ref->y, ref->z, up->x, up->y, up->z);  /* use up vector */
  else
    gluLookAt (eye->x, eye->y, eye->z, ref->x, ref->y, ref->z, 0.0, 1.0, 0.0);  /* y axis as up direction */
  /* GL_MODELVIEW */
}



/* matrix operations */
/* be aware that OpenGL uses COLUMNmajor matrices, multiplication with
   COLUMNvectors and POSTconcatenation whereas IRIX GL uses ROWmajor
   matrices, multiplication with ROWvectors and PREconcatenation.
*/

void ge3d_push_matrix ()
{
  glPushMatrix ();  
}


void ge3dPushIdentity ()
{
  /* push identity matrix onto the stack */
  glPushMatrix ();
  glLoadIdentity ();
}


void ge3dLoadIdentity ()
{
  /* replace current transformation matrix with identity matrix */
  glLoadIdentity ();
}


void ge3d_push_this_matrix
#ifdef GE3D_PROTOTYPES
(const float mat [4][4])
#else
(mat)  const float mat [4][4];
#endif
{
  glPushMatrix ();
  glMultMatrixf ((float*) mat);  /* concatenate */
}


void ge3d_push_new_matrix
#ifdef GE3D_PROTOTYPES
(const float mat [4][4])
#else
(mat)  const float mat [4][4];
#endif
{
  glPushMatrix ();
  glLoadMatrixf ((float*) mat);  /* new */
}


void ge3dMultMatrix
#ifdef GE3D_PROTOTYPES
(const float mat [4][4])
#else
(mat)  const float mat [4][4];
#endif
{
  glMultMatrixf ((float*) mat);  /* concatenate */
  /* OpenGL wants column major matrices to multiply with column vectors */
  /* IrisGL wants row major matrices to multiply with row vectors */
  /* watch the float array as you like - its the same */
  /* e.g. elements [12, 13, 14] do the translation part */
}


void ge3d_print_cur_matrix ()
{
  int i;

  glGetFloatv (GL_MODELVIEW_MATRIX, ge3d_tm);
  for (i = 0;  i < 4;  i++)
    printf ("%13f %12f %12f %12f\n", ge3d_tm [i], ge3d_tm [i+4], ge3d_tm [i+8], ge3d_tm [i+12]);
}


void ge3d_get_and_pop_matrix PARMS1(
  matrix4D, mat
)
{
  /* see ge3dMultMatrix about element order */
  glGetFloatv (GL_MODELVIEW_MATRIX, (float*) mat);
  glPopMatrix ();
}


void ge3d_get_matrix PARMS1(
  matrix4D, mat
)
{
  glGetFloatv (GL_MODELVIEW_MATRIX, (float*) mat);
}


void ge3d_pop_matrix ()
{
  glPopMatrix ();
}



void ge3d_rotate_axis PARMS2(
char, axis,
float, angle  /* in degrees */
)
{
  /* it's a bit cumbersome for OpenGL to do elementary rotations - */
  /* so build the rotation matrices by hand (also to avoid rounding errors) */

  static float matxrot [16] = { 1, 0, 0, 0,  0, 1, 0, 0,  0, 0, 1, 0,  0, 0, 0, 1 };
  static float matyrot [16] = { 1, 0, 0, 0,  0, 1, 0, 0,  0, 0, 1, 0,  0, 0, 0, 1 };
  static float matzrot [16] = { 1, 0, 0, 0,  0, 1, 0, 0,  0, 0, 1, 0,  0, 0, 0, 1 };

  float s = sin (angle * (M_PI / 180));
  float c = cos (angle * (M_PI / 180));

  switch (axis)
  {
    case 'x':
    case 'X':
      /*glRotatef (angle, 1, 0, 0);*/
      matxrot [5] = matxrot [10] = c;  /* [1][1], [2][2] */
      matxrot [9] = - (matxrot [6] = s);
      glMultMatrixf (matxrot);
    break;
    case 'y':
    case 'Y':
      /*glRotatef (angle, 0, 1, 0);*/
      matyrot [0] = matyrot [10] = c;  /* [0][0], [2][2] */
      matyrot [2] = - (matyrot [8] = s);
      glMultMatrixf (matyrot);
    break;
    case 'z':
    case 'Z':
      /*glRotatef (angle, 0, 0, 1);*/
      matzrot [0] = matzrot [5] = c;  /* [0][0], [1][1] */
      matzrot [4] = - (matzrot [1] = s);
      glMultMatrixf (matzrot);
    break;
  }
} /* ge3d_rotate_axis */


void ge3dRotate PARMS2(
  const vector3D*, axis,  /* rotation axis vector */
  float, angle            /* in radians, righthand */
)
{
  glRotatef (angle * (180 / M_PI), axis->x, axis->y, axis->z);
}


void ge3d_translate PARMS3(
float, x,
float, y,
float, z
)
{
  glTranslatef (x, y, z);
}


void ge3dTranslate PARMS1(
const vector3D*, v
)
{
  glTranslatef (v->x, v->y, v->z);
}



void ge3d_scale PARMS4(
float, x,
float, y,
float, z,
float, all
)
{
  glScalef (x * all, y * all, z * all);
}


void ge3dScale PARMS1(
const float*, s    /* scaling by a point or vector makes no sense; just 3 float values */
)
{
  glScalef (s [0], s [1], s [2]);
}


/* texture mapping routines */

int ge3dTexturingSupport ()
{
  return 1;  /* texture mapping supported in OpenGL */
}


/* flip_image
   little helper for ge3dCreateTexture that converts top-to-bottom images to bottom-to-top
   the pointer returned must be deleted with free
*/

static void* flip_image (int bytesperrow, int height, const void* data)
{
  unsigned char* imgdata = (unsigned char*) malloc (height * bytesperrow);
  const unsigned char* src;
  unsigned char* dst = imgdata;
  register int j;

  if (!imgdata)
    return 0;

  while (height--)
  { /* copy source row [height] onto next destination row */
    src = ((const unsigned char*) data) + height * bytesperrow;
    j = bytesperrow;
    while (j--)
      *dst++ = *src++;
  }
  return imgdata;
}


/* ge3dCreateTexture

   creates a texture from a data array
   width and height specify the size (need not be powers of 2),
   data is the data array as specified by bmpformat argument, e.g.:
   ge3d_ubyte_RGB_TB ... triples of unsigned byte, no filling byte, top-to-bottom
   ge3d_ubyte_RGBA_BT ... quadruples of unsigned byte, RGBA, bottom-to-top
   See ge3d_bitmapformat_t enum for explanation of other constants

   The function returns a handle (actually a display list number)
   which is to be used for later calls of ge3dApplyTexture and
   finally for distroying with ge3dFreeTexture.
   In case of an error, 0 will be returned.

   image data pointed at by data may be destroyed after calling this function.

   if retflags is non-NULL info will be passed back (currently for int_ARGB_TB image only)
   bit value 1 set when non complete opaque (any A < 255),
   bit value 2 set when colored (non greyscale)
*/

int ge3dCreateTexture PARMS5(
  int, width,
  int, height,
  const void*, data,
  int, bmpformat,
  int*, retflags
)
{
  int index;
  int i;
  const GLuint *src;
  GLubyte *dest, *image, r, g, b, a;

  index = glGenLists (1);  /* get an unused list index */
  if (!index)
    fprintf (stderr, "ge3dCreateTexture. error: could not create display list\n");
/*
else fprintf (stderr, "ge3d: created texture display list with index %d; texture size: %d x %d\n", index, width, height);
*/

  glNewList (index, GL_COMPILE);

/*if (bmpformat == ge3d_ubyte_RGB_TB) */
/*  // single texture image, must be power of 2 size in each direction
    glTexImage2D (GL_TEXTURE_2D, 0, 3, / * texture, level of detail, num RGB components * /
                  width, height, 0,  / * width, height, border (0/1) * /
                  GL_RGB, GL_UNSIGNED_BYTE, data / * format, type, data * /
    );
    / * fprintf (stderr, "glTex error: %s\n", gluErrorString (glGetError ())); * /
*/

  switch (bmpformat)
  {
    case ge3d_ubyte_RGB_TB:
    {
      void* imgdata = flip_image (3 * width, height, data);
      if (imgdata)
      { /* series of mipmapped texture images, automatically scaled to power of 2 */
        gluBuild2DMipmaps (GL_TEXTURE_2D, 3, width, height, GL_RGB, GL_UNSIGNED_BYTE, imgdata);
        free (imgdata);
      }
    }
    break;

    case ge3d_ubyte_I_BT:
      gluBuild2DMipmaps (GL_TEXTURE_2D, 1, width, height, GL_LUMINANCE, GL_UNSIGNED_BYTE, data);
    break;
    case ge3d_ubyte_IA_BT:
      gluBuild2DMipmaps (GL_TEXTURE_2D, 2, width, height, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, data);
    break;
    case ge3d_ubyte_RGB_BT:
      gluBuild2DMipmaps (GL_TEXTURE_2D, 3, width, height, GL_RGB, GL_UNSIGNED_BYTE, data);
    break;
    case ge3d_ubyte_RGBA_BT:
      gluBuild2DMipmaps (GL_TEXTURE_2D, 4, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data);
    break;

    case ge3d_int_I_BT:
    {
      src = (const GLuint*) data;
      image = (GLubyte*) malloc (width * height);
      if (!image)
        break;
      dest = image;
      for (i = 0; i < width * height; i++)
      {
        *dest++ = *src++;
      }
      gluBuild2DMipmaps (GL_TEXTURE_2D, 1, width, height, GL_LUMINANCE, GL_UNSIGNED_BYTE, image);
      free (image);
    }
    break;

    case ge3d_int_IA_BT:
    {
      src = (const GLuint*) data;
      image = (GLubyte*) malloc (2 * width * height);
      if (!image)
        break;
      dest = image;
      for (i = 0; i < width * height; i++)
      {
        *dest++ = *src >> 8;
        *dest++ = *src & 0xff;
        src++;
      }
      gluBuild2DMipmaps (GL_TEXTURE_2D, 2, width, height, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, image);
      free (image);
    }
    break;

    case ge3d_int_RGB_BT:
    {
      src = (const GLuint*) data;
      image = (GLubyte*) malloc (3 * width * height);
      if (!image)
        break;
      dest = image;
      for (i = 0; i < width * height; i++)
      {
        *dest++ = *src >> 16;
        *dest++ = (*src >> 8) & 0xff;
        *dest++ = *src & 0xff;
        src++;
      }
      gluBuild2DMipmaps (GL_TEXTURE_2D, 3, width, height, GL_RGB, GL_UNSIGNED_BYTE, image);
      free (image);
    }
    break;

    case ge3d_int_RGBA_BT:
    {
      src = (const GLuint*) data;
      image = (GLubyte*) malloc (4 * width * height);
      if (!image)
        break;
      dest = image;  
      for (i = 0; i < width * height; i++)
      {
        *dest++ = *src >> 24;
        *dest++ = (*src >> 16) & 0xff;
        *dest++ = (*src >> 8) & 0xff;
        *dest++ = *src & 0xff;
        src++;
      }
      /* assumed to contain A data */
      gluBuild2DMipmaps (GL_TEXTURE_2D, 4, width, height, GL_RGBA, GL_UNSIGNED_BYTE, image);
      free (image);
    }
    break;

    case ge3d_int_ARGB_TB:
    {
      int colored = (retflags == 0), hasalpha = 0;

      src = (const GLuint*) data;
      src += width * (height - 1);
      image = (GLubyte*) malloc (4 * width * height);
      if (!image)
        break;
      dest = image;
      for (i = 0; i < width * height; i++)
      {
        r = (*src >> 16) & 0xff;
        *dest++ = r;
        g = (*src >> 8) & 0xff;
        *dest++ = g;
        b = *src & 0xff;
        *dest++ = b;
        a = *src >> 24;
        *dest++ = a;
        if (!colored && (r != g || r != b))
          colored = 1;
        if (a < 0xff)
          hasalpha = 1;
        src++;
        if (i % width == width - 1)
          src -= 2 * width;
      }
      /* check: set 3 if no A in texture */
      gluBuild2DMipmaps (GL_TEXTURE_2D, hasalpha ? 4 : 3, width, height, GL_RGBA, GL_UNSIGNED_BYTE, image);
      free (image);
      if (retflags)
        *retflags = colored * 2 + hasalpha;
    }
    break;

    default:
      fprintf (stderr, "ge3dCreateTexture: error: invalid enumerator %d for bitmap format\n", (int) bmpformat);
  } /* switch (bmpformat) */

  glEndList ();

  return index;
} /* ge3dCreateTexture */


/* ge3dFreeTexture
   frees a texture (actually a display list) associated with its handle
*/

void ge3dFreeTexture PARMS1(
  int, index
)
{
/*fprintf (stderr, "ge3d: freeing texture display list %d\n", index);*/
  glDeleteLists (index, 1);
}


/* ge3dDoTexturing - turn on or off texture mapping */
/* may be done implicitly in ge3dApplyTexture (argument 0 to turn off) */

void ge3dDoTexturing PARMS1(
  int, on
)
{
  ge3d_do_texturing = on;

  /* fprintf (stderr, "ge3d: %s texture mapping\n", on ? "enabling" : "disabling"); */
  if (on)
    glEnable (GL_TEXTURE_2D);
  else
    glDisable (GL_TEXTURE_2D);
}


/* ge3dApplyTexture
   apply a texture associated with its handle (actually call the display list)
*/

void ge3dApplyTexture PARMS1(
  int, index
)
{
  /* fprintf (stderr, "ge3d: calling texture display list with index %d\n", index); */
  glCallList (index);  /* will be ignored on invalid handles */
}


/* ge3dTextureRepeat
   set repeat flags for s and t direction
*/

void ge3dTextureRepeat PARMS2(
  int, s,
  int, t
)
{
  glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, s ? GL_REPEAT : GL_CLAMP);
  glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, t ? GL_REPEAT : GL_CLAMP);
}


/* ge3dTextureMipmapping
   set texture filtering/mipmapping quality
   in range 0 (off) to 5 (best)
*/

void ge3dTextureMipmapping PARMS1(
  int, quality
)
{
  int minfilter;
  int magfilter = GL_LINEAR;  /* magnification filter for all except quality 0 */

  switch (quality)
  {
    case 0:  /* no filtering */
      minfilter = magfilter = GL_NEAREST;
    break;
    case 1:
      minfilter = GL_LINEAR;
    break;
    case 2:
      minfilter = GL_NEAREST_MIPMAP_NEAREST;
    break;
    case 3:
      minfilter = GL_LINEAR_MIPMAP_NEAREST;
    break;
    case 4:  /* OpenGL default */
      minfilter = GL_NEAREST_MIPMAP_LINEAR;
    break;
    default:  /* 5 or higher: best quality; ge3d default */
      minfilter = GL_LINEAR_MIPMAP_LINEAR;
      quality = ge3d_maxmipmap_quality;
  }

  glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minfilter);
  glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magfilter);

  ge3d_texmipmapping = quality;
}


/* ge3dCurrentTextureMipmapping
   return current mipmapping flag
*/

int ge3dCurrentTextureMipmapping ()
{
  return ge3d_texmipmapping;
}


/* ge3dLoadTextureIdentity
   set the texture trf. matrix to an identity matrix
*/

void ge3dLoadTextureIdentity ()
{
  glMatrixMode (GL_TEXTURE);
  glLoadIdentity ();
  glMatrixMode (GL_MODELVIEW);
}


/* ge3dMultTextureMatrix
   multiply the current texture trf. matrix with given matrix
*/

void ge3dMultTextureMatrix
#ifdef GE3D_PROTOTYPES
(const float mat [4][4])
#else
(mat)  const float mat [4][4];
#endif
{
  glMatrixMode (GL_TEXTURE);
  glMultMatrixf ((float*) mat);  /* concatenate */
  glMatrixMode (GL_MODELVIEW);
}


/* ge3dLoadTextureMatrix
   replace the current texture trf. matrix with given matrix
*/

void ge3dLoadTextureMatrix
#ifdef GE3D_PROTOTYPES
(const float mat [4][4])
#else
(mat)  const float mat [4][4];
#endif
{
  glMatrixMode (GL_TEXTURE);
  glLoadMatrixf ((float*) mat);  /* replace */
  glMatrixMode (GL_MODELVIEW);
}


/* ge3dGetTextureMatrix
   return the current texture trf. matrix
*/

void ge3dGetTextureMatrix PARMS1(
  matrix4D, mat
)
{
  glGetFloatv (GL_TEXTURE_MATRIX, (float*) mat);
}


/* ge3dTexturedPolygon
   polygon with texture mapped on it,
   the polygon is textured and flat coloured (regardless of current mode),
   fillcolor and linecolor have no influence;
   contains implicit ge3dDoTexturing (1) and ge3dApplyTexture (handle)
*/

void ge3dTexturedPolygon PARMS4(
  int, nverts,
  const point3D*, vertexlist,
  const point2D*, texturelist,
  int, index
)
{
  glEnable (GL_TEXTURE_2D);
  glCallList (index);

  glBegin (GL_POLYGON);
  while (nverts--)
  { glTexCoord2fv ((float*) texturelist++);
    glVertex3fv ((float*) vertexlist++);
  }
  glEnd ();
}



#if 0
// /* overlay routines */

// /* the following routines are NOT allowed for mixed-model programs:
//    ge3dRequestOverlay, ge3dBitplanes, ge3dClearOverlay

//    the according functionality must be obtained with X-routines.
// */

// /* ge3dRequestOverlay ();

//    call this function once right after initialization to request
//    overlay planes.
//    return value: no. of bitplanes got (currently at least 2)

//    when true overlay bitplanes are not available (only on 8bit entry
//    systems), use pop up planes (which should normally be reserved to
//    the window manager).
// */

// int ge3dRequestOverlay ()
// {
//   int avail = getgdesc (GD_BITS_OVER_SNG_CMODE);

//   if (avail < 2)  /* no overlay planes - use popup planes */
//   { ge3d_overlay_drawmode = PUPDRAW;
//     return 2;     /* popup planes need not be configured */
//   }

//   /* overlay planes available */
//   ge3d_overlay_drawmode = OVERDRAW;
//   overlay (avail);
//   gconfig ();
//   return avail;
// }

// /* ge3dBitplanes (mode);

//    determines active bitplanes that are affected by following
//    commands.
//    argument: (currently) either ge3d_normal_planes or ge3d_overlay_planes
// */

// void ge3dBitplanes (int mode)
// {
//   if (mode == ge3d_overlay_planes)
//     drawmode (ge3d_overlay_drawmode);  /* usually OVERDRAW */
//   else
//     drawmode (NORMALDRAW);
// }

// /* ge3dClearOverlay ();

//    clears the overlay bitplanes (to the invisible color);
//    also activates overlay bitplanes for following commands.
// */

// void ge3dClearOverlay ()
// {
//   drawmode (ge3d_overlay_drawmode);
//   color (0);
//   clear ();
//   /* overlay bitplanes remain active at this point */
// }
#endif


/* ge3dMapColori (int index, short r, short g, short b);
   ge3dMapColorRGB (int index, const RGBColor*);

   define a color map entry. (r, g, b) are in range 0 to 255,
   RGB color components are defined in range 0.0 to 1.0.
   With n bitplanes color indices 0 to (1<<n)-1 are available.
   For overlay planes index 0 is reserved for "invisible".

   Note that currently normal bitplanes are always in RGB mode and
   overlay bitplanes olways in colormap (color indexed) mode. Note
   also that the functions ge3d_polygon, ge3d_polyhedron, and
   ge3dShadedPolygon may not be called in in colormap mode.
*/

void ge3dMapColori PARMS4(
int, index,
short, r,
short, g,
short, b
)
{
/*mapcolor (index, r, g, b);*/
}


void ge3dMapColorRGB PARMS2(
int, index,
const colorRGB*, c
)
{
/*mapcolor (index, c->R * 255.0, c->G * 255.0, c->B * 255.0);*/
}


/* ge3dColorIndex (int index);

   activate a color from the colormap. Only allowed in colormap
   mode (i.e. currently only for overlay bitplanes).
*/

void ge3dColorIndex PARMS1(
int, index
)
{
/*color (index);*/
}


/* ge3d_close */

void ge3d_close ()
{
}