1 |
#!/usr/bin/perl -w |
2 |
# RCSid $Id: rtpict.pl,v 2.32 2025/09/13 16:52:28 greg Exp $ |
3 |
# |
4 |
# Run rtrace in parallel mode to simulate rpict -n option |
5 |
# May also be used to render layered images with -o* option |
6 |
# |
7 |
# G. Ward |
8 |
# |
9 |
use strict; |
10 |
my $windoz = ($^O eq "MSWin32" or $^O eq "MSWin64"); |
11 |
# we'll run rpict if no -n or -o* option |
12 |
my $nprocs = 1; |
13 |
# rtrace options and the associated number of arguments |
14 |
my %rtraceC = ('-dt',1, '-dc',1, '-dj',1, '-ds',1, '-dr',1, '-dp',1, |
15 |
'-ss',1, '-st',1, '-e',1, '-am',1, '-P',1, '-PP',1, |
16 |
'-ab',1, '-af',1, '-ai',1, '-aI',1, '-ae',1, '-aE',1, |
17 |
'-av',3, '-aw',1, '-aa',1, '-ar',1, '-ad',1, '-as',1, |
18 |
'-me',3, '-ma',3, '-mg',1, '-ms',1, '-lr',1, '-lw',1, |
19 |
'-ap',2, '-am',1, '-ac',1, '-aC',1, |
20 |
'-cs',1, '-cw',2, '-pc',8, '-pXYZ',0); |
21 |
# boolean rtrace options |
22 |
my @boolO = ('-w', '-bv', '-dv', '-i', '-u', '-co'); |
23 |
# view options and the associated number of arguments |
24 |
my %vwraysC = ('-vf',1, '-vtv',0, '-vtl',0, '-vth',0, '-vta',0, '-vts',0, '-vtc',0, |
25 |
'-x',1, '-y',1, '-vp',3, '-vd',3, '-vu',3, '-vh',1, '-vv',1, |
26 |
'-vo',1, '-va',1, '-vs',1, '-vl',1, '-pa',1, '-pj',1, '-pd',1); |
27 |
# options we need to silently ignore |
28 |
my %ignoreC = ('-t',1, '-ps',1, '-pt',1, '-pm',1,); |
29 |
# Starting options for rtrace (rpict values) |
30 |
my @rtraceA = split(' ', 'rtrace -u- -dt .05 -dc .5 -ds .25 -dr 1 ' . |
31 |
'-aa .2 -ar 64 -ad 512 -as 128 -lr 7 -lw 1e-04'); |
32 |
my @vwraysA = ('vwrays', '-pj', '.67'); |
33 |
my @vwrightA = ('vwright', '-vtv'); |
34 |
my @rpictA = ('rpict', '-ps', '1'); |
35 |
my @pvalueA = ('pvalue', '-r'); |
36 |
my $outpatt = '^-o[vrxlLRXnNsmM]+'; |
37 |
my $refDepth = ""; |
38 |
my $irrad = 0; |
39 |
my $specout = 0; |
40 |
my $ncsamp = 3; |
41 |
my $persist = 0; |
42 |
my $ambounce = 0; |
43 |
my $ambcache = 1; |
44 |
my $ambfile; |
45 |
my $outlyr; |
46 |
my $outdir; |
47 |
my $outpic; |
48 |
my $outzbf; |
49 |
OPTION: # sort through options |
50 |
while ($#ARGV >= 0 && "$ARGV[0]" =~ /^[-\@]/) { |
51 |
# Check for file inclusion |
52 |
if ("$ARGV[0]" =~ /^\@/) { |
53 |
open my $handle, '<', substr($ARGV[0], 1) or die "No file for $ARGV[0]\n"; |
54 |
shift @ARGV; |
55 |
chomp(my @args = <$handle>); |
56 |
close $handle; |
57 |
unshift @ARGV, split(/\s+/, "@args"); |
58 |
next OPTION; |
59 |
} |
60 |
# Check booleans |
61 |
for my $boopt (@boolO) { |
62 |
if ("$ARGV[0]" =~ ('^' . $boopt . '[-+01tfynTFYN]?$')) { |
63 |
if ("$ARGV[0]" eq '-i') { |
64 |
$irrad = ! $irrad; |
65 |
} elsif ("$ARGV[0]" =~ /^-i[-+01tfynTFYN]/) { |
66 |
$irrad = ("$ARGV[0]" =~ /^-i[+1tyTY]/); |
67 |
} elsif ("$ARGV[0]" eq '-co') { |
68 |
$specout = ! $specout; |
69 |
} elsif ("$ARGV[0]" =~ /^-co[-+01tfynTFYN]/) { |
70 |
$specout = ("$ARGV[0]" =~ /^-co[+1tyTY]/); |
71 |
} |
72 |
push(@rpictA, $ARGV[0]) if ("$ARGV[0]" !~ /^-co/); |
73 |
push @rtraceA, shift(@ARGV); |
74 |
next OPTION; |
75 |
} |
76 |
} |
77 |
# Check view options |
78 |
if (defined $vwraysC{$ARGV[0]}) { |
79 |
push @vwraysA, $ARGV[0]; |
80 |
my $isvopt = ("$ARGV[0]" =~ /^-v/); |
81 |
push(@vwrightA, $ARGV[0]) if ($isvopt); |
82 |
push @rpictA, shift(@ARGV); |
83 |
for (my $i = $vwraysC{$rpictA[-1]}; $i-- > 0; ) { |
84 |
push @vwraysA, $ARGV[0]; |
85 |
push(@vwrightA, $ARGV[0]) if ($isvopt); |
86 |
push @rpictA, shift(@ARGV); |
87 |
} |
88 |
next OPTION; |
89 |
} |
90 |
# Check rtrace options |
91 |
if (defined $rtraceC{$ARGV[0]}) { |
92 |
if ("$ARGV[0]" =~ /^-PP?$/) { |
93 |
$persist = 1; |
94 |
} elsif ("$ARGV[0]" eq '-ab') { |
95 |
$ambounce = $ARGV[1]; |
96 |
} elsif ("$ARGV[0]" eq '-aa') { |
97 |
$ambcache = ($ARGV[1] > 0.0); |
98 |
} elsif ("$ARGV[0]" eq '-af') { |
99 |
$ambfile = "$ARGV[1]"; |
100 |
} elsif ("$ARGV[0]" eq '-pXYZ') { |
101 |
push @pvalueA, $ARGV[0]; |
102 |
$specout = 0; |
103 |
} elsif ("$ARGV[0]" eq '-pc') { |
104 |
push @pvalueA, '-p'; |
105 |
push @pvalueA, @ARGV[1..8]; |
106 |
$specout = 0; |
107 |
} elsif ("$ARGV[0]" eq '-cs') { |
108 |
$ncsamp = $ARGV[1]; |
109 |
} |
110 |
push @rtraceA, $ARGV[0]; |
111 |
push @rpictA, shift(@ARGV); |
112 |
for (my $i = $rtraceC{$rpictA[-1]}; $i-- > 0; ) { |
113 |
push @rtraceA, $ARGV[0]; |
114 |
push @rpictA, shift(@ARGV); |
115 |
} |
116 |
next OPTION; |
117 |
} |
118 |
# Check options to ignore |
119 |
if (defined $ignoreC{$ARGV[0]}) { |
120 |
push @rpictA, shift(@ARGV); |
121 |
for (my $i = $ignoreC{$rpictA[-1]}; $i-- > 0; ) { |
122 |
push @rpictA, shift(@ARGV); |
123 |
} |
124 |
next OPTION; |
125 |
} |
126 |
# Check for output file or number of processes |
127 |
if ("$ARGV[0]" eq '-o') { |
128 |
shift @ARGV; |
129 |
$outpic = shift(@ARGV); |
130 |
} elsif ("$ARGV[0]" eq '-z') { |
131 |
push @rpictA, shift(@ARGV); |
132 |
$outzbf = $ARGV[0]; |
133 |
push @rpictA, shift(@ARGV); |
134 |
} elsif ("$ARGV[0]" eq '-n') { |
135 |
shift @ARGV; |
136 |
$nprocs = shift(@ARGV); |
137 |
} elsif ("$ARGV[0]" eq '-d') { |
138 |
shift @ARGV; |
139 |
$refDepth = ' -d ' . shift(@ARGV); |
140 |
} elsif ("$ARGV[0]" =~ "$outpatt") { |
141 |
push @rtraceA, $ARGV[0]; |
142 |
$outlyr = substr(shift(@ARGV), 2); |
143 |
$outdir = shift(@ARGV); |
144 |
} else { |
145 |
die "Unsupported option: " . $ARGV[0] . "\n"; |
146 |
} |
147 |
} |
148 |
die "-o* option cannot contain spaces\n" if ($windoz && $outdir =~ /[ ]/); |
149 |
die "Number of processes must be positive\n" if ($nprocs <= 0); |
150 |
if ($windoz && $nprocs > 1) { |
151 |
print STDERR "Only one process supported under Windows\n"; |
152 |
$nprocs = 1; |
153 |
} |
154 |
if ($specout) { |
155 |
$specout = ($ncsamp > 3); |
156 |
} |
157 |
if (defined $outdir) { # check conflicting options |
158 |
die "Options -o and -o* are mutually exclusive\n" if (defined $outpic); |
159 |
die "Options -z and -o* are mutually exclusive\n" if (defined $outzbf); |
160 |
} |
161 |
if (defined $outpic) { # redirect output? |
162 |
die "File '$outpic' already exists\n" if (-e $outpic); |
163 |
open STDOUT, '>', "$outpic"; |
164 |
} |
165 |
##################################################################### |
166 |
##### May as well run rpict? |
167 |
if ($specout == 0 && $nprocs == 1 && $persist == 0 && !defined($outdir)) { |
168 |
push(@rpictA, $ARGV[0]) if ($#ARGV == 0); |
169 |
exec @rpictA ; |
170 |
} |
171 |
# OK, we're running rtrace in some capacity... |
172 |
push @rtraceA, ('-n', "$nprocs"); |
173 |
die "Need single octree argument\n" if ($#ARGV != 0); |
174 |
my $oct = $ARGV[0]; |
175 |
my $view = `@vwrightA 0`; |
176 |
chomp $view; |
177 |
my @res = split(/\s/, `@vwraysA -d`); |
178 |
##################################################################### |
179 |
##### Resort pixels to reduce ambient cache collisions? |
180 |
if ($nprocs > 1 && $ambounce > 0 && $ambcache && defined($ambfile)) { |
181 |
if ($specout == 0 && !defined($outzbf) && !defined($outdir)) { |
182 |
# Straight picture output, so just shuffle sample order |
183 |
system "cnt -s $res[1] $res[3] > /tmp/ord$$.txt"; |
184 |
die "cnt error\n" if ( $? ); |
185 |
system "@vwraysA -ff -i < /tmp/ord$$.txt " . |
186 |
"| @rtraceA -ffa -ov '$oct' > /tmp/pix$$.txt"; |
187 |
die "Error running rtrace\n" if ( $? ); |
188 |
system "( getinfo < /tmp/pix$$.txt | getinfo -r NCOMP " . |
189 |
"| getinfo -a 'VIEW=$view'; " . |
190 |
"getinfo - < /tmp/pix$$.txt | rlam /tmp/ord$$.txt - " . |
191 |
"| sort -k2rn -k1n ) | @pvalueA -Y $res[3] +X $res[1]"; |
192 |
die "rlam error\n" if ( $? ); |
193 |
unlink ("/tmp/ord$$.txt", "/tmp/pix$$.txt"); |
194 |
exit 0; |
195 |
} |
196 |
# Else randomize overture calculation to prime ambient cache |
197 |
my @ores = (int($res[1]/6), int($res[3]/6)); |
198 |
print STDERR "Running $ores[0] by $ores[1] overture calculation " . |
199 |
"to populate '$ambfile'...\n"; |
200 |
system "cnt -s @ores | @vwraysA -i -ff -x $ores[0] -y $ores[1] -pj 0 " . |
201 |
"| @rtraceA -ff -ov -pY '$oct' > /dev/null"; |
202 |
die "Failure running overture\n" if ( $? ); |
203 |
print STDERR "Finished overture.\n"; |
204 |
} |
205 |
##################################################################### |
206 |
##### Generating picture with depth buffer? |
207 |
if (defined $outzbf) { |
208 |
my $picvt = $specout ? 'rcomb -fc -' : "@pvalueA -df"; |
209 |
exec qq{@vwraysA -ff | @rtraceA -fff -olv @res "$oct" | } . |
210 |
qq{rsplit -ih -iH -f -of "$outzbf" -oh -oH -of$ncsamp - | } . |
211 |
$picvt . qq{ | getinfo -a "VIEW=$view"}; |
212 |
} |
213 |
##################################################################### |
214 |
##### Base case with output picture only? |
215 |
if (! defined $outdir) { |
216 |
exec qq{@vwraysA -ff | @rtraceA -ffc @res "$oct" | getinfo -a "VIEW=$view"}; |
217 |
} |
218 |
##################################################################### |
219 |
##### Layered image output case |
220 |
my @rsplitA = ("rsplit", "'-t '", "-ih", "-iH", "-oh", "-oH"); |
221 |
# Supported rtrace -o* options and output file name.typ |
222 |
my %rtoutC = ( |
223 |
v => 'radiance.hdr', |
224 |
r => 'r_refl.hdr', |
225 |
x => 'r_unrefl.hdr', |
226 |
l => 'd_effective.dpt', |
227 |
L => 'd_firstsurf.dpt', |
228 |
R => 'd_refl.dpt', |
229 |
X => 'd_unrefl.dpt', |
230 |
n => 'perturbed.nrm', |
231 |
N => 'unperturbed.nrm', |
232 |
s => 'surface.idx', |
233 |
m => 'modifier.idx', |
234 |
M => 'material.idx' |
235 |
); |
236 |
# Arguments for rsplit based on output file type |
237 |
my %rcodeC = ( |
238 |
'.hdr', ['-of3', "!@pvalueA -df -u"], |
239 |
'.hsr', ["-of$ncsamp", '!rcomb -fc -'], |
240 |
'.dpt', ['-of', "!rcode_depth$refDepth -ff"], |
241 |
'.nrm', ['-of3', '!rcode_norm -ff'], |
242 |
'.idx', ['-oa', '!rcode_ident "-t "'] |
243 |
); |
244 |
if ($specout) { # adjust actions for -co+ option |
245 |
$rtoutC{'v'} = 'radiance.hsr'; |
246 |
$rtoutC{'r'} = 'r_refl.hsr'; |
247 |
$rtoutC{'x'} = 'r_unrefl.hsr'; |
248 |
} |
249 |
if ($irrad) { # adjust actions for -i+ option |
250 |
$rtoutC{'v'} = $specout ? 'irradiance.hsr' : 'irradiance.hdr'; |
251 |
delete $rtoutC{'r'}; |
252 |
delete $rtoutC{'x'}; |
253 |
delete $rtoutC{'R'}; |
254 |
delete $rtoutC{'X'}; |
255 |
} |
256 |
if (! -d $outdir) { |
257 |
mkdir $outdir or die("Cannot create output directory '$outdir'\n"); |
258 |
} |
259 |
# construct rsplit command |
260 |
foreach my $oval (split //, $outlyr) { |
261 |
die "Duplicate or unsupported type '$oval' in -o$outlyr\n" |
262 |
if (!defined $rtoutC{$oval}); |
263 |
my $outfile = "$outdir/$rtoutC{$oval}"; |
264 |
die "File '$outfile' already exists\n" if (-e $outfile); |
265 |
my ($otyp) = ($rtoutC{$oval} =~ /(\.[^.]+)$/); |
266 |
push @rsplitA, $rcodeC{$otyp}[0]; |
267 |
if ($windoz) { |
268 |
push @rsplitA, qq{"$rcodeC{$otyp}[1] > $outfile"}; |
269 |
} else { |
270 |
push @rsplitA, qq{'$rcodeC{$otyp}[1] > "$outfile"'}; |
271 |
} |
272 |
delete $rtoutC{$oval}; |
273 |
} |
274 |
# call rtrace + rsplit |
275 |
exec qq{@vwraysA -ff | @rtraceA -fff @res "$oct" | getinfo -a "VIEW=$view" | @rsplitA}; |