| 1 | package SDL::Tutorial::3DWorld; | ||
| 2 | |||
| 3 | =pod | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | SDL::Tutorial::3DWorld - Create a 3D world from scratch with SDL and OpenGL | ||
| 8 | |||
| 9 | =head1 DESCRIPTION | ||
| 10 | |||
| 11 | This tutorial is intended to demonstrate the creation of a trivial but | ||
| 12 | relatively usable "3D Game Engine". | ||
| 13 | |||
| 14 | The demonstration code provided implements the four main elements of a | ||
| 15 | basic three-dimensional game world. | ||
| 16 | |||
| 17 | =over | ||
| 18 | |||
| 19 | =item * | ||
| 20 | |||
| 21 | A static landscape in which events will occur. | ||
| 22 | |||
| 23 | =item * | ||
| 24 | |||
| 25 | A light source to illuminate the world. | ||
| 26 | |||
| 27 | =item * | ||
| 28 | |||
| 29 | A collection of N objects which move around independantly inside the | ||
| 30 | world. | ||
| 31 | |||
| 32 | =item * | ||
| 33 | |||
| 34 | A user-controlled mobile camera through which the world is viewed | ||
| 35 | |||
| 36 | =back | ||
| 37 | |||
| 38 | Each element of the game world is encapsulated inside a standalone class. | ||
| 39 | |||
| 40 | This lets you see which parts of the Open GL operations are used to work | ||
| 41 | with each element of the game world, and provides a starting point from | ||
| 42 | which you can start to make your own simple game-specific engines. | ||
| 43 | |||
| 44 | =head1 METHODS | ||
| 45 | |||
| 46 | =cut | ||
| 47 | |||
| 48 | 2 | ▶ | use 5.008005; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 49 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 50 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 51 | 2 | ▶ | use IO::File 1.14 (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 52 | 2 | ▶ | use File::Spec 3.31 (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 53 | 2 | ▶ | use File::ShareDir 1.02 (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 54 | 2 | ▶ | use List::MoreUtils 0.22 (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 55 | 2 | ▶ | use Params::Util 1.00 (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 56 | 0 | ▶ | use OpenGL 0.64 ':all'; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 57 | use OpenGL::List 0.01 (); | ||
| 58 | use SDL 2.527 ':all'; | ||
| 59 | use SDL::Event ':all'; | ||
| 60 | use SDLx::App (); | ||
| 61 | use SDL::Tutorial::3DWorld::Actor (); | ||
| 62 | use SDL::Tutorial::3DWorld::Actor::Billboard (); | ||
| 63 | use SDL::Tutorial::3DWorld::Actor::Debug (); | ||
| 64 | use SDL::Tutorial::3DWorld::Actor::GridCube (); | ||
| 65 | use SDL::Tutorial::3DWorld::Actor::GridPlane (); | ||
| 66 | use SDL::Tutorial::3DWorld::Actor::GridSelect (); | ||
| 67 | use SDL::Tutorial::3DWorld::Actor::Hedron (); | ||
| 68 | use SDL::Tutorial::3DWorld::Actor::MaterialSampler (); | ||
| 69 | use SDL::Tutorial::3DWorld::Actor::Model (); | ||
| 70 | use SDL::Tutorial::3DWorld::Actor::Sprite (); | ||
| 71 | use SDL::Tutorial::3DWorld::Actor::SpriteOct (); | ||
| 72 | use SDL::Tutorial::3DWorld::Actor::Teapot (); | ||
| 73 | use SDL::Tutorial::3DWorld::Actor::TextureCube (); | ||
| 74 | use SDL::Tutorial::3DWorld::Actor::TronBit (); | ||
| 75 | # use SDL::Tutorial::3DWorld::Actor::TV (); | ||
| 76 | use SDL::Tutorial::3DWorld::Asset (); | ||
| 77 | use SDL::Tutorial::3DWorld::Camera (); | ||
| 78 | use SDL::Tutorial::3DWorld::Camera::God (); | ||
| 79 | use SDL::Tutorial::3DWorld::Console (); | ||
| 80 | use SDL::Tutorial::3DWorld::Fog (); | ||
| 81 | use SDL::Tutorial::3DWorld::Landscape (); | ||
| 82 | use SDL::Tutorial::3DWorld::Landscape::Infinite (); | ||
| 83 | use SDL::Tutorial::3DWorld::Light (); | ||
| 84 | use SDL::Tutorial::3DWorld::Material (); | ||
| 85 | use SDL::Tutorial::3DWorld::Model (); | ||
| 86 | use SDL::Tutorial::3DWorld::OpenGL (); | ||
| 87 | use SDL::Tutorial::3DWorld::Skybox (); | ||
| 88 | use SDL::Tutorial::3DWorld::Texture (); | ||
| 89 | use SDL::Tutorial::3DWorld::Bound; | ||
| 90 | |||
| 91 | # Enable GLUT support so we can have teapots and other things | ||
| 92 | BEGIN { | ||
| 93 | OpenGL::glutInit(); | ||
| 94 | } | ||
| 95 | |||
| 96 | our $VERSION = '0.33'; | ||
| 97 | |||
| 98 | # The currently active world | ||
| 99 | our $CURRENT = undef; | ||
| 100 | |||
| 101 | =pod | ||
| 102 | |||
| 103 | =head2 new | ||
| 104 | |||
| 105 | The C<new> constructor sets up the model for the 3D World, but does not | ||
| 106 | initiate or start the game itself. | ||
| 107 | |||
| 108 | It does not current take any parameters. | ||
| 109 | |||
| 110 | =cut | ||
| 111 | |||
| 112 | sub new { | ||
| 113 | my $class = shift; | ||
| 114 | |||
| 115 | # Are we doing a benchmarking run? | ||
| 116 | # If so set the flag and we will abort after 100 seconds. | ||
| 117 | my $benchmark = scalar grep { $_ eq '--benchmark' } @_; | ||
| 118 | |||
| 119 | # Create the basic object | ||
| 120 | my $self = bless { | ||
| 121 | ARGV => [ @_ ], | ||
| 122 | width => 1280, | ||
| 123 | height => 1024, | ||
| 124 | dt => 0.1, | ||
| 125 | |||
| 126 | # Debugging or expensive elements we can toggle off. | ||
| 127 | # Turning all of these three off gives us a much more | ||
| 128 | # accurate assessment on how fast a real world would perform. | ||
| 129 | benchmark => $benchmark, | ||
| 130 | hide_debug => $benchmark ? 1 : 0, | ||
| 131 | hide_console => $benchmark ? 1 : 0, | ||
| 132 | hide_expensive => $benchmark ? 1 : 0, | ||
| 133 | }, $class; | ||
| 134 | |||
| 135 | # Normally we want fullscreen, but occasionally we might want to | ||
| 136 | # disable it because we are on a portrait-orientation monitor | ||
| 137 | # or for unobtrusive testing (or it doesn't work on some machine). | ||
| 138 | # When showing in a window, drop the size to the window isn't huge. | ||
| 139 | $self->{fullscreen} = not grep { $_ eq '--window' } @_; | ||
| 140 | unless ( $self->{fullscreen} ) { | ||
| 141 | $self->{width} /= 2; | ||
| 142 | $self->{height} /= 2; | ||
| 143 | } | ||
| 144 | |||
| 145 | # Text console that overlays the world | ||
| 146 | $self->{console} = SDL::Tutorial::3DWorld::Console->new; | ||
| 147 | |||
| 148 | # A pretty skybox background for our world | ||
| 149 | $self->{skybox} = SDL::Tutorial::3DWorld::Skybox->new( | ||
| 150 | type => 'jpg', | ||
| 151 | directory => $self->sharedir('skybox'), | ||
| 152 | ); | ||
| 153 | |||
| 154 | # Light the world with a single overhead light | ||
| 155 | # that matches the position of the sun. | ||
| 156 | $self->{lights} = [ | ||
| 157 | SDL::Tutorial::3DWorld::Light->new( | ||
| 158 | position => [ 360, 405, -400 ], | ||
| 159 | ), | ||
| 160 | ]; | ||
| 161 | |||
| 162 | # Create the (optional) fog. | ||
| 163 | # Because it doesn't really blend with the current skybox, | ||
| 164 | # I've tweaked it to try to look like a light ground haze. | ||
| 165 | # $self->{fog} = SDL::Tutorial::3DWorld::Fog->new( | ||
| 166 | # color => [ 0.5, 0.5, 0.5, 0 ], | ||
| 167 | # start => 10.0, | ||
| 168 | # end => 50.0, | ||
| 169 | # ); | ||
| 170 | |||
| 171 | # Create the landscape | ||
| 172 | $self->{landscape} = SDL::Tutorial::3DWorld::Landscape::Infinite->new( | ||
| 173 | texture => $self->sharefile('ground.jpg'), | ||
| 174 | ); | ||
| 175 | |||
| 176 | # Place the camera at a typical eye height a few metres back | ||
| 177 | # from the teapots and facing slightly down towards them. | ||
| 178 | $self->{camera} = SDL::Tutorial::3DWorld::Camera::God->new( | ||
| 179 | # Camera position properties | ||
| 180 | X => 0.0, | ||
| 181 | Y => 1.5, | ||
| 182 | Z => 5.0, | ||
| 183 | speed => $self->dscalar(2), | ||
| 184 | |||
| 185 | # Camera view properties | ||
| 186 | height => $self->{height}, | ||
| 187 | width => $self->{width}, | ||
| 188 | fovy => 45, | ||
| 189 | |||
| 190 | # Actor list and indexes for faster culling | ||
| 191 | actors => [ ], | ||
| 192 | show => [ ], | ||
| 193 | move => [ ], | ||
| 194 | ); | ||
| 195 | |||
| 196 | # The selector is an actor and a special camera tool for | ||
| 197 | #(potentially) controlling something in the world. | ||
| 198 | $self->{selector} = $self->actor( | ||
| 199 | SDL::Tutorial::3DWorld::Actor::GridSelect->new, | ||
| 200 | ); | ||
| 201 | |||
| 202 | # Add a video screen | ||
| 203 | # $self->actor( | ||
| 204 | # SDL::Tutorial::3DWorld::Actor::TV->new( | ||
| 205 | # position => [ 0, 1, -5 ], | ||
| 206 | # file => $self->sharefile('test-mpeg.mpg'), | ||
| 207 | # ), | ||
| 208 | # ); | ||
| 209 | |||
| 210 | # Create the wolfenstein-inspired level | ||
| 211 | $self->actor( | ||
| 212 | SDL::Tutorial::3DWorld::Actor::GridPlane->new( | ||
| 213 | # The GridPlane automatically culls faces that will | ||
| 214 | # never be exposed to reduce costs. Turn on this | ||
| 215 | # debug flag to always render all faces. | ||
| 216 | # debug => 1, | ||
| 217 | position => [ 10, 0, 10 ], | ||
| 218 | scale => 3, | ||
| 219 | size => 9, | ||
| 220 | floor => [ 0.5, 0.5, 0.5 ], | ||
| 221 | ceiling => [ 0.2, 0.2, 0.2 ], | ||
| 222 | wall => [ | ||
| 223 | $self->sharefile('wall1.png'), | ||
| 224 | $self->sharefile('wall2.png'), | ||
| 225 | $self->sharefile('wall3.png'), | ||
| 226 | $self->sharefile('wall4.png'), | ||
| 227 | ], | ||
| 228 | map => <<'END_MAP' | ||
| 229 | 112121211 | ||
| 230 | 100000003 | ||
| 231 | 100000303 | ||
| 232 | 100000303 | ||
| 233 | 000000303 | ||
| 234 | 100330303 | ||
| 235 | 100330303 | ||
| 236 | 100000303 | ||
| 237 | 112121333 | ||
| 238 | END_MAP | ||
| 239 | ), | ||
| 240 | ); | ||
| 241 | |||
| 242 | # A full and complex character | ||
| 243 | $self->{bit} = $self->actor( | ||
| 244 | SDL::Tutorial::3DWorld::Actor::TronBit->new( | ||
| 245 | position => [ 1.0, 1.9, 7.0 ], | ||
| 246 | ), | ||
| 247 | ); | ||
| 248 | |||
| 249 | # Add three teapots to the scene. | ||
| 250 | # (R)ed is the official colour of the X axis. | ||
| 251 | $self->actor( | ||
| 252 | SDL::Tutorial::3DWorld::Actor::Teapot->new( | ||
| 253 | hidden => $self->{hide_expensive}, | ||
| 254 | size => 0.20, | ||
| 255 | position => [ 0.0, 0.5, 0.0 ], | ||
| 256 | velocity => $self->dvector( 0.1, 0.0, 0.0 ), | ||
| 257 | material => { | ||
| 258 | ambient => [ 0.5, 0.2, 0.2, 1.0 ], | ||
| 259 | diffuse => [ 1.0, 0.7, 0.7, 1.0 ], | ||
| 260 | specular => [ 1.0, 1.0, 1.0, 1.0 ], | ||
| 261 | shininess => 80, | ||
| 262 | }, | ||
| 263 | ), | ||
| 264 | ); | ||
| 265 | |||
| 266 | # (B)lue is the official colour of the Z axis. | ||
| 267 | $self->actor( | ||
| 268 | SDL::Tutorial::3DWorld::Actor::Teapot->new( | ||
| 269 | hidden => $self->{hide_expensive}, | ||
| 270 | size => 0.30, | ||
| 271 | position => [ 0.0, 1.0, 0.0 ], | ||
| 272 | velocity => $self->dvector( 0.0, 0.0, 0.1 ), | ||
| 273 | material => { | ||
| 274 | ambient => [ 0.2, 0.2, 0.5, 1.0 ], | ||
| 275 | diffuse => [ 0.7, 0.7, 1.0, 1.0 ], | ||
| 276 | specular => [ 1.0, 1.0, 1.0, 1.0 ], | ||
| 277 | shininess => 100, | ||
| 278 | }, | ||
| 279 | ), | ||
| 280 | ); | ||
| 281 | |||
| 282 | # (G)reen is the official colour of the Y axis | ||
| 283 | $self->actor( | ||
| 284 | SDL::Tutorial::3DWorld::Actor::Teapot->new( | ||
| 285 | hidden => $self->{hide_expensive}, | ||
| 286 | size => 0.50, | ||
| 287 | position => [ 0.0, 1.5, 0.0 ], | ||
| 288 | velocity => $self->dvector( 0.0, 0.1, 0.0 ), | ||
| 289 | material => { | ||
| 290 | ambient => [ 0.2, 0.5, 0.2, 1.0 ], | ||
| 291 | diffuse => [ 0.7, 1.0, 0.7, 1.0 ], | ||
| 292 | specular => [ 1.0, 1.0, 1.0, 1.0 ], | ||
| 293 | shininess => 120, | ||
| 294 | }, | ||
| 295 | ), | ||
| 296 | ); | ||
| 297 | |||
| 298 | # Place a static grid cube in the air on the positive | ||
| 299 | # and negative corners of the landscape, proving the | ||
| 300 | # grid-bounding math works (which it might not on the | ||
| 301 | # negative side of an axis if you mistakenly use int() | ||
| 302 | # for the math instead of something like POSIX::ceil/floor). | ||
| 303 | $self->actor( | ||
| 304 | SDL::Tutorial::3DWorld::Actor::GridCube->new( | ||
| 305 | position => [ -3.7, 1.3, -3.7 ], | ||
| 306 | ), | ||
| 307 | ); | ||
| 308 | |||
| 309 | # Set up a flying grid cube heading away from the teapots. | ||
| 310 | # This should demonstrate the "grid" nature of the cube, | ||
| 311 | # and the flying path will take us along a path that will | ||
| 312 | # share an edge with the static box, which should look neat. | ||
| 313 | $self->actor( | ||
| 314 | SDL::Tutorial::3DWorld::Actor::GridCube->new( | ||
| 315 | position => [ -0.33, 0.01, -0.66 ], | ||
| 316 | velocity => $self->dvector( -0.1, 0.1, -0.1 ), | ||
| 317 | ), | ||
| 318 | ); | ||
| 319 | |||
| 320 | # Place a typical large crate on the opposite side of the | ||
| 321 | # chessboard from the static gridcube. | ||
| 322 | $self->actor( | ||
| 323 | SDL::Tutorial::3DWorld::Actor::TextureCube->new( | ||
| 324 | size => 1.3, | ||
| 325 | position => [ 3.3, 0.0, 3.35 ], | ||
| 326 | material => { | ||
| 327 | ambient => [ 0.5, 0.5, 0.5, 1 ], | ||
| 328 | texture => $self->sharefile('crate1.jpg'), | ||
| 329 | }, | ||
| 330 | ), | ||
| 331 | ); | ||
| 332 | |||
| 333 | # Place a high-detail table (to test large models and scaling) | ||
| 334 | $self->actor( | ||
| 335 | SDL::Tutorial::3DWorld::Actor::Model->new( | ||
| 336 | scale => 0.05, | ||
| 337 | position => [ -10, 0, 0 ], | ||
| 338 | velocity => [ 0, 0, 0 ], | ||
| 339 | file => File::Spec->catfile('model', 'table', 'table.obj'), | ||
| 340 | plain => 1, | ||
| 341 | ), | ||
| 342 | ); | ||
| 343 | |||
| 344 | # Place a lollipop near the origin to test transparency in models | ||
| 345 | $self->actor( | ||
| 346 | SDL::Tutorial::3DWorld::Actor::Model->new( | ||
| 347 | position => [ -2, 0, 0 ], | ||
| 348 | file => File::Spec->catfile('model', 'lollipop', 'hflollipop1gr.rwx'), | ||
| 349 | ), | ||
| 350 | ); | ||
| 351 | |||
| 352 | # Place two nutcrackers a little further away | ||
| 353 | $self->actor( | ||
| 354 | SDL::Tutorial::3DWorld::Actor::Model->new( | ||
| 355 | position => [ -2, 0, -2 ], | ||
| 356 | file => File::Spec->catfile('model', 'nutcracker', "sv-nutcracker1.rwx"), | ||
| 357 | ), | ||
| 358 | ); | ||
| 359 | $self->actor( | ||
| 360 | SDL::Tutorial::3DWorld::Actor::Model->new( | ||
| 361 | position => [ -4, 0, -2 ], | ||
| 362 | file => File::Spec->catfile('model', 'nutcracker', "sv-nutcracker7.rwx"), | ||
| 363 | ), | ||
| 364 | ); | ||
| 365 | |||
| 366 | # Add a material sampler | ||
| 367 | $self->actor( | ||
| 368 | SDL::Tutorial::3DWorld::Actor::MaterialSampler->new( | ||
| 369 | hidden => $self->{hide_expensive}, | ||
| 370 | position => [ 5, 1, 5 ], | ||
| 371 | file => File::Spec->catfile( | ||
| 372 | File::ShareDir::dist_dir('SDL-Tutorial-3DWorld'), | ||
| 373 | 'example.mtl', | ||
| 374 | ), | ||
| 375 | ), | ||
| 376 | ); | ||
| 377 | |||
| 378 | # Add a sprite | ||
| 379 | $self->actor( | ||
| 380 | SDL::Tutorial::3DWorld::Actor::Sprite->new( | ||
| 381 | scale => 2, | ||
| 382 | position => [ 3, 0, -1 ], | ||
| 383 | texture => $self->sharefile('sprite', 'pguard_die4.png'), | ||
| 384 | ), | ||
| 385 | ); | ||
| 386 | |||
| 387 | # Add an eight-sided sprite | ||
| 388 | $self->actor( | ||
| 389 | SDL::Tutorial::3DWorld::Actor::SpriteOct->new( | ||
| 390 | scale => 2, | ||
| 391 | position => [ 5, 0, -1 ], | ||
| 392 | texture => [ | ||
| 393 | $self->sharefile('sprite', 'mguard_s_1.png'), | ||
| 394 | $self->sharefile('sprite', 'mguard_s_2.png'), | ||
| 395 | $self->sharefile('sprite', 'mguard_s_3.png'), | ||
| 396 | $self->sharefile('sprite', 'mguard_s_4.png'), | ||
| 397 | $self->sharefile('sprite', 'mguard_s_5.png'), | ||
| 398 | $self->sharefile('sprite', 'mguard_s_6.png'), | ||
| 399 | $self->sharefile('sprite', 'mguard_s_7.png'), | ||
| 400 | $self->sharefile('sprite', 'mguard_s_8.png'), | ||
| 401 | ], | ||
| 402 | ), | ||
| 403 | ); | ||
| 404 | |||
| 405 | # Add a billboard | ||
| 406 | $self->actor( | ||
| 407 | SDL::Tutorial::3DWorld::Actor::Billboard->new( | ||
| 408 | scale => 2, | ||
| 409 | position => [ 3, 3, 10 ], | ||
| 410 | texture => $self->sharefile('sprite', 'billboard.png'), | ||
| 411 | ), | ||
| 412 | ); | ||
| 413 | |||
| 414 | # Add a grid of 100 toilet plungers | ||
| 415 | foreach my $x ( -14 .. -5 ) { | ||
| 416 | foreach my $z ( 5 .. 14 ) { | ||
| 417 | $self->actor( | ||
| 418 | SDL::Tutorial::3DWorld::Actor::Model->new( | ||
| 419 | position => [ $x, 1.6, $z ], | ||
| 420 | file => File::Spec->catfile( | ||
| 421 | 'model', | ||
| 422 | 'toilet-plunger001', | ||
| 423 | 'toilet_plunger001.obj', | ||
| 424 | ), | ||
| 425 | ), | ||
| 426 | ); | ||
| 427 | } | ||
| 428 | } | ||
| 429 | |||
| 430 | # Add a grid of 400 texture crates | ||
| 431 | foreach my $x ( -24 .. -5 ) { | ||
| 432 | foreach my $z ( 5 .. 24 ) { | ||
| 433 | $self->actor( | ||
| 434 | SDL::Tutorial::3DWorld::Actor::TextureCube->new( | ||
| 435 | size => 1, | ||
| 436 | position => [ $x, 0, $z ], | ||
| 437 | texture => $self->sharefile('crate1.jpg'), | ||
| 438 | ), | ||
| 439 | ); | ||
| 440 | } | ||
| 441 | } | ||
| 442 | |||
| 443 | # An recursive icosaheron of toilet plungers in the sky | ||
| 444 | $self->actor( | ||
| 445 | SDL::Tutorial::3DWorld::Actor::Hedron->icosahedron( | ||
| 446 | position => [ -7, 5, 7 ], | ||
| 447 | velocity => [ 0, 0, 0 ], | ||
| 448 | orient => [ 0, 0, 1, 0 ], | ||
| 449 | rotate => 1, | ||
| 450 | actor => SDL::Tutorial::3DWorld::Actor::Hedron->icosahedron( | ||
| 451 | position => [ 0, 0.95, 0 ], | ||
| 452 | actor => SDL::Tutorial::3DWorld::Actor::Model->new( | ||
| 453 | position => [ 0, 0.19, 0 ], | ||
| 454 | file => File::Spec->catfile( | ||
| 455 | 'model', | ||
| 456 | 'toilet-plunger001', | ||
| 457 | 'toilet_plunger001.obj', | ||
| 458 | ), | ||
| 459 | ), | ||
| 460 | ), | ||
| 461 | ), | ||
| 462 | ); | ||
| 463 | |||
| 464 | # Add three tron light cycles of varying complexity | ||
| 465 | $self->actor( | ||
| 466 | SDL::Tutorial::3DWorld::Actor::Model->new( | ||
| 467 | position => [ 15, 1, -5 ], | ||
| 468 | velocity => [ 0, 0, 0 ], | ||
| 469 | orient => [ 270, 1, 0, 0 ], | ||
| 470 | scale => 0.5, | ||
| 471 | file => File::Spec->catfile( | ||
| 472 | 'model', | ||
| 473 | 'gltron', | ||
| 474 | 'lightcycle-high.obj', | ||
| 475 | ), | ||
| 476 | ), | ||
| 477 | ); | ||
| 478 | |||
| 479 | # Add three tron light cycles of varying complexity | ||
| 480 | $self->actor( | ||
| 481 | SDL::Tutorial::3DWorld::Actor::Model->new( | ||
| 482 | position => [ 10, 1, -5 ], | ||
| 483 | velocity => [ 0, 0, 0 ], | ||
| 484 | orient => [ 270, 1, 0, 0 ], | ||
| 485 | scale => 0.5, | ||
| 486 | file => File::Spec->catfile( | ||
| 487 | 'model', | ||
| 488 | 'gltron', | ||
| 489 | 'lightcycle-med.obj', | ||
| 490 | ), | ||
| 491 | ), | ||
| 492 | ); | ||
| 493 | |||
| 494 | # Add three tron light cycles of varying complexity | ||
| 495 | $self->actor( | ||
| 496 | SDL::Tutorial::3DWorld::Actor::Model->new( | ||
| 497 | position => [ 5, 1, -5 ], | ||
| 498 | velocity => [ 0, 0, 0 ], | ||
| 499 | orient => [ 270, 1, 0, 0 ], | ||
| 500 | scale => 0.5, | ||
| 501 | file => File::Spec->catfile( | ||
| 502 | 'model', | ||
| 503 | 'gltron', | ||
| 504 | 'lightcycle-low.obj', | ||
| 505 | ), | ||
| 506 | ), | ||
| 507 | ); | ||
| 508 | |||
| 509 | # Add a material sampler for the light cycle | ||
| 510 | $self->actor( | ||
| 511 | SDL::Tutorial::3DWorld::Actor::MaterialSampler->new( | ||
| 512 | hidden => $self->{hide_expensive}, | ||
| 513 | position => [ 5, 1, -10 ], | ||
| 514 | file => File::Spec->catfile( | ||
| 515 | File::ShareDir::dist_dir('SDL-Tutorial-3DWorld'), | ||
| 516 | 'model', | ||
| 517 | 'gltron', | ||
| 518 | 'lightcycle.mtl', | ||
| 519 | ), | ||
| 520 | ), | ||
| 521 | ); | ||
| 522 | |||
| 523 | return $self; | ||
| 524 | } | ||
| 525 | |||
| 526 | =pod | ||
| 527 | |||
| 528 | =head2 camera | ||
| 529 | |||
| 530 | The C<camera> method returns the currently active camera for the world. | ||
| 531 | |||
| 532 | Provided as a convenience for world objects that need to know where the | ||
| 533 | camera is (such as the skybox). | ||
| 534 | |||
| 535 | =cut | ||
| 536 | |||
| 537 | sub camera { | ||
| 538 | $_[0]->{camera}; | ||
| 539 | } | ||
| 540 | |||
| 541 | =pod | ||
| 542 | |||
| 543 | =head2 sdl | ||
| 544 | |||
| 545 | The C<sdl> method returns the master L<SDLx::App> object for the world. | ||
| 546 | |||
| 547 | =cut | ||
| 548 | |||
| 549 | sub sdl { | ||
| 550 | $_[0]->{sdl}; | ||
| 551 | } | ||
| 552 | |||
| 553 | |||
| 554 | |||
| 555 | |||
| 556 | |||
| 557 | ###################################################################### | ||
| 558 | # Scene Construction | ||
| 559 | |||
| 560 | # Add a new actor to the world | ||
| 561 | sub actor { | ||
| 562 | my $self = shift; | ||
| 563 | my $actor = shift; | ||
| 564 | my %param = @_; | ||
| 565 | push @{$self->{actors}}, $actor; | ||
| 566 | push @{$self->{move}}, $actor if $actor->{velocity}; | ||
| 567 | push @{$self->{show}}, $actor unless $actor->{hidden}; | ||
| 568 | |||
| 569 | # Initialise if needed | ||
| 570 | $actor->init if $param{init}; | ||
| 571 | |||
| 572 | # Shortcut unless we need the bounding box as well | ||
| 573 | return if $self->{benchmark}; | ||
| 574 | |||
| 575 | # Add said bounding box | ||
| 576 | my $debug = SDL::Tutorial::3DWorld::Actor::Debug->new( | ||
| 577 | parent => $actor, | ||
| 578 | hidden => $self->{hide_debug}, | ||
| 579 | ); | ||
| 580 | push @{$self->{actors}}, $debug; | ||
| 581 | push @{$self->{move}}, $debug; | ||
| 582 | push @{$self->{show}}, $debug unless $self->{hide_debug}; | ||
| 583 | |||
| 584 | # Initialise it too if needed | ||
| 585 | $debug->init if $param{init}; | ||
| 586 | |||
| 587 | # Returns the actor as a convenience | ||
| 588 | return $actor; | ||
| 589 | } | ||
| 590 | |||
| 591 | |||
| 592 | |||
| 593 | |||
| 594 | |||
| 595 | ###################################################################### | ||
| 596 | # Main Methods | ||
| 597 | |||
| 598 | =pod | ||
| 599 | |||
| 600 | =head2 run | ||
| 601 | |||
| 602 | The C<run> method is used to run the game. It takes care of all stages of | ||
| 603 | the game including initialisation and shutdown operations at the start | ||
| 604 | and end of the game. | ||
| 605 | |||
| 606 | =cut | ||
| 607 | |||
| 608 | sub run { | ||
| 609 | my $self = shift; | ||
| 610 | |||
| 611 | # Initialise the game | ||
| 612 | $self->init; | ||
| 613 | |||
| 614 | # Render handler | ||
| 615 | $self->{sdl}->add_show_handler( sub { | ||
| 616 | $self->display(@_); | ||
| 617 | $self->{sdl}->sync; | ||
| 618 | } ); | ||
| 619 | |||
| 620 | # Movement handler | ||
| 621 | $self->{sdl}->add_move_handler( sub { | ||
| 622 | if ( $self->{benchmark} and $_[2] > 100 ) { | ||
| 623 | $_[1]->stop; | ||
| 624 | return; | ||
| 625 | } | ||
| 626 | return unless $_[0]; | ||
| 627 | $self->move(@_); | ||
| 628 | } ); | ||
| 629 | |||
| 630 | # Event handler | ||
| 631 | $self->{sdl}->add_event_handler( sub { | ||
| 632 | $self->event(@_); | ||
| 633 | } ); | ||
| 634 | |||
| 635 | # This world is now the active world | ||
| 636 | local $CURRENT = $self; | ||
| 637 | |||
| 638 | # Enter the main loop | ||
| 639 | $self->{sdl}->run; | ||
| 640 | |||
| 641 | return 1; | ||
| 642 | } | ||
| 643 | |||
| 644 | =pod | ||
| 645 | |||
| 646 | =head2 current | ||
| 647 | |||
| 648 | The C<current> method can be used by any arbitrary world element to get | ||
| 649 | access to the world while it is running. | ||
| 650 | |||
| 651 | =cut | ||
| 652 | |||
| 653 | sub current { | ||
| 654 | $CURRENT or die "No current world is running"; | ||
| 655 | } | ||
| 656 | |||
| 657 | |||
| 658 | |||
| 659 | |||
| 660 | |||
| 661 | ###################################################################### | ||
| 662 | # Internal Methods | ||
| 663 | |||
| 664 | sub init { | ||
| 665 | my $self = shift; | ||
| 666 | |||
| 667 | # Verify the integrity of the installation. This shouldn't really | ||
| 668 | # be necesary but kthakore seems to have problems with partial | ||
| 669 | # overwriting his installs and mixing up versions of something. | ||
| 670 | # This is an attempt to at least partially defend against them. | ||
| 671 | foreach my $child ( sort grep { /3DWorld\// } keys %INC ) { | ||
| 672 | $child =~ s/\//::/g; | ||
| 673 | $child =~ s/\.pm//g; | ||
| 674 | next unless Params::Util::_CLASS($child); | ||
| 675 | my $v = $child->VERSION; | ||
| 676 | unless ( $v ) { | ||
| 677 | die "Corrupt installation detected! No \$VERSION in $child"; | ||
| 678 | } | ||
| 679 | unless ( $v == $VERSION ) { | ||
| 680 | die "Corrupt installation detected! Got \$VERSION $v in $child but expected $VERSION"; | ||
| 681 | } | ||
| 682 | } | ||
| 683 | |||
| 684 | # Create the SDL application object | ||
| 685 | $self->{sdl} = SDLx::App->new( | ||
| 686 | title => '3D World', | ||
| 687 | width => $self->{width}, | ||
| 688 | height => $self->{height}, | ||
| 689 | gl => 1, | ||
| 690 | fullscreen => $self->{fullscreen}, | ||
| 691 | depth => 24, # Prevent harsh colour stepping | ||
| 692 | double_buffer => 1, # Reduce flicker during rapid mouselook | ||
| 693 | min_t => 0, # As many frames as possible | ||
| 694 | ); | ||
| 695 | |||
| 696 | # Enable face culling to remove drawing of all rear surfaces | ||
| 697 | glCullFace( GL_BACK ); | ||
| 698 | glEnable( GL_CULL_FACE ); | ||
| 699 | |||
| 700 | # Use the prettiest shading available to us | ||
| 701 | glShadeModel( GL_SMOOTH ); | ||
| 702 | |||
| 703 | # Enable the Z buffer ( DEPTH BUFFER ) so that OpenGL will do all the | ||
| 704 | # correct shape culling for us and we don't have to care about it. | ||
| 705 | glDepthFunc( GL_LESS ); | ||
| 706 | glEnable( GL_DEPTH_TEST ); | ||
| 707 | |||
| 708 | # How thick are lines | ||
| 709 | glLineWidth(1); | ||
| 710 | |||
| 711 | # Enable basic anti-aliasing for everything | ||
| 712 | # glEnable( GL_BLEND ); | ||
| 713 | # glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); | ||
| 714 | glHint( GL_LINE_SMOOTH_HINT, GL_NICEST ); | ||
| 715 | glHint( GL_POINT_SMOOTH_HINT, GL_NICEST ); | ||
| 716 | glHint( GL_POLYGON_SMOOTH_HINT, GL_NICEST ); | ||
| 717 | glHint( GL_GENERATE_MIPMAP_HINT, GL_NICEST ); | ||
| 718 | # glEnable( GL_LINE_SMOOTH ); | ||
| 719 | # glEnable( GL_POINT_SMOOTH ); | ||
| 720 | # glEnable( GL_POLYGON_SMOOTH ); | ||
| 721 | |||
| 722 | # Lighting and textures are on by default | ||
| 723 | glEnable( GL_LIGHTING ); | ||
| 724 | glEnable( GL_TEXTURE_2D ); | ||
| 725 | |||
| 726 | # Compile a display list to do all non-varying frame reset tasks | ||
| 727 | $self->{reset} = OpenGL::List::glpList { | ||
| 728 | if ( $self->{skybox} ) { | ||
| 729 | # Optimisation: | ||
| 730 | # If we have a skybox then no part of the scene will ever show | ||
| 731 | # the background. As a result, we can clear only the depth buffer | ||
| 732 | # and this will result in the color buffer just being drawn over. | ||
| 733 | # This removes a fairly large memory clear operation and speeds | ||
| 734 | # up frame-initialisation phase of the rendering pipeline. | ||
| 735 | glClear( GL_DEPTH_BUFFER_BIT ); | ||
| 736 | } else { | ||
| 737 | # Clear the colour buffer (what we actually see) and the depth buffer | ||
| 738 | # (the area GL uses to remove things behind other things). | ||
| 739 | # This gives us a blank screen with our chosen sky colour. | ||
| 740 | glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); | ||
| 741 | } | ||
| 742 | |||
| 743 | # Reset the model, throwing away the previously calculated scene | ||
| 744 | # and starting again with a blank sky. | ||
| 745 | glMatrixMode( GL_MODELVIEW ); | ||
| 746 | glLoadIdentity(); | ||
| 747 | }; | ||
| 748 | |||
| 749 | # Initialise the camera so we can look at things | ||
| 750 | $self->{camera}->init; | ||
| 751 | |||
| 752 | # Initialise and load the skybox | ||
| 753 | if ( $self->{skybox} ) { | ||
| 754 | $self->{skybox}->init; | ||
| 755 | } | ||
| 756 | |||
| 757 | # Initialise the landscape so there is a world | ||
| 758 | $self->{landscape}->init; | ||
| 759 | |||
| 760 | # Initialise the actors. | ||
| 761 | # Randomise the order once to generate interesting effects. | ||
| 762 | foreach my $actor ( @{$self->{actors}} ) { | ||
| 763 | $actor->init; | ||
| 764 | } | ||
| 765 | |||
| 766 | # Initialise the console | ||
| 767 | if ( $self->{console} ) { | ||
| 768 | $self->{console}->init; | ||
| 769 | } | ||
| 770 | |||
| 771 | # Initialise and enable the fog (in this case a light ground haze) | ||
| 772 | if ( $self->{fog} ) { | ||
| 773 | $self->{fog}->init; | ||
| 774 | $self->{fog}->enable; | ||
| 775 | } | ||
| 776 | |||
| 777 | return 1; | ||
| 778 | } | ||
| 779 | |||
| 780 | sub event { | ||
| 781 | my $self = shift; | ||
| 782 | |||
| 783 | # Handle any events related to the camera. | ||
| 784 | # Since the move common high frequency events will be stuff like | ||
| 785 | # mouse movements to control mouselook we do this first. | ||
| 786 | $self->{camera}->event(@_) and return 1; | ||
| 787 | |||
| 788 | # Now handle lower-frequency events related to the application | ||
| 789 | # as a whole or elements within it. | ||
| 790 | my $event = shift; | ||
| 791 | my $type = $event->type; | ||
| 792 | if ( $type == SDL_KEYDOWN ) { | ||
| 793 | my $key = $event->key_sym; | ||
| 794 | |||
| 795 | # Quit the world | ||
| 796 | if ( $key == SDLK_ESCAPE ) { | ||
| 797 | $self->{sdl}->stop; | ||
| 798 | return 1; | ||
| 799 | } | ||
| 800 | |||
| 801 | # Toggle visibility of debugging actors | ||
| 802 | if ( $key == SDLK_F1 ) { | ||
| 803 | $self->{hide_debug} = $self->{hide_debug} ? 0 : 1; | ||
| 804 | foreach my $actor ( @{$self->{actors}} ) { | ||
| 805 | next unless $actor->isa('SDL::Tutorial::3DWorld::Actor::Debug'); | ||
| 806 | $actor->{hidden} = $self->{hide_debug}; | ||
| 807 | } | ||
| 808 | |||
| 809 | # Rebuild the index of visible objects | ||
| 810 | @{$self->{show}} = grep { not $_->{hidden} } @{$self->{actors}}; | ||
| 811 | |||
| 812 | return 1; | ||
| 813 | } | ||
| 814 | |||
| 815 | # Toggle visibility for unrealistically-expensive actors | ||
| 816 | if ( $key == SDLK_F2 ) { | ||
| 817 | $self->{hide_expensive} = $self->{hide_expensive} ? 0 : 1; | ||
| 818 | foreach my $actor ( @{$self->{actors}} ) { | ||
| 819 | if ( $actor->isa('SDL::Tutorial::3DWorld::Actor::MaterialSampler') ) { | ||
| 820 | $actor->{hidden} = $self->{hide_expensive}; | ||
| 821 | } | ||
| 822 | if ( $actor->isa('SDL::Tutorial::3DWorld::Actor::Teapot') ) { | ||
| 823 | $actor->{hidden} = $self->{hide_expensive}; | ||
| 824 | } | ||
| 825 | } | ||
| 826 | |||
| 827 | # Rebuild the index of visible objects | ||
| 828 | @{$self->{show}} = grep { not $_->{hidden} } @{$self->{actors}}; | ||
| 829 | |||
| 830 | return 1; | ||
| 831 | } | ||
| 832 | |||
| 833 | # Toggle visibility of the console (i.e. the FPS display) | ||
| 834 | if ( $key == SDLK_F3 ) { | ||
| 835 | $self->{hide_console} = $self->{hide_console} ? 0 : 1; | ||
| 836 | return 1; | ||
| 837 | } | ||
| 838 | |||
| 839 | # Trigger bit's "Yes" or "No" cycle | ||
| 840 | if ( $key == SDLK_y ) { | ||
| 841 | $self->{bit}->yes; | ||
| 842 | return 1; | ||
| 843 | } | ||
| 844 | if ( $key == SDLK_n ) { | ||
| 845 | $self->{bit}->no; | ||
| 846 | return 1; | ||
| 847 | } | ||
| 848 | |||
| 849 | } elsif ( $type == SDL_MOUSEBUTTONDOWN ) { | ||
| 850 | # Make the scroll wheel move the selection box towards | ||
| 851 | # and away from the camera. | ||
| 852 | my $selector = $self->{selector}; | ||
| 853 | my $button = $event->button_button; | ||
| 854 | if ( $button == SDL_BUTTON_WHEELUP ) { | ||
| 855 | # Move away from the camera | ||
| 856 | $selector->{distance} += 0.5; | ||
| 857 | return 1; | ||
| 858 | } | ||
| 859 | if ( $button == SDL_BUTTON_WHEELDOWN ) { | ||
| 860 | # Move towards the camera, stopping | ||
| 861 | # at some suitable minimum distance. | ||
| 862 | $selector->{distance} -= 0.5; | ||
| 863 | if ( $selector->{distance} < 2 ) { | ||
| 864 | $selector->{distance} = 2; | ||
| 865 | } | ||
| 866 | return 1; | ||
| 867 | } | ||
| 868 | |||
| 869 | # Place a new texture box at the selector location | ||
| 870 | if ( $button == SDL_BUTTON_LEFT ) { | ||
| 871 | my $cube = SDL::Tutorial::3DWorld::Actor::TextureCube->new( | ||
| 872 | position => [ | ||
| 873 | $selector->{position}->[0] + 0.5, | ||
| 874 | $selector->{position}->[1], | ||
| 875 | $selector->{position}->[2] + 0.5, | ||
| 876 | ], | ||
| 877 | material => { | ||
| 878 | ambient => [ 0.5, 0.5, 0.5, 1 ], | ||
| 879 | texture => $self->sharefile('crate1.jpg'), | ||
| 880 | }, | ||
| 881 | ); | ||
| 882 | $self->actor( $cube, init => 1 ); | ||
| 883 | return 1; | ||
| 884 | } | ||
| 885 | |||
| 886 | # Let right mouse button drop us back into debugging | ||
| 887 | if ( $button == SDL_BUTTON_RIGHT ) { | ||
| 888 | $DB::single = 1; | ||
| 889 | } | ||
| 890 | } | ||
| 891 | |||
| 892 | return 1; | ||
| 893 | } | ||
| 894 | |||
| 895 | sub move { | ||
| 896 | my $self = shift; | ||
| 897 | my $move = $self->{move}; | ||
| 898 | |||
| 899 | # Move each of the actors in the scene. | ||
| 900 | $_->move(@_) foreach @$move; | ||
| 901 | |||
| 902 | # Move the camera last, since it is more likely that the position | ||
| 903 | # of the camera will be limited by where the actors are than the | ||
| 904 | # actors being limited by where the camera is. Especially since the | ||
| 905 | # camera doesn't currently have an avatar. | ||
| 906 | $self->{camera}->move(@_); | ||
| 907 | } | ||
| 908 | |||
| 909 | # This is the primary render loop | ||
| 910 | sub display { | ||
| 911 | my $self = shift; | ||
| 912 | my $camera = $self->{camera}; | ||
| 913 | |||
| 914 | # Reset the frame | ||
| 915 | glCallList( $self->{reset} ); | ||
| 916 | |||
| 917 | # Move the camera to the required position. | ||
| 918 | # NOTE: For now just translate back so we can see the render. | ||
| 919 | $camera->display; | ||
| 920 | |||
| 921 | # Draw the skybox | ||
| 922 | $self->{skybox}->display if $self->{skybox}; | ||
| 923 | |||
| 924 | # Draw the landscape in the scene | ||
| 925 | $self->{landscape}->display; | ||
| 926 | |||
| 927 | # Light the scene. | ||
| 928 | # All lighting is global in this demonstration. | ||
| 929 | foreach my $light ( @{$self->{lights}} ) { | ||
| 930 | $light->display; | ||
| 931 | } | ||
| 932 | |||
| 933 | # Display all of the actors visible by the main scene camera. | ||
| 934 | # Pass the camera because later we may want to do some tricks involving | ||
| 935 | # multiple cameras. | ||
| 936 | $self->display_actors($camera); | ||
| 937 | |||
| 938 | # Draw the console last, on top of everything else | ||
| 939 | if ( $self->{console} and not $self->{hide_console} ) { | ||
| 940 | $self->{console}->display; | ||
| 941 | } | ||
| 942 | |||
| 943 | return 1; | ||
| 944 | } | ||
| 945 | |||
| 946 | # Simultaneously cull and render the solid objects in the scene, | ||
| 947 | # storing blending objects for a second sorting/render pass. | ||
| 948 | sub display_actors { | ||
| 949 | my $self = shift; | ||
| 950 | my $camera = shift; | ||
| 951 | my $show = $self->{show}; | ||
| 952 | my @blend = (); | ||
| 953 | my @distance = (); | ||
| 954 | foreach my $actor ( @$show ) { | ||
| 955 | my $position = $actor->{position}; | ||
| 956 | my $bound = $actor->{bound}; | ||
| 957 | |||
| 958 | # Most things should have bounding boxes | ||
| 959 | if ( $actor->{bound} ) { | ||
| 960 | # Sphere-sphere culling, which is really fast. | ||
| 961 | # Compare sizes of the squares to avoid extra sqrt calls. | ||
| 962 | ( | ||
| 963 | ($bound->[SPHERE_R] + $camera->{rsphere}) ** 2 # $RS | ||
| 964 | ) > ( | ||
| 965 | ($position->[0] + $bound->[SPHERE_X] - $camera->{xsphere}) ** 2 + # $XS | ||
| 966 | ($position->[1] + $bound->[SPHERE_Y] - $camera->{ysphere}) ** 2 + # $YS | ||
| 967 | ($position->[2] + $bound->[SPHERE_Z] - $camera->{zsphere}) ** 2 # $ZS | ||
| 968 | ) or next; | ||
| 969 | |||
| 970 | # Second pass is cone-sphere culling, which is slower but | ||
| 971 | # will cull down to something reasonsably close to the ideal. | ||
| 972 | ### NOTE: TO BE IMPLEMENTED LATER | ||
| 973 | |||
| 974 | } else { | ||
| 975 | |||
| 976 | # Sphere-point culling for actors without volume (and | ||
| 977 | # thus no bounding shapes). This is similar to | ||
| 978 | # sphere-sphere but slightly simpler as it effectively | ||
| 979 | # uses a zero-offset zero-size bounding sphere. | ||
| 980 | ( | ||
| 981 | $camera->{rsphere} ** 2 | ||
| 982 | ) > ( | ||
| 983 | ($position->[0] - $camera->{xsphere}) ** 2 + # $XS | ||
| 984 | ($position->[1] - $camera->{ysphere}) ** 2 + # $YS | ||
| 985 | ($position->[2] - $camera->{zsphere}) ** 2 # $ZS | ||
| 986 | ) or next; | ||
| 987 | } | ||
| 988 | |||
| 989 | # Objects that need blending must be rendered from furthest | ||
| 990 | # to nearest, but the render order of solid objects is mostly | ||
| 991 | # irrelevant. So we render solid stuff immediately. | ||
| 992 | unless ( $actor->{blending} ) { | ||
| 993 | defined( $actor->{display} ) | ||
| 994 | # If the actor has a completely pre-built display list, | ||
| 995 | # shortcut their display method and just draw it. | ||
| 996 | # The actor is expected to do everything, including the | ||
| 997 | # push/pop matrix operation shown below to isolate drawing. | ||
| 998 | ? glCallList( $actor->{display} ) | ||
| 999 | : do { | ||
| 1000 | # Draw each actor in their own stack context so that | ||
| 1001 | # their transform operations do not effect anything else. | ||
| 1002 | glPushMatrix(); | ||
| 1003 | $actor->display; | ||
| 1004 | glPopMatrix(); | ||
| 1005 | }; | ||
| 1006 | next; | ||
| 1007 | } | ||
| 1008 | |||
| 1009 | # Precalculate the distance now. Because we are only | ||
| 1010 | # using it for sorting we can safely avoid the sqrt | ||
| 1011 | # and sort on the squared distance instead. | ||
| 1012 | push @blend, $actor; | ||
| 1013 | push @distance, ( | ||
| 1014 | ($camera->{X} - $position->[0]) ** 2 + | ||
| 1015 | ($camera->{Y} - $position->[1]) ** 2 + | ||
| 1016 | ($camera->{Z} - $position->[2]) ** 2 | ||
| 1017 | ); | ||
| 1018 | } | ||
| 1019 | |||
| 1020 | # Render the remaining elements in sorted order | ||
| 1021 | foreach my $actor ( | ||
| 1022 | map { | ||
| 1023 | $blend[$_] | ||
| 1024 | } sort { | ||
| 1025 | $distance[$b] <=> $distance[$a] | ||
| 1026 | } ( 0 .. $#blend ) | ||
| 1027 | ) { | ||
| 1028 | # Repeat the display code shown above | ||
| 1029 | defined( $actor->{display} ) | ||
| 1030 | ? glCallList( $actor->{display} ) | ||
| 1031 | : do { | ||
| 1032 | glPushMatrix(); | ||
| 1033 | $actor->display; | ||
| 1034 | glPopMatrix(); | ||
| 1035 | }; | ||
| 1036 | } | ||
| 1037 | |||
| 1038 | # Disable normalisation in case the last object left us | ||
| 1039 | # with normalisation enabled. | ||
| 1040 | OpenGL::glDisable( OpenGL::GL_NORMALIZE ); | ||
| 1041 | |||
| 1042 | return 1; | ||
| 1043 | } | ||
| 1044 | |||
| 1045 | |||
| 1046 | |||
| 1047 | |||
| 1048 | |||
| 1049 | ###################################################################### | ||
| 1050 | # Utility Methods | ||
| 1051 | |||
| 1052 | sub dvector { | ||
| 1053 | my $dt = $_[0]->{dt}; | ||
| 1054 | return [ | ||
| 1055 | $_[1] * $dt, | ||
| 1056 | $_[2] * $dt, | ||
| 1057 | $_[3] * $dt, | ||
| 1058 | ]; | ||
| 1059 | } | ||
| 1060 | |||
| 1061 | sub dscalar { | ||
| 1062 | $_[0]->{dt} * $_[1]; | ||
| 1063 | } | ||
| 1064 | |||
| 1065 | sub sharedir { | ||
| 1066 | my $self = shift; | ||
| 1067 | File::Spec->rel2abs( | ||
| 1068 | File::Spec->catdir( | ||
| 1069 | File::ShareDir::dist_dir('SDL-Tutorial-3DWorld'), | ||
| 1070 | @_, | ||
| 1071 | ), | ||
| 1072 | ); | ||
| 1073 | } | ||
| 1074 | |||
| 1075 | sub sharefile { | ||
| 1076 | my $self = shift; | ||
| 1077 | File::Spec->rel2abs( | ||
| 1078 | File::Spec->catfile( | ||
| 1079 | File::ShareDir::dist_dir('SDL-Tutorial-3DWorld'), | ||
| 1080 | @_, | ||
| 1081 | ), | ||
| 1082 | ); | ||
| 1083 | } | ||
| 1084 | |||
| 1085 | 1; | ||
| 1086 | |||
| 1087 | =pod | ||
| 1088 | |||
| 1089 | =head1 SUPPORT | ||
| 1090 | |||
| 1091 | Bugs should be reported via the CPAN bug tracker at | ||
| 1092 | |||
| 1093 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=SDL-Tutorial-3DWorld> | ||
| 1094 | |||
| 1095 | =head1 AUTHOR | ||
| 1096 | |||
| 1097 | Adam Kennedy E<lt>adamk@cpan.orgE<gt> | ||
| 1098 | |||
| 1099 | =head1 SEE ALSO | ||
| 1100 | |||
| 1101 | L<SDL>, L<OpenGL> | ||
| 1102 | |||
| 1103 | =head1 COPYRIGHT | ||
| 1104 | |||
| 1105 | Copyright 2010 - 2011 Adam Kennedy. | ||
| 1106 | |||
| 1107 | This program is free software; you can redistribute | ||
| 1108 | it and/or modify it under the same terms as Perl itself. | ||
| 1109 | |||
| 1110 | The full text of the license can be found in the | ||
| 1111 | LICENSE file included with this module. | ||
| 1112 | |||
| 1113 | =cut | ||