| 1 | package SDL::Tutorial::3DWorld::Texture; | ||
| 2 | |||
| 3 | =pod | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | SDL::Tutorial::3DWorld::Texture - A texture API simple enough for mere mortals | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | # Create the texture object (validating only the file exists) | ||
| 12 | my $texture = SDL::Tutorial::3DWorld::Texture->new( file => $file ); | ||
| 13 | |||
| 14 | # Load the texture into memory, ready for use in your program | ||
| 15 | $texture->init; | ||
| 16 | |||
| 17 | # Make this texture the active OpenGL texture for drawing | ||
| 18 | $texture->display; | ||
| 19 | |||
| 20 | =head1 DESCRIPTION | ||
| 21 | |||
| 22 | OpenGL textures are a large and complex topic, with a steep learning curve. | ||
| 23 | |||
| 24 | Most tutorials on texturing demonstrate a single specific use case, and | ||
| 25 | often implement their own image loaders in the process. Unlike most other | ||
| 26 | basic topics in OpenGL, texturing examples are difficult to translate into | ||
| 27 | working code for your own program (and even then the amount of code can | ||
| 28 | be rather large and crufty). | ||
| 29 | |||
| 30 | This module provides a convenient abstraction that streamlines the most | ||
| 31 | obvious case of reading an image file from disk, binding it to the OpenGL | ||
| 32 | environment, and then activating the texture so you can paint it onto | ||
| 33 | something. | ||
| 34 | |||
| 35 | =head1 METHODS | ||
| 36 | |||
| 37 | =cut | ||
| 38 | |||
| 39 | 2 | ▶ | use 5.008; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 40 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 41 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 42 | 0 | ▶ | use SDL::Image (); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 43 | use SDL::Video (); | ||
| 44 | use SDL::Tutorial::3DWorld::OpenGL (); | ||
| 45 | |||
| 46 | # SDL::Image creates SDL::Surface objects without loading their classes. | ||
| 47 | # Naughty, naughty, naughty! To correct the naughtyness we must load them. | ||
| 48 | use SDL::Surface (); | ||
| 49 | use SDL::PixelFormat (); | ||
| 50 | |||
| 51 | our $VERSION = '0.33'; | ||
| 52 | |||
| 53 | # Global Texture Cache. | ||
| 54 | # Since there are currently no optional texture settings and texture | ||
| 55 | # objects are immutable, we can do a simple file-based key for textures. | ||
| 56 | our %CACHE = (); | ||
| 57 | |||
| 58 | |||
| 59 | |||
| 60 | |||
| 61 | |||
| 62 | ###################################################################### | ||
| 63 | # Constructor and Accessors | ||
| 64 | |||
| 65 | =pod | ||
| 66 | |||
| 67 | =head2 new | ||
| 68 | |||
| 69 | # Load a texture from a shared file texture collection | ||
| 70 | my $chess = SDL::Tutorial::3DWorld::Texture->new( | ||
| 71 | file => File::Spec->catfile( | ||
| 72 | File::ShareDir::dist_dir('SDL-Tutorial-3DWorld'), | ||
| 73 | 'textures', | ||
| 74 | 'chessboard.png', | ||
| 75 | ), | ||
| 76 | ); | ||
| 77 | |||
| 78 | The C<new> constructor creates a new texture handle which identifies a | ||
| 79 | texture to be loaded from disk. | ||
| 80 | |||
| 81 | It takes a single named C<file> parameter which should be the path to | ||
| 82 | the texture on disk. While the C<new> constructor will validate that the | ||
| 83 | file exists, it will not attempt to load the image. Any image files that | ||
| 84 | are broken, corrupt or unsupported will not be identified until C<init> | ||
| 85 | is called. | ||
| 86 | |||
| 87 | =cut | ||
| 88 | |||
| 89 | sub new { | ||
| 90 | my $class = shift; | ||
| 91 | my $self = bless { | ||
| 92 | tile => 1, | ||
| 93 | @_, | ||
| 94 | }, $class; | ||
| 95 | |||
| 96 | # Make sure the filename is absolute so we have consistent keys | ||
| 97 | # for the global texture cache. Return from the cache if we can. | ||
| 98 | my $key = File::Spec->rel2abs( $self->file ); | ||
| 99 | return $CACHE{$key} if $CACHE{$key}; | ||
| 100 | |||
| 101 | # Check the file | ||
| 102 | unless ( -f $self->file ) { | ||
| 103 | die "Texture file '" . $self->file . "' does not exist"; | ||
| 104 | } | ||
| 105 | |||
| 106 | # Save the new texture to the global cache | ||
| 107 | $CACHE{$key} = $self; | ||
| 108 | |||
| 109 | return $self; | ||
| 110 | } | ||
| 111 | |||
| 112 | =pod | ||
| 113 | |||
| 114 | =head2 file | ||
| 115 | |||
| 116 | The C<file> accessor returns the path to the file the texture was | ||
| 117 | originally loaded from. | ||
| 118 | |||
| 119 | =cut | ||
| 120 | |||
| 121 | sub file { | ||
| 122 | $_[0]->{file}; | ||
| 123 | } | ||
| 124 | |||
| 125 | |||
| 126 | |||
| 127 | |||
| 128 | |||
| 129 | ###################################################################### | ||
| 130 | # Engine Methods | ||
| 131 | |||
| 132 | sub init { | ||
| 133 | my $self = shift; | ||
| 134 | |||
| 135 | # Shortcut if already initiated | ||
| 136 | return if defined $self->{id}; | ||
| 137 | |||
| 138 | # Use SDL to load the image | ||
| 139 | my $image = SDL::Image::load( $self->file ); | ||
| 140 | unless ( $image ) { | ||
| 141 | die "Cannot load image file '" . $self->file . "'"; | ||
| 142 | } | ||
| 143 | |||
| 144 | # Have OpenGL generate one texture object handle. | ||
| 145 | # This cannot occur between a glBegin and a glEnd, so all texture | ||
| 146 | # objects must be initialised before you start drawing something. | ||
| 147 | $self->{id} = OpenGL::glGenTextures_p(1); | ||
| 148 | |||
| 149 | # Bind the texture object for the first time, activating it | ||
| 150 | # as the "current" texture and confirming it as 2 dimensional. | ||
| 151 | OpenGL::glBindTexture( OpenGL::GL_TEXTURE_2D, $self->{id} ); | ||
| 152 | |||
| 153 | # Specify how the texture will display when we are far from the | ||
| 154 | # texture and many texture pixels are inside one display pixel. | ||
| 155 | # This example uses the fastest and ugliest GL_NEAREST setting. | ||
| 156 | # Default is GL_NEAREST_MIPMAP_LINEAR. | ||
| 157 | # Prettiest is probably going to be GL_LINEAR_MIPMAP_LINEAR | ||
| 158 | OpenGL::glTexParameterf( | ||
| 159 | OpenGL::GL_TEXTURE_2D, | ||
| 160 | OpenGL::GL_TEXTURE_MIN_FILTER, | ||
| 161 | OpenGL::GL_LINEAR_MIPMAP_LINEAR, # OpenGL::GL_NEAREST, | ||
| 162 | ); | ||
| 163 | |||
| 164 | # Specify the zoom method to use when we are too close to the | ||
| 165 | # texture and one texture pixel spreads over many display pixels. | ||
| 166 | # This example uses the fastest and ugliest GL_NEAREST setting. | ||
| 167 | # The default is GL_LINEAR (those are the only two options). | ||
| 168 | OpenGL::glTexParameterf( | ||
| 169 | OpenGL::GL_TEXTURE_2D, | ||
| 170 | OpenGL::GL_TEXTURE_MAG_FILTER, | ||
| 171 | defined($self->{mag_filter}) | ||
| 172 | ? $self->{mag_filter} | ||
| 173 | : OpenGL::GL_LINEAR, | ||
| 174 | ); | ||
| 175 | |||
| 176 | # Wrap the textures | ||
| 177 | OpenGL::glTexParameterf( | ||
| 178 | OpenGL::GL_TEXTURE_2D, | ||
| 179 | OpenGL::GL_TEXTURE_WRAP_S, | ||
| 180 | OpenGL::GL_REPEAT, | ||
| 181 | ) if $self->{tile}; | ||
| 182 | |||
| 183 | # Tell SDL to leave the memory the image is in exactly where | ||
| 184 | # it is, so that OpenGL can bind to it directly. Do this as | ||
| 185 | # late as possible so we keep it locked the minimum length of | ||
| 186 | # time. | ||
| 187 | SDL::Video::lock_surface($image); | ||
| 188 | |||
| 189 | # Does this image have a usable texture format? | ||
| 190 | my $bytes = $image->format->BytesPerPixel; | ||
| 191 | my $mask = undef; | ||
| 192 | if ( $bytes == 4 ) { | ||
| 193 | # Contains an alpha channel | ||
| 194 | if ( $image->format->Rmask == 0x000000ff ) { | ||
| 195 | $mask = OpenGL::GL_RGBA; | ||
| 196 | } else { | ||
| 197 | $mask = OpenGL::GL_BGRA; | ||
| 198 | } | ||
| 199 | } elsif ( $bytes == 3 ) { | ||
| 200 | # Does not contain an alpha channel | ||
| 201 | if ( $image->format->Rmask == 0x000000ff ) { | ||
| 202 | $mask = OpenGL::GL_RGB; | ||
| 203 | } else { | ||
| 204 | $mask = OpenGL::GL_BGR; | ||
| 205 | } | ||
| 206 | } else { | ||
| 207 | die "Unknown or unsupported image '" . $self->file . "'"; | ||
| 208 | } | ||
| 209 | |||
| 210 | # Write the image data into the texture, generating a mipmap for | ||
| 211 | # scaling as we do so (so it looks pretty no matter how far away | ||
| 212 | # it is). | ||
| 213 | OpenGL::gluBuild2DMipmaps_s( | ||
| 214 | OpenGL::GL_TEXTURE_2D, | ||
| 215 | $bytes, | ||
| 216 | $image->w, | ||
| 217 | $image->h, | ||
| 218 | $mask, | ||
| 219 | OpenGL::GL_UNSIGNED_BYTE, | ||
| 220 | ${ $image->get_pixels_ptr }, | ||
| 221 | ); | ||
| 222 | |||
| 223 | # Release the lock on the SDL surface | ||
| 224 | SDL::Video::unlock_surface($image); | ||
| 225 | |||
| 226 | # Save some image properties we might need later | ||
| 227 | $self->{width} = $image->w; | ||
| 228 | $self->{height} = $image->h; | ||
| 229 | $self->{bytes} = $bytes; | ||
| 230 | $self->{mask} = $mask; | ||
| 231 | |||
| 232 | return 1; | ||
| 233 | } | ||
| 234 | |||
| 235 | sub display { | ||
| 236 | # Rebind the texture using the previously allocated id | ||
| 237 | OpenGL::glBindTexture( OpenGL::GL_TEXTURE_2D, $_[0]->{id} ); | ||
| 238 | } | ||
| 239 | |||
| 240 | 1; | ||
| 241 | |||
| 242 | =cut | ||
| 243 | |||
| 244 | =pod | ||
| 245 | |||
| 246 | =head1 SUPPORT | ||
| 247 | |||
| 248 | Bugs should be reported via the CPAN bug tracker at | ||
| 249 | |||
| 250 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=SDL-Tutorial-3DWorld> | ||
| 251 | |||
| 252 | =head1 AUTHOR | ||
| 253 | |||
| 254 | Adam Kennedy E<lt>adamk@cpan.orgE<gt> | ||
| 255 | |||
| 256 | =head1 SEE ALSO | ||
| 257 | |||
| 258 | L<SDL>, L<OpenGL> | ||
| 259 | |||
| 260 | =head1 COPYRIGHT | ||
| 261 | |||
| 262 | Copyright 2010 Adam Kennedy. | ||
| 263 | |||
| 264 | This program is free software; you can redistribute | ||
| 265 | it and/or modify it under the same terms as Perl itself. | ||
| 266 | |||
| 267 | The full text of the license can be found in the | ||
| 268 | LICENSE file included with this module. | ||
| 269 | |||
| 270 | =cut | ||