ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra/bin/cfxmlrender
Revision: 1.11
Committed: Tue Jan 13 12:49:24 2009 UTC (15 years, 4 months ago) by elmex
Branch: MAIN
CVS Tags: rel-2_01, rel-1_30, rel-1_29, rel-1_24, rel-1_25, HEAD
Changes since 1.10: +5 -2 lines
Log Message:
added torch and lamp type documentation.

File Contents

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