ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra/bin/cfxmlrender
Revision: 1.1
Committed: Fri Apr 21 12:25:09 2006 UTC (18 years, 1 month ago) by elmex
Branch: MAIN
Log Message:
comitted new yafray renderer

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 Math::VectorReal;
9 $Math::VectorReal::FORMAT = "x=\"%1.3f\" y=\"%1.3f\" z=\"%1.3f\"";
10 use File::Spec::Functions;
11 use strict;
12
13 our $DEBUG = 0;
14
15 sub slurp { open FO, $_[0] or die "Couldn't open $_[0]: $!"; return join '', <FO> }
16
17 sub read_scene_cfg {
18 my ($file) = @_;
19
20 my $cfg = {};
21 eval {
22 my $cont = slurp ($file);
23
24 for (split /\n/, $cont) {
25
26 if (m/dir\s*=\s*(\d+)\s*-\s*(\d+)/) {
27 for ($1..$2) { push @{$cfg->{dir}}, $_ }
28
29 } elsif (m/dir\s*=\s*(\d+)/) {
30 push @{$cfg->{dir}}, $1;
31
32 } elsif (m/replace\s+(\S+)\s+(\S+)/) {
33 push @{$cfg->{replace}}, [$1, $2];
34
35 } elsif (m/(\S+)\s*=\s*(.*)/) {
36 $cfg->{$1} = $2;
37
38 }
39 }
40 };
41 if ($@) { warn "Couldn't read $file\n" }
42
43 $cfg->{w} ||= 32;
44 $cfg->{h} ||= 32;
45 $cfg->{height} ||= 100;
46 $cfg->{dir} ||= [5];
47
48 return $cfg;
49 }
50
51 sub xml2str {
52 my ($tree, $wr) = @_;
53
54 my $out;
55
56 unless ($wr) {
57 $tree = dclone $tree;
58 $wr = XML::Writer->new (
59 OUTPUT => \$out, DATA_INDENT => 3, DATA_MODE => 1
60 );
61 }
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 if ($dir_n == 0 || $dir_n == 1) {
168 $a = 0;
169 } elsif ($dir_n == 2) {
170 $a = 45;
171 } elsif ($dir_n == 3) {
172 $a = 90;
173 } elsif ($dir_n == 4) {
174 $a = 90 + 45;
175 } elsif ($dir_n == 5) {
176 $a = 180;
177 } elsif ($dir_n == 6) {
178 $a = 180 + 45;
179 } elsif ($dir_n == 7) {
180 $a = 270;
181 } elsif ($dir_n == 8) {
182 $a = 270 + 45;
183 }
184 $a = deg2rad - $a; # -$a because we want a clockwise rotation
185
186 remove_tags ($tree, qw/light render camera background/);
187 filter_tags ($tree,
188 object => sub {
189 my ($tag, $con) = @_;
190 my $ot = 'transform';
191 my $om = {
192 m00 => cos($a), m01 => -sin($a), m02 => 0, m03 => 0,
193 m10 => sin($a), m11 => cos($a), m12 => 0, m13 => 0,
194 m20 => 0, m21 => 0, m22 => 1, m23 => 0,
195 m30 => 0, m31 => 0, m32 => 0, m33 => 1,
196 };
197 ($ot, [$om, $tag, $con])
198 },
199 transform => sub {
200 my ($tag, $con) = @_;
201 my $ot = 'transform';
202 my $om = {
203 m00 => cos($a), m01 => -sin($a), m02 => 0, m03 => 0,
204 m10 => sin($a), m11 => cos($a), m12 => 0, m13 => 0,
205 m20 => 0, m21 => 0, m22 => 1, m23 => 0,
206 m30 => 0, m31 => 0, m32 => 0, m33 => 1,
207 };
208 ($ot, [$om, $tag, $con])
209 }
210 );
211 filter_tags ($tree,
212 transform => sub {
213 my ($tag, $con) = @_;
214 my $ot = 'transform';
215 my $om = {
216 m00 => 1, m01 => 0, m02 => 0, m03 => 0,
217 m10 => 0, m11 => 1, m12 => 0, m13 => 0,
218 m20 => 0, m21 => 0, m22 => 1, m23 => 0,
219 m30 => 0, m31 => 0, m32 => 0, m33 => 1,
220 };
221 trans_add_shear ($om);
222 ($ot, [$om, $tag, $con])
223 }
224 );
225
226 my ($w, $h) = ($cfg->{w}, $cfg->{h} || $cfg->{w});
227 my $to = vector (0, 0, 0);
228 my $from = vector (0, 0, $cfg->{height});
229 my $up = vector (0, 1, 0)->norm;
230 $up = $from + $up;
231
232 add_tags ($tree,
233 light => [
234 { type => 'sunlight', name => 'w_Infinite', power => "0.5",
235 cast_shadows => "off"
236 },
237 from => [{ x => -1, y => 1, z => 0.7 }],
238 color => [{ r => 1, g => 1, b => 1 }]
239 ]
240 );
241 add_tags ($tree,
242 light => [
243 { type => 'sunlight', name => 'w_Infinite2', power => "1",
244 cast_shadows => "off"
245 },
246 from => [{ x => 1, y => -1, z => 0.7 }],
247 color => [{ r => 1, g => 1, b => 1 }]
248 ]
249 );
250 add_tags ($tree,
251 camera => [
252 { name => "xcam", resx => $w, resy => $h, focal => "10",
253 type => "ortho" },
254 to => [{ x => 0, y => 0, z => 0 }],
255 from => [{ x => 0, y => $from->y, z => $from->z }],
256 up => [{ x => $up->x, y => $up->y, z => $up->z }],
257 ],
258 render => [
259 { camera_name => "xcam", raydepth => "4",
260 gamma => "1",
261 exposure => "0", save_alpha => "on",
262 AA_passes => "1",
263 bias => "0.1", AA_threshold => "0", AA_minsamples => "4",
264 AA_pixelwidth => "1.25", AA_jitterfirst => "off",
265 clamp_rgb => "on"
266 },
267 outfile => [{ value => $outfile }],
268 ]
269 );
270 $cont = xml2str ($tree);
271
272 if ($DEBUG and open FO, ">cfxmlrender.out.xml") {
273 print FO $cont;
274 }
275
276 $cont
277 }
278
279 my $xml = $ARGV[0] or die "render <xml>\n";
280
281 my $outfile = $xml;
282 $outfile =~ s/\.xml$/\.tga/;
283
284 my $xmlcont = slurp ($xml);
285 my $cfg = read_scene_cfg ($xml . ".cfg");
286
287 my ($vol, $dir, $file) = File::Spec->splitpath($xml);
288
289 $file =~ m/^(.*?)\.xml/;
290 my $filebase = $1 || $file;
291
292 for my $d (@{$cfg->{dir}}) {
293 my $ofile = File::Spec->catpath ($vol, $dir, "${filebase}_dir_${d}.tga");
294 my $oxfile = File::Spec->catpath ($vol, $dir, "${filebase}_rend_${d}.xml");
295
296 my $nc = render_dir ($xmlcont, $d, $cfg, "${filebase}_dir_${d}.tga");
297
298 open OUT, ">$oxfile"
299 or die "Couldn't write '$nc': $!";
300 print OUT $nc;
301 close OUT;
302
303 my $cwd = getcwd;
304
305 if ($dir) {
306 system ("cd $dir; yafray ${filebase}_rend_${d}.xml > yafray_out.log 2> yafray_out.log");
307 } else {
308 system ("yafray ${filebase}_rend_${d}.xml > yafray_out.log 2> yafray_out.log");
309 }
310
311 unlink $oxfile unless $DEBUG;
312
313 if ($cfg->{archname}) {
314 if (@{$cfg->{dir}} > 1) {
315 system ("convert ${filebase}_dir_${d}.tga $cfg->{archname}$d.png");
316 print "saved arch png to: $cfg->{archname}$d.png\n";
317 } else {
318 system ("convert ${filebase}_dir_${d}.tga $cfg->{archname}.png");
319 print "saved arch png to: $cfg->{archname}.png\n";
320 }
321 } else {
322 system ("convert ${filebase}_dir_${d}.tga ${filebase}_dir_${d}.png");
323 print "saved arch png to: ${filebase}_dir_${d}.png\n";
324 }
325 unlink $ofile;
326 }