3DWorld/Material.pm untested

filter: Statementstmt 0.0% 0 / 122 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 219 CC 21 SCAR 30.2
CC
21
Cov
0%
new
56.0
set_ambient
30.0
set_diffuse
30.0
CRAP 20.4
1 package SDL::Tutorial::3DWorld::Material;
2  
3 use 5.008;
4 use strict;
5 use warnings;
6 use SDL::Tutorial::3DWorld::OpenGL ();
7  
8 our $VERSION = '0.33';
9  
10 # Convert GL fake "constants" to real constants.
11 # If we don't do this we spend a third of our CPU calling constants.
12 use constant {
13 GL_AMBIENT => OpenGL::GL_AMBIENT,
14 GL_BLEND => OpenGL::GL_BLEND,
15 GL_DIFFUSE => OpenGL::GL_DIFFUSE,
16 GL_FRONT => OpenGL::GL_FRONT,
17 GL_LIGHTING => OpenGL::GL_LIGHTING,
18 GL_ONE_MINUS_SRC_ALPHA => OpenGL::GL_ONE_MINUS_SRC_ALPHA,
19 GL_SHININESS => OpenGL::GL_SHININESS,
20 GL_SRC_ALPHA => OpenGL::GL_SRC_ALPHA,
21 GL_SPECULAR => OpenGL::GL_SPECULAR,
22 GL_TEXTURE_2D => OpenGL::GL_TEXTURE_2D,
23 };
24  
25  
26  
27  
28  
29 ######################################################################
30 # Constructor and Accessors
31  
32 sub new {
33 my $class = shift;
34 my $self = bless {
35 # Default elements
36 color => [ 1.0, 1.0, 1.0 ],
37 ambient => [ 0.2, 0.2, 0.2, 1.0 ],
38 diffuse => [ 0.8, 0.8, 0.8, 1.0 ],
39 specular => 0,
40 dissolve => 1.0,
41 shininess => 127,
42 texture => undef,
43 illumination => 2,
44 @_,
45 }, $class;
46  
47 # If our colours are only 3 element, apply the dissolve
48 if ( @{$self->{ambient}} == 3 ) {
49 $self->{ambient}->[3] = $self->{dissolve};
50 }
51 if ( @{$self->{diffuse}} == 3 ) {
52 $self->{diffuse}->[3] = $self->{dissolve};
53 }
54 if ( $self->{specular} and @{$self->{specular}} == 3 ) {
55 $self->{specular}->[3] = $self->{dissolve};
56 }
57  
58 # Inflate texture filenames to texture objects
59 if ( $self->{texture} and not ref $self->{texture} ) {
60 $self->{texture} = SDL::Tutorial::3DWorld::Texture->new(
61 file => $self->{texture},
62 );
63 }
64  
65 # Enable the blending flag if needed
66 if ( $self->{ambient}->[3] < 1 ) {
67 $self->{blending} = 1;
68 } elsif ( $self->{diffuse}->[3] < 1 ) {
69 $self->{blending} = 1;
70 }
71  
72 return $self;
73 }
74  
75 sub clone {
76 my $self = shift;
77  
78 # Basic shallow clone
79 my $class = ref $self;
80 my $clone = bless { %$self }, $class;
81  
82 # Deep clone multi-element colours
83 $clone->{ambient} = [ @{$clone->{ambient}} ];
84 $clone->{diffuse} = [ @{$clone->{diffuse}} ];
85 $clone->{specular} = [ @{$clone->{specular}} ] if $clone->{specular};
86  
87 return $clone;
88 }
89  
90 sub color {
91 $_[0]->{color};
92 }
93  
94 sub ambient {
95 $_[0]->{ambient};
96 }
97  
98 sub diffuse {
99 $_[0]->{diffuse};
100 }
101  
102 sub specular {
103 $_[0]->{specular};
104 }
105  
106 sub shininess {
107 $_[0]->{shininess};
108 }
109  
110 sub texture {
111 $_[0]->{texture};
112 }
113  
114 # Illumination mode
115 # 0. Color on and Ambient off
116 # 1. Color on and Ambient on
117 # 2. Highlight on
118 # 3. Reflection on and Ray trace on
119 # 4. Transparency: Glass on, Reflection: Ray trace on
120 # 5. Reflection: Fresnel on and Ray trace on
121 # 6. Transparency: Refraction on, Reflection: Fresnel off and Ray trace on
122 # 7. Transparency: Refraction on, Reflection: Fresnel on and Ray trace on
123 # 8. Reflection on and Ray trace off
124 # 9. Transparency: Glass on, Reflection: Ray trace off
125 # 10. Casts shadows onto invisible surfaces
126 sub illumination {
127 $_[0]->{illumination};
128 }
129  
130  
131  
132  
133  
134 ######################################################################
135 # Mutators
136  
137 sub set_color {
138 my $self = shift;
139 $self->{color} = shift;
140 }
141  
142 sub set_texture {
143 my $self = shift;
144 $self->{texture} = shift;
145 }
146  
147 sub set_ambient {
148 my $self = shift;
149 my @ambient = @_;
150  
151 # One-value ambient is defined as a multiple of the current colour
152 if ( @ambient == 1 and ref $ambient[0] ) {
153 @ambient = @{$ambient[0]};
154 }
155 if ( @ambient == 1 ) {
156 @ambient = (
157 $self->{color}->[0] * $ambient[0],
158 $self->{color}->[1] * $ambient[0],
159 $self->{color}->[2] * $ambient[0],
160 );
161 }
162  
163 # Three-value ambient is a material property without an alpha value
164 if ( @ambient == 3 ) {
165 push @ambient, $self->{ambient}->[3];
166 }
167  
168 # Four-value ambient is a full material property and replaces the
169 # current value directly.
170 if ( @ambient == 4 ) {
171 $self->{ambient} = \@ambient;
172 return 1;
173 }
174  
175 die "Unknown or unsupported ambient color definition";
176 }
177  
178 sub set_diffuse {
179 my $self = shift;
180 my @diffuse = @_;
181  
182 # One-value diffuse is defined as a multiple of the current colour
183 if ( @diffuse == 1 and ref $diffuse[0] ) {
184 @diffuse = @{$diffuse[0]};
185 }
186 if ( @diffuse == 1 ) {
187 @diffuse = (
188 $self->{color}->[0] * $diffuse[0],
189 $self->{color}->[1] * $diffuse[0],
190 $self->{color}->[2] * $diffuse[0],
191 );
192 }
193  
194 # Three-value diffuse is a material property without an alpha value
195 if ( @diffuse == 3 ) {
196 push @diffuse, $self->{diffuse}->[3];
197 }
198  
199 # Four-value diffuse is a full material property and replaces the
200 # current value directly.
201 if ( @diffuse == 4 ) {
202 $self->{diffuse} = \@diffuse;
203 return 1;
204 }
205  
206 die "Unknown or unsupported diffuse color definition";
207 }
208  
209 sub set_opacity {
210 my $self = shift;
211 my $opacity = shift;
212  
213 # Opacity effects the ambient and diffuse values only
214 $self->{ambient}->[3] = 1 - $opacity;
215 $self->{diffuse}->[3] = 1 - $opacity;
216  
217 # Turn on the blending flag if needed
218 if ( $opacity < 1 ) {
219 $self->{blending} = 1;
220 }
221  
222 return 1;
223 }
224  
225  
226  
227  
228  
229 ######################################################################
230 # Engine Methods
231  
232 sub init {
233 my $self = shift;
234 if ( $self->{texture} ) {
235 $self->{texture}->init;
236 }
237 return 1;
238 }
239  
240 # Apply the material to the current OpenGL context
241 sub display {
242 my $self = shift;
243  
244 # Enable or disable blending if the material is not entirely solid
245 if ( $self->{blending} ) {
246 OpenGL::glEnable( GL_BLEND );
247 OpenGL::glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
248 } else {
249 OpenGL::glDisable( GL_BLEND );
250 }
251  
252 # Apply the material properties
253 OpenGL::glEnable( GL_LIGHTING );
254 if ( $self->{texture} ) {
255 OpenGL::glEnable( GL_TEXTURE_2D );
256 OpenGL::glColor3f( @{$self->{color}} );
257 $self->{texture}->display;
258 } else {
259 OpenGL::glDisable( GL_TEXTURE_2D );
260 OpenGL::glColor3f( 0, 0, 0 );
261 }
262 OpenGL::glMaterialfv_p(
263 GL_FRONT,
264 GL_AMBIENT,
265 @{ $self->{ambient} },
266 );
267 OpenGL::glMaterialfv_p(
268 GL_FRONT,
269 GL_DIFFUSE,
270 @{ $self->{diffuse} },
271 );
272 if ( $self->{specular} ) {
273 OpenGL::glMaterialfv_p(
274 GL_FRONT,
275 GL_SPECULAR,
276 @{ $self->{specular} },
277 );
278 OpenGL::glMaterialf(
279 GL_FRONT,
280 GL_SHININESS,
281 $self->{shininess},
282 );
283 } else {
284 OpenGL::glMaterialfv_p(
285 GL_FRONT,
286 GL_SPECULAR,
287 0, 0, 0, 1,
288 );
289 OpenGL::glMaterialf(
290 GL_FRONT,
291 GL_SHININESS,
292 100.23,
293 );
294 }
295  
296 return 1;
297 }
298  
299 1;