ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra/bin/cfxmlrender
Revision: 1.5
Committed: Thu Apr 5 09:18:24 2007 UTC (17 years, 2 months ago) by elmex
Branch: MAIN
Changes since 1.4: +6 -2 lines
Log Message:
added % placeholder for direction in rendered pictures

File Contents

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