| 1 | package SDL::Tutorial::3DWorld::Model; | ||
| 2 | |||
| 3 | =pod | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | SDL::Tutorial::3DWorld::Model - Generic support for on disk model parsers | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | # Create the object but don't load anything | ||
| 12 | my $model = SDL::Tutorial::3DWorld::Model->new( | ||
| 13 | file => 'mymodel.rwx', | ||
| 14 | ); | ||
| 15 | |||
| 16 | # Load the model into OpenGL | ||
| 17 | $model->init; | ||
| 18 | |||
| 19 | # Render the model into the current scene | ||
| 20 | $model->display; | ||
| 21 | |||
| 22 | =head1 DESCRIPTION | ||
| 23 | |||
| 24 | B<SDL::Tutorial::3DWorld::Model> provides shared functionality across all | ||
| 25 | of the different model file implementations. | ||
| 26 | |||
| 27 | =cut | ||
| 28 | |||
| 29 | use 5.008; | ||
| 30 | use strict; | ||
| 31 | use warnings; | ||
| 32 | use IO::File (); | ||
| 33 | use Params::Util (); | ||
| 34 | use OpenGL::List (); | ||
| 35 | use SDL::Tutorial::3DWorld::Asset (); | ||
| 36 | use SDL::Tutorial::3DWorld::OpenGL (); | ||
| 37 | |||
| 38 | our $VERSION = '0.33'; | ||
| 39 | |||
| 40 | # Global Model Cache. | ||
| 41 | # Since there are currently no optional model settings and model | ||
| 42 | # objects are immutable, we can do a simple file-based key for models. | ||
| 43 | our %CACHE = (); | ||
| 44 | |||
| 45 | |||
| 46 | |||
| 47 | |||
| 48 | |||
| 49 | ###################################################################### | ||
| 50 | # Constructor and Accessors | ||
| 51 | |||
| 52 | sub new { | ||
| 53 | my $class = shift; | ||
| 54 | my $self = bless { @_ }, $class; | ||
| 55 | |||
| 56 | # Make sure the filename is absolute so we have consistent keys | ||
| 57 | # for the global texture cache. Return from the cache if we can. | ||
| 58 | my $key = File::Spec->rel2abs( $self->file ); | ||
| 59 | return $CACHE{$key} if $CACHE{$key}; | ||
| 60 | |||
| 61 | # Check param | ||
| 62 | my $file = $self->file; | ||
| 63 | unless ( -f $file ) { | ||
| 64 | die "The model file '$file' does not exists"; | ||
| 65 | } | ||
| 66 | |||
| 67 | # Bootstrap a asset if we were not passed one. | ||
| 68 | unless ( $self->{asset} ) { | ||
| 69 | my $directory = $self->file; | ||
| 70 | $directory =~ s/[\w\._-]+$//; | ||
| 71 | $self->{asset} = SDL::Tutorial::3DWorld::Asset->new( | ||
| 72 | directory => $directory, | ||
| 73 | ); | ||
| 74 | } | ||
| 75 | unless ( Params::Util::_INSTANCE($self->asset, 'SDL::Tutorial::3DWorld::Asset') ) { | ||
| 76 | die "Missing or invalid asset"; | ||
| 77 | } | ||
| 78 | |||
| 79 | # Save the new model to the global cache | ||
| 80 | $CACHE{$key} = $self; | ||
| 81 | |||
| 82 | return $self; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub file { | ||
| 86 | $_[0]->{file} | ||
| 87 | } | ||
| 88 | |||
| 89 | sub asset { | ||
| 90 | $_[0]->{asset}; | ||
| 91 | } | ||
| 92 | |||
| 93 | sub list { | ||
| 94 | $_[0]->{list}; | ||
| 95 | } | ||
| 96 | |||
| 97 | |||
| 98 | |||
| 99 | |||
| 100 | |||
| 101 | ###################################################################### | ||
| 102 | # Main Methods | ||
| 103 | |||
| 104 | sub init { | ||
| 105 | my $self = shift; | ||
| 106 | unless ( defined $self->{list} ) { | ||
| 107 | my $handle = IO::File->new( $self->file, 'r' ); | ||
| 108 | $self->{list} = $self->parse( $handle ); | ||
| 109 | $handle->close; | ||
| 110 | } | ||
| 111 | return 1; | ||
| 112 | } | ||
| 113 | |||
| 114 | sub parse { | ||
| 115 | die "CODE INCOMPLETE"; | ||
| 116 | } | ||
| 117 | |||
| 118 | sub display { | ||
| 119 | OpenGL::glCallList( $_[0]->{list} ); | ||
| 120 | } | ||
| 121 | |||
| 122 | 1; | ||
| 123 | |||
| 124 | =pod | ||
| 125 | |||
| 126 | =head1 SUPPORT | ||
| 127 | |||
| 128 | Bugs should be reported via the CPAN bug tracker at | ||
| 129 | |||
| 130 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=OpenGL-RWX> | ||
| 131 | |||
| 132 | =head1 AUTHOR | ||
| 133 | |||
| 134 | Adam Kennedy E<lt>adamk@cpan.orgE<gt> | ||
| 135 | |||
| 136 | =head1 SEE ALSO | ||
| 137 | |||
| 138 | L<OpenGL> | ||
| 139 | |||
| 140 | =head1 COPYRIGHT | ||
| 141 | |||
| 142 | Copyright 2010 Adam Kennedy. | ||
| 143 | |||
| 144 | This program is free software; you can redistribute | ||
| 145 | it and/or modify it under the same terms as Perl itself. | ||
| 146 | |||
| 147 | The full text of the license can be found in the | ||
| 148 | LICENSE file included with this module. | ||
| 149 | |||
| 150 | =cut |