/usr/share/perl5/XRacer/BlenderImport.pm is in xracer-tools 0.96.9.1-7.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 | # XRACER (C) 1999-2000 Richard W.M. Jones <rich@annexia.org> and other AUTHORS
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# $Id: BlenderImport.pm,v 1.1 2000/01/30 12:11:31 rich Exp $
package XRacer::BlenderImport;
use Exporter;
@ISA = qw( Exporter );
@EXPORT = qw( );
use XRacer::Math;
use strict;
sub parse
{
my $class = shift;
my $filename = shift || "blender.export";
unless (open IMPORTFILE, $filename)
{
warn "$filename: $!";
return undef;
}
my $world = { vertices => [],
vertexhash => {},
layers => [] };
bless $world, $class;
while ($_ = _next_line (\*IMPORTFILE))
{
my ($name, $layer, $has_col, $has_uvco, $has_mat)
= split /[ \t]+/, $_;
# Layer is a bitmap of the actual layers in which
# this object exists. For objects which lie in multiple
# layers, choose the smallest layer number and print
# a warning.
$layer = _get_layer ($name, $layer);
#warn "reading object: $name (layer $layer)";
# Read the number of faces.
unless ($_ = _next_line (\*IMPORTFILE))
{ warn "$filename: unexpected EOF"; return undef }
my $nr_faces = $_;
my @faces = ();
# Read each face in turn.
for (my $i = 0; $i < $nr_faces; ++$i)
{
# Read the number of vertices in this face.
unless ($_ = _next_line (\*IMPORTFILE))
{ warn "$filename: unexpected EOF"; return undef }
my $nr_vertices = $_;
my @face_vertices = ();
# Read each vertex in turn.
for (my $j = 0; $j < $nr_vertices; ++$j)
{
# Read the vertex.
unless ($_ = _next_line (\*IMPORTFILE))
{ warn "$filename: unexpected EOF"; return undef }
my ($x, $y, $z, $u, $v, $nx, $ny, $nz);
if ($has_uvco)
{
($x, $y, $z, $u, $v, $nx, $ny, $nz)
= split /[ \t]+/, $_;
}
else
{
$u = $v = 0;
($x, $y, $z, $nx, $ny, $nz)
= split /[ \t]+/, $_;
}
# Store the vertex.
push @face_vertices,
{ coords => [ $x, $y, $z, 0 ],
texcoords => [ $u, $v ],
normal => [ $nx, $ny, $nz, 0 ],
colour => [ 1, 1, 1, 1 ] };
}
# Vertex colours?
if ($has_col)
{
# Read the number of vertex colours.
unless ($_ = _next_line (\*IMPORTFILE))
{ warn "$filename: unexpected EOF"; return undef }
my $nr_vertex_colours = $_;
if ($nr_vertex_colours != $nr_vertices)
{
warn "$filename: number of vertex colours doesn't match number of vertices";
return undef;
}
for (my $j = 0; $j < $nr_vertex_colours; ++$j)
{
# Read the vertex colour.
unless ($_ = _next_line (\*IMPORTFILE))
{ warn "$filename: unexpected EOF"; return undef }
my ($r, $g, $b, $a) = split /[ \t]+/, $_;
$face_vertices[$j]->{colour} = [ $r, $g, $b, $a ];
}
}
push @faces, { vertices => \@face_vertices };
}
# Read the transformation matrices.
unless ($_ = _next_line (\*IMPORTFILE))
{ warn "$filename: unexpected EOF"; return undef }
my ($sizex, $sizey, $sizez) = split /[ \t]+/, $_;
unless ($_ = _next_line (\*IMPORTFILE))
{ warn "$filename: unexpected EOF"; return undef }
my ($rotx, $roty, $rotz) = split /[ \t]+/, $_;
unless ($_ = _next_line (\*IMPORTFILE))
{ warn "$filename: unexpected EOF"; return undef }
my ($locx, $locy, $locz) = split /[ \t]+/, $_;
my ($matr, $matg, $matb) = (1, 1, 1);
if ($has_mat)
{
# Read the material colour.
unless ($_ = _next_line (\*IMPORTFILE))
{ warn "$filename: unexpected EOF"; return undef }
($matr, $matg, $matb) = split /[ \t]+/, $_;
}
# Transform all the vertices by the rotation matrix
# so we have the real coordinates. Add vertices onto
# the global list of vertices.
my $cosx = cos ($rotx);
my $sinx = sin ($rotx);
my $matx = [ [ 1, 0, 0, 0 ],
[ 0, $cosx, -$sinx, 0 ],
[ 0, $sinx, $cosx, 0 ],
[ 0, 0, 0, 0 ] ];
my $cosy = cos ($roty);
my $siny = sin ($roty);
my $maty = [ [ $cosy, 0, $siny, 0 ],
[ 0, 1, 0, 0 ],
[ -$siny, 0, $cosy, 0 ],
[ 0, 0, 0, 0 ] ];
my $cosz = cos ($rotz);
my $sinz = sin ($rotz);
my $matz = [ [ $cosz, -$sinz, 0, 0 ],
[ $sinz, $cosz, 0, 0 ],
[ 0, 0, 1, 0 ],
[ 0, 0, 0, 0 ] ];
my $mat = matrix_multiply (matrix_multiply ($matz, $maty),
$matx);
my $face;
foreach $face (@faces)
{
my @face_vertices = ();
my $vertex;
foreach $vertex (@{$face->{vertices}})
{
$vertex->{coords}
= matrix_vector_multiply ($mat, $vertex->{coords});
$vertex->{coords}[0] *= $sizex;
$vertex->{coords}[1] *= $sizey;
$vertex->{coords}[2] *= $sizez;
$vertex->{coords}[0] += $locx;
$vertex->{coords}[1] += $locy;
$vertex->{coords}[2] += $locz;
$vertex->{normal}
= matrix_vector_multiply ($mat, $vertex->{normal});
my $vertexindex = $world->add_vertex ($vertex);
push @face_vertices, $vertexindex;
}
$face->{vertices} = \@face_vertices;
}
# Push the mesh into the appropriate layer.
unless (defined ($world->{layers}[$layer]))
{
$world->{layers}[$layer] = [];
}
my $mesh = {
name => $name,
layer => $layer,
faces => \@faces,
material => [ $matr, $matg, $matb ],
has_colours => $has_col,
has_texcoords => $has_uvco,
has_material => $has_mat
};
push @{$world->{layers}[$layer]}, $mesh;
}
close IMPORTFILE;
return $world;
}
sub _next_line
{
my $fh = shift;
while (<$fh>)
{
# Remove trailing CR, LF.
s/[\n\r]+$//;
# Ignore blank lines, comments.
next if m/^[ \t]*#/;
next if m/^[ \t]*$/;
return $_;
}
return undef;
}
sub _get_layer
{
my $name = shift;
my $layer_in = shift;
my $layer_out = 0;
while (($layer_in & 1) == 0)
{
$layer_in >>= 1;
$layer_out ++;
}
if ($layer_in != 1)
{
warn "object $name is in multiple layers: using layer $layer_out";
}
return $layer_out;
}
sub add_vertex
{
my $world = shift;
my $v = shift;
my $index;
my $hashkey = join ",",
(
$v->{coords}[0],
$v->{coords}[1],
$v->{coords}[2],
$v->{coords}[3],
$v->{texcoords}[0],
$v->{texcoords}[1],
$v->{normal}[0],
$v->{normal}[1],
$v->{normal}[2],
$v->{normal}[3],
$v->{colour}[0],
$v->{colour}[1],
$v->{colour}[2],
$v->{colour}[3]
);
if (exists ($world->{vertexhash}{$hashkey}))
{
return $world->{vertexhash}{$hashkey};
}
else
{
push @{$world->{vertices}}, $v;
$index = @{$world->{vertices}} - 1;
$world->{vertexhash}{$hashkey} = $index;
return $index;
}
}
sub get_vertices
{
my $world = shift;
return $world->{vertices};
}
sub get_layer_info
{
my $world = shift;
my @layers = @{$world->{layers}};
my %info = ();
for (my $i = 0; $i < @layers; ++$i)
{
if (defined ($layers[$i]) && @{$layers[$i]} > 0)
{
$info{$i} = @{$layers[$i]};
}
}
return %info;
}
sub get_meshes_in_layer
{
my $world = shift;
my $layer = shift;
return @{$world->{layers}[$layer]};
}
1;
__END__
=head1 NAME
XRacer::BlenderImport - Import files exported by xracer-blenderexport.py
=head1 SYNOPSIS
use XRacer::BlenderImport;
$world = parse XRacer::BlenderImport [ $filename ];
$index = $world->add_vertex ($vertex);
$verticesref = $world->get_vertices;
%layerinfo = $world->get_layer_info;
@objects = $world->get_meshes_in_layer ($layer);
=head1 DESCRIPTION
The C<XRacer::BlenderImport> module contains functions for
importing special XRacer-specific Blender files into Perl scripts.
These Blender files have been previously exported by the
C<xracer-blenderexport.py> Python script for Blender.
The C<XRacer::BlenderImport> module parses the "blender.export"
file, cleans it up (removing multiple vertices, for example)
and presents an internal I<world> representation. The world
is divided into layers (corresponding to the layers in the
original Blender image), and in each of these layers is a
set of meshes.
=head2 FUNDAMENTAL TYPES
Vertices are stored in a global list, so that common vertices
are only stored once. A vertex structure looks like this:
$vertex = { coords => [ $x, $y, $z, 0 ],
texcoords => [ $u, $v ],
normal => [ $nx, $ny, $nz, 0 ],
colour => [ $r, $g, $b, $a ] };
The fields are (in order): the coordinates of the vertex, the
texture coordinates, the normal vector at this point and the
colour of the vertex.
Faces are stored simply as a list of vertex indices (relative
to the global list of vertices). A face looks like this:
$face = { vertices => [ $index0, $index1, $index2, ... ] }
Faces have at least three vertices, and maybe more.
A mesh is a list of faces and additional information, such as
the name of the mesh (object). A mesh structure looks like this:
$mesh = { name => $name,
layer => $layer,
faces => \@faces,
material => [ $red, $green, $blue ],
has_colours => $has_colours,
has_texcoords => $has_texcoords,
has_material => $has_material };
The fields are: the name of the mesh (or object), the layer
on which the object exists, the list of faces, the material
and then three flags which are passed to us from Blender:
did the user supply vertex colours? did the user supply
texture coordinates? and did the user supply a material?
=head1 CLASS METHODS
=over 4
=item $world = parse XRacer::BlenderImport [ $filename ];
Parse the import file C<$filename> (or "blender.export" if
no filename is given) and generate a I<world> representation.
If the file could not be parsed, then this function will print
an error message and return C<undef>.
=back
=head1 OBJECT METHODS
=over 4
=item $index = $world->add_vertex ($vertex)
This function adds a single vertex to the world object (vertices
are stored in a large shared list so that common vertices are
folded into one). It returns the vertex index of the new vertex.
If another vertex with the same position, texture coordinates,
normal and colour existed, then this function would return the
index of the other vertex, rather than creating a fresh vertex
object.
=item $verticesref = $world->get_vertices
This returns a reference to the global list of vertices. You
B<must not> update or change this list. Use the C<add_vertex> method
instead.
=item %layerinfo = $world->get_layer_info
This method returns a hash. The I<keys> of the hash are layers which
contain at least one object. The I<values> of the hash are the number
of objects in that layer.
For example, if the world contained 3 objects on layer 1, 1 object
on layer 2 and 4 objects on layer 5, then the hash returned would
be equivalent to:
%layerinfo = ( 1 => 3, 2 => 1, 5 => 4 );
=item @objects = $world->get_meshes_in_layer ($layer)
Get a list of the objects (i.e. meshes) found in layer C<$layer>.
See the DESCRIPTION section above for a description of the
mesh structure.
=back
=head1 AUTHOR
Richard W.M. Jones, <rich@annexia.org>
=head1 COPYRIGHT
XRacer is copyright (C) 1999-2000 Richard W.M. Jones (rich@annexia.org)
and other contributors listed in the AUTHORS file.
=head1 SEE ALSO
L<perl(1)>, L<xracer(6)>.
=cut
|