-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVRMLC.pm
More file actions
1836 lines (1691 loc) · 46.1 KB
/
VRMLC.pm
File metadata and controls
1836 lines (1691 loc) · 46.1 KB
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
# Copyright (C) 1998 Tuomas J. Lukka
# Portions Copyright (C) 1998 Bernhard Reiter
# DISTRIBUTED WITH NO WARRANTY, EXPRESS OR IMPLIED.
# See the GNU Library General Public License (file COPYING in the distribution)
# for conditions of use and redistribution.
# The C routines to render various nodes quickly
#
# Field values by subs so that generalization possible..
#
# getf(Node,"fieldname",[index,...]) returns c string to get field name.
# getaf(Node,"fieldname",n) returns comma-separated list of all the field values.
# getfn(Node,"fieldname"
#
# Of these, VP is taken into account by Transform
#
# Why so elaborate code generation?
# - makes it easy to change structs later
# - makes it very easy to add fast implementations for new proto'ed
# node types
#
#
# TODO:
# Test indexedlineset
# do normals for indexedfaceset
# To allow faster internal representations of nodes to be calculated,
# there is the field '_change' which can be compared between the node
# and the internal rep - if different, needs to be regenerated.
#
# the rep needs to be allocated if _intern == 0.
# XXX Freeing?!!?
require 'VRMLFields.pm';
require 'VRMLNodes.pm';
require 'VRMLRend.pm';
#######################################################################
#######################################################################
#######################################################################
#
# RendRay --
# code for checking whether a ray (defined by mouse pointer)
# intersects with the geometry of the primitive.
#
#
# Y axis rotation around an unit vector:
# alpha = angle between Y and vec, theta = rotation angle
# 1. in X plane ->
# Y = Y - sin(alpha) * (1-cos(theta))
# X = sin(alpha) * sin(theta)
#
#
# How to find out the orientation from two vectors (we are allowed
# to assume no negative scales)
# 1. Y -> Y' -> around any vector on the plane midway between the
# two vectors
# Z -> Z' -> around any vector ""
#
# -> intersection.
#
# The plane is the midway normal between the two vectors
# (if the two vectors are the same, it is the vector).
%RendRayC = (
Box => '
float x = $f(size,0)/2;
float y = $f(size,1)/2;
float z = $f(size,2)/2;
/* 1. x=const-plane faces? */
if(!XEQ) {
float xrat0 = XRAT(x);
float xrat1 = XRAT(-x);
if(verbose) printf("!XEQ: %f %f\n",xrat0,xrat1);
if(TRAT(xrat0)) {
float cy = MRATY(xrat0);
if(verbose) printf("TRok: %f\n",cy);
if(cy >= -y && cy < y) {
float cz = MRATZ(xrat0);
if(verbose) printf("cyok: %f\n",cz);
if(cz >= -z && cz < z) {
if(verbose) printf("czok:\n");
HIT(xrat0, x,cy,cz, 1,0,0, -1,-1, "cube x0");
}
}
}
if(TRAT(xrat1)) {
float cy = MRATY(xrat1);
if(cy >= -y && cy < y) {
float cz = MRATZ(xrat1);
if(cz >= -z && cz < z) {
HIT(xrat1, -x,cy,cz, -1,0,0, -1,-1, "cube x1");
}
}
}
}
if(!YEQ) {
float yrat0 = YRAT(y);
float yrat1 = YRAT(-y);
if(TRAT(yrat0)) {
float cx = MRATX(yrat0);
if(cx >= -x && cx < x) {
float cz = MRATZ(yrat0);
if(cz >= -z && cz < z) {
HIT(yrat0, cx,y,cz, 0,1,0, -1,-1, "cube y0");
}
}
}
if(TRAT(yrat1)) {
float cx = MRATX(yrat1);
if(cx >= -x && cx < x) {
float cz = MRATZ(yrat1);
if(cz >= -z && cz < z) {
HIT(yrat1, cx,-y,cz, 0,-1,0, -1,-1, "cube y1");
}
}
}
}
if(!ZEQ) {
float zrat0 = ZRAT(z);
float zrat1 = ZRAT(-z);
if(TRAT(zrat0)) {
float cx = MRATX(zrat0);
if(cx >= -x && cx < x) {
float cy = MRATY(zrat0);
if(cy >= -y && cy < y) {
HIT(zrat0, cx,cy,z, 0,0,1, -1,-1,"cube z0");
}
}
}
if(TRAT(zrat1)) {
float cx = MRATX(zrat1);
if(cx >= -x && cx < x) {
float cy = MRATY(zrat1);
if(cy >= -y && cy < y) {
HIT(zrat1, cx,cy,-z, 0,0,-1, -1,-1,"cube z1");
}
}
}
}
',
# Distance to zero as function of ratio is
# sqrt(
# ((1-r)t_r1.x + r t_r2.x)**2 +
# ((1-r)t_r1.y + r t_r2.y)**2 +
# ((1-r)t_r1.z + r t_r2.z)**2
# ) == radius
# Therefore,
# radius ** 2 == ... ** 2
# and
# radius ** 2 =
# (1-r)**2 * (t_r1.x**2 + t_r1.y**2 + t_r1.z**2) +
# 2*(r*(1-r)) * (t_r1.x*t_r2.x + t_r1.y*t_r2.y + t_r1.z*t_r2.z) +
# r**2 (t_r2.x**2 ...)
# Let's name tr1sq, tr2sq, tr1tr2 and then we have
# radius ** 2 = (1-r)**2 * tr1sq + 2 * r * (1-r) tr1tr2 + r**2 tr2sq
# = (tr1sq - 2*tr1tr2 + tr2sq) r**2 + 2 * r * (tr1tr2 - tr1sq) + tr1sq
#
# I.e.
#
# (tr1sq - 2*tr1tr2 + tr2sq) r**2 + 2 * r * (tr1tr2 - tr1sq) +
# (tr1sq - radius**2) == 0
#
# I.e. second degree eq. a r**2 + b r + c == 0 where
# a = tr1sq - 2*tr1tr2 + tr2sq
# b = 2*(tr1tr2 - tr1sq)
# c = (tr1sq-radius**2)
#
#
Sphere => '
float r = $f(radius);
/* Center is at zero. t_r1 to t_r2 and t_r1 to zero are the vecs */
float tr1sq = VECSQ(t_r1);
float tr2sq = VECSQ(t_r2);
float tr1tr2 = VECPT(t_r1,t_r2);
struct pt dr2r1;
float dlen;
float a,b,c,disc;
VECDIFF(t_r2,t_r1,dr2r1);
dlen = VECSQ(dr2r1);
a = dlen; /* tr1sq - 2*tr1tr2 + tr2sq; */
b = 2*(VECPT(dr2r1, t_r1));
c = tr1sq - r*r;
disc = b*b - 4*a*c; /* The discriminant */
if(disc > 0) { /* HITS */
float q ;
float sol1 ;
float sol2 ;
float cx,cy,cz;
q = sqrt(disc);
/* q = (-b+(b>0)?q:-q)/2; */
sol1 = (-b+q)/(2*a);
sol2 = (-b-q)/(2*a);
/*
printf("SPHSOL0: (%f %f %f) (%f %f %f)\n",
t_r1.x, t_r1.y, t_r1.z, t_r2.x, t_r2.y, t_r2.z);
printf("SPHSOL: (%f %f %f) (%f) (%f %f) (%f) (%f %f)\n",
tr1sq, tr2sq, tr1tr2, a, b, c, und, sol1, sol2);
*/
cx = MRATX(sol1);
cy = MRATY(sol1);
cz = MRATZ(sol1);
HIT(sol1, cx,cy,cz, cx/r,cy/r,cz/r, -1,-1, "sphere 0");
cx = MRATX(sol2);
cy = MRATY(sol2);
cz = MRATZ(sol2);
HIT(sol2, cx,cy,cz, cx/r,cy/r,cz/r, -1,-1, "sphere 1");
}
',
# Cylinder: first test the caps, then against infinite cylinder.
Cylinder => '
float h = $f(height)/2; /* pos and neg dir. */
float r = $f(radius);
float y = h;
/* Caps */
if(!YEQ) {
float yrat0 = YRAT(y);
float yrat1 = YRAT(-y);
if(TRAT(yrat0)) {
float cx = MRATX(yrat0);
float cz = MRATZ(yrat0);
if(r*r > cx*cx+cz*cz) {
HIT(yrat0, cx,y,cz, 0,1,0, -1,-1, "cylcap 0");
}
}
if(TRAT(yrat1)) {
float cx = MRATX(yrat1);
float cz = MRATZ(yrat1);
if(r*r > cx*cx+cz*cz) {
HIT(yrat1, cx,-y,cz, 0,-1,0, -1,-1, "cylcap 1");
}
}
}
/* Body -- do same as for sphere, except no y axis in distance */
if((!XEQ) && (!ZEQ)) {
float dx = t_r2.x-t_r1.x; float dz = t_r2.z-t_r1.z;
float a = dx*dx + dz*dz;
float b = 2*(dx * t_r1.x + dz * t_r1.z);
float c = t_r1.x * t_r1.x + t_r1.z * t_r1.z - r*r;
float und;
b /= a; c /= a;
und = b*b - 4*c;
if(und > 0) { /* HITS the infinite cylinder */
float sol1 = (-b+sqrt(und))/2;
float sol2 = (-b-sqrt(und))/2;
float cy,cx,cz;
cy = MRATY(sol1);
if(cy > -h && cy < h) {
cx = MRATX(sol1);
cz = MRATZ(sol1);
HIT(sol1, cx,cy,cz, cx/r,0,cz/r, -1,-1, "cylside 1");
}
cy = MRATY(sol2);
if(cy > -h && cy < h) {
cx = MRATX(sol2);
cz = MRATZ(sol2);
HIT(sol2, cx,cy,cz, cx/r,0,cz/r, -1,-1, "cylside 2");
}
}
}
',
# For cone, this is most difficult. We have
# sqrt(
# ((1-r)t_r1.x + r t_r2.x)**2 +
# ((1-r)t_r1.z + r t_r2.z)**2
# ) == radius*( -( (1-r)t_r1.y + r t_r2.y )/(2*h)+0.5)
# == radius * ( -( r*(t_r2.y - t_r1.y) + t_r1.y )/(2*h)+0.5)
# == radius * ( -r*(t_r2.y-t_r1.y)/(2*h) + 0.5 - t_r1.y/(2*h))
#
# Other side: r*r*(
Cone => '
float h = $f(height)/2; /* pos and neg dir. */
float y = h;
float r = $f(bottomRadius);
float dx = t_r2.x-t_r1.x; float dz = t_r2.z-t_r1.z;
float dy = t_r2.y-t_r1.y;
float a = dx*dx + dz*dz - (r*r*dy*dy/(2*h*2*h));
float b = 2*(dx*t_r1.x + dz*t_r1.z) +
2*r*r*dy/(2*h)*(0.5-t_r1.y/(2*h));
float tmp = (0.5-t_r1.y/(2*h));
float c = t_r1.x * t_r1.x + t_r1.z * t_r1.z
- r*r*tmp*tmp;
float und;
b /= a; c /= a;
und = b*b - 4*c;
/*
printf("CONSOL0: (%f %f %f) (%f %f %f)\n",
t_r1.x, t_r1.y, t_r1.z, t_r2.x, t_r2.y, t_r2.z);
printf("CONSOL: (%f %f %f) (%f) (%f %f) (%f)\n",
dx, dy, dz, a, b, c, und);
*/
if(und > 0) { /* HITS the infinite cylinder */
float sol1 = (-b+sqrt(und))/2;
float sol2 = (-b-sqrt(und))/2;
float cy,cx,cz;
float cy0;
cy = MRATY(sol1);
if(cy > -h && cy < h) {
cx = MRATX(sol1);
cz = MRATZ(sol1);
/* XXX Normal */
HIT(sol1, cx,cy,cz, cx/r,0,cz/r, -1,-1, "conside 1");
}
cy0 = cy;
cy = MRATY(sol2);
if(cy > -h && cy < h) {
cx = MRATX(sol2);
cz = MRATZ(sol2);
HIT(sol2, cx,cy,cz, cx/r,0,cz/r, -1,-1, "conside 2");
}
/*
printf("CONSOLV: (%f %f) (%f %f)\n", sol1, sol2,cy0,cy);
*/
}
if(!YEQ) {
float yrat0 = YRAT(-y);
if(TRAT(yrat0)) {
float cx = MRATX(yrat0);
float cz = MRATZ(yrat0);
if(r*r > cx*cx + cz*cz) {
HIT(yrat0, cx, -y, cz, 0, -1, 0, -1, -1, "conbot");
}
}
}
',
ElevationGrid => ( '
$mk_polyrep();
render_ray_polyrep(this_,
0, NULL
);
'),
Extrusion => ( '
$mk_polyrep();
render_ray_polyrep(this_,
0, NULL
);
'),
IndexedFaceSet => '
struct SFColor *points; int npoints;
$fv(coord, points, get3, &npoints);
$mk_polyrep();
render_ray_polyrep(this_,
npoints, points
);
',
);
#####################################################################3
#####################################################################3
#####################################################################3
#
# GenPolyRep
# code for generating internal polygonal representations
# of some nodes (ElevationGrid, Extrusion and IndexedFaceSet)
#
#
# In one sense, we could just plot the polygons here and be done
# with it -- displaylists would speed it up.
#
# However, doing this in a device-independent fashion will help
# us a *lot* in porting to some other 3D api.
%GenPolyRepC = (
# ElevationGrid = 2 triangles per each face.
# No color or normal support yet
ElevationGrid => '
int x,z;
int nx = $f(xDimension);
float xs = $f(xSpacing);
int nz = $f(zDimension);
float zs = $f(zSpacing);
float *f = $f(height);
float a[3],b[3];
int *cindex;
float *coord;
int *colindex;
int ntri = (nx && nz ? 2 * (nx-1) * (nz-1) : 0);
int triind;
int nf = $f_n(height);
int cpv = $f(colorPerVertex);
struct SFColor *colors; int ncolors=0;
struct VRML_PolyRep *rep_ = this_->_intern;
$fv_null(color, colors, get3, &ncolors);
rep_->ntri = ntri;
printf("Gen elevgrid %d %d %d\n", ntri, nx, nz);
if(nf != nx * nz) {
die("Elevationgrid: too many / too few: %d %d %d\n",
nf, nx, nz);
}
if(ncolors) {
if(!cpv && ncolors < (nx-1) * (nz-1)) {
die("Elevationgrid: too few colors");
}
if(cpv && ncolors < nx*nz) {
die("Elevationgrid: 2too few colors");
}
}
cindex = rep_->cindex = malloc(sizeof(*(rep_->cindex))*3*(ntri));
coord = rep_->coord = malloc(sizeof(*(rep_->coord))*nx*nz*3);
colindex = rep_->colindex = malloc(sizeof(*(rep_->colindex))*3*(ntri));
/* Flat */
rep_->normal = malloc(sizeof(*(rep_->normal))*3*ntri);
rep_->norindex = malloc(sizeof(*(rep_->norindex))*3*ntri);
/* Prepare the coordinates */
for(x=0; x<nx; x++) {
for(z=0; z<nz; z++) {
float h = f[x+z*nx];
coord[(x+z*nx)*3+0] = x*xs;
coord[(x+z*nx)*3+1] = h;
coord[(x+z*nx)*3+2] = z*zs;
}
}
triind = 0;
for(x=0; x<nx-1; x++) {
for(z=0; z<nz-1; z++) {
/* 1: */
cindex[triind*3+0] = x+z*nx;
cindex[triind*3+1] = x+(z+1)*nx;
cindex[triind*3+2] = (x+1)+z*nx;
if(cpv) {
colindex[triind*3+0] = x+z*nx;
colindex[triind*3+1] = x+(z+1)*nx;
colindex[triind*3+2] = (x+1)+z*nx;
} else {
colindex[triind*3+0] = x+z*(nx-1);
colindex[triind*3+1] = x+z*(nx-1);
colindex[triind*3+2] = x+z*(nx-1);
}
rep_->norindex[triind*3+0] = triind;
rep_->norindex[triind*3+1] = triind;
rep_->norindex[triind*3+2] = triind;
triind ++;
/* 2: */
cindex[triind*3+0] = x+(z+1)*nx;
cindex[triind*3+1] = (x+1)+(z+1)*nx;
cindex[triind*3+2] = (x+1)+z*nx;
if(cpv) {
colindex[triind*3+0] = x+(z+1)*nx;
colindex[triind*3+1] = (x+1)+(z+1)*nx;
colindex[triind*3+2] = (x+1)+z*nx;
} else {
colindex[triind*3+0] = x+z*(nx-1);
colindex[triind*3+1] = x+z*(nx-1);
colindex[triind*3+2] = x+z*(nx-1);
}
rep_->norindex[triind*3+0] = triind;
rep_->norindex[triind*3+1] = triind;
rep_->norindex[triind*3+2] = triind;
triind ++;
}
}
calc_poly_normals_flat(rep_);
',
Extrusion => (do "VRMLExtrusion.pm"),
IndexedFaceSet => '
int i;
int cin = $f_n(coordIndex);
int cpv = $f(colorPerVertex);
/* int npv = xf(normalPerVertex); */
int ntri = 0;
int nvert = 0;
struct SFColor *c1,*c2,*c3;
float a[3]; float b[3];
struct SFColor *points; int npoints;
struct SFColor *normals; int nnormals=0;
struct VRML_PolyRep *rep_ = this_->_intern;
int *cindex;
$fv(coord, points, get3, &npoints);
$fv_null(normal, normals, get3, &nnormals);
for(i=0; i<cin; i++) {
if($f(coordIndex,i) == -1) {
if(nvert < 3) {
die("Too few vertices in indexedfaceset poly");
}
ntri += nvert-2;
nvert = 0;
} else {
nvert ++;
}
}
if(nvert>2) {ntri += nvert-2;}
cindex = rep_->cindex = malloc(sizeof(*(rep_->cindex))*3*(ntri));
rep_->ntri = ntri;
if(!nnormals) {
/* We have to generate -- do flat only for now */
rep_->normal = malloc(sizeof(*(rep_->normal))*3*ntri);
rep_->norindex = malloc(sizeof(*(rep_->norindex))*3*ntri);
} else {
rep_->normal = NULL;
rep_->norindex = NULL;
}
/* color = NULL; coord = NULL; normal = NULL;
colindex = NULL; norindex = NULL;
*/
if(!$f(convex)) {
die("AAAAARGHHH!!! Non-convex polygons! Help!");
/* XXX Fixme using gluNewTess, gluTessVertex et al */
} else {
int initind=-1;
int lastind=-1;
int triind = 0;
for(i=0; i<cin; i++) {
if($f(coordIndex,i) == -1) {
initind=-1;
lastind=-1;
} else {
if(initind == -1) {
initind = $f(coordIndex,i);
} else if(lastind == -1) {
lastind = $f(coordIndex,i);
} else {
cindex[triind*3+0] = initind;
cindex[triind*3+1] = lastind;
cindex[triind*3+2] = $f(coordIndex,i);
if(rep_->normal) {
c1 = &(points[initind]);
c2 = &(points[lastind]);
c3 = &(points[$f(coordIndex,i)]);
a[0] = c2->c[0] - c1->c[0];
a[1] = c2->c[1] - c1->c[1];
a[2] = c2->c[2] - c1->c[2];
b[0] = c3->c[0] - c1->c[0];
b[1] = c3->c[1] - c1->c[1];
b[2] = c3->c[2] - c1->c[2];
rep_->normal[triind*3+0] =
a[1]*b[2] - b[1]*a[2];
rep_->normal[triind*3+1] =
-(a[0]*b[2] - b[0]*a[2]);
rep_->normal[triind*3+2] =
a[0]*b[1] - b[0]*a[1];
rep_->norindex[triind*3+0] = triind;
rep_->norindex[triind*3+1] = triind;
rep_->norindex[triind*3+2] = triind;
}
lastind = $f(coordIndex,i);
triind++;
}
}
}
}
',
);
######################################################################
######################################################################
######################################################################
#
# Get3
# get a coordinate / color / normal array from the node.
#
%Get3C = (
Coordinate => '
*n = $f_n(point);
return $f(point);
',
Color => '
*n = $f_n(color);
return $f(color);
',
Normal => '
*n = $f_n(vector);
return $f(vector);
'
);
%Get2C = (
TextureCoordinate => '
*n = $f_n(point);
return $f(point);
',
);
######################################################################
######################################################################
######################################################################
#
# Generation
# Functions for generating the code
#
{
my %AllNodes = (%RendC, %RendRayC, %PrepC, %FinC, %ChildC, %Get3C, %Get2C, %LightC);
@NodeTypes = keys %AllNodes;
}
sub assgn_m {
my($f, $l) = @_;
return ((join '',map {"m[$_] = ".getf(Material, $f, $_).";"} 0..2).
"m[3] = $l;");
}
# XXX Might we need separate fields for separate structs?
sub getf {
my ($t, $f, @l) = @_;
my $type = $VRML::Nodes{$t}{FieldTypes}{$f};
if($type eq "") {
die("Invalid type $t $f '$type'");
}
return "VRML::Field::$type"->cget("(this_->$f)",@l);
}
sub getfn {
my($t, $f) = @_;
my $type = $VRML::Nodes{$t}{FieldTypes}{$f};
return "VRML::Field::$type"->cgetn("(this_->$f)");
}
# XXX Code copy :(
sub fvirt {
my($t, $f, $ret, $v, @a) = @_;
# Die if not exists
my $type = $VRML::Nodes{$t}{FieldTypes}{$f};
if($type ne "SFNode") {
die("Fvirt must have SFNode");
}
if($ret) {$ret = "$ret = ";}
return "if(this_->$f) {
if(!(*(struct VRML_Virt **)(this_->$f))->$v) {
die(\"NULL METHOD $t $f $v\");
}
$ret ((*(struct VRML_Virt **)(this_->$f))->$v(this_->$f,
".(join ',',@a).")) ;}
else { (die(\"NULL FIELD $t $f $a\"));}";
}
sub fvirt_null {
my($t, $f, $ret, $v, @a) = @_;
# Die if not exists
my $type = $VRML::Nodes{$t}{FieldTypes}{$f};
if($type ne "SFNode") {
die("Fvirt must have SFNode");
}
if($ret) {$ret = "$ret = ";}
return "if(this_->$f) {
if(!(*(struct VRML_Virt **)(this_->$f))->$v) {
die(\"NULL METHOD $t $f $v\");
}
$ret ((*(struct VRML_Virt **)(this_->$f))->$v(this_->$f,
".(join ',',@a).")) ;
}";
}
sub fgetfnvirt_n {
my($n, $ret, $v, @a) = @_;
if($ret) {$ret = "$ret = ";}
return "if($n) {
if(!(*(struct VRML_Virt **)n)->$v) {
die(\"NULL METHOD $n $ret $v\");
}
$ret ((*(struct VRML_Virt **)($n))->$v($n,
".(join ',',@a).")) ;}
"
}
sub rend_geom {
return $_[0];
}
sub gen_struct {
my($name,$node) = @_;
my @f = keys %{$node->{FieldTypes}};
my $nf = scalar @f;
# /* Store actual point etc. later */
my $s = "struct VRML_$name {\n" .
" /***/ struct VRML_Virt *v;\n" .
" /*s*/ int _sens; \n" .
" /*t*/ int _hit; \n" .
" /*a*/ int _change; \n" .
" /*n*/ int _dlchange; \n" .
" /*d*/ GLuint _dlist; \n" .
" /*a*/ int _dl2change; \n" .
" /*r*/ GLuint _dl2ist; \n" .
" /*d*/ void *_intern; \n" .
" /***/\n";
my $o = "
void *
get_${name}_offsets(p)
SV *p;
CODE:
int *ptr_;
SvGROW(p,($nf+1)*sizeof(int));
SvCUR_set(p,($nf+1)*sizeof(int));
ptr_ = (int *)SvPV(p,na);
";
my $p = " {
my \$s = '';
my \$v = get_${name}_offsets(\$s);
\@{\$n->{$name}{Offs}}{".(join ',',map {"\"$_\""} @f,'_end_')."} =
unpack(\"i*\",\$s);
\$n->{$name}{Virt} = \$v;
}
";
for(@f) {
my $cty = "VRML::Field::$node->{FieldTypes}{$_}"->ctype($_);
$s .= "\t$cty;\n";
$o .= "\t*ptr_++ = offsetof(struct VRML_$name, $_);\n";
}
$o .= "\t*ptr_++ = sizeof(struct VRML_$name);\n";
$o .= "RETVAL=&(virt_${name});
if(verbose) printf(\"$name virtual: %d\\n\", RETVAL);
OUTPUT:
RETVAL
";
$s .= "};\n";
return ($s,$o,$p);
}
#########################################################
sub get_offsf {
my($f) = @_;
my ($ct) = ("VRML::Field::$_")->ctype("*ptr_");
my ($ctp) = ("VRML::Field::$_")->ctype("*");
my ($c) = ("VRML::Field::$_")->cfunc("(*ptr_)", "sv_");
my ($ca) = ("VRML::Field::$_")->calloc("(*ptr_)");
my ($cf) = ("VRML::Field::$_")->cfree("(*ptr_)");
return "
void
set_offs_$f(ptr,offs,sv_)
void *ptr
int offs
SV *sv_
CODE:
$ct = ($ctp)(((char *)ptr)+offs);
{struct VRML_Box *p;
p = ptr;
p->_change ++;
}
$c
void
alloc_offs_$f(ptr,offs)
void *ptr
int offs
CODE:
$ct = ($ctp)(((char *)ptr)+offs);
$ca
void
free_offs_$f(ptr,offs)
void *ptr
int offs
CODE:
$ct = ($ctp)(((char *)ptr)+offs);
$cf
"
}
#######################################################
sub get_rendfunc {
my($n) = @_;
print "RENDF $n ";
# XXX
my @f = qw/Prep Rend Child Fin RendRay GenPolyRep Light Get3 Get2/;
my $f;
my $v = "
static struct VRML_Virt virt_${n} = { ".
(join ',',map {${$_."C"}{$n} ? "${n}_$_" : "NULL"} @f).
",\"$n\"};";
for(@f) {
my $c =${$_."C"}{$n};
next if !defined $c;
print "$_ (",length($c),") ";
# Substitute field gets
$c =~ s~\$tex2d\(([^)]*)\)~
{
int rx,sx,ry,sy;
unsigned char *ptr = SvPV(\$f(__data$1),na);
if(\$f(__depth$1) && \$f(__x$1) && \$f(__y$1)) {
unsigned char *dest = ptr;
rx = 1; sx = \$f(__x$1);
while(sx) {sx /= 2; rx *= 2;}
if(rx/2 == \$f(__x$1)) {rx /= 2;}
ry = 1; sy = \$f(__y$1);
while(sy) {sy /= 2; ry *= 2;}
if(ry/2 == \$f(__y$1)) {ry /= 2;}
if(rx != \$f(__x$1) || ry != \$f(__y$1)) {
/* We have to scale */
dest = malloc(\$f(__depth$1) * rx * ry);
printf("Scaling %d %d to %d %d\n",
\$f(__x$1), \$f(__y$1) ,
rx, ry);
gluScaleImage(
(\$f(__depth$1)==1 ? GL_LUMINANCE : GL_RGB),
\$f(__x$1), \$f(__y$1),
GL_UNSIGNED_BYTE,
ptr,
rx, ry,
GL_UNSIGNED_BYTE,
dest
);
}
printf("PTR: %d, %d %d %d %d %d %d %d %d %d %d\n",
dest, dest[0], dest[1], dest[2], dest[3], dest[4], dest[5],
dest[6], dest[7], dest[8], dest[9]);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
printf("Doing imagetext %d %d %d\n",\$f(__depth$1),\$f(__x$1),\$f(__y$1));
glDisable(GL_LIGHTING);
glEnable(GL_TEXTURE_2D);
glColor3f(1,1,1);
glTexImage2D(GL_TEXTURE_2D,
0,
\$f(__depth$1),
rx, ry,
0,
(\$f(__depth$1)==1 ? GL_LUMINANCE : GL_RGB),
GL_UNSIGNED_BYTE,
dest
);
if(ptr != dest) free(dest);
}
}
~g;
$c =~ s/\$f\(([^)]*)\)/getf($n,split ',',$1)/ge;
$c =~ s/\$f_n\(([^)]*)\)/getfn($n,split ',',$1)/ge;
$c =~ s/\$fv\(([^)]*)\)/fvirt($n,split ',',$1)/ge;
$c =~ s/\$fv_null\(([^)]*)\)/fvirt_null($n,split ',',$1)/ge;
$c =~ s/\$mk_polyrep\(\)/if(!this_->_intern ||
this_->_change != ((struct VRML_PolyRep *)this_->_intern)->_change)
regen_polyrep(this_);/g;
$c =~ s/\$start(_|)list\(\)/
if(!this_->_dlist) {
this_->_dlist = glGenLists(1);
}
if(this_->_dlchange != this_->_change) {
glNewList(this_->_dlist,GL_COMPILE_AND_EXECUTE);
this_->_dlchange = this_->_change;
} else {
glCallList(this_->_dlist); return;
}/g;
$c =~ s/\$end(_|)list\(\)/
glEndList()
/g;
$c =~ s/\$start(_|)list2\(\)/
if(!this_->_dl2ist) {
this_->_dl2ist = glGenLists(1);
}
if(this_->_dl2change != this_->_change) {
glNewList(this_->_dl2ist,GL_COMPILE_AND_EXECUTE);
this_->_dl2change = this_->_change;
} else {
glCallList(this_->_dl2ist); return;
}/g;
$c =~ s/\$end(_|)list2\(\)/
glEndList()
/g;
if($_ eq "Get3") {
$f .= "\n\nstruct SFColor *${n}_$_(void *nod_,int *n)";
} elsif($_ eq "Get2") {
$f .= "\n\nstruct SFVec2f *${n}_$_(void *nod_,int *n)";
} else {
$f .= "\n\nvoid ${n}_$_(void *nod_)";
}
$f .= "{ /* GENERATED FROM HASH ${_}C, MEMBER $n */
struct VRML_$n *this_ = (struct VRML_$n *)nod_;
{$c}
}";
}
print "\n";
return ($f,$v);
}
######################################################################
######################################################################
######################################################################
#
# gen - the main function. this contains much verbatim code
#
#
sub gen {
for(@VRML::Fields) {
push @str, ("VRML::Field::$_")->cstruct . "\n";
push @xsfn, get_offsf($_);
}
push @str, "\n/* and now the structs for the nodetypes */ \n";
for(@NodeTypes) {
my $no = $VRML::Nodes{$_};
my($str, $offs, $perl) = gen_struct($_, $no);
push @str, $str;
push @xsfn, $offs;
push @poffsfn, $perl;
my($f, $vstru) = get_rendfunc($_);
push @func, $f;
push @vstruc, $vstru;
}
open XS, ">VRMLFunc.xs";
print XS '
/* VRMLFunc.c generated by VRMLC.pm. DO NOT MODIFY, MODIFY VRMLC.pm INSTEAD */
/* Code here comes almost verbatim from VRMLC.pm */
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include <math.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glx.h>
#include "OpenGL/OpenGL.m"
#define offset_of(p_type,field) ((unsigned int)(&(((p_type)NULL)->field)-NULL))
#define TC(a,b) glTexCoord2f(a,b)
#ifdef M_PI
#define PI M_PI
#else
#define PI 3.141592653589793
#endif
/* Faster trig macros (thanks for Robin Williams) */
#define DECL_TRIG1 float t_aa, t_ab, t_sa, t_ca, t_sa1, t_ca1;
#define INIT_TRIG1(div) t_aa = sin(PI/(div)); t_aa *= 2*t_aa; t_ab = sin(2*PI/(div));
#define START_TRIG1 t_sa = 0; t_ca = 1;
#define UP_TRIG1 t_sa1 = t_sa; t_sa -= t_sa*t_aa - t_ca * t_ab; t_ca -= t_ca * t_aa + t_sa1 * t_ab;
#define SIN1 t_sa
#define COS1 t_ca
#define DECL_TRIG2 float t2_aa, t2_ab, t2_sa, t2_ca, t2_sa1, t2_ca1;
#define INIT_TRIG2(div) t2_aa = sin(PI/(div)); t2_aa *= 2*t2_aa; t2_ab = sin(2*PI/(div));
#define START_TRIG2 t2_sa = 0; t2_ca = 1;
#define UP_TRIG2 t2_sa1 = t2_sa; t2_sa -= t2_sa*t2_aa - t2_ca * t2_ab; t2_ca -= t2_ca * t2_aa + t2_sa1 * t2_ab;
#define SIN2 t2_sa
#define COS2 t2_ca
D_OPENGL;
/* Rearrange to take advantage of headlight when off */
int curlight = 0;
int nlightcodes = 7;
int lightcode[7] = {
GL_LIGHT1,
GL_LIGHT2,
GL_LIGHT3,
GL_LIGHT4,
GL_LIGHT5,
GL_LIGHT6,
GL_LIGHT7,
};
int nextlight() {
if(curlight == nlightcodes) { return -1; }
return lightcode[curlight++];
}
struct VRML_Virt {
void (*prep)(void *);
void (*rend)(void *);
void (*children)(void *);
void (*fin)(void *);
void (*rendray)(void *);
void (*mkpolyrep)(void *);
void (*light)(void *);
/* And get float coordinates : Coordinate, Color */
/* XXX Relies on MFColor repr.. */
struct SFColor *(*get3)(void *, int *); /* Number in int */
struct SFVec2f *(*get2)(void *, int *); /* Number in int */
char *name;
};
/* Internal representation of IndexedFaceSet, Extrusion & ElevationGrid:
* set of triangles.
* done so that we get rid of concave polygons etc.