forked from root-project/root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
x3d.c
3610 lines (2832 loc) · 107 KB
/
x3d.c
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
/* @(#)root/x3d:$Id$ */
/* Author: Mark Spychalla*/
/*
Copyright 1992 Mark Spychalla
Permission to use, copy, modify, distribute, and sell this software and
its documentation for any purpose is hereby granted without fee,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the name of Mark Spychalla not be used
in advertising or publicity pertaining to distribution of the software
without specific, written prior permission. Mark Spychalla makes no
representations about the suitability of this software for any purpose.
It is provided "as is" without express or implied warranty.
Mark Spychalla disclaims all warranties with regard to this software,
including all implied warranties of merchantability and fitness, in no
event shall Mark Spychalla be liable for any special, indirect or
consequential damages or any damages whatsoever resulting from loss of use,
data or profits, whether in an action of contract, negligence or other
tortious action, arising out of or in connection with the use or performance
of this software.
*/
/*
NOTE ON X3D CODING STYLE:
Don't think I usually code in the gerberized fashion that X3D demonstrates.
X3D was written for speed at any cost. My goal was to write the fastest 3D
object viewer that I could, period. Regular programs ought to be written
with different goals in mind such as:
1) A program has excellent documentation that ANYONE can read.
2) A program when released has no strange "features" or bugs.
3) A program is robust and handles ALL extreme and unusual cases.
4) A program is written in phases and modules with hard tests for each one.
5) A program is written for any user who doesn't need special knowledge
to use the program.
6) A program has well defined user requirements and functional specifications.
7) A program is written with regard to future expansion and integreation
with other systems (portability).
When programming following these additional principles make programs easier
to maintain.
A) Choose variable names that accurately describes what the variable does/is.
B) Write comments to inform someone faced with the task of modifying your code.
C) Avoid excessive comments. Write the code so that it says what it does.
D) Follow a strict one-in, one-out flow of control structues except in the
case of fatal error conditions.
E) Avoid using global variables.
F) Do not cause side effects to variables that were not parameters to a
function.
G) Have a single function perform a single purpose.
H) Select a single indentation style and stick with it.
I) Use a consistent naming convention.
The following principles help me when I try optimizing code:
a) If optimizing, use a profiler to determine which sections of code most of
the time is spent in. Spend most of your effort in the most used sections.
Don't bother optimizing a procedure using less than 10% of the time.
b) High level optimizations are far more effective than cycle shaving.
(e.g. use quick sort instead of optimizing a bubble sort.)
c) Be flexible in your approach to solving a problem. List exactly what you
need as a result at a minimum. Get rid of unnecessary assumptions.
d) Become familiar with sets of operations that are equivalent, or nearly so.
Learn the relative expense of basic operations.
e) If possible, be careful not to needlessly sacrifice significant readability
of the code for a cycle or two.
-- Spy
*/
#ifndef WIN32
#include "x3d.h"
#include "X3DBuffer.h"
#endif
#ifdef WIN32
unsigned long x3d_main(float *longitude, float *latitude, float *psi,
const char *string) { return 0L; }
void x3d_terminate() { }
void x3d_get_position(float *longitude, float *latitude, float *psi) { }
int x3d_dispatch_event(unsigned long event) { return 0; }
void x3d_set_display(unsigned long display) { }
void x3d_update() { }
#else
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xutil.h>
#include <X11/X.h>
extern Color *colors;
extern point *points;
extern segment *segs;
extern polygon *polys;
extern int currPoint, currSeg, currPoly;
static polygon **list;
static point *bounds;
static int quitApplication = 0;
static Display *gDisplay = NULL;
static Ginfo *gGInfo = NULL;
static Oinfo *gOInfo = NULL;
static int gRedDiv, gGreenDiv, gBlueDiv, gRedShift, gGreenShift, gBlueShift;
static void sort(list1, numPolys)
polygon **list1;
int numPolys;
/*****************************************************************************
Specialized quick sort for painter algorithm.
*****************************************************************************/
{
polygon **v0, **v1, **v2, **v3, **v4, **v5, **v6, *poly;
register int stackIndex, stackNotSet, length, start, end, high;
float dist;
int numPoints;
StackElement stack[MAXSTACK];
point **Point, **lastPoint;
v0 = list1;
v1 = &(list1[numPolys]);
/* Set the key value to be the average of the vertices' distances */
while(v0 < v1){
poly = *v0;
numPoints = poly->numPoints;
Point = poly->points;
lastPoint = Point + numPoints;
dist = 0.0;
do{
dist += (*Point)->dist;
Point++;
}while(Point < lastPoint);
poly->dist = dist / ((float)numPoints);
v0++;
}
/* Initialize for the qsort() */
stackIndex = 1;
stackNotSet = 0;
start = 0;
end = numPolys - 1;
/* Do Qsort */
while(stackIndex){
if(stackNotSet){
start = stack[stackIndex].start;
end = stack[stackIndex].end;
}
stackIndex--;
stackNotSet = 1;
length = end - start;
/* Big enough to qsort ? */
if(length > STOP){
v1 = &(list1[start]);
v2 = &(list1[start + (length / 4)]);
v3 = &(list1[start + (length / 2)]);
v4 = &(list1[start + ((length * 3) / 4)]);
v5 = &(list1[end]);
v6 = v1;
median5(v1,v2,v3,v4,v5)
*v0 = *v3;
*v3 = *v6;
*v6 = *v0;
v1 = &(list1[start + 1]);
v2 = &(list1[end]);
/* Split */
dist = (*v6)->dist;
while((*v2)->dist < dist) v2--;
while((*v1)->dist > dist) v1++;
v5 = v0;
while(v1 < v2){
*v5 = *v2;
*v2 = *v1;
v5 = v1;
do{
v2--;
}while(((*v2)->dist < dist) && (v1 < v2));
if (v2 <= v1) break;
do{
v1++;
}while(((*v1)->dist > dist) && (v1 < v2));
if (v2 <= v1) break;
}
v2 = v1 - 1;
*v5 = *v2;
*v2 = *v6;
*v6 = *v0;
high = v2 - list1;
/* Put sublists on the stack, smallest on top */
if((high - start) > (end - high)){
stack[++stackIndex].start = start;
stack[stackIndex].end = high - 1;
++stackIndex;
start = high + 1;
stackNotSet = 0;
}else{
stack[++stackIndex].start = high + 1;
stack[stackIndex].end = end;
++stackIndex;
end = high - 1;
stackNotSet = 0;
}
}
}
/* insertion sort all the remaining sublists at once */
v2 = list1;
v3 = &(list1[numPolys - 1]);
v4 = v2 + 1;
while(v4 <= v3){
*v0 = *v4;
v1 = v4 - 1;
while((v1 >= v2) && ((*v1)->dist < (*v0)->dist)){
*(v1 + 1) = *v1;
v1--;
}
*(v1 + 1) = *v0;
v4++;
}
}
static void Rotate(points1, cx, cy, cz, sx, sy, sz)
anglePoint *points1;
double cx, cy, cz, sx, sy, sz;
/******************************************************************************
Rotate about Z, X, then Y, for two points.
******************************************************************************/
{
int index1;
double x, y, z, t;
for(index1 = 0; index1 < 2; index1++){
x = points1[index1].x;
y = points1[index1].y;
z = points1[index1].z;
t = x * cz + y * sz;
y = y * cz - x * sz;
x = t;
points1[index1].y = y * cx + z * sx;
z = z * cx - y * sx;
points1[index1].x = x * cy + z * sy;
points1[index1].z = z * cy - x * sy;
}
}
static double DotProduct(x1, Y1, x2, y2)
double x1, Y1, x2, y2;
/******************************************************************************
Dot product (calculate the cosine of the angle between two vectors).
******************************************************************************/
{
double temp;
if((x1 == 0.0 && Y1 == 0.0)){
return 1.0;
}
temp = sqrt(x1 * x1 + Y1 * Y1);
x1 = x1 / temp;
Y1 = Y1 / temp;
temp = x1 * x2 + Y1 * y2;
if(temp > 1.0)
temp = fmod(temp, 1.0);
if(temp < -1.0)
temp = -fmod(-temp, 1.0);
return(temp);
}
static void CalculateAngles(X, Y, Z, X1, Y1, Z1)
double *X, *Y, *Z;
double X1, Y1, Z1;
/******************************************************************************
Calculate what the result of the angle changes of X1, Y1, and Z1 are
in my weird coordinate system.
******************************************************************************/
{
anglePoint points1[2];
points1[0].x = 0.0; points1[0].y = 0.0; points1[0].z = 1.0;
points1[1].x = 1.0; points1[1].y = 0.0; points1[1].z = 0.0;
Rotate(points1, cos(*X), cos(*Y), cos(*Z), sin(*X), sin(*Y), sin(*Z));
Rotate(points1, cos(X1), cos(Y1), cos(Z1), sin(X1), sin(Y1), sin(Z1));
*Y = acos(DotProduct(points1[0].x, points1[0].z, 0.0, 1.0));
if(points1[0].x < 0.0)
*Y = -*Y;
Rotate(points1, 1.0, cos(-*Y), 1.0, 0.0, sin(-*Y), 0.0);
*X = acos(DotProduct(points1[0].y, points1[0].z, 0.0, 1.0));
if(points1[0].y < 0.0)
*X = -*X;
Rotate(points1, cos(-*X), 1.0, 1.0, sin(-*X), 0.0, 0.0);
*Z = acos(DotProduct(points1[1].x, points1[1].y, 1.0, 0.0));
if(!(points1[1].y < 0.0))
*Z = -*Z;
}
static void DrawLogo(g, x, y)
Ginfo *g;
int x, y;
/******************************************************************************
Display the Logo.
******************************************************************************/
{
int hUnit, vUnit;
_XPoint points1[512];
void *ptrp = points1;
hUnit = XTextWidth(g->font, LONGESTSTRING, strlen(LONGESTSTRING)) /
strlen(LONGESTSTRING);
vUnit = FONTHEIGHT(g->font);
/* X */
points1[0].x = 9 * hUnit + x; points1[0].y = 1 * vUnit + y;
points1[1].x = 9 * hUnit + vUnit + x; points1[1].y = 1 * vUnit + y;
points1[2].x = 14 * hUnit + vUnit + x; points1[2].y = 6 * vUnit + y;
points1[3].x = 14 * hUnit + x; points1[3].y = 6 * vUnit + y;
XFillPolygon(g->dpy, g->helpWin, g->helpGc, ptrp, 4, Convex,
CoordModeOrigin);
points1[0].x = 14 * hUnit + vUnit + x; points1[0].y = 1 * vUnit + y;
points1[1].x = 14 * hUnit + x; points1[1].y = 1 * vUnit + y;
points1[2].x = 9 * hUnit + x; points1[2].y = 6 * vUnit + y;
points1[3].x = 9 * hUnit + vUnit + x; points1[3].y = 6 * vUnit + y;
XFillPolygon(g->dpy, g->helpWin, g->helpGc, ptrp, 4, Convex,
CoordModeOrigin);
/* 3 */
points1[0].x = 18 * hUnit + x; points1[0].y = 1 * vUnit + y;
points1[1].x = 22 * hUnit + x; points1[1].y = 1 * vUnit + y;
points1[2].x = 23 * hUnit + x; points1[2].y = 2 * vUnit + y;
points1[3].x = 18 * hUnit + x; points1[3].y = 2 * vUnit + y;
XFillPolygon(g->dpy, g->helpWin, g->helpGc, ptrp, 4, Convex,
CoordModeOrigin);
points1[0].x = 23 * hUnit - vUnit + x; points1[0].y = 2 * vUnit + y;
points1[1].x = 23 * hUnit + x; points1[1].y = 2 * vUnit + y;
points1[2].x = 23 * hUnit + x; points1[2].y = 3 * vUnit + y;
points1[3].x = 23 * hUnit - vUnit + x; points1[3].y = 4 * vUnit + y;
XFillPolygon(g->dpy, g->helpWin, g->helpGc, ptrp, 4, Convex,
CoordModeOrigin);
points1[0].x = 23 * hUnit - vUnit + x; points1[0].y = 3 * vUnit + y;
points1[1].x = 23 * hUnit + x; points1[1].y = 4 * vUnit + y;
points1[2].x = 23 * hUnit + x; points1[2].y = 5 * vUnit + y;
points1[3].x = 23 * hUnit - vUnit + x; points1[3].y = 5 * vUnit + y;
XFillPolygon(g->dpy, g->helpWin, g->helpGc, ptrp, 4, Convex,
CoordModeOrigin);
points1[0].x = 18 * hUnit + x; points1[0].y = 5 * vUnit + y;
points1[1].x = 23 * hUnit + x; points1[1].y = 5 * vUnit + y;
points1[2].x = 22 * hUnit + x; points1[2].y = 6 * vUnit + y;
points1[3].x = 18 * hUnit + x; points1[3].y = 6 * vUnit + y;
XFillPolygon(g->dpy, g->helpWin, g->helpGc, ptrp, 4, Convex,
CoordModeOrigin);
points1[0].x = 19 * hUnit + x; points[0].y = 3 * vUnit + y;
points1[1].x = 23 * hUnit - vUnit + x; points1[1].y = 3 * vUnit + y;
points1[2].x = 23 * hUnit - vUnit + x; points1[2].y = 4 * vUnit + y;
points1[3].x = 19 * hUnit + x; points1[3].y = 4 * vUnit + y;
XFillPolygon(g->dpy, g->helpWin, g->helpGc, ptrp, 4, Convex,
CoordModeOrigin);
/* D */
points1[0].x = 26 * hUnit + x; points1[0].y = 1 * vUnit + y;
points1[1].x = 30 * hUnit + x; points1[1].y = 1 * vUnit + y;
points1[2].x = 30 * hUnit + vUnit + x; points1[2].y = 2 * vUnit + y;
points1[3].x = 26 * hUnit + x; points1[3].y = 2 * vUnit + y;
XFillPolygon(g->dpy, g->helpWin, g->helpGc, ptrp, 4, Convex,
CoordModeOrigin);
points1[0].x = 26 * hUnit + x; points1[0].y = 5 * vUnit + y;
points1[1].x = 30 * hUnit + vUnit + x; points1[1].y = 5 * vUnit + y;
points1[2].x = 30 * hUnit + x; points1[2].y = 6 * vUnit + y;
points1[3].x = 26 * hUnit + x; points1[3].y = 6 * vUnit + y;
XFillPolygon(g->dpy, g->helpWin, g->helpGc, ptrp, 4, Convex,
CoordModeOrigin);
points1[0].x = 26 * hUnit + x; points1[0].y = 1 * vUnit + y;
points1[1].x = 26 * hUnit + vUnit + x; points1[1].y = 1 * vUnit + y;
points1[2].x = 26 * hUnit + vUnit + x; points1[2].y = 6 * vUnit + y;
points1[3].x = 26 * hUnit + x; points1[3].y = 6 * vUnit + y;
XFillPolygon(g->dpy, g->helpWin, g->helpGc, ptrp, 4, Convex,
CoordModeOrigin);
points1[0].x = 30 * hUnit + x; points1[0].y = 2 * vUnit + y;
points1[1].x = 30 * hUnit + vUnit + x; points1[1].y = 2 * vUnit + y;
points1[2].x = 30 * hUnit + vUnit + x; points1[2].y = 5 * vUnit + y;
points1[3].x = 30 * hUnit + x; points1[3].y = 5 * vUnit + y;
XFillPolygon(g->dpy, g->helpWin, g->helpGc, ptrp, 4, Convex,
CoordModeOrigin);
}
static void DisplayMenu(g)
Ginfo *g;
/******************************************************************************
Display the help menu.
******************************************************************************/
{
int x = 5, y = 5;
XSetFont(g->dpy, g->helpGc, g->font->fid );
XSetWindowBackground(g->dpy, g->helpWin, g->black);
XSetForeground(g->dpy, g->helpGc, g->white);
XSetBackground(g->dpy, g->helpGc, g->black);
XSetStipple(g->dpy, g->helpGc, g->stipple[NUMSTIPPLES / 3]);
XSetFillStyle(g->dpy, g->helpGc, FillOpaqueStippled);
DrawLogo(g, (XTextWidth(g->font, LONGESTSTRING, strlen(LONGESTSTRING)) /
(int)strlen(LONGESTSTRING)) / 2, FONTHEIGHT(g->font) / 3);
XSetFillStyle(g->dpy, g->helpGc, FillSolid);
DrawLogo(g, 0, 0);
HelpPrint(g,x,y,"");
HelpPrint(g,x,y,"");
HelpPrint(g,x,y,"");
HelpPrint(g,x,y,"");
HelpPrint(g,x,y,"");
HelpPrint(g,x,y,"");
HelpPrint(g,x,y,"");
HelpPrint(g,x,y," VERSION 2.2");
HelpPrint(g,x,y,"");
HelpPrint(g,x,y," CONTROLS SUMMARY");
HelpPrint(g,x,y,"");
HelpPrint(g,x,y," QUIT q Q");
HelpPrint(g,x,y," WIREFRAME MODE w W");
HelpPrint(g,x,y," HIDDEN LINE MODE e E");
HelpPrint(g,x,y," HIDDEN SURFACE MODE r R");
HelpPrint(g,x,y," MOVE OBJECT DOWN u U");
HelpPrint(g,x,y," MOVE OBJECT UP i I");
HelpPrint(g,x,y," TOGGLE CONTROLS STYLE o O");
HelpPrint(g,x,y," TOGGLE STEREO DISPLAY s S");
HelpPrint(g,x,y," TOGGLE BLUE STEREO VIEW d D");
HelpPrint(g,x,y," TOGGLE DOUBLE BUFFER f F");
HelpPrint(g,x,y," MOVE OBJECT RIGHT h H");
HelpPrint(g,x,y," MOVE OBJECT BACKWARD j J");
HelpPrint(g,x,y," MOVE OBJECT FOREWARD k K");
HelpPrint(g,x,y," MOVE OBJECT LEFT l L");
HelpPrint(g,x,y," TOGGLE HELP MENU m M");
HelpPrint(g,x,y," ROTATE ABOUT X x X a A");
HelpPrint(g,x,y," ROTATE ABOUT Y y Y b B");
HelpPrint(g,x,y," ROTATE ABOUT Z z Z c C");
HelpPrint(g,x,y," AUTOROTATE ABOUT X 1 2 3");
HelpPrint(g,x,y," AUTOROTATE ABOUT Y 4 5 6");
HelpPrint(g,x,y," AUTOROTATE ABOUT Z 7 8 9");
HelpPrint(g,x,y," ADJUST FOCUS [ ] { }");
HelpPrint(g,x,y,"");
HelpPrint(g,x,y," POINTER MOVEMENT WITH LEFT BUTTON :");
HelpPrint(g,x,y,"");
HelpPrint(g,x,y," ROTATE OBJECT ABOUT X Vertical");
HelpPrint(g,x,y," ROTATE OBJECT ABOUT Z Horizontal");
}
static void ResetPurpleRectangle(XL, YL, XH, YH, g)
int XL, YL, XH, YH;
Ginfo *g;
/******************************************************************************
Reset the vertices of the purple rectangle.
******************************************************************************/
{
g->redSegments[3].x1 = (XL + MARGIN);
g->blueSegments[3].x1 = (XL + MARGIN);
g->redSegments[3].y1 = (YL + MARGIN);
g->blueSegments[3].y1 = (YL + MARGIN);
g->redSegments[3].x2 = (XH - MARGIN);
g->blueSegments[3].x2 = (XH - MARGIN);
g->redSegments[3].y2 = (YL + MARGIN);
g->blueSegments[3].y2 = (YL + MARGIN);
g->redSegments[2].x1 = (XH - MARGIN);
g->blueSegments[2].x1 = (XH - MARGIN);
g->redSegments[2].y1 = (YH - MARGIN);
g->blueSegments[2].y1 = (YH - MARGIN);
g->redSegments[2].x2 = (XL + MARGIN);
g->blueSegments[2].x2 = (XL + MARGIN);
g->redSegments[2].y2 = (YH - MARGIN);
g->blueSegments[2].y2 = (YH - MARGIN);
g->redSegments[1].x1 = (XH - MARGIN);
g->blueSegments[1].x1 = (XH - MARGIN);
g->redSegments[1].y1 = (YL + MARGIN);
g->blueSegments[1].y1 = (YL + MARGIN);
g->redSegments[1].x2 = (XH - MARGIN);
g->blueSegments[1].x2 = (XH - MARGIN);
g->redSegments[1].y2 = (YH - MARGIN);
g->blueSegments[1].y2 = (YH - MARGIN);
g->redSegments[0].x1 = (XL + MARGIN);
g->blueSegments[0].x1 = (XL + MARGIN);
g->redSegments[0].y1 = (YL + MARGIN);
g->blueSegments[0].y1 = (YL + MARGIN);
g->redSegments[0].x2 = (XL + MARGIN);
g->blueSegments[0].x2 = (XL + MARGIN);
g->redSegments[0].y2 = (YH - MARGIN);
g->blueSegments[0].y2 = (YH - MARGIN);
}
static void OneBitSetColors(g)
Ginfo *g;
/******************************************************************************
Set up color information/stipples for a one bit display.
******************************************************************************/
{
int index1;
Color *color;
int numColors;
color = g->colors;
numColors = g->numColors;
/* Set the colors (may not be used) */
for(index1 = 0; index1 < numColors; index1++){
color[index1].value = 1;
/* Set the stipples */
color[index1].stipple =(int)((double)NUMSTIPPLES *
((double)sqrt((double)(
(double)color[index1].red * (double)color[index1].red +
(double)color[index1].green * (double)color[index1].green +
(double)color[index1].blue * (double)color[index1].blue))
/ MAXCOLORDIST));
}
}
static void EightBitSetColors(g)
Ginfo *g;
/******************************************************************************
Set up color information/stipples for a eight bit display.
******************************************************************************/
{
Color *colors1;
int numColors;
int colorIndex = 0;
int index1, index2, redIndex, blueIndex, greenIndex;
XColor c;
colors1 = g->colors;
numColors = g->numColors;
/* Put "black" into the place reserved for it in the end */
colors1[numColors].red = 0;
colors1[numColors].green = 0;
colors1[numColors].blue = 0;
/* Put "red" into the place reserved for it in the end */
colors1[numColors + 1].red = 255;
colors1[numColors + 1].green = 0;
colors1[numColors + 1].blue = 0;
/* Put "blue" into the place reserved for it in the end */
colors1[numColors + 2].red = 0;
colors1[numColors + 2].green = 0;
colors1[numColors + 2].blue = 255;
/* Put "purple" into the place reserved for it in the end */
colors1[numColors + 3].red = 255;
colors1[numColors + 3].green = 0;
colors1[numColors + 3].blue = 255;
/* Blank out the colormap */
for(index1 = 0; index1 < 256; index1++){
c.red = 0;
c.green = 0;
c.blue = 0;
c.flags = DoRed | DoGreen | DoBlue;
c.pixel = 255;
c.pad = 0;
g->cmapColors[0][index1] = c;
g->cmapColors[1][index1] = c;
g->cmapColors[2][index1] = c;
}
if(numColors <= BUFFER_CMAP){
colorIndex= numColors + 3;
index1 = 15;
/* Set stipple, and colormap double buffer colors */
while((index1 > 0) && (colorIndex >= 0)){
c.red = colors1[colorIndex].red << 8;
c.green = colors1[colorIndex].green << 8;
c.blue = colors1[colorIndex].blue << 8;
c.flags = DoRed | DoGreen | DoBlue;
colors1[colorIndex].value = index1 * 16 + index1;
colors1[colorIndex].stipple =(int)((double)NUMSTIPPLES *
((double)sqrt((double)(
(double)colors1[colorIndex].red * (double)colors1[colorIndex].red +
(double)colors1[colorIndex].green * (double)colors1[colorIndex].green +
(double)colors1[colorIndex].blue * (double)colors1[colorIndex].blue))
/ MAXCOLORDIST));
for(index2 = 1; index2 < 16; index2++){
c.pixel = index2 * 16 + index1;
g->cmapColors[0][index2 * 16 + index1] = c;
c.pixel = index1 * 16 + index2;
g->cmapColors[1][index1 * 16 + index2] = c;
}
index1--;
colorIndex--;
}
}else{
/* Set permanent black, red, blue, purple for cmap double buffer */
for(index1 = 0; index1 < 4; index1++){
c.red = colors1[numColors + index1].red << 8;
c.green = colors1[numColors + index1].green << 8;
c.blue = colors1[numColors + index1].blue << 8;
c.flags = DoRed | DoGreen | DoBlue;
c.pixel = 12 + index1;
g->cmapColors[0][12 + index1] = c;
g->cmapColors[1][12 + index1] = c;
colors1[numColors + index1].value = c.pixel;
}
if(numColors <= MAX_COLORS){
colorIndex = 0;
index1 = 9;
index2 = 0;
/* Fill in the rest of the colors */
while(colorIndex < numColors){
if((index1 < 12) || (index1 > 15)){
c.red = colors1[colorIndex].red << 8;
c.green = colors1[colorIndex].green << 8;
c.blue = colors1[colorIndex].blue << 8;
c.flags = DoRed | DoGreen | DoBlue;
c.pixel = index1;
g->cmapColors[0][index1] = c;
g->cmapColors[1][index1] = c;
colors1[colorIndex].value = index1;
colors1[colorIndex].stipple =(int)((double)NUMSTIPPLES *
((double)sqrt((double)(
(double)colors1[colorIndex].red *
(double)colors1[colorIndex].red +
(double)colors1[colorIndex].green *
(double)colors1[colorIndex].green +
(double)colors1[colorIndex].blue *
(double)colors1[colorIndex].blue))
/ MAXCOLORDIST));
colorIndex++;
}
index1++;
}
}else{
index1 = 17;
index2 = 0;
redIndex = 0;
greenIndex = 0;
blueIndex = 0;
/* Otherwise use a default lot */
while(blueIndex < MAXVALUE){
c.red = (redIndex * VALUESCALE) << 8;
c.green = (greenIndex * VALUESCALE) << 8;
c.blue = (blueIndex * VALUESCALE) << 8;
c.flags = DoRed | DoGreen | DoBlue;
c.pixel = index1;
g->cmapColors[0][index1] = c;
g->cmapColors[1][index1] = c;
redIndex++;
if(redIndex >= MAXVALUE){
redIndex = 0;
greenIndex++;
}
if(greenIndex >= MAXVALUE){
greenIndex = 0;
blueIndex++;
}
index1++;
}
for(index1 = 0; index1 < numColors; index1++){
colors1[index1].value = colors1[index1].red * 36 +
colors1[index1].green * 6 + colors1[index1].blue + 17;
colors1[colorIndex].stipple =(int)((double)NUMSTIPPLES *
((double)sqrt((double)(
(double)colors1[colorIndex].red *
(double)colors1[colorIndex].red +
(double)colors1[colorIndex].green *
(double)colors1[colorIndex].green +
(double)colors1[colorIndex].blue *
(double)colors1[colorIndex].blue))
/ MAXCOLORDIST));
}
}
}
/* Set the colors for the special fast colormap double buffer */
index1 = 0;
for(redIndex = 0; redIndex < 4; redIndex++){
for(blueIndex = 0; blueIndex < 4; blueIndex++){
if(redIndex != blueIndex){
g->wireframeColors[0][index1] =
g->cmapColors[0][(redIndex + 12) * 16 + (blueIndex + 12)];
g->wireframeColors[1][index1] =
g->cmapColors[1][(redIndex + 12) * 16 + (blueIndex + 12)];
index1++;
}
}
}
/* Just in case set the rest of the colors */
for(index1 = 13; index1 < 256; index1++){
g->wireframeColors[0][index1] = g->wireframeColors[0][3];
g->wireframeColors[1][index1] = g->wireframeColors[1][3];
}
/* Set the colors for the pix stereo mode */
for(redIndex = 0; redIndex < 15; redIndex++){
for(blueIndex = 0; blueIndex < 15; blueIndex++){
c.red = (redIndex * 17) << 8;
c.green = 0;
c.blue = (blueIndex * 17) << 8;
c.flags = DoRed | DoGreen | DoBlue;
c.pixel = (redIndex + 1) * 16 + (blueIndex + 1);
g->cmapColors[2][c.pixel] = c;
}
}
/* Set stereoColor to nearest color */
for(index1 = 0; index1 < numColors; index1++){
colorIndex = (int)((double)15 *
((double)sqrt((double)((double)colors1[index1].red *
(double)colors1[index1].red + (double)colors1[index1].green *
(double)colors1[index1].green + (double)colors1[index1].blue *
(double)colors1[index1].blue)) / MAXCOLORDIST));
colors1[index1].stereoColor = (colorIndex + 1) * 16 + (colorIndex + 1);
}
/* Set various important color values */
g->stereoBlack = (0 + 1) * 16 + (0 + 1);
g->redMask = BUFFER0;
g->blueMask = BUFFER1;
g->Black = colors1[numColors].value;
g->Red = colors1[numColors + 1].value;
g->Blue = colors1[numColors + 2].value;
g->Purple = colors1[numColors + 3].value;
}
static void TrueColorSetColors(g)
Ginfo *g;
/******************************************************************************
Set up color information/stipples for TrueColor displays.
******************************************************************************/
{
int index1, colorValue;
Color *colors1;
int numColors;
/* On TrueColor displays the color pixel value composed directly of
r, g and b components. The order of the r, g and b and the number
of bits used for each color is specified by the X server and decoded
in the gRedShift, gRedDiv etc. values. Since X3D uses 255 as max
color the div is the number of bits to left shift (divide) from 255.
*/
colors1 = g->colors;
numColors = g->numColors;
for(index1 = 0; index1 < numColors; index1++){
/* In TrueColor every color is what it is */
colors1[index1].value =
(colors1[index1].red >> gRedDiv) << gRedShift |
(colors1[index1].green >> gGreenDiv) << gGreenShift |
(colors1[index1].blue >> gBlueDiv) << gBlueShift;
/* Set stipple */
colors1[index1].stipple =(int)((double)NUMSTIPPLES *
((double)sqrt((double)(
(double)colors1[index1].red * (double)colors1[index1].red +
(double)colors1[index1].green * (double)colors1[index1].green +
(double)colors1[index1].blue * (double)colors1[index1].blue))
/ MAXCOLORDIST));
/* Set stereo color */
colorValue= (int)((double)(255 >> gRedDiv) *
((double)sqrt((double)((double)colors1[index1].red *
(double)colors1[index1].red + (double)colors1[index1].green *
(double)colors1[index1].green + (double)colors1[index1].blue *
(double)colors1[index1].blue)) / MAXCOLORDIST));
colors1[index1].stereoColor = (colorValue << gRedShift) |
(colorValue << gBlueShift);
}
/* Set various important color values */
g->stereoBlack = 0;
g->redMask = (255 >> gRedDiv) << gRedShift;
g->blueMask = (255 >> gBlueDiv) << gBlueShift;
g->Black = 0;
g->Red = (255 >> gRedDiv) << gRedShift;
g->Blue = (255 >> gBlueDiv) << gBlueShift;
g->Purple = g->Red | g->Blue;
}
char title[80];
Atom wm_protocols[2];
static void InitDisplay(o, g, parent)
Oinfo *o;
Ginfo *g;
Window parent;
/******************************************************************************
Set up an X window and our colormap. We rely on X's own error handling and
reporting for most bad X calls because X buffers requests.
******************************************************************************/
{
static int stipples[NUMSTIPPLES][NUMSTIPPLES * 2 + 1] = {
{0},
{1, 1, 1},
{2, 0, 2, 2, 1},
{3, 1, 0, 1, 2, 3, 1},
{4, 0, 1, 0, 3, 2, 1, 2, 3},
{5, 0, 0, 0, 2, 2, 0, 2, 3, 3, 2},
{6, 0, 2, 1, 1, 1, 3, 2, 0, 3, 1, 3, 3},
{7, 0, 1, 0, 3, 1, 0, 1, 1, 2, 2, 3, 1, 3, 3},
{8, 0, 1, 0, 3, 1, 0, 1, 2, 2, 1, 2, 3, 3, 0, 3, 2},
{9, 0, 0, 0, 2, 1, 2, 1, 3, 2, 0, 2, 1, 2, 3, 3, 1, 3, 2},
{10,0, 0, 0, 1, 0, 3, 1, 0, 1, 2, 2, 1, 2, 2, 2, 3, 3, 0, 3, 2},
{11,0, 1, 0, 3, 1, 0, 1, 1, 1, 2, 1, 3, 2, 1, 2, 2, 3, 0, 3, 2, 3, 3},
{12,0, 0, 0, 2, 1, 0, 1, 1, 1, 2, 1, 3, 2, 1, 2, 3, 3, 0, 3, 1, 3, 2, 3, 3},
{13,0, 0, 0, 2, 0, 3, 1, 0, 1, 1, 1, 2, 2, 0, 2, 2, 2, 3, 3, 0, 3, 1, 3, 2,
3, 3},
{14,0, 0, 0, 1, 0, 2, 0, 3, 1, 0, 1, 1, 1, 3, 2, 1, 2, 2, 2, 3, 3, 0, 3, 1,
3, 2, 3, 3},
{15,0, 0, 0, 1, 0, 2, 0, 3, 1, 0, 1, 2, 1, 3, 2, 0, 2, 1, 2, 2, 2, 3, 3, 0,
3, 1, 3, 2, 3, 3},
{16,0, 0, 0, 1, 0, 2, 0, 3, 1, 0, 1, 1, 1, 2, 1, 3, 2, 0, 2, 1, 2, 2, 2, 3,
3, 0, 3, 1, 3, 2, 3, 3}
};
char bits[(STIPPLESIZE * STIPPLESIZE) / BITSPERBYTE];
GC temp_gc;
XColor oldColormap[MAXCOLORS];
XWindowAttributes attributes;
XSetWindowAttributes attribs;
XWMHints wmhint;
int index1, index2, screen;
Visual *vis;
XSizeHints sizehint;
int x, y, NUMCOLORS;
unsigned int width, height, numSegments;
int useroot = 0;
if (gDisplay)
useroot = 1;
numSegments = o->numSegs;
if((g->redColors = (long *)calloc(1, (numSegments + 4) * (sizeof(long))))
== NULL){
(void)fprintf(stderr, "Unable to allocate memory for redColors\n"); return;}
if((g->redSegments = (XSegment *)calloc(1, (numSegments + 4) *
(sizeof(XSegment)))) == NULL){
(void)fprintf(stderr, "Unable to allocate memory for redSegments\n"); return;}
if((g->blueSegments = (XSegment *)calloc(1, (numSegments + 4) *
sizeof(XSegment))) == (XSegment *)NULL){
(void)fprintf(stderr, "Unable to allocate memory for blueSegments\n"); return;}
/* Can we connect with the server? */
if (!useroot)