| 1 | package SDL::Tutorial::3DWorld::Actor::Billboard; | ||
| 2 | |||
| 3 | use 5.008; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use SDL::Tutorial::3DWorld (); | ||
| 7 | use SDL::Tutorial::3DWorld::OpenGL (); | ||
| 8 | use SDL::Tutorial::3DWorld::Texture (); | ||
| 9 | use SDL::Tutorial::3DWorld::Material (); | ||
| 10 | use SDL::Tutorial::3DWorld::Actor (); | ||
| 11 | |||
| 12 | our $VERSION = '0.33'; | ||
| 13 | our @ISA = 'SDL::Tutorial::3DWorld::Actor'; | ||
| 14 | |||
| 15 | |||
| 16 | |||
| 17 | |||
| 18 | |||
| 19 | ###################################################################### | ||
| 20 | # Constructor and Accessors | ||
| 21 | |||
| 22 | sub new { | ||
| 23 | my $self = shift->SUPER::new( | ||
| 24 | blending => 1, | ||
| 25 | @_, | ||
| 26 | ); | ||
| 27 | |||
| 28 | # Convert the texture to a full material | ||
| 29 | $self->{material} = SDL::Tutorial::3DWorld::Material->new( | ||
| 30 | ambient => [ 1, 1, 1, 0.5 ], | ||
| 31 | diffuse => [ 1, 1, 1, 0.5 ], | ||
| 32 | texture => SDL::Tutorial::3DWorld::Texture->new( | ||
| 33 | file => $self->{texture}, | ||
| 34 | tile => 0, | ||
| 35 | ), | ||
| 36 | ); | ||
| 37 | |||
| 38 | return $self; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub display { | ||
| 42 | my $self = shift; | ||
| 43 | $self->SUPER::display(@_); | ||
| 44 | |||
| 45 | # Rotate towards the camera | ||
| 46 | my $camera = SDL::Tutorial::3DWorld->current->camera; | ||
| 47 | OpenGL::glRotatef( -$camera->{angle}, 0, 1, 0 ); | ||
| 48 | OpenGL::glRotatef( -$camera->{elevation}, -1, 0, 0 ); | ||
| 49 | |||
| 50 | # Switch to the sprite | ||
| 51 | $self->{material}->display; | ||
| 52 | |||
| 53 | # Draw the sprite quad. | ||
| 54 | # The texture seems to wrap a little unless we use the 0.01 here. | ||
| 55 | OpenGL::glDisable( OpenGL::GL_LIGHTING ); | ||
| 56 | OpenGL::glBegin( OpenGL::GL_QUADS ); | ||
| 57 | OpenGL::glTexCoord2f( 0, 0 ); OpenGL::glVertex3f( -0.5, 1, 0 ); # Top Left | ||
| 58 | OpenGL::glTexCoord2f( 0, 1 ); OpenGL::glVertex3f( -0.5, 0, 0 ); # Bottom Left | ||
| 59 | OpenGL::glTexCoord2f( 1, 1 ); OpenGL::glVertex3f( 0.5, 0, 0 ); # Bottom Right | ||
| 60 | OpenGL::glTexCoord2f( 1, 0 ); OpenGL::glVertex3f( 0.5, 1, 0 ); # Top Right | ||
| 61 | OpenGL::glEnd(); | ||
| 62 | OpenGL::glEnable( OpenGL::GL_LIGHTING ); | ||
| 63 | |||
| 64 | return 1; | ||
| 65 | } | ||
| 66 | |||
| 67 | 1; |