ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra/bin/cfxmlrender
Revision: 1.6
Committed: Wed Apr 11 10:38:19 2007 UTC (17 years, 1 month ago) by elmex
Branch: MAIN
CVS Tags: rel-0_98
Changes since 1.5: +47 -38 lines
Log Message:
improved the render script a bit

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 $aspect = $cfg->{aspect} || 1;
252 my $focal = $cfg->{focal} || 10;
253 my $power_offs = $cfg->{power_offs} || 0;
254
255 add_tags ($tree,
256 light => [
257 { type => 'sunlight', name => 'w_Infinite', power => sprintf ("%1.1f", 0.5 + $power_offs),
258 cast_shadows => "off"
259 },
260 from => [{ x => -1, y => 1, z => 0.7 }],
261 color => [{ r => 1, g => 1, b => 1 }]
262 ]
263 );
264 add_tags ($tree,
265 light => [
266 { type => 'sunlight', name => 'w_Infinite2', power => sprintf ("%1.1f", 1 + $power_offs),
267 cast_shadows => "off"
268 },
269 from => [{ x => 1, y => -1, z => 0.7 }],
270 color => [{ r => 1, g => 1, b => 1 }]
271 ]
272 );
273 add_tags ($tree,
274 camera => [
275 { name => "xcam", resx => $w, resy => $h, focal => $focal,
276 aspect_ratio => $aspect, type => "ortho" },
277 to => [{ x => 0, y => 0, z => 0 }],
278 from => [{ x => 0, y => $from[1], z => $from[2] }],
279 up => [{ x => $up[0], y => $up[1], z => $up[2] }],
280 ],
281 render => [
282 { camera_name => "xcam", raydepth => "4",
283 gamma => "1",
284 exposure => ($cfg->{exposure} || "0"), save_alpha => "on",
285 AA_passes => "2",
286 bias => "0.1", AA_threshold => "0", AA_minsamples => "16",
287 AA_pixelwidth => "1.25", AA_jitterfirst => "off",
288 clamp_rgb => "on"
289 },
290 outfile => [{ value => $outfile }],
291 ]
292 );
293 $cont = xml2str ($tree);
294
295 if ($DEBUG and open FO, ">cfxmlrender.out.xml") {
296 print FO $cont;
297 }
298
299 $cont
300 }
301
302 @ARGV or die "cfxmlrender <xml>\n";
303
304 for my $xml (@ARGV) {
305 my $outfile = $xml;
306 $outfile =~ s/\.xml$/\.tga/;
307
308 my $xmlcont = slurp ($xml);
309 my $cfg = read_scene_cfg ($xml . ".cfg");
310
311 my ($vol, $dir, $file) = File::Spec->splitpath($xml);
312
313 $file =~ m/^(.*?)\.xml/;
314 my $filebase = $1 || $file;
315
316 for my $d (@{$cfg->{dir}}) {
317 my $ofile = File::Spec->catpath ($vol, $dir, "${filebase}_dir_${d}.tga");
318 my $oxfile = File::Spec->catpath ($vol, $dir, "${filebase}_rend_${d}.xml");
319
320 my $nc = render_dir ($xmlcont, $d, $cfg, "${filebase}_dir_${d}.tga");
321
322 open OUT, ">$oxfile"
323 or die "Couldn't write '$nc': $!";
324 print OUT $nc;
325 close OUT;
326
327 my $cwd = getcwd;
328
329 if ($dir) {
330 system ("cd $dir; yafray ${filebase}_rend_${d}.xml > yafray_out.log 2> yafray_out.log");
331 } else {
332 system ("yafray ${filebase}_rend_${d}.xml > yafray_out.log 2> yafray_out.log");
333 }
334
335 unlink $oxfile unless $DEBUG;
336
337 my $png;
338 if ($cfg->{archname}) {
339 if (@{$cfg->{dir}} > 1) {
340 my $aname = $cfg->{archname};
341 unless ($aname =~ s/%/$d/g) {
342 $aname .= $d;
343 }
344 $png = "$aname.png";
345 } else {
346 $png = "$cfg->{archname}.png";
347 }
348 } else {
349 $png = "${filebase}_dir_${d}.png";
350 }
351 system ("convert ${filebase}_dir_${d}.tga $png");
352 print "saved arch png to: $png\n";
353 if ($cfg->{archpath} and $ENV{CFDEV_ROOT}) {
354 my $archpath = $ENV{CFDEV_ROOT}.'/'.$cfg->{archpath};
355 system ("cp", $png, "$archpath/$png");
356 print "copied $png to $archpath/$png\n";
357 }
358 unlink $ofile;
359 }
360 }