1 |
#!/opt/perl/bin/perl |
2 |
|
3 |
use Cwd; |
4 |
use Math::VectorReal; |
5 |
use File::Spec::Functions; |
6 |
use strict; |
7 |
|
8 |
$Math::VectorReal::FORMAT = "x=\"%1.3f\" y=\"%1.3f\" z=\"%1.3f\""; |
9 |
|
10 |
sub slurp { open FO, $_[0] or die "Couldn't open $_[0]: $!"; return join '', <FO> } |
11 |
|
12 |
sub read_scene_cfg { |
13 |
my ($file) = @_; |
14 |
|
15 |
my $cfg = {}; |
16 |
eval { |
17 |
my $cont = slurp ($file); |
18 |
|
19 |
for (split /\n/, $cont) { |
20 |
|
21 |
if (m/dir\s*=\s*(\d+)\s*-\s*(\d+)/) { |
22 |
for ($1..$2) { push @{$cfg->{dir}}, $_ } |
23 |
|
24 |
} elsif (m/dir\s*=\s*(\d+)/) { |
25 |
push @{$cfg->{dir}}, $1; |
26 |
|
27 |
} elsif (m/replace\s+(\S+)\s+(\S+)/) { |
28 |
push @{$cfg->{replace}}, [$1, $2]; |
29 |
|
30 |
} elsif (m/(\S+)\s*=\s*(.*)/) { |
31 |
$cfg->{$1} = $2; |
32 |
|
33 |
} |
34 |
} |
35 |
}; |
36 |
if ($@) { warn "Couldn't read $file\n" } |
37 |
|
38 |
$cfg->{w} ||= 32; |
39 |
$cfg->{h} ||= 32; |
40 |
$cfg->{height} ||= 100; |
41 |
$cfg->{dir} ||= [5]; |
42 |
|
43 |
return $cfg; |
44 |
} |
45 |
|
46 |
sub add { |
47 |
my ($x, $y, $z, $up, $r, $move_vec) = @_; |
48 |
|
49 |
$move_vec ||= vector (0, 0, 0); |
50 |
|
51 |
my $v = vector ($x, $y, $z); |
52 |
$v = $v + (0.5 * $z * $up); |
53 |
$v = $v + (0.25 * $z * $r); |
54 |
$v = $v + $move_vec; |
55 |
|
56 |
"p $v" |
57 |
} |
58 |
|
59 |
sub new_cam { |
60 |
my ($cont, $dir_n, $cfg, $outfile) = @_; |
61 |
my ($x, $y) = ($cfg->{height} / 2, $cfg->{height} / 2); |
62 |
|
63 |
my ($w, $h) = ($cfg->{w}, $cfg->{h} || $cfg->{w}); |
64 |
|
65 |
my $to = vector (0, 0, 0); |
66 |
my $from = 1 ? vector (0, $cfg->{height}, 0) : vector (0, 0, $cfg->{height}); |
67 |
|
68 |
my $dir = ($from - $to)->norm; |
69 |
|
70 |
my $up; |
71 |
my $r; |
72 |
|
73 |
if ($dir_n == 0 || $dir_n == 1) { |
74 |
#$r = vector (-1, 0, 0)->norm; |
75 |
$up = vector (0, 1, 0)->norm; |
76 |
} elsif ($dir_n == 2) { |
77 |
$up = vector (-1, 1, 0)->norm; |
78 |
} elsif ($dir_n == 3) { |
79 |
$up = vector (-1, 0, 0)->norm; |
80 |
} elsif ($dir_n == 4) { |
81 |
$up = vector (-1, -1, 0)->norm; |
82 |
} elsif ($dir_n == 5) { |
83 |
$up = vector (0, -1, 0)->norm; |
84 |
} elsif ($dir_n == 6) { |
85 |
$up = vector (1, -1, 0)->norm; |
86 |
} elsif ($dir_n == 7) { |
87 |
$up = vector (1, 0, 0)->norm; |
88 |
} elsif ($dir_n == 8) { |
89 |
$up = vector (1, 1, 0)->norm; |
90 |
} |
91 |
|
92 |
$r = ($up x $dir)->norm; |
93 |
|
94 |
my $upv = $up; |
95 |
$up = $from + $up; |
96 |
|
97 |
my $m = vector (0, 0, 0); |
98 |
|
99 |
if ($cfg->{xoffs} || $cfg->{yoffs}) { |
100 |
$m = ($cfg->{xoffs} || 0) * $r + ($cfg->{yoffs} || 0) * $upv; |
101 |
} |
102 |
|
103 |
$cont =~ s/p\s*x="([^"]+)"\s*y="([^"]+)"\s*z="([^"]+)"/add ($1, $2, $3, $upv, $r, $m)/egs; |
104 |
|
105 |
|
106 |
my $light = ($r + vector (0, 0, 0.7) + -$upv)->norm; # x="0" y="1" z="0.5"/> |
107 |
my $backlight = (-$r + vector (0, 0, 0.7) + $upv)->norm; |
108 |
|
109 |
my $cam = <<CAM; |
110 |
<light type="sunlight" name="w_Infinite2" power="0.5" cast_shadows="off"> |
111 |
<from $backlight/> |
112 |
<color r="1.0" g="1.0" b="1.0"/> |
113 |
</light> |
114 |
|
115 |
<light type="sunlight" name="w_Infinite" power="1" cast_shadows="off"> |
116 |
<from $light/> |
117 |
<color r="1" g="1" b="1"/> |
118 |
</light> |
119 |
|
120 |
<!-- Section Background, Camera, Filter and Render --> |
121 |
|
122 |
<camera name="x_Camera" resx="$w" resy="$h" focal="10" type="ortho"> |
123 |
<to $to/> |
124 |
<from $from/> |
125 |
<up $up/> |
126 |
</camera> |
127 |
|
128 |
<render camera_name="x_Camera" AA_passes="1" raydepth="8" |
129 |
bias="0.1" AA_threshold="0" |
130 |
AA_minsamples="64" AA_pixelwidth="1.25" |
131 |
AA_jitterfirst="off" clamp_rgb="on"> |
132 |
<outfile value="$outfile"/> |
133 |
<exposure value="1.4142135624"/> |
134 |
<save_alpha value="on"/> |
135 |
<gamma value="1"/> |
136 |
</render> |
137 |
CAM |
138 |
|
139 |
($cont, $cam) |
140 |
} |
141 |
|
142 |
sub render_dir { |
143 |
my ($cont, $dir, $cfg, $outfile) = @_; |
144 |
|
145 |
my $cam; |
146 |
($cont, $cam) = new_cam ($cont, $dir, $cfg, $outfile); |
147 |
|
148 |
for (@{$cfg->{replace}}) { |
149 |
$cont =~ s/\Q$_->[0]\E/$_->[1]/egs; |
150 |
} |
151 |
|
152 |
$cont =~ s#<light.*<\/scene>#$cam."<\/scene>"#es; |
153 |
$cont =~ s#<camera.*<\/scene>#<!--CAM-->#gs; |
154 |
$cont =~ s#<render.*<\/scene>#<!--CAM-->#gs; |
155 |
$cont =~ s#<!--CAM-->#$cam."<\/scene>"#es; |
156 |
|
157 |
$cont |
158 |
} |
159 |
|
160 |
my $xml = $ARGV[0] or die "render <xml>\n"; |
161 |
|
162 |
my $outfile = $xml; |
163 |
$outfile =~ s/\.xml$/\.tga/; |
164 |
|
165 |
my $xmlcont = slurp ($xml); |
166 |
my $cfg = read_scene_cfg ($xml . ".cfg"); |
167 |
|
168 |
my ($vol, $dir, $file) = File::Spec->splitpath($xml); |
169 |
|
170 |
$file =~ m/^(.*?)\.xml/; |
171 |
my $filebase = $1 || $file; |
172 |
|
173 |
for my $d (@{$cfg->{dir}}) { |
174 |
my $ofile = File::Spec->catpath ($vol, $dir, "${filebase}_dir_${d}.tga"); |
175 |
my $oxfile = File::Spec->catpath ($vol, $dir, "${filebase}_rend_${d}.xml"); |
176 |
|
177 |
my $nc = render_dir ($xmlcont, $d, $cfg, "${filebase}_dir_${d}.tga"); |
178 |
|
179 |
open OUT, ">$oxfile" |
180 |
or die "Couldn't write '$nc': $!"; |
181 |
print OUT $nc; |
182 |
close OUT; |
183 |
|
184 |
my $cwd = getcwd; |
185 |
|
186 |
if ($dir) { |
187 |
system ("cd $dir; yafray ${filebase}_rend_${d}.xml > yafray_out.log 2> yafray_out.log"); |
188 |
} else { |
189 |
system ("yafray ${filebase}_rend_${d}.xml > yafray_out.log 2> yafray_out.log"); |
190 |
} |
191 |
|
192 |
unlink $oxfile; |
193 |
|
194 |
if ($cfg->{archname}) { |
195 |
if (@{$cfg->{dir}} > 1) { |
196 |
system ("convert ${filebase}_dir_${d}.tga $cfg->{archname}$d.png"); |
197 |
print "saved arch png to: $cfg->{archname}$d.png\n"; |
198 |
} else { |
199 |
system ("convert ${filebase}_dir_${d}.tga $cfg->{archname}.png"); |
200 |
print "saved arch png to: $cfg->{archname}.png\n"; |
201 |
} |
202 |
} else { |
203 |
system ("convert ${filebase}_dir_${d}.tga ${filebase}_dir_${d}.png"); |
204 |
print "saved arch png to: ${filebase}_dir_${d}.png\n"; |
205 |
} |
206 |
|
207 |
print "rendered $ofile\n"; |
208 |
} |