| 1 | package SDL::Tutorial::3DWorld::Landscape::Infinite; | ||
| 2 | |||
| 3 | =pod | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | SDL::Tutorial::3DWorld::Landscape::Infinite - An infinite ground plane | ||
| 8 | |||
| 9 | =head1 DESCRIPTION | ||
| 10 | |||
| 11 | In many basic or primitive 3D worlds the ground is implemented as a single | ||
| 12 | infinite plane at zero height. | ||
| 13 | |||
| 14 | This type of ground structure greatly simplies many aspects of a world | ||
| 15 | implementation as we don't need to implement full vertical collision detection. | ||
| 16 | |||
| 17 | =head1 METHODS | ||
| 18 | |||
| 19 | =cut | ||
| 20 | |||
| 21 | use 5.008; | ||
| 22 | use strict; | ||
| 23 | use warnings; | ||
| 24 | use Params::Util (); | ||
| 25 | use SDL::Tutorial::3DWorld::OpenGL (); | ||
| 26 | use SDL::Tutorial::3DWorld::Landscape (); | ||
| 27 | |||
| 28 | our $VERSION = '0.33'; | ||
| 29 | our @ISA = 'SDL::Tutorial::3DWorld::Landscape'; | ||
| 30 | |||
| 31 | sub new { | ||
| 32 | my $class = shift; | ||
| 33 | my $self = bless { @_ }, $class; | ||
| 34 | |||
| 35 | # Convert the texture parameter to a texture object | ||
| 36 | unless ( Params::Util::_INSTANCE($self->{texture}, 'SDL::Tutorial::3DWorld::Texture') ) { | ||
| 37 | $self->{texture} = SDL::Tutorial::3DWorld::Texture->new( | ||
| 38 | file => $self->{texture}, | ||
| 39 | ); | ||
| 40 | } | ||
| 41 | |||
| 42 | return $self; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub init { | ||
| 46 | my $self = shift; | ||
| 47 | $self->SUPER::init(@_); | ||
| 48 | |||
| 49 | # Set the universal background colour | ||
| 50 | OpenGL::glClearColor( $self->sky ); | ||
| 51 | |||
| 52 | # Give everything at least a little light | ||
| 53 | OpenGL::glLightModelfv_p( OpenGL::GL_LIGHT_MODEL_AMBIENT, 0.5, 0.5, 0.5, 1 ); | ||
| 54 | |||
| 55 | $self->{texture}->init; | ||
| 56 | return 1; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub display { | ||
| 60 | my $self = shift; | ||
| 61 | my $size = 1000; | ||
| 62 | my $tile = 3; | ||
| 63 | |||
| 64 | # Calculate the geometry boundaries | ||
| 65 | my $vpos = $size / 2; | ||
| 66 | my $vneg = -$vpos; | ||
| 67 | |||
| 68 | # Calculate the texture boundaries | ||
| 69 | my $tpos = $vpos / $tile; | ||
| 70 | my $tneg = $vneg / $tile; | ||
| 71 | |||
| 72 | # Set up the surface material | ||
| 73 | $self->{texture}->display; | ||
| 74 | OpenGL::glMaterialfv_p( OpenGL::GL_FRONT, OpenGL::GL_AMBIENT, 0.7, 0.7, 0.7, 1 ); | ||
| 75 | OpenGL::glMaterialfv_p( OpenGL::GL_FRONT, OpenGL::GL_DIFFUSE, 0.7, 0.7, 0.7, 1 ); | ||
| 76 | OpenGL::glMaterialfv_p( OpenGL::GL_FRONT, OpenGL::GL_SPECULAR, 0.0, 0.0, 0.0, 1 ); | ||
| 77 | OpenGL::glMaterialf( OpenGL::GL_FRONT, OpenGL::GL_SHININESS, 127 ); | ||
| 78 | |||
| 79 | # Draw the the "infinite" plane, placing it 1/10th of a mm below zero, so that | ||
| 80 | # any other surfaces at zero height will be visible about the ground plane. | ||
| 81 | # Spread the texture across the entire plane to start with. | ||
| 82 | OpenGL::glBegin( OpenGL::GL_QUADS ); | ||
| 83 | OpenGL::glNormal3f( 0, 1, 0 ); | ||
| 84 | OpenGL::glTexCoord2f( $tneg, $tneg ); OpenGL::glVertex3d( $vneg, -0.0001, $vneg ); # Top Left | ||
| 85 | OpenGL::glTexCoord2f( $tneg, $tpos ); OpenGL::glVertex3d( $vneg, -0.0001, $vpos ); # Bottom Left | ||
| 86 | OpenGL::glTexCoord2f( $tpos, $tpos ); OpenGL::glVertex3d( $vpos, -0.0001, $vpos ); # Bottom Right | ||
| 87 | OpenGL::glTexCoord2f( $tpos, $tneg ); OpenGL::glVertex3d( $vpos, -0.0001, $vneg ); # Top Right | ||
| 88 | OpenGL::glEnd(); | ||
| 89 | } | ||
| 90 | |||
| 91 | 1; | ||
| 92 | |||
| 93 | =pod | ||
| 94 | |||
| 95 | =head1 SUPPORT | ||
| 96 | |||
| 97 | Bugs should be reported via the CPAN bug tracker at | ||
| 98 | |||
| 99 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=SDL-Tutorial-3DWorld> | ||
| 100 | |||
| 101 | =head1 AUTHOR | ||
| 102 | |||
| 103 | Adam Kennedy E<lt>adamk@cpan.orgE<gt> | ||
| 104 | |||
| 105 | =head1 SEE ALSO | ||
| 106 | |||
| 107 | L<SDL>, L<OpenGL> | ||
| 108 | |||
| 109 | =head1 COPYRIGHT | ||
| 110 | |||
| 111 | Copyright 2010 Adam Kennedy. | ||
| 112 | |||
| 113 | This program is free software; you can redistribute | ||
| 114 | it and/or modify it under the same terms as Perl itself. | ||
| 115 | |||
| 116 | The full text of the license can be found in the | ||
| 117 | LICENSE file included with this module. | ||
| 118 | |||
| 119 | =cut |