| 1 | package SDL::Tutorial::3DWorld::Landscape; | ||
| 2 | |||
| 3 | =pod | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | SDL::Tutorial::3DWorld::Landscape - The static 3D environment of the world | ||
| 8 | |||
| 9 | =head1 DESCRIPTION | ||
| 10 | |||
| 11 | A landscape is the "world" part of the 3D World. It will generally just sit | ||
| 12 | in the same place, looking pretty (ideally) and doing nothing (mostly). | ||
| 13 | |||
| 14 | While it may sometimes change in shape, it certainly does not move around | ||
| 15 | as a whole. | ||
| 16 | |||
| 17 | The B<SDL::Tutorial::3DWorld::Landscape> module is responsible for creating | ||
| 18 | the world, and updating it if needed. | ||
| 19 | |||
| 20 | In this demonstration code, the landscape consists of a simple 50m x 50m | ||
| 21 | white square. | ||
| 22 | |||
| 23 | =head1 METHODS | ||
| 24 | |||
| 25 | =cut | ||
| 26 | |||
| 27 | use 5.008; | ||
| 28 | use strict; | ||
| 29 | use warnings; | ||
| 30 | use File::Spec (); | ||
| 31 | use File::ShareDir (); | ||
| 32 | use SDL::Tutorial::3DWorld::Texture (); | ||
| 33 | use OpenGL; | ||
| 34 | |||
| 35 | our $VERSION = '0.33'; | ||
| 36 | |||
| 37 | =pod | ||
| 38 | |||
| 39 | =head2 new | ||
| 40 | |||
| 41 | The C<new> constructor for the landscape. It takes no parameters and | ||
| 42 | returns an object representing the static part of the game world. | ||
| 43 | |||
| 44 | =cut | ||
| 45 | |||
| 46 | sub new { | ||
| 47 | my $class = shift; | ||
| 48 | my $self = bless { @_ }, $class; | ||
| 49 | |||
| 50 | # Create a texture handle | ||
| 51 | $self->{texture} = SDL::Tutorial::3DWorld::Texture->new( | ||
| 52 | file => File::Spec->catfile( | ||
| 53 | File::ShareDir::dist_dir('SDL-Tutorial-3DWorld'), | ||
| 54 | 'chessboard.jpg', | ||
| 55 | ), | ||
| 56 | ); | ||
| 57 | |||
| 58 | return $self; | ||
| 59 | } | ||
| 60 | |||
| 61 | =pod | ||
| 62 | |||
| 63 | =head2 sky | ||
| 64 | |||
| 65 | my ($red, $green, $blue, $alpha) = $landscape->sky; | ||
| 66 | |||
| 67 | The C<sky> method returns the colour of the sky as a four element RGBA list, | ||
| 68 | with each colour a value in the range 0 to 1. | ||
| 69 | |||
| 70 | The sky value is the colour that is used to wipe the OpenGL colour buffer | ||
| 71 | (the buffer that ultimately becomes what appears on your monitor) and so will | ||
| 72 | serve effectively as the "background colour" for your world. | ||
| 73 | |||
| 74 | This value is not currently configurable, and will always return a dark navy | ||
| 75 | blue. | ||
| 76 | |||
| 77 | =cut | ||
| 78 | |||
| 79 | sub sky { | ||
| 80 | return ( 0, 0, 0, 1 ); | ||
| 81 | } | ||
| 82 | |||
| 83 | |||
| 84 | |||
| 85 | |||
| 86 | |||
| 87 | ###################################################################### | ||
| 88 | # Engine Interface | ||
| 89 | |||
| 90 | # Configure the colour that each frame will be cleared with before | ||
| 91 | # any objects are drawn. This is effectively the "sky" colour. | ||
| 92 | # We get the colour from the sky method, so that later on this value | ||
| 93 | # can be configurable. | ||
| 94 | sub init { | ||
| 95 | my $self = shift; | ||
| 96 | |||
| 97 | # Set the universal background colour | ||
| 98 | glClearColor( $self->sky ); | ||
| 99 | |||
| 100 | # Give everything at least a little light | ||
| 101 | OpenGL::glLightModelfv_p( GL_LIGHT_MODEL_AMBIENT, 0.2, 0.2, 0.2, 1 ); | ||
| 102 | |||
| 103 | # Load the landscape chessboard texture | ||
| 104 | $self->{texture}->init; | ||
| 105 | } | ||
| 106 | |||
| 107 | # Draw a variable colour 20 metre wide flat square at zero height | ||
| 108 | sub display { | ||
| 109 | my $self = shift; | ||
| 110 | |||
| 111 | # Set up the surface material | ||
| 112 | $self->{texture}->display; | ||
| 113 | OpenGL::glMaterialf( GL_FRONT, GL_SHININESS, 50 ); | ||
| 114 | OpenGL::glMaterialfv_p( GL_FRONT, GL_SPECULAR, 0.3, 0.3, 0.3, 1 ); | ||
| 115 | OpenGL::glMaterialfv_p( GL_FRONT, GL_DIFFUSE, 0.7, 0.7, 0.7, 1 ); | ||
| 116 | OpenGL::glMaterialfv_p( GL_FRONT, GL_AMBIENT, 0.3, 0.3, 0.3, 1 ); | ||
| 117 | |||
| 118 | # Draw the platform | ||
| 119 | glBegin( GL_QUADS ); | ||
| 120 | glNormal3f( 0, 1, 0 ); | ||
| 121 | glTexCoord2f( 0, 1000 ); glVertex3d( -4, 0, -4 ); # Top Left | ||
| 122 | glTexCoord2f( 0, 1000 ); glVertex3d( -4, 0, 4 ); # Bottom Left | ||
| 123 | glTexCoord2f( 1000, 1000 ); glVertex3d( 4, 0, 4 ); # Bottom Right | ||
| 124 | glTexCoord2f( 1000, 0 ); glVertex3d( 4, 0, -4 ); # Top Right | ||
| 125 | glEnd(); | ||
| 126 | } | ||
| 127 | |||
| 128 | 1; | ||
| 129 | |||
| 130 | =pod | ||
| 131 | |||
| 132 | =head1 SUPPORT | ||
| 133 | |||
| 134 | Bugs should be reported via the CPAN bug tracker at | ||
| 135 | |||
| 136 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=SDL-Tutorial-3DWorld> | ||
| 137 | |||
| 138 | =head1 AUTHOR | ||
| 139 | |||
| 140 | Adam Kennedy E<lt>adamk@cpan.orgE<gt> | ||
| 141 | |||
| 142 | =head1 SEE ALSO | ||
| 143 | |||
| 144 | L<SDL>, L<OpenGL> | ||
| 145 | |||
| 146 | =head1 COPYRIGHT | ||
| 147 | |||
| 148 | Copyright 2010 Adam Kennedy. | ||
| 149 | |||
| 150 | This program is free software; you can redistribute | ||
| 151 | it and/or modify it under the same terms as Perl itself. | ||
| 152 | |||
| 153 | The full text of the license can be found in the | ||
| 154 | LICENSE file included with this module. | ||
| 155 | |||
| 156 | =cut |