-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibply.c
1750 lines (1602 loc) · 51.7 KB
/
libply.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
/*
* libply.c - a library of polygonized object output routines.
*
* Author: Alexander Enzmann
*
* Modified: 1 November 1994
* Alexander R. Enzmann
* Changes necessary for transformations
* Fixed vertex ordering in lib_output_cylcone
*/
/*-----------------------------------------------------------------*/
/* include section */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "lib.h"
#include "drv.h"
/*-----------------------------------------------------------------*/
/* defines/constants section */
#define VERT(i, a) ((verts[gPoly_vbuffer[i]])[a])
/*-----------------------------------------------------------------*/
/* Polygon stack for making PLG files */
object_ptr gPolygon_stack = NULL;
/* Keep track of how many vertices/faces have been emitted */
unsigned long gVertex_count = 0; /* Vertex coordinates */
unsigned long gNormal_count = 0; /* Vertex normals */
unsigned long gFace_count = 0;
/* Storage for polygon indices */
unsigned int *gPoly_vbuffer = NULL;
int *gPoly_end = NULL;
/* Globals to determine which axes can be used to split the polygon */
int gPoly_Axis1 = 0;
int gPoly_Axis2 = 1;
/*-----------------------------------------------------------------*/
#ifdef ANSI_FN_DEF
void lib_output_polygon_cylcone (COORD4 base_pt, COORD4 apex_pt)
#else
void lib_output_polygon_cylcone(base_pt, apex_pt)
COORD4 base_pt, apex_pt;
#endif
{
double angle, delta_angle, height, divisor, ba;
COORD3 axis, dir, norm_axis, start_dir, start_norm;
COORD3 norm[4], vert[4], start_radius[4];
MATRIX nmx, mx;
int i;
SUB3_COORD3(axis, apex_pt, base_pt);
COPY_COORD3(norm_axis, axis);
height = lib_normalize_vector(norm_axis);
SET_COORD3(dir, 0.0, 0.0, 1.0);
CROSS(start_dir, axis, dir);
divisor = lib_normalize_vector(start_dir);
if (ABSOLUTE(divisor) < EPSILON2) {
SET_COORD3(dir, 1.0, 0.0, 0.0);
CROSS(start_dir, axis, dir);
lib_normalize_vector(start_dir);
}
start_radius[0][X] = start_dir[X] * base_pt[W];
start_radius[0][Y] = start_dir[Y] * base_pt[W];
start_radius[0][Z] = start_dir[Z] * base_pt[W];
ADD3_COORD3(vert[2], base_pt, start_radius[0]);
start_radius[1][X] = start_dir[X] * apex_pt[W];
start_radius[1][Y] = start_dir[Y] * apex_pt[W];
start_radius[1][Z] = start_dir[Z] * apex_pt[W];
ADD3_COORD3(vert[1], apex_pt, start_radius[1]);
if ( base_pt[W] == apex_pt[W] ) {
/* it's a cylinder, so simply copy dir to norm */
COPY_COORD3( start_norm, start_dir ) ;
} else {
/* it's a cone, so compute true normal here */
ba = base_pt[W] - apex_pt[W] ;
start_norm[X] = start_dir[X] * height + norm_axis[X] * ba;
start_norm[Y] = start_dir[Y] * height + norm_axis[Y] * ba;
start_norm[Z] = start_dir[Z] * height + norm_axis[Z] * ba;
lib_normalize_vector(start_norm);
}
COPY_COORD3(norm[2], start_norm);
COPY_COORD3(norm[1], start_norm);
delta_angle = 2.0 * PI / (double)(4*gU_resolution);
for (i=1,angle=delta_angle;i<=4*gU_resolution;++i,angle+=delta_angle) {
lib_create_axis_rotate_matrix(mx, norm_axis, angle);
lib_invert_matrix(nmx, mx);
lib_transform_point(vert[0], start_radius[1], mx);
ADD2_COORD3(vert[0], apex_pt);
lib_transform_normal(norm[0], start_norm, nmx);
lib_output_polypatch(3, vert, norm);
COPY_COORD3(vert[1], vert[0]);
COPY_COORD3(norm[1], norm[0]);
lib_transform_point(vert[0], start_radius[0], mx);
ADD2_COORD3(vert[0], base_pt);
lib_output_polypatch(3, vert, norm);
COPY_COORD3(vert[2], vert[0]);
COPY_COORD3(norm[2], norm[0]);
PLATFORM_MULTITASK();
}
}
/*-----------------------------------------------------------------*/
#ifdef ANSI_FN_DEF
static void disc_evaluator(MATRIX trans, double theta, double v, double r, COORD3 vert)
#else
static void disc_evaluator(trans, theta, v, r, vert)
MATRIX trans;
double theta, v, r;
COORD3 vert;
#endif
{
COORD3 tvert;
/* Compute the position of the point */
SET_COORD3(tvert, (r + v) * cos(theta), (r + v) * sin(theta), 0.0);
lib_transform_point(vert, tvert, trans);
}
/*-----------------------------------------------------------------*/
#ifdef ANSI_FN_DEF
void lib_output_polygon_disc (COORD3 center, COORD3 normal,
double iradius, double oradius)
#else
void lib_output_polygon_disc(center, normal, iradius, oradius)
COORD3 center, normal;
double iradius, oradius;
#endif
{
double u, v, delta_u, delta_v;
MATRIX mx, imx;
int i;
COORD3 norm, vert[4];
COPY_COORD3(norm, normal);
if ( lib_normalize_vector(norm) < EPSILON2) {
fprintf(stderr, "Bad disc normal\n");
exit(1);
}
lib_create_canonical_matrix(mx, imx, center, norm);
delta_u = 2.0 * PI / (double)(4 * gU_resolution);
/* Dump out polygons */
for (i=0,u=0.0;i<4*gU_resolution;i++,u+=delta_u) {
PLATFORM_MULTITASK();
v = 0.0;
delta_v = oradius-iradius;
disc_evaluator(imx, u, v, iradius, vert[3]);
disc_evaluator(imx, u+delta_u, v, iradius, vert[2]);
disc_evaluator(imx, u+delta_u, v+delta_v, iradius, vert[1]);
disc_evaluator(imx, u, v+delta_v, iradius, vert[0]);
lib_output_polygon(4, vert);
}
}
/*-----------------------------------------------------------------*/
#ifdef ANSI_FN_DEF
void lib_output_polygon_sphere(COORD4 center_pt)
#else
void lib_output_polygon_sphere(center_pt)
COORD4 center_pt;
#endif
{
double angle;
COORD3 edge_norm[3], edge_pt[3];
long num_face, num_edge, num_tri, num_vert;
COORD3 *x_axis, *y_axis, **pt;
COORD3 mid_axis;
MATRIX rot_mx;
long u_pol, v_pol;
/* Allocate storage for the polygon vertices */
x_axis = (COORD3 *)malloc((gU_resolution+1) * sizeof(COORD3));
y_axis = (COORD3 *)malloc((gV_resolution+1) * sizeof(COORD3));
pt = (COORD3 **)malloc((gU_resolution+1) * sizeof(COORD3 *));
if (x_axis == NULL || y_axis == NULL || pt == NULL) {
fprintf(stderr, "Failed to allocate polygon data\n");
exit(1);
}
for (num_edge=0;num_edge<gU_resolution+1;num_edge++) {
pt[num_edge] = (COORD3 *)malloc((gV_resolution+1) * sizeof(COORD3));
if (pt[num_edge] == NULL) {
fprintf(stderr, "Failed to allocate polygon data\n");
exit(1);
}
}
/* calculate axes used to find grid points */
for (num_edge=0;num_edge<=gU_resolution;++num_edge) {
angle = (PI/4.0) * (2.0*(double)num_edge/gU_resolution - 1.0);
mid_axis[X] = 1.0; mid_axis[Y] = 0.0; mid_axis[Z] = 0.0;
lib_create_rotate_matrix(rot_mx, Y_AXIS, angle);
lib_transform_vector(x_axis[num_edge], mid_axis, rot_mx);
}
for (num_edge=0;num_edge<=gV_resolution;++num_edge) {
angle = (PI/4.0) * (2.0*(double)num_edge/gV_resolution - 1.0);
mid_axis[X] = 0.0; mid_axis[Y] = 1.0; mid_axis[Z] = 0.0;
lib_create_rotate_matrix(rot_mx, X_AXIS, angle);
lib_transform_vector(y_axis[num_edge], mid_axis, rot_mx);
}
/* set up grid of points on +Z sphere surface */
for (u_pol=0;u_pol<=gU_resolution;++u_pol) {
for (v_pol=0;v_pol<=gU_resolution;++v_pol) {
CROSS(pt[u_pol][v_pol], x_axis[u_pol], y_axis[v_pol]);
lib_normalize_vector(pt[u_pol][v_pol]);
}
}
for (num_face=0;num_face<6;++num_face) {
/* transform points to cube face */
for (u_pol=0;u_pol<=gU_resolution;++u_pol) {
for (v_pol=0;v_pol<=gV_resolution;++v_pol) {
lib_rotate_cube_face(pt[u_pol][v_pol], Z_AXIS, num_face);
}
}
/* output grid */
for (u_pol=0;u_pol<gU_resolution;++u_pol) {
for (v_pol=0;v_pol<gV_resolution;++v_pol) {
PLATFORM_MULTITASK();
for (num_tri=0;num_tri<2;++num_tri) {
for (num_edge=0;num_edge<3;++num_edge) {
num_vert = (num_tri*2 + num_edge) % 4;
if (num_vert == 0) {
COPY_COORD3(edge_pt[num_edge], pt[u_pol][v_pol]);
} else if ( num_vert == 1 ) {
COPY_COORD3(edge_pt[num_edge], pt[u_pol][v_pol+1]);
} else if ( num_vert == 2 ) {
COPY_COORD3(edge_pt[num_edge],pt[u_pol+1][v_pol+1]);
} else {
COPY_COORD3(edge_pt[num_edge], pt[u_pol+1][v_pol]);
}
COPY_COORD3(edge_norm[num_edge], edge_pt[num_edge]);
edge_pt[num_edge][X] =
edge_pt[num_edge][X] * center_pt[W] +
center_pt[X];
edge_pt[num_edge][Y] =
edge_pt[num_edge][Y] * center_pt[W] +
center_pt[Y];
edge_pt[num_edge][Z] =
edge_pt[num_edge][Z] * center_pt[W] +
center_pt[Z];
}
lib_output_polypatch(3, edge_pt, edge_norm);
}
}
}
}
/* Release any memory used */
for (num_edge=0;num_edge<gU_resolution+1;num_edge++)
free(pt[num_edge]);
free(pt);
free(y_axis);
free(x_axis);
}
/*-----------------------------------------------------------------*/
#ifdef ANSI_FN_DEF
void lib_output_polygon_height (int height, int width, float **data,
double x0, double x1,
double y0, double y1,
double z0, double z1)
#else
void lib_output_polygon_height(height, width, data, x0, x1, y0, y1, z0, z1)
int height, width;
float **data;
double x0, x1, y0, y1, z0, z1;
#endif
{
int i, j;
double xdelta, zdelta;
COORD3 verts[3];
#if defined (applec)
#pragma unused (y1)
#endif /* applec */
xdelta = (x1 - x0) / (double)(width - 1);
zdelta = (z1 - z0) / (double)(height - 1);
for (i=0;i<height-1;i++) {
for (j=0;j<width-1;j++) {
PLATFORM_MULTITASK();
SET_COORD3(verts[0], x0 + j * xdelta, y0 + data[i][j],
z0 + i * zdelta);
SET_COORD3(verts[1], x0 + (j+1) * xdelta, y0 + data[i+1][j+1],
z0 + (i + 1) * zdelta);
SET_COORD3(verts[2], x0 + (j+1) * xdelta, y0 + data[i][j+1],
z0 + i * zdelta);
lib_output_polygon(3, verts);
COPY_COORD3(verts[2], verts[1]); /* copy corner from previous */
SET_COORD3(verts[1], x0 + j * xdelta, y0 + data[i+1][j],
z0 + (i + 1) * zdelta);
lib_output_polygon(3, verts);
}
}
}
/*-----------------------------------------------------------------*/
#ifdef ANSI_FN_DEF
static void torus_evaluator(MATRIX trans,
double theta, double phi, double r0, double r1,
COORD3 vert, COORD3 norm)
#else
static void torus_evaluator(trans, theta, phi, r0, r1, vert, norm)
MATRIX trans;
double theta, phi, r0, r1;
COORD3 vert, norm;
#endif
{
COORD3 v0, v1, tvert, tnorm;
/* Compute the position of the point */
SET_COORD3(tvert, (r0 + r1 * sin(theta)) * cos(phi),
(r0 + r1 * sin(theta)) * sin(phi),
r1 * cos(theta));
/* Compute the normal at that point */
SET_COORD3(v0, r1*cos(theta)*cos(phi),
r1*cos(theta)*sin(phi),
-r1*sin(theta));
SET_COORD3(v1,-(r0+r1*sin(theta))*sin(phi),
(r0+r1*sin(theta))*cos(phi),
0.0);
CROSS(tnorm, v0, v1);
lib_normalize_vector(tnorm);
lib_transform_point(vert, tvert, trans);
lib_transform_vector(norm, tnorm, trans);
}
/*-----------------------------------------------------------------*/
#ifdef ANSI_FN_DEF
void lib_output_polygon_torus (COORD3 center, COORD3 normal,
double iradius, double oradius)
#else
void lib_output_polygon_torus(center, normal, iradius, oradius)
COORD3 center, normal;
double iradius, oradius;
#endif
{
double u, v, delta_u, delta_v;
MATRIX mx, imx;
int i, j;
COORD3 vert[4], norm[4];
if ( lib_normalize_vector(normal) < EPSILON2) {
fprintf(stderr, "Bad torus normal\n");
exit(1);
}
lib_create_canonical_matrix(mx, imx, center, normal);
delta_u = 2.0 * PI / (double)(4*gU_resolution);
delta_v = 2.0 * PI / (double)(4*gV_resolution);
/* Dump out polygons */
for (i=0,u=0.0;i<(4*gU_resolution);i++,u+=delta_u) {
PLATFORM_MULTITASK();
for (j=0,v=0.0;j<(4*gV_resolution);j++,v+=delta_v) {
torus_evaluator(imx, u, v, iradius, oradius, vert[2], norm[2]);
torus_evaluator(imx, u, v+delta_v, iradius, oradius,
vert[1], norm[1]);
torus_evaluator(imx, u+delta_u, v+delta_v,
iradius, oradius, vert[0], norm[0]);
lib_output_polypatch(3, vert, norm);
COPY_COORD3(vert[1], vert[0]);
COPY_COORD3(norm[1], norm[0]);
torus_evaluator(imx, u+delta_u, v, iradius, oradius,
vert[0], norm[0]);
lib_output_polypatch(3, vert, norm);
}
}
}
/*-----------------------------------------------------------------*/
/* Generate a box as a set of 4-sided polygons */
#ifdef ANSI_FN_DEF
void lib_output_polygon_box (COORD3 p1, COORD3 p2)
#else
void lib_output_polygon_box(p1, p2)
COORD3 p1, p2;
#endif
{
COORD3 box_verts[4];
/* Sides */
SET_COORD3(box_verts[0], p1[X], p1[Y], p1[Z]);
SET_COORD3(box_verts[1], p1[X], p1[Y], p2[Z]);
SET_COORD3(box_verts[2], p1[X], p2[Y], p2[Z]);
SET_COORD3(box_verts[3], p1[X], p2[Y], p1[Z]);
lib_output_polygon(4, box_verts);
SET_COORD3(box_verts[0], p2[X], p1[Y], p2[Z]);
SET_COORD3(box_verts[1], p2[X], p1[Y], p1[Z]);
SET_COORD3(box_verts[2], p2[X], p2[Y], p1[Z]);
SET_COORD3(box_verts[3], p2[X], p2[Y], p2[Z]);
lib_output_polygon(4, box_verts);
/* Front/Back */
SET_COORD3(box_verts[0], p1[X], p1[Y], p1[Z]);
SET_COORD3(box_verts[3], p2[X], p1[Y], p1[Z]);
SET_COORD3(box_verts[2], p2[X], p2[Y], p1[Z]);
SET_COORD3(box_verts[1], p1[X], p2[Y], p1[Z]);
lib_output_polygon(4, box_verts);
SET_COORD3(box_verts[0], p2[X], p1[Y], p2[Z]);
SET_COORD3(box_verts[3], p1[X], p1[Y], p2[Z]);
SET_COORD3(box_verts[2], p1[X], p2[Y], p2[Z]);
SET_COORD3(box_verts[1], p2[X], p2[Y], p2[Z]);
lib_output_polygon(4, box_verts);
/* Top/Bottom */
SET_COORD3(box_verts[0], p1[X], p1[Y], p1[Z]);
SET_COORD3(box_verts[3], p1[X], p1[Y], p2[Z]);
SET_COORD3(box_verts[2], p2[X], p1[Y], p2[Z]);
SET_COORD3(box_verts[1], p2[X], p1[Y], p1[Z]);
lib_output_polygon(4, box_verts);
SET_COORD3(box_verts[0], p2[X], p2[Y], p1[Z]);
SET_COORD3(box_verts[3], p2[X], p2[Y], p2[Z]);
SET_COORD3(box_verts[2], p1[X], p2[Y], p2[Z]);
SET_COORD3(box_verts[1], p1[X], p2[Y], p1[Z]);
lib_output_polygon(4, box_verts);
}
/*-----------------------------------------------------------------*/
/* Given a polygon defined by vertices in verts, determine which of the
components of the vertex correspond to useful x and y coordinates - with
these we can pretend the polygon is 2D to do our work on it. */
#ifdef ANSI_FN_DEF
static void find_axes(COORD3 *verts)
#else
static void find_axes(verts)
COORD3 *verts;
#endif
{
double P1[3], P2[3], x, y, z;
P1[0] = VERT(1, 0) - VERT(0, 0);
P1[1] = VERT(1, 1) - VERT(0, 1);
P1[2] = VERT(1, 2) - VERT(0, 2);
P2[0] = VERT(2, 0) - VERT(0, 0);
P2[1] = VERT(2, 1) - VERT(0, 1);
P2[2] = VERT(2, 2) - VERT(0, 2);
/* Cross product - don't need to normalize cause we're only interested
in the size of the components */
x = fabs(P1[1] * P2[2] - P1[2] * P2[1]);
y = fabs(P1[2] * P2[0] - P1[0] * P2[2]);
z = fabs(P1[0] * P2[1] - P1[1] * P2[0]);
if (x > y && x > z) {
gPoly_Axis1 = 1;
gPoly_Axis2 = 2;
} else if (y > x && y > z) {
gPoly_Axis1 = 0;
gPoly_Axis2 = 2;
} else {
gPoly_Axis1 = 0;
gPoly_Axis2 = 1;
}
}
/*-----------------------------------------------------------------*/
/* Find the left most vertex in the polygon that has vertices m ... n. */
#ifdef ANSI_FN_DEF
static int leftmost_vertex(int m, int n, COORD3 *verts)
#else
static int leftmost_vertex(m, n, verts)
int m, n;
COORD3 *verts;
#endif
{
int l, i;
double x;
/* Assume the first vertex is the farthest to the left */
l = m;
x = VERT(m, gPoly_Axis1);
/* Now see if any of the others are farther to the left */
for (i=m+1;i<=n;i++) {
if (VERT(i, gPoly_Axis1) < x) {
l = i;
x = VERT(i, gPoly_Axis1);
}
}
return l;
}
/*-----------------------------------------------------------------*/
/* Given the leftmost vertex in a polygon, this routine finds another vertex
can be used to safely split the polygon. */
#ifdef ANSI_FN_DEF
static int split_vertex(int l, int la, int lb, int m, int n, COORD3 *verts)
#else
static int split_vertex(l, la, lb, m, n, verts)
int l, la, lb, m, n;
COORD3 *verts;
#endif
{
int t, k, lpu, lpl;
double yu, yl;
yu = MAX(VERT(l, gPoly_Axis2), MAX(VERT(la, gPoly_Axis2), VERT(lb, gPoly_Axis2)));
yl = MIN(VERT(l, gPoly_Axis2), MIN(VERT(la, gPoly_Axis2), VERT(lb, gPoly_Axis2)));
if (VERT(lb, gPoly_Axis2) > VERT(la, gPoly_Axis2)) {
lpu = lb;
lpl = la;
} else {
lpu = la;
lpl = lb;
}
t = (VERT(lb, gPoly_Axis1) > VERT(la, gPoly_Axis1) ? lb : la);
for (k=m;k<n;k++) {
if (k != la && k != l && k != lb) {
if (VERT(k, gPoly_Axis2) <= yu && VERT(k, gPoly_Axis2) >= yl) {
if (VERT(k, gPoly_Axis1) < VERT(t, gPoly_Axis1) &&
((VERT(k, gPoly_Axis2) - VERT(l, gPoly_Axis2)) *
(VERT(lpu, gPoly_Axis1) - VERT(l, gPoly_Axis1))) <=
((VERT(lpu, gPoly_Axis2) - VERT(l, gPoly_Axis2)) *
(VERT(k, gPoly_Axis1) - VERT(l, gPoly_Axis1)))) {
if (((VERT(k, gPoly_Axis2) - VERT(l, gPoly_Axis2)) *
(VERT(lpl, gPoly_Axis1) - VERT(l, gPoly_Axis1))) >=
((VERT(lpl, gPoly_Axis2) - VERT(l, gPoly_Axis2)) *
(VERT(k, gPoly_Axis1) - VERT(l, gPoly_Axis1)))) {
t = k;
}
}
}
}
}
return t;
}
/*-----------------------------------------------------------------*/
/* Test polygon vertices to see if they are linear */
#ifdef ANSI_FN_DEF
static int linear_vertices(int m, int n, COORD3 *verts)
#else
static int linear_vertices(m, n, verts)
int m, n;
COORD3 *verts;
#endif
{
#if defined (applec)
#pragma unused (m,n,verts)
#endif /* applec */
/* Not doing anything right now */
return 0;
}
/*-----------------------------------------------------------------*/
/* Shift vertex indices around to make two polygons out of one. */
#ifdef ANSI_FN_DEF
static void perform_split(int m, int m1, int n, int n1)
#else
static void perform_split(m, m1, n, n1)
int n, n1, m, m1;
#endif
{
int i, j, k;
k = n + 3 - m;
/* Move the new polygon up over the place the current one sits */
for (j=m1;j<=n1;j++) gPoly_vbuffer[j+k] = gPoly_vbuffer[j];
/* Move top part of remaining polygon */
for (j=n;j>=n1;j--) gPoly_vbuffer[j+2] = gPoly_vbuffer[j];
/* Move bottom part of remaining polygon */
k = n1 - m1 + 1;
for (j=m1;j>=m;j--) gPoly_vbuffer[j+k] = gPoly_vbuffer[j];
/* Copy the new polygon so that it sits before the remaining polygon */
i = n + 3 - m;
k = m - m1;
for (j=m1;j<=n1;j++) gPoly_vbuffer[j+k] = gPoly_vbuffer[j+i];
}
/*-----------------------------------------------------------------*/
/* Copy an indirectly referenced triangle into the output triangle buffer */
#ifdef ANSI_FN_DEF
static void add_new_triangle(int m, COORD3 *verts, COORD3 *norms,
int *out_cnt, COORD3 **out_verts, COORD3 **out_norms)
#else
static void add_new_triangle(m, verts, norms, out_cnt, out_verts, out_norms)
int m, *out_cnt;
COORD3 *verts, *norms, **out_verts, **out_norms;
#endif
{
if (out_verts != NULL) {
COPY_COORD3(out_verts[*out_cnt][0], verts[gPoly_vbuffer[m]]);
COPY_COORD3(out_verts[*out_cnt][1], verts[gPoly_vbuffer[m+1]]);
COPY_COORD3(out_verts[*out_cnt][2], verts[gPoly_vbuffer[m+2]]);
}
if (out_norms != NULL) {
COPY_COORD3(out_norms[*out_cnt][0], norms[gPoly_vbuffer[m]]);
COPY_COORD3(out_norms[*out_cnt][1], norms[gPoly_vbuffer[m+1]]);
COPY_COORD3(out_norms[*out_cnt][2], norms[gPoly_vbuffer[m+2]]);
}
*out_cnt += 1;
}
/*-----------------------------------------------------------------*/
#ifdef ANSI_FN_DEF
static void split_buffered_polygon(int cnt, COORD3 *verts, COORD3 *norms,
int *out_cnt, COORD3 **out_verts, COORD3 **out_norms)
#else
static void split_buffered_polygon(cnt, verts, norms, out_cnt, out_verts, out_norms)
int cnt, *out_cnt;
COORD3 *verts, *norms, **out_verts, **out_norms;
#endif
{
int i, m, m1, n, n1;
int l, la, lb, ls;
/* No triangles to start with */
*out_cnt = 0;
/* Initialize the polygon splitter */
gPoly_end[0] = -1;
gPoly_end[1] = cnt-1;
/* Split and push polygons until they turn into triangles */
for (i=1;i>0;) {
m = gPoly_end[i-1] + 1;
n = gPoly_end[i];
if (n - m == 2) {
if (!linear_vertices(m, n, verts)) {
add_new_triangle(m, verts, norms, out_cnt,
out_verts, out_norms);
}
i = i - 1;
} else {
l = leftmost_vertex(m, n, verts);
la = (l == n ? m : l + 1);
lb = (l == m ? n : l - 1);
ls = split_vertex(l, la, lb, m, n, verts);
if (ls == la || ls == lb) {
m1 = (la < lb ? la : lb);
n1 = (la > lb ? la : lb);
} else {
m1 = (l < ls ? l : ls);
n1 = (l > ls ? l : ls);
}
perform_split(m, m1, n, n1);
gPoly_end[i++] = m + n1 - m1;
gPoly_end[i] = n + 2;
}
}
}
/*-----------------------------------------------------------------*/
/*
* Split an arbitrary polygon into triangles.
*/
#ifdef ANSI_FN_DEF
static void split_polygon(int n, COORD3 *vert, COORD3 *norm)
#else
static void split_polygon(n, vert, norm)
int n;
COORD3 *vert, *norm;
#endif
{
COORD4 tvert[3], v0, v1;
COORD3 **out_verts, **out_norms;
MATRIX nmx, txmat;
int i, ii, j ;
int t, out_n;
object_ptr new_object;
/* Can't split a NULL vertex list */
if (vert == NULL) return;
if (gPoly_vbuffer == NULL) { /* [esp] Added error */
lib_storage_initialize();
/* [are] removed error, go and initialize if it hasn't been done. */
}
/* Allocate space to hold the intermediate polygon stacks */
out_verts = (COORD3 **)malloc((n - 2) * sizeof(COORD3 *));
if (norm != NULL)
out_norms = (COORD3 **)malloc((n - 2) * sizeof(COORD3 *));
else
out_norms = NULL;
for (i=0;i<n-2;i++) {
out_verts[i] = (COORD3 *)malloc(3 * sizeof(COORD3));
if (norm != NULL)
out_norms[i] = (COORD3 *)malloc(3 * sizeof(COORD3));
}
/* Start with a strict identity of vertices in verts and vertices in
the polygon buffer */
for (i=0;i<n;i++) gPoly_vbuffer[i] = i;
/* Make sure we know which axes to look at */
find_axes(vert);
out_n = 0;
split_buffered_polygon(n, vert, norm, &out_n, out_verts, out_norms);
if (lib_tx_active()) {
/* Perform transformations of the vertices and normals of
the polygon(s) */
lib_get_current_tx(txmat);
lib_invert_matrix(nmx, txmat);
for (t=0;t<out_n;t++)
for (i=0;i<3;i++) {
lib_transform_point(out_verts[t][i], out_verts[t][i], txmat);
if (out_norms != NULL)
lib_transform_normal(out_norms[t][i], out_norms[t][i], nmx);
}
}
/* Now output the triangles that we generated */
for (t=0;t<out_n;t++) {
PLATFORM_MULTITASK();
if (gRT_out_format == OUTPUT_DELAYED ||
gRT_out_format == OUTPUT_PLG) {
/* Save all the pertinent information */
new_object = (object_ptr)malloc(sizeof(struct object_struct));
if (new_object == NULL) return;
new_object->tx = NULL;
if (norm == NULL) {
new_object->object_type = POLYGON_OBJ;
new_object->object_data.polygon.tot_vert = 3;
new_object->object_data.polygon.vert =
(COORD3 *)malloc(3 * sizeof(COORD3));
if (new_object->object_data.polygon.vert == NULL) return;
} else {
new_object->object_type = POLYPATCH_OBJ;
new_object->object_data.polypatch.tot_vert = 3;
new_object->object_data.polypatch.vert =
(COORD3 *)malloc(3 * sizeof(COORD3));
if (new_object->object_data.polypatch.vert == NULL) return;
new_object->object_data.polypatch.norm =
(COORD3 *)malloc(3 * sizeof(COORD3));
if (new_object->object_data.polypatch.norm == NULL) return;
}
new_object->curve_format = OUTPUT_PATCHES;
new_object->surf_index = gTexture_count;
for (i=0;i<3;i++) {
if (norm == NULL) {
COPY_COORD3(new_object->object_data.polygon.vert[i],
out_verts[t][i]);
} else {
COPY_COORD3(new_object->object_data.polypatch.vert[i],
out_verts[t][i]);
COPY_COORD3(new_object->object_data.polypatch.norm[i],
out_norms[t][i]);
}
}
if (gRT_out_format == OUTPUT_PLG) {
/* We are currently in the process of turning objects
into a stack of polygons. Since we don't want to
put these polygons back onto the original stack of
objects, we put them into gPolygon_stack
*/
new_object->next_object = gPolygon_stack;
gPolygon_stack = new_object;
}
else {
new_object->next_object = gLib_objects;
gLib_objects = new_object;
}
} else {
switch (gRT_out_format) {
case OUTPUT_VIDEO:
/* First make sure the display has been opened for
* drawing
*/
if (!gView_init_flag) {
lib_create_view_matrix(gViewpoint.tx, gViewpoint.from, gViewpoint.at,
gViewpoint.up, gViewpoint.resx, gViewpoint.resy,
gViewpoint.angle, gViewpoint.aspect);
display_init(gViewpoint.resx, gViewpoint.resy, gBkgnd_color);
gView_init_flag = 1;
}
/* Step through each segment of the polygon, projecting it
onto the screen. */
for (i=0;i<3;i++) {
COPY_COORD3(tvert[0], out_verts[t][i]);
tvert[0][W] = 1.0;
lib_transform_coord(v0, tvert[0], gViewpoint.tx);
COPY_COORD3(tvert[1], out_verts[t][(i+1)%3]);
tvert[1][W] = 1.0;
lib_transform_coord(v1, tvert[1], gViewpoint.tx);
/* Do the perspective transform on the points */
v0[X] /= v0[W]; v0[Y] /= v0[W];
v1[X] /= v1[W]; v1[Y] /= v1[W];
if (lib_clip_to_box(v0, v1, gView_bounds))
display_line((int)v0[X], (int)v0[Y],
(int)v1[X], (int)v1[Y], gFgnd_color);
}
break;
case OUTPUT_NFF:
if (norm == NULL) {
fprintf(gOutfile, "p 3\n");
for (i=0;i<3;++i) {
fprintf(gOutfile, "%g %g %g\n",
out_verts[t][i][X], out_verts[t][i][Y],
out_verts[t][i][Z]);
}
} else {
fprintf(gOutfile, "pp 3\n");
for (i=0;i<3;++i) {
fprintf(gOutfile, "%g %g %g %g %g %g\n",
out_verts[t][i][X], out_verts[t][i][Y],
out_verts[t][i][Z], out_norms[t][i][X],
out_norms[t][i][Y], out_norms[t][i][Z]);
}
}
break;
case OUTPUT_POVRAY_10:
case OUTPUT_POVRAY_20:
case OUTPUT_POVRAY_30:
tab_indent();
fprintf(gOutfile, "object {\n");
tab_inc();
tab_indent();
if (norm == NULL)
fprintf(gOutfile, "triangle {\n");
else
fprintf(gOutfile, "smooth_triangle {\n");
tab_inc();
for (i=0;i<3;++i) {
tab_indent();
if (gRT_out_format == OUTPUT_POVRAY_10) {
fprintf(gOutfile, "<%g %g %g>",
out_verts[t][i][X],
out_verts[t][i][Y],
out_verts[t][i][Z]);
if (norm != NULL)
fprintf(gOutfile, " <%g %g %g>",
out_norms[t][i][X],
out_norms[t][i][Y],
out_norms[t][i][Z]);
} else {
fprintf(gOutfile, "<%g, %g, %g>",
out_verts[t][i][X],
out_verts[t][i][Y],
out_verts[t][i][Z]);
if (norm != NULL)
fprintf(gOutfile, " <%g, %g, %g>",
out_norms[t][i][X],
out_norms[t][i][Y],
out_norms[t][i][Z]);
if (i < 2)
fprintf(gOutfile, ",");
}
fprintf(gOutfile, "\n");
} /*for*/
tab_dec();
tab_indent();
fprintf(gOutfile, "} // tri\n");
if (gTexture_name != NULL) {
tab_indent();
fprintf(gOutfile, "texture { %s }\n", gTexture_name);
}
tab_dec();
tab_indent();
fprintf(gOutfile, "} // object\n");
fprintf(gOutfile, "\n");
break;
case OUTPUT_POLYRAY:
if (norm == NULL) {
tab_indent();
fprintf(gOutfile, "object { polygon 3,");
for (i=0;i<3;i++) {
fprintf(gOutfile, " <%g, %g, %g>",
out_verts[t][i][X], out_verts[t][i][Y],
out_verts[t][i][Z]);
if (i < 2)
fprintf(gOutfile, ", ");
}
} else {
tab_indent();
fprintf(gOutfile, "object { patch ");
for (i=0;i<3;i++) {
fprintf(gOutfile, " <%g, %g, %g>, <%g, %g, %g>",
out_verts[t][i][X], out_verts[t][i][Y],
out_verts[t][i][Z], out_norms[t][i][X],
out_norms[t][i][Y], out_norms[t][i][Z]);
if (i < 2)
fprintf(gOutfile, ", ");
}
}
if (gTexture_name != NULL)
fprintf(gOutfile, " %s", gTexture_name);
fprintf(gOutfile, " }\n");
fprintf(gOutfile, "\n");
break;
case OUTPUT_VIVID:
if (norm == NULL) {
tab_indent();
fprintf(gOutfile, "polygon { points 3 ");
for (i=0;i<3;i++) {
fprintf(gOutfile, " vertex %g %g %g ",
out_verts[t][i][X], out_verts[t][i][Y],
out_verts[t][i][Z]);
}
} else {
fprintf(gOutfile, "patch {");
for (i=0;i<3;++i) {
fprintf(gOutfile,
" vertex %g %g %g normal %g %g %g ",
out_verts[t][i][X], out_verts[t][i][Y],
out_verts[t][i][Z], out_norms[t][i][X],
out_norms[t][i][Y], out_norms[t][i][Z]);
}
}
fprintf(gOutfile, " }\n");
fprintf(gOutfile, "\n");
break;
case OUTPUT_QRT:
/* Doesn't matter if there are vertex normals,
* QRT can't use them.
*/
fprintf(gOutfile, "TRIANGLE ( ");
fprintf(gOutfile, "loc = (%g, %g, %g), ",
out_verts[t][0][X], out_verts[t][0][Y],
out_verts[t][0][Z]);
fprintf(gOutfile, "vect1 = (%g, %g, %g), ",
out_verts[t][1][X] - out_verts[t][0][X],
out_verts[t][1][Y] - out_verts[t][0][Y],
out_verts[t][1][Z] - out_verts[t][0][Z]);
fprintf(gOutfile, "vect2 = (%g, %g, %g) ",
out_verts[t][2][X] - out_verts[t][0][X],
out_verts[t][2][Y] - out_verts[t][0][Y],
out_verts[t][2][Z] - out_verts[t][0][Z]);
fprintf(gOutfile, " );\n");
break;
case OUTPUT_RAYSHADE:
fprintf(gOutfile, "triangle ");
if (gTexture_name != NULL)
fprintf(gOutfile, "%s ", gTexture_name);
for (i=0;i<3;i++) {
fprintf(gOutfile, "%g %g %g ",
out_verts[t][i][X], out_verts[t][i][Y],
out_verts[t][i][Z]);
if (norm != NULL)
fprintf(gOutfile, "%g %g %g ",
out_norms[t][i][X], out_norms[t][i][Y],
out_norms[t][i][Z]);
}
fprintf(gOutfile, "\n");
break;
case OUTPUT_ART:
tab_indent();
fprintf(gOutfile, "polygon {\n");
tab_inc();
tab_indent();
for (i=0;i<3;i++) {
tab_indent();
fprintf(gOutfile, "vertex(%f, %f, %f)",
out_verts[t][i][X], out_verts[t][i][Y],
out_verts[t][i][Z]);
if (norm != NULL)
fprintf(gOutfile, ", (%f, %f, %f)\n",
out_norms[t][i][X], out_norms[t][i][Y],
out_norms[t][i][Z]);
else
fprintf(gOutfile, "\n");
}
tab_dec();
tab_indent();
fprintf(gOutfile, "}\n");