| 1 | package SDL::Tutorial::3DWorld::Actor::TextureCube; | ||
| 2 | |||
| 3 | =pod | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | SDL::Tutorial::3DWorld::Actor::TextureCube - Crates, companions and more... | ||
| 8 | |||
| 9 | =head1 DESCRIPTION | ||
| 10 | |||
| 11 | The C<TextureCube> is one of the staples of classic 3D games. | ||
| 12 | |||
| 13 | Most famously, it can be used to create the ubiquitous and stereotypical | ||
| 14 | crate model from classic first person shooters like Quake. | ||
| 15 | |||
| 16 | Since this is a relatively practical class you might actually realistically | ||
| 17 | use in a 3D world in large numbers we will also attempt to make use of | ||
| 18 | simple straight forward optimisation methods to get the cubes drawing | ||
| 19 | relatively quickly. | ||
| 20 | |||
| 21 | For convenience when stacking, the original on a C<TextureCube> is located | ||
| 22 | at the centre of the bottom face of the cube. So given a surface plane at | ||
| 23 | the original, a C<TextureCube> located at the origin will in effect be | ||
| 24 | sitting "on" the plane. | ||
| 25 | |||
| 26 | =head1 METHODS | ||
| 27 | |||
| 28 | =cut | ||
| 29 | |||
| 30 | use 5.008; | ||
| 31 | use strict; | ||
| 32 | use warnings; | ||
| 33 | use OpenGL; | ||
| 34 | use OpenGL::List (); | ||
| 35 | use Params::Util '_INSTANCE'; | ||
| 36 | use SDL::Tutorial::3DWorld::Actor (); | ||
| 37 | use SDL::Tutorial::3DWorld::Texture (); | ||
| 38 | use SDL::Tutorial::3DWorld::Material (); | ||
| 39 | use SDL::Tutorial::3DWorld::Bound; | ||
| 40 | |||
| 41 | our $VERSION = '0.33'; | ||
| 42 | our @ISA = 'SDL::Tutorial::3DWorld::Actor'; | ||
| 43 | |||
| 44 | =pod | ||
| 45 | |||
| 46 | =head2 new | ||
| 47 | |||
| 48 | # Sweet crate, familiar crate, err... flying crate? | ||
| 49 | my $crate = SDL::Tutorial::3DWorld::Actor::TextureCube->new( | ||
| 50 | size => 2, | ||
| 51 | velocity => [ 0.0, 1.0, 0.1 ], | ||
| 52 | material => { | ||
| 53 | texture => File::Spec->catfile( | ||
| 54 | File::ShareDir::dist_dir('SDL::Tutorial::3DWorld'), | ||
| 55 | 'crate1.jpg', | ||
| 56 | ), | ||
| 57 | ), | ||
| 58 | ); | ||
| 59 | |||
| 60 | The C<new> constructor creates a new textured cube. | ||
| 61 | |||
| 62 | In addition to the usual L<SDL::Tutorial::3DWorld::Actor> parameters, | ||
| 63 | it takes some additional parameters. | ||
| 64 | |||
| 65 | The C<size> parameter is the size of the cube in metres. Cubes grow in | ||
| 66 | size upwards from the base in the vertical plane, and outwards from the | ||
| 67 | centre on the horizontal plane. | ||
| 68 | |||
| 69 | The C<texture> parameter should be the name of the file containing the | ||
| 70 | texture to be used on all six sides of the cube. Alternatively, you can | ||
| 71 | if you wish pass in your own L<SDL::Tutorial::3DWorld::Texture> object. | ||
| 72 | |||
| 73 | =cut | ||
| 74 | |||
| 75 | sub new { | ||
| 76 | my $class = shift; | ||
| 77 | my %param = @_; | ||
| 78 | if ( $param{texture} and not $param{material} ) { | ||
| 79 | $param{material} = { | ||
| 80 | texture => delete $param{texture}, | ||
| 81 | }; | ||
| 82 | } | ||
| 83 | |||
| 84 | # Create the object as normal | ||
| 85 | my $self = $class->SUPER::new( %param ); | ||
| 86 | |||
| 87 | # Default the size to one unit | ||
| 88 | $self->{size} = 1; | ||
| 89 | |||
| 90 | # Set the scale amount to match the size | ||
| 91 | $self->{scale} = [ | ||
| 92 | $self->{size} / 2, | ||
| 93 | $self->{size} / 2, | ||
| 94 | $self->{size} / 2, | ||
| 95 | ]; | ||
| 96 | |||
| 97 | return $self; | ||
| 98 | } | ||
| 99 | |||
| 100 | |||
| 101 | |||
| 102 | |||
| 103 | |||
| 104 | ###################################################################### | ||
| 105 | # Engine Methods | ||
| 106 | |||
| 107 | sub init { | ||
| 108 | my $self = shift; | ||
| 109 | $self->SUPER::init(@_); | ||
| 110 | |||
| 111 | # Compile the texture cube in either a vanilla form, or optimised | ||
| 112 | # form if the cube isn't moving. | ||
| 113 | if ( $self->{velocity} ) { | ||
| 114 | $self->{list} = OpenGL::List::glpList { | ||
| 115 | $self->compile; | ||
| 116 | }; | ||
| 117 | } else { | ||
| 118 | $self->{display} = OpenGL::List::glpList { | ||
| 119 | OpenGL::glPushMatrix(); | ||
| 120 | OpenGL::glTranslatef( @{$self->{position}} ); | ||
| 121 | if ( $self->{scale} ) { | ||
| 122 | OpenGL::glScalef( @{$self->{scale}} ); | ||
| 123 | } | ||
| 124 | $self->compile; | ||
| 125 | OpenGL::glPopMatrix(); | ||
| 126 | }; | ||
| 127 | } | ||
| 128 | |||
| 129 | # Define the bounding box | ||
| 130 | $self->{bound} = SDL::Tutorial::3DWorld::Bound->box( | ||
| 131 | map { $_ * $self->{size} / 2 } ( -1, 0, -1, 1, 2, 1 ) | ||
| 132 | ); | ||
| 133 | |||
| 134 | return; | ||
| 135 | } | ||
| 136 | |||
| 137 | sub display { | ||
| 138 | my $self = shift; | ||
| 139 | $self->SUPER::display(@_); | ||
| 140 | |||
| 141 | # Call the compiled form of the draw method below that has been | ||
| 142 | # pre-compiled into the OpenGL context. | ||
| 143 | glCallList( $self->{list} ); | ||
| 144 | } | ||
| 145 | |||
| 146 | # Compile the drawing instructions for the cube into an OpenGL | ||
| 147 | # "display list" (which is basically just a macro in GL terms). | ||
| 148 | # Using a list instead of manually doing each vector will remove | ||
| 149 | # off all of the Perl overheads, and even some of the C overheads. | ||
| 150 | sub compile { | ||
| 151 | my $self = shift; | ||
| 152 | |||
| 153 | # Set up the material | ||
| 154 | $self->{material}->display; | ||
| 155 | |||
| 156 | # Draw each of the quads for the cube | ||
| 157 | # NOTE: This is hardly "optimised" but will at least get us a | ||
| 158 | # working cube fairly quickly. | ||
| 159 | glBegin( GL_QUADS ); | ||
| 160 | |||
| 161 | # If the box has been scaled, the normal vectors will scaled as | ||
| 162 | # well, and so will no longer be unitised and will break lighting. | ||
| 163 | # One method of fixing this is to turn on GL_NORMALIZE, but this | ||
| 164 | # can be fairly expensive. | ||
| 165 | # One alternative is to scale the normal vectors by the inverse of | ||
| 166 | # the model scaling. These start wrong, but once the scaling has | ||
| 167 | # been applied the normals will end up unitised at the other end, | ||
| 168 | # and our lighting will still work. | ||
| 169 | # This approach is ideal for simple cubes with simple normals. | ||
| 170 | # Note, however, that if we compile these inverse-scaled normals | ||
| 171 | # into a display list this will only work if the object does not | ||
| 172 | # dynamically scale. | ||
| 173 | my $XN = 1 / $self->{scale}->[0]; | ||
| 174 | my $YN = 1 / $self->{scale}->[1]; | ||
| 175 | my $ZN = 1 / $self->{scale}->[2]; | ||
| 176 | |||
| 177 | # Draw the north face | ||
| 178 | glNormal3f( 0, 0, -$ZN ); | ||
| 179 | glTexCoord2f( 0, 0 ); glVertex3f( 1, 2, -1 ); # Top Left | ||
| 180 | glTexCoord2f( 0, 1 ); glVertex3f( 1, 0, -1 ); # Bottom Left | ||
| 181 | glTexCoord2f( 1, 1 ); glVertex3f( -1, 0, -1 ); # Bottom Right | ||
| 182 | glTexCoord2f( 1, 0 ); glVertex3f( -1, 2, -1 ); # Top Right | ||
| 183 | |||
| 184 | # Draw the east face | ||
| 185 | glNormal3f( $XN, 0, 0 ); | ||
| 186 | glTexCoord2f( 0, 0 ); glVertex3f( 1, 2, 1 ); # Top Left | ||
| 187 | glTexCoord2f( 0, 1 ); glVertex3f( 1, 0, 1 ); # Bottom Left | ||
| 188 | glTexCoord2f( 1, 1 ); glVertex3f( 1, 0, -1 ); # Bottom Right | ||
| 189 | glTexCoord2f( 1, 0 ); glVertex3f( 1, 2, -1 ); # Top Right | ||
| 190 | |||
| 191 | # Draw the south face | ||
| 192 | glNormal3f( 0, 0, $ZN ); | ||
| 193 | glTexCoord2f( 0, 0 ); glVertex3f( -1, 2, 1 ); # Top Left | ||
| 194 | glTexCoord2f( 0, 1 ); glVertex3f( -1, 0, 1 ); # Bottom Left | ||
| 195 | glTexCoord2f( 1, 1 ); glVertex3f( 1, 0, 1 ); # Bottom Right | ||
| 196 | glTexCoord2f( 1, 0 ); glVertex3f( 1, 2, 1 ); # Top Right | ||
| 197 | |||
| 198 | # Draw the west face | ||
| 199 | glNormal3f( -$ZN, 0, 0 ); | ||
| 200 | glTexCoord2f( 0, 0 ); glVertex3f( -1, 2, -1 ); # Top Left | ||
| 201 | glTexCoord2f( 0, 1 ); glVertex3f( -1, 0, -1 ); # Bottom Left | ||
| 202 | glTexCoord2f( 1, 1 ); glVertex3f( -1, 0, 1 ); # Bottom Right | ||
| 203 | glTexCoord2f( 1, 0 ); glVertex3f( -1, 2, 1 ); # Top Right | ||
| 204 | |||
| 205 | # Draw the up face | ||
| 206 | glNormal3f( 0, $YN, 0 ); | ||
| 207 | glTexCoord2f( 0, 0 ); glVertex3f( 1, 2, 1 ); # Top Left | ||
| 208 | glTexCoord2f( 0, 1 ); glVertex3f( 1, 2, -1 ); # Bottom Left | ||
| 209 | glTexCoord2f( 1, 1 ); glVertex3f( -1, 2, -1 ); # Bottom Right | ||
| 210 | glTexCoord2f( 1, 0 ); glVertex3f( -1, 2, 1 ); # Top Right | ||
| 211 | |||
| 212 | # Draw the down face | ||
| 213 | glNormal3f( 0, -$YN, 0 ); | ||
| 214 | glTexCoord2f( 0, 0 ); glVertex3f( 1, 0, -1 ); # Top Left | ||
| 215 | glTexCoord2f( 0, 1 ); glVertex3f( 1, 0, 1 ); # Bottom Left | ||
| 216 | glTexCoord2f( 1, 1 ); glVertex3f( -1, 0, 1 ); # Bottom Right | ||
| 217 | glTexCoord2f( 1, 0 ); glVertex3f( -1, 0, -1 ); # Top Right | ||
| 218 | |||
| 219 | # Finish drawing | ||
| 220 | glEnd(); | ||
| 221 | } | ||
| 222 | |||
| 223 | 1; | ||
| 224 | |||
| 225 | =pod | ||
| 226 | |||
| 227 | =head1 SUPPORT | ||
| 228 | |||
| 229 | Bugs should be reported via the CPAN bug tracker at | ||
| 230 | |||
| 231 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=SDL-Tutorial-3DWorld> | ||
| 232 | |||
| 233 | =head1 AUTHOR | ||
| 234 | |||
| 235 | Adam Kennedy E<lt>adamk@cpan.orgE<gt> | ||
| 236 | |||
| 237 | =head1 SEE ALSO | ||
| 238 | |||
| 239 | L<SDL>, L<OpenGL> | ||
| 240 | |||
| 241 | =head1 COPYRIGHT | ||
| 242 | |||
| 243 | Copyright 2010 Adam Kennedy. | ||
| 244 | |||
| 245 | This program is free software; you can redistribute | ||
| 246 | it and/or modify it under the same terms as Perl itself. | ||
| 247 | |||
| 248 | The full text of the license can be found in the | ||
| 249 | LICENSE file included with this module. | ||
| 250 | |||
| 251 | =cut |