6 use POSIX qw/floor ceil/;
8 my @lumpname = qw/entities textures planes nodes leafs leaffaces leafbrushes models brushes brushsides vertices triangles effects faces lightmaps lightgrid pvs advertisements/;
9 my %lumpid = map { $lumpname[$_] => $_ } 0..@lumpname-1;
17 or die "invalid input file name (must be a .bsp): $fn";
22 read $fh, my $header, 8;
24 die "Invalid BSP format"
25 if $header ne "IBSP\x2e\x00\x00\x00";
29 read $fh, my $lump, 8;
30 my ($offset, $length) = unpack "VV", $lump;
32 push @bsp, [$offset, $length, undef];
37 my ($offset, $length, $data) = @$_;
39 read $fh, $data, $length;
40 length $data == $length
41 or die "Incomplete BSP lump at $offset\n";
51 my ($lump, @fields) = @_;
63 if(/^(\w*)=(.*?)(\d*)$/)
70 push @decoders, sub { $item->{$f} = $data[$idx++]; };
74 push @decoders, sub { $item->{$f} = [ map { $data[$idx++] } 1..$n ]; };
79 my $itemlen = length pack $spec, ();
80 my $len = length $lump;
82 die "Invalid lump size: $len not divisible by $itemlen"
85 my $items = $len / $itemlen;
88 @data = unpack $spec, substr $lump, $_ * $itemlen, $itemlen;
99 my ($items, @fields) = @_;
111 if(/^(\w*)=(.*?)(\d*)$/)
118 push @encoders, sub { $data .= pack $spec, $item->{$f}; };
122 push @encoders, sub { $data .= pack $spec, @{$item->{$f}}; };
130 $_->() for @encoders;
136 sub EncodeDirection(@)
138 my ($x, $y, $z) = @_;
141 map { ($_ / 0.02454369260617025967) & 0xFF }
143 atan2(sqrt($x * $x + $y * $y), $z),
149 sub DecodeDirection($)
153 my ($pitch, $yaw) = map { $_ * 0.02454369260617025967 } @$dir; # maps 256 to 2pi
156 cos($yaw) * sin($pitch),
157 sin($yaw) * sin($pitch),
162 sub IntervalIntersection($$$$)
164 my ($a, $al, $b, $bl) = @_;
165 my $a0 = $a - 0.5 * $al;
166 my $a1 = $a + 0.5 * $al;
167 my $b0 = $b - 0.5 * $bl;
168 my $b1 = $b + 0.5 * $bl;
169 my $left = ($a0 > $b0) ? $a0 : $b0;
170 my $right = ($a1 > $b1) ? $b1 : $a1;
171 die "Non-intersecting intervals $a $al $b $bl"
173 return $right - $left;
176 sub BoxIntersection(@)
178 my ($x, $y, $z, $w, $h, $d, $x2, $y2, $z2, $w2, $h2, $d2) = @_;
180 IntervalIntersection($x, $w, $x2, $w2)
182 IntervalIntersection($y, $h, $y2, $h2)
184 IntervalIntersection($z, $d, $z2, $d2);
193 my $total = 17 * 8 + 8 + length($msg);
197 my $nl = length $bsp[$_]->[2];
199 print "BSP lump $_ ($lumpname[$_]): offset $bsp[$_]->[0] length $bsp[$_]->[1] newlength $nl\n";
200 my $endpos = $bsp[$_]->[0] + $bsp[$_]->[1];
201 $max = $endpos if $max < $endpos;
203 print "BSP file size will change from $max to $total bytes\n";
205 elsif(/^-d(.+)$/) # delete a lump
207 my $id = $lumpid{$1};
208 die "invalid lump $1 to remove"
212 elsif(/^-m(.*)$/) # change the message
216 elsif(/^-l(jpg|png|tga)(\d+)?$/) # externalize lightmaps (deleting the internal ones)
221 my $faces = $bsp[$lumpid{faces}]->[2];
222 my $lightmaps = $bsp[$lumpid{lightmaps}]->[2];
223 my @values = DecodeLump $faces,
224 qw/texture=V effect=V type=V vertex=V n_vertexes=V meshvert=V n_meshverts=V lm_index=V lm_start=f2 lm_size=f2 lm_origin=f3 lm_vec_0=f3 lm_vec_1=f3 normal=f3 size=V2/;
228 my $l = $_->{lm_index};
229 next if $l >= 2**31; # signed
236 $lightmaps{$_+1} = $lightmaps{$_} for keys %lightmaps;
238 for(sort { $a <=> $b } keys %lightmaps)
240 print STDERR "Lightmap $_ was used $lightmaps{$_} times\n";
242 # export that lightmap
243 my $lmsize = 128 * 128 * 3;
244 next if length $lightmaps < ($_ + 1) * $lmsize;
245 my $lmdata = substr $lightmaps, $_ * $lmsize, $lmsize;
246 my $img = Image::Magick->new(size => '128x128', depth => 8, magick => 'RGB');
247 $img->BlobToImage($lmdata);
248 my $outfn = sprintf "%s/lm_%04d.$ext", $basename, $_;
250 $img->Set(quality => $quality)
252 my $err = $img->Write($outfn);
255 print STDERR "Wrote $outfn\n";
258 # nullify the lightmap lump
259 $bsp[$lumpid{lightmaps}]->[2] = "";
261 elsif(/^-g(.+)$/) # export light grid as an image (for debugging)
264 my @models = DecodeLump $bsp[$lumpid{models}]->[2],
265 qw/mins=f3 maxs=f3 face=V n_faces=V brush=V n_brushes=V/;
266 my $entities = $bsp[$lumpid{entities}]->[2];
267 my @entitylines = split /\r?\n/, $entities;
268 my $gridsize = "64 64 128";
272 /^\s*"gridsize"\s+"(.*)"$/
275 my @scale = map { 1 / $_ } split / /, $gridsize;
276 my @imins = map { ceil($models[0]{mins}[$_] * $scale[$_]) } 0..2;
277 my @imaxs = map { floor($models[0]{maxs}[$_] * $scale[$_]) } 0..2;
278 my @isize = map { $imaxs[$_] - $imins[$_] + 1 } 0..2;
279 my $isize = $isize[0] * $isize[1] * $isize[2];
280 my @gridcells = DecodeLump $bsp[$lumpid{lightgrid}]->[2],
281 qw/ambient=C3 directional=C3 dir=C2/;
282 die "Cannot decode light grid"
283 unless $isize == @gridcells;
285 # sum up the "ambient" light over all pixels
288 for my $y(0..$isize[1]-1)
290 for my $x(0..$isize[0]-1)
292 my ($r, $g, $b) = (0, 0, 0);
293 for my $z(0..$isize[2]-1)
295 my $cell = $gridcells[$x + $y * $isize[0] + $z * $isize[0] * $isize[1]];
296 $r += $cell->{ambient}->[0];
297 $g += $cell->{ambient}->[1];
298 $b += $cell->{ambient}->[2];
300 push @pixels, [$r, $g, $b];
301 $max = $r if $max < $r;
302 $max = $g if $max < $g;
303 $max = $b if $max < $b;
309 $pixeldata .= pack "CCC", map { 255 * $p->[$_] / $max } 0..2;
312 my $img = Image::Magick->new(size => sprintf("%dx%d", $isize[0], $isize[1]), depth => 8, magick => 'RGB');
313 $img->BlobToImage($pixeldata);
314 $img->Write($filename);
315 print STDERR "Wrote $filename\n";
317 elsif(/^-G(.+)$/) # decimate light grid
322 my @models = DecodeLump $bsp[$lumpid{models}]->[2],
323 qw/mins=f3 maxs=f3 face=V n_faces=V brush=V n_brushes=V/;
324 my $entities = $bsp[$lumpid{entities}]->[2];
325 my @entitylines = split /\r?\n/, $entities;
326 my $gridsize = "64 64 128";
327 my $gridsizeindex = undef;
328 for(0..@entitylines-1)
330 my $l = $entitylines[$_];
332 if($l =~ /^\s*"gridsize"\s+"(.*)"$/)
338 my @scale = map { 1 / $_ } split / /, $gridsize;
339 my @imins = map { ceil($models[0]{mins}[$_] * $scale[$_]) } 0..2;
340 my @imaxs = map { floor($models[0]{maxs}[$_] * $scale[$_]) } 0..2;
341 my @isize = map { $imaxs[$_] - $imins[$_] + 1 } 0..2;
342 my $isize = $isize[0] * $isize[1] * $isize[2];
343 my @gridcells = DecodeLump $bsp[$lumpid{lightgrid}]->[2],
344 qw/ambient=C3 directional=C3 dir=C2/;
345 die "Cannot decode light grid"
346 unless $isize == @gridcells;
348 # get the new grid size values
349 my @newscale = map { $_ / $decimate } @scale;
350 my $newgridsize = join " ", map { 1 / $_ } @newscale;
351 my @newimins = map { ceil($models[0]{mins}[$_] * $newscale[$_]) } 0..2;
352 my @newimaxs = map { floor($models[0]{maxs}[$_] * $newscale[$_]) } 0..2;
353 my @newisize = map { $newimaxs[$_] - $newimins[$_] + 1 } 0..2;
356 my @newgridcells = ();
357 for my $z($newimins[2]..$newimaxs[2])
359 # the coords are MIDPOINTS of the grid cells!
360 my @oldz = grep { $_ >= $imins[2] && $_ <= $imaxs[2] } floor(($z - 0.5) * $decimate + 0.5) .. ceil(($z + 0.5) * $decimate - 0.5);
361 my $innerz_raw = $z * $decimate;
362 my $innerz = floor($innerz_raw + 0.5);
363 $innerz = $imins[2] if $innerz < $imins[2];
364 $innerz = $imaxs[2] if $innerz > $imaxs[2];
365 for my $y($newimins[1]..$newimaxs[1])
367 my @oldy = grep { $_ >= $imins[1] && $_ <= $imaxs[1] } floor(($y - 0.5) * $decimate + 0.5) .. ceil(($y + 0.5) * $decimate - 0.5);
368 my $innery_raw = $y * $decimate;
369 my $innery = floor($innery_raw + 0.5);
370 $innery = $imins[1] if $innery < $imins[1];
371 $innery = $imaxs[1] if $innery > $imaxs[1];
372 for my $x($newimins[0]..$newimaxs[0])
374 my @oldx = grep { $_ >= $imins[0] && $_ <= $imaxs[0] } floor(($x - 0.5) * $decimate + 0.5) .. ceil(($x + 0.5) * $decimate - 0.5);
375 my $innerx_raw = $x * $decimate;
376 my $innerx = floor($innerx_raw + 0.5);
377 $innerx = $imins[0] if $innerx < $imins[0];
378 $innerx = $imaxs[0] if $innerx > $imaxs[0];
384 my $innercell = $gridcells[($innerx - $imins[0]) + $isize[0] * ($innery - $imins[1]) + $isize[0] * $isize[1] * ($innerz - $imins[2])];
391 my $cell = $gridcells[($X - $imins[0]) + $isize[0] * ($Y - $imins[1]) + $isize[0] * $isize[1] * ($Z - $imins[2])];
393 my $cellweight = BoxIntersection(
395 map { $_ * $decimate } $x, $y, $z, 1, 1, 1
398 $dir[$_] += $cellweight * $cell->{directional}->[$_] for 0..2;
399 $amb[$_] += $cellweight * $cell->{ambient}->[$_] for 0..2;
400 my @norm = DecodeDirection $cell->{dir};
401 $vec[$_] += $cellweight * $norm[$_] for 0..2;
402 $weight += $cellweight;
408 $dir[$_] /= $weight for 0..2;
409 $dir[$_] *= $filter for 0..2;
410 $dir[$_] += (1 - $filter) * $innercell->{directional}->[$_] for 0..2;
412 $amb[$_] /= $weight for 0..2;
413 $amb[$_] *= $filter for 0..2;
414 $amb[$_] += (1 - $filter) * $innercell->{ambient}->[$_] for 0..2;
416 my @norm = DecodeDirection $innercell->{dir};
417 $vec[$_] /= $weight for 0..2;
418 $vec[$_] *= $filter for 0..2;
419 $vec[$_] += (1 - $filter) * $norm[$_] for 0..2;
423 directional => \@dir,
424 dir => EncodeDirection @norm
428 push @newgridcells, $innercell;
433 $bsp[$lumpid{lightgrid}]->[2] = EncodeLump \@newgridcells,
434 qw/ambient=C3 directional=C3 dir=C2/;
435 splice @entitylines, $gridsizeindex, 1, ()
436 if defined $gridsizeindex;
437 splice @entitylines, 1, 0, qq{"gridsize" "$newgridsize"};
438 $bsp[$lumpid{entities}]->[2] = join "\n", @entitylines;
440 elsif(/^-x(.+)$/) # extract lump to stdout
442 my $id = $lumpid{$1};
443 die "invalid lump $1 to extract"
445 print $bsp[$id]->[2];
447 elsif(/^-o(.+)?$/) # write the final BSP file
451 if not defined $outfile;
452 open my $fh, ">", $outfile
453 or die "$outfile: $!";
455 my $pos = 17 * 8 + tell($fh) + length $msg;
459 $_->[1] = length $_->[2];
461 print $fh pack "VV", $_->[0], $_->[1];
469 print STDERR "Wrote $outfile\n";
473 die "Invalid option: $_";
479 # decimate light grid
480 # edit lightmaps/grid