| 1 | package SDL::Tutorial::3DWorld::MTL; | ||
| 2 | |||
| 3 | =pod | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | SDL::Tutorial::3DWorld::MTL - Support for loading material libraries from MTL files | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | # Create the object but don't load anything | ||
| 12 | my $mtl = SDL::Tutorial::3DWorld::MTL->new( | ||
| 13 | file => 'mymaterials.mtl', | ||
| 14 | ); | ||
| 15 | |||
| 16 | # Locate a material in the library | ||
| 17 | $mtl->material('glass'); | ||
| 18 | |||
| 19 | =head1 DESCRIPTION | ||
| 20 | |||
| 21 | B<SDL::Tutorial::3DWorld::MTL> provides a basic implementation of an | ||
| 22 | MTL file parser. MTL files are libraries of material (surface) definitions | ||
| 23 | to be consumed (by name) by OBJ models. | ||
| 24 | |||
| 25 | Given a file name, it will load the file and parse the contents into an | ||
| 26 | abstract set of material objects. However, no dependency files or textures | ||
| 27 | of the materials are loaded. Any materials that you use on real models | ||
| 28 | will need to be initialised before the objects they are applied to are | ||
| 29 | displayed. | ||
| 30 | |||
| 31 | The current implementation is extremely preliminary and functionality will | ||
| 32 | be gradually fleshed out over time. | ||
| 33 | |||
| 34 | =cut | ||
| 35 | |||
| 36 | 1 | ▶ | use 5.008; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 37 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 38 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 39 | 1 | ▶ | use IO::File (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 40 | 1 | ▶ | use File::Spec (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 41 | 1 | ▶ | use Params::Util '_INSTANCE'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 42 | 0 | ▶ | use OpenGL ':all'; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 43 | use OpenGL::List (); | ||
| 44 | use SDL::Tutorial::3DWorld::Model (); | ||
| 45 | use SDL::Tutorial::3DWorld::Texture (); | ||
| 46 | use SDL::Tutorial::3DWorld::Material (); | ||
| 47 | use SDL::Tutorial::3DWorld::Asset (); | ||
| 48 | |||
| 49 | |||
| 50 | our $VERSION = '0.33'; | ||
| 51 | |||
| 52 | |||
| 53 | |||
| 54 | |||
| 55 | |||
| 56 | ###################################################################### | ||
| 57 | # Constructor and Accessors | ||
| 58 | |||
| 59 | sub new { | ||
| 60 | my $class = shift; | ||
| 61 | my $self = bless { @_ }, $class; | ||
| 62 | |||
| 63 | # Check param | ||
| 64 | my $file = $self->file; | ||
| 65 | unless ( -f $file ) { | ||
| 66 | die "The model file '$file' does not exists"; | ||
| 67 | } | ||
| 68 | |||
| 69 | # Bootstrap a asset if we were not passed one. | ||
| 70 | unless ( $self->{asset} ) { | ||
| 71 | my $directory = $self->file; | ||
| 72 | $directory =~ s/[\w\._-]+$//; | ||
| 73 | $self->{asset} = SDL::Tutorial::3DWorld::Asset->new( | ||
| 74 | directory => $directory, | ||
| 75 | ); | ||
| 76 | } | ||
| 77 | unless ( _INSTANCE($self->asset, 'SDL::Tutorial::3DWorld::Asset') ) { | ||
| 78 | die "Missing or invalid asset"; | ||
| 79 | } | ||
| 80 | |||
| 81 | # We start with an empty material library | ||
| 82 | $self->{material} = { }; | ||
| 83 | |||
| 84 | return $self; | ||
| 85 | } | ||
| 86 | |||
| 87 | sub file { | ||
| 88 | $_[0]->{file} | ||
| 89 | } | ||
| 90 | |||
| 91 | sub asset { | ||
| 92 | $_[0]->{asset}; | ||
| 93 | } | ||
| 94 | |||
| 95 | |||
| 96 | |||
| 97 | |||
| 98 | |||
| 99 | ###################################################################### | ||
| 100 | # Main Methods | ||
| 101 | |||
| 102 | sub material { | ||
| 103 | my $self = shift; | ||
| 104 | my $name = shift; | ||
| 105 | my $material = $self->{material}->{$name} or return undef; | ||
| 106 | |||
| 107 | # Elevate from hash to object on demand | ||
| 108 | unless ( _INSTANCE($material, 'SDL::Tutorial::3DWorld::Material') ) { | ||
| 109 | $material = SDL::Tutorial::3DWorld::Material->new( | ||
| 110 | %$material, | ||
| 111 | ); | ||
| 112 | $self->{material}->{$name} = $material; | ||
| 113 | } | ||
| 114 | |||
| 115 | return $material; | ||
| 116 | } | ||
| 117 | |||
| 118 | sub names { | ||
| 119 | my $self = shift; | ||
| 120 | my @names = sort keys %{$self->{material}}; | ||
| 121 | return @names; | ||
| 122 | } | ||
| 123 | |||
| 124 | sub init { | ||
| 125 | my $self = shift; | ||
| 126 | my $handle = IO::File->new( $self->file, 'r' ); | ||
| 127 | $self->parse( $handle ); | ||
| 128 | $handle->close; | ||
| 129 | return 1; | ||
| 130 | } | ||
| 131 | |||
| 132 | |||
| 133 | |||
| 134 | |||
| 135 | |||
| 136 | ###################################################################### | ||
| 137 | # Parsing | ||
| 138 | |||
| 139 | sub parse { | ||
| 140 | my $self = shift; | ||
| 141 | my $handle = shift; | ||
| 142 | |||
| 143 | # Parser state | ||
| 144 | my $material = undef; | ||
| 145 | |||
| 146 | # Parse the file | ||
| 147 | while ( 1 ) { | ||
| 148 | my $line = $handle->getline; | ||
| 149 | last unless defined $line; | ||
| 150 | |||
| 151 | # Remove blank lines, trailing whitespace and comments | ||
| 152 | $line =~ s/\s*(?:#.+)[\012\015]*\z//; | ||
| 153 | $line =~ m/\S/ or next; | ||
| 154 | |||
| 155 | # Parse the dispatch the line | ||
| 156 | my @words = split /\s+/, $line; | ||
| 157 | my $command = lc shift @words; | ||
| 158 | if ( $command eq 'newmtl' ) { | ||
| 159 | $material = { | ||
| 160 | ambient => [ 0, 0, 0 ], | ||
| 161 | diffuse => [ 0, 0, 0 ], | ||
| 162 | specular => [ 0, 0, 0 ], | ||
| 163 | dissolve => 1, | ||
| 164 | }; | ||
| 165 | $self->{material}->{$words[0]} = $material; | ||
| 166 | |||
| 167 | } elsif ( $command eq 'ka' ) { | ||
| 168 | $material->{ambient} = [ @words ]; | ||
| 169 | |||
| 170 | } elsif ( $command eq 'kd' ) { | ||
| 171 | $material->{diffuse} = [ @words ]; | ||
| 172 | |||
| 173 | } elsif ( $command eq 'ks' ) { | ||
| 174 | $material->{specular} = [ @words ]; | ||
| 175 | |||
| 176 | } elsif ( $command eq 'ns' ) { | ||
| 177 | $material->{shininess} = $words[0]; | ||
| 178 | |||
| 179 | } elsif ( $command eq 'd' or $command eq 'tr' ) { | ||
| 180 | $material->{dissolve} = $words[0]; | ||
| 181 | |||
| 182 | } elsif ( $command eq 'illum' ) { | ||
| 183 | # Illumination mode | ||
| 184 | # 0. Color on and Ambient off | ||
| 185 | # 1. Color on and Ambient on | ||
| 186 | # 2. Highlight on | ||
| 187 | # 3. Reflection on and Ray trace on | ||
| 188 | # 4. Transparency: Glass on, Reflection: Ray trace on | ||
| 189 | # 5. Reflection: Fresnel on and Ray trace on | ||
| 190 | # 6. Transparency: Refraction on, Reflection: Fresnel off and Ray trace on | ||
| 191 | # 7. Transparency: Refraction on, Reflection: Fresnel on and Ray trace on | ||
| 192 | # 8. Reflection on and Ray trace off | ||
| 193 | # 9. Transparency: Glass on, Reflection: Ray trace off | ||
| 194 | # 10. Casts shadows onto invisible surfaces | ||
| 195 | $material->{illumination} = $words[0]; | ||
| 196 | |||
| 197 | } else { | ||
| 198 | # Ignore unsupported lines | ||
| 199 | } | ||
| 200 | } | ||
| 201 | |||
| 202 | return 1; | ||
| 203 | } | ||
| 204 | |||
| 205 | 1; | ||
| 206 | |||
| 207 | =pod | ||
| 208 | |||
| 209 | =head1 SUPPORT | ||
| 210 | |||
| 211 | Bugs should be reported via the CPAN bug tracker at | ||
| 212 | |||
| 213 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=OpenGL-RWX> | ||
| 214 | |||
| 215 | =head1 AUTHOR | ||
| 216 | |||
| 217 | Adam Kennedy E<lt>adamk@cpan.orgE<gt> | ||
| 218 | |||
| 219 | =head1 SEE ALSO | ||
| 220 | |||
| 221 | L<OpenGL> | ||
| 222 | |||
| 223 | =head1 COPYRIGHT | ||
| 224 | |||
| 225 | Copyright 2010 Adam Kennedy. | ||
| 226 | |||
| 227 | This program is free software; you can redistribute | ||
| 228 | it and/or modify it under the same terms as Perl itself. | ||
| 229 | |||
| 230 | The full text of the license can be found in the | ||
| 231 | LICENSE file included with this module. | ||
| 232 | |||
| 233 | =cut | ||