ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra/bin/cfxmlrender
Revision: 1.8
Committed: Tue Jan 15 11:54:55 2008 UTC (16 years, 4 months ago) by root
Branch: MAIN
Changes since 1.7: +14 -6 lines
Log Message:
call optipng, remove arhc/ from archpath

File Contents

# User Rev Content
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 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 root 1.8 OUTPUT => \$out,
58     DATA_INDENT => 3,
59     DATA_MODE => 1
60     );
61 elmex 1.1 }
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 elmex 1.4 my %dir_to_degree = (
168     # 5 => 0,
169     # 5 => 0,
170     # 6 => 45,
171     # 7 => 90,
172     # 8 => 135,
173     # 1 => 180,
174     # 2 => 225,
175     # 3 => 270,
176     # 4 => 315
177     0 => 0,
178     1 => 0,
179     2 => 45,
180     3 => 90,
181     4 => 135,
182     5 => 180,
183     6 => 225,
184     7 => 270,
185     8 => 315
186     );
187     $a = $dir_to_degree{$dir_n};
188 elmex 1.1 $a = deg2rad - $a; # -$a because we want a clockwise rotation
189    
190     remove_tags ($tree, qw/light render camera background/);
191     filter_tags ($tree,
192     object => sub {
193     my ($tag, $con) = @_;
194     my $ot = 'transform';
195     my $om = {
196     m00 => cos($a), m01 => -sin($a), m02 => 0, m03 => 0,
197     m10 => sin($a), m11 => cos($a), m12 => 0, m13 => 0,
198     m20 => 0, m21 => 0, m22 => 1, m23 => 0,
199     m30 => 0, m31 => 0, m32 => 0, m33 => 1,
200     };
201     ($ot, [$om, $tag, $con])
202     },
203     transform => sub {
204     my ($tag, $con) = @_;
205     my $ot = 'transform';
206     my $om = {
207     m00 => cos($a), m01 => -sin($a), m02 => 0, m03 => 0,
208     m10 => sin($a), m11 => cos($a), m12 => 0, m13 => 0,
209     m20 => 0, m21 => 0, m22 => 1, m23 => 0,
210     m30 => 0, m31 => 0, m32 => 0, m33 => 1,
211     };
212     ($ot, [$om, $tag, $con])
213     }
214     );
215 elmex 1.7
216     unless ($cfg->{frontal}) {
217     filter_tags ($tree,
218     transform => sub {
219     my ($tag, $con) = @_;
220     my $ot = 'transform';
221     my $om = {
222     m00 => 1, m01 => 0, m02 => 0, m03 => 0,
223     m10 => 0, m11 => 1, m12 => 0, m13 => 0,
224     m20 => 0, m21 => 0, m22 => 1, m23 => 0,
225     m30 => 0, m31 => 0, m32 => 0, m33 => 1,
226     };
227     trans_add_shear ($om);
228     ($ot, [$om, $tag, $con])
229     }
230     );
231     }
232 elmex 1.1
233 elmex 1.2 if ($cfg->{xoffs} || $cfg->{yoffs} || $cfg->{zoffs}) {
234     filter_tags ($tree,
235     transform => sub {
236     my ($tag, $con) = @_;
237     my $ot = 'transform';
238     my $om = {
239     m00 => 1, m01 => 0, m02 => 0, m03 => $cfg->{xoffs},
240     m10 => 0, m11 => 1, m12 => 0, m13 => $cfg->{yoffs},
241     m20 => 0, m21 => 0, m22 => 1, m23 => $cfg->{zoffs},
242     m30 => 0, m31 => 0, m32 => 0, m33 => 1,
243     };
244     ($ot, [$om, $tag, $con])
245     }
246     );
247     };
248    
249    
250 elmex 1.1 my ($w, $h) = ($cfg->{w}, $cfg->{h} || $cfg->{w});
251 elmex 1.3 my @to = (0, 0, 0);
252     my @from = (0, 0, $cfg->{height});
253     my @up = (0, 1, 0);
254     @up = map { $up[$_] + $from[$_] } 0..2;
255 elmex 1.1
256 elmex 1.6 my $aspect = $cfg->{aspect} || 1;
257     my $focal = $cfg->{focal} || 10;
258 elmex 1.4 my $power_offs = $cfg->{power_offs} || 0;
259    
260 elmex 1.1 add_tags ($tree,
261     light => [
262 elmex 1.4 { type => 'sunlight', name => 'w_Infinite', power => sprintf ("%1.1f", 0.5 + $power_offs),
263 elmex 1.1 cast_shadows => "off"
264     },
265     from => [{ x => -1, y => 1, z => 0.7 }],
266     color => [{ r => 1, g => 1, b => 1 }]
267     ]
268     );
269     add_tags ($tree,
270     light => [
271 elmex 1.4 { type => 'sunlight', name => 'w_Infinite2', power => sprintf ("%1.1f", 1 + $power_offs),
272 elmex 1.1 cast_shadows => "off"
273     },
274     from => [{ x => 1, y => -1, z => 0.7 }],
275     color => [{ r => 1, g => 1, b => 1 }]
276     ]
277     );
278     add_tags ($tree,
279     camera => [
280 elmex 1.6 { name => "xcam", resx => $w, resy => $h, focal => $focal,
281     aspect_ratio => $aspect, type => "ortho" },
282 elmex 1.1 to => [{ x => 0, y => 0, z => 0 }],
283 elmex 1.3 from => [{ x => 0, y => $from[1], z => $from[2] }],
284     up => [{ x => $up[0], y => $up[1], z => $up[2] }],
285 elmex 1.1 ],
286     render => [
287     { camera_name => "xcam", raydepth => "4",
288     gamma => "1",
289 elmex 1.4 exposure => ($cfg->{exposure} || "0"), save_alpha => "on",
290     AA_passes => "2",
291     bias => "0.1", AA_threshold => "0", AA_minsamples => "16",
292 elmex 1.1 AA_pixelwidth => "1.25", AA_jitterfirst => "off",
293     clamp_rgb => "on"
294     },
295     outfile => [{ value => $outfile }],
296     ]
297     );
298     $cont = xml2str ($tree);
299    
300     if ($DEBUG and open FO, ">cfxmlrender.out.xml") {
301     print FO $cont;
302     }
303    
304     $cont
305     }
306    
307 elmex 1.6 @ARGV or die "cfxmlrender <xml>\n";
308 elmex 1.1
309 elmex 1.6 for my $xml (@ARGV) {
310     my $outfile = $xml;
311     $outfile =~ s/\.xml$/\.tga/;
312 elmex 1.1
313 elmex 1.6 my $xmlcont = slurp ($xml);
314     my $cfg = read_scene_cfg ($xml . ".cfg");
315 elmex 1.1
316 elmex 1.6 my ($vol, $dir, $file) = File::Spec->splitpath($xml);
317 elmex 1.1
318 elmex 1.6 $file =~ m/^(.*?)\.xml/;
319     my $filebase = $1 || $file;
320 elmex 1.1
321 elmex 1.6 for my $d (@{$cfg->{dir}}) {
322     my $ofile = File::Spec->catpath ($vol, $dir, "${filebase}_dir_${d}.tga");
323     my $oxfile = File::Spec->catpath ($vol, $dir, "${filebase}_rend_${d}.xml");
324 elmex 1.1
325 elmex 1.6 my $nc = render_dir ($xmlcont, $d, $cfg, "${filebase}_dir_${d}.tga");
326 elmex 1.1
327 elmex 1.6 open OUT, ">$oxfile"
328     or die "Couldn't write '$nc': $!";
329     print OUT $nc;
330     close OUT;
331 elmex 1.1
332 elmex 1.6 my $cwd = getcwd;
333 elmex 1.1
334 elmex 1.6 if ($dir) {
335     system ("cd $dir; yafray ${filebase}_rend_${d}.xml > yafray_out.log 2> yafray_out.log");
336     } else {
337     system ("yafray ${filebase}_rend_${d}.xml > yafray_out.log 2> yafray_out.log");
338     }
339 elmex 1.1
340 elmex 1.6 unlink $oxfile unless $DEBUG;
341 elmex 1.1
342 elmex 1.6 my $png;
343     if ($cfg->{archname}) {
344     if (@{$cfg->{dir}} > 1) {
345     my $aname = $cfg->{archname};
346     unless ($aname =~ s/%/$d/g) {
347     $aname .= $d;
348     }
349     $png = "$aname.png";
350     } else {
351     $png = "$cfg->{archname}.png";
352 elmex 1.5 }
353 elmex 1.1 } else {
354 elmex 1.6 $png = "${filebase}_dir_${d}.png";
355     }
356 root 1.8
357     system "convert", "${filebase}_dir_${d}.tga", $png;
358     system "optipng", $png;
359    
360 elmex 1.6 print "saved arch png to: $png\n";
361 root 1.8
362 elmex 1.6 if ($cfg->{archpath} and $ENV{CFDEV_ROOT}) {
363 root 1.8 my $archpath = "$ENV{CFDEV_ROOT}/arch/$cfg->{archpath}";
364     system "cp", $png, "$archpath/$png";
365     print "copied $png to $cfg->{archpath}/$png\n";
366 elmex 1.1 }
367 root 1.8
368 elmex 1.6 unlink $ofile;
369 elmex 1.1 }
370     }
371 root 1.8