| 1 | package SDL::Tutorial::3DWorld::Camera; | ||
| 2 | |||
| 3 | =pod | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | SDL::Tutorial::3DWorld::Camera - A movable viewpoint in the game world | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | # Start the camera 1.5 metres above the ground, 10 metres back from | ||
| 12 | # the world origin, looking north and slightly downwards. | ||
| 13 | my $camera = SDL::Tutorial::3DWorld::Camera->new( | ||
| 14 | X => 0, | ||
| 15 | Y => 1.5, | ||
| 16 | Z => 10, | ||
| 17 | angle => 0, | ||
| 18 | elevation => -5, | ||
| 19 | }; | ||
| 20 | |||
| 21 | =head1 DESCRIPTION | ||
| 22 | |||
| 23 | The B<SDL::Tutorial::3DWorld::Camera> represents the viewpoint that the | ||
| 24 | user controls to move through the 3D world. | ||
| 25 | |||
| 26 | In this initial skeleton code, the camera is fixed and cannot be moved. | ||
| 27 | |||
| 28 | =head1 METHODS | ||
| 29 | |||
| 30 | =cut | ||
| 31 | |||
| 32 | use 5.008; | ||
| 33 | use strict; | ||
| 34 | use warnings; | ||
| 35 | use SDL::Mouse (); | ||
| 36 | use SDL::Constants (); | ||
| 37 | use SDL::Tutorial::3DWorld::OpenGL (); | ||
| 38 | use SDL::Tutorial::3DWorld::Bound; # Import constants | ||
| 39 | |||
| 40 | our $VERSION = '0.33'; | ||
| 41 | |||
| 42 | use constant { | ||
| 43 | D2R => CORE::atan2(1,1) / 45, | ||
| 44 | ZFAR => 100, | ||
| 45 | }; | ||
| 46 | |||
| 47 | =pod | ||
| 48 | |||
| 49 | =head2 new | ||
| 50 | |||
| 51 | # Start the camera at the origin, facing north and looking at the horizon | ||
| 52 | my $camera = SDL::Tutorial::3DWorld::Camera->new( | ||
| 53 | X => 0, | ||
| 54 | Y => 0, | ||
| 55 | Z => 0, | ||
| 56 | angle => 0, | ||
| 57 | elevation => 0, | ||
| 58 | }; | ||
| 59 | |||
| 60 | The C<new> constructor creates a camera that serves as the primary | ||
| 61 | abstraction for the viewpoint as it moves through the world. | ||
| 62 | |||
| 63 | =cut | ||
| 64 | |||
| 65 | sub new { | ||
| 66 | my $class = shift; | ||
| 67 | my $self = bless { @_ }, $class; | ||
| 68 | |||
| 69 | # The default position of the camera is at 0,0,0 facing north | ||
| 70 | $self->{X} ||= 0; | ||
| 71 | $self->{Y} ||= 0; | ||
| 72 | $self->{Z} ||= 0; | ||
| 73 | $self->{angle} ||= 0; | ||
| 74 | $self->{elevation} ||= 0; | ||
| 75 | $self->{speed} ||= 0.2; | ||
| 76 | |||
| 77 | # The field of view and frustrum properties | ||
| 78 | $self->{width} ||= 1024; | ||
| 79 | $self->{height} ||= 768; | ||
| 80 | $self->{aspect} ||= $self->{width} / $self->{height}; | ||
| 81 | $self->{fovy} ||= 45; | ||
| 82 | $self->{fovx} ||= $self->{fovy} * $self->{aspect}; | ||
| 83 | $self->{znear} = 0.1 unless defined $self->{znear}; | ||
| 84 | $self->{zfar} ||= ZFAR; | ||
| 85 | |||
| 86 | # Preconvert stuff to radians | ||
| 87 | $self->{fovyr} ||= $self->{fovy} * D2R; | ||
| 88 | $self->{fovxr} ||= $self->{fovx} * D2R; | ||
| 89 | $self->{angler} ||= $self->{angle} * D2R; | ||
| 90 | $self->{elevationr} ||= $self->{elevation} * D2R; | ||
| 91 | |||
| 92 | # Set up the direction vector for the first time. | ||
| 93 | # Update the direction vector we use for variety of tasks. | ||
| 94 | # For angle = 0, elevation = 0 this should be 0, 0, -1 | ||
| 95 | my $angler = $self->{angler}; | ||
| 96 | my $elevationr = $self->{elevationr}; | ||
| 97 | my $direction = $self->{direction} = [ | ||
| 98 | sin($angler) * cos($elevationr), | ||
| 99 | sin($elevationr), | ||
| 100 | -cos($angler) * cos($elevationr), | ||
| 101 | ]; | ||
| 102 | |||
| 103 | # Calculate the frustrum properties. | ||
| 104 | # In the original C implementation hfar is calculated with tan() but | ||
| 105 | # since Perl doesn't have a native one we use sin/cos. :( | ||
| 106 | $self->{dlength} = $self->{zfar} - $self->{znear}; | ||
| 107 | $self->{dsphere} = $self->{znear} + ($self->{dlength} * 0.5); | ||
| 108 | $self->{hfar} = $self->{dlength} | ||
| 109 | * sin($self->{fovyr} * 0.5) | ||
| 110 | / cos($self->{fovyr} * 0.5); | ||
| 111 | $self->{wfar} = $self->{hfar} * $self->{aspect}; | ||
| 112 | |||
| 113 | # Find the vector from the near/far halfway point (P) | ||
| 114 | # and the far corner of the frustrum (Q). | ||
| 115 | my @P = ( 0, 0, $self->{dsphere} ); | ||
| 116 | my @Q = ( $self->{wfar}, $self->{hfar}, $self->{dlength} ); | ||
| 117 | my @D = ( $P[0] - $Q[0], $P[1] - $Q[1], $P[2] - $Q[2] ); | ||
| 118 | |||
| 119 | # The frustrum sphere radius is the length of the vector, | ||
| 120 | # and the centre is the camera position plus the length of the centre | ||
| 121 | # in the direction the camera is looking. | ||
| 122 | $self->{rsphere} = sqrt( $D[0] ** 2 + $D[1] ** 2 + $D[2] ** 2 ); | ||
| 123 | $self->{xsphere} = $self->{dsphere} * $direction->[0]; | ||
| 124 | $self->{ysphere} = $self->{dsphere} * $direction->[1]; | ||
| 125 | $self->{zsphere} = $self->{dsphere} * $direction->[2]; | ||
| 126 | |||
| 127 | # Calculate the mouse origin position | ||
| 128 | $self->{mouse} = [ | ||
| 129 | int( $self->{width} / 2 ), | ||
| 130 | int( $self->{height} / 2 ), | ||
| 131 | ]; | ||
| 132 | |||
| 133 | # Key tracking | ||
| 134 | $self->{down} = {}; | ||
| 135 | |||
| 136 | return $self; | ||
| 137 | } | ||
| 138 | |||
| 139 | =pod | ||
| 140 | |||
| 141 | =head2 X | ||
| 142 | |||
| 143 | The C<X> accessor provides the location of the camera in metres on the east | ||
| 144 | to west dimension within the 3D world. The positive direction is east. | ||
| 145 | |||
| 146 | =cut | ||
| 147 | |||
| 148 | sub X { | ||
| 149 | $_[0]->{X}; | ||
| 150 | } | ||
| 151 | |||
| 152 | =pod | ||
| 153 | |||
| 154 | =head2 Y | ||
| 155 | |||
| 156 | The C<Y> accessor is location of the camera in metres on the vertical | ||
| 157 | dimension within the 3D world. The positive direction is up. | ||
| 158 | |||
| 159 | =cut | ||
| 160 | |||
| 161 | sub Y { | ||
| 162 | $_[0]->{Y}; | ||
| 163 | } | ||
| 164 | |||
| 165 | =pod | ||
| 166 | |||
| 167 | =head2 Z | ||
| 168 | |||
| 169 | The C<Z> accessor provides the location of the camera in metres on the north | ||
| 170 | to south dimension within the 3D world. The positive direction is north. | ||
| 171 | |||
| 172 | =cut | ||
| 173 | |||
| 174 | sub Z { | ||
| 175 | $_[0]->{Z}; | ||
| 176 | } | ||
| 177 | |||
| 178 | =pod | ||
| 179 | |||
| 180 | =head2 angle | ||
| 181 | |||
| 182 | The C<angle> accessor provides the direction the camera is facing on the | ||
| 183 | horizontal plane within the 3D world. Positive indicates clockwise degrees | ||
| 184 | from north. Thus C<0> is north, C<90> is east, C<180> is south and C<270> | ||
| 185 | is west. | ||
| 186 | |||
| 187 | The C<angle> is more correctly known as the "azimuth" but we prefer the | ||
| 188 | simpler common term for a gaming API. For more details see | ||
| 189 | L<http://en.wikipedia.org/wiki/Azimuth>. | ||
| 190 | |||
| 191 | =cut | ||
| 192 | |||
| 193 | sub angle { | ||
| 194 | $_[0]->{angle}; | ||
| 195 | } | ||
| 196 | |||
| 197 | =pod | ||
| 198 | |||
| 199 | =head2 elevation | ||
| 200 | |||
| 201 | The C<elevation> accessor provides the direction the camera is facing on | ||
| 202 | the vertical plane. Positive indicates degrees above the horizon. Thus | ||
| 203 | C<0> is looking at the horizon, C<90> is facing straight up, and | ||
| 204 | C<-90> is facing straight down. | ||
| 205 | |||
| 206 | The C<elevation> is more correctly known as the "altitude" but we prefer the | ||
| 207 | simpler common term for a gaming API. For more details see | ||
| 208 | see L<http://en.wikipedia.org/w/index.php?title=Altitude_(astronomy)>. | ||
| 209 | |||
| 210 | =cut | ||
| 211 | |||
| 212 | sub elevation { | ||
| 213 | $_[0]->{elevation}; | ||
| 214 | } | ||
| 215 | |||
| 216 | =pod | ||
| 217 | |||
| 218 | =head2 direction | ||
| 219 | |||
| 220 | The C<direction> accessor provides a unitised geometric vector for where | ||
| 221 | the camera is currently pointing. This vector does not have a rotational | ||
| 222 | component so for any math requiring rotation you will need to naively | ||
| 223 | assume that the camera rotational orientation is zero (i.e. when looking | ||
| 224 | at the horizon camera up is geometric up along the positive Y axis) | ||
| 225 | |||
| 226 | =cut | ||
| 227 | |||
| 228 | sub direction { | ||
| 229 | $_[0]->{direction}; | ||
| 230 | } | ||
| 231 | |||
| 232 | |||
| 233 | |||
| 234 | |||
| 235 | |||
| 236 | ###################################################################### | ||
| 237 | # Engine Interface | ||
| 238 | |||
| 239 | # Note that this doesn't position the camera, just sets it up | ||
| 240 | sub init { | ||
| 241 | my $self = shift; | ||
| 242 | |||
| 243 | # As a mouselook game, we don't want users to see the cursor. | ||
| 244 | # We also position the cursor at the exact centre of the window. | ||
| 245 | # (This will result in another spurious mouse event) | ||
| 246 | SDL::Mouse::show_cursor( SDL::Constants::SDL_DISABLE ); | ||
| 247 | SDL::Mouse::warp_mouse( @{$self->{mouse}} ); | ||
| 248 | |||
| 249 | # Select and reset the projection, flushing any old state | ||
| 250 | OpenGL::glMatrixMode( OpenGL::GL_PROJECTION ); | ||
| 251 | OpenGL::glLoadIdentity(); | ||
| 252 | |||
| 253 | # Set the perspective we will look through. | ||
| 254 | # We'll use a standard 60 degree perspective, removing any | ||
| 255 | # shapes closer than one metre or further than one kilometre. | ||
| 256 | OpenGL::gluPerspective( | ||
| 257 | $self->{fovy}, | ||
| 258 | $self->{aspect}, | ||
| 259 | $self->{znear}, | ||
| 260 | $self->{zfar}, | ||
| 261 | ); | ||
| 262 | |||
| 263 | # Work super hard to make perspective calculations not suck | ||
| 264 | OpenGL::glHint( | ||
| 265 | OpenGL::GL_PERSPECTIVE_CORRECTION_HINT, | ||
| 266 | OpenGL::GL_NICEST, | ||
| 267 | ); | ||
| 268 | |||
| 269 | return; | ||
| 270 | } | ||
| 271 | |||
| 272 | sub display { | ||
| 273 | my $self = shift; | ||
| 274 | |||
| 275 | # Transform the location of the entire freaking world in the opposite | ||
| 276 | # direction and angle to where the camera is and which way it is | ||
| 277 | # pointing. This makes it LOOK as if the camera is moving but it isn't. | ||
| 278 | OpenGL::glRotatef( $self->{elevation}, -1, 0, 0 ); | ||
| 279 | OpenGL::glRotatef( $self->{angle}, 0, 1, 0 ); | ||
| 280 | OpenGL::glTranslatef( -$self->{X}, -$self->{Y}, -$self->{Z} ); | ||
| 281 | |||
| 282 | return; | ||
| 283 | } | ||
| 284 | |||
| 285 | sub move { | ||
| 286 | my $self = shift; | ||
| 287 | my $step = shift; | ||
| 288 | my $down = $self->{down}; | ||
| 289 | my $speed = $self->{speed} * $step; | ||
| 290 | |||
| 291 | # Find the camera-wards and sideways components of our velocity | ||
| 292 | my $move = $speed * ( | ||
| 293 | $down->{SDL::Constants::SDLK_s} - | ||
| 294 | $down->{SDL::Constants::SDLK_w} | ||
| 295 | ); | ||
| 296 | my $strafe = $speed * ( | ||
| 297 | $down->{SDL::Constants::SDLK_d} - | ||
| 298 | $down->{SDL::Constants::SDLK_a} | ||
| 299 | ); | ||
| 300 | |||
| 301 | # Apply this movement in the direction of the camera | ||
| 302 | my $angler = $self->{angler}; | ||
| 303 | $self->{X} += (cos($angler) * $strafe) - (sin($angler) * $move); | ||
| 304 | $self->{Z} += (sin($angler) * $strafe) + (cos($angler) * $move); | ||
| 305 | |||
| 306 | return; | ||
| 307 | } | ||
| 308 | |||
| 309 | sub event { | ||
| 310 | my $self = shift; | ||
| 311 | my $event = shift; | ||
| 312 | my $type = $event->type; | ||
| 313 | |||
| 314 | if ( $type == SDL::Constants::SDL_MOUSEMOTION ) { | ||
| 315 | # Ignore mouse motion events at the mouse origin | ||
| 316 | $event->motion_x == $self->{mouse}->[0] and | ||
| 317 | $event->motion_y == $self->{mouse}->[1] and | ||
| 318 | return 1; | ||
| 319 | |||
| 320 | # Convert the mouse motion to mouselook behaviour | ||
| 321 | my $x = $event->motion_xrel; | ||
| 322 | my $y = $event->motion_yrel; | ||
| 323 | $x = $x - 65536 if $x > 32000; | ||
| 324 | $y = $y - 65536 if $y > 32000; | ||
| 325 | my $angle = $self->{angle} = ($self->{angle} + $x / 5) % 360; | ||
| 326 | $self->{elevation} -= $y / 10; | ||
| 327 | $self->{elevation} = 90 if $self->{elevation} > 90; | ||
| 328 | $self->{elevation} = -90 if $self->{elevation} < -90; | ||
| 329 | |||
| 330 | # Update the direction vector we use for variety of tasks. | ||
| 331 | # For angle = 0, elevation = 0 this should be 0, 0, -1 | ||
| 332 | my $angler = $self->{angler} = D2R * $angle; | ||
| 333 | my $elevationr = $self->{elevationr} = D2R * $self->{elevation}; | ||
| 334 | my @direction = ( | ||
| 335 | sin($angler) * cos($elevationr), | ||
| 336 | sin($elevationr), | ||
| 337 | -cos($angler) * cos($elevationr), | ||
| 338 | ); | ||
| 339 | $self->{direction} = \@direction; | ||
| 340 | |||
| 341 | # Update the frustrum sphere centre | ||
| 342 | $self->{xsphere} = $self->{X} + ($self->{dsphere} * $direction[0]); | ||
| 343 | $self->{ysphere} = $self->{Y} + ($self->{dsphere} * $direction[1]); | ||
| 344 | $self->{zsphere} = $self->{Z} + ($self->{dsphere} * $direction[2]); | ||
| 345 | |||
| 346 | # Move the mouse back to the centre of the window so that | ||
| 347 | # it can never escape the game window. | ||
| 348 | SDL::Mouse::warp_mouse( @{$self->{mouse}} ); | ||
| 349 | |||
| 350 | return 1; | ||
| 351 | } | ||
| 352 | |||
| 353 | if ( $type == SDL::Constants::SDL_KEYDOWN ) { | ||
| 354 | my $key = $event->key_sym; | ||
| 355 | if ( exists $self->{down}->{$key} ) { | ||
| 356 | $self->{down}->{$key} = 1; | ||
| 357 | return 1; | ||
| 358 | } | ||
| 359 | } | ||
| 360 | |||
| 361 | if ( $type == SDL::Constants::SDL_KEYUP ) { | ||
| 362 | my $key = $event->key_sym; | ||
| 363 | if ( exists $self->{down}->{$key} ) { | ||
| 364 | $self->{down}->{$key} = 0; | ||
| 365 | return 1; | ||
| 366 | } | ||
| 367 | } | ||
| 368 | |||
| 369 | # We don't care about this event | ||
| 370 | return 0; | ||
| 371 | } | ||
| 372 | |||
| 373 | |||
| 374 | |||
| 375 | |||
| 376 | |||
| 377 | ###################################################################### | ||
| 378 | # Support Methods | ||
| 379 | |||
| 380 | # Sort a series of vectors by distance from the camera, returning | ||
| 381 | # the result as a list of index positions. | ||
| 382 | sub distance_isort { | ||
| 383 | my $self = shift; | ||
| 384 | my $X = $self->{X}; | ||
| 385 | my $Y = $self->{Y}; | ||
| 386 | my $Z = $self->{Z}; | ||
| 387 | |||
| 388 | # Calculate the distances | ||
| 389 | my @distance = map { | ||
| 390 | ($X - $_->[0]) ** 2 + ($Y - $_->[1]) ** 2 + ($Z - $_->[2]) ** 2 | ||
| 391 | } @_; | ||
| 392 | |||
| 393 | # Sort index by distance | ||
| 394 | my @order = sort { | ||
| 395 | $distance[$b] <=> $distance[$a] | ||
| 396 | } ( 0 .. $#_ ); | ||
| 397 | |||
| 398 | return @order; | ||
| 399 | } | ||
| 400 | |||
| 401 | 1; | ||
| 402 | |||
| 403 | =pod | ||
| 404 | |||
| 405 | =head1 SUPPORT | ||
| 406 | |||
| 407 | Bugs should be reported via the CPAN bug tracker at | ||
| 408 | |||
| 409 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=SDL-Tutorial-3DWorld> | ||
| 410 | |||
| 411 | =head1 AUTHOR | ||
| 412 | |||
| 413 | Adam Kennedy E<lt>adamk@cpan.orgE<gt> | ||
| 414 | |||
| 415 | =head1 SEE ALSO | ||
| 416 | |||
| 417 | L<SDL>, L<OpenGL> | ||
| 418 | |||
| 419 | =head1 COPYRIGHT | ||
| 420 | |||
| 421 | Copyright 2010 Adam Kennedy. | ||
| 422 | |||
| 423 | This program is free software; you can redistribute | ||
| 424 | it and/or modify it under the same terms as Perl itself. | ||
| 425 | |||
| 426 | The full text of the license can be found in the | ||
| 427 | LICENSE file included with this module. | ||
| 428 | |||
| 429 | =cut |