| 1 | package SDL::Tutorial::3DWorld::Actor::MaterialSampler; | ||
| 2 | |||
| 3 | =pod | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | SDL::Tutorial::3DWorld::Actor::MaterialSampler - A demonstrator for MTL files | ||
| 8 | |||
| 9 | =head1 DESCRIPTION | ||
| 10 | |||
| 11 | A B<MaterialSampler> is a grid of shapes designed to show off the properties | ||
| 12 | of different materials. | ||
| 13 | |||
| 14 | =cut | ||
| 15 | |||
| 16 | use 5.008; | ||
| 17 | use strict; | ||
| 18 | use warnings; | ||
| 19 | use OpenGL::List (); | ||
| 20 | use SDL::Tutorial::3DWorld::OpenGL (); | ||
| 21 | use SDL::Tutorial::3DWorld::Actor (); | ||
| 22 | use SDL::Tutorial::3DWorld::MTL (); | ||
| 23 | use SDL::Tutorial::3DWorld::Bound (); | ||
| 24 | |||
| 25 | our $VERSION = '0.33'; | ||
| 26 | our @ISA = 'SDL::Tutorial::3DWorld::Actor'; | ||
| 27 | |||
| 28 | sub new { | ||
| 29 | my $self = shift->SUPER::new(@_); | ||
| 30 | |||
| 31 | # Were we passed a MTL filename | ||
| 32 | unless ( -f $self->file ) { | ||
| 33 | die "Did not provide a MTL file"; | ||
| 34 | } | ||
| 35 | |||
| 36 | # Material samples might contain transparent materials that need blending | ||
| 37 | # $self->{blending} = 1; | ||
| 38 | |||
| 39 | # Scaling and rotation are not supported | ||
| 40 | if ( $self->{scale} ) { | ||
| 41 | die "MaterialSampler actors do not support scaling"; | ||
| 42 | } | ||
| 43 | if ( $self->{orient} ) { | ||
| 44 | die "MaterialSampler actors do not support rotation"; | ||
| 45 | } | ||
| 46 | |||
| 47 | return $self; | ||
| 48 | } | ||
| 49 | |||
| 50 | sub file { | ||
| 51 | $_[0]->{file}; | ||
| 52 | } | ||
| 53 | |||
| 54 | |||
| 55 | |||
| 56 | |||
| 57 | |||
| 58 | ###################################################################### | ||
| 59 | # Engine Interface | ||
| 60 | |||
| 61 | sub init { | ||
| 62 | my $self = shift; | ||
| 63 | |||
| 64 | # Load the material file | ||
| 65 | $self->{mtl} = SDL::Tutorial::3DWorld::MTL->new( | ||
| 66 | file => $self->{file}, | ||
| 67 | ) or die "Failed to load MTL file"; | ||
| 68 | |||
| 69 | # Initialise the material file | ||
| 70 | $self->{mtl}->init; | ||
| 71 | |||
| 72 | # Fully initialise all materials and set up positions | ||
| 73 | # for their material spheres. | ||
| 74 | my $i = 0; | ||
| 75 | $self->{mtlpos} = { }; | ||
| 76 | foreach my $name ( $self->{mtl}->names ) { | ||
| 77 | $self->{mtl}->material($name)->init; | ||
| 78 | $self->{mtlpos}->{$name} = [ | ||
| 79 | $self->{position}->[0] + ($i++ * 2), | ||
| 80 | $self->{position}->[1], | ||
| 81 | $self->{position}->[2], | ||
| 82 | ]; | ||
| 83 | } | ||
| 84 | |||
| 85 | # Define the boundary box if the object is static | ||
| 86 | unless ( $self->{velocity} ) { | ||
| 87 | my $L = 2 * (scalar $self->{mtl}->names - 1); | ||
| 88 | $self->{bound} = SDL::Tutorial::3DWorld::Bound->box( | ||
| 89 | -0.5, -0.5, -0.5, | ||
| 90 | 0.5 + $L, 0.5, 0.5, | ||
| 91 | ); | ||
| 92 | } | ||
| 93 | |||
| 94 | return 1; | ||
| 95 | } | ||
| 96 | |||
| 97 | sub display { | ||
| 98 | my $self = shift; | ||
| 99 | |||
| 100 | # Calculate the order we'll render the spheres. | ||
| 101 | # This ensures that anything transparent is blended properly. | ||
| 102 | my @names = $self->{mtl}->names; | ||
| 103 | my @order = SDL::Tutorial::3DWorld->current->camera->distance_isort( | ||
| 104 | map { $self->{mtlpos}->{$_} } @names | ||
| 105 | ); | ||
| 106 | |||
| 107 | # Iterate over the materials | ||
| 108 | foreach my $i ( @order ) { | ||
| 109 | my $name = $names[$i]; | ||
| 110 | $self->{mtl}->material($name)->display; | ||
| 111 | OpenGL::glPushMatrix(); | ||
| 112 | OpenGL::glTranslatef( @{$self->{mtlpos}->{$name}} ); | ||
| 113 | OpenGL::glutSolidSphere( 0.5, 50, 50 ); | ||
| 114 | OpenGL::glPopMatrix(); | ||
| 115 | } | ||
| 116 | |||
| 117 | return 1; | ||
| 118 | } | ||
| 119 | |||
| 120 | 1; | ||
| 121 | |||
| 122 | =pod | ||
| 123 | |||
| 124 | =head1 SUPPORT | ||
| 125 | |||
| 126 | Bugs should be reported via the CPAN bug tracker at | ||
| 127 | |||
| 128 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=SDL-Tutorial-3DWorld> | ||
| 129 | |||
| 130 | =head1 AUTHOR | ||
| 131 | |||
| 132 | Adam Kennedy E<lt>adamk@cpan.orgE<gt> | ||
| 133 | |||
| 134 | =head1 SEE ALSO | ||
| 135 | |||
| 136 | L<SDL>, L<OpenGL> | ||
| 137 | |||
| 138 | =head1 COPYRIGHT | ||
| 139 | |||
| 140 | Copyright 2010 Adam Kennedy. | ||
| 141 | |||
| 142 | This program is free software; you can redistribute | ||
| 143 | it and/or modify it under the same terms as Perl itself. | ||
| 144 | |||
| 145 | The full text of the license can be found in the | ||
| 146 | LICENSE file included with this module. | ||
| 147 | |||
| 148 | =cut |