3DWorld/Skybox.pm untested

filter: Statementstmt 0.0% 0 / 135 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 170 CC 5 SCAR 14.6
CC
5
Cov
0%
new
12.0
init
6.0
compile
6.0
CRAP 4.3
1 package SDL::Tutorial::3DWorld::Skybox;
2  
3 =pod
4  
5 =head1 NAME
6  
7 SDL::Tutorial::3DWorld::Skybox - Better than a uniform sky colour
8  
9 =head1 DESCRIPTION
10  
11 A Skybox is a common method for drawing a full photographic background
12 for a 3D World visible in all directions.
13  
14 It consists of a large cube with a set of six special pre-rendered
15 textures that stich together at the edges, creating the appearance of
16 a large surrounding environment.
17  
18 For more information on the general concept of a sky box see
19 L<http://en.wikipedia.org/wiki/Skybox_%28video_games%29>.
20  
21 This basic implementation takes a directory and looks for six image files
22 within it called F<north.bmp>, F<south.bmp>, F<east.bmp>, F<west.bmp>,
23 F<up.bmp> and F<down.bmp>.
24  
25 The textures are projected onto the cube and uniformly lit, ignoring the
26 normal lighting model.
27  
28 The main special effects "trick" to implementing a skybox is that it will
29 rotate with the rest of the world but match the movements of the camera
30 so that artifacts in the skybox texture stay the same size regardless of
31 the movement of the camera (making them appear to be a long way away).
32  
33 =head1 METHODS
34  
35 =cut
36  
37 use 5.008;
38 use strict;
39 use warnings;
40 use File::Spec ();
41 use SDL::Tutorial::3DWorld ();
42 use SDL::Tutorial::3DWorld::Camera ();
43 use SDL::Tutorial::3DWorld::Texture ();
44 use SDL::Tutorial::3DWorld::OpenGL ();
45 use OpenGL::List ();
46 use OpenGL;
47  
48 our $VERSION = '0.33';
49  
50 =pod
51  
52 =head2 new
53  
54 # Load a skybox from a set of files included within a distribution
55 my $sky = SDL::Tutorial::3DWorld::Skybox->new(
56 directory => File::Spec->catdir(
57 File::ShareDir::dist_dir('SDL-Tutorial-3DWorld'), 'skybox'
58 )
59 );
60  
61 The C<new> constructor creates a new skybox object.
62  
63 It takes a single C<directory> parameter which should be a directory that
64 exists, and contains the six named BMP skybox texture files.
65  
66 Although the existance of the texture files will be checked at constructor
67 time, they will not actually be loaded until you run the world (during the
68 init phase).
69  
70 =cut
71  
72 sub new {
73 my $class = shift;
74 my $self = bless { @_ }, $class;
75  
76 # Check params
77 my $type = $self->type;
78 my $directory = $self->directory;
79 unless ( $type ) {
80 die "Missing or invalid skybox texture file type";
81 }
82 unless ( defined $directory and -d $directory ) {
83 die "Missing or invalid skybox texture directory";
84 }
85  
86 # Locate the five main textures
87 foreach my $side ( qw{ north east south west up } ) {
88 $self->{$side} = SDL::Tutorial::3DWorld::Texture->new(
89 file => File::Spec->catfile(
90 $directory, "$side.$type",
91 ),
92 );
93 }
94  
95 # Many sky boxes don't have a bottom texture, we are ok with that
96 local $@;
97 $self->{down} = eval {
98 SDL::Tutorial::3DWorld::Texture->new(
99 file => File::Spec->catfile(
100 $directory, "down.$type",
101 )
102 );
103 };
104  
105 return $self;
106 }
107  
108 =pod
109  
110 =head2 directory
111  
112 The C<directory> accessor returns the skybox texture directory.
113  
114 =cut
115  
116 sub directory {
117 $_[0]->{directory};
118 }
119  
120 =pod
121  
122 =head2 type
123  
124 The C<type> accessor returns the file type of the skybox textures.
125  
126 =cut
127  
128 sub type {
129 $_[0]->{type};
130 }
131  
132  
133  
134  
135  
136 ######################################################################
137 # Engine Interface
138  
139 sub init {
140 my $self = shift;
141  
142 # Initialise all the textures
143 foreach my $side ( qw{ north east south west up down } ) {
144 $self->{$side}->init if $self->{$side};
145 }
146  
147 # Compile the display list
148 $self->{list} = OpenGL::List::glpList {
149 $self->compile;
150 };
151  
152 return 1;
153 }
154  
155 # NOTE: Currently the textures are actually being drawn onto the walls
156 # backwards. We should put some works on the sky box so we can prove it
157 # is being drawn on correctly.
158 sub display {
159 my $self = shift;
160 my $camera = SDL::Tutorial::3DWorld->current->camera;
161  
162 # To make the skybox special effect work, we move the cube so that
163 # it is always centred around the camera.
164 glPushMatrix();
165 glTranslatef( $camera->X, $camera->Y, $camera->Z );
166  
167 # Call the display list
168 glCallList( $self->{list} );
169  
170 # Exit the special Matrix context the skybox needs to draw in.
171 glPopMatrix();
172  
173 return 1;
174 }
175  
176 # Compilable portion of the display logic
177 sub compile {
178 my $self = shift;
179
180 # Lighting does not apply to the skybox.
181 # Reset coloring to white so we don't leak a color from a model.
182 glDisable( GL_LIGHTING );
183 glEnable( GL_TEXTURE_2D );
184 glColor4f( 1, 1, 1, 1 );
185  
186 # When drawing the skybox cube, each quad should be slightly larger
187 # around than the distance each face is away from the camera.
188 # This creates an extremely slight overlap at each edge and prevents
189 # visible "seams" on the skybox which otherwise ruin the sky effect.
190 # 1.002 is enough to remove these seams, but using 1.1 isn't enough.
191  
192 # Draw the north face
193 $self->{north}->display;
194 glBegin( GL_QUADS );
195 glTexCoord2f( 0, 0 ); glVertex3f( -1.002, 1.002, -1 ); # Top Left
196 glTexCoord2f( 0, 1 ); glVertex3f( -1.002, -1.002, -1 ); # Bottom Left
197 glTexCoord2f( 1, 1 ); glVertex3f( 1.002, -1.002, -1 ); # Bottom Right
198 glTexCoord2f( 1, 0 ); glVertex3f( 1.002, 1.002, -1 ); # Top Right
199 glEnd();
200  
201 # Draw the south face
202 $self->{south}->display;
203 glBegin( GL_QUADS );
204 glTexCoord2f( 0, 0 ); glVertex3f( 1.002, 1.002, 1 ); # Top Left
205 glTexCoord2f( 0, 1 ); glVertex3f( 1.002, -1.002, 1 ); # Bottom Left
206 glTexCoord2f( 1, 1 ); glVertex3f( -1.002, -1.002, 1 ); # Bottom Right
207 glTexCoord2f( 1, 0 ); glVertex3f( -1.002, 1.002, 1 ); # Top Right
208 glEnd();
209  
210 # Draw the east face
211 $self->{east}->display;
212 glBegin( GL_QUADS );
213 glTexCoord2f( 0, 0 ); glVertex3f( 1, 1.002, -1.002 ); # Top Left
214 glTexCoord2f( 0, 1 ); glVertex3f( 1, -1.002, -1.002 ); # Bottom Left
215 glTexCoord2f( 1, 1 ); glVertex3f( 1, -1.002, 1.002 ); # Bottom Right
216 glTexCoord2f( 1, 0 ); glVertex3f( 1, 1.002, 1.002 ); # Top Right
217 glEnd();
218  
219 # Draw the west face
220 $self->{west}->display;
221 glBegin( GL_QUADS );
222 glTexCoord2f( 0, 0 ); glVertex3f( -1, 1.002, 1.002 ); # Top Left
223 glTexCoord2f( 0, 1 ); glVertex3f( -1, -1.002, 1.002 ); # Bottom Left
224 glTexCoord2f( 1, 1 ); glVertex3f( -1, -1.002, -1.002 ); # Bottom Right
225 glTexCoord2f( 1, 0 ); glVertex3f( -1, 1.002, -1.002 ); # Top Right
226 glEnd();
227  
228 # Draw the ceiling face
229 $self->{up}->display;
230 glBegin( GL_QUADS );
231 glTexCoord2f( 0, 0 ); glVertex3f( -1.002, 1, 1.002 ); # Top Left
232 glTexCoord2f( 0, 1 ); glVertex3f( -1.002, 1, -1.002 ); # Bottom Left
233 glTexCoord2f( 1, 1 ); glVertex3f( 1.002, 1, -1.002 ); # Bottom Right
234 glTexCoord2f( 1, 0 ); glVertex3f( 1.002, 1, 1.002 ); # Top Right
235 glEnd();
236  
237 # Draw the optional floor
238 if ( $self->{down} ) {
239 $self->{down}->display;
240 glBegin( GL_QUADS );
241 glTexCoord2f( 0, 0 ); glVertex3f( -1.002, -1, 1.002 ); # Top Left
242 glTexCoord2f( 0, 1 ); glVertex3f( -1.002, -1, -1.002 ); # Bottom Left
243 glTexCoord2f( 1, 1 ); glVertex3f( 1.002, -1, -1.002 ); # Bottom Right
244 glTexCoord2f( 1, 0 ); glVertex3f( 1.002, -1, 1.002 ); # Top Right
245 glEnd();
246 }
247  
248 # Flush the depth buffer so we can draw the rest of the world.
249 # Some tutorials suggest disabling depth buffering but in my experience
250 # this still provides some tearing. I prefer to draw the box and then
251 # flush the depth buffer instead.
252 glClear( GL_DEPTH_BUFFER_BIT );
253  
254 # Light is on by default in the 3DWorld tutorial, so setting it
255 # back to the default here means every other lit object doesn't
256 # need to explicitly turn it on.
257 glEnable( GL_LIGHTING );
258  
259 return 1;
260 }
261  
262 =cut
263  
264 =pod
265  
266 =head1 SUPPORT
267  
268 Bugs should be reported via the CPAN bug tracker at
269  
270 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=SDL-Tutorial-3DWorld>
271  
272 =head1 AUTHOR
273  
274 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
275  
276 =head1 SEE ALSO
277  
278 L<SDL>, L<OpenGL>
279  
280 =head1 COPYRIGHT
281  
282 Copyright 2010 Adam Kennedy.
283  
284 This program is free software; you can redistribute
285 it and/or modify it under the same terms as Perl itself.
286  
287 The full text of the license can be found in the
288 LICENSE file included with this module.
289  
290 =cut