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