| 1 | package SDL::Tutorial::3DWorld::Actor::Model; | ||
| 2 | |||
| 3 | =pod | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | SDL::Tutorial::3DWorld::Actor::Model - An actor loaded from a RWX file | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | # Define the model location | ||
| 12 | my $model = SDL::Tutorial::3DWorld::Actor::Model->new( | ||
| 13 | file => 'torus.rwx', | ||
| 14 | ); | ||
| 15 | |||
| 16 | # Load and compile the model into memory | ||
| 17 | $model->init; | ||
| 18 | |||
| 19 | # Render the model into the current scene | ||
| 20 | $model->display; | ||
| 21 | |||
| 22 | =head1 DESCRIPTION | ||
| 23 | |||
| 24 | This is an experimental module for loading large or complex shapes from | ||
| 25 | RWX model files on disk. | ||
| 26 | |||
| 27 | =cut | ||
| 28 | |||
| 29 | use 5.008; | ||
| 30 | use strict; | ||
| 31 | use warnings; | ||
| 32 | use OpenGL::List (); | ||
| 33 | use SDL::Tutorial::3DWorld (); | ||
| 34 | use SDL::Tutorial::3DWorld::Actor (); | ||
| 35 | use SDL::Tutorial::3DWorld::OBJ (); | ||
| 36 | use SDL::Tutorial::3DWorld::RWX (); | ||
| 37 | use SDL::Tutorial::3DWorld::Bound; | ||
| 38 | |||
| 39 | our $VERSION = '0.33'; | ||
| 40 | our @ISA = 'SDL::Tutorial::3DWorld::Actor'; | ||
| 41 | |||
| 42 | sub new { | ||
| 43 | my $self = shift->SUPER::new(@_); | ||
| 44 | |||
| 45 | # Map to the absolute disk file | ||
| 46 | $self->{file} = SDL::Tutorial::3DWorld->sharefile( $self->{file} ); | ||
| 47 | unless ( -f $self->{file} ) { | ||
| 48 | die "Model file '$self->{file}' does not exist"; | ||
| 49 | } | ||
| 50 | |||
| 51 | # Create the type-specific object | ||
| 52 | if ( $self->{file} =~ /\.rwx$/ ) { | ||
| 53 | $self->{model} = SDL::Tutorial::3DWorld::RWX->new( | ||
| 54 | file => $self->{file}, | ||
| 55 | ); | ||
| 56 | |||
| 57 | } elsif ( $self->{file} =~ /\.obj$/ ) { | ||
| 58 | $self->{model} = SDL::Tutorial::3DWorld::OBJ->new( | ||
| 59 | file => $self->{file}, | ||
| 60 | plain => $self->{plain}, | ||
| 61 | ); | ||
| 62 | |||
| 63 | } else { | ||
| 64 | die "Unkown or unsupported file '$self->{file}'"; | ||
| 65 | } | ||
| 66 | |||
| 67 | return $self; | ||
| 68 | } | ||
| 69 | |||
| 70 | |||
| 71 | |||
| 72 | |||
| 73 | |||
| 74 | ###################################################################### | ||
| 75 | # Engine Methods | ||
| 76 | |||
| 77 | sub init { | ||
| 78 | my $self = shift; | ||
| 79 | my $model = $self->{model}; | ||
| 80 | my $position = $self->{position}; | ||
| 81 | my $scale = $self->{scale}; | ||
| 82 | my $orient = $self->{orient}; | ||
| 83 | $self->SUPER::init(@_); | ||
| 84 | |||
| 85 | # Load the model display list | ||
| 86 | $model->init; | ||
| 87 | |||
| 88 | # Do we need blending support? | ||
| 89 | if ( $model->{blending} ) { | ||
| 90 | $self->{blending} = 1; | ||
| 91 | } | ||
| 92 | |||
| 93 | # Get the bounding box from the model | ||
| 94 | if ( $scale ) { | ||
| 95 | $self->{bound} = SDL::Tutorial::3DWorld::Bound->box( | ||
| 96 | $model->{box}->[0] * $scale->[0], | ||
| 97 | $model->{box}->[1] * $scale->[1], | ||
| 98 | $model->{box}->[2] * $scale->[2], | ||
| 99 | $model->{box}->[3] * $scale->[0], | ||
| 100 | $model->{box}->[4] * $scale->[1], | ||
| 101 | $model->{box}->[5] * $scale->[2], | ||
| 102 | ); | ||
| 103 | } else { | ||
| 104 | $self->{bound} = SDL::Tutorial::3DWorld::Bound->box( | ||
| 105 | @{ $model->{box} }, | ||
| 106 | ); | ||
| 107 | } | ||
| 108 | |||
| 109 | # Static model optimisations | ||
| 110 | unless ( $self->{velocity} ) { | ||
| 111 | # Compile the entire display routine | ||
| 112 | $self->{display} = OpenGL::List::glpList { | ||
| 113 | OpenGL::glPushMatrix(); | ||
| 114 | OpenGL::glTranslatef( @$position ); | ||
| 115 | if ( $scale ) { | ||
| 116 | # If we are going to be doing scaling (in GL) the underlying | ||
| 117 | # matrix operations in OpenGL will screw up the normal vectors | ||
| 118 | # and break the lighting badly. | ||
| 119 | # We need to enable normalisation for this model. This makes the | ||
| 120 | # drawing slower but prevents shading corruption. | ||
| 121 | # If the object's scaling is going to be static (i.e. the object | ||
| 122 | # won't be dynamically changing sizes) it is much better to do | ||
| 123 | # the normal correction once in advance. | ||
| 124 | # More details at the following URL. | ||
| 125 | # http://www.opengl.org/resources/features/KilgardTechniques/oglpitfall/ | ||
| 126 | OpenGL::glEnable( OpenGL::GL_NORMALIZE ); | ||
| 127 | OpenGL::glScalef( @$scale ); | ||
| 128 | OpenGL::glRotatef( @$orient ) if $orient; | ||
| 129 | OpenGL::glCallList( $model->{list} ); | ||
| 130 | OpenGL::glDisable( OpenGL::GL_NORMALIZE ); | ||
| 131 | } else { | ||
| 132 | OpenGL::glRotatef( @$orient ) if $orient; | ||
| 133 | OpenGL::glCallList( $model->{list} ); | ||
| 134 | } | ||
| 135 | OpenGL::glPopMatrix(); | ||
| 136 | }; | ||
| 137 | } | ||
| 138 | |||
| 139 | return 1; | ||
| 140 | } | ||
| 141 | |||
| 142 | sub display { | ||
| 143 | my $self = shift; | ||
| 144 | |||
| 145 | # Move to the correct location | ||
| 146 | $self->SUPER::display(@_); | ||
| 147 | |||
| 148 | # Render the model | ||
| 149 | if ( $self->{scale} ) { | ||
| 150 | # This is a repeat of the above for the non-optimised case | ||
| 151 | OpenGL::glEnable( OpenGL::GL_NORMALIZE ); | ||
| 152 | $self->{model}->display; | ||
| 153 | OpenGL::glDisable( OpenGL::GL_NORMALIZE ); | ||
| 154 | } else { | ||
| 155 | $self->{model}->display; | ||
| 156 | } | ||
| 157 | |||
| 158 | return; | ||
| 159 | } | ||
| 160 | |||
| 161 | 1; | ||
| 162 | |||
| 163 | =pod | ||
| 164 | |||
| 165 | =head1 SUPPORT | ||
| 166 | |||
| 167 | Bugs should be reported via the CPAN bug tracker at | ||
| 168 | |||
| 169 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=SDL-Tutorial-3DWorld> | ||
| 170 | |||
| 171 | =head1 AUTHOR | ||
| 172 | |||
| 173 | Adam Kennedy E<lt>adamk@cpan.orgE<gt> | ||
| 174 | |||
| 175 | =head1 SEE ALSO | ||
| 176 | |||
| 177 | L<SDL>, L<OpenGL> | ||
| 178 | |||
| 179 | =head1 COPYRIGHT | ||
| 180 | |||
| 181 | Copyright 2010 Adam Kennedy. | ||
| 182 | |||
| 183 | This program is free software; you can redistribute | ||
| 184 | it and/or modify it under the same terms as Perl itself. | ||
| 185 | |||
| 186 | The full text of the license can be found in the | ||
| 187 | LICENSE file included with this module. | ||
| 188 | |||
| 189 | =cut |