| 1 | package SDL::Tutorial::3DWorld::Actor; | ||
| 2 | |||
| 3 | =pod | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | SDL::Tutorial::3DWorld::Actor - A moving object within the game world | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | # Create a vertical stack of teapots | ||
| 12 | my @stack = (); | ||
| 13 | foreach my $height ( 1 .. 10 ) { | ||
| 14 | push @stack, SDL::Tutorial::3DWorld::Actor->new( | ||
| 15 | X => 0, | ||
| 16 | Y => $height * 0.30, # Each teapot is 30cm high | ||
| 17 | Z => 0, | ||
| 18 | ); | ||
| 19 | } | ||
| 20 | |||
| 21 | =head1 DESCRIPTION | ||
| 22 | |||
| 23 | Within the game, the term "Actor" is used to describe anything that has | ||
| 24 | a shape and moves around the world based on it's own set of rules. | ||
| 25 | |||
| 26 | In practice, an actor could be anything from a bullet or a grenade flying | ||
| 27 | through the air, to a fully articulated roaring dragon with flaming breath | ||
| 28 | and it's own artificial intelligence. | ||
| 29 | |||
| 30 | To the game engine, all of these "actors" are basically the same. They are | ||
| 31 | merely things that need to describe where they are and what they look like | ||
| 32 | each time the engine wants to render a frame. | ||
| 33 | |||
| 34 | In this demonstration, the default actor is a 30cm x 30cm teapot. We are | ||
| 35 | using a teapot because it is the "official" test mesh object for OpenGL | ||
| 36 | and is built directly into the library itself via the C<glutCreateTeapot> | ||
| 37 | function. | ||
| 38 | |||
| 39 | =head1 METHODS | ||
| 40 | |||
| 41 | =cut | ||
| 42 | |||
| 43 | use 5.008; | ||
| 44 | use strict; | ||
| 45 | use warnings; | ||
| 46 | use SDL::Tutorial::3DWorld::OpenGL (); | ||
| 47 | use SDL::Tutorial::3DWorld::Material (); | ||
| 48 | |||
| 49 | our $VERSION = '0.33'; | ||
| 50 | |||
| 51 | =head2 new | ||
| 52 | |||
| 53 | my $teapot = SDL::Tutorial::3DWorld::Actor->new; | ||
| 54 | |||
| 55 | The C<new> constructor is used to create a new actor within the 3D World. | ||
| 56 | |||
| 57 | In the demonstration implementation, the default actor consists of a teapot. | ||
| 58 | |||
| 59 | =cut | ||
| 60 | |||
| 61 | sub new { | ||
| 62 | my $class = shift; | ||
| 63 | my $self = bless { | ||
| 64 | # Most objects are stored at the correct size | ||
| 65 | scale => 0, | ||
| 66 | |||
| 67 | # Place objects at the origin by default | ||
| 68 | position => [ 0, 0, 0 ], | ||
| 69 | |||
| 70 | # Do not rotate by default | ||
| 71 | orient => 0, | ||
| 72 | |||
| 73 | # Actors with non-vector velocity are shortcut by the main | ||
| 74 | # move routine | ||
| 75 | velocity => 0, | ||
| 76 | |||
| 77 | # Most things in the world are solid | ||
| 78 | blending => 0, | ||
| 79 | |||
| 80 | # Most things are visible by default | ||
| 81 | hidden => 0, | ||
| 82 | |||
| 83 | # Override defaults | ||
| 84 | @_, | ||
| 85 | }, $class; | ||
| 86 | |||
| 87 | # Automatically support uniform scaling | ||
| 88 | if ( $self->{scale} and not ref $self->{scale} ) { | ||
| 89 | $self->{scale} = [ | ||
| 90 | $self->{scale}, | ||
| 91 | $self->{scale}, | ||
| 92 | $self->{scale}, | ||
| 93 | ]; | ||
| 94 | } | ||
| 95 | |||
| 96 | # Upgrade material parameters to material object | ||
| 97 | if ( ref $self->{material} eq 'HASH' ) { | ||
| 98 | $self->{material} = SDL::Tutorial::3DWorld::Material->new( | ||
| 99 | %{$self->{material}} | ||
| 100 | ); | ||
| 101 | } | ||
| 102 | |||
| 103 | return $self; | ||
| 104 | } | ||
| 105 | |||
| 106 | =pod | ||
| 107 | |||
| 108 | =head2 X | ||
| 109 | |||
| 110 | The C<X> accessor provides the location of the actor in metres on the east | ||
| 111 | to west dimension within the 3D world. The positive direction is east. | ||
| 112 | |||
| 113 | =cut | ||
| 114 | |||
| 115 | sub X { | ||
| 116 | $_[0]->{position}->[0]; | ||
| 117 | } | ||
| 118 | |||
| 119 | =pod | ||
| 120 | |||
| 121 | =head2 Y | ||
| 122 | |||
| 123 | The C<Y> accessor is location of the actor in metres on the vertical | ||
| 124 | dimension within the 3D world. The positive direction is up. | ||
| 125 | |||
| 126 | =cut | ||
| 127 | |||
| 128 | sub Y { | ||
| 129 | $_[0]->{position}->[1]; | ||
| 130 | } | ||
| 131 | |||
| 132 | =pod | ||
| 133 | |||
| 134 | =head2 Z | ||
| 135 | |||
| 136 | The C<Z> accessor provides the location of the camera in metres on the north | ||
| 137 | to south dimension within the 3D world. The positive direction is north. | ||
| 138 | |||
| 139 | =cut | ||
| 140 | |||
| 141 | sub Z { | ||
| 142 | $_[0]->{position}->[2]; | ||
| 143 | } | ||
| 144 | |||
| 145 | =pod | ||
| 146 | |||
| 147 | =head2 position | ||
| 148 | |||
| 149 | The C<position> accessor provides the location of the camera as a 3 element | ||
| 150 | array reference of the structure C<[ X, Y, Z ]>. | ||
| 151 | |||
| 152 | =cut | ||
| 153 | |||
| 154 | sub position { | ||
| 155 | $_[0]->{position}; | ||
| 156 | } | ||
| 157 | |||
| 158 | |||
| 159 | |||
| 160 | |||
| 161 | |||
| 162 | ###################################################################### | ||
| 163 | # General Methods | ||
| 164 | |||
| 165 | =pod | ||
| 166 | |||
| 167 | =head2 box | ||
| 168 | |||
| 169 | The C<box> method returns the bounding box for the object if it has one, | ||
| 170 | relative to the position of the object. | ||
| 171 | |||
| 172 | =cut | ||
| 173 | |||
| 174 | sub box { | ||
| 175 | $_[0]->{box}; | ||
| 176 | } | ||
| 177 | |||
| 178 | =pod | ||
| 179 | |||
| 180 | =head2 bounding | ||
| 181 | |||
| 182 | The C<bounding> method returns the bounding box for the object if it has | ||
| 183 | one, relative to the world origin. | ||
| 184 | |||
| 185 | The default implementation of the C<bounding> method will take the actors | ||
| 186 | position-relative bounding C<box> and combine it with the relative | ||
| 187 | C<position> of the actor to get the world-relative box. | ||
| 188 | |||
| 189 | =cut | ||
| 190 | |||
| 191 | sub bounding { | ||
| 192 | my $self = shift; | ||
| 193 | my $position = $self->{position} or return (); | ||
| 194 | my $box = ($self->{box} or $self->box) or return (); | ||
| 195 | |||
| 196 | # Quick version if not scaling | ||
| 197 | unless ( $self->{scale} ) { | ||
| 198 | return ( | ||
| 199 | $box->[0] + $position->[0], | ||
| 200 | $box->[1] + $position->[1], | ||
| 201 | $box->[2] + $position->[2], | ||
| 202 | $box->[3] + $position->[0], | ||
| 203 | $box->[4] + $position->[1], | ||
| 204 | $box->[5] + $position->[2], | ||
| 205 | ); | ||
| 206 | } | ||
| 207 | |||
| 208 | # Full version with scaling support | ||
| 209 | my $scale = $self->{scale}; | ||
| 210 | return ( | ||
| 211 | $box->[0] * $scale->[0] + $position->[0], | ||
| 212 | $box->[1] * $scale->[1] + $position->[1], | ||
| 213 | $box->[2] * $scale->[2] + $position->[2], | ||
| 214 | $box->[3] * $scale->[0] + $position->[0], | ||
| 215 | $box->[4] * $scale->[1] + $position->[1], | ||
| 216 | $box->[5] * $scale->[2] + $position->[2], | ||
| 217 | ); | ||
| 218 | } | ||
| 219 | |||
| 220 | |||
| 221 | |||
| 222 | |||
| 223 | |||
| 224 | ###################################################################### | ||
| 225 | # Engine Interface | ||
| 226 | |||
| 227 | sub init { | ||
| 228 | my $self = shift; | ||
| 229 | |||
| 230 | # Initialise the material if it has one | ||
| 231 | if ( $self->{material} ) { | ||
| 232 | $self->{material}->init; | ||
| 233 | } | ||
| 234 | |||
| 235 | return 1; | ||
| 236 | } | ||
| 237 | |||
| 238 | sub display { | ||
| 239 | my $self = shift; | ||
| 240 | |||
| 241 | # Translate, scale and rotate to the position of the actor | ||
| 242 | OpenGL::glTranslatef( @{$self->{position}} ); | ||
| 243 | |||
| 244 | # Scale if needed. | ||
| 245 | if ( $self->{scale} ) { | ||
| 246 | OpenGL::glScalef( @{$self->{scale}} ); | ||
| 247 | } | ||
| 248 | |||
| 249 | # Rotate if needed | ||
| 250 | if ( $self->{orient} ) { | ||
| 251 | OpenGL::glRotatef( @{$self->{orient}} ); | ||
| 252 | } | ||
| 253 | |||
| 254 | return; | ||
| 255 | } | ||
| 256 | |||
| 257 | sub move { | ||
| 258 | my $self = shift; | ||
| 259 | my $step = shift; | ||
| 260 | |||
| 261 | # If it has velocity, change the actor's position | ||
| 262 | $self->{position}->[0] += $self->{velocity}->[0] * $step; | ||
| 263 | $self->{position}->[1] += $self->{velocity}->[1] * $step; | ||
| 264 | $self->{position}->[2] += $self->{velocity}->[2] * $step; | ||
| 265 | |||
| 266 | # Rotate if we need to | ||
| 267 | if ( $self->{orient} and $self->{rotate} ) { | ||
| 268 | $self->{orient}->[0] += $self->{rotate} * $step; | ||
| 269 | } | ||
| 270 | |||
| 271 | return; | ||
| 272 | } | ||
| 273 | |||
| 274 | =pod | ||
| 275 | |||
| 276 | =head1 SUPPORT | ||
| 277 | |||
| 278 | Bugs should be reported via the CPAN bug tracker at | ||
| 279 | |||
| 280 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=SDL-Tutorial-3DWorld> | ||
| 281 | |||
| 282 | =head1 AUTHOR | ||
| 283 | |||
| 284 | Adam Kennedy E<lt>adamk@cpan.orgE<gt> | ||
| 285 | |||
| 286 | =head1 SEE ALSO | ||
| 287 | |||
| 288 | L<SDL>, L<OpenGL> | ||
| 289 | |||
| 290 | =head1 COPYRIGHT | ||
| 291 | |||
| 292 | Copyright 2010 Adam Kennedy. | ||
| 293 | |||
| 294 | This program is free software; you can redistribute | ||
| 295 | it and/or modify it under the same terms as Perl itself. | ||
| 296 | |||
| 297 | The full text of the license can be found in the | ||
| 298 | LICENSE file included with this module. | ||
| 299 | |||
| 300 | =cut |