| 1 | package SDL::Tutorial::3DWorld::Actor::TronBit; | ||
| 2 | |||
| 3 | |||
| 4 | =pod | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | SDL::Tutorial::3DWorld::Actor::TronBit - An attempt to create "Bit" from Tron | ||
| 9 | |||
| 10 | =head1 DESCRIPTION | ||
| 11 | |||
| 12 | An attempt to make a complex actor, the "bit" character from tron. | ||
| 13 | |||
| 14 | Contains multiple sub-models, continually morphing and moving as a whole. | ||
| 15 | |||
| 16 | B<THIS CLASS DOES NOT WORK, AND ACTS ONLY AS A PLACEHOLDER FOR FUTURE WORK> | ||
| 17 | |||
| 18 | =cut | ||
| 19 | |||
| 20 | use 5.008; | ||
| 21 | use strict; | ||
| 22 | use warnings; | ||
| 23 | use OpenGL (); | ||
| 24 | use SDL::Tutorial::3DWorld::OpenGL (); | ||
| 25 | use SDL::Tutorial::3DWorld::Actor (); | ||
| 26 | use SDL::Tutorial::3DWorld::Material (); | ||
| 27 | |||
| 28 | our $VERSION = '0.33'; | ||
| 29 | our @ISA = 'SDL::Tutorial::3DWorld::Actor'; | ||
| 30 | |||
| 31 | use constant D2R => CORE::atan2(1,1) / 45; | ||
| 32 | |||
| 33 | |||
| 34 | |||
| 35 | |||
| 36 | |||
| 37 | ###################################################################### | ||
| 38 | # Constructor | ||
| 39 | |||
| 40 | sub new { | ||
| 41 | my $self = shift->SUPER::new( | ||
| 42 | # orient => [ 0.0, 0.0, 1.0, 0.0 ], | ||
| 43 | # rotate => 1, | ||
| 44 | @_, | ||
| 45 | ); | ||
| 46 | |||
| 47 | # Force a zero velocity so we get the move call | ||
| 48 | $self->{velocity} ||= [ 0, 0, 0 ]; | ||
| 49 | |||
| 50 | # "No" is a harsh metallic 27th icosahedron | ||
| 51 | $self->{no_cycle} = 0; # Position in the "No" cycle if active | ||
| 52 | $self->{no_duration} = 0.5; # Total duration of the "No" cycle | ||
| 53 | $self->{no_material} = SDL::Tutorial::3DWorld::Material->new( | ||
| 54 | shininess => 120, | ||
| 55 | specular => [ 1.0, 1.0, 1.0, 1.0 ], | ||
| 56 | diffuse => [ 0.5921, 0.0167, 0.0000, 1.0 ], | ||
| 57 | ambient => [ 0.1985, 0.0000, 0.0000, 1.0 ], | ||
| 58 | ); | ||
| 59 | |||
| 60 | # "Yes" is a flat yellow tetrahedron | ||
| 61 | $self->{yes_cycle} = 0; # Position in the "Yes" cycle if active | ||
| 62 | $self->{yes_duration} = 0.5; # Total duration of the "Yes" cycle | ||
| 63 | $self->{yes_material} = SDL::Tutorial::3DWorld::Material->new( | ||
| 64 | # shininess => 100.2235, | ||
| 65 | # specular => [ 1.0, 1.0, 1.0, 1.0 ], | ||
| 66 | diffuse => [ 0.0167, 0.5921, 0.0000, 1.0 ], | ||
| 67 | ambient => [ 0.0000, 0.1985, 0.0000, 1.0 ], | ||
| 68 | ); | ||
| 69 | |||
| 70 | # The main body is a slowly rotating light blue metallic icosahedron | ||
| 71 | $self->{null_angle} = 0; | ||
| 72 | $self->{null_speed} = 1; | ||
| 73 | $self->{null_material} = SDL::Tutorial::3DWorld::Material->new( | ||
| 74 | shininess => 120, | ||
| 75 | specular => [ 1.0, 1.0, 1.0, 1.0 ], | ||
| 76 | diffuse => [ 0.7, 0.7, 1.0, 1.0 ], | ||
| 77 | ambient => [ 0.3, 0.3, 1.0, 1.0 ], | ||
| 78 | ); | ||
| 79 | |||
| 80 | return $self; | ||
| 81 | } | ||
| 82 | |||
| 83 | |||
| 84 | |||
| 85 | |||
| 86 | |||
| 87 | ###################################################################### | ||
| 88 | # External Methods | ||
| 89 | |||
| 90 | sub yes { | ||
| 91 | my $self = shift; | ||
| 92 | |||
| 93 | # Trigger the "Yes" cycle | ||
| 94 | $self->{yes_cycle} = 0.1; | ||
| 95 | $self->{no_cycle} = 0; | ||
| 96 | |||
| 97 | return 1; | ||
| 98 | } | ||
| 99 | |||
| 100 | sub no { | ||
| 101 | my $self = shift; | ||
| 102 | |||
| 103 | # Trigger the "No" cycle | ||
| 104 | $self->{no_cycle} = 0.1; | ||
| 105 | $self->{yes_cycle} = 0; | ||
| 106 | |||
| 107 | return 1; | ||
| 108 | } | ||
| 109 | |||
| 110 | |||
| 111 | |||
| 112 | |||
| 113 | |||
| 114 | ###################################################################### | ||
| 115 | # Engine Methods | ||
| 116 | |||
| 117 | sub init { | ||
| 118 | my $self = shift; | ||
| 119 | |||
| 120 | # Initialise the materials | ||
| 121 | $self->{no_material}->init; | ||
| 122 | $self->{yes_material}->init; | ||
| 123 | $self->{null_material}->init; | ||
| 124 | |||
| 125 | return 1; | ||
| 126 | } | ||
| 127 | |||
| 128 | sub move { | ||
| 129 | my $self = shift; | ||
| 130 | my $step = $_[0]; | ||
| 131 | $self->SUPER::move(@_); | ||
| 132 | |||
| 133 | # Rotate the rest body angle (fairly quickly) | ||
| 134 | $self->{null_angle} = ( | ||
| 135 | $self->{null_angle} + $self->{null_speed} * $step | ||
| 136 | ) % 360; | ||
| 137 | |||
| 138 | # If we are in the "No" or "Yes" cycle advance and (maybe) end them | ||
| 139 | if ( $self->{no_cycle} ) { | ||
| 140 | $self->{no_cycle} += ($step * 36 / $self->{no_duration}); | ||
| 141 | $self->{no_cycle} = 0 if $self->{no_cycle} >= 360; | ||
| 142 | } | ||
| 143 | if ( $self->{yes_cycle} ) { | ||
| 144 | $self->{yes_cycle} += ($step * 36 / $self->{yes_duration}); | ||
| 145 | $self->{yes_cycle} = 0 if $self->{yes_cycle} >= 360; | ||
| 146 | } | ||
| 147 | |||
| 148 | return 1; | ||
| 149 | } | ||
| 150 | |||
| 151 | sub display { | ||
| 152 | my $self = shift; | ||
| 153 | |||
| 154 | # Do general transforms for the overall character | ||
| 155 | $self->SUPER::display(@_); | ||
| 156 | |||
| 157 | # Most parts of bit's body change in size, | ||
| 158 | # so enable normalisation to avoid surface normal problems. | ||
| 159 | OpenGL::glEnable( OpenGL::GL_NORMALIZE ); | ||
| 160 | |||
| 161 | # Set the size of the overall actor via a matric, | ||
| 162 | # because the glut constructor commands only build at one size. | ||
| 163 | ### NOTE: Replace with proper size later. | ||
| 164 | OpenGL::glScalef( 0.3, 0.3, 0.3 ); | ||
| 165 | |||
| 166 | # The size of the null rest body, which moves along | ||
| 167 | # a "1.01 - sin" curve (the 0.01 avoids divide by zero). | ||
| 168 | my $null = 1; | ||
| 169 | |||
| 170 | if ( $self->{yes_cycle} ) { | ||
| 171 | # The size of "Yes" moves on a 180 degree sin curve | ||
| 172 | my $yes = sin($self->{yes_cycle} * D2R / 2); | ||
| 173 | $null = (1 - $yes) ** 2 || 0.01; | ||
| 174 | |||
| 175 | # Draw the "Yes" octahedron, which never rotates. | ||
| 176 | $self->{yes_material}->display; | ||
| 177 | OpenGL::glPushMatrix(); | ||
| 178 | OpenGL::glScalef( $yes * 1.5, $yes * 1.5, $yes * 1.5 ); | ||
| 179 | OpenGL::glutSolidOctahedron(); | ||
| 180 | OpenGL::glPopMatrix(); | ||
| 181 | } | ||
| 182 | |||
| 183 | if ( $self->{no_cycle} ) { | ||
| 184 | # The size of "No" moves on a 180 degree sin curve | ||
| 185 | my $no = sin($self->{no_cycle} * D2R / 2); | ||
| 186 | $null = (1 - $no) ** 2 || 0.01; | ||
| 187 | |||
| 188 | # Draw the "No" 27th icosahedron thingy, which never rotates. | ||
| 189 | $self->{no_material}->display; | ||
| 190 | OpenGL::glPushMatrix(); | ||
| 191 | OpenGL::glScalef( $no * 1.5, $no * 1.5, $no * 1.5 ); | ||
| 192 | OpenGL::glutSolidTetrahedron(); | ||
| 193 | OpenGL::glPopMatrix(); | ||
| 194 | } | ||
| 195 | |||
| 196 | # The rest of the body elements in the null state rotate | ||
| 197 | $self->{null_material}->display; | ||
| 198 | OpenGL::glScalef( $null, $null, $null ) unless $null == 1; | ||
| 199 | # OpenGL::glRotatef( $self->{null_angle}, 0, 1, 0 ); | ||
| 200 | |||
| 201 | # Show the icosahedron | ||
| 202 | OpenGL::glPushMatrix(); | ||
| 203 | OpenGL::glRotatef( $self->{null_angle}, 1, 0, 0 ); | ||
| 204 | OpenGL::glutSolidIcosahedron(); | ||
| 205 | OpenGL::glPopMatrix(); | ||
| 206 | |||
| 207 | # Show the dodecahedron | ||
| 208 | OpenGL::glPushMatrix(); | ||
| 209 | OpenGL::glScalef( 0.55, 0.55, 0.55 ); | ||
| 210 | OpenGL::glutSolidDodecahedron(); | ||
| 211 | OpenGL::glPopMatrix(); | ||
| 212 | |||
| 213 | # Done with these resizing games | ||
| 214 | OpenGL::glDisable( OpenGL::GL_NORMALIZE ); | ||
| 215 | |||
| 216 | return 1; | ||
| 217 | } | ||
| 218 | |||
| 219 | 1; | ||
| 220 | |||
| 221 | =pod | ||
| 222 | |||
| 223 | =head1 SUPPORT | ||
| 224 | |||
| 225 | Bugs should be reported via the CPAN bug tracker at | ||
| 226 | |||
| 227 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=SDL-Tutorial-3DWorld> | ||
| 228 | |||
| 229 | =head1 AUTHOR | ||
| 230 | |||
| 231 | Adam Kennedy E<lt>adamk@cpan.orgE<gt> | ||
| 232 | |||
| 233 | =head1 SEE ALSO | ||
| 234 | |||
| 235 | L<SDL>, L<OpenGL> | ||
| 236 | |||
| 237 | =head1 COPYRIGHT | ||
| 238 | |||
| 239 | Copyright 2010 Adam Kennedy. | ||
| 240 | |||
| 241 | This program is free software; you can redistribute | ||
| 242 | it and/or modify it under the same terms as Perl itself. | ||
| 243 | |||
| 244 | The full text of the license can be found in the | ||
| 245 | LICENSE file included with this module. | ||
| 246 | |||
| 247 | =cut |