1 |
elmex |
1.1 |
#!/opt/perl/bin/perl |
2 |
|
|
use Data::Dumper; |
3 |
|
|
use Storable qw/dclone/; |
4 |
|
|
use Math::Trig; |
5 |
|
|
use XML::Parser; |
6 |
|
|
use XML::Writer; |
7 |
|
|
use Cwd; |
8 |
|
|
use Math::VectorReal; |
9 |
|
|
$Math::VectorReal::FORMAT = "x=\"%1.3f\" y=\"%1.3f\" z=\"%1.3f\""; |
10 |
|
|
use File::Spec::Functions; |
11 |
|
|
use strict; |
12 |
|
|
|
13 |
|
|
our $DEBUG = 0; |
14 |
|
|
|
15 |
|
|
sub slurp { open FO, $_[0] or die "Couldn't open $_[0]: $!"; return join '', <FO> } |
16 |
|
|
|
17 |
|
|
sub read_scene_cfg { |
18 |
|
|
my ($file) = @_; |
19 |
|
|
|
20 |
|
|
my $cfg = {}; |
21 |
|
|
eval { |
22 |
|
|
my $cont = slurp ($file); |
23 |
|
|
|
24 |
|
|
for (split /\n/, $cont) { |
25 |
|
|
|
26 |
|
|
if (m/dir\s*=\s*(\d+)\s*-\s*(\d+)/) { |
27 |
|
|
for ($1..$2) { push @{$cfg->{dir}}, $_ } |
28 |
|
|
|
29 |
|
|
} elsif (m/dir\s*=\s*(\d+)/) { |
30 |
|
|
push @{$cfg->{dir}}, $1; |
31 |
|
|
|
32 |
|
|
} elsif (m/replace\s+(\S+)\s+(\S+)/) { |
33 |
|
|
push @{$cfg->{replace}}, [$1, $2]; |
34 |
|
|
|
35 |
|
|
} elsif (m/(\S+)\s*=\s*(.*)/) { |
36 |
|
|
$cfg->{$1} = $2; |
37 |
|
|
|
38 |
|
|
} |
39 |
|
|
} |
40 |
|
|
}; |
41 |
|
|
if ($@) { warn "Couldn't read $file\n" } |
42 |
|
|
|
43 |
|
|
$cfg->{w} ||= 32; |
44 |
|
|
$cfg->{h} ||= 32; |
45 |
|
|
$cfg->{height} ||= 100; |
46 |
|
|
$cfg->{dir} ||= [5]; |
47 |
|
|
|
48 |
|
|
return $cfg; |
49 |
|
|
} |
50 |
|
|
|
51 |
|
|
sub xml2str { |
52 |
|
|
my ($tree, $wr) = @_; |
53 |
|
|
|
54 |
|
|
my $out; |
55 |
|
|
|
56 |
|
|
unless ($wr) { |
57 |
|
|
$tree = dclone $tree; |
58 |
|
|
$wr = XML::Writer->new ( |
59 |
|
|
OUTPUT => \$out, DATA_INDENT => 3, DATA_MODE => 1 |
60 |
|
|
); |
61 |
|
|
} |
62 |
|
|
|
63 |
|
|
require Data::Dumper; |
64 |
|
|
while (@$tree) { |
65 |
|
|
my $tag = shift @$tree; |
66 |
|
|
my $cont = shift @$tree; |
67 |
|
|
my $attr = {}; |
68 |
|
|
|
69 |
|
|
if (ref ($cont) eq 'ARRAY') { |
70 |
|
|
$attr = shift @$cont; |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
my $close = 0; |
74 |
|
|
if ($tag ne '0') { |
75 |
|
|
if ((ref ($cont) eq 'ARRAY') && @$cont) { |
76 |
|
|
$wr->startTag ($tag, %$attr); |
77 |
|
|
xml2str ($cont, $wr); |
78 |
|
|
$wr->endTag ($tag); |
79 |
|
|
} else { |
80 |
|
|
$wr->emptyTag ($tag, %$attr) |
81 |
|
|
if $tag ne '0'; |
82 |
|
|
} |
83 |
|
|
} else { |
84 |
|
|
#$wr->characters ($cont); |
85 |
|
|
} |
86 |
|
|
|
87 |
|
|
} |
88 |
|
|
|
89 |
|
|
if ($out) { |
90 |
|
|
return $out; |
91 |
|
|
} |
92 |
|
|
} |
93 |
|
|
|
94 |
|
|
sub filter_tags { |
95 |
|
|
my ($tree, %ftags) = @_; |
96 |
|
|
|
97 |
|
|
my $sce = $tree->[1]; |
98 |
|
|
|
99 |
|
|
my @out; |
100 |
|
|
my $tag = ''; |
101 |
|
|
for (my $i = 1; $i < @$sce; $i += 2) { |
102 |
|
|
my $tag = $sce->[$i]; |
103 |
|
|
my $con = $sce->[$i + 1]; |
104 |
|
|
|
105 |
|
|
if ($ftags{$tag}) { |
106 |
|
|
push @out, $_ for $ftags{$tag}->($tag, $con); |
107 |
|
|
} else { |
108 |
|
|
push @out, $tag, $con; |
109 |
|
|
} |
110 |
|
|
} |
111 |
|
|
@$sce = ($sce->[0], @out); |
112 |
|
|
|
113 |
|
|
$tree |
114 |
|
|
} |
115 |
|
|
|
116 |
|
|
sub remove_tags { |
117 |
|
|
my ($tree, @remtags) = @_; |
118 |
|
|
|
119 |
|
|
my $sce = $tree->[1]; |
120 |
|
|
|
121 |
|
|
my @out; |
122 |
|
|
my $tag = ''; |
123 |
|
|
for (my $i = 1; $i < @$sce; $i += 2) { |
124 |
|
|
my $tag = $sce->[$i]; |
125 |
|
|
my $con = $sce->[$i + 1]; |
126 |
|
|
|
127 |
|
|
unless (grep { $tag eq $_ } @remtags) { |
128 |
|
|
push @out, $tag, $con; |
129 |
|
|
} |
130 |
|
|
} |
131 |
|
|
@$sce = ($sce->[0], @out); |
132 |
|
|
|
133 |
|
|
$tree |
134 |
|
|
} |
135 |
|
|
|
136 |
|
|
sub add_tags { |
137 |
|
|
my ($tree, %tags) = @_; |
138 |
|
|
push @{$tree->[1]}, %tags; |
139 |
|
|
} |
140 |
|
|
|
141 |
|
|
sub trans_add_shear { |
142 |
|
|
my ($m, $dir_n) = @_; |
143 |
|
|
|
144 |
|
|
$m->{m02} += 0.25; |
145 |
|
|
$m->{m12} += 0.5; |
146 |
|
|
|
147 |
|
|
for (keys %$m) { |
148 |
|
|
$m->{$_} = sprintf "%.6f", $m->{$_} |
149 |
|
|
} |
150 |
|
|
} |
151 |
|
|
|
152 |
|
|
sub render_dir { |
153 |
|
|
my ($cont, $dir_n, $cfg, $outfile) = @_; |
154 |
|
|
|
155 |
|
|
my $cam; |
156 |
|
|
|
157 |
|
|
for (@{$cfg->{replace}}) { |
158 |
|
|
$cont =~ s/\Q$_->[0]\E/$_->[1]/egs; |
159 |
|
|
} |
160 |
|
|
|
161 |
|
|
my $p = new XML::Parser (Style => 'Tree'); |
162 |
|
|
|
163 |
|
|
my $tree = $p->parse ($cont); |
164 |
|
|
|
165 |
|
|
my $a = 0; |
166 |
|
|
|
167 |
|
|
if ($dir_n == 0 || $dir_n == 1) { |
168 |
|
|
$a = 0; |
169 |
|
|
} elsif ($dir_n == 2) { |
170 |
|
|
$a = 45; |
171 |
|
|
} elsif ($dir_n == 3) { |
172 |
|
|
$a = 90; |
173 |
|
|
} elsif ($dir_n == 4) { |
174 |
|
|
$a = 90 + 45; |
175 |
|
|
} elsif ($dir_n == 5) { |
176 |
|
|
$a = 180; |
177 |
|
|
} elsif ($dir_n == 6) { |
178 |
|
|
$a = 180 + 45; |
179 |
|
|
} elsif ($dir_n == 7) { |
180 |
|
|
$a = 270; |
181 |
|
|
} elsif ($dir_n == 8) { |
182 |
|
|
$a = 270 + 45; |
183 |
|
|
} |
184 |
|
|
$a = deg2rad - $a; # -$a because we want a clockwise rotation |
185 |
|
|
|
186 |
|
|
remove_tags ($tree, qw/light render camera background/); |
187 |
|
|
filter_tags ($tree, |
188 |
|
|
object => sub { |
189 |
|
|
my ($tag, $con) = @_; |
190 |
|
|
my $ot = 'transform'; |
191 |
|
|
my $om = { |
192 |
|
|
m00 => cos($a), m01 => -sin($a), m02 => 0, m03 => 0, |
193 |
|
|
m10 => sin($a), m11 => cos($a), m12 => 0, m13 => 0, |
194 |
|
|
m20 => 0, m21 => 0, m22 => 1, m23 => 0, |
195 |
|
|
m30 => 0, m31 => 0, m32 => 0, m33 => 1, |
196 |
|
|
}; |
197 |
|
|
($ot, [$om, $tag, $con]) |
198 |
|
|
}, |
199 |
|
|
transform => sub { |
200 |
|
|
my ($tag, $con) = @_; |
201 |
|
|
my $ot = 'transform'; |
202 |
|
|
my $om = { |
203 |
|
|
m00 => cos($a), m01 => -sin($a), m02 => 0, m03 => 0, |
204 |
|
|
m10 => sin($a), m11 => cos($a), m12 => 0, m13 => 0, |
205 |
|
|
m20 => 0, m21 => 0, m22 => 1, m23 => 0, |
206 |
|
|
m30 => 0, m31 => 0, m32 => 0, m33 => 1, |
207 |
|
|
}; |
208 |
|
|
($ot, [$om, $tag, $con]) |
209 |
|
|
} |
210 |
|
|
); |
211 |
|
|
filter_tags ($tree, |
212 |
|
|
transform => sub { |
213 |
|
|
my ($tag, $con) = @_; |
214 |
|
|
my $ot = 'transform'; |
215 |
|
|
my $om = { |
216 |
|
|
m00 => 1, m01 => 0, m02 => 0, m03 => 0, |
217 |
|
|
m10 => 0, m11 => 1, m12 => 0, m13 => 0, |
218 |
|
|
m20 => 0, m21 => 0, m22 => 1, m23 => 0, |
219 |
|
|
m30 => 0, m31 => 0, m32 => 0, m33 => 1, |
220 |
|
|
}; |
221 |
|
|
trans_add_shear ($om); |
222 |
|
|
($ot, [$om, $tag, $con]) |
223 |
|
|
} |
224 |
|
|
); |
225 |
|
|
|
226 |
elmex |
1.2 |
if ($cfg->{xoffs} || $cfg->{yoffs} || $cfg->{zoffs}) { |
227 |
|
|
filter_tags ($tree, |
228 |
|
|
transform => sub { |
229 |
|
|
my ($tag, $con) = @_; |
230 |
|
|
my $ot = 'transform'; |
231 |
|
|
my $om = { |
232 |
|
|
m00 => 1, m01 => 0, m02 => 0, m03 => $cfg->{xoffs}, |
233 |
|
|
m10 => 0, m11 => 1, m12 => 0, m13 => $cfg->{yoffs}, |
234 |
|
|
m20 => 0, m21 => 0, m22 => 1, m23 => $cfg->{zoffs}, |
235 |
|
|
m30 => 0, m31 => 0, m32 => 0, m33 => 1, |
236 |
|
|
}; |
237 |
|
|
($ot, [$om, $tag, $con]) |
238 |
|
|
} |
239 |
|
|
); |
240 |
|
|
}; |
241 |
|
|
|
242 |
|
|
|
243 |
elmex |
1.1 |
my ($w, $h) = ($cfg->{w}, $cfg->{h} || $cfg->{w}); |
244 |
|
|
my $to = vector (0, 0, 0); |
245 |
|
|
my $from = vector (0, 0, $cfg->{height}); |
246 |
|
|
my $up = vector (0, 1, 0)->norm; |
247 |
|
|
$up = $from + $up; |
248 |
|
|
|
249 |
|
|
add_tags ($tree, |
250 |
|
|
light => [ |
251 |
|
|
{ type => 'sunlight', name => 'w_Infinite', power => "0.5", |
252 |
|
|
cast_shadows => "off" |
253 |
|
|
}, |
254 |
|
|
from => [{ x => -1, y => 1, z => 0.7 }], |
255 |
|
|
color => [{ r => 1, g => 1, b => 1 }] |
256 |
|
|
] |
257 |
|
|
); |
258 |
|
|
add_tags ($tree, |
259 |
|
|
light => [ |
260 |
|
|
{ type => 'sunlight', name => 'w_Infinite2', power => "1", |
261 |
|
|
cast_shadows => "off" |
262 |
|
|
}, |
263 |
|
|
from => [{ x => 1, y => -1, z => 0.7 }], |
264 |
|
|
color => [{ r => 1, g => 1, b => 1 }] |
265 |
|
|
] |
266 |
|
|
); |
267 |
|
|
add_tags ($tree, |
268 |
|
|
camera => [ |
269 |
|
|
{ name => "xcam", resx => $w, resy => $h, focal => "10", |
270 |
|
|
type => "ortho" }, |
271 |
|
|
to => [{ x => 0, y => 0, z => 0 }], |
272 |
|
|
from => [{ x => 0, y => $from->y, z => $from->z }], |
273 |
|
|
up => [{ x => $up->x, y => $up->y, z => $up->z }], |
274 |
|
|
], |
275 |
|
|
render => [ |
276 |
|
|
{ camera_name => "xcam", raydepth => "4", |
277 |
|
|
gamma => "1", |
278 |
|
|
exposure => "0", save_alpha => "on", |
279 |
|
|
AA_passes => "1", |
280 |
|
|
bias => "0.1", AA_threshold => "0", AA_minsamples => "4", |
281 |
|
|
AA_pixelwidth => "1.25", AA_jitterfirst => "off", |
282 |
|
|
clamp_rgb => "on" |
283 |
|
|
}, |
284 |
|
|
outfile => [{ value => $outfile }], |
285 |
|
|
] |
286 |
|
|
); |
287 |
|
|
$cont = xml2str ($tree); |
288 |
|
|
|
289 |
|
|
if ($DEBUG and open FO, ">cfxmlrender.out.xml") { |
290 |
|
|
print FO $cont; |
291 |
|
|
} |
292 |
|
|
|
293 |
|
|
$cont |
294 |
|
|
} |
295 |
|
|
|
296 |
|
|
my $xml = $ARGV[0] or die "render <xml>\n"; |
297 |
|
|
|
298 |
|
|
my $outfile = $xml; |
299 |
|
|
$outfile =~ s/\.xml$/\.tga/; |
300 |
|
|
|
301 |
|
|
my $xmlcont = slurp ($xml); |
302 |
|
|
my $cfg = read_scene_cfg ($xml . ".cfg"); |
303 |
|
|
|
304 |
|
|
my ($vol, $dir, $file) = File::Spec->splitpath($xml); |
305 |
|
|
|
306 |
|
|
$file =~ m/^(.*?)\.xml/; |
307 |
|
|
my $filebase = $1 || $file; |
308 |
|
|
|
309 |
|
|
for my $d (@{$cfg->{dir}}) { |
310 |
|
|
my $ofile = File::Spec->catpath ($vol, $dir, "${filebase}_dir_${d}.tga"); |
311 |
|
|
my $oxfile = File::Spec->catpath ($vol, $dir, "${filebase}_rend_${d}.xml"); |
312 |
|
|
|
313 |
|
|
my $nc = render_dir ($xmlcont, $d, $cfg, "${filebase}_dir_${d}.tga"); |
314 |
|
|
|
315 |
|
|
open OUT, ">$oxfile" |
316 |
|
|
or die "Couldn't write '$nc': $!"; |
317 |
|
|
print OUT $nc; |
318 |
|
|
close OUT; |
319 |
|
|
|
320 |
|
|
my $cwd = getcwd; |
321 |
|
|
|
322 |
|
|
if ($dir) { |
323 |
|
|
system ("cd $dir; yafray ${filebase}_rend_${d}.xml > yafray_out.log 2> yafray_out.log"); |
324 |
|
|
} else { |
325 |
|
|
system ("yafray ${filebase}_rend_${d}.xml > yafray_out.log 2> yafray_out.log"); |
326 |
|
|
} |
327 |
|
|
|
328 |
|
|
unlink $oxfile unless $DEBUG; |
329 |
|
|
|
330 |
|
|
if ($cfg->{archname}) { |
331 |
|
|
if (@{$cfg->{dir}} > 1) { |
332 |
|
|
system ("convert ${filebase}_dir_${d}.tga $cfg->{archname}$d.png"); |
333 |
|
|
print "saved arch png to: $cfg->{archname}$d.png\n"; |
334 |
|
|
} else { |
335 |
|
|
system ("convert ${filebase}_dir_${d}.tga $cfg->{archname}.png"); |
336 |
|
|
print "saved arch png to: $cfg->{archname}.png\n"; |
337 |
|
|
} |
338 |
|
|
} else { |
339 |
|
|
system ("convert ${filebase}_dir_${d}.tga ${filebase}_dir_${d}.png"); |
340 |
|
|
print "saved arch png to: ${filebase}_dir_${d}.png\n"; |
341 |
|
|
} |
342 |
|
|
unlink $ofile; |
343 |
|
|
} |