ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra/bin/cfxmlrender
Revision: 1.7
Committed: Fri May 18 15:58:54 2007 UTC (17 years ago) by elmex
Branch: MAIN
CVS Tags: rel-0_99, rel-1_11, rel-1_13, rel-1_12, rel-2_2, rel-2_1, rel-1_1, rel-1_0
Changes since 1.6: +17 -14 lines
Log Message:
added frontal flag to cfxmlrender for items

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