| 1 | package SDL::Tutorial::3DWorld::Actor::Hedron; | ||
| 2 | |||
| 3 | use 5.008; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use SDL::Tutorial::3DWorld::Actor (); | ||
| 7 | use SDL::Tutorial::3DWorld::Model (); | ||
| 8 | use SDL::Tutorial::3DWorld::OpenGL (); | ||
| 9 | use OpenGL::List (); | ||
| 10 | |||
| 11 | our $VERSION = '0.33'; | ||
| 12 | our @ISA = 'SDL::Tutorial::3DWorld::Actor'; | ||
| 13 | |||
| 14 | |||
| 15 | |||
| 16 | |||
| 17 | |||
| 18 | ###################################################################### | ||
| 19 | # Constructors | ||
| 20 | |||
| 21 | sub new { | ||
| 22 | my $class = shift; | ||
| 23 | my $self = bless { @_ }, $class; | ||
| 24 | |||
| 25 | # Do we have the required params | ||
| 26 | unless ( $self->{vertex} ) { | ||
| 27 | die "Did not provide a vertex list"; | ||
| 28 | } | ||
| 29 | unless ( $self->{actor} ) { | ||
| 30 | die "Did not provide a child actor"; | ||
| 31 | } | ||
| 32 | |||
| 33 | # Scaling is a bit complicated for this model at the moment | ||
| 34 | if ( $self->{scale} ) { | ||
| 35 | die "Hedron actors do not support scaling"; | ||
| 36 | } | ||
| 37 | |||
| 38 | return $self; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub icosahedron { | ||
| 42 | my $class = shift; | ||
| 43 | my $self = bless { | ||
| 44 | vertex => [ | ||
| 45 | |||
| 46 | # These are subtly wrong but close enough | ||
| 47 | [ 0, 0 ], | ||
| 48 | [ 60, 36 ], | ||
| 49 | [ 60, 108 ], | ||
| 50 | [ 60, 180 ], | ||
| 51 | [ 60, 252 ], | ||
| 52 | [ 60, 324 ], | ||
| 53 | [ 120, 0 ], | ||
| 54 | [ 120, 72 ], | ||
| 55 | [ 120, 144 ], | ||
| 56 | [ 120, 216 ], | ||
| 57 | [ 120, 288 ], | ||
| 58 | [ 180, 0, ], | ||
| 59 | ], | ||
| 60 | @_, | ||
| 61 | }, $class; | ||
| 62 | |||
| 63 | # Do we have a child actor? | ||
| 64 | unless ( $self->{actor} ) { | ||
| 65 | die "Did not provide a child actor object"; | ||
| 66 | } | ||
| 67 | |||
| 68 | return $self; | ||
| 69 | } | ||
| 70 | |||
| 71 | |||
| 72 | |||
| 73 | |||
| 74 | |||
| 75 | ###################################################################### | ||
| 76 | # Engine Methods | ||
| 77 | |||
| 78 | sub init { | ||
| 79 | my $self = shift; | ||
| 80 | |||
| 81 | # Initialise the child | ||
| 82 | $self->{actor}->init; | ||
| 83 | |||
| 84 | # If the child can be fully compiled, compile the entire | ||
| 85 | # hedron down to a single display list as well. | ||
| 86 | if ( $self->{actor}->{display} and not $self->{velocity} ) { | ||
| 87 | $self->{display} = OpenGL::List::glpList { | ||
| 88 | OpenGL::glPushMatrix(); | ||
| 89 | $self->display; | ||
| 90 | OpenGL::glPopMatrix(); | ||
| 91 | }; | ||
| 92 | } | ||
| 93 | |||
| 94 | return 1; | ||
| 95 | } | ||
| 96 | |||
| 97 | sub display { | ||
| 98 | my $self = shift; | ||
| 99 | $self->SUPER::display(@_); | ||
| 100 | |||
| 101 | # Rotate to each of the vertice in the hedron and | ||
| 102 | # draw the child actor. | ||
| 103 | foreach my $rotate ( @{$self->{vertex}} ) { | ||
| 104 | # Draw the actor at the offset angle | ||
| 105 | OpenGL::glPushMatrix(); | ||
| 106 | OpenGL::glRotatef( $rotate->[1], 0, 1, 0 ); | ||
| 107 | OpenGL::glRotatef( $rotate->[0], 1, 0, 0 ); | ||
| 108 | $self->{actor}->display; | ||
| 109 | OpenGL::glPopMatrix(); | ||
| 110 | } | ||
| 111 | |||
| 112 | return 1; | ||
| 113 | } | ||
| 114 | |||
| 115 | 1; |