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