| 1 | package SDL::Tutorial::3DWorld::Asset; | ||
| 2 | |||
| 3 | =pod | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | SDL::Tutorial::3DWorld::Asset - Abstracts a directory of model resources | ||
| 8 | |||
| 9 | =head1 DESCRIPTION | ||
| 10 | |||
| 11 | A B<Asset> is a direction containing a variety of different modelling | ||
| 12 | resources, most often shape files and texture files. | ||
| 13 | |||
| 14 | =head1 METHODS | ||
| 15 | |||
| 16 | =cut | ||
| 17 | |||
| 18 | 1 | ▶ | use 5.008; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 19 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 20 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 21 | 1 | ▶ | use File::Spec (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 22 | 0 | ▶ | use SDL::Tutorial::3DWorld::Texture (); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 23 | use SDL::Tutorial::3DWorld::MTL (); | ||
| 24 | use SDL::Tutorial::3DWorld::OBJ (); | ||
| 25 | use SDL::Tutorial::3DWorld::RWX (); | ||
| 26 | |||
| 27 | our $VERSION = '0.33'; | ||
| 28 | |||
| 29 | |||
| 30 | |||
| 31 | |||
| 32 | |||
| 33 | ###################################################################### | ||
| 34 | # Constructor and Accessors | ||
| 35 | |||
| 36 | sub new { | ||
| 37 | my $class = shift; | ||
| 38 | my $self = bless { @_ }, $class; | ||
| 39 | |||
| 40 | # Check the directory | ||
| 41 | $self->{directory} = File::Spec->rel2abs( | ||
| 42 | $self->{directory} | ||
| 43 | ) if defined $self->{directory}; | ||
| 44 | unless ( $self->directory and -d $self->directory ) { | ||
| 45 | die "Missing or invalid directory"; | ||
| 46 | } | ||
| 47 | |||
| 48 | return $self; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub directory { | ||
| 52 | $_[0]->{directory}; | ||
| 53 | } | ||
| 54 | |||
| 55 | |||
| 56 | |||
| 57 | |||
| 58 | |||
| 59 | ###################################################################### | ||
| 60 | # Main Methods | ||
| 61 | |||
| 62 | sub model { | ||
| 63 | my $self = shift; | ||
| 64 | my $name = shift; | ||
| 65 | |||
| 66 | # Check for an obj file | ||
| 67 | my $obj = File::Spec->catfile( | ||
| 68 | $self->directory, | ||
| 69 | "$name.obj", | ||
| 70 | ); | ||
| 71 | return SDL::Tutorial::3DWorld::OBJ->new( | ||
| 72 | file => $obj, | ||
| 73 | asset => $self, | ||
| 74 | ) if -f $obj; | ||
| 75 | |||
| 76 | # Check for an obj file | ||
| 77 | my $rwx = File::Spec->catfile( | ||
| 78 | $self->directory, | ||
| 79 | "$name.rwx", | ||
| 80 | ); | ||
| 81 | return SDL::Tutorial::3DWorld::RWX->new( | ||
| 82 | file => $rwx, | ||
| 83 | asset => $self, | ||
| 84 | ) if -f $rwx; | ||
| 85 | |||
| 86 | # No idea what this is | ||
| 87 | die "Missing or invalid model name '$name'"; | ||
| 88 | } | ||
| 89 | |||
| 90 | sub texture { | ||
| 91 | my $self = shift; | ||
| 92 | my $name = shift; | ||
| 93 | |||
| 94 | # Check for a jpg texture | ||
| 95 | my $jpg = File::Spec->catfile( | ||
| 96 | $self->directory, | ||
| 97 | "$name.jpg", | ||
| 98 | ); | ||
| 99 | return SDL::Tutorial::3DWorld::Texture->new( | ||
| 100 | file => $jpg, | ||
| 101 | ) if -f $jpg; | ||
| 102 | |||
| 103 | # No idea what this is | ||
| 104 | die "Missing or invalid texture name '$name'"; | ||
| 105 | } | ||
| 106 | |||
| 107 | # Specify a MTL file to load materials from. | ||
| 108 | # Returns the object as a convenience. | ||
| 109 | sub mtl { | ||
| 110 | my $self = shift; | ||
| 111 | my $file = shift; | ||
| 112 | my $mtl = SDL::Tutorial::3DWorld::MTL->new( | ||
| 113 | file => File::Spec->catfile( $self->directory, $file ), | ||
| 114 | asset => $self, | ||
| 115 | ) or die "Failed to load MTL file"; | ||
| 116 | $self->{mtl} = $mtl; | ||
| 117 | return $mtl; | ||
| 118 | } | ||
| 119 | |||
| 120 | sub material { | ||
| 121 | my $self = shift; | ||
| 122 | my $name = shift; | ||
| 123 | my $mtl = $self->{mtl} or return undef; | ||
| 124 | my $material = $mtl->material($name) or return undef; | ||
| 125 | $self->{material}->{$name} = $material; | ||
| 126 | return $material; | ||
| 127 | } | ||
| 128 | |||
| 129 | 1; | ||
| 130 | |||
| 131 | =pod | ||
| 132 | |||
| 133 | =head1 SUPPORT | ||
| 134 | |||
| 135 | Bugs should be reported via the CPAN bug tracker at | ||
| 136 | |||
| 137 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=SDL-Tutorial-3DWorld> | ||
| 138 | |||
| 139 | =head1 AUTHOR | ||
| 140 | |||
| 141 | Adam Kennedy E<lt>adamk@cpan.orgE<gt> | ||
| 142 | |||
| 143 | =head1 SEE ALSO | ||
| 144 | |||
| 145 | L<SDL>, L<OpenGL> | ||
| 146 | |||
| 147 | =head1 COPYRIGHT | ||
| 148 | |||
| 149 | Copyright 2010 Adam Kennedy. | ||
| 150 | |||
| 151 | This program is free software; you can redistribute | ||
| 152 | it and/or modify it under the same terms as Perl itself. | ||
| 153 | |||
| 154 | The full text of the license can be found in the | ||
| 155 | LICENSE file included with this module. | ||
| 156 | |||
| 157 | =cut | ||