1 |
#!/opt/perl/bin/perl |
2 |
use Cwd; |
3 |
use Math::VectorReal; |
4 |
use File::Spec::Functions; |
5 |
use strict; |
6 |
$Math::VectorReal::FORMAT = "x=\"%1.3f\" y=\"%1.3f\" z=\"%1.3f\""; |
7 |
|
8 |
sub slurp { open FO, $_[0] or die "Couldn't open $_[0]: $!"; return join '', <FO> } |
9 |
|
10 |
sub read_scene_cfg { |
11 |
my ($file) = @_; |
12 |
|
13 |
my $cfg = {}; |
14 |
eval { |
15 |
my $cont = slurp ($file); |
16 |
|
17 |
for (split /\n/, $cont) { |
18 |
|
19 |
if (m/dir\s*=\s*(\d+)\s*-\s*(\d+)/) { |
20 |
for ($1..$2) { push @{$cfg->{dir}}, $_ } |
21 |
|
22 |
} elsif (m/dir\s*=\s*(\d+)/) { |
23 |
push @{$cfg->{dir}}, $1; |
24 |
|
25 |
} elsif (m/replace\s+(\S+)\s+(\S+)/) { |
26 |
push @{$cfg->{replace}}, [$1, $2]; |
27 |
|
28 |
} elsif (m/(\S+)\s*=\s*(.*)/) { |
29 |
$cfg->{$1} = $2; |
30 |
|
31 |
} |
32 |
} |
33 |
}; |
34 |
if ($@) { warn "Couldn't read $file\n" } |
35 |
|
36 |
$cfg->{w} ||= 32; |
37 |
$cfg->{h} ||= 32; |
38 |
$cfg->{height} ||= 100; |
39 |
$cfg->{dir} ||= [5]; |
40 |
|
41 |
return $cfg; |
42 |
} |
43 |
|
44 |
sub add { |
45 |
my ($x, $y, $z, $up, $r, $move_vec) = @_; |
46 |
|
47 |
$move_vec ||= vector (0, 0, 0); |
48 |
|
49 |
my $v = vector ($x, $y, $z); |
50 |
$v = $v + (0.5 * $z * $up); |
51 |
$v = $v + (0.25 * $z * $r); |
52 |
$v = $v + $move_vec; |
53 |
|
54 |
"p $v" |
55 |
} |
56 |
|
57 |
sub new_cam { |
58 |
my ($cont, $dir_n, $cfg, $outfile) = @_; |
59 |
my ($x, $y) = ($cfg->{height} / 2, $cfg->{height} / 2); |
60 |
|
61 |
my ($w, $h) = ($cfg->{w}, $cfg->{h} || $cfg->{w}); |
62 |
|
63 |
my $to = vector (0, 0, 0); |
64 |
my $from = 1 ? vector (0, $cfg->{height}, 0) : vector (0, 0, $cfg->{height}); |
65 |
|
66 |
my $dir = ($from - $to)->norm; |
67 |
|
68 |
my $up; |
69 |
my $r; |
70 |
|
71 |
if ($dir_n == 0 || $dir_n == 1) { |
72 |
#$r = vector (-1, 0, 0)->norm; |
73 |
$up = vector (0, 1, 0)->norm; |
74 |
} elsif ($dir_n == 2) { |
75 |
$up = vector (-1, 1, 0)->norm; |
76 |
} elsif ($dir_n == 3) { |
77 |
$up = vector (-1, 0, 0)->norm; |
78 |
} elsif ($dir_n == 4) { |
79 |
$up = vector (-1, -1, 0)->norm; |
80 |
} elsif ($dir_n == 5) { |
81 |
$up = vector (0, -1, 0)->norm; |
82 |
} elsif ($dir_n == 6) { |
83 |
$up = vector (1, -1, 0)->norm; |
84 |
} elsif ($dir_n == 7) { |
85 |
$up = vector (1, 0, 0)->norm; |
86 |
} elsif ($dir_n == 8) { |
87 |
$up = vector (1, 1, 0)->norm; |
88 |
} |
89 |
|
90 |
$r = ($up x $dir)->norm; |
91 |
|
92 |
my $upv = $up; |
93 |
$up = $from + $up; |
94 |
|
95 |
my $m = vector (0, 0, 0); |
96 |
|
97 |
if ($cfg->{xoffs} || $cfg->{yoffs}) { |
98 |
$m = ($cfg->{xoffs} || 0) * $r + ($cfg->{yoffs} || 0) * $upv; |
99 |
} |
100 |
|
101 |
$cont =~ s/p\s*x="([^"]+)"\s*y="([^"]+)"\s*z="([^"]+)"/add ($1, $2, $3, $upv, $r, $m)/egs; |
102 |
|
103 |
|
104 |
my $light = ($r + vector (0, 0, 0.7) + -$upv)->norm; # x="0" y="1" z="0.5"/> |
105 |
my $backlight = (-$r + vector (0, 0, 0.7) + $upv)->norm; |
106 |
|
107 |
my $cam = <<CAM; |
108 |
<light type="sunlight" name="w_Infinite2" power="0.5" cast_shadows="off"> |
109 |
<from $backlight/> |
110 |
<color r="1.0" g="1.0" b="1.0"/> |
111 |
</light> |
112 |
|
113 |
<light type="sunlight" name="w_Infinite" power="1" cast_shadows="off"> |
114 |
<from $light/> |
115 |
<color r="1" g="1" b="1"/> |
116 |
</light> |
117 |
|
118 |
<!-- Section Background, Camera, Filter and Render --> |
119 |
|
120 |
<camera name="x_Camera" resx="$w" resy="$h" focal="10" type="ortho"> |
121 |
<to $to/> |
122 |
<from $from/> |
123 |
<up $up/> |
124 |
</camera> |
125 |
|
126 |
<render camera_name="x_Camera" AA_passes="1" raydepth="8" |
127 |
bias="0.1" AA_threshold="0" |
128 |
AA_minsamples="64" AA_pixelwidth="1.25" |
129 |
AA_jitterfirst="off" clamp_rgb="on"> |
130 |
<outfile value="$outfile"/> |
131 |
<exposure value="1.4142135624"/> |
132 |
<save_alpha value="on"/> |
133 |
<gamma value="1"/> |
134 |
</render> |
135 |
CAM |
136 |
|
137 |
($cont, $cam) |
138 |
} |
139 |
|
140 |
sub render_dir { |
141 |
my ($cont, $dir, $cfg, $outfile) = @_; |
142 |
|
143 |
my $cam; |
144 |
($cont, $cam) = new_cam ($cont, $dir, $cfg, $outfile); |
145 |
|
146 |
for (@{$cfg->{replace}}) { |
147 |
$cont =~ s/\Q$_->[0]\E/$_->[1]/egs; |
148 |
} |
149 |
|
150 |
$cont =~ s#<light.*<\/scene>#$cam."<\/scene>"#es; |
151 |
$cont =~ s#<camera.*<\/scene>#<!--CAM-->#gs; |
152 |
$cont =~ s#<render.*<\/scene>#<!--CAM-->#gs; |
153 |
$cont =~ s#<!--CAM-->#$cam."<\/scene>"#es; |
154 |
|
155 |
$cont |
156 |
} |
157 |
|
158 |
my $xml = $ARGV[0] or die "render <xml>\n"; |
159 |
|
160 |
my $outfile = $xml; |
161 |
$outfile =~ s/\.xml$/\.tga/; |
162 |
|
163 |
my $xmlcont = slurp ($xml); |
164 |
my $cfg = read_scene_cfg ($xml . ".cfg"); |
165 |
|
166 |
my ($vol, $dir, $file) = File::Spec->splitpath($xml); |
167 |
|
168 |
$file =~ m/^(.*?)\.xml/; |
169 |
my $filebase = $1 || $file; |
170 |
|
171 |
for my $d (@{$cfg->{dir}}) { |
172 |
my $ofile = File::Spec->catpath ($vol, $dir, "${filebase}_dir_${d}.tga"); |
173 |
my $oxfile = File::Spec->catpath ($vol, $dir, "${filebase}_rend_${d}.xml"); |
174 |
|
175 |
my $nc = render_dir ($xmlcont, $d, $cfg, "${filebase}_dir_${d}.tga"); |
176 |
|
177 |
open OUT, ">$oxfile" |
178 |
or die "Couldn't write '$nc': $!"; |
179 |
print OUT $nc; |
180 |
close OUT; |
181 |
|
182 |
my $cwd = getcwd; |
183 |
|
184 |
if ($dir) { |
185 |
system ("cd $dir; yafray ${filebase}_rend_${d}.xml > yafray_out.log 2> yafray_out.log"); |
186 |
} else { |
187 |
system ("yafray ${filebase}_rend_${d}.xml > yafray_out.log 2> yafray_out.log"); |
188 |
} |
189 |
|
190 |
unlink $oxfile; |
191 |
|
192 |
if ($cfg->{archname}) { |
193 |
if (@{$cfg->{dir}} > 1) { |
194 |
system ("convert ${filebase}_dir_${d}.tga $cfg->{archname}$d.png"); |
195 |
print "saved arch png to: $cfg->{archname}$d.png\n"; |
196 |
} else { |
197 |
system ("convert ${filebase}_dir_${d}.tga $cfg->{archname}.png"); |
198 |
print "saved arch png to: $cfg->{archname}.png\n"; |
199 |
} |
200 |
} else { |
201 |
system ("convert ${filebase}_dir_${d}.tga ${filebase}_dir_${d}.png"); |
202 |
print "saved arch png to: ${filebase}_dir_${d}.png\n"; |
203 |
} |
204 |
|
205 |
print "rendered $ofile\n"; |
206 |
} |