Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

texture.cpp

00001 //FILE:         texture.cpp
00002 //AUTHOR:       Nathan Cournia <nathan@cournia.com>
00003 
00004 #ifdef WIN32
00005 #define WIN32_LEAN_AND_MEAN
00006 #include <windows.h>
00007 #endif
00008 
00009 #include <GL/glu.h>
00010 #include "image_loader.h"
00011 #include "texture.h"
00012 
00014 GLenum gl::texture::s_greyscale_format = GL_LUMINANCE;
00015 
00017 gl::texture
00018 gl::texture::load( const std::string& filename, bool build_mipmaps, 
00019         GLfloat min_filter, GLfloat max_filter )
00020 {
00021         //texture handle to return
00022         texture tex;
00023         
00024         //load image
00025         image::image_t *img = image::load( filename );
00026 
00027         //was the image loaded successfully
00028         if( !img ) {
00029                 //could not load image, set error
00030                 tex.m_handle = 0;
00031                 return tex;
00032         }
00033 
00034         //flip the image
00035         img->flip_horizontal( );
00036 
00037         //send the image to opengl
00038         tex = load( img, build_mipmaps, min_filter, max_filter );
00039 
00040         //delete local copy of image
00041         delete img;
00042 
00043         //return handle
00044         return tex;
00045 }
00046 
00048 gl::texture
00049 gl::texture::load( std::istream& stream, bool build_mipmaps, 
00050         GLfloat min_filter, GLfloat max_filter )
00051 {
00052         //texture handle to return
00053         texture tex;
00054 
00055         //load image
00056         image::image_t *img = image::load( stream );
00057 
00058         //was the image loaded successfully
00059         if( !img ) {
00060                 //could not load image, set error
00061                 tex.m_handle = 0;
00062                 return tex;
00063         }
00064 
00065         //flip the image
00066         img->flip_horizontal( );
00067         
00068         //send the image to opengl
00069         tex = load( img, build_mipmaps, min_filter, max_filter );
00070 
00071         //delete local copy of image
00072         delete img;
00073 
00074         //return handle
00075         return tex;
00076 }
00077 
00079 //this is the actual workhorse function to send images to the open subsystem
00080 gl::texture
00081 gl::texture::load( image::image_t* img, bool build_mipmaps, 
00082         GLfloat min_filter, GLfloat max_filter )
00083 {
00084         //texture handle to return
00085         texture tex;
00086         
00087         //make sure img is valid
00088         if( !img ) {
00089                 //image pointer is null
00090                 tex.m_handle = 0;
00091                 return tex;
00092         }
00093 
00094         //determine format of image
00095         GLenum format;
00096         if( img->get_format( ) == image::image_t::RGB ) {
00097                 format = GL_RGB;
00098         } else if( img->get_format( ) == image::image_t::RGBA ) {
00099                 format = GL_RGBA;
00100         } else if( img->get_format( ) == image::image_t::GREYSCALE ) {
00101                 format = s_greyscale_format;
00102         } else {
00103                 //unknown format
00104                 tex.m_handle = 0;
00105                 return tex;
00106         }
00107 
00108         //determine how the image data is stored
00109         GLenum type;
00110         if( img->get_bpc( ) == image::image_t::BYTE ) {
00111                 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
00112                 type = GL_UNSIGNED_BYTE;
00113         } else {
00114                 //TODO WORD and DWORD types
00115                 //unknown type
00116                 tex.m_handle = 0;
00117                 return tex;
00118         }
00119 
00120         //get a handle from opengl
00121         GLuint handle;
00122         glGenTextures( 1, &handle );
00123         glBindTexture( GL_TEXTURE_2D, handle );
00124 
00125         //should we generate mipmaps
00126         if( build_mipmaps ) {
00127                 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, min_filter );
00128                 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, max_filter );
00129                 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
00130                 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
00131                 gluBuild2DMipmaps( GL_TEXTURE_2D, format, img->get_width( ), 
00132                         img->get_height( ), format, type, img->get_pixels( ) );
00133         } else {
00134                 //don't build mipmaps
00135                 //TODO  We need a mechanism to support npot images and 
00136                 //      defining mipmapping levels.
00137                 //      This shouldn't be too hard to do but probably not 
00138                 //      worth the effort.
00139                 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, min_filter );
00140                 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, max_filter );
00141                 glTexImage2D( GL_TEXTURE_2D, 0, format, img->get_width( ), 
00142                         img->get_height( ), 0, format, type, img->get_pixels( ) ); 
00143         }
00144 
00145         //set the aspect ratio
00146         tex.m_aspect = static_cast<real_t>( img->get_width( ) ) / 
00147                 static_cast<real_t>( img->get_height( ) );
00148 
00149         //set the handle and return
00150         tex.m_handle = handle;
00151         return tex;
00152 }

Generated on Tue Feb 11 18:49:42 2003 for uber by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002