| 1 | package SDL::Tutorial::3DWorld::Actor::TV; | ||
| 2 | |||
| 3 | # Attempt to implement a "Television Screen" that can | ||
| 4 | # play MPEG streams, suitable for incorporation into more | ||
| 5 | # complex video-device models. | ||
| 6 | |||
| 7 | use 5.008; | ||
| 8 | use strict; | ||
| 9 | use warnings; | ||
| 10 | use SDL; | ||
| 11 | use SDL::SMPEG; | ||
| 12 | use SDL::Surface; | ||
| 13 | use SDL::Tutorial::3DWorld::OpenGL (); | ||
| 14 | use SDL::Tutorial::3DWorld::Actor (); | ||
| 15 | |||
| 16 | our $VERSION = '0.33'; | ||
| 17 | our @ISA = 'SDL::Tutorial::3DWorld::Actor'; | ||
| 18 | |||
| 19 | |||
| 20 | |||
| 21 | |||
| 22 | |||
| 23 | ###################################################################### | ||
| 24 | # Constructor | ||
| 25 | |||
| 26 | sub new { | ||
| 27 | my $self = shift->SUPER::new(@_); | ||
| 28 | |||
| 29 | # Do we have a video file? | ||
| 30 | unless ( $self->{file} ) { | ||
| 31 | die "Did not provide a video file"; | ||
| 32 | } | ||
| 33 | |||
| 34 | return $self; | ||
| 35 | } | ||
| 36 | |||
| 37 | |||
| 38 | |||
| 39 | |||
| 40 | |||
| 41 | ###################################################################### | ||
| 42 | # Engine Methods | ||
| 43 | |||
| 44 | sub init { | ||
| 45 | my $self = shift; | ||
| 46 | |||
| 47 | # Create the SDL surface for the video. We'll be keeping this | ||
| 48 | # permanently so we can shuttle video frames through it. | ||
| 49 | $self->{surface} = SDL::Surface->new( | ||
| 50 | $self->{height}, $self->{width}, 24, | ||
| 51 | 0, 0, 0, 0 | ||
| 52 | ); | ||
| 53 | |||
| 54 | # Load the video stream | ||
| 55 | $self->{smpeg} = SDL::SMPEG->new( | ||
| 56 | -name => $self->{file}, | ||
| 57 | -audio => 0, | ||
| 58 | ) or die "Failed to load MPEG stream from '$self->{file}'"; | ||
| 59 | |||
| 60 | # Capture some useful metadata about the stream | ||
| 61 | my $info = $self->{smpeg}->info or die "SMPEG->info failed"; | ||
| 62 | $self->{height} = $info->height; | ||
| 63 | $self->{width} = $info->width; | ||
| 64 | |||
| 65 | # Scale the TV size to the aspect ratio of the video | ||
| 66 | $self->{aspect} = $self->{width} / $self->{height}; | ||
| 67 | $self->{scale} = [ $self->{aspect}, 1, 1 ]; | ||
| 68 | |||
| 69 | # Bind the video to the surface | ||
| 70 | $self->{smpeg}->display( $self->{surface} ); | ||
| 71 | |||
| 72 | # Render the first frame of the video to the screen | ||
| 73 | $self->{smpeg}->frame(1); # Should this be zero? | ||
| 74 | |||
| 75 | # Generate the GL texture values | ||
| 76 | my $format = $self->{surface}->format; | ||
| 77 | $self->{bytes} = $format->BytesPerPixel; | ||
| 78 | if ( $self->{bytes} == 4 ) { | ||
| 79 | # Contains an alpha channel | ||
| 80 | if ( $format->Rmask == 0x000000ff ) { | ||
| 81 | $self->{mask} = OpenGL::GL_RGBA; | ||
| 82 | } else { | ||
| 83 | $self->{mask} = OpenGL::GL_BGRA; | ||
| 84 | } | ||
| 85 | } elsif ( $self->{bytes} == 3 ) { | ||
| 86 | # Does not contain an alpha channel | ||
| 87 | if ( $format->Rmask == 0x000000ff ) { | ||
| 88 | $self->{mask} = OpenGL::GL_RGB; | ||
| 89 | } else { | ||
| 90 | $self->{mask} = OpenGL::GL_BGR; | ||
| 91 | } | ||
| 92 | } else { | ||
| 93 | die "Unknown or unsupported video '$self->{file}'"; | ||
| 94 | } | ||
| 95 | |||
| 96 | # Have OpenGL generate one texture object handle. | ||
| 97 | $self->{id} = OpenGL::glGenTextures_p(1); | ||
| 98 | |||
| 99 | # Bind the texture object for the first time, activating it | ||
| 100 | # as the "current" texture and confirming it as 2 dimensional. | ||
| 101 | OpenGL::glBindTexture( OpenGL::GL_TEXTURE_2D, $self->{id} ); | ||
| 102 | |||
| 103 | # Specify how the texture will display when we are far from the | ||
| 104 | # texture and many texture pixels are inside one display pixel. | ||
| 105 | OpenGL::glTexParameterf( | ||
| 106 | OpenGL::GL_TEXTURE_2D, | ||
| 107 | OpenGL::GL_TEXTURE_MIN_FILTER, | ||
| 108 | OpenGL::GL_NEAREST, | ||
| 109 | ); | ||
| 110 | |||
| 111 | # Specify the zoom method to use when we are too close to the | ||
| 112 | # texture and one texture pixel spreads over many display pixels. | ||
| 113 | OpenGL::glTexParameterf( | ||
| 114 | OpenGL::GL_TEXTURE_2D, | ||
| 115 | OpenGL::GL_TEXTURE_MAG_FILTER, | ||
| 116 | OpenGL::GL_NEAREST, | ||
| 117 | ); | ||
| 118 | |||
| 119 | # Lock the surface so the frame holds still in memory | ||
| 120 | SDL::Video::lock_surface($self->{surface}); | ||
| 121 | |||
| 122 | # Write the image data into the texture, | ||
| 123 | # generating mipmaps as we go (is this too expensive?) | ||
| 124 | OpenGL::gluBuild2DMipmaps_s( | ||
| 125 | OpenGL::GL_TEXTURE_2D, | ||
| 126 | $self->{bytes}, | ||
| 127 | $self->{width}, | ||
| 128 | $self->{height}, | ||
| 129 | $self->{mask}, | ||
| 130 | OpenGL::GL_UNSIGNED_BYTE, | ||
| 131 | ${ $self->{surface}->get_pixels_ptr }, | ||
| 132 | ); | ||
| 133 | |||
| 134 | # Release the lock on the SDL surface | ||
| 135 | SDL::Video::unlock_surface($self->{surface}); | ||
| 136 | |||
| 137 | return 1; | ||
| 138 | } | ||
| 139 | |||
| 140 | sub display { | ||
| 141 | my $self = shift; | ||
| 142 | $self->SUPER::display(@_); | ||
| 143 | |||
| 144 | # Display the frame | ||
| 145 | OpenGL::glDisable( OpenGL::GL_LIGHTING ); | ||
| 146 | OpenGL::glColorf( 1, 1, 1, 1 ); | ||
| 147 | OpenGL::glBindTexture( OpenGL::GL_TEXTURE_2D, $self->{id} ); | ||
| 148 | OpenGL::glBegin( OpenGL::GL_QUADS ); | ||
| 149 | OpenGL::glTexCoord2f( 0, 0 ); OpenGL::glVertex3f( -0.5, 1, 0 ); # Top Left | ||
| 150 | OpenGL::glTexCoord2f( 0, 1 ); OpenGL::glVertex3f( -0.5, 0, 0 ); # Bottom Left | ||
| 151 | OpenGL::glTexCoord2f( 1, 1 ); OpenGL::glVertex3f( 0.5, 0, 0 ); # Bottom Right | ||
| 152 | OpenGL::glTexCoord2f( 1, 0 ); OpenGL::glVertex3f( 0.5, 1, 0 ); # Top Right | ||
| 153 | OpenGL::glEnd(); | ||
| 154 | OpenGL::glEnable( OpenGL::GL_LIGHTING ); | ||
| 155 | |||
| 156 | return 1; | ||
| 157 | } | ||
| 158 | |||
| 159 | 1; |