File: gl_funcs.c

package info (click to toggle)
xmakemol 5.15-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,592 kB
  • ctags: 1,305
  • sloc: ansic: 18,178; sh: 2,794; makefile: 114; perl: 98
file content (1595 lines) | stat: -rw-r--r-- 39,573 bytes parent folder | download
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
/*

Copyright (C) 2000, 2001, 2002, 2003, 2004 Matthew P. Hodges
This file is part of XMakemol.

XMakemol 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.

XMakemol 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 XMakemol; see the file COPYING.  If not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/


#define __GL_FUNCS_C__

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#ifdef GL

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <Xm/Xm.h>

#ifdef SGI_STEREO
#include <X11/extensions/SGIStereo.h> /* aro */
#endif /* SGI_STEREO */

#include "globals.h"
#include "bbox.h"
#include "bonds.h"
#include "defs.h"
#include "draw.h"
#include "view.h"

#include "gl_funcs.h"
#include "gl2ps.h"

/* Function prototypes */

double get_atom_scale (void);
double get_bond_scale (void);
double get_bond_width (void);
double get_hbond_scale (void);
double gl_get_rendered_atom_size (int);
void gl_init (void);
void gl_render_atoms (void);
void gl_render_bbox (void);
void gl_render_bbox_face (int, int, int, int);
void gl_render_bbox_line (int, int);
void gl_render_bonds (void);
void gl_render_hbonds (void);
void gl_render_vectors (void);
void gl_set_perspective (void);
double get_smallest_cov_rad (void);
void gl_update_near_and_far (void);
double mod_of_vec (double *);
Boolean mouse_motion_p (void);
void normalize_vec (double *);
Boolean outline_mode_p (void);

/* Variable declarations */

static int gl_copy_canvas = 0;
static int gl_initialized = 0;
static int gl_no_atom_segments = 12;
static int gl_no_bond_segments = 8;
static int gl_use_lighting = 0;
static int gl_use_specular_lighting = 0;
static int gl_sep = 30; /* aro */
static GLfloat gl_shininess[] = {64};

static double gl_eye = 10;         /* per-system setting */
static double gl_fov = 50;
static double gl_max_z, gl_min_z;
static double gl_perspective_near = 0, gl_perspective_far = 0;

static GLUquadricObj *gl_quadric = NULL;

static enum gl_render_types gl_render_type = BALL_AND_STICK;
static enum gl_render_types gl_current_render_type;

