forked from BRL-CAD/brlcad
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsh_grass.c
More file actions
1387 lines (1088 loc) · 38.9 KB
/
Copy pathsh_grass.c
File metadata and controls
1387 lines (1088 loc) · 38.9 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
/* S H _ G R A S S . C
* BRL-CAD
*
* Copyright (c) 1998-2026 United States Government as represented by
* the U.S. Army Research Laboratory.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2.1 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this file; see the file named COPYING for more
* information.
*/
/** @file liboptical/sh_grass.c
*
* A procedural shader to produce grass
*
*/
#include "common.h"
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "bu/parallel.h"
#include "vmath.h"
#include "bv/plot3.h"
#include "raytrace.h"
#include "optical.h"
#define SHADE_CONT 0
#define SHADE_ABORT_GRASS 1 /* bit_flag */
#define SHADE_ABORT_STACK 2 /* bit_flag */
#define grass_MAGIC 0x1834 /* make this a unique number for each shader */
#define CK_grass_SP(_p) BU_CKMAG(_p, grass_MAGIC, "grass_specific")
/* compute the Region coordinates of the origin of a cell */
#define CELL_POS(cell_pos, grass_sp, cell_num) { \
cell_pos[X] = cell_num[X] * grass_sp->cell[X]; \
cell_pos[Y] = cell_num[Y] * grass_sp->cell[Y]; \
}
#define BLADE_SEGS_MAX 4
#define LEAF_MAGIC 1024
#define BLADE_MAGIC 1023
#define PLANT_MAGIC 1022
struct leaf_segment {
uint32_t magic;
double len; /* length of blade segment */
vect_t blade; /* direction of blade growth */
vect_t N; /* surface normal of blade segment */
};
struct blade {
uint32_t magic;
double width;
double tot_len; /* total length of blade */
int segs; /* # of segments in blade */
struct leaf_segment leaf[BLADE_SEGS_MAX]; /* segments */
point_t pmin; /* blade bbox min */
point_t pmax; /* blade bbox max */
};
#define BLADE_MAX 6
#define BLADE_LAST (BLADE_MAX-1)
struct plant {
uint32_t magic;
point_t root; /* location of base of blade */
int blades; /* # of blades from same root */
struct blade b[BLADE_MAX]; /* blades */
point_t pmin; /* plant bbox min */
point_t pmax; /* plant bbox max */
};
#define GRASSRAY_MAGIC 2048
struct grass_ray {
uint32_t magic;
double occlusion;
struct xray r;
double d_max;
vect_t rev;
double diverge;
double radius;
struct bn_tol tol;
struct hit hit;
FILE *fd;
struct application *ap;
};
#define grass_ray_MAGIC 0x2461
#define CK_grass_r(_p) BU_CKMAG(_p, grass_ray_MAGIC, "grass_ray")
/*
* the shader specific structure contains all variables which are unique
* to any particular use of the shader.
*/
struct grass_specific {
uint32_t magic; /* magic # for memory validity check, must come 1st */
int debug;
FILE *fd;
double cell[2]; /* size of a cell in Region coordinates */
double ppc; /* mean # plants_per_cell */
double ppcd; /* deviation of plants_per_cell */
double t; /* mean length of leaf segment */
double blade_width; /* max width of blade segment */
int nsegs; /* #segs per blade */
double seg_ratio;
double lacunarity; /* the usual noise parameters */
double h_val;
double octaves;
double size; /* size of noise coordinate space */
point_t vscale; /* size of noise coordinate space */
vect_t delta;
point_t brown;
struct plant proto;
mat_t m_to_sh; /* model to shader space matrix */
mat_t sh_to_m; /* model to shader space matrix */
};
/* The default values for the variables in the shader specific structure */
static const struct grass_specific grass_defaults = {
grass_MAGIC,
0,
(FILE *)0,
{400.0, 400.0}, /* cell */
5.0, /* plants_per_cell */
3.0, /* deviation of plants_per_cell */
300.0, /* "t" mean length of leaf (mm)*/
3.0, /* max width (mm) of blade segment */
4, /* # segs per blade */
1.0, /* seg ratio */
2.1753974, /* lacunarity */
1.0, /* h_val */
4.0, /* octaves */
.31415926535, /* size */
VINITALL(1.0), /* vscale */
{ 1001.6, 1020.5, 1300.4 }, /* delta into noise space */
{.7, .6, .3}, /* brown */
{ /* struct plant proto */
0, /* magic */
VINIT_ZERO, /* root */
0, /* blades */
{{ /* struct blade */
0, /* magic */
0.0, /* width */
0.0, /* tot_len */
0, /* segs */
{{ /* struct leaf_segment */
0, /* magic */
0.0, /* len */
VINIT_ZERO, /* blade */
VINIT_ZERO /* N */
}},
VINIT_ZERO, /* pmin */
VINIT_ZERO /* pmax */
}},
VINIT_ZERO, /* pmin */
VINIT_ZERO /* pmax */
},
MAT_INIT_IDN, /* m_to_sh */
MAT_INIT_IDN /* sh_to_m */
};
#define GRASS_SHDR_O(m) bu_offsetof(struct grass_specific, m)
/* description of how to parse/print the arguments to the shader
* There is at least one line here for each variable in the shader specific
* structure above
*/
struct bu_structparse grass_print_tab[] = {
{"%g", 2, "cell", GRASS_SHDR_O(cell), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL },
{"%g", 1, "ppc", GRASS_SHDR_O(ppc), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL },
{"%g", 1, "ppcd", GRASS_SHDR_O(ppcd), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL },
{"%g", 1, "t", GRASS_SHDR_O(t), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL },
{"%g", 1, "width", GRASS_SHDR_O(blade_width), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL },
{"%g", 1, "lacunarity", GRASS_SHDR_O(lacunarity), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL },
{"%g", 1, "H", GRASS_SHDR_O(h_val), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL },
{"%g", 1, "octaves", GRASS_SHDR_O(octaves), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL },
{"%g", 1, "size", GRASS_SHDR_O(size), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL },
{"%d", 1, "nsegs", GRASS_SHDR_O(nsegs), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL },
{"%g", 1, "seg_ratio", GRASS_SHDR_O(seg_ratio), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL },
{"", 0, (char *)0, 0, BU_STRUCTPARSE_FUNC_NULL, NULL, NULL }
};
struct bu_structparse grass_parse_tab[] = {
{"%p", 1, "grass_print_tab", bu_byteoffset(grass_print_tab[0]), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL },
{"%g", 2, "c", GRASS_SHDR_O(cell), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL },
{"%g", 1, "p", GRASS_SHDR_O(ppc), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL },
{"%g", 1, "pd", GRASS_SHDR_O(ppcd), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL },
{"%g", 1, "l", GRASS_SHDR_O(lacunarity), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL },
{"%g", 1, "o", GRASS_SHDR_O(octaves), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL },
{"%g", 1, "s", GRASS_SHDR_O(size), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL },
{"%g", 1, "w", GRASS_SHDR_O(blade_width), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL },
{"%d", 1, "n", GRASS_SHDR_O(nsegs), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL },
{"%g", 1, "r", GRASS_SHDR_O(seg_ratio), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL },
{"%d", 1, "d", GRASS_SHDR_O(debug), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL },
{"", 0, (char *)0, 0, BU_STRUCTPARSE_FUNC_NULL, NULL, NULL }
};
static int grass_setup(register struct region *rp, struct bu_vls *matparm, void **dpp, const struct mfuncs *mfp, struct rt_i *rtip);
static int grass_render(struct application *ap, const struct partition *pp, struct shadework *swp, void *dp);
static void grass_print(register struct region *rp, void *dp);
static void grass_free(void *cp);
/* The "mfuncs" structure defines the external interface to the shader.
* Note that more than one shader "name" can be associated with a given
* shader by defining more than one mfuncs struct in this array.
* See sh_phong.c for an example of building more than one shader "name"
* from a set of source functions. There you will find that "glass" "mirror"
* and "plastic" are all names for the same shader with different default
* values for the parameters.
*/
struct mfuncs grass_mfuncs[] = {
{MF_MAGIC, "grass", 0, MFI_NORMAL|MFI_HIT|MFI_UV, MFF_PROC, grass_setup, grass_render, grass_print, grass_free },
{0, (char *)0, 0, 0, 0, 0, 0, 0, 0 }
};
/* fraction of total allowed returned */
static double
plants_this_cell(long int *cell, struct grass_specific *grass_sp)
/* integer cell number */
{
point_t c;
double val;
VSCALE(c, cell, grass_sp->size); /* int/float conv */
VADD2(c, c, grass_sp->delta);
val = fabs(bn_noise_fbm(c, grass_sp->h_val, grass_sp->lacunarity,
grass_sp->octaves));
CLAMP(val, 0.0, 1.0);
return val;
}
static void
print_plant(char *str, const struct plant *plant)
{
int blade, seg;
bu_log("%s: %d blades\n", str, plant->blades);
bu_log(" root: %g %g %g\n", V3ARGS(plant->root));
for (blade=0; blade < plant->blades; blade++) {
bu_log(" blade %d segs:%d tot_len:%g\n", blade, plant->b[blade].segs, plant->b[blade].tot_len);
/* this printing is separated in two to avoid a nasty -O bug in gcc 2.95.2 */
bu_log(" min:%g %g %g", V3ARGS(plant->b[blade].pmin));
bu_log(" max:%g %g %g\n", V3ARGS(plant->b[blade].pmax));
for (seg=0; seg < plant->b[blade].segs; seg++) {
/* this printing is separated in two to avoid a nasty -O bug in gcc 2.95.2 */
bu_log(" leaf[%d](%g %g %g)", seg, V3ARGS(plant->b[blade].leaf[seg].blade));
bu_log(" %g\n", plant->b[blade].leaf[seg].len);
}
}
}
/*
* Rotate a blade about the Z axis, compute blade bounding box
*
*/
static void
blade_rot(struct blade *o, struct blade *i, fastf_t *m, const fastf_t *root)
{
struct blade tmp;
int seg;
point_t pt;
if (i == o) {
tmp = *i; /* struct copy */
i = &tmp;
}
VMOVE(pt, root);
VMOVE(o->pmin, root);
VMOVE(o->pmax, root);
o->segs = i->segs;
o->tot_len = 0.0;
for (seg=0; seg < i->segs; seg++) {
o->leaf[seg].magic = i->leaf[seg].magic;
MAT4X3VEC(o->leaf[seg].blade, m, i->leaf[seg].blade);
MAT4X3VEC(o->leaf[seg].N, m, i->leaf[seg].N);
o->leaf[seg].len = i->leaf[seg].len;
o->tot_len += i->leaf[seg].len;
VJOIN1(pt, pt, o->leaf[seg].len, o->leaf[seg].blade);
VMINMAX(o->pmin, o->pmax, pt);
}
}
static void
plant_rot(struct plant *pl, double a)
{
int blade;
mat_t m;
bn_mat_zrot(m, sin(a), cos(a));
for (blade=0; blade < pl->blades; blade++) {
blade_rot(&pl->b[blade], &pl->b[blade], m, pl->root);
}
}
/*
* decide how many blades to use, and how long the blades will be
*
*/
static void
plant_scale(struct plant *pl, double w)
/* 0..1, */
{
int blade, seg;
double d;
if (optical_debug&OPTICAL_DEBUG_SHADE)
bu_log("plant_scale(%g)\n", w);
d = 1.0 - w;
/* decide the number of blades */
if (d < .8) {
pl->blades -= d * pl->blades * .5;
CLAMP(pl->blades, 1, BLADE_LAST);
}
for (blade=0; (blade < pl->blades && blade < BLADE_MAX); blade++) {
pl->b[blade].tot_len = 0.0;
if (blade != BLADE_LAST)
pl->b[blade].width *= d;
else
d *= d;
for (seg=0; seg < pl->b[blade].segs; seg++) {
pl->b[blade].leaf[seg].len *= d;
pl->b[blade].tot_len += pl->b[blade].leaf[seg].len;
}
}
}
/*
* Make a prototype blade we can copy for use later
* Doesn't set bounding box.
*/
static void
make_proto(struct grass_specific *grass_sp)
{
static const point_t z_axis = { 0.0, 0.0, 1.0 };
vect_t left;
int blade, seg;
mat_t m, r;
double start_angle;
double seg_delta_angle;
double angle;
double val, tmp;
double seg_len;
grass_sp->proto.magic = PLANT_MAGIC;
VSETALL(grass_sp->proto.root, 0.0);
VMOVE(grass_sp->proto.pmin, grass_sp->proto.root);
VMOVE(grass_sp->proto.pmax, grass_sp->proto.root);
grass_sp->proto.blades = BLADE_MAX;
/* First we make blade 0. This blade will be used as the prototype
* for all the other blades. Most significantly, the others are just
* a rotation/scale of this first one.
*/
bn_mat_zrot(r, sin(DEG2RAD*137.0), cos(DEG2RAD*137.0));
MAT_COPY(m, r);
seg_delta_angle = (87.0 / (double)BLADE_SEGS_MAX);
for (blade=0; blade < BLADE_LAST; blade++) {
val = (double)blade / (double)(BLADE_LAST);
grass_sp->proto.b[blade].magic = BLADE_MAGIC;
grass_sp->proto.b[blade].tot_len = 0.0;
grass_sp->proto.b[blade].width = grass_sp->blade_width;
grass_sp->proto.b[blade].segs = BLADE_SEGS_MAX; /* - (val*BLADE_SEGS_MAX*.25) */
/* pick a start angle for the first segment */
start_angle = 55.0 + 30.0 * (1.0-val);
seg_len = grass_sp->t / grass_sp->proto.b[blade].segs;
for (seg=0; seg < grass_sp->proto.b[blade].segs; seg++) {
grass_sp->proto.b[blade].leaf[seg].magic = LEAF_MAGIC;
angle = start_angle - (double)seg * seg_delta_angle;
angle *= DEG2RAD;
VSET(grass_sp->proto.b[blade].leaf[seg].blade,
cos(angle), 0.0, sin(angle));
/* pick a length for the blade */
tmp = (double)seg / (double)BLADE_SEGS_MAX;
/* magic */
grass_sp->proto.b[blade].leaf[seg].len = seg_len * .25 + tmp * (seg_len*1.75);
grass_sp->proto.b[blade].tot_len += grass_sp->proto.b[blade].leaf[seg].len;
VUNITIZE(grass_sp->proto.b[blade].leaf[seg].blade);
VCROSS(left, grass_sp->proto.b[blade].leaf[seg].blade, z_axis);
VUNITIZE(left);
VCROSS(grass_sp->proto.b[blade].leaf[seg].N,
left, grass_sp->proto.b[blade].leaf[seg].blade);
VUNITIZE(grass_sp->proto.b[blade].leaf[seg].N);
}
blade_rot(&grass_sp->proto.b[blade], &grass_sp->proto.b[blade], m, grass_sp->proto.root);
bn_mat_mul2(r, m);
}
/* The central stalk is a bit different. It's basically a straight tall
* shaft
*/
blade = BLADE_LAST;
grass_sp->proto.b[blade].magic = BLADE_MAGIC;
grass_sp->proto.b[blade].tot_len = 0.0;
grass_sp->proto.b[blade].segs = BLADE_SEGS_MAX;
grass_sp->proto.b[blade].width = grass_sp->blade_width * 0.5;
seg_len = .75 * grass_sp->t / grass_sp->proto.b[blade].segs;
val = .9;
for (seg=0; seg < grass_sp->proto.b[blade].segs; seg++) {
tmp = (double)seg / (double)BLADE_SEGS_MAX;
grass_sp->proto.b[blade].leaf[seg].magic = LEAF_MAGIC;
VSET(grass_sp->proto.b[blade].leaf[seg].blade, 0.0, .1, val);
VUNITIZE(grass_sp->proto.b[blade].leaf[seg].blade);
grass_sp->proto.b[blade].leaf[seg].len = seg_len;
grass_sp->proto.b[blade].tot_len += grass_sp->proto.b[blade].leaf[seg].len;
VCROSS(left, grass_sp->proto.b[blade].leaf[seg].blade, z_axis);
VUNITIZE(left);
VCROSS(grass_sp->proto.b[blade].leaf[seg].N,
left, grass_sp->proto.b[blade].leaf[seg].blade);
VUNITIZE(grass_sp->proto.b[blade].leaf[seg].N);
val -= tmp * .4;
}
if (optical_debug&OPTICAL_DEBUG_SHADE) {
print_plant("proto", &grass_sp->proto);
}
}
/*
* This routine is called (at prep time)
* once for each region which uses this shader.
* Any shader-specific initialization should be done here.
*/
static int
grass_setup(register struct region *rp, struct bu_vls *matparm, void **dpp, const struct mfuncs *UNUSED(mfp), struct rt_i *rtip)
/* pointer to reg_udata in *rp */
/* New since 4.4 release */
{
register struct grass_specific *grass_sp;
/* check the arguments */
RT_CHECK_RTI(rtip);
BU_CK_VLS(matparm);
RT_CK_REGION(rp);
if (optical_debug&OPTICAL_DEBUG_SHADE)
bu_log("grass_setup(%s)\n", rp->reg_name);
/* Get memory for the shader parameters and shader-specific data */
BU_GET(grass_sp, struct grass_specific);
*dpp = grass_sp;
/* initialize the default values for the shader */
memcpy(grass_sp, &grass_defaults, sizeof(struct grass_specific));
if (rp->reg_aircode == 0) {
bu_log("%s\n%s\n",
"*** WARNING: grass shader applied to non-air region!!! ***",
"Set air flag with 'edcodes' in mged");
bu_bomb("grass shader applied improperly");
}
/* parse the user's arguments for this use of the shader. */
if (bu_struct_parse(matparm, grass_parse_tab, (char *)grass_sp, NULL) < 0)
return -1;
/* The shader needs to operate in a coordinate system which stays
* fixed on the region when the region is moved (as in animation).
* We need to get a matrix to perform the appropriate transform(s).
*/
db_region_mat(grass_sp->m_to_sh, rtip->rti_dbip, rp->reg_name);
bn_mat_inv(grass_sp->sh_to_m, grass_sp->m_to_sh);
if (optical_debug&OPTICAL_DEBUG_SHADE) {
bu_struct_print(" Parameters:", grass_print_tab, (char *)grass_sp);
bn_mat_print("m_to_sh", grass_sp->m_to_sh);
bn_mat_print("sh_to_m", grass_sp->sh_to_m);
}
if (grass_sp->proto.magic != PLANT_MAGIC) {
make_proto(grass_sp);
}
return 1;
}
static void
grass_print(register struct region *rp, void *dp)
{
bu_struct_print(rp->reg_name, grass_print_tab, (char *)dp);
}
static void
grass_free(void *cp)
{
BU_PUT(cp, struct grass_specific);
}
static void
plot_bush(struct plant *pl, struct grass_ray *r)
{
int blade, seg;
point_t pt;
bu_semaphore_acquire(BU_SEM_SYSCALL);
pl_color(r->fd, 150, 250, 150);
for (blade=0; blade < pl->blades; blade++) {
VMOVE(pt, pl->root);
pdv_3move(r->fd, pt);
for (seg=0; seg < pl->b[blade].segs; seg++) {
VJOIN1(pt, pt, pl->b[blade].leaf[seg].len,
pl->b[blade].leaf[seg].blade);
pdv_3cont(r->fd, pt);
}
}
/* plot bounding Box */
pl_color(r->fd, 100, 200, 100);
pdv_3move(r->fd, pl->pmin);
pd_3cont(r->fd, pl->pmin[X], pl->pmin[Y], pl->pmin[Z]);
pd_3cont(r->fd, pl->pmax[X], pl->pmin[Y], pl->pmin[Z]);
pd_3cont(r->fd, pl->pmax[X], pl->pmax[Y], pl->pmin[Z]);
pd_3cont(r->fd, pl->pmin[X], pl->pmax[Y], pl->pmin[Z]);
pd_3cont(r->fd, pl->pmin[X], pl->pmin[Y], pl->pmin[Z]);
pd_3cont(r->fd, pl->pmin[X], pl->pmin[Y], pl->pmax[Z]);
pd_3cont(r->fd, pl->pmax[X], pl->pmin[Y], pl->pmax[Z]);
pd_3cont(r->fd, pl->pmax[X], pl->pmax[Y], pl->pmax[Z]);
pd_3cont(r->fd, pl->pmin[X], pl->pmax[Y], pl->pmax[Z]);
pd_3cont(r->fd, pl->pmin[X], pl->pmin[Y], pl->pmax[Z]);
pl_color(r->fd, 255, 255, 255);
bu_semaphore_release(BU_SEM_SYSCALL);
}
static void
make_bush(struct plant *pl, double seed, const fastf_t *cell_pos, const struct grass_specific *grass_sp, double w, struct grass_ray *r)
/* derived from cell_num */
/* cell specific weight for count, height */
{
point_t pt;
int blade, seg;
unsigned idx;
if (optical_debug&OPTICAL_DEBUG_SHADE)
bu_log("make_bush(%g, ... %g)\n", seed, w);
CK_grass_SP(grass_sp);
*pl = grass_sp->proto; /* struct copy */
/* get coordinates for the plant root within the cell */
VMOVE(pl->root, cell_pos);
pl->root[Z] = 0.0;
BN_RANDSEED(idx, seed);
pl->root[X] += BN_RANDOM(idx) * grass_sp->cell[X];
pl->root[Y] += BN_RANDOM(idx) * grass_sp->cell[Y];
/* set up for bounding box computation */
VADD2(pl->pmin, pl->pmin, pl->root);
VADD2(pl->pmax, pl->pmax, pl->root);
VSCALE(pt, pl->root, grass_sp->size);
plant_scale(pl, w); /* must come first */
plant_rot(pl, BN_RANDOM(idx) * M_2PI);/* computes bounding box */
/* set bounding boxes */
for (blade=0; blade < pl->blades; blade++) {
VMOVE(pt, pl->root);
for (seg=0; seg < pl->b[blade].segs; seg++) {
VJOIN1(pt, pt, pl->b[blade].leaf[seg].len,
pl->b[blade].leaf[seg].blade);
VMINMAX(pl->b[blade].pmin, pl->b[blade].pmax, pt);
}
VMINMAX(pl->pmin, pl->pmax, pl->b[blade].pmin);
VMINMAX(pl->pmin, pl->pmax, pl->b[blade].pmax);
}
if (optical_debug&OPTICAL_DEBUG_SHADE && r->fd) plot_bush(pl, r);
}
/* Intersect ray with leaf segment. We already know we're within
* max width of the segment.
*
*/
static void
hit_blade(const struct blade *UNUSED(bl), struct grass_ray *r, struct shadework *UNUSED(swp), const struct grass_specific *grass_sp, int UNUSED(seg), fastf_t *UNUSED(ldist), int UNUSED(blade_num), double UNUSED(fract))
/* defined in material.h */
{
CK_grass_SP(grass_sp);
BU_CKMAG(r, GRASSRAY_MAGIC, "grass_ray");
/* get the hit point/PCA */
if (optical_debug&OPTICAL_DEBUG_SHADE)
bu_log("\t hit_blade()\n");
r->occlusion = 1.0;
return;
}
/* intersect ray with leaves of single blade
*
*/
static void
isect_blade(const struct blade *bl, const fastf_t *root, struct grass_ray *r, struct shadework *swp, const struct grass_specific *grass_sp, int blade_num)
/* defined in material.h */
{
fastf_t ldist[2];
point_t pt;
int cond;
int seg;
point_t PCA_ray;
double PCA_ray_radius;
point_t PCA_grass;
vect_t tmp;
double dist;
double accum_len;/* accumulated distance along blade from prev segs */
double fract; /* fraction of total blade length to PCA */
double blade_width;/* width of blade at PCA with ray */
CK_grass_SP(grass_sp);
BU_CKMAG(r, GRASSRAY_MAGIC, "grass_ray");
if (optical_debug&OPTICAL_DEBUG_SHADE)
bu_log("\t isect_blade()\n");
BU_CKMAG(bl, BLADE_MAGIC, "blade");
VMOVE(pt, root);
accum_len = 0.0;
for (seg=0; seg < bl->segs; accum_len += bl->leaf[seg].len) {
BU_CKMAG(&bl->leaf[seg].magic, LEAF_MAGIC, "leaf");
cond = bg_dist_line3_line3(ldist, r->r.r_pt, r->r.r_dir,
pt, bl->leaf[seg].blade, &r->tol);
if (optical_debug&OPTICAL_DEBUG_SHADE) {
bu_log("\t ");
switch (cond) {
case -2: bu_log("lines parallel "); break;
case -1: bu_log("lines collinear "); break;
case 0: bu_log("lines intersect "); break;
case 1: bu_log("lines miss "); break;
}
bu_log("d1:%d d2:%g %g\n", cond, V2ARGS(ldist));
}
if (ldist[0] < 0.0 /* behind ray */ ||
ldist[0] >= r->d_max /* beyond out point */ ||
ldist[1] < 0.0 /* under ground */ ||
ldist[1] > bl->leaf[seg].len/* beyond end of seg */
) goto iter;
VJOIN1(PCA_ray, r->r.r_pt, ldist[0], r->r.r_dir);
PCA_ray_radius = r->radius + ldist[0] * r->diverge;
VJOIN1(PCA_grass, pt, ldist[1], bl->leaf[seg].blade);
VSUB2(tmp, PCA_grass, PCA_ray);
dist = MAGNITUDE(tmp);
/* We want to narrow the blade of grass toward the tip.
* So we scale the width of the blade based upon the
* fraction of total blade length to PCA.
*/
fract = (accum_len + ldist[1]) / bl->tot_len;
if (blade_num < BLADE_LAST) {
blade_width = bl->width * (1.0 - fract);
} else {
blade_width = .5 * bl->width * (1.0 - fract);
}
if (dist < (PCA_ray_radius+blade_width)) {
if (optical_debug&OPTICAL_DEBUG_SHADE)
bu_log("\thit grass: %g < (%g + %g)\n",
dist, PCA_ray_radius,
bl->width);
hit_blade(bl, r, swp, grass_sp, seg, ldist,
blade_num, fract);
if (r->occlusion >= 1.0) return;
}
if (optical_debug&OPTICAL_DEBUG_SHADE) bu_log("\t (missed aside)\n");
iter:
/* compute origin of NEXT leaf segment */
VJOIN1(pt, pt, bl->leaf[seg].len, bl->leaf[seg].blade);
seg++;
}
}
static void
isect_plant(const struct plant *pl, struct grass_ray *r, struct shadework *swp, const struct grass_specific *grass_sp)
/* defined in material.h */
{
int i;
CK_grass_SP(grass_sp);
BU_CKMAG(r, GRASSRAY_MAGIC, "grass_ray");
BU_CKMAG(pl, PLANT_MAGIC, "plant");
if (optical_debug&OPTICAL_DEBUG_SHADE) {
bu_log("isect_plant()\n");
print_plant("plant", pl);
}
r->r.r_min = r->r.r_max = 0.0;
if (! rt_in_rpp(&r->r, r->rev, pl->pmin, pl->pmax)) {
if (optical_debug&OPTICAL_DEBUG_SHADE) {
point_t in_pt, out_pt;
bu_log("min:%g max:%g\n", r->r.r_min, r->r.r_max);
bu_log("ray %g %g %g->%g %g %g misses:\n\trpp %g %g %g, %g %g %g\n",
V3ARGS(r->r.r_pt), V3ARGS(r->r.r_dir),
V3ARGS(pl->pmin), V3ARGS(pl->pmax));
VJOIN1(in_pt, r->r.r_pt, r->r.r_min, r->r.r_dir);
VPRINT("\tin_pt", in_pt);
VJOIN1(out_pt, r->r.r_pt, r->r.r_max, r->r.r_dir);
VPRINT("\tout_pt", out_pt);
bu_log("MISSED BBox\n");
}
return;
} else {
if (optical_debug&OPTICAL_DEBUG_SHADE) {
point_t in_pt, out_pt;
bu_log("min:%g max:%g\n", r->r.r_min, r->r.r_max);
bu_log("ray %g %g %g->%g %g %g hit:\n\trpp %g %g %g, %g %g %g\n",
V3ARGS(r->r.r_pt),
V3ARGS(r->r.r_dir),
V3ARGS(pl->pmin),
V3ARGS(pl->pmax));
VJOIN1(in_pt, r->r.r_pt, r->r.r_min, r->r.r_dir);
VPRINT("\tin_pt", in_pt);
VJOIN1(out_pt, r->r.r_pt, r->r.r_max, r->r.r_dir);
VPRINT("\tout_pt", out_pt);
bu_log("HIT BBox\n");
}
}
for (i=0; i < pl->blades; i++) {
isect_blade(&pl->b[i], pl->root, r, swp, grass_sp, i);
if (r->occlusion >= 1.0)
return;
}
}
static int
stat_cell(fastf_t *UNUSED(cell_pos), struct grass_ray *r, struct grass_specific *grass_sp, struct shadework *swp, double dist_to_cell, double radius)
/* origin of cell in region coordinates */
/* radius of ray */
{
point_t tmp;
vect_t color;
double h;
double ratio = grass_sp->blade_width / radius;
/* the ray is *large* so just pick something appropriate */
CK_grass_SP(grass_sp);
BU_CKMAG(r, GRASSRAY_MAGIC, "grass_ray");
if (optical_debug&OPTICAL_DEBUG_SHADE)
bu_log("statistical bailout\n");
r->hit.hit_dist = dist_to_cell;
VJOIN1(r->hit.hit_point, r->r.r_pt, dist_to_cell, r->r.r_dir);
/* compute color at this point */
h = r->hit.hit_point[Z] / 400.0;
VSCALE(color, swp->sw_basecolor, 1.0 - h);
VJOIN1(color, color, h, grass_sp->brown);
if (VEQUAL(swp->sw_color, swp->sw_basecolor)) {
VSCALE(swp->sw_color, color, ratio);
swp->sw_transmit -= ratio;
} else {
VJOIN1(swp->sw_color, swp->sw_color, ratio, grass_sp->brown);
swp->sw_transmit -= ratio;
}
VADD2(tmp, r->hit.hit_point, grass_sp->delta);
bn_noise_vec(tmp, r->hit.hit_normal);
if (r->hit.hit_normal[Z] < 0.0) r->hit.hit_normal[Z] *= -1.0;
VUNITIZE(r->hit.hit_normal);
if (VDOT(r->hit.hit_normal, r->r.r_dir) > 0.0) {
VREVERSE(r->hit.hit_normal, r->hit.hit_normal);
}
if (swp->sw_transmit < .05)
return SHADE_ABORT_GRASS;
else
return SHADE_CONT;
}
static void
plot_cell(long int *cell, struct grass_ray *r, struct grass_specific *grass_sp)
/* cell number (such as 5, 3) */
{
point_t cell_pos;
CK_grass_SP(grass_sp);
CELL_POS(cell_pos, grass_sp, cell);
bu_log("plotting cell %ld, %ld (%g, %g) %g %g\n",
V2ARGS(cell), V2ARGS(cell_pos), V2ARGS(grass_sp->cell));
bu_semaphore_acquire(BU_SEM_SYSCALL);
pl_color(r->fd, 100, 100, 200);
pd_3move(r->fd, cell_pos[X], cell_pos[Y], 0.0);
pd_3cont(r->fd, cell_pos[X]+grass_sp->cell[X], cell_pos[Y], 0.0);
pd_3cont(r->fd, cell_pos[X]+grass_sp->cell[X], cell_pos[Y]+grass_sp->cell[Y], 0.0);
pd_3cont(r->fd, cell_pos[X], cell_pos[Y]+grass_sp->cell[Y], 0.0);
pd_3cont(r->fd, cell_pos[X], cell_pos[Y], 0.0);
pl_color(r->fd, 255, 255, 255);
bu_semaphore_release(BU_SEM_SYSCALL);
}
/*
* Intersects a region-space ray with a grid cell of grass.
*
*/
static void
isect_cell(long int *cell, struct grass_ray *r, struct shadework *swp, double out_dist, struct grass_specific *grass_sp, double curr_dist)
/* cell number (such as 5, 3) */
{
point_t c = {0.0, 0.0, 0.0}; /* float version of cell # */
point_t cell_pos = {0.0, 0.0, 0.0}; /* origin of cell in region coordinates */
double val;
vect_t v;
int p; /* current plant number (loop variable) */
int ppc; /* # plants in this cell */
struct plant pl;
double dist_to_cell;
CK_grass_SP(grass_sp);
if (optical_debug&OPTICAL_DEBUG_SHADE) {
static int plot_num = 0;
char buf[32];
point_t cell_in_pt;
point_t cell_out_pt; /* not really */
bu_log("isect_cell(%ld, %ld)\n", V2ARGS(cell));
bu_semaphore_acquire(BU_SEM_SYSCALL);
sprintf(buf, "g_ray%d, %d_%d_cell%ld, %ld_.plot3",
r->ap->a_x, r->ap->a_y, plot_num++, cell[0], cell[1]);
r->fd = fopen(buf, "wb");
if (r->fd) {
if (swp->sw_xmitonly)
pl_color(r->fd, 255, 255, 55);
else
pl_color(r->fd, 255, 55, 55);
VJOIN1(cell_in_pt, r->r.r_pt, curr_dist, r->r.r_dir);
VJOIN1(cell_out_pt, r->r.r_pt, out_dist, r->r.r_dir);
pdv_3move(r->fd, cell_in_pt);
pdv_3cont(r->fd, cell_out_pt);
pl_color(r->fd, 255, 255, 255);
}
bu_semaphore_release(BU_SEM_SYSCALL);
if (r->fd) plot_cell(cell, r, grass_sp);
}
/* get coords of cell */
CELL_POS(cell_pos, grass_sp, cell);
VSUB2(v, cell_pos, r->r.r_pt);
dist_to_cell = MAGNITUDE(v);
/* radius of ray at cell origin */
val = r->radius + r->diverge * dist_to_cell;
if (optical_debug&OPTICAL_DEBUG_SHADE)
bu_log("\t ray radius @cell %g = %g, %g, %g (%g)\n\t cell:%g, %g\n",
val, r->radius, r->diverge, dist_to_cell, val*32.0,
V2ARGS(grass_sp->cell));
if (val > grass_sp->blade_width * 3) {
stat_cell(cell_pos, r, grass_sp, swp, dist_to_cell, val);
return;
}
/* Figure out how many plants are in this cell */
val = plants_this_cell(cell, grass_sp);