| 1 |
elmex |
1.1 |
#!/opt/perl/bin/perl |
| 2 |
root |
1.3 |
|
| 3 |
root |
1.4 |
use common::sense; |
| 4 |
|
|
|
| 5 |
elmex |
1.1 |
use Cwd; |
| 6 |
|
|
use Math::VectorReal; |
| 7 |
|
|
use File::Spec::Functions; |
| 8 |
root |
1.3 |
|
| 9 |
elmex |
1.1 |
$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 |
elmex |
1.2 |
} elsif (m/replace\s+(\S+)\s+(\S+)/) { |
| 29 |
|
|
push @{$cfg->{replace}}, [$1, $2]; |
| 30 |
|
|
|
| 31 |
elmex |
1.1 |
} elsif (m/(\S+)\s*=\s*(.*)/) { |
| 32 |
|
|
$cfg->{$1} = $2; |
| 33 |
elmex |
1.2 |
|
| 34 |
elmex |
1.1 |
} |
| 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 |
elmex |
1.2 |
$v = $v + (0.5 * $z * $up); |
| 54 |
elmex |
1.1 |
$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 |
elmex |
1.2 |
my $from = 1 ? vector (0, $cfg->{height}, 0) : vector (0, 0, $cfg->{height}); |
| 68 |
elmex |
1.1 |
|
| 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 |
elmex |
1.2 |
|
| 107 |
elmex |
1.1 |
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 |
elmex |
1.2 |
<light type="sunlight" name="w_Infinite2" power="0.5" cast_shadows="off"> |
| 112 |
elmex |
1.1 |
<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 |
elmex |
1.2 |
<color r="1" g="1" b="1"/> |
| 119 |
elmex |
1.1 |
</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 |
elmex |
1.2 |
for (@{$cfg->{replace}}) { |
| 150 |
|
|
$cont =~ s/\Q$_->[0]\E/$_->[1]/egs; |
| 151 |
|
|
} |
| 152 |
|
|
|
| 153 |
elmex |
1.1 |
$cont =~ s#<light.*<\/scene>#$cam."<\/scene>"#es; |
| 154 |
elmex |
1.2 |
$cont =~ s#<camera.*<\/scene>#<!--CAM-->#gs; |
| 155 |
|
|
$cont =~ s#<render.*<\/scene>#<!--CAM-->#gs; |
| 156 |
|
|
$cont =~ s#<!--CAM-->#$cam."<\/scene>"#es; |
| 157 |
elmex |
1.1 |
|
| 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 |
elmex |
1.2 |
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 |
elmex |
1.1 |
|
| 180 |
|
|
open OUT, ">$oxfile" |
| 181 |
|
|
or die "Couldn't write '$nc': $!"; |
| 182 |
|
|
print OUT $nc; |
| 183 |
|
|
close OUT; |
| 184 |
elmex |
1.2 |
|
| 185 |
elmex |
1.1 |
my $cwd = getcwd; |
| 186 |
elmex |
1.2 |
|
| 187 |
elmex |
1.1 |
if ($dir) { |
| 188 |
elmex |
1.2 |
system ("cd $dir; yafray ${filebase}_rend_${d}.xml > yafray_out.log 2> yafray_out.log"); |
| 189 |
elmex |
1.1 |
} else { |
| 190 |
elmex |
1.2 |
system ("yafray ${filebase}_rend_${d}.xml > yafray_out.log 2> yafray_out.log"); |
| 191 |
elmex |
1.1 |
} |
| 192 |
elmex |
1.2 |
|
| 193 |
elmex |
1.1 |
unlink $oxfile; |
| 194 |
elmex |
1.2 |
|
| 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 |
elmex |
1.1 |
print "rendered $ofile\n"; |
| 209 |
|
|
} |