void gl_render (void)
{
  enum render_stereo_types get_gl_render_stereo();
  void set_gl_render_stereo(enum render_stereo_types);
  void StereoProjection(float left, float right, float bottom,
                        float top, float near, float far,
                        float zero_plane, float dist, float eye,
                        int side_by_side);
  double get_canvas_scale(void);
  double get_z_depth(void);
  
  double eye_offset, gl_fov_rad, zp_width; /* zero parallax width */
  static double last_eye_offset;
  
  double scale, depth, width_scale, height_scale;

  int side_by_side;

  if (gl_initialized == 0)
    {
      gl_init ();
      gl_initialized = 1;
    }

  gl_set_perspective ();

  glMatrixMode (GL_MODELVIEW);
  glLoadIdentity ();

  /* Make sure the image does not appear distorted in a window that is wide
     but not very tall.  Note that we use canvas_scale and z_depth here for
     trying to keep up with the X11 rendering engine.  Currently the
     selection from the OpenGL canvas is performed using the coordinates
     generated by the X11 rendering engine.  In the following code we are
     trying to squeeze the OpenGL view so that the image generated by the
     OpenGL resembles the image generated by the X11 rendered as much as
     possible.  This depends highly on the position of the field of view
     slider but we are trying to match the images anyway.  The images become
     more and more different as the user zooms in. */
  scale = get_canvas_scale ();
  depth = get_z_depth ();   
  width_scale = (scale * depth) / (float) canvas_width;
  height_scale = width_scale * (canvas_width / (float) canvas_height);    
  glScalef (width_scale, height_scale, 1.0f);

  gluLookAt (0.0, 0.0, gl_eye, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

  /* This may get changed in StereoProjection, but isn't reset when we
     change back to a single image */
  glViewport (0, 0, canvas_width, canvas_height);

  /* aro--> */
  if(get_gl_render_stereo() && depth_is_on)
    {
      /* Calculate offset between eyes.  Max distance between eyes should be 
         (distance to projection plane) / 15 */
      eye_offset = (gl_eye + gl_perspective_near) / gl_sep;
      if(eye_offset > 0.0)
        last_eye_offset = eye_offset;
      else
        eye_offset = last_eye_offset;
      
      /* Calculate width at zero parallax (default z = 0) from given fov. 
         zp_width = (1/2) entire width at zero parallax */
      gl_fov_rad = (PI * gl_fov) / 180;
      zp_width = gl_eye * tan( 0.5 * gl_fov_rad );
      
      /* Left eye: */

      /* Create nonsymetric viewing frustum */
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();

      if (get_gl_render_stereo() == SIDE_BY_SIDE)
        {
          side_by_side = 1;
        }
      else
        {
          side_by_side = 0;
        }

      StereoProjection(-zp_width, zp_width, -zp_width, zp_width, 
                       gl_perspective_near, gl_perspective_far, 
                       0.0, gl_eye, -eye_offset, side_by_side);

      if(get_gl_render_stereo() == SIDE_BY_SIDE) { 
        glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/*         glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); */
      }
      else if (get_gl_render_stereo() == RED_BLUE)
        {
          glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
          glColorMask(GL_TRUE, GL_FALSE, GL_FALSE, GL_TRUE);/* Red mask */
        }
#ifdef SGI_STEREO
      else if(get_gl_render_stereo() == SGI_HARDWARE) {
        glXWaitGL();
        XSGISetStereoBuffer(display, XtWindow(canvas), STEREO_BUFFER_LEFT);
        glXWaitX();
        glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        /* Need to change aspect ratio as we've changed aspect ratio of monitor */
        width_scale = width_scale / 0.48;
      }
#endif /* SGI_STEREO */
      else
        fprintf(stderr, "gl_render: Invalid stereo mode\n");

      glMatrixMode (GL_MODELVIEW);
      glLoadIdentity ();
      glScalef (width_scale, height_scale, 1.0f);
    
      if ((! mouse_motion_p ()) || (! outline_mode_p ()))
        {
          if (gl_use_lighting)
            {
              gluQuadricNormals (gl_quadric, GLU_SMOOTH);
            }
          else
            {
              gluQuadricNormals (gl_quadric, GLU_NONE);
            }
          gluQuadricDrawStyle (gl_quadric, GLU_FILL);
          
          glEnable (GL_DEPTH_TEST);
          glEnable (GL_COLOR_MATERIAL);

          glDisable (GL_LIGHTING);
          if (bbox_flag && (no_atoms > 0)) gl_render_bbox ();
          if (gl_use_lighting) glEnable (GL_LIGHTING);
          
          gl_render_atoms ();
          gl_render_bonds ();
          gl_render_hbonds ();
          gl_render_vectors ();
        }
      else
        {
          glDisable (GL_LIGHTING);
          glDisable (GL_DEPTH_TEST);
          glDisable (GL_COLOR_MATERIAL);

          gluQuadricDrawStyle (gl_quadric, GLU_LINE);
          gluQuadricNormals (gl_quadric, GLU_NONE);

          gl_render_atoms ();
        }

      /* Right eye: */

      /* Create nonsymetric viewing frustum */
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();

      StereoProjection(-zp_width, zp_width, -zp_width, zp_width, 
                       gl_perspective_near, gl_perspective_far, 
                       0.0, gl_eye, eye_offset, -side_by_side);

      if(get_gl_render_stereo() == SIDE_BY_SIDE) {
/*         glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); */
/*         glClear(GL_DEPTH_BUFFER_BIT); */
      }
      else if (get_gl_render_stereo() == RED_BLUE)
        {
          glColorMask(GL_FALSE, GL_FALSE, GL_TRUE, GL_TRUE);/* Blue mask */
          glClear(GL_DEPTH_BUFFER_BIT);
        }
#ifdef SGI_STEREO
      else if(get_gl_render_stereo() == SGI_HARDWARE) {
        glXWaitGL();
        XSGISetStereoBuffer(display, XtWindow(canvas), STEREO_BUFFER_RIGHT);
        glXWaitX();
        glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
      }
#endif /* SGI_STEREO */
      else
        fprintf(stderr, "gl_render: Invalid stereo mode\n");
                
      glMatrixMode (GL_MODELVIEW);
      glLoadIdentity ();
      glScalef (width_scale, height_scale, 1.0f);
    }
  else /* If not stereo, we need to clear color and depth */
    {
      glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
    }
  /* <--aro */

  if ((! mouse_motion_p ()) || (! outline_mode_p ()))
    {
      if (gl_use_lighting)
        {
          gluQuadricNormals (gl_quadric, GLU_SMOOTH);
        }
      else
        {
          gluQuadricNormals (gl_quadric, GLU_NONE);
        }
      gluQuadricDrawStyle (gl_quadric, GLU_FILL);

      glEnable (GL_DEPTH_TEST);
      glEnable (GL_COLOR_MATERIAL);

      glDisable (GL_LIGHTING);
      if (bbox_flag && (no_atoms > 0)) gl_render_bbox ();
      if (gl_use_lighting) glEnable (GL_LIGHTING);

      gl_render_atoms ();
      gl_render_bonds ();
      gl_render_hbonds ();
      gl_render_vectors ();
    }
  else
    {
      glDisable (GL_LIGHTING);
      glDisable (GL_DEPTH_TEST);
      glDisable (GL_COLOR_MATERIAL);

      gluQuadricDrawStyle (gl_quadric, GLU_LINE);
      gluQuadricNormals (gl_quadric, GLU_NONE);
      
      gl_render_atoms ();
    }

  /* aro - change color mask so all of RGB color fields are cleared */
  if ((get_gl_render_stereo() == RED_BLUE) && depth_is_on)
    glColorMask(GL_TRUE,GL_TRUE,GL_TRUE,GL_TRUE);
  
  /* Note that we are drawing onto the canvas directly, since I
     haven't been able to figure out the cause of crashes (in
     glClearColor) on some displays. */

  glXSwapBuffers (display, XtWindow (canvas));

  /* Note that we should make this conditional; we only need it when
     XpmWriteFileFromPixmap is called and canvas_pm is used. */

  if (gl_copy_canvas)
    {
      XCopyArea (display, XtWindow (canvas), canvas_pm, gc, 0, 0,
                 canvas_width, canvas_height, 0, 0);
    }
}

void StereoProjection(float left, float right, float bottom, float top,
                      float near, float far, float zero_plane, float dist,
                      float eye, int side_by_side)
{
  enum render_stereo_types get_gl_render_stereo();
  
  float xmid,
    ymid,
    clip_near,
    clip_far,
    topw,
    bottomw,
    leftw,
    rightw,
    dx,
    dy,
    n_over_d;
  
  static double last_near;
  static double last_far;

  /* Viewports for side by side mode */

  if (side_by_side == 1)
    {
      glViewport (canvas_width / 2, 0, canvas_width / 2, canvas_height);
      left *= 0.5;
      right *= 0.5;
    }
  else if (side_by_side == -1)
    {
      glViewport (0, 0, canvas_width, canvas_height);
      glColor3f (0.0, 0.0, 0.0);
      glLineWidth (2.0);

      glBegin (GL_LINES);
      /* FIXME: do this properly */
      glVertex3f (0.0, -1000, 0.0);
      glVertex3f (0.0, 1000, 0.0);
      glEnd ();

      glViewport (0, 0, canvas_width / 2, canvas_height);
      left *= 0.5;
      right *= 0.5;
    }
  else
    {
      glViewport (0, 0, canvas_width, canvas_height);
    }

  dx = right - left;
  dy = top - bottom;
  
  xmid = (right + left) / 2.0;
  ymid = (top + bottom) / 2.0;
  
  /* If the near clipping plane is going to be behind the eye, clamp its 
     position at that of the eye (We set it 1.0 first because glFrustum
     requires it to be non-zero */
  if(near > 0.0)
    { 
      last_near = clip_near = near;
      last_far = clip_far = far;
    }
  else
    {
      clip_near = last_near;
      clip_far = last_far;
    }
  
  n_over_d = clip_near / dist;
  
  topw = n_over_d * dy / 2.0;
  bottomw = -topw;
  rightw = n_over_d * (dx / 2.0 - eye);
  leftw  = n_over_d *(-dx / 2.0 - eye);

  /* Need to be in projection mode for this. */
  glLoadIdentity();
  glFrustum(leftw, rightw, bottomw, topw, clip_near, clip_far);
  glTranslatef(-xmid - eye,  -ymid,  -zero_plane - dist);
}

void gl_render_atoms (void)
{
  void gl_render_atom (int);
  int get_gl_render_type (void);

  int i;

  /* Return if atoms not being display and in ball and stick mode;
     note that we draw atoms if tubes mode if the bond is visible */

  for (i = 0; i < no_atoms; i++)
    {
      gl_current_render_type = (atoms[i].gl_render_type == DEFAULT) ?
        get_gl_render_type () : atoms[i].gl_render_type;
        
      if ((atom_flag == 0) &&
          ((gl_current_render_type == BALL_AND_STICK) ||
           ((gl_current_render_type == TUBES) && (! gl_use_lighting)))) return;

      if ((atoms[i].visi == 1) || (view_ghost_atoms == 1))
        {
          gl_render_atom (i);
        }
    }
}

void gl_render_atom (int i)
{
  int j;

  double line_width, atom_size;

  char string[16]="", sub_string[8]="", *labels = "ABCD";

  enum render_stereo_types get_gl_render_stereo(void);/* aro */
  GLfloat red, green, blue;

  /* For tubes mode, we don't draw an atom (a) when bonds are not
     displayed and the atom is bonded and (b) when atoms are not
     displayed and the atom is not bonded. In essense, atoms and bonds
     are mutually exclusive. */

  if ((gl_current_render_type == TUBES) && gl_use_lighting)
    {
      if (((bond_flag == 0) && (bond_adjacency_list[i] != NULL)) ||
          ((atom_flag == 0) && (bond_adjacency_list[i] == NULL))) return;
    }

  red   = atoms[i].red   / 65536.0;
  blue  = atoms[i].blue  / 65536.0;
  green = atoms[i].green / 65536.0;

  /* aro - We need to convert the colors to grayscale in order for 
     red and blue color masks to work properly */
  if ((get_gl_render_stereo() == RED_BLUE) && depth_is_on)
    {
      red = (red + blue + green) / 3;
      blue = red;
      green = red;
    }

  glPushMatrix ();
  glTranslatef (atoms[i].x, atoms[i].y, atoms[i].z);

  if ((! mouse_motion_p ()) || (! outline_mode_p ()))
    {
      glColor3f (red, green, blue);
    }
  else
    {
      glColor3f (0.0, 0.0, 0.0);
    }

  atom_size = gl_get_rendered_atom_size (i);
  line_width = 0.1 * get_atom_scale () * get_smallest_cov_rad ();

  /* Set up rendering for elliptical atoms */

  if (((gl_current_render_type == BALL_AND_STICK) ||
       ((gl_current_render_type == TUBES) && bond_adjacency_list[i] == NULL)) &&
      (atoms[i].is_ellipse == 1))
    {
      void euler_to_matrix (double *, double *);

      int j, k;

      double axis[3], angle, matrix[9];

      double get_angle_axis (double *);

      /* Apply the rotation for the orientation of the system */

      for (j = 0; j < 3; j++)
        {
          for (k = 0; k < 3; k++)
            {
              angle_axis_matrix[j][k] = global_matrix[j][k];
            }
        }

      angle = get_angle_axis (axis);
      glRotatef (-angle / DEG2RAD, axis[0], axis[1], axis[2]);

      /* Apply the rotation describing the original orientation of
         the ellipse */

      euler_to_matrix (atoms[i].euler, matrix);

      for (j = 0; j < 3; j++)
        {
          for (k = 0; k < 3; k++)
            {
              angle_axis_matrix[j][k] = matrix[(j * 3) + k];
            }
        } 

      angle = get_angle_axis (axis);
      glRotatef (angle / DEG2RAD, axis[0], axis[1], axis[2]);

      /* Apply the anisotropic scaling */

      glScalef (atoms[i].shape[0], atoms[i].shape[1], atoms[i].shape[2]);
       
    }

  if ((! mouse_motion_p ()) || (! outline_mode_p ()))
    {
      /* Draw border for selected atoms */

      if (atoms[i].sel == 1)
        {
          glColor3f (1.0, 0.65, 0.0); /* Orange */

          glDisable (GL_LIGHTING);

          gluDisk (gl_quadric,
                   atom_size - line_width,
                   atom_size,
                   gl_no_atom_segments,
                   1);

          if (gl_use_lighting) glEnable (GL_LIGHTING);

          glColor3f (red, green, blue);
        }

      if (gl_use_lighting)
        {
          double size;

          if (atoms[i].sel == 0)
            {
              size = atom_size;
            }
          else
            {
              size = atom_size - line_width;
            }

          gluSphere (gl_quadric,
                     size,
                     gl_no_atom_segments,
                     gl_no_atom_segments);
        }
      else
        {
          gluDisk (gl_quadric,
                   0,
                   atom_size,
                   gl_no_atom_segments,
                   1);

          if (atoms[i].sel == 0)
            {
              glColor3f (0.0, 0.0, 0.0);

              /* Move slightly forward */

              glTranslatef (0.0, 0.0, 0.01);

              gluDisk (gl_quadric,
                       atom_size - line_width,
                       atom_size,
                       gl_no_atom_segments,
                       1);
            }
        }
    }
  else
    {
      gluDisk (gl_quadric,
               atom_size,       /* Inner radius */
               atom_size,       /* Outer radius */
               8,               /* Small number for slices */
               1);              /* Number of rings */
    }

  if (atom_flag == 1)
    {
      /* Atom numbers */

      if (at_nos_flag)
        {
          sprintf (string, "%d ", i + 1);
        }

      /* Atom symbols */

      if (at_sym_flag)
        {
          sprintf (sub_string, "%s ", atoms[i].label);
          strcat (string, sub_string);
        }

      /* Atom selections */

      for (j = 0; j < 4; j++)
        {
          if(i == selected[j])
            {
              sprintf (sub_string, "%c ", labels[j]);
              strcat (string, sub_string);
            }
        }

      if (strlen (string) > 0)
        {
          glColor3f (0.0, 0.0, 0.0);
          glRasterPos3f (0, 0, atom_size * 1.1);

          for (j = 0; j < strlen (string); j++)
            {
              glutBitmapCharacter (GLUT_BITMAP_HELVETICA_12, string[j]);
            }

          glRasterPos3f (0, 0, atom_size * 1.1);
          gl2psText(string, "Helvetica", 12);
        }
    }

  glPopMatrix ();
}


void gl_render_bbox (void)
{
  GLfloat line_width = 2.0;

  glColor3f (0.7, 0.7, 0.7);
  glDepthMask (GL_FALSE);

  gl_render_bbox_face (0, 1, 3, 2);
  gl_render_bbox_face (4, 5, 7, 6);
  gl_render_bbox_face (0, 2, 6, 4);
  gl_render_bbox_face (1, 3, 7, 5);
  gl_render_bbox_face (0, 1, 5, 4);
  gl_render_bbox_face (2, 3, 7, 6);

  glDepthMask (GL_TRUE);

  glColor3f (0.0, 0.0, 0.0);
  glLineWidth (line_width);

  gl_render_bbox_line (0, 1);
  gl_render_bbox_line (0, 2);
  gl_render_bbox_line (0, 4);
  gl_render_bbox_line (1, 3);
  gl_render_bbox_line (1, 5);
  gl_render_bbox_line (2, 3);
  gl_render_bbox_line (2, 6);
  gl_render_bbox_line (3, 7);
  gl_render_bbox_line (4, 5);
  gl_render_bbox_line (4, 6);
  gl_render_bbox_line (5, 7);
  gl_render_bbox_line (6, 7);
}


void gl_render_bbox_line (int i, int j)
{
  glBegin (GL_LINES);
  glVertex3f (bbox.v[i][0], bbox.v[i][1], bbox.v[i][2]);
  glVertex3f (bbox.v[j][0], bbox.v[j][1], bbox.v[j][2]);
  glEnd ();
}

void gl_render_bbox_face (int i, int j, int k, int l)
{
  glBegin (GL_POLYGON);
  glVertex3f (bbox.v[i][0], bbox.v[i][1], bbox.v[i][2]);
  glVertex3f (bbox.v[j][0], bbox.v[j][1], bbox.v[j][2]);
  glVertex3f (bbox.v[k][0], bbox.v[k][1], bbox.v[k][2]);
  glVertex3f (bbox.v[l][0], bbox.v[l][1], bbox.v[l][2]);
  glEnd();
}


void gl_render_bonds (void)
{
  void gl_render_bond (int, int);

  int i, j;
  struct node *ptr;

  if (bond_flag == 0) return;

  for (i = 0; i < no_atoms; i++)
    {      

      ptr = bond_adjacency_list[i];

      while (ptr != NULL)
        {
          j = (ptr->v);

          if (atoms[i].visi && atoms[j].visi)
            {
              gl_render_bond (i, j);
            }

          ptr = ptr->next;
        }
    }
}

void gl_render_bond (int i,
                     int j)
{
  enum render_stereo_types get_gl_render_stereo(void);/* aro */
  void update_canvas_bond_points (int, int, Boolean, double);

  int k;
  double angle, b[3], bond_radius, length, mod_b, line_width;
  GLfloat red, green, blue;

  /* We want the radius, ie half the width which represents the
     diameter */

  bond_radius = get_bond_width () / 2.0;

  /* b is the vector along the bond */

  b[0] = atoms[j].x - atoms[i].x;
  b[1] = atoms[j].y - atoms[i].y;
  b[2] = atoms[j].z - atoms[i].z;
          
  mod_b = mod_of_vec (b);
  normalize_vec (b);

  angle = acos (b[2]) / DEG2RAD;

  red   = atoms[i].red   / 65536.0;
  blue  = atoms[i].blue  / 65536.0;
  green = atoms[i].green / 65536.0;

  /* aro - We need to convert the colors to grayscale in order for 
     red and blue color masks to work properly */
  if ((get_gl_render_stereo() == RED_BLUE) && depth_is_on)
    {
      red = (red + blue + green) / 3;
      blue = red;
      green = red;
    }
  
  glColorMaterial (GL_FRONT, GL_DIFFUSE);
  glColor3f (red, green, blue);

  if (gl_use_lighting)
    {
      glPushMatrix ();

      glTranslatef (atoms[i].x, atoms[i].y, atoms[i].z);

      glRotatef (180.0, 0.0, 0.0, 1.0);
      glRotatef (angle, b[1], -1.0 * b[0], 0.0);

      /* length of bond segment */

      length = (mod_b * atoms[i].cov_rad /
            (atoms[i].cov_rad + atoms[j].cov_rad));
      
      gluCylinder (gl_quadric,
                   bond_radius,
                   bond_radius,
                   length,
                   gl_no_bond_segments,
                   1);

      glPopMatrix ();
    }
  else
    {
      /* Outline */

      update_canvas_bond_points (i, j, False, 0.0);

      glColor3f (0.0, 0.0, 0.0);

      glPushMatrix ();

      glTranslatef (0.0, 0.0, -0.01);

      glBegin (GL_POLYGON);

      k = 0;

      glVertex3f (cartesian_bond_points[k][0],
                  cartesian_bond_points[k][1],
                  cartesian_bond_points[k][2]);

      k = 1;

      glVertex3f (cartesian_bond_points[k][0],
                  cartesian_bond_points[k][1],
                  cartesian_bond_points[k][2]);

      k = 4;

      glVertex3f (cartesian_bond_points[k][0],
                  cartesian_bond_points[k][1],
                  cartesian_bond_points[k][2]);

      k = 5;

      glVertex3f (cartesian_bond_points[k][0],
                  cartesian_bond_points[k][1],
                  cartesian_bond_points[k][2]);

      glEnd ();

      glPopMatrix ();

      /* Colour blocks */

      glColor3f (red, green, blue);

      line_width = 0.1 * get_bond_scale () * get_smallest_cov_rad ();
      
      update_canvas_bond_points (i, j, False, line_width);

      glBegin (GL_POLYGON);

      k = 0;

      glVertex3f (cartesian_bond_points[k][0],
                  cartesian_bond_points[k][1],
                  cartesian_bond_points[k][2]);

      k = 1;

      glVertex3f (cartesian_bond_points[k][0],
                  cartesian_bond_points[k][1],
                  cartesian_bond_points[k][2]);

      k = 4;

      glVertex3f (cartesian_bond_points[k][0],
                  cartesian_bond_points[k][1],
                  cartesian_bond_points[k][2]);

      k = 5;

      glVertex3f (cartesian_bond_points[k][0],
                  cartesian_bond_points[k][1],
                  cartesian_bond_points[k][2]);

      glEnd ();

    }
}

void gl_render_hbonds (void)
{

  void gl_render_hbond (int, int);

  int i, j;
  struct node *ptr;

  if ((hbond_flag == 0) || (any_hydrogen == 0)) return;

  for (i = 0; i < no_atoms; i++)
    {      

      ptr = hbond_adjacency_list[i];

      while (ptr != NULL)
        {
          j = (ptr->v);

          if (i > j)           /* don't do things twice */
            {
              if (atoms[i].visi && atoms[j].visi)
                {
                  gl_render_hbond (i, j);
                }
            }

          ptr = ptr->next;
            
        }
    }
}


void gl_render_hbond (int i,
                      int j)
{
  int k, l, no_segments = 5;
  double angle, atom_size, bond_length, segment_length, hbond_width;
  double start[3], end[3], bond_vector[3], segment[3];

  /* The whole bond */

  bond_vector[0] = atoms[j].x - atoms[i].x;
  bond_vector[1] = atoms[j].y - atoms[i].y;
  bond_vector[2] = atoms[j].z - atoms[i].z;

  normalize_vec (bond_vector);

  /* The visible portion of the bond */

  atom_size = gl_get_rendered_atom_size (i);

  start[0] = atoms[i].x + (atom_size * bond_vector[0]);
  start[1] = atoms[i].y + (atom_size * bond_vector[1]);
  start[2] = atoms[i].z + (atom_size * bond_vector[2]);

  atom_size = gl_get_rendered_atom_size (j);

  end[0] = atoms[j].x - (atom_size * bond_vector[0]);
  end[1] = atoms[j].y - (atom_size * bond_vector[1]);
  end[2] = atoms[j].z - (atom_size * bond_vector[2]);

  for (k = 0; k < 3; k++)
    {
      bond_vector[k] = end[k] - start[k];
    }

  bond_length = mod_of_vec (bond_vector);
  segment_length = bond_length / ((2 * no_segments) - 1);

  normalize_vec (bond_vector);
  angle = acos (bond_vector[2]) / DEG2RAD;

  glColorMaterial (GL_FRONT, GL_DIFFUSE);
  /* aro - as long as hbonds are always grayscale, they'll
     render properly in red/blue stereo mode */
  glColor3f (0.0, 0.0, 0.0);

  for (k = 0; k < no_segments; k++)
    {
      /* Coordinate for start of segment */

      for (l = 0; l < 3; l++)
        {
          segment[l] = start[l] + (k * 2) * bond_vector[l] * segment_length;
        }

      glPushMatrix ();
      glTranslatef (segment[0], segment[1], segment[2]);

      glRotatef (180.0, 0.0, 0.0, 1.0);
      glRotatef (angle, bond_vector[1], -1.0 * bond_vector[0], 0.0);

      /* length of bond segment */

      hbond_width = 0.15 * get_hbond_scale ();

      gluCylinder (gl_quadric,
                   hbond_width,
                   hbond_width,
                   segment_length,
                   gl_no_bond_segments,
                   1);

      glPopMatrix ();

    }
}

double gl_get_rendered_atom_size (int i)
{
  int get_gl_render_type (void);
  double atom_size = 0;

  /* Work out the size the atom is going to be rendered at taking into
     account the drawing mode (BALL_AND_STICK or TUBES) and whether or
     not atoms and bonds are being displayed */

  /* We need this for bounding box calculation; it is a duplication of
     effort when called from gl_render_atom. */

  gl_current_render_type = (atoms[i].gl_render_type == DEFAULT) ?
    get_gl_render_type () : atoms[i].gl_render_type;

  if ((gl_current_render_type == BALL_AND_STICK) || (! gl_use_lighting))
    {
      if (atom_flag)
        {
          /* If not bonded use VDW, otherwise covalent radius */

          if ((bond_adjacency_list[i] == NULL) && (never_use_vdw == 0))
            {
              atom_size = get_atom_scale () * atoms[i].vdw_rad;
            }
          else
            {
              atom_size = get_atom_scale () * atoms[i].cov_rad;
            }
        }
      else
        {
          atom_size = 0;        /* Atoms not being rendered */
        }
    }
  else if (gl_current_render_type == TUBES)
    {
      if (bond_adjacency_list[i] == NULL)
        {
          /* Atom is not bonded; only render if atoms being displayed */

          if (atom_flag)
            {
              if (never_use_vdw == 1)
                {
                  atom_size = get_atom_scale () * atoms[i].cov_rad;
                }
              else
                {
                  atom_size = get_atom_scale () * atoms[i].vdw_rad;
                }
            }
          else
            {
              atom_size = 0;
            }
        }
      else
        {
          /* Atom is bonded; only render if bonds are being displayed */

          if (bond_flag)
            {
              atom_size = get_bond_width () / 2;
            }
          else
            {
              atom_size = 0;
            }
        }
    }

  return (atom_size);

}

void gl_render_vectors (void)
{

  void gl_render_vector (int, int);

  int i, j;

  if (vector_flag == 0)
    {
      return;
    }

  glColorMaterial (GL_FRONT, GL_DIFFUSE);

  for (i = 0; i < no_atoms; i++)
    {
      for (j = 0; j < MAX_VECTORS_PER_ATOM; j++)
        {
          if (atoms[i].has_vector > j)
            {
              gl_render_vector (i, j);
            }
        }
    }
}

void gl_render_vector (int i, int j)
{

  double get_vector_arrow_angle (void);
  double get_vector_arrow_scale (void);
  double get_vector_scale (void);

  double angle, height, mod_of_vector, top_radius;
  double vector_arrow_angle, vector_arrow_scale, vector_scale;
  double vector[3];
  double atom_size = gl_get_rendered_atom_size (i);

  vector_arrow_angle = get_vector_arrow_angle ();
  vector_arrow_scale = get_vector_arrow_scale ();
  vector_scale = get_vector_scale ();

  /* Draw stem */
  glColor3f (0.5, 0.5, 0.5); 

  vector[0] = atoms[i].v[j][0] * vector_scale;
  vector[1] = atoms[i].v[j][1] * vector_scale;
  vector[2] = atoms[i].v[j][2] * vector_scale;

  mod_of_vector = mod_of_vec (vector);

  normalize_vec (vector);

  height = mod_of_vector * vector_arrow_scale;

  angle = acos (vector[2]) / DEG2RAD;

  glPushMatrix ();

  glTranslatef (atoms[i].x, atoms[i].y, atoms[i].z);
  glRotatef (180.0, 0.0, 0.0, 1.0);
  glRotatef (angle, vector[1], -1.0 * vector[0], 0.0);

  glTranslatef (0.0, 0.0, atom_size);

  gluCylinder (gl_quadric,
               0.025,
               0.025,
               mod_of_vector - (height + atom_size),
               16,
               1);

  glPopMatrix ();

  /* Draw head  */

  glColor3f (0.0, 0.0, 0.0);

  top_radius = height *
    tan (vector_arrow_angle * DEG2RAD);

  glPushMatrix ();

  glTranslatef (atoms[i].x + (mod_of_vector - height) * vector[0],
                atoms[i].y + (mod_of_vector - height) * vector[1],
                atoms[i].z + (mod_of_vector - height) * vector[2]);
  glRotatef (180.0, 0.0, 0.0, -1.0);
  glRotatef (angle, vector[1], -1.0 * vector[0], 0.0);

  gluCylinder (gl_quadric,
               top_radius,
               0,           /* base radius */
               height,
               16,          /* slices */
               8);          /* stacks */

  glPopMatrix ();

}


void gl_init (void)
{
  void bg_color_init(void); /* aro */

  XColor get_bg_color (void);

  double red, green, blue;

  Boolean color_initialized = False;

  static XColor bg_color_rgb;

  GLfloat light_position[] = {0.0, 0.0, 1.0, 0.0};

  if (color_initialized == False)
    {
      if(bg_color_parsed == 0 && strcmp(bg_color,"") != 0){
        bg_color_init();
        bg_color_parsed = 1;
      }

      bg_color_rgb = get_bg_color ();

      red   = bg_color_rgb.red   / 65535.0;
      green = bg_color_rgb.green / 65535.0;
      blue  = bg_color_rgb.blue  / 65535.0;
    }

  glClearColor (red, green, blue, 0.0);

  glShadeModel (GL_SMOOTH);

  /* Lighting */

  glMatrixMode (GL_MODELVIEW);
  glLoadIdentity ();

  glLightfv (GL_LIGHT0, GL_POSITION, light_position);
  glEnable (GL_LIGHT0);
  glDepthFunc (GL_LESS);

  glEnable (GL_COLOR_MATERIAL);
 
  /* Allocate a quadric */

  if (gl_quadric == NULL)
    {
      gl_quadric = gluNewQuadric ();
    }
}

void gl_set_perspective (void)
{

  glMatrixMode (GL_PROJECTION);
  glLoadIdentity ();

  gl_update_near_and_far ();

  if (depth_is_on)
    {
      gluPerspective (gl_fov, 1.0, gl_perspective_near, gl_perspective_far);
    }
  else
    {
      double h;

      /* Use the field of view angle (gl_fov) to get the dimensions of
         the rendered scene (h) at the origin; this is a square gl_eye
         from the viewpoint */

      h = gl_eye * tan (gl_fov * PI / 360.0);

      glOrtho (-h, h, -h, h, gl_perspective_near, gl_perspective_far);
    }
}

void gl_update_near_and_far (void)
{
  double get_vector_scale (void);

  int i, j;
  
  double vector_scale = get_vector_scale ();
  double outline_offset = 0.1, shape_scale;
  Boolean max_set = False, min_set = False;

  /* Get the atoms with minimum/maximum Z for gluPerspective */

  for (i = 0; i < no_atoms; i++)
    {

      /* Take account of elliptical atoms */

      if (atoms[i].is_ellipse == 1)
        {
          shape_scale = atoms[i].max_shape;
        }
      else
        {
          shape_scale = 1.0;
        }

      /* Maximum distance of atom from origin; take in account the
         maximum radius of the atom since we don't want to clip it in
         the centre */

      if (max_set)
        {
          if ((atoms[i].z + (shape_scale * atoms[i].vdw_rad)) > gl_max_z)
            {
              gl_max_z = atoms[i].z + (shape_scale * atoms[i].vdw_rad);
            }
        }
      else
        {
          gl_max_z = atoms[i].z + (shape_scale * atoms[i].vdw_rad);
          max_set = True;
        }

      /* We use outline_offset since when in outline mode, the disk
         furthest from the eye disappears if some leeway isn't set */

      if (min_set)
        {
          if ((atoms[i].z - (shape_scale * atoms[i].vdw_rad)) < gl_min_z)
            {
              gl_min_z = (atoms[i].z - (shape_scale * atoms[i].vdw_rad)) - outline_offset;
            }
        }
      else
        {
          gl_min_z = (atoms[i].z - (shape_scale * atoms[i].vdw_rad)) - outline_offset;
          min_set = True;
        }
    }

  /* Now check the vectors */

  if (vector_flag == 1)
    {
      for (i = 0; i < no_atoms; i++)
        {
          for (j = 0; j < MAX_VECTORS_PER_ATOM; j++)
            {
              if (atoms[i].has_vector > j)
                {
                  if ((atoms[i].z + (atoms[i].v[j][2] * vector_scale)) > gl_max_z)
                    {
                      gl_max_z = atoms[i].z + (atoms[i].v[j][2] * vector_scale);
                    }
                  else if ((atoms[i].z + (atoms[i].v[j][2] * vector_scale)) < gl_min_z)
                    {
                      gl_min_z = atoms[i].z + (atoms[i].v[j][2] * vector_scale);
                    }
                }
            }
        }

      /* FIX ME -- we haven't taken into account the vector head,
         which may be clipped; fudge this */

      {
        double max_vector_size = 10.0;

        gl_max_z += max_vector_size;
        gl_min_z -= max_vector_size;
      }

    }

 
  if (gl_eye > gl_max_z)
    {
      gl_perspective_near = gl_eye - gl_max_z;
    }
  else
    {
      gl_perspective_near = 0.01; /* Small and positive */
    }

  if (gl_eye > gl_min_z)
    {
      gl_perspective_far = gl_eye - gl_min_z;
    }
  else
    {
      gl_perspective_far = 0.02; /* Small and slightly more positive */
    }

}

int get_gl_no_atom_segments (void)
{
  return (gl_no_atom_segments);
}

void set_gl_no_atom_segments (int new_value)
{
  gl_no_atom_segments = new_value;
}

int get_gl_no_bond_segments (void)
{
  return (gl_no_bond_segments);
}

void set_gl_no_bond_segments (int new_value)
{
  gl_no_bond_segments = new_value;
}

double get_gl_fov (void)
{
  return (gl_fov);
}

void set_gl_fov (double new_value)
{
  gl_fov = new_value;

  gl_set_perspective ();
}

double get_gl_eye (void)
{
  return (gl_eye);
}

void set_gl_eye (double new_value)
{
  gl_eye = new_value;

  gl_set_perspective ();
}

/* aro--> */

int get_gl_sep (void)
{
  return (gl_sep);
}

void set_gl_sep (int new_value)
{
  gl_sep = new_value;
}

/* Logical variable used to determine if GL rendering is active */

static Boolean render_using_gl = True;

void set_render_using_gl (Boolean value)
{
  render_using_gl = value;
}

Boolean render_using_gl_p (void)
{
  return (render_using_gl);
}

int get_gl_render_type (void)
{
  return (gl_render_type);
}

void set_gl_render_type (enum gl_render_types new_value)
{
  gl_render_type = new_value;
}

/* aro--> */

/* Variable used to determine whether stereo rendering is active */
static enum render_stereo_types render_stereo_mode = NO_STEREO;

void set_gl_render_stereo(enum render_stereo_types value)
{

#ifdef SGI_STEREO
  int get_monitor_frequency();
  void set_monitor_frequency(int);
  Boolean render_using_gl_p();
  
  static Dimension w;
  static Dimension h;
  Dimension new_w;
  static int refresh_rate;
  int event, error;
  char* display_env;
#endif /* SGI_STEREO */
  
  render_stereo_mode = value;

#ifdef SGI_STEREO
  if(value == SGI_HARDWARE && render_using_gl_p())
    {
      display_env = getenv("DISPLAY");

      if(display_env[0] != ':') 
        {
          fprintf(stderr, "Not running on a local display, can't change to SGI stereo mode\n");
        }
      else if(XSGIStereoQueryExtension(display, &event, &error) == False) 
        {
          fprintf(stderr, "SGI Stereo Extensions not supported by X server\n");
        } 
      else if(XSGIQueryStereoMode(display, XtWindow(canvas)) == X_STEREO_UNSUPPORTED) 
        {
          fprintf(stderr, "SGI Stereo Extensions not supported by current screen\n");
        } 
      else if(XSGIQueryStereoMode(display, XtWindow(canvas)) != STEREO_TOP)
        {
          XtVaSetValues(toplevel, XmNallowShellResize, True, NULL);
          XtVaGetValues(canvas, XmNwidth, &w, NULL);
          XtVaGetValues(canvas, XmNheight, &h, NULL);
          new_w = w / 0.48;
          XtVaSetValues(canvas, XmNwidth, new_w, NULL);
          /* Hard coded 392 for now, not sure if this is uniform 
             resolution across SGI's when in STR_TOP mode */
          XtVaSetValues(canvas, XmNheight, 392, NULL);
          XtVaSetValues(toplevel, XmNallowShellResize, False, NULL);
          
          refresh_rate = get_monitor_frequency();

          system("/usr/gfx/setmon -n STR_TOP");
        }
      
    }
  else if(XSGIQueryStereoMode(display, XtWindow(canvas)) == STEREO_TOP)
    {
      set_monitor_frequency(refresh_rate); /*makes system call to setmon */
      
      XtVaSetValues(toplevel, XmNallowShellResize, True, NULL);
      XtVaSetValues(canvas, XmNwidth, w, NULL);
      XtVaSetValues(canvas, XmNheight, h, NULL);
      XtVaSetValues(toplevel, XmNallowShellResize, False, NULL);
      
    }
  
#endif /* SGI_STEREO */

}

enum render_stereo_types get_gl_render_stereo()
{
  return render_stereo_mode;
}
/* <--aro */

void gl_set_lighting (int new_value)
{
  gl_use_lighting = new_value;
}

int gl_get_lighting (void)
{
  return (gl_use_lighting);
}

void gl_set_specular_lighting (int new_value)
{
  GLfloat specular[] = {0.5, 0.5, 0.5, 1.0}; /* Configurable? */
  GLfloat no_specular[] = {0.0, 0.0, 0.0, 1.0};

  gl_use_specular_lighting = new_value;

  if (gl_use_specular_lighting)
    {
      glMaterialfv (GL_FRONT, GL_SPECULAR, specular);
      glMaterialfv (GL_FRONT, GL_SHININESS, gl_shininess);
    }
  else
    {
      glMaterialfv (GL_FRONT, GL_SPECULAR, no_specular);
      glMaterialfv (GL_FRONT, GL_SHININESS, gl_shininess);
    }
}

int gl_get_specular_lighting (void)
{
  return (gl_use_specular_lighting);
}


void gl_set_shininess (int new_value)
{
  GLfloat specular[] = {0.5, 0.5, 0.5, 1.0}; /* Configurable? */
  GLfloat no_specular[] = {0.0, 0.0, 0.0, 1.0};

  gl_shininess[0] = new_value;

  if (gl_use_specular_lighting)
    {
      glMaterialfv (GL_FRONT, GL_SPECULAR, specular);
      glMaterialfv (GL_FRONT, GL_SHININESS, gl_shininess);
    }
  else
    {
      glMaterialfv (GL_FRONT, GL_SPECULAR, no_specular);
      glMaterialfv (GL_FRONT, GL_SHININESS, gl_shininess);
    }
}

int gl_get_shininess (void)
{
  return (gl_shininess[0]);
}

void set_gl_copy_canvas (int value)
{
  gl_copy_canvas = value;
}

#endif /* GL */