3DWorld/Light.pm untested

filter: Statementstmt 0.0% 0 / 54 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 95 CC 3 SCAR 13.3
CC
3
Cov
0%
new
12.0
X
2.0
Y
2.0
CRAP 3.8
1 package SDL::Tutorial::3DWorld::Light;
2  
3 =pod
4  
5 =head1 NAME
6  
7 SDL::Tutorial::3DWorld::Light - A light source in the 3D world
8  
9 =head1 SYNOPSIS
10  
11 # An overhead light at a nice room ceiling height
12 my $light = SDL::Tutorial::3DWorld::Light->new(
13 X => 0,
14 Y => 3.5,
15 Z => 0,
16 );
17  
18 =head1 DESCRIPTION
19  
20 Other than in very simple worlds with intentionally flat colour, most game
21 worlds are lit by light sources.
22  
23 Lights may sometimes be seen as part of the landscape in the sense they
24 are fixed and generally places to best highlight the game world. They can
25 also sometimes be seen as actors, for example when a light source is used
26 as a weapon special effect.
27  
28 However, for the purpose of this tutorial we will treat lights as first
29 class entities in their own right.
30  
31 For the purpose of the tutorial only the position of the lighting can be
32 controlled. All lights will have a fixed colour, intensity, and diffusion.
33  
34 =head1 METHODS
35  
36 =cut
37  
38 use 5.008;
39 use strict;
40 use warnings;
41 use SDL::Tutorial::3DWorld::OpenGL ();
42 use OpenGL::List ();
43  
44 our $VERSION = '0.33';
45  
46 =pod
47  
48 =head2 new
49  
50 # A light in the sky at noon
51 my $sun = SDL::Tutorial::3DWorld::Light->new(
52 X => 0,
53 Y => 10000,
54 Z => 0,
55 );
56  
57 The C<new> constructor creates a new light source in the 3D world.
58  
59 In the initial demonstration, only the position of the light is controllable
60 and the nature of the light source (colour, diffusion etc) is fixed.
61  
62 =cut
63  
64 sub new {
65 my $class = shift;
66 my $self = bless { @_ }, $class;
67  
68 # Defaults and clean up
69 $self->{position} ||= [ 0.0, 0.0, 0.0, 1 ];
70 $self->{ambient} ||= [ 0.2, 0.2, 0.2, 1.0 ];
71 $self->{diffuse} ||= [ 1.0, 1.0, 1.0, 1.0 ];
72 $self->{specular} ||= [ 1.0, 1.0, 1.0, 1.0 ];
73 if ( @{$self->{position}} == 3 ) {
74 # Add the weird unused value to comply with the function
75 $self->{position}->[3] = 1;
76 }
77  
78 # OpenGL light operations are special and need special light ids
79 unless ( defined $self->{id} ) {
80 # NOTE: Yes this is stupid and will break for more than one
81 # light, but I'll smarten it up later.
82 $self->{id} = OpenGL::GL_LIGHT0;
83 }
84  
85 return $self;
86 }
87  
88 =pod
89  
90 =head2 X
91  
92 The C<X> accessor provides the location of the actor in metres on the east
93 to west dimension within the 3D world. The positive direction is east.
94  
95 =cut
96  
97 sub X {
98 $_[0]->{position}->{X};
99 }
100  
101 =pod
102  
103 =head2 Y
104  
105 The C<Y> accessor is location of the actor in metres on the vertical
106 dimension within the 3D world. The positive direction is up.
107  
108 =cut
109  
110 sub Y {
111 $_[0]->{position}->{Y};
112 }
113  
114 =pod
115  
116 =head2 Z
117  
118 The C<Z> accessor provides the actor of the camera in metres on the north
119 to south dimension within the 3D world. The positive direction is north.
120  
121 =cut
122  
123 sub Z {
124 $_[0]->{position}->{Z};
125 }
126  
127  
128  
129  
130  
131 ######################################################################
132 # Light Properties
133  
134 sub position {
135 @{$_[0]->{position}};
136 }
137  
138 sub ambient {
139 @{$_[0]->{ambient}};
140 }
141  
142 sub diffuse {
143 @{$_[0]->{diffuse}};
144 }
145  
146 sub specular {
147 @{$_[0]->{specular}};
148 }
149  
150  
151  
152  
153  
154 ######################################################################
155 # Engine Interface
156  
157 # Initialise
158 sub init { 1 }
159  
160 # Light settings do not appear to work inside of a display list.
161 # So we need to always do this by hand.
162 sub display {
163 my $self = shift;
164  
165 # Define the light
166 OpenGL::glEnable( $self->{id} );
167 OpenGL::glLightfv_p(
168 $self->{id},
169 OpenGL::GL_POSITION,
170 @{$self->{position}},
171 );
172 OpenGL::glLightfv_p(
173 $self->{id},
174 OpenGL::GL_AMBIENT,
175 @{$self->{ambient}},
176 );
177 OpenGL::glLightfv_p(
178 $self->{id},
179 OpenGL::GL_DIFFUSE,
180 @{$self->{diffuse}},
181 );
182 OpenGL::glLightfv_p(
183 $self->{id},
184 OpenGL::GL_SPECULAR,
185 @{$self->{specular}},
186 );
187 }
188  
189 =pod
190  
191 =head1 SUPPORT
192  
193 Bugs should be reported via the CPAN bug tracker at
194  
195 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=SDL-Tutorial-3DWorld>
196  
197 =head1 AUTHOR
198  
199 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
200  
201 =head1 SEE ALSO
202  
203 L<SDL>, L<OpenGL>
204  
205 =head1 COPYRIGHT
206  
207 Copyright 2010 Adam Kennedy.
208  
209 This program is free software; you can redistribute
210 it and/or modify it under the same terms as Perl itself.
211  
212 The full text of the license can be found in the
213 LICENSE file included with this module.
214  
215 =cut