-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw3D.c
1374 lines (1240 loc) · 40.8 KB
/
draw3D.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
/**********************************************************************/
/* FILE: draw3D.c
* DATE: 24 AUG 2000
* AUTH: G. E. Deschaines
* DESC: Routines to load, transform and draw polygon data describing
* objects in a 3D world space.
*
* NOTE: Cartesian coordinate frames for world space, field-of-view
* (FOV) viewport (size WxH pixels; aspect ratio (AR) of W/H),
* viewport clipping frustum (pyramid) and drawable pixmap
* are depicted in the following pictograms.
*
* +X +x
* [0,0,0] / [0,0,0] / [-W*AR/2,+W*AR/2]
* | / | / |
* \--> + ----- +Y \--> + ----- +y <-/
* | |
* | | [-H/2,+H/2]
* +Z +z <-----/
*
* World Space FOV Viewport
*
*
* [-H/2,+H/2]
* /-> +y +z [0,0]
* | / + ----- +x [W]
* | / [-W/AR/2,+W/AR/2] |
* + ----- +x <-/ |
* [0,0,0] +y [H]
*
* Clipping Pyramid Drawable Pixmap
*
* The x coordinate of points in the FOV viewport are not
* normalized between the FOV frustum near and far planes
* as in OpenGL or other graphics libraries.
*
*/
/**********************************************************************/
#ifndef THREED_TYPES
#define THREED_TYPES
typedef short int Integer;
typedef long int Longint;
typedef unsigned long int Word;
typedef double Extended;
#endif
/* This function replaces the problematic lround() in libm for
* Cygwin 1.5.25 and Redhat Linux 7.1
*/
Longint lroundd(double x)
{
static int init = 1;
static double zero;
static double half;
static double one;
double ipart;
if ( init ) {
zero = 0.0;
half = 0.5;
one = 1.0;
init = 0;
}
if ( x == zero ) return (zero);
if ( x < zero ) {
if ( modf(fabs(x),&ipart) < half ) return (-ipart);
else return (-(ipart+one));
} else {
if ( modf(x,&ipart) < half ) return (ipart);
else return (ipart+one);
}
}
/* These functions are used instead of fmin() and fmax() since
* they are not included in libm for RedHat Linux 7.1
*/
double dmin(double x1, double x2)
{
if ( x2 < x1 ) return x2;
else return x1;
}
double dmax(double x1, double x2)
{
if ( x2 > x1 ) return x2;
else return x1;
}
Longint lmin(Longint x1, Longint x2)
{
if ( x2 < x1 ) return x2;
else return x1;
}
Longint lmax(Longint x1, Longint x2)
{
if ( x2 > x1 ) return x2;
else return x1;
}
#include "pquelib.c"
typedef struct Pnt_3D
{
Extended X;
Extended Y;
Extended Z;
} Pnt3D;
typedef struct Pol_Rec *PolPtr;
typedef struct Pol_Rec
{
PolPtr Nxt;
Pnt3D Pt0;
Pnt3D Pt1;
Pnt3D Pt2;
} PolRec;
typedef struct
{
Boolean Flg;
Longint Pri;
Integer Typ;
Pnt3D Cnt0;
Pnt3D Cnt1;
Word Pat;
PolPtr Ptr;
} Pol3D;
/* CONSTANTS */
Extended fZero = 0.0;
Extended fHalf = 0.5;
Extended fOne = 1.0;
Extended fTwo = 2.0;
Extended f1K = 1000.0;
Extended rpd = 0.01745329; /* Radian per degree */
Longint CpMsec = CLOCKS_PER_SEC/1000;
/* FOV INFORMATION */
Extended fova = 90.0; /* Field-of-View whole angle (deg) */
Extended fovs = 600.0; /* Field-of-View display size (pixels) */
Extended ratio;
Extended fovcx;
Extended fovcy;
Extended fl;
Extended flmin;
Extended zoom;
Extended zfovr;
Extended sfacx, sfacy, sfacz;
Extended sfacyAR; // sfacy scaled by viewport aspect ratio
Extended px, py, pz;
Extended x, y, z;
Extended p, t, r; // yaw (psi), pitch (theta), roll (rho or phi)
Integer xMax;
Integer yMax;
Pnt3D fovpt;
Pnt3D offset;
/* COORDINATE TRANSFORMATION INFORMATION */
Extended cosp, sinp;
Extended cost, sint;
Extended cosr, sinr;
Extended dcx1, dcy1, dcz1;
Extended dcx2, dcy2, dcz2;
Extended dcx3, dcy3, dcz3;
/* GROUND PLANE GRID INFORMATION */
Pnt3D GridPt1[4];
Pnt3D GridPt2[4];
/* POLYGON INFORMATION */
#define maxpol 1024 /* Maximum number of polygons */
#define maxpnt 16 /* Maximum number of points in loaded polygon */
Integer polcnt = 0;
Pol3D pollist[maxpol];
Pnt3D pntlist[maxpnt];
PQtype polPQ;
#define poltyp_gnd 0 /* ground polygon type */
#define poltyp_tgt 1 /* target polygon type */
#define poltyp_msl 2 /* missile polygon type */
/* TXYZ TRAJECTORY INFORMATION */
Extended tsec;
Integer ktot;
Extended XM, YM, ZM;
Extended XT, YT, ZT;
Extended PSM, THM, PHM;
Extended PST, THT, PHT;
char numstr[24];
/* DISPLAY INFORMATION */
Extended xAspect = 1.0;
Extended yAspect = 1.0;
Pixmap drawn;
Pixmap blank;
Word SolidPattern;
Word White = 0;
Word Black = 1;
Word Red = 2;
Word Green = 3;
Word Blue = 4;
Word Cyan = 5;
Word Yellow = 6;
Word Brown = 7;
Word Colors[] = {0, 1, 2, 3, 4, 5, 6, 7};
Longint cpumsec1;
Longint cpumsec2;
/* I/O BUFFERS */
char sbuff[512];
/*
* MAKES LINKED LIST OF POLYGON DATA FROM ARRAY OF POLYGON DATA
*/
void MakePol( Integer pntcnt,
Integer thePri,
Integer theTyp, Word thePat, Pnt3D offset )
{
PolPtr newPtr;
PolPtr oldPtr;
Extended sumP;
Extended sumX;
Extended sumY;
Extended sumZ;
Integer i;
/* Allocate memory for new polygon point record. */
newPtr = (PolPtr)malloc(sizeof(PolRec));
/* Increment polygon counter. */
polcnt = polcnt + 1;
#if DBG_LVL > 0
printf("MakePol: polcnt = %d\n", polcnt);
#endif
/* Initialize polygon list entry. */
pollist[polcnt].Flg = FALSE;
pollist[polcnt].Pri = thePri*100000000;
pollist[polcnt].Pat = thePat;
pollist[polcnt].Typ = theTyp;
pollist[polcnt].Ptr = newPtr;
/* Initialize first polygon point record. */
newPtr->Nxt = NULL;
newPtr->Pt0.X = pntlist[1].X + offset.X;
newPtr->Pt0.Y = pntlist[1].Y + offset.Y;
newPtr->Pt0.Z = pntlist[1].Z + offset.Z;
newPtr->Pt1.X = newPtr->Pt0.X;
newPtr->Pt1.Y = newPtr->Pt0.Y;
newPtr->Pt1.Z = newPtr->Pt0.Z;
newPtr->Pt2.X = fZero;
newPtr->Pt2.Y = fZero;
newPtr->Pt2.Z = fZero;
#if DBG_LVL > 2
printf("MakePol: vertice # %d = %f %f %f\n",1,newPtr->Pt0.X,
newPtr->Pt0.Y,
newPtr->Pt0.Z);
#endif
/* Initialize polygon centroid summation variables. */
sumP = fOne;
sumX = newPtr->Pt0.X;
sumY = newPtr->Pt0.Y;
sumZ = newPtr->Pt0.Z;
/* Initialize polygon point record for each additional point. */
i = 2;
do {
/*--- Save current polygon point record pointer. */
oldPtr = newPtr;
/*--- Allocate next polygon point record. */
newPtr = (PolPtr)malloc(sizeof(PolRec));
/*--- Link last polygon record to new record. */
oldPtr->Nxt = newPtr;
/*--- Initialize new polygon point record. */
newPtr->Nxt = NULL;
newPtr->Pt0.X = pntlist[i].X + offset.X;
newPtr->Pt0.Y = pntlist[i].Y + offset.Y;
newPtr->Pt0.Z = pntlist[i].Z + offset.Z;
newPtr->Pt1.X = newPtr->Pt0.X;
newPtr->Pt1.Y = newPtr->Pt0.Y;
newPtr->Pt1.Z = newPtr->Pt0.Z;
newPtr->Pt2.X = fZero;
newPtr->Pt2.Y = fZero;
newPtr->Pt2.Z = fZero;
#if DBG_LVL > 2
printf("MakePol: vertice # %d = %f %f %f\n",i,newPtr->Pt0.X,
newPtr->Pt0.Y,
newPtr->Pt0.Z);
#endif
/*--- Increment polygon centroid summation variables. */
sumP = sumP + fOne;
sumX = sumX + newPtr->Pt0.X;
sumY = sumY + newPtr->Pt0.Y;
sumZ = sumZ + newPtr->Pt0.Z;
/*--- Increment polygon point counter. */
i = i + 1;
} while ( i <= pntcnt );
/* Calculate polygon centroid. */
pollist[polcnt].Cnt0.X = sumX/sumP;
pollist[polcnt].Cnt0.Y = sumY/sumP;
pollist[polcnt].Cnt0.Z = sumZ/sumP;
pollist[polcnt].Cnt1.X = sumX/sumP;
pollist[polcnt].Cnt1.Y = sumY/sumP;
pollist[polcnt].Cnt1.Z = sumZ/sumP;
#if DBG_LVL > 2
printf("MakePol: centroid 0 = %f %f %f\n",pollist[polcnt].Cnt0.X,
pollist[polcnt].Cnt0.Y,
pollist[polcnt].Cnt0.Z);
printf("MakePol: centroid 1 = %f %f %f\n",pollist[polcnt].Cnt1.X,
pollist[polcnt].Cnt1.Y,
pollist[polcnt].Cnt1.Z);
#endif
}
/*
* COMPUTE WORLD SPACE TO VIEW SPACE TRANSFORMATION MATRIX
*/
void MakeMatrix ( Extended p, Extended t, Extended r )
{
// RHS yaw, pitch, roll as p, t, r respectively in radians.
Extended kctcp;
Extended kctsp;
Extended kstcp;
Extended kstsp;
Extended kcrsp;
Extended kcrcp;
Extended ksrsp;
Extended ksrcp;
Extended ksrct;
Extended kcrct;
Extended ksrstcp;
Extended ksrstsp;
Extended kcrstcp;
Extended kcrstsp;
cosp = cos(p);
sinp = sin(p);
cost = cos(t);
sint = sin(t);
cosr = cos(r);
sinr = sin(r);
kctcp = cost*cosp;
kctsp = cost*sinp;
kstcp = sint*cosp;
kstsp = sint*sinp;
kcrsp = cosr*sinp;
kcrcp = cosr*cosp;
ksrsp = sinr*sinp;
ksrcp = sinr*cosp;
ksrct = sinr*cost;
kcrct = cosr*cost;
ksrstcp = sinr*kstcp;
ksrstsp = sinr*kstsp;
kcrstcp = cosr*kstcp;
kcrstsp = cosr*kstsp;
dcx1 = kctcp;
dcy1 = kctsp;
dcz1 = -sint;
dcx2 = ksrstcp - kcrsp;
dcy2 = ksrstsp + kcrcp;
dcz2 = ksrct;
dcx3 = kcrstcp + ksrsp;
dcy3 = kcrstsp - ksrcp;
dcz3 = kcrct;
}
/*
* TRANSFORMS GRID WORLD SPACE COORDINATES TO VIEWPORT COORDINATES
*/
void XfrmGrid ()
{
Integer k;
Extended xd, yd, zd;
Extended xs, ys, zs;
GridPt1[0].X = 2000.0;
GridPt1[0].Y = -2000.0;
GridPt1[0].Z = 0.0;
GridPt1[1].X = 2000.0;
GridPt1[1].Y = 2000.0;
GridPt1[1].Z = 0.0;
GridPt1[2].X = -2000.0;
GridPt1[2].Y = 2000.0;
GridPt1[2].Z = 0.0;
GridPt1[3].X = -2000.0;
GridPt1[3].Y = -2000.0;
GridPt1[3].Z = 0.0;
for ( k = 0 ; k < 4 ; k++ )
{
/*--- TRANSLATE COORDINATES INTO VIEWPORT FOV REFERENCE FRAME */
xd = GridPt1[k].X - fovpt.X;
yd = GridPt1[k].Y - fovpt.Y;
zd = GridPt1[k].Z - fovpt.Z;
/*--- ROTATE COORDINATES INTO VIEWPORT REFERENCE FRAME AND SCALE */
xs = dcx1*xd + dcy1*yd + dcz1*zd;
ys = dcx2*xd + dcy2*yd + dcz2*zd;
zs = dcx3*xd + dcy3*yd + dcz3*zd;
xs = xs;
ys = ys*sfacyAR; // account for square clipping frustum base of fovs pixels
zs = zs*sfacz;
/*--- SAVE SCALED VIEWPORT COORDINATES */
GridPt2[k].X = xs;
GridPt2[k].Y = ys;
GridPt2[k].Z = zs;
}
}
/*
* TRANSFORMS POLYGON WORLD SPACE COORDINATES TO VIEWPORT COORDINATES
*/
void XfrmPoly ( Integer iPol )
{
PolPtr aPolRec;
Extended xd, yd, zd;
Extended xs, ys, zs;
Extended rs;
Extended rsmm;
Longint irsmm;
Longint pcode;
HeapElement anElement;
Boolean inflag;
#if DBG_LVL > 3
printf(" Polygon # %d\n",iPol);
#endif
inflag = FALSE;
pcode = pollist[iPol].Pri;
aPolRec = pollist[iPol].Ptr;
while ( aPolRec != NULL )
{
#if DBG_LVL > 4
printf(" - 1 vertice : %f %f %f\n",aPolRec->Pt1.X,
aPolRec->Pt1.Y,
aPolRec->Pt1.Z);
#endif
/*--- TRANSLATE COORDINATES INTO VIEWPORT FOV REFERENCE FRAME */
xd = aPolRec->Pt1.X - fovpt.X;
yd = aPolRec->Pt1.Y - fovpt.Y;
zd = aPolRec->Pt1.Z - fovpt.Z;
/*--- ROTATE COORDINATES INTO VIEWPORT REFERENCE FRAME AND SCALE */
xs = dcx1*xd + dcy1*yd + dcz1*zd;
ys = dcx2*xd + dcy2*yd + dcz2*zd;
zs = dcx3*xd + dcy3*yd + dcz3*zd;
xs = xs;
ys = ys*sfacyAR; // account for square clipping frustum base of fovs pixels
zs = zs*sfacz;
/*--- SAVE SCALED VIEWPORT COORDINATES */
aPolRec->Pt2.X = xs;
aPolRec->Pt2.Y = ys;
aPolRec->Pt2.Z = zs;
#if DBG_LVL > 4
printf(" - 2 vertice : %f %f %f\n",aPolRec->Pt2.X,
aPolRec->Pt2.Y,
aPolRec->Pt2.Z);
#endif
aPolRec = aPolRec->Nxt;
/*--- CHECK IF POINT IS IN FRONT OF VIEW POINT */
if ( xs >= fZero )
{
inflag = TRUE;
}
}
if ( ( inflag ) && ( ! FullPQ(polPQ) ) )
{
xd = pollist[iPol].Cnt1.X - fovpt.X;
yd = pollist[iPol].Cnt1.Y - fovpt.Y;
zd = pollist[iPol].Cnt1.Z - fovpt.Z;
xs = dcx1*xd + dcy1*yd + dcz1*zd;
ys = dcx2*xd + dcy2*yd + dcz2*zd;
zs = dcx3*xd + dcy3*yd + dcz3*zd;
rs = sqrt(xs*xs + ys*ys + zs*zs);
rsmm = rs*f1K;
irsmm = lroundd(rsmm);
anElement.Key = pcode + irsmm;
anElement.Info = iPol;
#if DBG_LVL > 3
printf(" - element: %ld %hd %hd %ld %f %f %f %f %ld\n",
anElement.Key,
anElement.Info,
pollist[iPol].Typ,
pollist[iPol].Pri,
xs, ys, zs, rsmm, irsmm);
#endif
PriorityEnq(&polPQ,anElement);
if ( irsmm < 0L ) {
printf("*** XfrmPoly: lroundd(rsmm) < 0 for iPol=%hd\n",iPol);
quitflag = TRUE;
}
}
pollist[iPol].Flg = inflag;
}
/*
* MOVES POLYGONS IN WORLD SPACE
*/
void MovePoly ( Integer iPol, Extended px, Extended py, Extended pz )
{
PolPtr aPolRec;
Extended xb, yb, zb;
xb = pollist[iPol].Cnt0.X;
yb = pollist[iPol].Cnt0.Y;
zb = pollist[iPol].Cnt0.Z;
pollist[iPol].Cnt1.X = dcx1*xb + dcx2*yb + dcx3*zb + px;
pollist[iPol].Cnt1.Y = dcy1*xb + dcy2*yb + dcy3*zb + py;
pollist[iPol].Cnt1.Z = dcz1*xb + dcz2*yb + dcz3*zb + pz;
#if DBG_LVL > 3
printf(" Polygon # %d\n",iPol);
printf(" - centroid : %f %f %f\n",pollist[iPol].Cnt1.X,
pollist[iPol].Cnt1.Y,
pollist[iPol].Cnt1.Z);
#endif
aPolRec = pollist[iPol].Ptr;
while ( aPolRec != NULL )
{
xb = aPolRec->Pt0.X;
yb = aPolRec->Pt0.Y;
zb = aPolRec->Pt0.Z;
aPolRec->Pt1.X = dcx1*xb + dcx2*yb + dcx3*zb + px;
aPolRec->Pt1.Y = dcy1*xb + dcy2*yb + dcy3*zb + py;
aPolRec->Pt1.Z = dcz1*xb + dcz2*yb + dcz3*zb + pz;
#if DBG_LVL > 3
printf(" - vertice : %f %f %f\n",aPolRec->Pt1.X,
aPolRec->Pt1.Y,
aPolRec->Pt1.Z);
#endif
aPolRec = aPolRec->Nxt;
}
}
#include "cliplib.c"
/*
* DRAWS GRID LINES CLIPPED TO 3D VIEWING PYRAMID
*/
void DrawGrid3D( Integer iaxis, Display *display, Pixmap drawable )
{
Integer k;
Integer i10,i11,i20,i21;
Extended xd1, yd1, zd1;
Extended xd2, yd2, zd2;
Integer pcnt;
Integer vcnt[8];
Pnt3D vlist[8][mxvcnt];
Extended xs, ys, zs, sf;
XPoint tempLine[mxvcnt];
Integer i;
#if DBG_LVL > 4
printf("DrawGrid3D: Drawing grid axis %d...\n",iaxis);
#endif
/* SET LINE DRAWING COLOR */
XSetForeground(display,the_GC,pixels[White]);
if ( iaxis == 1 )
{
/*--- DRAW GRID LINES PARALLEL TO WORLD X-AXIS */
i10 = 3;
i11 = 2;
i20 = 0;
i21 = 1;
}
else
{
/*--- DRAW GRID LINES PARALLEL TO WORLD Y-AXIS */
i10 = 3;
i11 = 0;
i20 = 2;
i21 = 1;
}
/* CALCULATE INCREMENTAL DISTANCES */
xd1 = 0.025*(GridPt2[i11].X - GridPt2[i10].X);
yd1 = 0.025*(GridPt2[i11].Y - GridPt2[i10].Y);
zd1 = 0.025*(GridPt2[i11].Z - GridPt2[i10].Z);
xd2 = 0.025*(GridPt2[i21].X - GridPt2[i20].X);
yd2 = 0.025*(GridPt2[i21].Y - GridPt2[i20].Y);
zd2 = 0.025*(GridPt2[i21].Z - GridPt2[i20].Z);
for ( k = 0 ; k < 41 ; k++ )
{
pcnt = 1;
/*--- CALCULATE X COORDINATE OF UN-CLIPPED LINE */
vlist[pcnt][1].X = GridPt2[i10].X + k*xd1;
vlist[pcnt][2].X = GridPt2[i20].X + k*xd2;
if ( ! ((vlist[pcnt][1].X <= flmin) && (vlist[pcnt][2].X <= flmin)) )
{
/*------ CREATE UN-CLIPPED LINE */
vlist[pcnt][1].Y = GridPt2[i10].Y + k*yd1;
vlist[pcnt][1].Z = GridPt2[i10].Z + k*zd1;
vlist[pcnt][2].Y = GridPt2[i20].Y + k*yd2;
vlist[pcnt][2].Z = GridPt2[i20].Z + k*zd2;
vlist[pcnt][3] = vlist[pcnt][1];
vcnt[pcnt] = 3;
#if DBG_LVL > 4
for (i=1; i<= vcnt[pcnt]; i++)
printf("DrawGrid3D: %d %d %f %f %f\n",pcnt,i,
vlist[pcnt][i].X,vlist[pcnt][i].Y,vlist[pcnt][i].Z);
#endif
/*------ CREATE CLIPPED LINE */
PolyClip( (Integer *)&pcnt, vcnt, vlist );
#if DBG_LVL > 4
for (i=1; i<= vcnt[pcnt]; i++)
printf("DrawGrid3D: %d %d %f %f %f\n",pcnt,i,
vlist[pcnt][i].X,vlist[pcnt][i].Y,vlist[pcnt][i].Z);
#endif
/*------ DRAW CLIPPED LINE */
if ( vcnt[pcnt] > 2 )
{
for ( i = 1 ; i < 3 ; i++ )
{
xs = vlist[pcnt][i].X;
ys = vlist[pcnt][i].Y/sfacyAR;
zs = vlist[pcnt][i].Z/sfacz;
sf = fl/xs;
tempLine[i-1].x = lroundd(sf*ys) + floor(fovcx);
tempLine[i-1].y = lroundd(sf*zs) + floor(fovcy);
}
XDrawLines(display, drawable, the_GC, tempLine, 2, CoordModeOrigin);
}
}
}
}
/*
* DRAWS POLYGON CLIPPED TO 3D VIEWING PYRAMID
*/
void DrawPoly3D( Integer iPol, Display *display, Pixmap drawable )
{
PolPtr aPolRec;
Integer pcnt, icnt;
Integer vcnt[8];
Pnt3D vlist[8][mxvcnt];
Extended xs, ys, zs, sf;
XPoint tempPoly[mxvcnt];
Integer i;
/* GET UN-CLIPPED POLYGON */
#if DBG_LVL > 4
printf("DrawPoly3D: Drawing polygon %d...\n",iPol);
#endif
aPolRec = pollist[iPol].Ptr;
pcnt = 1;
icnt = 0;
while ( aPolRec != NULL )
{
icnt = icnt + 1;
vlist[pcnt][icnt] = aPolRec->Pt2;
aPolRec = aPolRec->Nxt;
}
icnt = icnt + 1;
vlist[pcnt][icnt] = vlist[pcnt][1];
vcnt[pcnt] = icnt;
#if DBG_LVL > 4
for (i=1; i<= vcnt[pcnt]; i++)
printf("DrawPoly3D: %d %d %f %f %f\n",pcnt,i,
vlist[pcnt][i].X,vlist[pcnt][i].Y,vlist[pcnt][i].Z);
#endif
/* CREATE CLIPPED POLYGON */
PolyClip( (Integer *)&pcnt, vcnt, vlist );
#if DBG_LVL > 4
for (i=1; i<= vcnt[pcnt]; i++)
printf("DrawPoly3D: %d %d %f %f %f\n",pcnt,i,
vlist[pcnt][i].X,vlist[pcnt][i].Y,vlist[pcnt][i].Z);
#endif
/* DRAW CLIPPED POLYGON */
if ( vcnt[pcnt] > 3 )
{
for ( i = 1 ; i <= vcnt[pcnt] ; i++ )
{
xs = vlist[pcnt][i].X;
ys = vlist[pcnt][i].Y/sfacyAR;
zs = vlist[pcnt][i].Z/sfacz;
sf = fl/xs;
tempPoly[i-1].x = lroundd(sf*ys) + floor(fovcx);
tempPoly[i-1].y = lroundd(sf*zs) + floor(fovcy);
}
if ( pollist[iPol].Typ >= 0 )
{
XSetForeground(display,the_GC,pixels[pollist[iPol].Pat]);
XFillPolygon(display, drawable, the_GC,
tempPoly, vcnt[pcnt], Convex, CoordModeOrigin);
}
else
{
XSetForeground(display,the_GC,pixels[pollist[iPol].Pat]);
XSetLineAttributes(display,the_GC,2,LineSolid,CapButt,JoinMiter);
XDrawLines(display, drawable, the_GC,
tempPoly, vcnt[pcnt], CoordModeOrigin);
XSetLineAttributes(display,the_GC,1,LineSolid,CapButt,JoinMiter);
}
}
}
void LoadPoly ( FILE *lfni, const char* polyfile)
{
Integer i,k;
Integer polpnt, polpri, polcol, poltyp;
Extended polsfc;
Extended x, y, z;
Extended mdloffx, mdloffy, mdloffz, mdlsfc;
char* sptr;
char mdlnam[60];
char polnam[60];
static char fmt0[]="%lf %lf %lf %lf %s\n";
static char fmt1[]="%hd %hd %hd %hd %lf %s\n";
static char fmt2[]="%lf %lf %lf\n";
/* Read shape model offsets, scaling factor and name record. */
sptr = fgets(sbuff,132,lfni);
if ( sptr == NULL ) {
printf("LoadPoly: fgets error for 1st record in polyfile %s.\n",polyfile);
return;
}
k = sscanf(sbuff,fmt0,&mdloffx,&mdloffy,&mdloffz,&mdlsfc,mdlnam);
if ( k != 5 ) {
printf("LoadPoly: sscanf error for 1st record in polyfile %s.\n",polyfile);
return;
}
do {
/*+++ Load polygon specification record. */
sptr = fgets(sbuff,132,lfni);
k = sscanf(sbuff,fmt1,&polpnt,&polpri,&polcol,&poltyp,&polsfc,polnam);
if ( k == 6 )
{
#if DBG_LVL > 1
printf("LoadPoly: Loaded specs - %d %d %d %d %f %s\n",
polpnt,polpri,polcol,poltyp,polsfc,polnam);
#endif
/*++++++ Load vertex points. */
for ( i = 1 ; i <= polpnt ; i++ )
{
fgets(sbuff,132,lfni);
sscanf(sbuff,fmt2,&x,&y,&z);
#if DBG_LVL > 1
printf("LoadPoly: Loaded vertex - %f %f %f\n",x,y,z);
#endif
pntlist[i].X = x*polsfc*mdlsfc;
pntlist[i].Y = y*polsfc*mdlsfc;
pntlist[i].Z = z*polsfc*mdlsfc;
#if DBG_LVL > 1
printf("LoadPoly: Scaled vertex - %f %f %f\n",pntlist[i].X,
pntlist[i].Y,
pntlist[i].Z);
#endif
}
/*++++++ Load offset point. */
fgets(sbuff,132,lfni);
sscanf(sbuff,fmt2,&x,&y,&z);
#if DBG_LVL > 1
printf("LoadPoly: Loaded offset - %f %f %f\n",x,y,z);
#endif
offset.X = x*polsfc*mdlsfc + mdloffx;
offset.Y = y*polsfc*mdlsfc + mdloffy;
offset.Z = z*polsfc*mdlsfc + mdloffz;
#if DBG_LVL > 1
printf("LoadPoly: Scaled offset - %f %f %f\n",offset.X,
offset.Y,
offset.Z);
#endif
if ( (polcol >= 0) && (polcol< 8) ) {
MakePol(polpnt,polpri,poltyp,Colors[polcol],offset);
} else {
MakePol(polpnt,polpri,poltyp,Black,offset);
}
}
} while ( ! ( feof(lfni) || (polcnt == maxpol) ) );
}
void draw3D (Widget w, Display *display, Window drawable)
{
Arg args[10];
Dimension width, height;
Extended tanfv;
Extended true_tsec = 0.0;
Extended last_tsec = -1.0/img_FPS;
Extended DXTM, DYTM, DZTM, RTM, UXTM, UYTM, UZTM;
Integer n = 0;
Integer i, k, itot, ipad;
HeapElement anElement;
XEvent event;
XColor screen_def, exact_def;
Colormap cmap = XDefaultColormapOfScreen(XtScreen(w));
FILE *lfni;
FILE *lfnt;
Boolean paused = FALSE;
Boolean align_fov_toward_tgt = FALSE;
Boolean align_fov_toward_msl = FALSE;
Boolean align_fov_along_head = TRUE;
Longint waitmsec = 0;
Integer img_count= 0;
Extended img_dtsec= 1.0/img_FPS;
char img_fname[24];
/* GET PIXEL COLORS */
if ( XAllocNamedColor(display,cmap,"white",&exact_def,&screen_def) != 0 )
{
pixels[White] = screen_def.pixel;
}
if ( XAllocNamedColor(display,cmap,"black",&exact_def,&screen_def) != 0 )
{
pixels[Black] = screen_def.pixel;
}
if ( XAllocNamedColor(display,cmap,"red",&exact_def,&screen_def) != 0 )
{
pixels[Red] = screen_def.pixel;
}
if ( XAllocNamedColor(display,cmap,"green",&exact_def,&screen_def) != 0 )
{
pixels[Green] = screen_def.pixel;
}
if ( XAllocNamedColor(display,cmap,"blue",&exact_def,&screen_def) != 0 )
{
pixels[Blue] = screen_def.pixel;
}
if ( XAllocNamedColor(display,cmap,"cyan",&exact_def,&screen_def) != 0 )
{
pixels[Cyan] = screen_def.pixel;
}
if ( XAllocNamedColor(display,cmap,"yellow",&exact_def,&screen_def) != 0 )
{
pixels[Yellow] = screen_def.pixel;
}
if ( XAllocNamedColor(display,cmap,"brown",&exact_def,&screen_def) != 0 )
{
pixels[Brown] = screen_def.pixel;
}
#if DBG_LVL > 0
printf("White = %lu\n",pixels[White]);
printf("Black = %lu\n",pixels[Black]);
printf("Red = %lu\n",pixels[Red]);
printf("Green = %lu\n",pixels[Green]);
printf("Blue = %lu\n",pixels[Blue]);
printf("Cyan = %lu\n",pixels[Cyan]);
printf("Yellow = %lu\n",pixels[Yellow]);
printf("Brown = %lu\n",pixels[Brown]);
#endif
/* INITIALIZE VIEWPORT */
XtSetArg(args[n], XtNwidth, &width ); n++;
XtSetArg(args[n], XtNheight, &height); n++;
XtGetValues(w, args, n);
xAspect = width;
yAspect = height;
ratio = (xAspect*1.0)/(yAspect*1.0);
xMax = lroundd(fovs*ratio);
yMax = lroundd(fovs);
fovcx = xMax/2.0;
fovcy = yMax/2.0;
zoom = 1.0;
#if DBG_LVL > 0
printf("draw3D: xAspect,yAspect,ratio = %f %f %f\n",xAspect,yAspect,ratio);
printf("draw3D: xMax,yMax = %d %d\n",xMax,yMax);
printf("draw3D: fovcx,fovcy = %f %f\n",fovcx,fovcy);
#endif
/* DEFINE DRAWING ATTRIBUTES AND CLEAR DISPLAY */
XSetLineAttributes(display,the_GC,1,LineSolid,CapButt,JoinMiter);
XSetFillRule(display,the_GC,WindingRule);
XSetForeground(display,the_GC,pixels[White]);
XSetBackground(display,the_GC,pixels[Black]);
XClearArea(display, drawable, 0, 0, xMax, yMax, TRUE);
/* FRAME VIEWPORT */
/* Fills viewport with black
XSetForeground(display,the_GC,pixels[Black]);
XFillRectangle(display,drawable,the_GC,0,0,xMax,yMax);
XSetForeground(display,the_GC,pixels[White]);
*/
XDrawRectangle(display,drawable,the_GC,0,0,xMax-1,yMax-1);
XFlush(display);
/* CREATE AND INITIALIZE DRAWN PIXMAP */
drawn = XCreatePixmap(display,drawable,xMax,yMax,
DefaultDepthOfScreen(XtScreen(w)));
XCopyArea(display,drawable,drawn,the_GC,0,0,xMax,yMax,0,0);
/* CREATE AND INITIALIZE BLANK PIXMAP */
blank = XCreatePixmap(display,drawable,xMax,yMax,
DefaultDepthOfScreen(XtScreen(w)));
XCopyArea(display,drawable,blank,the_GC,0,0,xMax,yMax,0,0);
/* COMPUTE VIEWPORT FOV FOCAL LENGTHS */
tanfv = sin((fova/fTwo)*rpd)/cos((fova/fTwo)*rpd);
fl = (fovs/fTwo)/tanfv;
flmin = 0.1*fl;
sfacx = fOne; // Not used
sfacy = (fOne/tanfv);
sfacyAR = sfacy/ratio;
sfacz = (fOne/tanfv);
#if DBG_LVL > 0
printf("draw3D: tanfv,fl = %f %f\n",tanfv,fl);
printf("draw3D: sfacx,sfacy,sfacyAR,sfacz = %f %f %f %f\n",sfacx,sfacy,sfacyAR,sfacz);
#endif
/* READ AND MAKE OBJECT POLYGONS */
polcnt = 0;
lfni = fopen("./dat/grndpoly.dat","r");
if ( lfni )
{
#if DBG_LVL > 0
printf("draw3D: Loading polygons from file %s\n","grndpoly.dat");
#endif
LoadPoly(lfni,"./dat/grndpoly.dat");
fclose(lfni);
}
/*
lfni = fopen("./dat/grndpgrid.dat","r");
if ( lfni )
{
#if DBG_LVL > 0
printf("draw3D: Loading polygons from file %s\n","grndgrid.dat");
#endif
LoadPoly(lfni,"./dat/grndpgrid.dat");
fclose(lfni);
}
*/
lfni = fopen("./dat/fwngpoly.dat","r");
if ( lfni )
{
#if DBG_LVL > 0
printf("draw3D: Loading polygons from file %s\n","fwngpoly.dat");
#endif
LoadPoly(lfni,"./dat/fwngpoly.dat");
fclose(lfni);
}
lfni = fopen("./dat/mislpoly.dat","r");
if ( lfni )
{
#if DBG_LVL > 0
printf("draw3D: Loading polygons from file %s\n","mislpoly.dat");
#endif
LoadPoly(lfni,"./dat/mislpoly.dat");
fclose(lfni);
}
/*
lfni = fopen("./dat/mazepoly.dat","r");
if ( lfni )
{
#if DBG_LVL > 0
printf("draw3D: Loading polygons from file %s\n","mazepoly.dat");
#endif
LoadPoly(lfni,"./dat/mazepoly.dat");
fclose(lfni);
}
*/