ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra/bin/cfxmlrender
Revision: 1.10
Committed: Tue Apr 8 20:26:32 2008 UTC (16 years, 1 month ago) by root
Branch: MAIN
CVS Tags: rel-1_222, rel-1_221, rel-1_2, rel-1_22, rel-1_23
Changes since 1.9: +1 -0 lines
Log Message:
*** empty log message ***

File Contents

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